Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 5d9d742e authored by thierry's avatar thierry
Browse files

Update

parent 7f573067
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include <cstring>
#include <omp.h>
int main(int argc, char** argv)
{
char buffer[256]; /* upper bound */
const int len = strlen("Parallel region 1: tid:_\n"); /* _ = id */
int pos =0;
pos= 0;
#pragma omp parallel num_threads(8) shared(pos)
{
int local_pos;
#pragma omp atomic capture
local_pos = pos++;
/* copy string at the right location + update the id */
memcpy(buffer+len*local_pos,"Parallel region 2: tid:_\n", len);
buffer[len*local_pos+23] = '0'+ omp_get_thread_num();
}
std::cout << buffer << std::flush;
return 0;
}
File added
#include <iostream>
#include <omp.h>
int main(int argc, char** argv)
{
int last = -1;
#pragma omp parallel
{
int tid = omp_get_thread_num();
int num_threads = omp_get_num_threads();
std::cout << "Parallel region 1: tid:" << tid << "/num_threads" << std::endl;
last = tid;
}
std::cout << "Last thread is:" << last << std::endl;
return 0;
}
#include <iostream>
#include <omp.h>
int main(int argc, char** argv)
{
#pragma omp parallel
{
int tid = omp_get_thread_num();
#pragma omp masked filter( (tid%2 ==0 ? tid : -1)
#pragma omp critical
std::cout << "Do masked instruction (" << tid << ")" << std::endl;
}
return 0;
}
#include <iostream>
#include <omp.h>
int main(int argc, char** argv)
{
#pragma omp parallel num_threads(3)
{
int tid = omp_get_thread_num();
/* static scheduler with chunk_size == 1 */
#pragma omp for schedule(static, 1)
for (int i=0; i<16; ++i)
{
#pragma omp critical
std::cout << tid << ":: do iteration " << i << std::endl;
}
}
return 0;
}
#include <iostream>
#include <random>
#include <chrono>
#include <omp.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
long Nc=0, N=0;
int P = 0;
N = atoi(argv[1]);
/* uniform distribution U(0,1) */
std::uniform_real_distribution<float> distribution(0,1);
std::default_random_engine generator;
auto t0 = std::chrono::steady_clock::now();
#pragma omp parallel shared(Nc,P) firstprivate(N)
{
P = omp_get_num_threads();
#pragma omp for reduction(+:Nc)
for (long i=0; i<N; ++i)
{
double x,y;
#pragma omp critical
{
x= distribution(generator);
y= distribution(generator);
}
double d = x*x+y*y;
if (d<=1)++Nc;
}
}
auto t1 = std::chrono::steady_clock::now();
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t1 - t0);
std::cout << "Pi ~ " << 4*double(Nc)/double(N) << std::endl;
std::cout << "Tp= " << time_span.count() << " s" << std::endl;
std::cout << "P = " << P << std::endl;
return 0;
}
#include <iostream>
#include <random>
#include <omp.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
long Nc=0, N=0;
int P = 0;
N = atoi(argv[1]);
double t0 = omp_get_wtime();
#pragma omp parallel shared(Nc,P) firstprivate(N)
{
P = omp_get_num_threads();
std::uniform_real_distribution<float> distribution(0,1);
std::mt19937 generator;
#pragma omp for reduction(+:Nc)
for (long i=0; i<N; ++i)
{
double x= distribution(generator);
double y= distribution(generator);
double d = x*x+y*y;
if (d<=1)++Nc;
}
}
double t1 = omp_get_wtime();
std::cout << "Pi ~ " << 4*double(Nc)/double(N) << std::endl;
std::cout << "Tp= " << (t1-t0) << " s" << std::endl;
std::cout << "P = " << P << std::endl;
return 0;
}
void matmul_depend(
int N, int BS,
float A[N][N], float B[N][N], float C[N][N]
)
{
int i, j, k, ii, jj, kk;
for (i = 0; i < N; i+=BS) {
for (j = 0; j < N; j+=BS) {
for (k = 0; k < N; k+=BS) {
// Note 1: i, j, k, A, B, C are firstprivate by default
// Note 2: A, B and C are just pointers
#pragma omp task private(ii, jj, kk) \
depend ( in: A[i:BS][k:BS], B[k:BS][j:BS] ) \
depend ( inout: C[i:BS][j:BS] )
for (ii = i; ii < i+BS; ii++ )
for (jj = j; jj < j+BS; jj++ )
for (kk = k; kk < k+BS; kk++ )
C[ii][jj] = C[ii][jj] + A[ii][kk] * B[kk][jj];
}
}
}
}
}
#include <iostream>
#include <omp.h>
int fibonacci(int n)
{
if (n<2)
return n;
int sum;
int r1;
int r2;
#pragma omp task if (n>20) depend(out:r1) shared(r1) firstprivate(n)
r1 = fibonacci(n-1);
#pragma omp task if (n>20) depend(out:r2) shared(r2) firstprivate(n)
r2 = fibonacci(n-2);
#pragma omp task if (n>20) depend(out:sum) depend(in:r1,r2) shared(r1,r2,sum)
sum = r1 + r2;
#pragma omp taskwait
return sum;
}
int main(int argc, char** argv)
{
int r, n;
n = atoi(argv[1]);
double t0 = omp_get_wtime();
#pragma omp parallel shared(r) firstprivate(n)
{
#pragma omp master
{
#pragma omp task depend(out:r) firstprivate(n) shared(r)
r = fibonacci( n );
#pragma omp task depend(in:r) firstprivate(n) shared(r)
{
double t1 = omp_get_wtime();
std::cout <<"Fibonacci(" << n << ") =" << r << std::endl;
std::cout << "Time: " << t1-t0 << std::endl;
}
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment