From 794bcf9d1c6d3c83a07787fd54e0495826cd293f Mon Sep 17 00:00:00 2001 From: Olivia Fernandez Date: Sun, 30 Jan 2022 18:38:02 -0300 Subject: [PATCH] use fork to parallel covariance --- fftma_module/gen/lib_src/chunk_array.c | 18 ++++++++++-- fftma_module/gen/lib_src/covariance.c | 39 ++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/fftma_module/gen/lib_src/chunk_array.c b/fftma_module/gen/lib_src/chunk_array.c index 75eb201..6a5a196 100644 --- a/fftma_module/gen/lib_src/chunk_array.c +++ b/fftma_module/gen/lib_src/chunk_array.c @@ -2,8 +2,11 @@ #include "stdbool.h" #include "stdlib.h" #include +#include +#include +#include -#define MAX_CHUNK_SIZE 2048*2048 +#define MAX_CHUNK_SIZE 268435456 int min(int value1, int value2) { if (value1 < value2) return value1; @@ -94,12 +97,22 @@ chunk_array_t* chunk_array_create(char* filename, size_t total_size) { return NULL; } - chunk_array->mmap_array = mmap ( NULL, total_size*sizeof(double), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0 ); + int fd = open(filename, O_RDWR | O_CREAT, 0644); + if(fd < 0){ + free(chunk_array); + return NULL; + } + + chunk_array->mmap_array = mmap ( NULL, total_size * sizeof(double), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); if (chunk_array->mmap_array == NULL) { + free(chunk_array); return NULL; } + ftruncate(fd, total_size * sizeof(double)); + close(fd); + chunk_array->chunk_size = min(MAX_CHUNK_SIZE, total_size); chunk_array->buffer1 = buffer_create(chunk_array->chunk_size); @@ -113,7 +126,6 @@ chunk_array_t* chunk_array_create(char* filename, size_t total_size) { chunk_array->buffer2 = buffer_create(chunk_array->chunk_size); if (chunk_array->buffer2 == NULL) { - //fclose(chunk_array->fp); buffer_free(chunk_array->buffer1); free(chunk_array); return NULL; diff --git a/fftma_module/gen/lib_src/covariance.c b/fftma_module/gen/lib_src/covariance.c index a96a652..4ff4030 100644 --- a/fftma_module/gen/lib_src/covariance.c +++ b/fftma_module/gen/lib_src/covariance.c @@ -4,14 +4,14 @@ /*builds the sampled covariance function*/ /*dimensions are even*/ -void covariance(chunk_array_t* covar, struct vario_mod variogram, struct grid_mod mesh, int n[3]) { - int i, j, k, maille, n2[3], symmetric; +void process(chunk_array_t* covar, struct vario_mod variogram, struct grid_mod mesh, int n[3], int n2[3], int init, int end) { + int i, j, k, maille, symmetric; double di, dj, dk; for (i = 0; i < 3; i++) n2[i] = n[i] / 2; - for (i = 0; i <= n2[0]; i++) { + for (i = init; i <= end; i++) { for (j = 0; j <= n2[1]; j++) { for (k = 0; k <= n2[2]; k++) { @@ -84,3 +84,36 @@ void covariance(chunk_array_t* covar, struct vario_mod variogram, struct grid_mo } } } + + +/*builds the sampled covariance function*/ +/*dimensions are even*/ +void covariance(chunk_array_t* covar, struct vario_mod variogram, struct grid_mod mesh, int n[3]) { + int n2[3]; + int i; + + for (i = 0; i < 3; i++) { + n2[i] = n[i] / 2; + } + + int init, end; + + // child process because return value zero + if (fork() == 0) { + printf("Covar child\n"); + init = 0; + end = n2[0]/2; + process(covar, variogram, mesh, n, n2, init, end); + exit(0); + } + // parent process because return value non-zero. + else { + printf("Covar parent\n"); + init = n2[0]/2 + 1; + end = n2[0]; + process(covar, variogram, mesh, n, n2, init, end); + wait(NULL); + } + + printf("finish covariance\n"); +}