Skip to content

Commit

Permalink
split out netcdf read&write functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EC2 Default User committed Apr 16, 2017
1 parent 12d5745 commit a751fca
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 96 deletions.
97 changes: 3 additions & 94 deletions Parallel_Algorithm/OpenMP/Kmean_omp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../shared/timing.h" //for timer seconds()
#include "../shared/make_2D_array.h"
#include "../shared/ncdf_util.h"
#include <stdio.h>
#include <stdlib.h>
#include <float.h> //for FLT_MAX
Expand All @@ -10,97 +11,6 @@
#define TOL 0.0001
#define MAX_ITER 100

/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

/* Read the input data from NetCDF file.
* Dynamically allocate the array based on the data size.
*
* Why need 3-levels of pointers:
* The first two levels are for 2D dynamic array,
* the last level is for modifying function arguments in place.
* (need to pass the address)
*/
int readX(float*** p_X,int*** p_GUESS,
size_t* p_N_samples,size_t* p_N_features,
size_t* p_N_clusters,size_t* p_N_repeat ) {
int ncid, varid,dimid;
int retval;

/* Open the file. NC_NOWRITE tells netCDF we want read-only access
* to the file.*/
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);

/* Get the size of the data for dynamical allocation*/
nc_inq_dimid(ncid,"N_samples",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_samples);
printf("Number of samples: %d \n",*p_N_samples);

nc_inq_dimid(ncid,"N_features",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_features);
printf("Number of features: %d \n",*p_N_features);

nc_inq_dimid(ncid,"N_clusters",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_clusters);
printf("Number of clusters: %d \n",*p_N_clusters);

nc_inq_dimid(ncid,"N_repeat",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_repeat);
printf("Number of repeated runs: %d \n",*p_N_repeat);

/* Get the varid of the data variable, based on its name. */
if ((retval = nc_inq_varid(ncid, "X", &varid)))
ERR(retval);
/* Read the data. */
*p_X = Make2DFloatArray(*p_N_samples,*p_N_features);
if ((retval = nc_get_var_float(ncid, varid, (*p_X)[0])))
ERR(retval);

/* Initial Guess*/
if ((retval = nc_inq_varid(ncid, "GUESS", &varid)))
ERR(retval);
*p_GUESS = Make2DIntArray(*p_N_repeat,*p_N_clusters);
if ((retval = nc_get_var_int(ncid, varid, (*p_GUESS)[0])))
ERR(retval);

/*close the netcdf file*/
if ((retval = nc_close(ncid) ))
ERR(retval);

printf("=====reading data finished======\n");

return 0;
}

int writeY(int* labels, float inert) {
int ncid, varid;
int retval;

if ((retval = nc_open(FILE_NAME, NC_WRITE, &ncid)))
ERR(retval);

if ((retval = nc_inq_varid(ncid, "INERT_C", &varid)))
ERR(retval)
if ((retval = nc_put_var_float(ncid, varid, &inert )))
ERR(retval);

if ((retval = nc_inq_varid(ncid, "Y_C", &varid)))
ERR(retval)
if ((retval = nc_put_var_int(ncid, varid, labels )))
ERR(retval);

/*close the netcdf file*/
if ((retval = nc_close(ncid) ))
ERR(retval);

printf("=====writting data finished======\n");

return 0;
}

