Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Created October 6, 2015 00:18
Show Gist options
  • Save nathan-cruz77/54d3d7b0ae26cca30cd9 to your computer and use it in GitHub Desktop.
Save nathan-cruz77/54d3d7b0ae26cca30cd9 to your computer and use it in GitHub Desktop.
Programa do James
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define tam 10000
#define randRange 80
#define num_threads 4
float** mat;
float* vet;
float* res; //vetor resultado da multip
void aloca_tretas(){
int i;
/* Aloca os vetores */
vet = malloc(sizeof(float) * tam);
res = malloc(sizeof(float) * tam);
/* Aloca a matriz */
mat = malloc(sizeof(float*) * tam);
for(i=0; i<tam; i++){
mat[i] = malloc(sizeof(float) * tam);
}
}
int main(){
omp_set_num_threads(num_threads);
int i=0,j=0,seed=0;
double end, start;
aloca_tretas();
//inicialização do vetor resultado
for(i=0;i<tam;i++){
res[i]=0.0;
}
start=omp_get_wtime();
//inicialização do vetor de multiplicação
#pragma omp parallel for shared(vet) private(i)
for(i=0;i<tam;i++){
vet[i]=(float)rand_r(&seed)/(float)(RAND_MAX/randRange);
}
//inicialização da matriz de multiplicação
#pragma omp parallel for shared(vet) private(i)
for(i=0;i<tam;i++){
for(j=0;j<tam;j++){
mat[i][j]=(float)rand_r(&seed)/(float)(RAND_MAX/randRange);
}
}
//multiplicação vetXmat
#pragma omp parallel for shared(res, vet, mat) private(i, j)
for(i=0;i<tam;i++){
for(j=0;j<tam;j++){
res[i]=res[i]+(vet[j]*mat[j][i]);
}
}
end=omp_get_wtime();
printf(" took %f seconds.\n", end-start);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment