0% found this document useful (0 votes)
8 views2 pages

Trial C

The document contains multiple C programs demonstrating the use of OpenMP for parallel processing. It includes examples for printing thread information, calculating power based on thread ID, and summing even and odd elements of an array using parallel sections. Each program highlights different aspects of parallel computing with OpenMP.

Uploaded by

prajwalprabhu025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Trial C

The document contains multiple C programs demonstrating the use of OpenMP for parallel processing. It includes examples for printing thread information, calculating power based on thread ID, and summing even and odd elements of an array using parallel sections. Each program highlights different aspects of parallel computing with OpenMP.

Uploaded by

prajwalprabhu025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

trial.

#include<stdio.h>
#include<omp.h>

int main(){

#pragma omp parallel


printf("hello from %d of %d", omp_get_thread_num(), omp_get_num_threads());
return 0 ;
}

Week-2
2.
#include<stdio.h>
#include<omp.h>
#include<math.h>

int main(){

int i;

printf("Enter value of i: ");


scanf("%d", &i);

#pragma omp parallel


{
int thread_id=omp_get_thread_num();
int result=pow(i,thread_id);
printf("pow(i,x) for thread %d is %d\n",thread_id,result);
}
return 0 ;
}

3. Sum of even and odd elements of an array


#include<stdio.h>
#include<omp.h>
#include<math.h>

int main(){

int size,evenSum=0,oddSum=0,i;

printf("Enter size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter elements into the array: ");


for(i=0;i<size;i++){
scanf("%d", &arr[i]);
}

omp_set_num_threads(2);
#pragma omp parallel sections
{

#pragma omp section


{
for(i=0;i<size;i++){
if(arr[i]%2==0){
evenSum+=arr[i];
}
}
printf("Sum of even numbers: %d calculated by thread %d\n",
evenSum,omp_get_thread_num());
}

#pragma omp section


{
for(i=0;i<size;i++){
if(arr[i]%2!=0){
oddSum+=arr[i];
}
}
printf("Sum of odd numbers: %d calculated by thread %d\n",
oddSum,omp_get_thread_num());
}
}

return 0 ;
}

4.

You might also like