Functions in UPC part II

Phil Merkey

Introduction

We will rewrite the dot product example using a function to compute the dot product of the shared arrays given with the two different data distributions.

<function3.c>=
#include <stdio.h>
#include <upc.h>

#define NperTHREAD 10
#define SIZE  (NperTHREAD * THREADS)
#define BLOCK  NperTHREAD

shared float dot_cyc, dot_blk;
shared  float x_cyc[SIZE], y_cyc[SIZE];
shared [BLOCK] float x_blk[SIZE], y_blk[SIZE];
shared float partialdot[THREADS];

main ()
{
  int i;
  float dot_product();

  upc_forall( i=0; i< SIZE; i++; i ){
      x_cyc[i] =  (float) i;
      y_cyc[i] = x_cyc[i];

      x_blk[i] =  (float) i;
      y_blk[i] = x_blk[i];
  }
  upc_barrier(1);

  printf("The dot product for cyclic dist in thread %2d is  %g\n", MYTHREAD,
            dot_product( x_cyc , y_cyc , SIZE) );

  upc_barrier(2);

  printf("The dot product for block dist in thread %2d is  %g\n", MYTHREAD,
            dot_product( x_blk , y_blk , SIZE) );
  return(0);
}

float dot_product( shared float *x, shared float *y, int n)
{
   int i, t; 
   float dotprod = 0.0;
   partialdot[MYTHREAD] = 0.0; 
 
   for( i=0; i< n; i++ ) {
     if( MYTHREAD == upc_threadof( &x[i] ) )
      partialdot[MYTHREAD] += x[i] * y[i];
   }
   printf ("Thread %2d holds %g partial sum\n", MYTHREAD, partialdot[MYTHREAD]);
   upc_barrier(10);

   for( t=0; t < THREADS ; t++) { 
      dotprod += partialdot[t];
   }
   return( dotprod );
}