icp/oer/courses/c-openmp/sections/01-introduction/01-parallel-for/program.c

39 lines
1001 B
C

#include <stdio.h>
#include <unistd.h>
#include <omp.h>
#include <time.h>
void workload()
{
// We are simulating a workload by sleeping the thread
// This is obviously not an accurate representation
// of real computation, but it works for this example
usleep(100000);
}
int main(int argc, const char *argv[])
{
// this sets the default number of threads to 5
// you can still overwrite the number of threads used in a parallel region with a clause
omp_set_num_threads(5);
// We use openMP functions to measure our programs run time.
double start = omp_get_wtime();
#pragma omp parallel
{
// A for loop inside a parallel section can be parallelized using
// the for directive
// TODO: Share the workload of the loop below among all threads
for (int i = 0; i<10; i++) {
workload();
printf("Iteration %d in thread: %d\n", i, omp_get_thread_num());
}
}
double end = omp_get_wtime();
printf("That took a total of %f seconds.\n", end - start);
return 0;
}