Skip to content

Commit

Permalink
use dspace class to wrap all the client function
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhezhe committed Oct 24, 2018
1 parent 505dddb commit 6301f4f
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/Python/dspaceswrapper/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018 The Python Packaging Authority

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions tests/Python/dspaceswrapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python wrapper for DataSpaces project
1 change: 1 addition & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "dspaceswrapper"
73 changes: 73 additions & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/dataspaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

import numpy as np
import dspaces
import copy

class dataspaceClient:

# input a multidimensional array and get the size of it
# input data should be an nparray
def getUpBound(this,lb,data):

shape = data.shape

if(len(lb)!=len(shape)):
print ('shape data shoule be in the same dimention with lb')
exit(-1)

dim = len(shape)
offset = np.ones(dim)

ub = np.asarray(lb)+np.asarray(shape) - offset
ub.astype(int)
return ub.tolist(),dim


def dspaces_init(this,comm,num_peers,appid):
dspaces.wrapper_dspaces_init(comm,num_peers,appid)

def dspaces_wrapper_finalize(this):
dspaces.wrapper_finalize()

def dspaces_lock_on_read(this,lock_name):
dspaces.wrapper_dspaces_lock_on_read(lock_name)

def dspaces_lock_on_write(this,lock_name):
dspaces.wrapper_dspaces_lock_on_write(lock_name)

def dspaces_get_data(this,var_name,ver,elemsize,lb,data):

originalShape = data.shape
ub,ndim = this.getUpBound(lb,data)


# transfer to one dimention
arraydataFlaten = data.flatten()

dspaces.wrapper_get_data(var_name,ver,elemsize,ndim,lb,ub,arraydataFlaten)

# reshape into original format
getdata = arraydataFlaten.reshape(originalShape)
return getdata


def dspaces_put_data(this,var_name,ver,elemsize,lb,data):

arraydata = np.asarray(data)
ub,ndim = this.getUpBound(lb,arraydata)

# transfer to one dimention
arraydata = arraydata.flatten()

dspaces.wrapper_put_data(var_name,ver,elemsize,ndim,lb,ub,arraydata)

def dspaces_unlock_on_read(this,lock_name):
dspaces.wrapper_dspaces_unlock_on_read(lock_name)

def dspaces_unlock_on_write(this,lock_name):
dspaces.wrapper_dspaces_unlock_on_write(lock_name)





112 changes: 112 additions & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/dspaces.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* put.c : Example 1: DataSpaces put tutorial
* This example will show you the simplest way
* to put a 1D array of 3 elements into the DataSpace.
* */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "dataspaces.h"
#include "mpi.h"

void wrapper_dspaces_init(MPI_Comm pgcomm, int num_peers, int appid)
{
int nprocs;
int rank;
MPI_Comm_size(pgcomm, &nprocs);
MPI_Comm_rank(pgcomm, &rank);
MPI_Comm gcomm = pgcomm;
MPI_Barrier(gcomm);

printf("num_peers %d appid %d\n", num_peers, appid);

// Initalize DataSpaces
// # of Peers, Application ID, ptr MPI comm, additional parameters
// # Peers: Number of connecting clients to the DS server
// Application ID: Unique idenitifier (integer) for application
// Pointer to the MPI Communicator, allows DS Layer to use MPI barrier func
// Addt'l parameters: Placeholder for future arguments, currently NULL.
// the first parameter should be same with the number of threads to access server (namely -n after mpiexec)
// the second parameter is used to label current application, one application/program should have unique appid
// init is only needed to call once for multiple timesteps
dspaces_init(num_peers, appid, &gcomm, NULL);

return;
}

int wrapper_put_data(const char *var_name,
unsigned int timestep, int size,
int ndim, unsigned long long *lb, int n1, unsigned long long *ub, int n2, double *data, int n)
{

//printf("Timestep %d: put data %lf %lf %lf\n", timestep, data[0], data[1], data[2]);
//TODO if debug
/*
int i=0;
for (i=0;i<ndim;i++){
printf("index lb %d is %d\n", i, lb[i]);
}
for (i=0;i<ndim;i++){
printf("index ub %d is %d\n", i, ub[i]);
}
*/


return dspaces_put(var_name, timestep, size, ndim, (uint64_t *)lb, (uint64_t *)ub, data);
}

int wrapper_get_data(const char *var_name,
unsigned int timestep, int size,
int ndim, unsigned long long *lb, int n1, unsigned long long *ub, int n2, double *data, int n)
{

//TODO if debug
/*
int i=0;
for (i=0;i<ndim;i++){
printf("index lb %d is %d\n", i, lb[i]);
}
for (i=0;i<ndim;i++){
printf("index ub %d is %d\n", i, ub[i]);
}
*/

int rcode = dspaces_get(var_name, timestep, size, ndim, (uint64_t *)lb, (uint64_t *)ub, data);
return 0;
}

void wrapper_dspaces_lock_on_write(char *varname)
{
//printf("get varname %s\n", varname);
MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_lock_on_write(varname, &gcomm);
return;
}

void wrapper_dspaces_unlock_on_write(char *varname)
{
MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_unlock_on_write(varname, &gcomm);
return;
}

