#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
#include<pthread.h>
#define N 100
#define T 20
int *arr1,*arr2,*arr3;
void *hello(void *threadId){
long tid = (long)threadId;
long localSum = 0;
int chunk_size = N / T;
int start = tid * chunk_size;
int end = (tid + 1) * chunk_size;
//ensure the last thread processes the remaining elements
if(tid == T - 1){
end = N;
}
for(int i=start; i<end;i++){
arr3[i] = arr1[i] + arr2[i];
}
return NULL;
}
int main(){
arr1
= (int *)malloc(sizeof(int) * N
); arr2
= (int *)malloc(sizeof(int) * N
); arr3
= (int *)malloc(sizeof(int) * N
); for(int i=0;i<N;i++){
arr1[i] = i+ 1;
arr2[i] = i + 1;
arr3[i] = 0;
}
pthread_t threads[T];
//create threads
for(long i=0;i<T;i++){
pthread_create(&threads[i],NULL,hello,(void*)i);
}
//join threads and aggregate the local sums
for(long i=0;i<T;i++){
pthread_join(threads[i],NULL);
}
for(int i=0i<N;i++){
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPG1waS5oPgojaW5jbHVkZTxwdGhyZWFkLmg+CiNkZWZpbmUgTiAxMDAKI2RlZmluZSBUIDIwCgppbnQgKmFycjEsKmFycjIsKmFycjM7CnZvaWQgKmhlbGxvKHZvaWQgKnRocmVhZElkKXsKICAgICAgICBsb25nIHRpZCA9IChsb25nKXRocmVhZElkOwogICAgICAgIGxvbmcgbG9jYWxTdW0gPSAwOyAKICAgICAgICBpbnQgY2h1bmtfc2l6ZSA9IE4gLyBUOwogICAgICAgIGludCBzdGFydCA9IHRpZCAqIGNodW5rX3NpemU7CiAgICAgICAgaW50IGVuZCA9ICh0aWQgKyAxKSAqIGNodW5rX3NpemU7CiAgICAgICAgCiAgICAgICAgLy9lbnN1cmUgdGhlIGxhc3QgdGhyZWFkIHByb2Nlc3NlcyB0aGUgcmVtYWluaW5nIGVsZW1lbnRzCiAgICAgICAgaWYodGlkID09IFQgLSAxKXsKICAgICAgICAgICAgZW5kID0gTjsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpPXN0YXJ0OyBpPGVuZDtpKyspewogICAgICAgICAgICBhcnIzW2ldID0gYXJyMVtpXSArIGFycjJbaV07CiAgICAgICAgfQogICAgICAgIHJldHVybiBOVUxMOwogICAgICAgIAogICAgfQogICAgaW50IG1haW4oKXsKICAgICAgICBhcnIxID0gKGludCAqKW1hbGxvYyhzaXplb2YoaW50KSAqIE4pOwogICAgICAgIGFycjIgPSAoaW50ICopbWFsbG9jKHNpemVvZihpbnQpICogTik7CiAgICAgICAgYXJyMyA9IChpbnQgKiltYWxsb2Moc2l6ZW9mKGludCkgKiBOKTsKICAgICAgICBmb3IoaW50IGk9MDtpPE47aSsrKXsKICAgICAgICAgICAgYXJyMVtpXSA9ICBpKyAxOwogICAgICAgICAgICBhcnIyW2ldID0gaSArIDE7CiAgICAgICAgICAgIGFycjNbaV0gPSAwOwogICAgICAgIH0gIAogICAgICAgIHB0aHJlYWRfdCB0aHJlYWRzW1RdOwogICAgICAgIC8vY3JlYXRlIHRocmVhZHMKICAgICAgICBmb3IobG9uZyBpPTA7aTxUO2krKyl7CiAgICAgICAgICAgIHB0aHJlYWRfY3JlYXRlKCZ0aHJlYWRzW2ldLE5VTEwsaGVsbG8sKHZvaWQqKWkpOwogICAgICAgIH0KICAgICAgICAvL2pvaW4gdGhyZWFkcyBhbmQgYWdncmVnYXRlIHRoZSBsb2NhbCBzdW1zCiAgICAgICAgZm9yKGxvbmcgaT0wO2k8VDtpKyspewogICAgICAgICAgICBwdGhyZWFkX2pvaW4odGhyZWFkc1tpXSxOVUxMKTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpPTBpPE47aSsrKXsKICAgICAgICAgICAgcHJpbnRmKCIlZCIsYXJyM1tpXSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiAwOwogICAgICAgIAogICAgfQ==