[CSC 435] Matrix Multiplication Example (using variable number of threads)

Andrew J. Pounds pounds_aj at mercer.edu
Sun Feb 16 17:18:16 EST 2014


Guys -- I have attached a piece of code that follows the  code practice 
I would like you to try: all parallel and serial source
variations in one file.   The file I am including should be much easier 
to read than the one I handed out in class because I just basically  
broke up the code into three different sections for the C pre-processor 
to parse (say that three times fast).
I also corrected the matrix multiplication code -- I am no longer using 
the transpose of matrix b.  If you need help building a makefile to 
compile this and link it with your code, just let me know.

I left the Pthreads section blank (just a stub) so you could fill it in 
drawing on examples from the Pthreads book (pp. 52-59).  You may have to 
do some looking into how memory is going to work with threads (some 
things will need to be in a global context) -- but we will talk about 
that on Tuesday.

Did anybody try the EDITOR commands I sent out earlier to see if they 
work with SVN?

-- 
Andrew J. Pounds, Ph.D.  (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627
http://faculty.mercer.edu/pounds_aj

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc435/attachments/20140216/186e93f2/attachment.html>
-------------- next part --------------
#ifdef __cplusplus
extern "C" {
#endif
    void mmm_( int *len,  double *a, double *b, double*c );
#ifdef __cplusplus
    }
#endif


#ifdef OPENMP 

/*   O P E N M P   S E C T I O N  */

void mmm_( int *len,  double *a, double *b, double *c ){
  
int i, j, k;
double sum;
int veclen = *len;

// Set the number of threads here you want to use 
omp_set_num_threads(4);

#pragma omp parallel shared(veclen,a,b,c) private(i,j,k) reduction(+ : sum)  
{
#pragma omp for
for (i=0; i<veclen; i++) {
    for (j=0; j<veclen; j++) {
       sum = 0.0; 
       for (k=0;k<veclen;k++){
          sum += *(a+(i*veclen+k)) * *(b+(k*veclen+j));    
       }
       *(c+(i*veclen+j)) = sum; 

       }
    }
  }
} /* End Parallel Region */

#elif PTHREADS

/*   P T H R E A D S   S E C T I O N  */

#else

/*   S E R I A L   S E C T I O N   */
 
void mmm_( int *len,  double *a, double *b, double *c ){
  
int i, j, k;
int veclen = *len;

for (i=0; i<veclen; i++) {
    for (j=0; j<veclen; j++) {
       *(c+(i*veclen+j)) = 0.0;
       for (k=0;k<veclen;k++) {
          *(c+(i*veclen+j)) += *(a+(i*veclen+k)) * *(b+(k*veclen+j)); 
       }
       }
    }
}

#endif



More information about the csc435 mailing list