#include #include #include #include /* add stdlib.h (for malloc) */ #include /* N has to be removed here */ shared double runtimes[THREADS]; shared double dTmax_local[THREADS]; /* ----------------------------------------------------------------- */ /* Since N is unknown at compile time, no blocking factor can be expressed. One solution is to declare a structure in which the local shared data of each thread will be allocated. The blocking factor of such structure is known, and equals 1. This structure, declared as shared, is used for grid[] and new_grid[] */ shared struct { shared [] double *local; } grid[THREADS], new_grid[THREADS]; /* All pointer tables need to be declared, without any dependencies to N. The memory allocation for ptr and tmp_ptr will be done in the main() */ shared [] double **ptr, **new_ptr, *tmp_ptr; /* ----------------------------------------------------------------- */ double **ptr_priv, **new_ptr_priv, *tmp_ptr_priv; /* To make our life easier, let us define N and PRIV_SIZE, for later */ int N, PRIV_SIZE; void initialize(void) { int i, j; /* ----------------------------------------------------------------- */ /* to be changed to use new_ptr[][] and ptr[][] instead of grid[][] and new_grid[][] The data grid must be initialized (to 0) */ for( i=0; i 2 ) { if( MYTHREAD == 0 ) printf("Usage: %s [N]\n", argv[0]); upc_global_exit(0); } if (argc == 2) { /* get N and compute PRIV_SIZE */ sscanf( argv[1], "%d", &N ); } else N = (100*THREADS)-2; if( (N+2)%THREADS != 0 ) { if( MYTHREAD == 0 ) printf("(N+2)%%THREADS must be == 0\n"); upc_global_exit(0); } PRIV_SIZE = (N+2)/THREADS; /* ----------------------------------------------------------------- */ /* memory allocation of grid[MYTHREAD].local[] and new_grid[MYTHREAD].local[] */ grid[MYTHREAD].local = (shared [] double *) upc_alloc( PRIV_SIZE * (N+2) * sizeof( double )); if( grid[MYTHREAD].local == NULL ) /* ----------------------------------------------------------------- */ { printf("TH%02d: grid[MYTHREAD].local == NULL\n", MYTHREAD ); upc_global_exit(0); } /* ----------------------------------------------------------------- */ new_grid[MYTHREAD].local = (shared [] double *) upc_alloc( PRIV_SIZE * (N+2) * sizeof( double )); if( new_grid[MYTHREAD].local == NULL ) /* ----------------------------------------------------------------- */ { printf("TH%02d: grid[MYTHREAD].local == NULL\n", MYTHREAD ); upc_global_exit(0); } /* memory allocation of *ptr[] and *new_ptr[] */ /* ----------------------------------------------------------------- */ ptr = (shared [] double **) upc_alloc( (N+2) * sizeof( shared [] double * )); /* ----------------------------------------------------------------- */ if( ptr == NULL ) { printf("TH%02d: ptr == NULL\n", MYTHREAD ); upc_global_exit(0); } /* ----------------------------------------------------------------- */ new_ptr = (shared [] double **) upc_alloc( (N+2) * sizeof( shared [] double * )); /* ----------------------------------------------------------------- */ if( new_ptr == NULL ) { printf("TH%02d: new_ptr == NULL\n", MYTHREAD ); upc_global_exit(0); } /* memory allocation of *ptr_priv[] and *new_ptr_priv[] */ /* ----------------------------------------------------------------- */ ptr_priv = (double **) malloc( PRIV_SIZE * sizeof( double * )); /* ----------------------------------------------------------------- */ if( ptr_priv == NULL ) { printf("TH%02d: ptr_priv == NULL\n", MYTHREAD ); upc_global_exit(0); } /* ----------------------------------------------------------------- */ new_ptr_priv = (double **) malloc( PRIV_SIZE * sizeof( double * )); /* ----------------------------------------------------------------- */ if( new_ptr_priv == NULL ) { printf("TH%02d: new_ptr_priv == NULL\n", MYTHREAD ); upc_global_exit(0); } /* add a synchronization barrier, to be sure that all threads have Allocated their buffers */ upc_barrier; /* notice initialize() has been moved after the pointer Initializations, so it can use them */ /* ----------------------------------------------------------------- */ /* change the pointer initialization expressions to use the new format of grid[].local[] and new_grid[].local[] */ for( i=0; i