~ubuntu-branches/ubuntu/trusty/nwchem/trusty-proposed

« back to all changes in this revision

Viewing changes to src/tools/ga-5-1/armci/src-gemini/shmem.c

  • Committer: Package Import Robot
  • Author(s): Michael Banck, Daniel Leidert, Andreas Tille, Michael Banck
  • Date: 2013-07-04 12:14:55 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130704121455-5tvsx2qabor3nrui
Tags: 6.3-1
* New upstream release.
* Fixes anisotropic properties (Closes: #696361).
* New features include:
  + Multi-reference coupled cluster (MRCC) approaches
  + Hybrid DFT calculations with short-range HF 
  + New density-functionals including Minnesota (M08, M11) and HSE hybrid
    functionals
  + X-ray absorption spectroscopy (XAS) with TDDFT
  + Analytical gradients for the COSMO solvation model
  + Transition densities from TDDFT 
  + DFT+U and Electron-Transfer (ET) methods for plane wave calculations
  + Exploitation of space group symmetry in plane wave geometry optimizations
  + Local density of states (LDOS) collective variable added to Metadynamics
  + Various new XC functionals added for plane wave calculations, including
    hybrid and range-corrected ones
  + Electric field gradients with relativistic corrections 
  + Nudged Elastic Band optimization method
  + Updated basis sets and ECPs 

[ Daniel Leidert ]
* debian/watch: Fixed.

[ Andreas Tille ]
* debian/upstream: References

[ Michael Banck ]
* debian/upstream (Name): New field.
* debian/patches/02_makefile_flags.patch: Refreshed.
* debian/patches/06_statfs_kfreebsd.patch: Likewise.
* debian/patches/07_ga_target_force_linux.patch: Likewise.
* debian/patches/05_avoid_inline_assembler.patch: Removed, no longer needed.
* debian/patches/09_backported_6.1.1_fixes.patch: Likewise.
* debian/control (Build-Depends): Added gfortran-4.7 and gcc-4.7.
* debian/patches/10_force_gcc-4.7.patch: New patch, explicitly sets
  gfortran-4.7 and gcc-4.7, fixes test suite hang with gcc-4.8 (Closes:
  #701328, #713262).
* debian/testsuite: Added tests for COSMO analytical gradients and MRCC.
* debian/rules (MRCC_METHODS): New variable, required to enable MRCC methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#if HAVE_CONFIG_H
2
 
#   include "config.h"
3
 
#endif
4
 
 
5
 
/* $Id: shmem.c,v 1.87.2.2 2007-09-10 23:31:32 manoj Exp $ */
6
 
/* System V shared memory allocation and managment
7
 
 *
8
 
 * Interface:
9
 
 * ~~~~~~~~~
10
 
 *  char *Create_Shared_Region(long *idlist, long size, long *offset)
11
 
 *       . to be called by just one process. 
12
 
 *       . calls kr_malloc,  malloc-like memory allocator from the K&R book. 
13
 
 *         kr_malloc inturn calls armci_allocate() that does shmget() and shmat(). 
14
 
 *       . idlist might be just a pointer to integer or a true array in the
15
 
 *         MULTIPLE_REGIONS versions (calling routine has to take care of it) 
16
 
 *  char *Attach_Shared_Region(long *idlist, long size, long offset)
17
 
 *       . called by any other process to attach to existing shmem region or
18
 
 *         if attached just calculate the address based on the offset
19
 
 *       . has to be called after shmem region was created
20
 
 *  void  Free_Shmem_Ptr(long id, long size, char* addr)
21
 
 *       . called ONLY by the process that created shmem region (id) to return
22
 
 *         pointer to kr_malloc (shmem is not destroyed)
23
 
 *  void  Delete_All_Regions()
24
 
 *       . destroys all shared memory regions
25
 
 *       . can be called by any process assuming that all processes attached
26
 
 *         to alllocated shmem regions 
27
 
 *       . needs to by called by cleanup procedure(s)
28
 
 *
29
 
 * Jarek Nieplocha, 06.13.94
30
 
 * 
31
 
 */
32
 
 
33
 
#ifdef SYSV
34
 
 
35
 
 
36
 
#define DEBUG_ 0
37
 
#define DEBUG1 0
38
 
#define DEBUG2_ 0
39
 
 
40
 
/* For debugging purposes at the beginning of the shared memory region
41
 
 * creator process can write a stamp which then is read by attaching processes
42
 
 * NOTE: on clusters we cannot use it anymore since ARMCI node master
43
 
 * uses it since Nov 99 to write the value of address it attached at
44
 
 * This feature is used in the ARMCI memlock table.
45
 
 */
46
 
#define STAMP 0
47
 
 
48
 
 
49
 
#include <sys/types.h>
50
 
#include <sys/ipc.h>
51
 
#include <sys/shm.h>
52
 
#include <sys/sem.h>
53
 
#include <sys/param.h>
54
 
#include <errno.h>
55
 
#include <stdio.h>
56
 
#include <stdlib.h>
57
 
#include "shmem.h"
58
 
#include "kr_malloc.h"
59
 
#include "shmlimit.h"
60
 
#include "message.h"
61
 
#include "armcip.h"
62
 
 
63
 
#ifdef   ALLOC_MUNMAP
64
 
#include <sys/mman.h>
65
 
#include <unistd.h>
66
 
static  size_t pagesize=0;
67
 
static  int logpagesize=0;
68
 
/* allow only that big shared memory segment (in MB)- incresed from 128 11/02 */
69
 
#define MAX_ALLOC_MUNMAP 128
70
 
#define MAX_ALLOC_MUNMAP_ 368
71
 
static long max_alloc_munmap=MAX_ALLOC_MUNMAP;
72
 
#endif
73
 
 
74
 
#if defined(SUN)
75
 
  extern char *shmat();
76
 
#endif
77
 
 
78
 
#define SHM_UNIT (1024)
79
 
 
80
 
 
81
 
/* Need to determine the max shmem segment size. There are 2 alternatives:
82
 
 * 1. use predefined SHMMAX if available or set some reasonable values, or
83
 
 * 2. trial-and-error search for a max value (default)
84
 
 *    case a) fork a process to determine shmmax size (more accurate)
85
 
 *    case b) search w/o forking until success (less accurate)
86
 
 */
87
 
 
88
 
/* under Myrinet GM, we cannot fork */
89
 
#if defined(GM) || defined(VAPI)
90
 
#   define SHMMAX_SEARCH_NO_FORK 
91
 
#endif
92
 
#if defined(LAPI) || defined(AIX) || defined(SHMMAX_SEARCH_NO_FORK) || defined(CRAY_XT)
93
 
#   define NO_SHMMAX_SEARCH
94
 
#endif
95
 
 
96
 
/* Limits for the largest shmem segment are in Kilobytes to avoid passing
97
 
 * Gigavalues to kr_malloc
98
 
 * the limit for the KSR is lower than SHMMAX in sys/param.h because
99
 
 * shmat would fail -- SHMMAX cannot be trusted (a bug)
100
 
 */
101
 
#define _SHMMAX 4*1024
102
 
 
103
 
#if defined(SUN)||defined(SOLARIS)
104
 
#  undef _SHMMAX
105
 
#  define _SHMMAX (1024)  /* memory in KB */
106
 
#elif defined(SGI64) || defined(AIX) || defined(CONVEX)
107
 
#  undef _SHMMAX
108
 
#  define _SHMMAX ((unsigned long)512*1024)
109
 
#elif defined(SGI) && !defined(SGI64)
110
 
#  undef _SHMMAX
111
 
#  define _SHMMAX ((unsigned long)128*1024)
112
 
#elif defined(KSR)
113
 
#  undef _SHMMAX
114
 
#  define _SHMMAX ((unsigned long)512*1024)
115
 
#elif defined(HPUX)
116
 
#  undef _SHMMAX
117
 
#  define _SHMMAX ((unsigned long)64*1024)
118
 
#elif defined(__FreeBSD__)
119
 
#  undef _SHMMAX
120
 
#  define _SHMMAX ((unsigned long)3*1024)
121
 
#elif defined(LINUX) 
122
 
#  if !defined(SHMMAX) /* Red Hat does not define SHMMAX */
123
 
#     undef _SHMMAX
124
 
#     if defined(__sparc__) || defined(__powerpc__) 
125
 
#       define _SHMMAX ((unsigned long)16*1024)
126
 
#     elif defined(__alpha__)
127
 
#       define _SHMMAX ((unsigned long)4072)
128
 
#     else
129
 
        /* Intel */
130
 
#       define _SHMMAX ((unsigned long)32*1024)
131
 
#     endif
132
 
#  endif
133
 
#elif defined(SHMMAX)
134
 
#  undef _SHMMAX
135
 
#  define _SHMMAX (((unsigned long)SHMMAX)>>10)
136
 
#endif
137
 
 
138
 
static  unsigned long MinShmem = _SHMMAX;  
139
 
static  unsigned long MaxShmem = MAX_REGIONS*_SHMMAX;
140
 
static  context_t ctx_shmem; /* kr_malloc context */ 
141
 
static  context_t *ctx_shmem_global; /* kr_malloc context stored in shmem */
142
 
static  int create_call=0;
143
 
 
144
 
#ifdef  SHMMAX_SEARCH_NO_FORK
145
 
static  char *ptr_search_no_fork = (char*)0;
146
 
static  int id_search_no_fork=0;
147
 
#endif
148
 
 
149
 
 
150
 
#ifdef LINUX
151
 
#define CLEANUP_CMD(command) sprintf(command,"/usr/bin/ipcrm shm %d",id);
152
 
#elif  defined(SOLARIS) 
153
 
#define CLEANUP_CMD(command) sprintf(command,"/bin/ipcrm -m %d",id);
154
 
#elif  defined(SGI) 
155
 
#define CLEANUP_CMD(command) sprintf(command,"/usr/sbin/ipcrm -m %d",id);
156
 
#else
157
 
#define CLEANUP_CMD(command) sprintf(command,"/usr/bin/ipcrm -m %d",id);
158
 
#endif
159
 
 
160
 
 
161
 
#ifdef   ALLOC_MUNMAP
162
 
#ifdef QUADRICS
163
 
#  include <elan/elan.h>
164
 
#  include <elan3/elan3.h>
165
 
   static  char *armci_elan_starting_address = (char*)0;
166
 
 
167
 
#  ifdef __ia64__
168
 
#    define ALLOC_MUNMAP_ALIGN 1024*1024
169
 
#  else
170
 
#    define ALLOC_MUNMAP_ALIGN 64*1024
171
 
#  endif
172
 
 
173
 
#  define ALGN_MALLOC(s,a) elan_allocMain(elan_base->state, (a), (s))
174
 
#else 
175
 
#  define ALGN_MALLOC(s,a) malloc((s))
176
 
#endif
177
 
 
178
 
static char* alloc_munmap(size_t size)
179
 
{
180
 
char *tmp;
181
 
unsigned long iptr;
182
 
size_t bytes = size+pagesize-1;
183
 
 
184
 
    if(armci_elan_starting_address){
185
 
       tmp = armci_elan_starting_address;
186
 
       armci_elan_starting_address += size; 
187
 
#      ifdef ALLOC_MUNMAP_ALIGN
188
 
         armci_elan_starting_address += ALLOC_MUNMAP_ALIGN;
189
 
#      endif
190
 
       if(DEBUG_) {printf("%d: address for shm attachment is %p size=%ld\n",
191
 
                         armci_me,tmp,(long)size); fflush(stdout); }
192
 
    } else {
193
 
      tmp = ALGN_MALLOC(bytes, getpagesize());
194
 
      if(tmp){
195
 
        iptr = (unsigned long)tmp + pagesize-1;
196
 
        iptr >>= logpagesize; iptr <<= logpagesize;
197
 
        if(DEBUG_) printf("%d:unmap ptr=%p->%p size=%d pagesize=%d\n",armci_me, 
198
 
                          tmp,(char*)iptr,(int)size,pagesize);
199
 
        tmp = (char*)iptr;
200
 
        if(munmap(tmp, size) == -1) armci_die("munmap failed",0);
201
 
        if(DEBUG_){printf("%d: unmap OK\n",armci_me); fflush(stdout);}
202
 
      }else armci_die("alloc_munmap: malloc failed",(int)size);
203
 
    }
204
 
    return tmp;
205
 
}
206
 
#endif
207
 
 
208
 
/*\ A wrapper to shmget. Just to be sure that ID is not 0.
209
 
\*/
210
 
int armci_shmget(size_t size,char *from)
211
 
{
212
 
int id;
213
 
 
214
 
    id = shmget(IPC_PRIVATE, size, (IPC_CREAT | 00600));
215
 
 
216
 
    /*attaching with id 0 somehow fails (Seen on pentium4+linux24+gm163)
217
 
     *so if id=0, shmget again. */
218
 
    while(id==0){
219
 
       /* free id=0 and get a new one */
220
 
       if(shmctl((int)id,IPC_RMID,(struct shmid_ds *)NULL)) {
221
 
         fprintf(stderr,"id=%d \n",id);
222
 
         armci_die("allocate: failed to _delete_ shared region ",id);
223
 
       }
224
 
       id = shmget(IPC_PRIVATE, size, (IPC_CREAT | 00600));
225
 
    }
226
 
    if(DEBUG_){
227
 
       printf("\n%d:armci_shmget sz=%ld caller=%s id=%d\n",armci_me,(long)size,
228
 
               from,id);
229
 
       fflush(stdout);
230
 
    }
231
 
    return(id);
232
 
}
233
 
 
234
 
static int 
235
 
Semget(key_t key,int nsems,int semflg) {
236
 
      int ret;
237
 
 
238
 
      if((ret = semget(key,nsems,semflg)) == -1) {
239
 
         switch(errno) {
240
 
           case EACCES: fprintf(stdout," semget errno=EACCES.\n"); break;
241
 
           case EINVAL: fprintf(stdout," semget errno=EINVAL.\n"); break;
242
 
           case ENOENT: fprintf(stdout," semget errno=ENOENT.\n"); break;
243
 
           case ENOSPC: fprintf(stdout," semget errno=ENOSPC -- check system limit for sysv semaphores.\n"); break;
244
 
           case ENOMEM: fprintf(stdout," semget errno=ENOMEM.\n"); break;
245
 
           case EEXIST: fprintf(stdout," semget errno=EEXIST.\n"); break;
246
 
           default:
247
 
             fprintf(stdout," unusual semget errno=%d\n",errno); break;
248
 
         }
249
 
         armci_die("semget failed",errno);
250
 
      }
251
 
 
252
 
      return ret;
253
 
}
254
 
 
255
 
 
256
 
int armci_semget(int count) 
257
 
{
258
 
        int id;
259
 
        id = Semget(IPC_PRIVATE,2,0600);
260
 
        return id;
261
 
}
262
 
 
263
 
int armci_semrm(int id) 
264
 
{
265
 
        semctl(id,0,IPC_RMID);
266
 
}
267
 
 
268
 
int armci_shmrm(int id)
269
 
{
270
 
        int ret;
271
 
        if((ret = shmctl(id,IPC_RMID,NULL)) != 0) {
272
 
          fprintf(stdout,"[cp]: shmctl return an error.\n");
273
 
          switch(errno) {
274
 
            case EINVAL:
275
 
              fprintf(stdout," Error EINVAL: shmid is not a valid shared memory segment.\n");
276
 
              break;
277
 
            case EFAULT:
278
 
              fprintf(stdout," Error EFAULT: argument 3 is not a valid struct shmid_ds.\n");
279
 
              break;
280
 
            case EPERM:
281
 
              fprintf(stdout," Error EPREM: permission to access/change shared mem segment denied.\n");
282
 
              break;
283
 
            default:
284
 
              fprintf(stdout," unusual shmctl errno=%d\n",errno); break;
285
 
              break;
286
 
            }
287
 
            armci_die("error deleting shmid",id);
288
 
        }
289
 
        return ret;
290
 
}
291
 
 
292
 
/*\ test is a shared memory region of a specified size can be allocated
293
 
 *  return 0 (no) or 1 (yes)
294
 
\*/
295
 
int armci_test_allocate(long size)
296
 
{
297
 
   char *ptr;
298
 
   int id = armci_shmget((size_t)size,"armci_test_allocate");
299
 
   if (id <0) return 0;
300
 
 
301
 
   /* attach to segment */
302
 
   ptr =  shmat(id, (char *) NULL, 0);
303
 
 
304
 
   /* delete segment id */
305
 
   if(shmctl( id, IPC_RMID, (struct shmid_ds *)NULL))
306
 
      fprintf(stderr,"failed to remove shm id=%d\n",id);
307
 
 
308
 
   /* test pointer */
309
 
   if (((long)ptr) == -1L) return 0;
310
 
   else return 1;
311
 
}
312
 
 
313
 
 
314
 
/*\ try to allocate a shared memory region of a specified size; return pointer
315
 
\*/
316
 
static int armci_shmalloc_try(long size)
317
 
{
318
 
#ifdef  SHMMAX_SEARCH_NO_FORK
319
 
   char *ptr;
320
 
   int id = armci_shmget((size_t) size,"armci_shmalloc_try");
321
 
   if (id <0) return 0;
322
 
 
323
 
   /* attach to segment */
324
 
   ptr =  shmat(id, (char *) NULL, 0);
325
 
 
326
 
   /* test pointer */
327
 
   if (((long)ptr) == -1L) return 0;
328
 
 
329
 
   ptr_search_no_fork = ptr;
330
 
   id_search_no_fork = id;
331
 
#endif
332
 
   return 1;
333
 
}
334
 
 
335
 
 
336
 
 
337
 
 
338
 
/* parameters that define range and granularity of search for shm segment size
339
 
 * UBOUND is chosen to be < 2GB to avoid overflowing on 32-bit systems
340
 
 * smaller PAGE gives more accurate results but with more search steps
341
 
 * LBOUND  is set to minimum amount for our purposes
342
 
 * change UBOUND=512MB if you need larger arrays than 512 MB
343
 
 */
344
 
#define PAGE (16*65536L)
345
 
#define LBOUND  1048576L
346
 
#if defined(MULTI_CTX) && defined(QUADRICS)
347
 
#define UBOUND 256*LBOUND
348
 
#else
349
 
#define UBOUND 512*LBOUND
350
 
#endif
351
 
 
352
 
static long get_user_shmmax()
353
 
{
354
 
char *uval;
355
 
long x=0;
356
 
     uval = getenv("ARMCI_DEFAULT_SHMMAX"); 
357
 
     if(uval != NULL){
358
 
       sscanf(uval,"%ld",&x);
359
 
       if(x<1L || x> 2048L){ 
360
 
          fprintf(stderr,"incorrect ARMCI_DEFAULT_SHMMAX should be <1,2048>mb and 2^N Found=%ld\n",x);
361
 
          x=0;
362
 
       }
363
 
     }
364
 
     return x*1048576; /* return value in bytes */
365
 
}
366
 
 
367
 
/*\ determine the max shmem segment size using bisection
368
 
\*/
369
 
int armci_shmem_test()
370
 
{
371
 
long x;
372
 
int  i,rc;
373
 
long upper_bound=UBOUND;
374
 
long lower_bound=0;
375
 
 
376
 
     x = get_user_shmmax();
377
 
     if(!x) x = upper_bound;
378
 
     else upper_bound =x;
379
 
     
380
 
     if(DEBUG_){printf("%d: x = %ld upper_bound=%ld\n",armci_me, x, upper_bound); fflush(stdout);}
381
 
 
382
 
     for(i=1;;i++){
383
 
        long step;
384
 
        rc = armci_test_allocate(x);
385
 
        if(DEBUG_) 
386
 
           printf("%d:test %d size=%ld bytes status=%d\n",armci_me,i,x,rc);
387
 
        if(rc){
388
 
          lower_bound = x;
389
 
          step = (upper_bound -x)>>1;
390
 
          if(step < PAGE) break;
391
 
          x += step;
392
 
        }else{
393
 
          upper_bound = x;
394
 
          step = (x-lower_bound)>>1;
395
 
          if(step<PAGE) break;
396
 
          x -= step;
397
 
        }
398
 
        /* round it up to a full base-2 MB */
399
 
        x += 1048576L -1L;
400
 
        x >>=20;
401
 
        x <<=20; 
402
 
      }
403
 
 
404
 
      if(!lower_bound){
405
 
          /* try if can get LBOUND - necessary if search starts from UBOUND */
406
 
          lower_bound=LBOUND;
407
 
          rc = armci_test_allocate(lower_bound);
408
 
          if(!rc) return(0);
409
 
      }
410
 
 
411
 
      if(DEBUG_) printf("%ld bytes segment size, %d calls \n",lower_bound,i);
412
 
      return (int)( lower_bound>>20); /* return shmmax in mb */
413
 
}
414
 
 
415
 
 
416
 
#ifdef SHMMAX_SEARCH_NO_FORK
417
 
/*\ determine the max shmem segment size by halving
418
 
\*/
419
 
static int armci_shmem_test_no_fork()                          
420
 
{
421
 
long x;                                                     
422
 
int  i,rc;
423
 
long lower_bound=_SHMMAX*SHM_UNIT;
424
 
#define UBOUND_SEARCH_NO_FORK (256*SHM_UNIT*SHM_UNIT)
425
 
 
426
 
     x = get_user_shmmax();
427
 
     if(!x) x = UBOUND_SEARCH_NO_FORK;
428
 
 
429
 
     for(i=1;;i++){
430
 
 
431
 
        rc = armci_shmalloc_try(x);
432
 
        if(DEBUG_)
433
 
           printf("%d:test by halving size=%ld bytes rc=%d\n",armci_me,x,rc);
434
 
 
435
 
        if(rc){
436
 
          lower_bound = x;
437
 
          break;
438
 
        }else{
439
 
          x >>= 1 ;
440
 
          if(x<lower_bound) break;
441
 
        }
442
 
     }
443
 
 
444
 
     if(DEBUG_) printf("%ld: shmax test no fork: bytes segment size, %d calls \n",lower_bound,i);
445
 
     return (int)( lower_bound>>20); /* return shmmax in mb */
446
 
}
447
 
#endif
448
 
 
449
 
 
450
 
#ifdef MULTI_CTX
451
 
void armci_nattach_preallocate_info(int* segments, int *segsize)
452
 
{
453
 
     int x;
454
 
     char *uval;
455
 
     uval = getenv("LIBELAN_NATTACH");
456
 
     if(uval != NULL){
457
 
        sscanf(uval,"%d",&x);
458
 
        if(x<2 || x>8) armci_die("Error in LIBELAN_NATTACH <8, >1 ",(int)x);
459
 
     }else
460
 
        armci_die("Inconsistent configuration: ARMCI needs LIBELAN_NATTACH",0);
461
 
     *segments =x;
462
 
     *segsize = (int) (SHM_UNIT * MinShmem);
463
 
 
464
 
}
465
 
#endif
466
 
        
467
 
/* Create shared region to store kr_malloc context in shared memory */
468
 
void armci_krmalloc_init_ctxshmem() {
469
 
    void *myptr=NULL;
470
 
    long idlist[SHMIDLEN];
471
 
    long size; 
472
 
    int offset = sizeof(void*)/sizeof(int);
473
 
 
474
 
    /* to store shared memory context and  myptr */
475
 
    size = SHMEM_CTX_MEM;
476
 
    
477
 
    if(armci_me == armci_master ){
478
 
       myptr = Create_Shared_Region(idlist+1,size,idlist);
479
 
       if(!myptr && size>0 ) armci_die("armci_krmalloc_init_ctxshmem: could not create", (int)(size>>10));
480
 
       if(size) *(volatile void**)myptr = myptr;
481
 
       if(DEBUG_){
482
 
          printf("%d:armci_krmalloc_init_ctxshmem addr mptr=%p ref=%p size=%ld\n", armci_me, myptr, *(void**)myptr, size);
483
 
          fflush(stdout);
484
 
       }
485
 
       
486
 
       /* Bootstrapping: allocate storage for ctx_shmem_global. NOTE:there is 
487
 
          offset,as master places its address at begining for others to see */
488
 
       ctx_shmem_global = (context_t*) ( ((int*)myptr)+offset );
489
 
       *ctx_shmem_global = ctx_shmem; /*master copies ctx into shared region */
490
 
    }
491
 
 
492
 
    /* broadcast shmem id to other processes on the same cluster node */
493
 
    armci_msg_clus_brdcst(idlist, SHMIDLEN*sizeof(long));
494
 
 
495
 
    if(armci_me != armci_master){
496
 
       myptr=(double*)Attach_Shared_Region(idlist+1,size,idlist[0]);
497
 
       if(!myptr)armci_die("armci_krmalloc_init_ctxshmem: could not attach", (int)(size>>10));
498
 
       
499
 
       /* now every process in a SMP node needs to find out its offset
500
 
        * w.r.t. master - this offset is necessary to use memlock table
501
 
        */
502
 
       if(size) armci_set_mem_offset(myptr);
503
 
       if(DEBUG_){
504
 
          printf("%d:armci_krmalloc_init_ctxshmem attached addr mptr=%p ref=%p size=%ld\n", armci_me,myptr, *(void**)myptr,size); fflush(stdout);
505
 
       }
506
 
       /* store context info */
507
 
       ctx_shmem_global = (context_t*) ( ((int*)myptr)+offset );
508
 
       if(DEBUG_){
509
 
          printf("%d:armci_krmalloc_init_ctxshmem: shmid=%d off=%ld size=%ld\n", armci_me, ctx_shmem_global->shmid, ctx_shmem_global->shmoffset,
510
 
                 (long)ctx_shmem_global->shmsize);
511
 
          fflush(stdout);
512
 
       }
513
 
    }
514
 
}
515
 
 
516
 
void armci_shmem_init()
517
 
{
518
 
 
519
 
#ifdef ALLOC_MUNMAP
520
 
 
521
 
#if defined(QUADRICS) 
522
 
#   if (defined(__ia64__) || defined(__alpha)) && !defined(DECOSF) 
523
 
 
524
 
      /* this is to determine size of Elan Main memory allocator for munmap */
525
 
      long x;
526
 
      char *uval;
527
 
      uval = getenv("LIBELAN_ALLOC_SIZE");
528
 
      if(uval != NULL){
529
 
        sscanf(uval,"%ld",&x);
530
 
        if((x>80000000) && (x< 4*1024*1024*1024L)){ 
531
 
          max_alloc_munmap = (x>>20) - 72;
532
 
          if(DEBUG_){
533
 
            printf("%d: max_alloc_munmap is %ld\n",armci_me,max_alloc_munmap);
534
 
            fflush(stdout);
535
 
          }
536
 
        }
537
 
      }
538
 
 
539
 
      /* an alternative approach is to use MMAP area where we get
540
 
         the address from the Elan environment variable in qsnetlibs 1.4+  */
541
 
      uval = getenv("LIBELAN3_MMAPBASE");
542
 
      if(uval != NULL){
543
 
         sscanf(uval,"%p",&armci_elan_starting_address);
544
 
      }
545
 
 
546
 
#   endif
547
 
#   if defined(__ia64__)
548
 
       /* need aligment on 1MB boundary rather than the actual pagesize */
549
 
       pagesize = 1024*1024;
550
 
       logpagesize = 20;
551
 
#   else
552
 
       /* determine log2(pagesize) needed for address alignment */
553
 
       int tp=512;
554
 
       logpagesize = 9;
555
 
       pagesize = getpagesize();
556
 
       if(tp>pagesize)armci_die("armci_shmem_init:pagesize",pagesize);
557
 
 
558
 
       while(tp<pagesize){
559
 
         tp <<= 1;
560
 
         logpagesize++;
561
 
       }
562
 
       if(tp!=pagesize)armci_die("armci_shmem_init:pagesize pow 2",pagesize);
563
 
#   endif
564
 
 
565
 
   if(DEBUG_) {
566
 
     printf("page size =%d log=%d\n",pagesize,logpagesize); fflush(stdout); }
567
 
 
568
 
#endif
569
 
#endif
570
 
 
571
 
   if(armci_me == armci_master){
572
 
#if !defined(NO_SHMMAX_SEARCH) || defined(SHMMAX_SEARCH_NO_FORK)
573
 
#       ifdef SHMMAX_SEARCH_NO_FORK
574
 
          int x = armci_shmem_test_no_fork();
575
 
#       else
576
 
          int x = armci_child_shmem_init();
577
 
#       endif
578
 
 
579
 
        if(x<1)
580
 
          armci_die("no usable amount of shared memory available: only got \n",
581
 
          (int)LBOUND);
582
 
 
583
 
#       if defined(ALLOC_MUNMAP)
584
 
          /* cap down for special memory allocator unless ARMCI_DEFAULT_SHMMAX
585
 
             not set - the user knows what is doing*/
586
 
#         if !defined(REGION_ALLOC)
587
 
          if(!getenv("ARMCI_DEFAULT_SHMMAX"))
588
 
            if(x>max_alloc_munmap && !armci_elan_starting_address) x=max_alloc_munmap;
589
 
#         else
590
 
            x = 10; /* mb */
591
 
#         endif
592
 
#       endif
593
 
 
594
 
        if(DEBUG_){
595
 
           printf("%d:shmem_init: %d mbytes max segment size\n",armci_me,x);fflush(stdout);}
596
 
 
597
 
        MinShmem = (long)(x<<10); /* make sure it is in kb: mb <<10 */ 
598
 
        MaxShmem = MAX_REGIONS*MinShmem;
599
 
#       ifdef REPORT_SHMMAX
600
 
              printf("%d using x=%d SHMMAX=%ldKB\n", armci_me,x, MinShmem);
601
 
              fflush(stdout);
602
 
#       endif
603
 
#else
604
 
 
605
 
      /* nothing to do here - limits were given */
606
 
 
607
 
#endif
608
 
    }
609
 
 
610
 
    armci_krmalloc_init_ctxshmem();
611
 
    if(DEBUG_)printf("%d: out of shmem_init\n",armci_me);
612
 
}
613
 
 
614
 
 
615
 
/*\ application can reset the upper limit (bytes) for memory allocation
616
 
\*/
617
 
void armci_set_shmem_limit(unsigned long shmemlimit)
618
 
{
619
 
     unsigned long kbytes;
620
 
     kbytes = (shmemlimit + SHM_UNIT -1)/SHM_UNIT;
621
 
     if(MaxShmem > kbytes) MaxShmem = kbytes;
622
 
     if(MinShmem > kbytes) MinShmem = kbytes;
623
 
}
624
 
 
625
 
 
626
 
static void shmem_errmsg(size_t size)
627
 
{
628
 
long sz=(long)size;
629
 
    printf("******************* ARMCI INFO ************************\n");
630
 
    printf("The application attempted to allocate a shared memory segment ");
631
 
    printf("of %ld bytes in size. This might be in addition to segments ",sz);
632
 
    printf("that were allocated succesfully previously. ");
633
 
    printf("The current system configuration does not allow enough ");
634
 
    printf("shared memory to be allocated to the application.\n");
635
 
    printf("This is most often caused by:\n1) system parameter SHMMAX ");
636
 
    printf("(largest shared memory segment) being too small or\n");
637
 
    printf("2) insufficient swap space.\n");
638
 
    printf("Please ask your system administrator to verify if SHMMAX ");
639
 
    printf("matches the amount of memory needed by your application and ");
640
 
    printf("the system has sufficient amount of swap space. ");
641
 
    printf("Most UNIX systems can be easily reconfigured ");
642
 
    printf("to allow larger shared memory segments,\n");
643
 
    printf("see http://www.emsl.pnl.gov/docs/global/support.html\n");
644
 
    printf("In some cases, the problem might be caused by insufficient swap space.\n");
645
 
    printf("*******************************************************\n");
646
 
}
647
 
 
648
 
 
649
 
static struct shm_region_list{
650
 
   char     *addr;
651
 
   long     id;
652
 
   long     sz;
653
 
   long     attached;
654
 
}region_list[MAX_REGIONS];
655
 
static int alloc_regions=0;
656
 
static long occup_blocks=0;
657
 
 
658
 
/* Terminology
659
 
 *   region - actual piece of shared memory allocated from OS
660
 
 *   block  - a part of allocated shmem that is given to the requesting process
661
 
 */
662
 
 
663
 
 
664
 
static int last_allocated=-1;
665
 
 
666
 
 
667
 
unsigned long armci_max_region()
668
 
{
669
 
  return MinShmem;
670
 
}
671
 
 
672
 
 
673
 
int find_regions(char *addrp,  long* id, int *region)
674
 
{
675
 
int nreg, reg;
676
 
 
677
 
    if(last_allocated!=-1){
678
 
       reg=last_allocated;
679
 
       last_allocated = -1;
680
 
    } else{
681
 
       
682
 
       for(reg=-1,nreg=0;nreg<alloc_regions; nreg++)
683
 
       {
684
 
          if(addrp >= region_list[nreg].addr &&
685
 
             addrp < (region_list[nreg].addr + region_list[nreg].sz))
686
 
          {
687
 
             reg = nreg;
688
 
             break;
689
 
          }
690
 
       }
691
 
       
692
 
       if(reg == -1)
693
 
          armci_die("find_regions: failed to locate shared region", 0L);
694
 
    }
695
 
 
696
 
    *region = reg;
697
 
    *id = region_list[reg].id;
698
 
 
699
 
    return 1;
700
 
}
701
 
 
702
 
/* returns the shmem info based on the addr */
703
 
int armci_get_shmem_info(char *addrp,  int* shmid, long *shmoffset, 
704
 
                         size_t *shmsize) 
705
 
{    
706
 
    int region; long id;
707
 
 
708
 
    find_regions(addrp, &id, &region);
709
 
    *shmid     = id;
710
 
    *shmoffset = (long)(addrp - region_list[region].addr);
711
 
    *shmsize   = region_list[region].sz;
712
 
 
713
 
    return 1;
714
 
}
715
 
 
716
 
long armci_shm_reg_size(int i, long id)
717
 
{
718
 
     if(i<0 || i>= MAX_REGIONS)armci_die("armci_shmem_reg_size: bad i",i); 
719
 
     return region_list[i].sz;
720
 
}
721
 
 
722
 
void* armci_shm_reg_ptr(int i)
723
 
{
724
 
     if(i<0 || i>= MAX_REGIONS)armci_die("armci_shmem_reg_ptr: bad i",i); 
725
 
     return (void *)region_list[i].addr;
726
 
}
727
 
 
728
 
Header *armci_get_shmem_ptr(int shmid, long shmoffset, size_t shmsize) 
729
 
{
730
 
/* returns, address of the shared memory region based on shmid, offset.
731
 
 * (i.e. return_addr = stating address of shmid + offset)*/
732
 
    long idlist[SHMIDLEN];
733
 
    Header *p = NULL;
734
 
 
735
 
    idlist[1] = (long)shmid;
736
 
    idlist[0] = shmoffset;
737
 
    idlist[IDLOC+1] = shmsize; /* CHECK : idlist in CreateShmem????*/
738
 
 
739
 
    if(!(p=(Header*)Attach_Shared_Region(idlist+1, shmsize, idlist[0])))
740
 
       armci_die("kr_malloc:could not attach",(int)(p->s.shmsize>>10));
741
 
#if DEBUG_
742
 
    printf("%d: armci_get_shmem_ptr: %d %ld %ld %p\n",
743
 
           armci_me, idlist[1], idlist[0], shmsize, p);
744
 
    fflush(stdout);    
745
 
#endif
746
 
    return p;
747
 
}
748
 
 
749
 
 
750
 
char *Attach_Shared_Region(id, size, offset)
751
 
     long *id, offset, size;
752
 
{
753
 
int reg, found, shmflag=0;
754
 
static char *temp;
755
 
 
756
 
  if(alloc_regions>=MAX_REGIONS)
757
 
       armci_die("Attach_Shared_Region: to many regions ",0);
758
 
 
759
 
  if(DEBUG_){
760
 
      printf("%d:AttachSharedRegion %d:size=%ld id=%ld\n",
761
 
             armci_me, create_call++, size,*id);
762
 
      fflush(stdout);
763
 
  }
764
 
 
765
 
 
766
 
  /* under Linux we can get valid id=0 */
767
 
#ifndef LINUX
768
 
  if(!*id) armci_die("Attach_Shared_Region: shmem ID=0 ",(int)*id);
769
 
#endif
770
 
 
771
 
  /* first time needs to initialize region_list structure */
772
 
  if(!alloc_regions){
773
 
      for(reg=0;reg<MAX_REGIONS;reg++){
774
 
        region_list[reg].addr=(char*)0;
775
 
        region_list[reg].attached=0;
776
 
        region_list[reg].id=0;
777
 
      }
778
 
      MinShmem= id[SHMIDLEN-2];
779
 
      if(DEBUG2_){
780
 
         printf("%d:attach: allocation unit: %ldK\n",armci_me,MinShmem);
781
 
         fflush(stdout);
782
 
      }
783
 
  }
784
 
 
785
 
  /* search region_list for the current shmem id */
786
 
  for(found = 0, reg=0; reg < MAX_REGIONS;reg++)
787
 
      if((found=(region_list[reg].id == *id)))break;
788
 
 
789
 
  if(!found){
790
 
     reg = alloc_regions;
791
 
     region_list[reg].id =*id;
792
 
     alloc_regions++;
793
 
  }
794
 
 
795
 
  /* we need to use the actual shared memory segment not user req size */ 
796
 
  size = id[IDLOC];
797
 
 
798
 
  /* attach if not attached yet */
799
 
  if(!region_list[reg].attached){
800
 
 
801
 
#   ifdef ALLOC_MUNMAP
802
 
       char *pref_addr = alloc_munmap((size_t) (size));
803
 
#   else
804
 
       char *pref_addr = (char*)0;
805
 
#   endif
806
 
    if ( (long) (temp = shmat((int) *id, pref_addr, shmflag)) == -1L){
807
 
       fprintf(stderr,"%d:attach error:id=%ld off=%ld seg=%ld\n",armci_me,*id,offset,MinShmem);
808
 
       shmem_errmsg((size_t)MinShmem*1024);
809
 
       armci_die("Attach_Shared_Region:failed to attach to segment id=",(int)*id);
810
 
    }
811
 
    if(DEBUG_){
812
 
        printf("%d:attached: id=%d address=%p\n",armci_me,(int)*id, temp);
813
 
        fflush(stdout);
814
 
    }
815
 
    POST_ALLOC_CHECK(temp,size);
816
 
    region_list[reg].addr = temp; 
817
 
    region_list[reg].attached = 1;
818
 
    region_list[reg].sz= size;
819
 
    if(DEBUG_){
820
 
    printf("\n%d:%s:new memory region\n",armci_me,__FUNCTION__);fflush(stdout);
821
 
    }
822
 
 
823
 
  }
824
 
 
825
 
  if(STAMP)
826
 
  /* check stamp to make sure that we are attached in the right place */
827
 
  if(*((int*)(region_list[reg].addr+ offset))!= alloc_regions-1)
828
 
      armci_die("Attach_Shared_Region: wrong stamp value !", 
829
 
                *((int*)(region_list[reg].addr+ offset)));
830
 
  occup_blocks++;
831
 
  return (region_list[reg].addr+ offset);
832
 
}
833
 
 
834
 
#ifdef ALLOW_PIN
835
 
extern void armci_region_register_shm(void *start, long size);
836
 
#endif
837
 
 
838
 
/*\ allocates shmem, to be called by krmalloc that is called by process that
839
 
 *  creates shmem region
840
 
\*/
841
 
void *armci_allocate(long size)
842
 
{
843
 
char * temp;
844
 
int id,shmflag=0;
845
 
size_t sz = (size_t)size;
846
 
#ifdef ALLOC_MUNMAP
847
 
       char *pref_addr = alloc_munmap((size_t) (MinShmem*SHM_UNIT));
848
 
#else
849
 
       char *pref_addr = (char*)0;
850
 
#endif
851
 
#if defined(SGI_N32) && defined(SHM_SGI_ANYADDR)
852
 
  shmflag= SHM_SGI_ANYADDR;
853
 
#endif
854
 
 
855
 
    if(DEBUG1){
856
 
       printf("%d:allocate: Shmem allocate size %ld bytes\n",armci_me,size); 
857
 
       fflush(stdout);
858
 
    }
859
 
 
860
 
    if( alloc_regions >= MAX_REGIONS)
861
 
       armci_die("Create_Shared_Region:allocate:too many regions allocated ",0);
862
 
 
863
 
    last_allocated = alloc_regions;
864
 
 
865
 
#ifdef SHMMAX_SEARCH_NO_FORK
866
 
    if (ptr_search_no_fork){
867
 
        temp = ptr_search_no_fork;
868
 
        id   = id_search_no_fork;
869
 
        ptr_search_no_fork = (char*)0; /* do not look at it again */
870
 
    }else 
871
 
#endif
872
 
    {
873
 
       if ( (id = armci_shmget(sz,"armci_allocate")) < 0 ) {
874
 
          fprintf(stderr,"id=%d size=%ld\n",id, size);
875
 
          shmem_errmsg(sz);
876
 
          armci_die("allocate: failed to create shared region ",id);
877
 
       }
878
 
 
879
 
       if ( (long)( (temp = shmat(id, pref_addr, shmflag))) == -1L){
880
 
          char command[64];
881
 
          CLEANUP_CMD(command);
882
 
          if(system(command) == -1) 
883
 
            printf("Please clean shared memory (id=%d): see man ipcrm\n",id);
884
 
          armci_die("allocate: failed to attach to shared region id=",id);
885
 
       }
886
 
       if(DEBUG_){
887
 
         printf("%d:allocate:attach:id=%d paddr=%p size=%ld\n",armci_me,id,temp,size);
888
 
         fflush(stdout);
889
 
       }
890
 
#if 1
891
 
       /* delete segment id so that OS cleans it when all attached processes are gone */
892
 
       if(shmctl( id, IPC_RMID, (struct shmid_ds *)NULL))
893
 
          fprintf(stderr,"failed to remove shm id=%d\n",id);
894
 
#endif
895
 
 
896
 
    }
897
 
    POST_ALLOC_CHECK(temp,sz);
898
 
 
899
 
    region_list[alloc_regions].addr = temp;
900
 
    region_list[alloc_regions].id = id;
901
 
    region_list[alloc_regions].attached=1;
902
 
    region_list[alloc_regions].sz=sz;
903
 
    alloc_regions++;
904
 
 
905
 
    if(DEBUG2_){
906
 
      printf("%d:allocate:id=%d addr=%p size=%ld\n",armci_me,id,temp,size);
907
 
      fflush(stdout);
908
 
    }
909
 
 
910
 
    return (void*) (temp);
911
 
}
912
 
    
913
 
/******************** common code for the two versions *********************/
914
 
 
915
 
 
916
 
/*\ Allocate a block of shared memory - called by master process
917
 
\*/
918
 
char *Create_Shared_Region(long *id, long size, long *offset)
919
 
{
920
 
  char *temp;  
921
 
int  reg, refreg=0,nreg;
922
 
  
923
 
    if(alloc_regions>=MAX_REGIONS)
924
 
       armci_die("Create_Shared_Region: to many regions ",0);
925
 
 
926
 
    if(DEBUG_){
927
 
      printf("%d:CreateSharedRegion %d:size=%ld\n",armci_me,create_call++,size);
928
 
      fflush(stdout);
929
 
    }
930
 
 
931
 
    /*initialization: 1st allocation request */
932
 
    if(!alloc_regions){
933
 
       for(reg=0;reg<MAX_REGIONS;reg++){
934
 
          region_list[reg].addr=(char*)0;
935
 
          region_list[reg].attached=0;
936
 
          region_list[reg].id=0;
937
 
       }
938
 
       if(DEBUG_){
939
 
          printf("%d:1st CreateSharedRegion: allocation unit:%ldK,shmax:%ldK\n",
940
 
                 armci_me,MinShmem,MaxShmem);
941
 
          fflush(stdout);
942
 
       }
943
 
 
944
 
       kr_malloc_init(SHM_UNIT, (size_t)MinShmem, (size_t)MaxShmem, 
945
 
                      armci_allocate, 0, &ctx_shmem);
946
 
       ctx_shmem.ctx_type = KR_CTX_SHMEM;
947
 
       id[SHMIDLEN-2]=MinShmem;
948
 
    }
949
 
 
950
 
    if(!alloc_regions)  temp = kr_malloc((size_t)size, &ctx_shmem,0, NULL, NULL);
951
 
    else temp = kr_malloc((size_t)size, ctx_shmem_global,0,NULL,NULL);
952
 
 
953
 
    if(temp == (char*)0 )
954
 
       armci_die("CreateSharedRegion:kr_malloc failed KB=",(int)size>>10);
955
 
    
956
 
    if(!(nreg=find_regions(temp,id,&reg)))
957
 
        armci_die("CreateSharedRegion: allocation inconsitent",0);
958
 
 
959
 
#ifndef MULTIPLE_REGIONS
960
 
    refreg = reg;
961
 
#endif
962
 
 
963
 
    if(STAMP) *((int*)temp) = alloc_regions-1;
964
 
    *offset = (long) (temp - region_list[refreg].addr);
965
 
    id[IDLOC]=region_list[reg].sz; /* elan post check */
966
 
    occup_blocks++;
967
 
  
968
 
    if(DEBUG_){ 
969
 
      printf("%d:CreateShmReg:reg=%d id=%ld off=%ld ptr=%p adr=%p s=%d n=%d sz=%ld\n",
970
 
           armci_me,reg,region_list[reg].id,*offset,region_list[reg].addr,
971
 
           temp,(int)size,nreg,id[IDLOC]);
972
 
      fflush(stdout);
973
 
    }
974
 
 
975
 
    return temp;
976
 
}
977
 
 
978
 
 
979
 
 
980
 
/*\ only process that created shared region returns the pointer to kr_malloc 
981
 
\*/
982
 
void Free_Shmem_Ptr( id, size, addr)
983
 
     long id, size;
984
 
     char *addr;
985
 
{
986
 
  kr_free(addr, ctx_shmem_global);
987
 
}
988
 
 
989
 
 
990
 
void Delete_All_Regions()
991
 
{
992
 
int reg;
993
 
int code=0;
994
 
extern int armci_me;
995
 
 
996
 
  for(reg = 0; reg < MAX_REGIONS; reg++){
997
 
    if(region_list[reg].addr != (char*)0){
998
 
      code += shmctl((int)region_list[reg].id,IPC_RMID,(struct shmid_ds *)NULL);
999
 
      region_list[reg].addr = (char*)0;
1000
 
      region_list[reg].attached = 0;
1001
 
      if(DEBUG_)
1002
 
         fprintf(stderr,"%d Delete_All_Regions id=%d code=%d\n",armci_me, 
1003
 
                (int)region_list[reg].id, code);
1004
 
    }
1005
 
  }
1006
 
}
1007
 
 
1008
 
 
1009
 
static int
1010
 
Semop(int semid,struct sembuf *opers,size_t nops) {
1011
 
      int ret;
1012
 
 
1013
 
      if((ret = semop(semid,opers,nops)) == -1) {
1014
 
         printf("%d: semop error performing %i operation(s) on semid %i.\n",armci_me,(int) nops,semid);
1015
 
         switch(errno) {
1016
 
           case EFBIG:  printf(" semop errno=EFBIG.\n"); break;
1017
 
           case E2BIG:  printf(" semop errno=E2BIG.\n"); break;
1018
 
           case EINTR:  printf(" semop errno=EINTR.\n"); break;
1019
 
           case EINVAL: printf(" semop errno=EINVAL.\n"); break;
1020
 
           case EACCES: printf(" semop errno=EACCES.\n"); break;
1021
 
           case EAGAIN: printf(" semop errno=EAGAIN.\n"); break;
1022
 
           case ENOSPC: printf(" semop errno=ENOSPC.\n"); break;
1023
 
           case ERANGE: printf(" semop errno=ERANGE.\n"); break;
1024
 
           case EFAULT: printf(" semop errno=EFAULT.\n"); break;
1025
 
           default:
1026
 
              printf(" unusual semop errno=%d\n",errno); break;
1027
 
         }
1028
 
         armci_die("semaphore error",911);
1029
 
      }
1030
 
 
1031
 
      return ret;
1032
 
}
1033
 
 
1034
 
 
1035
 
int
1036
 
semaphoreOperation(int semid,int semnum,int semop) {
1037
 
      struct sembuf op;
1038
 
      op.sem_op  = semop;
1039
 
      op.sem_num = semnum;
1040
 
      op.sem_flg = 0;
1041
 
      return Semop(semid,&op,1);
1042
 
}
1043
 
 
1044
 
 
1045
 
void
1046
 
semaphoreAcquire(int semid, int semnum,int access) {
1047
 
     struct sembuf op;
1048
 
      op.sem_op = -access;
1049
 
      op.sem_num = semnum;
1050
 
      op.sem_flg = 0;
1051
 
      Semop(semid,&op,1);
1052
 
}
1053
 
 
1054
 
 
1055
 
void
1056
 
semaphoreRelease(int semid,int semnum,int access) {
1057
 
      struct sembuf op;
1058
 
      op.sem_op  = access;
1059
 
      op.sem_num = semnum;
1060
 
      op.sem_flg = 0;
1061
 
      Semop(semid,&op,1);
1062
 
}
1063
 
 
1064
 
#else
1065
 
 what are doing here ?
1066
 
#endif