How to decrease running time using openmp

Asked

Viewed 60 times

0

I wanted to know what I can do to decrease the running time using Openmp threads. I made a code to add the values of a size 2² vector, but the time measures are practically equal when I increase the number of threads... should not drop the run time for each thread added?

#include <stdio.h>
#include <omp.h>
#include <math.h>
#include <stdlib.h>

int main(){
    long int N = pow(2,27);
    long int *vet = malloc(sizeof(long int)*N);
    long int i;
    int nThreads = omp_get_num_threads();
    long int soma = 0;
    for(i=0; i<N; i++) vet[i] = i;

#pragma omp parallel for reduction(+:soma)
    for(i=0; i<N; i++){
        soma += vet[i];
    }

    printf("Resultado: %ld\n", soma);
    return 0;
}

inserir a descrição da imagem aqui

  • Why don’t you use a string to sum these values ? , much simpler than what you’re doing.

  • Perhaps the non-parallelizable part is taking the longest run time. Test with a smaller vector or measure only the sum time. It is expected that with many threads there is no real gain, as the system consumes more resources just to scale threads and perform locks.

  • I don’t know, but when I used it I didn’t put #pragma put something like #omp Parallel for reduction(+:soma)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.