// square of the distance between x1[N_features] and x2[N_features]
float distance(int N_features,float *x1,float *x2){
float dist=0.0;
Expand All @@ -109,7 +19,6 @@ float distance(int N_features,float *x1,float *x2){
return(dist);
}


int main() {

size_t N_samples,N_features,N_clusters,N_repeat;
Expand All @@ -122,7 +31,7 @@ int main() {

// get input data and its size
double iStart1 = seconds();
readX(&X,&GUESS,&N_samples,&N_features,&N_clusters,&N_repeat);
readX(FILE_NAME,&X,&GUESS,&N_samples,&N_features,&N_clusters,&N_repeat);
double iElaps1 = seconds() - iStart1;

// each data point belongs to which cluster
Expand Down Expand Up @@ -234,7 +143,7 @@ int main() {


// write data back to NetCDF file
writeY(labels_best, inert_best);
writeY(FILE_NAME,labels_best, inert_best);

// print summary
printf("Best inertia: %f \n",inert_best);
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 ../shared/make_2D_array.c Kmean_omp.c -o Kmean_omp.out
gcc -O2 -std=c99 -lnetcdf -fopenmp -lpthread ../shared/make_2D_array.c ../shared/ncdf_util.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
2 changes: 1 addition & 1 deletion Parallel_Algorithm/shared/make_2D_array.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "./make_2D_array.h"
#include <stdlib.h> //for malloc
#include "make_2D_array.h"

/* For dynamically allocating 2D array in pure-C environment.
Unlke in HW2, the array here is contagious!
Expand Down
5 changes: 5 additions & 0 deletions Parallel_Algorithm/shared/make_2D_array.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#ifndef MAKE_2D_ARRAY_H
#define MAKE_2D_ARRAY_H

float** Make2DFloatArray(int rows, int cols);
int** Make2DIntArray(int rows, int cols);

#endif // MAKE_2D_ARRAY_H
98 changes: 98 additions & 0 deletions Parallel_Algorithm/shared/ncdf_util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>
#include "make_2D_array.h"
#include "ncdf_util.h"
// including <stdlib.h> at last leads to "error: unknown type name ‘size_t’"
// no idea why?

/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

/* Read the input data from NetCDF file.
* Dynamically allocate the array based on the data size.
*
* Why need 3-levels of pointers:
* The first two levels are for 2D dynamic array,
* the last level is for modifying function arguments in place.
* (need to pass the address)
*/
int readX(char* FILE_NAME, float*** p_X,int*** p_GUESS,
size_t* p_N_samples,size_t* p_N_features,
size_t* p_N_clusters,size_t* p_N_repeat ) {
int ncid, varid,dimid;
int retval;

/* Open the file. NC_NOWRITE tells netCDF we want read-only access
* to the file.*/
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);

/* Get the size of the data for dynamical allocation*/
nc_inq_dimid(ncid,"N_samples",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_samples);
printf("Number of samples: %d \n",*p_N_samples);

nc_inq_dimid(ncid,"N_features",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_features);
printf("Number of features: %d \n",*p_N_features);

nc_inq_dimid(ncid,"N_clusters",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_clusters);
printf("Number of clusters: %d \n",*p_N_clusters);

nc_inq_dimid(ncid,"N_repeat",&dimid);
nc_inq_dimlen(ncid,dimid,p_N_repeat);
printf("Number of repeated runs: %d \n",*p_N_repeat);

/* Get the varid of the data variable, based on its name. */
if ((retval = nc_inq_varid(ncid, "X", &varid)))
ERR(retval);
/* Read the data. */
*p_X = Make2DFloatArray(*p_N_samples,*p_N_features);
if ((retval = nc_get_var_float(ncid, varid, (*p_X)[0])))
ERR(retval);

/* Initial Guess*/
if ((retval = nc_inq_varid(ncid, "GUESS", &varid)))
ERR(retval);
*p_GUESS = Make2DIntArray(*p_N_repeat,*p_N_clusters);
if ((retval = nc_get_var_int(ncid, varid, (*p_GUESS)[0])))
ERR(retval);

/*close the netcdf file*/
if ((retval = nc_close(ncid) ))
ERR(retval);

printf("=====reading data finished======\n");

return 0;
}

int writeY(char* FILE_NAME, int* labels, float inert) {
int ncid, varid;
int retval;

if ((retval = nc_open(FILE_NAME, NC_WRITE, &ncid)))
ERR(retval);

if ((retval = nc_inq_varid(ncid, "INERT_C", &varid)))
ERR(retval)
if ((retval = nc_put_var_float(ncid, varid, &inert )))
ERR(retval);

if ((retval = nc_inq_varid(ncid, "Y_C", &varid)))
ERR(retval)
if ((retval = nc_put_var_int(ncid, varid, labels )))
ERR(retval);

/*close the netcdf file*/
if ((retval = nc_close(ncid) ))
ERR(retval);

printf("=====writting data finished======\n");

return 0;
}
10 changes: 10 additions & 0 deletions Parallel_Algorithm/shared/ncdf_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef NCDF_UTIL_H
#define NCDF_UTIL_H

int readX(char* FILE_NAME, float*** p_X,int*** p_GUESS,
size_t* p_N_samples,size_t* p_N_features,
size_t* p_N_clusters,size_t* p_N_repeat );

int writeY(char* FILE_NAME, int* labels, float inert);

#endif // NCDF_UTIL_H

0 comments on commit a751fca

Please sign in to comment.