root/ompi/mca/common/ompio/common_ompio_buffer.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mca_common_ompio_check_gpu_buf
  2. mca_common_ompio_buffer_alloc_seg
  3. mca_common_ompio_buffer_free_seg
  4. mca_common_ompio_buffer_alloc_init
  5. mca_common_ompio_buffer_alloc_fini
  6. mca_common_ompio_alloc_buf
  7. mca_common_ompio_release_buf

   1 /*
   2  *  Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                          University Research and Technology
   4  *                          Corporation.  All rights reserved.
   5  *  Copyright (c) 2004-2016 The University of Tennessee and The University
   6  *                          of Tennessee Research Foundation.  All rights
   7  *                          reserved.
   8  *  Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                          University of Stuttgart.  All rights reserved.
  10  *  Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                          All rights reserved.
  12  *  Copyright (c) 2008-2019 University of Houston. All rights reserved.
  13  *  $COPYRIGHT$
  14  *
  15  *  Additional copyrights may follow
  16  *
  17  *  $HEADER$
  18  */
  19 
  20 #include "ompi_config.h"
  21 
  22 #include "opal/datatype/opal_convertor.h"
  23 #include "opal/datatype/opal_datatype_cuda.h"
  24 #include "opal/mca/common/cuda/common_cuda.h"
  25 #include "opal/util/sys_limits.h"
  26 
  27 #include "opal/mca/allocator/allocator.h"
  28 #include "opal/mca/allocator/base/base.h"
  29 #include "common_ompio.h"
  30 #include "common_ompio_buffer.h"
  31 
  32 
  33 static opal_mutex_t     mca_common_ompio_buffer_mutex;      /* lock for thread safety */
  34 static mca_allocator_base_component_t* mca_common_ompio_allocator_component=NULL;
  35 static mca_allocator_base_module_t* mca_common_ompio_allocator=NULL;  
  36 
  37 static opal_atomic_int32_t  mca_common_ompio_buffer_init = 0;
  38 static int32_t  mca_common_ompio_pagesize=4096;
  39 static void* mca_common_ompio_buffer_alloc_seg ( void *ctx, size_t *size );
  40 static void mca_common_ompio_buffer_free_seg ( void *ctx, void *buf );
  41 
  42 #if OPAL_CUDA_SUPPORT
  43 void mca_common_ompio_check_gpu_buf ( ompio_file_t *fh, const void *buf, int *is_gpu, 
  44                                       int *is_managed)
  45 {
  46     opal_convertor_t    convertor;  
  47     
  48     *is_gpu=0;
  49     *is_managed=0;
  50     
  51     convertor.flags=0;
  52     if ( opal_cuda_check_one_buf ( (char *)buf, &convertor ) ) {
  53         *is_gpu = 1;
  54         if ( convertor.flags & CONVERTOR_CUDA_UNIFIED ){
  55             *is_managed =1;
  56         }
  57     } 
  58     
  59     return;
  60 }
  61 #endif
  62 
  63 static void* mca_common_ompio_buffer_alloc_seg ( void*ctx, size_t *size )
  64 {
  65     char *buf=NULL;
  66     size_t realsize, numpages;
  67 
  68     numpages = (*size + mca_common_ompio_pagesize -1 )/mca_common_ompio_pagesize;
  69     realsize = numpages * mca_common_ompio_pagesize;
  70 
  71     buf = malloc ( realsize);
  72 #if OPAL_CUDA_SUPPORT
  73     if ( NULL != buf ) {
  74         mca_common_cuda_register ( ( char *)buf, realsize, NULL  );
  75     }
  76 #endif
  77     *size = realsize;
  78     return buf;
  79 }
  80 
  81 static void mca_common_ompio_buffer_free_seg ( void *ctx, void *buf )
  82 {
  83     if ( NULL != buf ) {
  84 #if OPAL_CUDA_SUPPORT
  85         mca_common_cuda_unregister ( (char *) buf, NULL );
  86 #endif
  87         free ( buf );
  88     }
  89     return;
  90 }
  91 
  92 int mca_common_ompio_buffer_alloc_init ( void )
  93 {
  94     bool thread_safe=true;
  95 
  96     if(OPAL_THREAD_ADD_FETCH32(&mca_common_ompio_buffer_init, 1) > 1)
  97         return OMPI_SUCCESS;
  98 
  99     /* initialize static objects */
 100     OBJ_CONSTRUCT(&mca_common_ompio_buffer_mutex, opal_mutex_t);
 101 
 102     OPAL_THREAD_LOCK (&mca_common_ompio_buffer_mutex );
 103     /* lookup name of the allocator to use */
 104     if(NULL == (mca_common_ompio_allocator_component = mca_allocator_component_lookup("basic"))) {
 105         OPAL_THREAD_UNLOCK(&mca_common_ompio_buffer_mutex);
 106         return OMPI_ERR_BUFFER;
 107     }
 108 
 109     /* create an instance of the allocator */
 110     mca_common_ompio_allocator = mca_common_ompio_allocator_component->allocator_init(thread_safe, 
 111                                                                                       mca_common_ompio_buffer_alloc_seg, 
 112                                                                                       mca_common_ompio_buffer_free_seg, 
 113                                                                                       NULL);
 114     if(NULL == mca_common_ompio_allocator) {
 115         OPAL_THREAD_UNLOCK(&mca_common_ompio_buffer_mutex);
 116         return OMPI_ERR_BUFFER;
 117     }
 118 
 119     mca_common_ompio_pagesize = opal_getpagesize();
 120 
 121     OPAL_THREAD_UNLOCK(&mca_common_ompio_buffer_mutex);
 122     return OMPI_SUCCESS;
 123 }
 124 
 125 int mca_common_ompio_buffer_alloc_fini ( void )
 126 {
 127     if ( NULL != mca_common_ompio_allocator ) {
 128         OPAL_THREAD_LOCK (&mca_common_ompio_buffer_mutex);
 129         mca_common_ompio_allocator->alc_finalize(mca_common_ompio_allocator);
 130         mca_common_ompio_allocator=NULL;
 131         OPAL_THREAD_UNLOCK (&mca_common_ompio_buffer_mutex);
 132         OBJ_DESTRUCT (&mca_common_ompio_buffer_mutex);
 133     }
 134 
 135     return OMPI_SUCCESS;
 136 }
 137 
 138 void *mca_common_ompio_alloc_buf ( ompio_file_t *fh, size_t bufsize )
 139 {
 140     char *tmp=NULL;
 141 
 142     if ( !mca_common_ompio_buffer_init ){
 143         mca_common_ompio_buffer_alloc_init ();
 144     }
 145     
 146     OPAL_THREAD_LOCK (&mca_common_ompio_buffer_mutex);
 147     tmp = mca_common_ompio_allocator->alc_alloc (mca_common_ompio_allocator,
 148                                                  bufsize, 0 );
 149     OPAL_THREAD_UNLOCK (&mca_common_ompio_buffer_mutex);
 150     return tmp;
 151 }
 152 
 153 void mca_common_ompio_release_buf ( ompio_file_t *fh, void *buf )
 154 {
 155 
 156     if ( !mca_common_ompio_buffer_init ){
 157         /* Should not happen. You can not release a buf without
 158         ** having it allocated first. 
 159         */
 160         opal_output (1, "error in mca_common_ompio_release_buf: allocator not initialized\n");
 161     }
 162 
 163     OPAL_THREAD_LOCK (&mca_common_ompio_buffer_mutex);
 164     mca_common_ompio_allocator->alc_free (mca_common_ompio_allocator,
 165                                           buf);
 166     OPAL_THREAD_UNLOCK (&mca_common_ompio_buffer_mutex);
 167 
 168     return;
 169 }
 170 

/* [<][>][^][v][top][bottom][index][help] */