void wrapper_dspaces_lock_on_read(char *varname)
{
//printf("get varname %s\n", varname);
MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_lock_on_read(varname, &gcomm);
return;
}

void wrapper_dspaces_unlock_on_read(char *varname)
{
MPI_Comm gcomm = MPI_COMM_WORLD;
dspaces_unlock_on_read(varname, &gcomm);
return;
}

int wrapper_finalize()
{
common_finalize();
}
47 changes: 47 additions & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/dspaces.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
%module dspaces

%{
#define SWIG_FILE_WITH_INIT
#define SWIG_PYTHON_STRICT_BYTE_CHAR
#include "mpi.h"
#include "dspaces.c"
%}


%include mpi4py/mpi4py.i
%include "numpy.i"

%init %{
import_array();
%}


%mpi4py_typemap(Comm, MPI_Comm);

%apply (double* IN_ARRAY1, int DIM1) {(double* data, int n)};

%apply (unsigned long long * IN_ARRAY1, int DIM1) {(unsigned long long * lb, int n1)};

%apply (unsigned long long * IN_ARRAY1, int DIM1) {(unsigned long long * ub, int n2)};

void wrapper_dspaces_init(MPI_Comm pgcomm,int num_peers, int appid);

void wrapper_dspaces_lock_on_write(char *varname);

void wrapper_dspaces_unlock_on_write(char *varname);

void wrapper_dspaces_lock_on_read(char *varname);

void wrapper_dspaces_unlock_on_read(char *varname);



void wrapper_finalize();

int wrapper_put_data(const char *var_name,
unsigned int ver, int size,
int ndim, unsigned long long *lb,int n1, unsigned long long*ub, int n2, double *data, int n);

int wrapper_get_data(const char *var_name,
unsigned int ver, int size,
int ndim, unsigned long long *lb,int n1, unsigned long long*ub, int n2, double *data, int n);
39 changes: 39 additions & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.PHONY: default
default: runtest

PYTHON = python
PYTHON_CONFIG = ${PYTHON} ../../python-config
MPI4PY_INCLUDE = ${shell ${PYTHON} -c 'import mpi4py; print( mpi4py.get_include() )'}
NUMPY_INCLUDE = ${shell ${PYTHON} -c 'import numpy; print( numpy.get_include() )'}
PREFIX = /home1/zw241/dataspaces
INCLUDES = -I$(PREFIX)/include
DSCOMMON = ../../../C/common.o
LFLAG = -L/home1/zw241/dataspaces/install/lib -ldspaces -ldart -ldscommon -ldspacesf -lpthread -lm -lrt -libverbs -lrdmacm
CFLAGS = -Wall -g

SWIG = swig
SWIG_PY = ${SWIG} -python
.PHONY: src
src: dspaces_wrap.c
dspaces_wrap.c: dspaces.i
[ -f ./numpy.i ] && echo "numpy.i already here, good" || curl -O https://raw.githubusercontent.com/numpy/numpy/master/tools/swig/numpy.i
${SWIG_PY} -I${MPI4PY_INCLUDE} -I${NUMPY_INCLUDE} $(INCLUDES) -o $@ $<

MPICC = mpicc
CFLAGS = -fPIC ${shell ${PYTHON_CONFIG} --includes}
LDFLAGS = -shared ${shell ${PYTHON_CONFIG} --libs}
SO = ${shell ${PYTHON_CONFIG} --extension-suffix}

.PHONY: buildput
buildput: _dspaces${SO}
_dspaces${SO}: dspaces_wrap.c
${MPICC} ${CFLAGS} -I${MPI4PY_INCLUDE} -I${NUMPY_INCLUDE} $(INCLUDES) $(DSCOMMON) $(LFLAG) -o $@ $< ${LDFLAGS}

.PHONY: runtest
runtest: buildput
/bin/bash ./testrun.sh


.PHONY: clean
clean:
${RM} -r dspaces_wrap.c dspaces.py* _dspaces*.so *pycache*
4 changes: 4 additions & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


This part use python script to call the DataSpaces api directly without extra c code wrapping

55 changes: 55 additions & 0 deletions tests/Python/dspaceswrapper/dspaceswrapper/testget1d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from mpi4py import MPI
import numpy as np
import dataspaces
import ctypes
import os
import time

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

# copy all conf.* file to current dir
serverdir = "/home1/zw241/dataspaces/tests/C"

confpath = serverdir+"/conf*"

copyCommand = "cp "+confpath+" ."

os.system(copyCommand)

# number of clients at clients end to join server
num_peers= 1
appid = 2


var_name = "ex1_sample_data"
lock_name = "my_test_lock"

data = np.array([0.0,0.0,0.0])

ds = dataspaces.dataspaceClient()

ds.dspaces_init(comm,num_peers,appid)


for ver in range (2):

ds.dspaces_lock_on_read(lock_name)
elemsize = ctypes.sizeof(ctypes.c_double)

lb = [0+ver*3]
ub,ndim = ds.getUpBound(lb,data)

getdata=ds.dspaces_get_data(var_name,ver,elemsize,lb,data)

print ("get data")
print (getdata)

ds.dspaces_unlock_on_read(lock_name)

time.sleep(1)


ds.dspaces_wrapper_finalize()
MPI.Finalize()

Loading

0 comments on commit 6301f4f

Please sign in to comment.