SCASH Sample Coding of C


C Laplace Program




#include  <stdio.h>
#include  <sys/types.h>
#include  <sys/time.h>
#include  <string.h>
#include  <math.h>

#include  <scash.h>
#include  <scashcrt.h>

#define  XSIZE   1022
#define  YSIZE   1022
#define  XS (XSIZE+2)
#define  YS (YSIZE+2)
#define  LAP_ALLOC_SIZE  (XS * YS * sizeof(double) * 2 + SCASH_PAGE_SIZE)

#define  PI   3.1415927
#define  NITER  50

double   time1, time2, time3;
double   second();
double  *u;
double  *uu;
double  *sum;

int     pe;  /* My PE Number */
int     ps;  /* Partition Size = Number of PE */

inline double   
second()
{
    double  ret;

    struct  timeval  tv;
    gettimeofday(&tv, NULL);
    
    ret = tv.tv_usec;
    ret = ret / 1000000.0;
    ret = ret + tv.tv_sec;
    return   ret;
}

void 
laplace_init_x(int  y)
{
    int  x;

    scash_poll();
    u[y * XS ] = uu[y * XS ] = 0.0;
    u[y * XS + XSIZE + 1] = uu[y * XS + XSIZE + 1] = 0.0;

    if ( y == 0 || y == XSIZE + 1) {
	for (x = 1 ; x <= XSIZE ; x++)
	    u[y * XS + x] = uu[y * XS + x] = 0.0;
    } else {
	for (x = 1 ; x <= XSIZE ; x++)
	    u[y * XS + x] = sin((double)(y-1)/YSIZE*PI) + cos((double)(x-1)/XSIZE*PI);
    }
    scash_poll();
}

void
laplace_init()
{
  int  y;

  ps = scash_ps(); 
  pe = scash_pe(); 

  for (y = YS / ps * pe; y < (YS / ps) * (pe+1) ; y++)
    laplace_init_x(y);

  scash_barrier(1);
}



Shared Memory Allocation





void 
laplace_alloc()
{
    int     i;
    int     alloc_size = sizeof(double) * XS * YS;
    caddr_t   up;
    caddr_t   uup;
    int       sp;

    ps = scash_ps();

    sp = alloc_size / ps;

    u = (double *)scash_alloc(alloc_size);
    if (u == NULL) 
	scash_exit(1);  
    scash_distribute_caddr((char**)&u);
    uu = (double *)scash_alloc(alloc_size);
    if (uu == NULL) 
	scash_exit(1); 
    scash_distribute_caddr((char**)&uu);

    up = (caddr_t)&u[0];
    uup = (caddr_t)&uu[0];

    for (i = 0 ; i < ps ; i++) {
        scash_home(i, up , sp / SCASH_PAGE_SIZE);
        scash_home(i, uup, sp / SCASH_PAGE_SIZE);
	up += sp;
	uup += sp;
    }

    sum = (double *)scash_alloc( sizeof(double) );
    scash_distribute_double((char**)&sum);
}



Barrier




void
laplace(int iteration)
{
    int   x, y;
    int   y_to, y_from;
    double *tmpu;

    y_from = (YS / ps) * pe;
    y_to = y_from + (YS / ps);
    if (y_from == 0)  y_from++;
    if (y_to == XS)   y_to--;

    scash_barrier(iteration);

    time1 = second();
    do {
	if (pe==0) fprintf(stderr, "laplace %d\n", iteration);
	for (y = y_from ; y < y_to ; y++) {
	    scash_poll();
	    for (x = 1 ; x <= XSIZE ; x++)  
		uu[y * XS + x] = (u[ (y-1) * XS + x] + u[(y+1) * XS + x] + u[y * XS + x-1] + u[y * XS + x+1])/4.0;
	}
	tmpu = uu;
	uu = u;
	u = tmpu;
	scash_barrier(iteration + 2);
    }while (iteration--) ;
    time2 = second();
}



Lock




void
laplace_sum()
{
    int   y_from, y_to;
    int   x, y;
    double local_sum = 0.0;

    y_from = YS / ps * pe;
    y_to = y_from + (YS / ps);
    if (y_from == 0)  y_from++;
    if (y_to == YS)   y_to--;

    for (y = y_from ; y < y_to ; y++)  
	for (x = 1 ; x <= XSIZE ; x++)  
	    local_sum += uu[y * XS + x];

    scash_lock(1);
    *sum += local_sum;
    scash_update((char*)sum, sizeof(*sum));
    scash_unlock(1);

    scash_barrier(1);

    if (pe==0)
	printf("\nsum = %e\n", *sum);
}



Initialization




int
main(int  argc,
     char** argv)
{
    int    i;

    scash_initialize(argc, argv, LAP_ALLOC_SIZE);

    laplace_alloc();

    for (i = 1 ; i < ps ; i++) 
	scash_ainvoke0(i, laplace_init);
    laplace_init();

    printf("laplace_mpc:init -- done");

    for (i = 1 ; i < ps ; i++)
	scash_ainvoke1(i, laplace, NITER);
    laplace(NITER);

    for (i = 1 ; i < ps ; i++) 
	scash_ainvoke0(i, laplace_sum);
    laplace_sum();

    time3 = second();
    printf("Total time = %g\nCalc time = %g\nChecksum time= %g\n",
	   time3 - time1, time2 - time1, time3 - time2);
    exit(0);

    return (0);
}



$Id: sample_crt.html,v 1.2 2002/02/15 06:34:21 hirose Exp $