I'm trying to get the sum of numbers from 1 to 100 using only 5 threads even though I have 12 available.
This was my approach. Please show me where I went wrong.
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads = 5, tid;
int sum = 0;
int a[100];
for(int i = 1; i < 101; i++){
a[i] = i;
}
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
nthreads = omp_get_num_threads();
#pragma omp parallel for reduction (+:sum)
for(int i = 0; i < 100; i++){
sum = sum + a[i + 1];
}
tid = omp_get_thread_num();
printf("Current thread is %d\n", tid);
printf("Number of threads = %d\n", nthreads);
printf("The total sum is %d\n\n\n", sum);
}
Edit: This is the output I am getting: Code Output
The Output that I want is the following:
You have some problems in your code, namely:
First:
for(int i = 1; i < 101; i++){
a[i] = i;
}
you have allocated the array a
as int a[100];
, so in your loop you are getting of the array boundaries, changed it to:
for(int i = 0; i < 100; i++){
a[i] = i + 1;
}
Second:
int nthreads = 5
...
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
nthreads = omp_get_num_threads();
this makes no sense. You create a parallel region, all threads have a copy of nthreads
they set that variable to the number of threads, but after the parallel region that value is gone. The only reason why you print afterwards 5
in:
printf("Number of threads = %d\n", nthreads);
is because nthreads
was initially set to 5.
Third:
for(int i = 0; i < 100; i++){
sum = sum + a[i + 1];
}
you are again accessing positions outside the array a
, change that to:
for(int i = 0; i < 100; i++){
sum = sum + a[i];
}
Fourth:
I'm trying to get the sum of numbers from 1 to 100 using only 5 threads even though I have 12 available.
Because you did not specified the number of threads in the parallel region:
#pragma omp parallel for reduction (+:sum)
you code is running with 12 threads.
The Output that I want is the following:
The total sum is 5050 instead of 4950 is there a way to output the each thread's local sum?
What you want to do is the following:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int sum = 0;
int a[100];
for(int i = 0; i < 100; i++){
a[i] = i + 1;
}
int total_threads_used;
// Create a parallel region with 5 threads and reduce the partial sum's values
#pragma omp parallel num_threads(5) reduction (+:sum)
{
total_threads_used = omp_get_num_threads(); // Get the total threads used
#pragma omp for
for(int i = 0; i < 100; i++){
sum = sum + a[i];
}
printf("Current thread is %d and SUM %d\n", omp_get_thread_num(), sum);
}
printf("Number of threads = %d\n", total_threads_used);
printf("The total sum is %d\n\n\n", sum);
}
Output:
Current thread is 0 and SUM 210
Current thread is 2 and SUM 1010
Current thread is 1 and SUM 610
Current thread is 3 and SUM 1410
Current thread is 4 and SUM 1810
Number of threads = 5
The total sum is 5050
The lines will be outputted in different orders from run to run, since these lines are being printed in parallel, but regardless of the run you should have 5 "Current thread is"
from 0 to 4, 1 "Number of threads = 5"
, and 1 "The total sum is 4950"
.