In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class on Wednesday February 10. If you are attending asynchronously, turn in your assignment using D2L no later than _11:59pm on Wednesday February 10.
As a class we are going to brainstorm topics and ideas that cut across your projects. We want to identify people in your class that are doing some aspect of their project which is similar to yours. The goal is to find teams of people that may be able to support each other.
The rest of class we will try to help each other out so that everyone gets a parallel OpenMP version of the Pi-code working. Lets see how far we get doing the following:
#include <omp.h>
static long num_steps = 100000; double step;
#define NUM_THREADS 2
void main ()
{
int i, nthreads; double pi, sum[NUM_THREADS];
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
int i, id,nthrds;
double x;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
if (id == 0) nthreads = nthrds;
for (i=id, sum[id]=0.0;i< num_steps; i=i+nthrds) {
x = (i+0.5)*step;
sum[id] += 4.0/(1.0+x*x);
}
}
for(i=0, pi=0.0;i<nthreads;i++)pi += sum[i] * step;
}
#include<iostream>
using std::cin; using std::cout; using std::endl;
#include<omp.h>
static long num_steps = 10000; // race condition...
double step;
int main()
{
//cin >> num_steps ;
int i; double x,pi=0.0;
step = 1.0/(double) num_steps;
omp_set_num_threads(4);
double sum [4] ;
#pragma omp parallel
{
int ID = omp_get_thread_num() ;
cout << ID << endl;
for (int i=ID ; i<num_steps; i += 4)
{
x = (i + 0.5) * step;
sum[ID] += 4.0/(1.0+x*x);
}
}
for (int i=0 ; i<4; i++){
pi += sum[i] * step ;
}
cout.precision(17) ;
cout << pi << " " << (pi-3.14159265358979324) << endl ;
}
If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asynchronously, turn in your assignment using D2L.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.