1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/**
* Copyright 1981-2007 ECMWF
*
* Licensed under the GNU Lesser General Public License which
* incorporates the terms and conditions of version 3 of the GNU
* General Public License.
* See LICENSE and gpl-3.0.txt for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
extern int debugSet;
#include "sharedmemory.h"
#include "fortdefs.h"
void createSharedMemoryCoefficients(
fortint Type, /* SP2LL, SP2QG, .. */
fortint Truncation, /* spectral truncation */
fortreal Grid, /* grid spacing or gaussian number */
fortint Numlat, /* number of latitudes in the shared memory array */
key_t memoryKey, /* shared memory key */
int size) /* size of the shared memory */
{
int type = (int) Type;
struct sembuf bufs[1];
union semun {
int val;
struct semid_ds *buf;
ushort *array;
} semvalues;
fortreal * array;
int shmflg, shmid;
char * shmaddr;
int status, i, oldmask;
char mode[] = "00666";
fortint gaussianNumber, one = 1, first = 1, returnCode;
int semaphoreId, value;
fortreal northPole = 90.0;
/*
// If the semaphore for the shared memory already exists,
// wait for it to become ready to read, and return
*/
if( DEBUG ) printf("createSharedMemoryCoefficients: see if shared memory semaphore exists\n");
semaphoreId = semget(memoryKey,1,0);
if( semaphoreId != -1 ) {
bufs[0].sem_num = 0;
bufs[0].sem_op = -READY_TO_READ;
bufs[0].sem_flg = 0;
if( DEBUG ) printf("createSharedMemoryCoefficients: wait for shared memory semaphore\n");
status = semop(semaphoreId,bufs,(size_t)1);
semvalues.val = READY_TO_READ;
value = semctl((int)semaphoreId, (int)0, (int)SETVAL,semvalues);
if( DEBUG ) printf("createSharedMemoryCoefficients: set shared memory semaphore readable\n");
return;
}
/*
// Create semaphore for the shared memory array
*/
if( DEBUG ) {
printf("createSharedMemoryCoefficients: have to create shared memory segment\n");
printf("createSharedMemoryCoefficients: so create shared memory semaphore\n");
}
oldmask = umask(000);
semaphoreId = semget(memoryKey,1,IPC_CREAT | 0666);
if( semaphoreId == -1 ) {
perror("creator: failed to create smaphore");
exit(1);
}
(void) umask(oldmask);
/*
// Create shared segment id
*/
if( DEBUG ) printf("createSharedMemoryCoefficients: get shared memory segment\n");
(void) umask(0);
shmflg = IPC_CREAT | 0666;
shmid = shmget(memoryKey, (size_t)size, shmflg);
if ( shmid < 0 ) {
perror("shmget error");
exit(1);
}
/*
// Attach the shared segment
*/
if( DEBUG ) printf("createSharedMemoryCoefficients: attach shared memory segment\n");
shmaddr = (char *) NULL;
shmflg = 0;
array = (fortreal *) shmat( shmid, shmaddr, shmflg);
if ( array == (fortreal *) -1 ) {
perror("shmat error");
exit(1);
}
/*
// Put some data into the shared segment
*/
if( DEBUG ) printf("createSharedMemoryCoefficients: put data into the shared segment\n");
returnCode = 0;
JDEBUG();
switch( type ) {
case SP2LL:
if( DEBUG ) printf("createSharedMemoryCoefficients: call NMAKLL for SP2LL\n");
NMAKLL( &Truncation, &Grid, &northPole, &Numlat, array, &returnCode);
if( returnCode ) {
printf("Problem with NMAKLL, return code = %d\n", returnCode);
exit(1);
}
break;
case SP2RG:
case SP2QG:
/*
*/
{
char htype[2];
fortreal * plat;
fortint * kpts;
gaussianNumber = (fortint) Grid;
plat = (fortreal *) malloc( gaussianNumber*2*sizeof(fortreal));
if( plat == NULL ) {
perror("createSharedMemoryCoefficients: plat malloc problem");
exit(1);
}
kpts = (fortint *) malloc( gaussianNumber*2*sizeof(fortint));
if( kpts == NULL ) {
perror("createSharedMemoryCoefficients: kpts malloc problem");
exit(1);
}
if( type == SP2RG ) {
if( DEBUG ) printf("createSharedMemoryCoefficients: handle SP2RG\n");
strcpy(htype,"F");
}
else {
if( DEBUG ) printf("createSharedMemoryCoefficients: handle SP2QG\n");
strcpy(htype,"R");
}
if( DEBUG ) printf("createSharedMemoryCoefficients: call JGETGG\n");
JGETGG(&gaussianNumber, &htype, plat, kpts, &returnCode, one);
if( returnCode ) {
printf("Problem with JGETGG, return code = %d\n", returnCode);
exit(1);
}
if( DEBUG ) printf("createSharedMemoryCoefficients: call NMAKGG\n");
NMAKGG(&Truncation, &first, plat, &Numlat, array, &returnCode);
if( returnCode ) {
printf("Problem with NMAKGG, return code = %d\n", returnCode);
exit(1);
}
free(plat);
free(kpts);
break;
}
/*
*/
default:
printf("creator: Type %d not yet handled\n", type);
exit(1);
}
/*
// Set the semaphore to make the memory readable for cooperating processes
*/
if( DEBUG ) printf("createSharedMemoryCoefficients: set semaphore to make memory readable\n");
semvalues.val = READY_TO_READ;
value = semctl((int)semaphoreId, (int)0, (int)SETVAL,semvalues);
/*
// Detach the shared segment
*/
if( DEBUG ) printf("createSharedMemoryCoefficients: detach the shared segment\n");
status = shmdt(array);
if ( status < 0 ) {
perror("shmdt error");
exit(1);
}
return;
}
|