>Pentru a descarca compilatorul GCC pentru C, accesati pagina http://www.tdragon.net/recentgcc
si descarcati ultima versiune de TDM/MinGW Installer.
La instalare, se selecteaza compilatorul dorit (in fereastra de selectare a componentelor care se vor instala, se alege drept compilator TDM Unstable 4.x-tdm-2-dw2) si se continua instalarea.
Dupa terminarea instalarii se verifica daca in variabila de sistem PATH (Environment Variables) exista intrarea „C:MinGWbin”, daca nu exista, se va introduce manual.
Pentru a compila apoi un program, se deschide un Command prompt si se executa comanda mingw32-make -C C:folder_ce_contine_pragramul_c sau se navigheaza pana in folderul unde este fisierul .c si se executa comanda mingw32-make. (In folderul in care se regaseste programul trebuie sa fie si un fisier makefile, care contine informatii despre compilatorul care va fi folosit).
Un exemplu de fisier makefile
CC = c:MinGWbingcc-dw2.exe
all: program.exe
program.exe: program.c
$(CC) -o program.exe -fopenmp -Wall program.c -lgomp
Pentru compilarea unui program pe un sistem de operare Linux cu GCC versiunea 4.X, se executa comanda gcc -fopenmp program.c rezultand un fisier a.out, iar pentru rularea acestui fisier se tasteaza comanda ./a.out.
Exemplu de program in C care foloseste OpenMP
/*
* Sample program to test runtime of simple matrix multiply
* with and without OpenMP on gcc-4.3.3-tdm1 (mingw)
* (c) 2009, Rajorshi Biswas
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <omp.h>
int main()
{
int i,j,k,n; //n=matrix dimensions (n*n)
int num_threads; // number of threads
int t1, t2;
double **A; // matrix 1 = A
double **B; // matrix 2 = B
double **M; // matrix 3 = A * B
double temp;
double start, end;
printf ("matmul - matrix multiplicationn");
printf("Threads: ");
scanf("%d",&num_threads);
printf("N: ");
scanf("%d",&n);
// simple OpenMP construction
#pragma omp parallel
{
t2 = omp_get_num_threads();
t1 = omp_get_thread_num();
printf("Hello from thread %d of %dn", t1, t2);
}
// allocate dynamic memory for the each matrix
A = (double **)malloc( sizeof(double*) * n);
B = (double **)malloc( sizeof(double*) * n);
M = (double **)malloc( sizeof(double*) * n);
for(i=0; i<n; ++i) {
A[i] = malloc( sizeof(double) * n );
B[i] = malloc( sizeof(double) * n );
M[i] = malloc( sizeof(double) * n );
}
// populating array with random values
srand( time(NULL) );
for(i=0; i<n; ++i) {
for(j=0; j<n; ++j) {
A[i][j] = (1.0 * rand() / RAND_MAX);
B[i][j] = (1.0 * rand() / RAND_MAX);
}
}
// calculate the product of two matrices without OpenMP
printf("Calculation without OpenMP...");
fflush(stdout);
/*************************************************/
start = omp_get_wtime();
for(i=0; i<n; ++i) {
for(j=0; j<n; ++j) {
temp = 0;
for(k=0; k<n; ++k) {
temp += A[i][k] * B[k][j];
}
M[i][j] = temp;
}
}
end = omp_get_wtime();
/*************************************************/
printf(" took %f seconds.n", end-start);
printf("Calculation without OpenMP... %d threads...", num_threads);
fflush(stdout);
/*************************************************/
omp_set_num_threads(num_threads);
start = omp_get_wtime();
#pragma omp parallel for private(i, j, k, temp) shared(A, B, M)
for(i=0; i<n; ++i) {
for(j=0; j<n; ++j) {
temp = 0;
for(k=0; k<n; ++k) {
temp += A[i][k] * B[k][j];
}
M[i][j] = temp;
}
}
end = omp_get_wtime();
/*************************************************/
printf(" took %f seconds.n", end-start);
return 1;
}
0 comentarii