Commit 8c6b5076 authored by jansik's avatar jansik
Browse files

Adding exercises

parent e1baf163
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, nprocs, len;
char pname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Get_processor_name(pname,&len);
printf("Hello world! from rank %d of %d on host %s\n", rank, nprocs,pname);
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, nprocs, len;
char pname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(); //Work for you
MPI_Comm_size(); //Work for you
MPI_Comm_rank(); //Work for you
MPI_Get_processor_name(); //Work for you
printf("Hello world! from rank %d of %d on host %s\n", rank, nprocs,pname);
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, nprocs, len, n, sum;
char pname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Get_processor_name(pname,&len);
printf("Hello world! from rank %d of %d on host %s\n", rank, nprocs,pname);
MPI_Barrier(MPI_COMM_WORLD);
if (rank==0) {
printf("Enter the number to broadcast ");
scanf("%d",&n);
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("1, rank %d, n= %d\n",rank,n);
if (rank==1) {
n=5;
MPI_Send(&n, 1, MPI_INT, 2, 0, MPI_COMM_WORLD);
printf("2. rank %d, n= %d\n",rank,n);
MPI_Recv(&n, 1, MPI_INT, 2, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("3. rank %d, n= %d\n",rank,n);
}
if (rank==2) {
MPI_Recv(&n, 1, MPI_INT,1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("2. rank %d, n= %d\n",rank,n);
n=15;
MPI_Send(&n, 1, MPI_INT,1, 0, MPI_COMM_WORLD);
printf("3. rank %d, n= %d\n",rank,n);
}
MPI_Barrier(MPI_COMM_WORLD);
sum=0;
MPI_Reduce(&n,&sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
printf("4. rank %d, n= %d\n",rank,sum);
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, nprocs, len, n, sum;
char pname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Get_processor_name(pname,&len);
printf("Hello world! from rank %d of %d on host %s\n", rank, nprocs,pname);
MPI_Barrier(); //Work for you
if (rank==0) {
printf("Enter the number to broadcast ");
scanf("%d",&n);
}
MPI_Bcast(); //Work: broadcast n from rank 0 to all other ranks
printf("1, rank %d, n= %d\n",rank,n);
if (rank==1) {
n=5;
MPI_Send(); //Work: send n from rank to rank 2
printf("2. rank %d, n= %d\n",rank,n);
MPI_Recv(); //Work: receive n from rank 2
printf("3. rank %d, n= %d\n",rank,n);
}
if (rank==2) {
MPI_Recv(); //Work: receive n from rank 1
printf("2. rank %d, n= %d\n",rank,n);
n=15;
MPI_Send(); //Work: send n to rank 1
printf("3. rank %d, n= %d\n",rank,n);
}
//Work: insert barrier
sum=0;
MPI_Reduce(); //Work: Reduce values of n to variable sum, on rank 0.
printf("4. rank %d, n= %d\n",rank,sum);
MPI_Finalize();
return 0;
}
#include "mpi.h"
#include <math.h>
#include <stdio.h>
#include <omp.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, myid, numprocs, i, len, provided, tid;
double PI25DT = 3.141592653589793238462643;
double mypi, pi, h, sum, x;
char pname[MPI_MAX_PROCESSOR_NAME];
MPI_Init_thread(&argc,&argv,MPI_THREAD_FUNNELED,&provided);
if (provided<MPI_THREAD_FUNNELED)
{ MPI_Finalize(); return 1; }
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Get_processor_name(pname,&len);
printf("Process %d of %d is alive on host %s\n",myid,numprocs,pname);
while (!done)
{
if (myid == 0) {
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
}
//Work: Broadcast n to all ranks
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
//Work: Split the loop over MPI processes
for (i = 1; i <= n; i ++) {
//Work: Find out which process runs which iterations
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
mypi = h * sum;
//Work: Do reduction of partial results into pi
if (myid == 0)
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
MPI_Finalize();
return 0;
}
#include <math.h>
#include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, tid, numprocs, i, len;
double PI25DT = 3.141592653589793238462643;
double pi, h, sum, x;
while (!done)
{
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
for (i = 1; i <= n; i++) {
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
pi = h * sum;
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
return 0;
}
#include "mpi.h"
#include <math.h>
#include <stdio.h>
#include <omp.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, myid, numprocs, i, len, provided, tid;
double PI25DT = 3.141592653589793238462643;
double mypi, pi, h, sum, x;
char pname[MPI_MAX_PROCESSOR_NAME];
MPI_Init_thread(&argc,&argv,MPI_THREAD_FUNNELED,&provided);
if (provided<MPI_THREAD_FUNNELED)
{ MPI_Finalize(); return 1; }
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Get_processor_name(pname,&len);
printf("Process %d of %d is alive on host %s\n",myid,numprocs,pname);
while (!done)
{
if (myid == 0) {
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
#pragma omp parallel for private(i,x,tid) reduction(+:sum)
for (i = myid + 1; i <= n; i += numprocs) {
tid = omp_get_thread_num();
printf("Thread %d rank %d executing iteration %d\n",tid,myid,i);
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
mypi = h * sum;
MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,
MPI_COMM_WORLD);
if (myid == 0)
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
MPI_Finalize();
return 0;
}
#include <omp.h>
#include <stdio.h>
int main(void)
{
int tid, nthreads;
#pragma omp parallel private(tid) shared(nthreads)
{
#pragma omp master
nthreads = omp_get_num_threads();
#pragma omp sections
{
#pragma omp section
{
tid = omp_get_thread_num();
printf("Hello world from section 1 thread %d of %d\n", tid, nthreads);
}
#pragma omp section
{
tid = omp_get_thread_num();
printf("Hello world from section 2 thread %d of %d\n", tid, nthreads);
}
#pragma omp section
{
tid = omp_get_thread_num();
printf("Hello world from section 3 thread %d of %d\n", tid, nthreads);
}
}
}
return 0;
}
#include <omp.h>
#include <stdio.h>
int main(void)
{
int tid, nthreads;
printf("Hello world from thread %d of %d\n", tid, nthreads);
return 0;
}
#include <omp.h>
#include <math.h>
#include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, tid, numprocs, i, len;
double PI25DT = 3.141592653589793238462643;
double pi, h, sum, x;
while (!done)
{
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
#pragma omp parallel private(i,x,tid,numprocs) default(shared) reduction(+:sum)
{
tid = omp_get_thread_num();
numprocs= omp_get_num_threads();
for (i = 1 + tid ; i <= n; i+= numprocs) {
printf("%d: %d\n",tid,i);
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
}
pi = h * sum;
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
return 0;
}
#include <omp.h>
#include <math.h>
#include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, tid, numprocs, i, len;
double PI25DT = 3.141592653589793238462643;
double pi, h, sum, x;
while (!done)
{
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
#pragma omp parallel private(i,x,tid,numprocs) default(shared) reduction(+:sum)
{
tid = omp_get_thread_num();
numprocs= omp_get_num_threads();
#pragma omp for schedule(static,10)
for (i = 1; i <= n; i++) {
printf("%d: %d\n",tid,i);
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
}
pi = h * sum;
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
return 0;
}
#include <omp.h>
#include <math.h>
#include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, tid, numprocs, i, len;
double PI25DT = 3.141592653589793238462643;
double pi, h, sum, x;
while (!done)
{
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
#pragma omp parallel for private(i,x) default(shared) reduction(+:sum)
for (i = 1; i <= n; i++) {
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
pi = h * sum;
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
return 0;
}
#include <math.h>
#include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, tid, numprocs, i, len;
double PI25DT = 3.141592653589793238462643;
double pi, h, sum, x;
while (!done)
{
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
for (i = 1; i <= n; i++) {
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
pi = h * sum;
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
return 0;
}
#include <omp.h>
#include <stdio.h>
int main(void)
{
int tid;
int var1;
var1 = -10;
printf ("Serial var1= %d, &var1= %p\n",var1,&var1);
#pragma omp parallel private(tid) reduction(+:var1)
{
tid = omp_get_thread_num();
//var1 += tid;
printf ("Thread %d var1= %d, &var1= %p\n",tid,var1,&var1);
}
printf ("Serial var1= %d, &var1= %p\n",var1,&var1);
return 0;
}
#include <omp.h>
#include <stdio.h>
int main(void)
{
int tid;
int var1;
var1 = -10;
printf ("Serial var1= %d, &var1= %p\n",var1,&var1);
#pragma omp parallel private(tid) reduction(+:var1)
{
tid = omp_get_thread_num();
var1 = tid;
printf ("Thread %d var1= %d, &var1= %p\n",tid,var1,&var1);
}
printf ("Serial var1= %d, &var1= %p\n",var1,&var1);
return 0;
}
#include <omp.h>
#include <stdio.h>
int main(void)
{
int tid;
int var1;
var1 = -10;
printf ("Serial var1= %d, &var1= %p\n",var1,&var1);
#pragma omp parallel private(tid,var1)
{
tid = omp_get_thread_num();
printf ("Thread %d var1= %d, &var1= %p\n",tid,var1,&var1);
}
printf ("Serial var1= %d, &var1= %p\n",var1,&var1);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment