Dot Product Dynamically Allocated Arrays

Phil Merkey

Introduction

This is exactly the same dot product example that we used to demonstrate cyclic and blocked distribution. In this version the arrays are dynamically allocated with upc_all_alloc. Note that the pointer for the arrays have a blocking factor just like the arrays did in the previous example.

<dotprod2.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,  *y_cyc;
shared [BLOCK] float *x_blk,  *y_blk;

upc_lock_t *dotlock;

main ()
{
  int i;
  float mydot;

  dotlock = upc_all_lock_alloc();
  upc_lock_init( dotlock );

  x_cyc = (shared float *) upc_all_alloc(SIZE, sizeof(float));
  y_cyc = (shared float *) upc_all_alloc(SIZE, sizeof(float));

  if(MYTHREAD == 0) 
    dot_blk = dot_cyc = 0.0;
  upc_barrier(0);

  // "affinity" is an int so it is (i mod THREADS)
  upc_forall( i=0; i< SIZE; i++; i ){
      x_cyc[i] =  (float) i;
      y_cyc[i] = x_cyc[i];
  }
  upc_barrier(1);
  
  mydot = 0.0;
  // "affinity" is found from affinity of x_cyc[i]
  upc_forall( i=0; i< SIZE; i++; &x_cyc[i] )
      mydot += x_cyc[i] * y_cyc[i];

  printf ("Process %2d holds %g (cyclic)\n", MYTHREAD, mydot);

  upc_lock(dotlock);
    dot_cyc = dot_cyc + mydot;
  upc_unlock(dotlock);

  upc_barrier(2);

  if( MYTHREAD == 0 )
    printf("Total (cyclic) is %g\n", dot_cyc);

  if( MYTHREAD == 0 ) {
     upc_free( x_cyc );
     upc_free( y_cyc );
  }

  x_blk = (shared [BLOCK] float *) upc_all_alloc(SIZE, sizeof(float));
  y_blk = (shared [BLOCK] float *) upc_all_alloc(SIZE, sizeof(float));

  upc_barrier(3);
  upc_forall( i=0; i< SIZE; i++; i ){
      x_blk[i] =  (float) i;
      y_blk[i] = x_blk[i];
  }

  upc_barrier(4);
  mydot = 0.0;
  // "affinity" is found from affinity of x[i]
  upc_forall( i=0; i< SIZE; i++; &x_blk[i] )
      mydot += x_blk[i] * y_blk[i];

  printf ("Process %2d holds %g (blocked)\n", MYTHREAD, mydot);

  upc_lock(dotlock);
    dot_blk = dot_blk + mydot;
  upc_unlock(dotlock);

  upc_barrier(5);

  if( MYTHREAD == 0 ) {
     upc_free( x_cyc );
     upc_free( y_cyc );
  }

  if( MYTHREAD == 0 )
    printf("Total (blocked) is %g\n", dot_blk);
}