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

Practica Suma de Elementos Con Hilos

This C program demonstrates passing arguments to threads during creation using Pthreads. It creates 8192 threads to each sum a portion of an array in parallel. Each thread is passed its ID as an argument. The main thread collects the sums from each thread and calculates a total sum. It also times the parallel execution and prints the total time.

Uploaded by

Fabiola Camilo
Copyright
© Attribution Non-Commercial (BY-NC)
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)
83 views2 pages

Practica Suma de Elementos Con Hilos

This C program demonstrates passing arguments to threads during creation using Pthreads. It creates 8192 threads to each sum a portion of an array in parallel. Each thread is passed its ID as an argument. The main thread collects the sums from each thread and calculates a total sum. It also times the parallel execution and prints the total time.

Uploaded by

Fabiola Camilo
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 2

/*****************************************************************

*************
* FILE: hello_arg1.c
* DESCRIPTION:
*
A "hello world" Pthreads program which demonstrates one safe
way
*
to pass arguments to threads during thread creation.
*
******************************************************************
************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 8192
#define N 8192
#define hmax 1500
#define max 1500
int arreglo[N],arregloF[NUM_THREADS];
int sumatotal=0;
//float sumatotal=0;
void *PrintHello(void *threadid){
int *id_ptr, taskid,inicio=0,fin=0,i,j,l;
int sumat=0;
float x1=0;
id_ptr = (int *) threadid;
taskid = *id_ptr;
inicio=(N/NUM_THREADS)*taskid;
fin=(N/NUM_THREADS)*(taskid+1);
for(i=inicio;i<fin;i++){
sumat=sumat+arreglo[i];
for(j=0;j<hmax;j++){
for(l=0;l<max;l++){
x1=sin(300);
}
}
}
arregloF[taskid]=sumat;
printf("Thread %d: la suma es %d\n", taskid,sumat);
pthread_exit(NULL);
}
int main(int argc, char *argv[]){
pthread_t threads[NUM_THREADS];
time_t t1,t2;
int *taskids[NUM_THREADS];
int t,n,l,k;
float sumatotal=0;
double x=0;

for(n=0;n<N;n++){
arreglo[n]=1;
}
t1=time(NULL);
for(k=0;k<NUM_THREADS;k++) {
taskids[k] = (int *) malloc(sizeof(int));
*taskids[k] = k;
pthread_create(&threads[k], NULL, PrintHello, (void *)
taskids[k]);
}
for(t=0;t<NUM_THREADS;t++){
pthread_join(threads[t],NULL);
}
for(t=0;t<NUM_THREADS;t++){
sumatotal=sumatotal+arregloF[t];
}
printf("La suma total es %f \n",sumatotal);
t2=time(NULL);
x=difftime(t2,t1);
printf("El tiempo total es %lf \n",x);
// printf("La suma total es %d \n",sumat);
}

You might also like