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

FICHA 7 - Multithreading

The document discusses multithreading in C using pthreads. It allocates memory for an array of doubles and divides the array among 20 threads. Each thread is assigned a portion of the array and performs 110 iterations of randomly generating values for its portion and then calculating the sine of each value cubed. The main function creates the threads, joins the threads after they complete, and prints the elapsed, user, and system times.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

FICHA 7 - Multithreading

The document discusses multithreading in C using pthreads. It allocates memory for an array of doubles and divides the array among 20 threads. Each thread is assigned a portion of the array and performs 110 iterations of randomly generating values for its portion and then calculating the sine of each value cubed. The main function creates the threads, joins the threads after they complete, and prints the elapsed, user, and system times.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FICHA 7 _multithreading

#include #include #include #include #include #include #include <stdio.h> <stdlib.h> <math.h> <time.h> <sys/time.h> <sys/resource.h> <pthread.h>

#define NUM_ITER 110 #define NUM_ELEM 10000000 #define NUM_THREADS 20 void *ope(void *); struct dados_thread { double *ptr_dados; int numero_elementos; }; int main() { time_t t0; int Tid; struct rusage usage; struct dados_thread d_thread[NUM_THREADS]; pthread_t thread_ID[NUM_THREADS]; double *dados=malloc(sizeof(double)*NUM_ELEM); if(dados==NULL) { perror("malloc"); exit(1); }

printf("MULTITHREAD - %ld KiB allocated\n",sizeof(double)*NUM_ELEM/1024); // CRIAR UM VECTOR DE ESTRUTURAS COM AS INFORMAES A PASSAR PARA AS THREADS for (Tid=0;Tid<NUM_THREADS;Tid ++) { printf("\nValor do Tid %d e num elementos a processar %d", Tid, NUM_ELEM/NUM_THREADS ); d_thread[Tid].ptr_dados = dados + Tid*(NUM_ELEM/NUM_THREADS); d_thread[Tid].numero_elementos = NUM_ELEM/NUM_THREADS; } t0=time(NULL); for (Tid=0;Tid<NUM_THREADS;Tid ++) printf ("\nThread %d criada com o Id %d.", Tid, pthread_create (&thread_ID[Tid], NULL, ope, (void *) &d_thread[Tid])); for (Tid=0;Tid<NUM_THREADS;Tid ++) printf ("\nA Thread %d com o id %d terminou", Tid, pthread_join (thread_ID[Tid],NULL));

if(getrusage(RUSAGE_SELF , &usage)==-1) perror("getrusage"); printf("\n\n%ld seconds elapsed\n", time(NULL)-t0); printf("user time: \t %3ld.%06ld s\n", (long int) usage.ru_utime.tv_sec, (long int) usage.ru_utime.tv_usec); printf("system time: \t %3ld.%06ld s\n", (long int) usage.ru_stime.tv_sec, (long int) usage.ru_stime.tv_usec); printf("total: %.3f seconds\n", usage.ru_utime.tv_sec + usage.ru_utime.tv_usec/1.0e6 + usage.ru_stime.tv_sec + usage.ru_stime.tv_usec/1.0e6); return 0; } void *ope (void *arg) { int i,j; double *v; struct dados_thread *parametros; parametros = (struct dados_thread*)arg; v = parametros->ptr_dados; printf("\nprocessing %d elements",parametros->numero_elementos); for(j=0;j<NUM_ITER;++j) { v = parametros->ptr_dados; for(i=0;i< (parametros->numero_elementos) ;++i) { *v=drand48(); v++; } v=parametros->ptr_dados; for(i=0;i<parametros->numero_elementos;++i) { *v=sin(pow(*v,3)); v++; } } return NULL; }

You might also like