Skip to content

Instantly share code, notes, and snippets.

@Adiqq
Last active December 13, 2017 23:06
Show Gist options
  • Save Adiqq/327aab40be452cdec52b137bedffdf01 to your computer and use it in GitHub Desktop.
Save Adiqq/327aab40be452cdec52b137bedffdf01 to your computer and use it in GitHub Desktop.
#include <omp.h>
#include <iostream>
void sum(){
int x,y;
int i,j,k;
long long table[40]; //8 bitów
x = 10000;
y = 10000;
int** A = new int*[x];
for(i = 0; i < x; ++i){
A[i] = new int[y];
}
for(i = 0; i < x; i++){
for(j = 0; j < y; j++){
A[i][j] = 1;
}
}
omp_set_num_threads(2);
long long suma = 0;
double start,stop;
for(k = 0; k < 10000; k++){
start = omp_get_wtime();
#pragma omp parallel
{
int id = omp_get_thread_num();
table[k + id] = 0; // zamiast sumal, używamy tablicy, long long ma 8 bitów
#pragma omp for
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
table[k + id] += A[j][i];
}
}
#pragma omp atomic
suma += table[k + id];
}
stop = omp_get_wtime();
std::cout << "Sum: " << suma << std::endl;
std::cout << "Elapsed time: " << (stop - start) << std::endl;
suma = 0;
}
for(int i = 0; i < x; ++i){
delete [] A[i];
}
delete [] A;
}
int main(){
sum();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment