0% found this document useful (0 votes)
9 views

202UCS19_HPC-LAB_EXP2

The document describes an experiment conducted by Sahil Yadav to calculate the sum of an array using two processors without MPI. It includes a C program that utilizes pthreads to create two threads, each calculating the sum of half of the array elements. The final output displays the total sum of the array elements after both threads have completed their calculations.

Uploaded by

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

202UCS19_HPC-LAB_EXP2

The document describes an experiment conducted by Sahil Yadav to calculate the sum of an array using two processors without MPI. It includes a C program that utilizes pthreads to create two threads, each calculating the sum of half of the array elements. The final output displays the total sum of the array elements after both threads have completed their calculations.

Uploaded by

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

Experiment - 2

NAME - SAHIL YADAV


ROLL NO. - 2022UCS1709

AIM : To run a program to find the sum of all elements of an array using 2
processors without MPI (Message Passing Interface).

CODE:
#include <stdio.h>
#include <pthread.h>

#define SIZE 10
int arr[SIZE] = {11, 12, 13, 14, 15, 16, 17};
int sum[2] = {0, 0};

void* calculate_sum(void* arg) {


int index = *(int*)arg;
int start = index * (SIZE / 2);
int end = start + (SIZE / 2);

for (int i = start; i < end; i++) {


sum[index] += arr[i];
}

return NULL;
}

int main() {
pthread_t threads[2];
int thread_index[2] = {0, 1};

for (int i = 0; i < 2; i++) {


pthread_create(&threads[i], NULL, calculate_sum, &thread_index[i]);
}

for (int i = 0; i < 2; i++) {


pthread_join(threads[i], NULL);
}

int total_sum = sum[0] + sum[1];


printf("Total Sum: %d\n", total_sum);

return 0;
}
OUTPUT:

You might also like