Skip to content

Commit

Permalink
split out make_2D_array
Browse files Browse the repository at this point in the history
  • Loading branch information
EC2 Default User committed Apr 16, 2017
1 parent 1f2aa44 commit 12d5745
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
25 changes: 1 addition & 24 deletions Parallel_Algorithm/OpenMP/Kmean_omp.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "../shared/timing.h" //for timer seconds()
#include "../shared/make_2D_array.h"
#include <stdio.h>
#include <stdlib.h>
#include <float.h> //for FLT_MAX
#include <netcdf.h>


/* This is the name of the data file we will read. */
#define FILE_NAME "../test_data/Blobs_smp20000_fea30_cls8.nc"
#define TOL 0.0001
Expand All @@ -15,29 +15,6 @@
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

/* For dynamically allocating 2D array in pure-C environment.
Unlke in HW2, the array here is contagious!
See:
http:https://stackoverflow.com/questions/33794657/how-to-pass-a-2d-array-to-a-function-in-c-when-the-array-is-formatted-like-this
http:https://stackoverflow.com/questions/5901476/sending-and-receiving-2d-array-over-mpi
*/
float** Make2DFloatArray(int rows, int cols) {
float *data = (float *)malloc(rows*cols*sizeof(float));
float **array= (float **)malloc(rows*sizeof(float*));
for (int i=0; i<rows; i++)
array[i] = &(data[cols*i]);

return array;
}
int** Make2DIntArray(int rows, int cols) {
int *data = (int *)malloc(rows*cols*sizeof(int));
int **array= (int **)malloc(rows*sizeof(int*));
for (int i=0; i<rows; i++)
array[i] = &(data[cols*i]);

return array;
}

/* Read the input data from NetCDF file.
* Dynamically allocate the array based on the data size.
*
Expand Down
2 changes: 1 addition & 1 deletion Parallel_Algorithm/OpenMP/compile.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
gcc -O2 -std=c99 -lnetcdf -fopenmp -lpthread Kmean_omp.c -o Kmean_omp.out
gcc -O2 -std=c99 -lnetcdf -fopenmp -lpthread ../shared/make_2D_array.c Kmean_omp.c -o Kmean_omp.out

#For debugging with gdb
#gcc -g -O0 -std=c99 -lnetcdf Kmean_seq.c -o Kmean_seq.out
27 changes: 27 additions & 0 deletions Parallel_Algorithm/shared/make_2D_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "./make_2D_array.h"
#include <stdlib.h> //for malloc

/* For dynamically allocating 2D array in pure-C environment.
Unlke in HW2, the array here is contagious!
See:
http:https://stackoverflow.com/questions/33794657/how-to-pass-a-2d-array-to-a-function-in-c-when-the-array-is-formatted-like-this
http:https://stackoverflow.com/questions/5901476/sending-and-receiving-2d-array-over-mpi
*/
float** Make2DFloatArray(int rows, int cols) {
float *data = (float *)malloc(rows*cols*sizeof(float));
float **array= (float **)malloc(rows*sizeof(float*));
for (int i=0; i<rows; i++)
array[i] = &(data[cols*i]);

return array;
}
int** Make2DIntArray(int rows, int cols) {
int *data = (int *)malloc(rows*cols*sizeof(int));
int **array= (int **)malloc(rows*sizeof(int*));
for (int i=0; i<rows; i++)
array[i] = &(data[cols*i]);

return array;
}


2 changes: 2 additions & 0 deletions Parallel_Algorithm/shared/make_2D_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
float** Make2DFloatArray(int rows, int cols);
int** Make2DIntArray(int rows, int cols);

0 comments on commit 12d5745

Please sign in to comment.