User Tools

Site Tools


Site Tools

alloc.f90
program alloc
!$ use OMP_LIB
	implicit none
	real(8), dimension(:), allocatable :: T,U
	integer                            :: Nx1 = 20, rank, Nx1min, Nx1max
	integer                            :: tnot ! total number of threads
 
	allocate(T(1:Nx1)) ! T is shared, allocate before PR
 
!$OMP PARALLEL SHARED(T) PRIVATE(U,rank,tnot,Nx1min,Nx1max)
	rank   = OMP_GET_THREAD_NUM ()  ! get the rank of current thread
	tnot   = OMP_GET_NUM_THREADS()  ! get the total number of threads currently running this PR
	Nx1min = 1 + rank  * (Nx1/tnot) ! calculate the first point to be calculated by this thread
	Nx1max = (rank +1) * (Nx1/tnot) ! calculate the last point to be calculated by this thread
 
	print *, "rank :", rank, "tnot :", tnot, "min and max :", Nx1min, Nx1max
 
	allocate(U(1:Nx1/tnot)) ! U is private, allocate inside the PR
	U(1:Nx1/tnot)    = rank + 10
	T(Nx1min:Nx1max) = U(1:Nx1/tnot)
	deallocate(U)
 
!$OMP END PARALLEL
 
	print *,T
	deallocate(T)
 
end program alloc
alloc.c
#include <stdio.h>
#include <omp.h>
 
int main(int argc, char** argv)
{
	int Nx1 = 20;
	int rank;
	int Nx1min;
	int Nx1max;
	int tnot;
	int i;
 
	double *T = (double*)malloc(Nx1 * sizeof(double));
	double *U;
 
#pragma omp parallel shared(T) private(i,U,rank,tnot,Nx1min,Nx1max)
{
	rank   = omp_get_thread_num();
	tnot   = omp_get_num_threads();
	Nx1min = rank * (Nx1 / tnot);
	Nx1max = (rank +1) * (Nx1 / tnot) -1;
 
	printf("rank : %d tnot : %d min and max %d %d\n", rank, tnot, Nx1min, Nx1max);
 
	U = (double*)malloc(Nx1 * sizeof(double));
 
	for(i = 0; i < Nx1 / tnot; i++)
		U[i] = rank + 10;
 
	for(i = Nx1min; i < Nx1max; i++)
		T[i] = U[i-Nx1min];
 
	free(U);
}
	for(i = 0; i < Nx1; i++)
		printf("%lf\n", T[i]);
 
	free(T);
 
	return 0;
}