Skip to content

Commit

Permalink
Update send custom int & float transfer
Browse files Browse the repository at this point in the history
send custom int and float

replace free by delete in MDDriver_setForces

update IMDport_pointer in IIMD_init
-> server can read the open port.

fix bug custom data in servertest

fix: add method for custom data to get size and avoid turn off new data
  • Loading branch information
lanrezac committed Jun 18, 2024
1 parent 96b5a9c commit 5b08216
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 62 deletions.
20 changes: 14 additions & 6 deletions plugins/UnityMol/UMolMDDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ API int MDDriver_getNbCustomInt(MDDriverAdapter *mddinstance);
API int MDDriver_getPositions(MDDriverAdapter *mddinstance, float *verts, int nbParticles);
API int MDDriver_getCustomFloat(MDDriverAdapter *mddinstance, char * datanamefloat, float *customfloat, int nbfloat);
API int MDDriver_getCustomInt(MDDriverAdapter *mddinstance, char * datanameint, int * customint, int nbint);
API void MDDriver_sendCustomInt(MDDriverAdapter *mddinstance, int nbint, const char *datanameint, int *customint);
API void MDDriver_sendCustomFloat(MDDriverAdapter *mddinstance, int nbfloat, const char *datanamefloat, float *customfloat);
API void MDDriver_pause(MDDriverAdapter *mddinstance);
API void MDDriver_play(MDDriverAdapter *mddinstance);
API void MDDriver_setForces(MDDriverAdapter *mddinstance, int nbforces, int *atomslist, float *forceslist);
Expand Down Expand Up @@ -205,14 +207,14 @@ int MDDriver_getNbParticles(MDDriverAdapter *mddinstance) {

int MDDriver_getNbCustomFloat(MDDriverAdapter *mddinstance) {
if (mddinstance->N_floats <= 0) {
IIMD_get_custom_float( &(mddinstance->datanamefloat), &(mddinstance->N_floats), (float **) & (mddinstance->customfloat) );
IIMD_get_nb_custom_float(&(mddinstance->N_floats));
}
return mddinstance->N_floats;
}

int MDDriver_getNbCustomInt(MDDriverAdapter *mddinstance) {
if (mddinstance->N_ints <= 0) {
IIMD_get_custom_int( &(mddinstance->datanameint), &(mddinstance->N_ints), (int **) & (mddinstance->customint) );
IIMD_get_nb_custom_int(&(mddinstance->N_ints));
}
return mddinstance->N_ints;
}
Expand All @@ -234,20 +236,26 @@ int MDDriver_getPositions(MDDriverAdapter *mddinstance, float *coordinates, int


int MDDriver_getCustomFloat(MDDriverAdapter *mddinstance, char * datanamefloat, float *customfloat, int nbfloat) {
IIMD_get_custom_float(&(mddinstance->datanamefloat), &(mddinstance->N_floats), (float **) & (mddinstance->customfloat) );
int rc = IIMD_get_custom_float(&(mddinstance->datanamefloat), &(mddinstance->N_floats), (float **) & (mddinstance->customfloat) );
memcpy(customfloat, mddinstance->customfloat, nbfloat * sizeof(float));
memcpy(datanamefloat, mddinstance->datanamefloat, 8 * sizeof(char));
return nbfloat;
return rc;
}

int MDDriver_getCustomInt(MDDriverAdapter *mddinstance, char * datanameint, int * customint, int nbint) {
IIMD_get_custom_float(&(mddinstance->datanameint) , &(mddinstance->N_ints), (float **) & (mddinstance->customint) );
int rc = IIMD_get_custom_int(&(mddinstance->datanameint) , &(mddinstance->N_ints), (int **) & (mddinstance->customint) );
memcpy(customint, mddinstance->customint, nbint * sizeof(int));
memcpy(datanameint, mddinstance->datanameint, 8 * sizeof(char));
return nbint;
return rc;
}

void MDDriver_sendCustomInt(MDDriverAdapter *mddinstance, int nbint, const char *datanameint, int *customint) {
IIMD_send_custom_int(datanameint, &nbint, customint);
}

void MDDriver_sendCustomFloat(MDDriverAdapter *mddinstance, int nbfloat, const char *datanamefloat, float *customfloat) {
IIMD_send_custom_float(datanamefloat, &nbfloat, customfloat);
}

void MDDriver_setForces(MDDriverAdapter *mddinstance, int nbforces, int *atomslist, float * forceslist) {
if (mddinstance->nb_forces != nbforces || mddinstance->forces_list == NULL) {
Expand Down
6 changes: 6 additions & 0 deletions protocol/include/imd.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ typedef struct
float sizecellz; //!< Size of the cell on z axis
} IMDGrid;

typedef struct
{
char *dataname ;
int nb;
void *data;
} IMDCustomData;


// Swap little <-> big endian
Expand Down
18 changes: 18 additions & 0 deletions protocol/include/imd_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ extern imd_int64 imd_value;
#define IIMD_send_custom_float iimd_send_custom_float_
#define IIMD_send_custom_int iimd_send_custom_int_
#define IIMD_get_custom_float iimd_get_custom_float_
#define IIMD_get_nb_custom_float iimd_get_nb_custom_float_
#define IIMD_get_custom_int iimd_get_custom_int_
#define IIMD_get_nb_custom_int iimd_get_nb_custom_int_
#define IIMD_get_coords iimd_get_coords_
#define IIMD_get_energies iimd_get_energies_
#define IIMD_get_grid iimd_get_grid_
Expand All @@ -89,7 +91,9 @@ extern imd_int64 imd_value;
#define IIMD_send_custom_float iimd_send_custom_float
#define IIMD_send_custom_int iimd_send_custom_int
#define IIMD_get_custom_float iimd_get_custom_float
#define IIMD_get_nb_custom_float iimd_get_nb_custom_float
#define IIMD_get_custom_int iimd_get_custom_int
#define IIMD_get_nb_custom_int iimd_get_nb_custom_int
#define IIMD_get_coords iimd_get_coords
#define IIMD_get_energies iimd_get_energies
#define IIMD_get_grid iimd_get_grid
Expand Down Expand Up @@ -241,6 +245,13 @@ Get custom float data
*/
extern int IIMD_get_custom_float(char ** dataname, int *n_floats, float **data);

/**
Get custom number of float data
@param n_floats number of floats
@return return number of floats
*/
extern int IIMD_get_nb_custom_float(int *n_floats);

/**
Get custom int data
@param dataname string id of the data array
Expand All @@ -250,6 +261,13 @@ Get custom int data
*/
extern int IIMD_get_custom_int(char ** dataname, int *n_ints, int **data);

/**
Get custom number of int data
@param n_ints number of ints
@return return number of ints
*/
extern int IIMD_get_nb_custom_int(int *n_ints);


/**
Terminate properly a MDDriver connection
Expand Down
83 changes: 81 additions & 2 deletions protocol/src/imd_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ void IIMD_treatprotocol()

if ( (vmd_length == 0) || ( n_forces == 0) )
{
fprintf(IMDlog, "MDDriver > Warning no force \n");
if (IMDmsg > 0)
fprintf(IMDlog, "MDDriver > Warning no force \n");
free( vmd_atoms );
free( vmd_forces );
vmd_atoms = 0; vmd_forces = 0;
Expand Down Expand Up @@ -567,6 +568,11 @@ void IIMD_treatprotocol()
fprintf(IMDlog, "MDDriver > Error reading custom float, killing connection\n");
SL_DelSocket(i_client);
}
else
{
if ( IMDswap ) imd_swap4( (char *) vmd_custom_float, vmd_Nfloat);
vmd_new_custom_float = 1;
}
}

if (IMDmsg > 0)
Expand Down Expand Up @@ -609,6 +615,11 @@ void IIMD_treatprotocol()
fprintf(IMDlog, "MDDriver > Error reading custom int, killing connection\n");
SL_DelSocket(i_client);
}
else
{
if ( IMDswap ) imd_swap4( (char *) vmd_custom_int, vmd_Nint);
vmd_new_custom_int = 1;
}
}

if (IMDmsg > 0)
Expand Down Expand Up @@ -719,7 +730,7 @@ void IIMD_treatprotocol()
if (vmd_Nint != 0)
free( vmd_custom_int );
vmd_Nint=vmd_length;
vmd_custom_int = (int*) malloc( vmd_Nfloat * sizeof(int));
vmd_custom_int = (int*) malloc( vmd_Nint * sizeof(int));
if (imd_recv_custom_int(sock, customdatanameint, vmd_Nint, vmd_custom_int))
{
fprintf(IMDlog, "MDDriver > \n");
Expand Down Expand Up @@ -848,6 +859,8 @@ FILE *IIMD_init( const char *hostname, imd_int32 *mode, imd_int32 *IMD
fprintf(IMDlog, "MDDriver > Interactive MD bind to /%s/%d \n", str, IMDport );
}

*IMDport_ = IMDport;

fflush( IMDlog );

// Listening incomming requests on sock
Expand Down Expand Up @@ -1180,6 +1193,15 @@ void IIMD_send_custom_float ( const char * dataname, int *n_floats , float * dat
imd_send_custom_float(clientsock, dataname, N, data);
}

// From client side to server
if(sock && !IMDserver)
{
if ( IMDswap ) imd_swap4( data, N);
if (imd_send_custom_float(sock, dataname, N, data)) {
fprintf( IMDlog, "MDDriver > ---- Failed to send custom float in %s\n", __FUNCTION__);
}
}

if (IMDmsg >= 2)
{
fprintf(IMDlog, "MDDriver > \n");
Expand Down Expand Up @@ -1213,6 +1235,15 @@ void IIMD_send_custom_int ( const char * dataname, int *n_ints, int * data )
imd_send_custom_int(clientsock, dataname, N, data);
}

// From client side to server
if(sock && !IMDserver)
{
if ( IMDswap ) imd_swap4( data, N);
if (imd_send_custom_int(sock, dataname, N, data)) {
fprintf( IMDlog, "MDDriver > ---- Failed to send custom int in %s\n", __FUNCTION__);
}
}

if (IMDmsg >= 2)
{
fprintf(IMDlog, "MDDriver > \n");
Expand Down Expand Up @@ -1398,6 +1429,30 @@ int IIMD_get_custom_float (char ** dataname, int *n_floats, float **data
return rc;
}

int IIMD_get_nb_custom_float (int *n_floats) {
int rc = 0;

if (IMDmsg >= 1)
fprintf(IMDlog, "MDDriver > ---- Entering in %s\n", __FUNCTION__);

if (vmd_new_custom_float) {
*n_floats = vmd_Nfloat;
rc = vmd_Nfloat;

if (IMDmsg >= 2) {
fprintf(IMDlog, "MDDriver > \n");
fprintf(IMDlog, "MDDriver > Get number of float (step %d) \n", vmd_energies.tstep);
fprintf(IMDlog, "MDDriver > ------------------------------------------ \n");
fprintf(IMDlog, "MDDriver > Number of floats: %d\n", *n_floats);
}
}

if (IMDmsg >= 1)
fprintf(IMDlog, "MDDriver > ---- Leaving %s\n", __FUNCTION__);

return rc;
}

int IIMD_get_custom_int (char ** dataname, int *n_ints, int **data)
{
int rc = 0;
Expand Down Expand Up @@ -1432,6 +1487,30 @@ int IIMD_get_custom_int (char ** dataname, int *n_ints, int **data)
return rc;
}

int IIMD_get_nb_custom_int (int *n_ints) {
int rc = 0;

if (IMDmsg >= 1)
fprintf(IMDlog, "MDDriver > ---- Entering in %s\n", __FUNCTION__);

if (vmd_new_custom_int) {
*n_ints = vmd_Nint;
rc = vmd_Nint;

if (IMDmsg >= 2) {
fprintf(IMDlog, "MDDriver > \n");
fprintf(IMDlog, "MDDriver > Get number of int (step %d) \n", vmd_energies.tstep);
fprintf(IMDlog, "MDDriver > ------------------------------------------ \n");
fprintf(IMDlog, "MDDriver > Number of ints: %d\n", *n_ints);
}
}

if (IMDmsg >= 1)
fprintf(IMDlog, "MDDriver > ---- Leaving %s\n", __FUNCTION__);

return rc;
}


int IIMD_get_energies( IMDEnergies **energ_ )
{
Expand Down
43 changes: 25 additions & 18 deletions protocol/test_exe/clienttest.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,18 @@ int main()
int atoms_list[N_FORCE];//Force
float forces_list[N_FORCE*3];

int nbfloat;
float * customfloat;
char * datanamefloat=(char *) malloc(sizeof(char)*8);

int nbint;
int * customint;
char * datanameint=(char *) malloc(sizeof(char)*8);

int nb = 5;
int intdata[5] = {6, 7, 8, 9, 10};
float floatdata[5] = {6.0, 7.0, 8.0, 9.0, 10.0};

// cintCS : custom int Client Server
IMDCustomData float_send = {.dataname="cfloCS", .nb=nb, .data=&floatdata};
IMDCustomData int_send = {.dataname="cintCS", .nb=nb, .data=&intdata};

IMDCustomData float_get, int_get;
float_get.dataname = (char *) malloc(sizeof(char)*8);
int_get.dataname = (char *) malloc(sizeof(char)*8);

int cont = 1; // Main loop control
int ffreq = 10;
int i;
Expand Down Expand Up @@ -266,36 +270,36 @@ int main()
}
}

if ( IIMD_get_custom_float(&datanamefloat, &nbfloat, (float **) &customfloat) )
if ( IIMD_get_custom_float(&float_get.dataname, &float_get.nb, (float**)&float_get.data) )
{
if (log)

{
fprintf(IMDlog, "MYMDD > \n");
fprintf(IMDlog, "MYMDD > Receive float array (Time step=%d)\n" , i);
fprintf(IMDlog, "MYMDD > ================================\n");
fprintf(IMDlog, "MYMDD > \n");
fprintf(IMDlog, "MYMDD > MYPROGRAM float array %s \n", datanamefloat);
fprintf(IMDlog, "MYMDD > MYPROGRAM float array %s \n", float_get.dataname);
fprintf(IMDlog, "MYMDD > ------------------------------------------ \n");
for (unsigned j = 0; j < nbfloat; j++)
for (unsigned j = 0; j < float_get.nb; j++)
{
fprintf(IMDlog, "MYMDD > float array[%d] = %f\n", j, customfloat[j]);
fprintf(IMDlog, "MYMDD > float array[%d] = %f\n", j, ((float*)(float_get.data))[j]);
}
fprintf(IMDlog, "MYMDD > \n");
}
}
if ( IIMD_get_custom_int(&datanameint, &nbint, (int **) &customint) )
if ( IIMD_get_custom_int(&int_get.dataname, &int_get.nb, (int**)&int_get.data) )
{
if (log)

{
fprintf(IMDlog, "MYMDD > \n");
fprintf(IMDlog, "MYMDD > Receive int array (Time step=%d)\n" , i);
fprintf(IMDlog, "MYMDD > ================================\n");
fprintf(IMDlog, "MYMDD > \n");
fprintf(IMDlog, "MYMDD > MYPROGRAM int array %s \n", datanameint);
fprintf(IMDlog, "MYMDD > MYPROGRAM int array %s \n", int_get.dataname);
fprintf(IMDlog, "MYMDD > ------------------------------------------ \n");
for (unsigned j = 0; j < nbint; j++)
for (unsigned j = 0; j < int_get.nb; j++)
{
fprintf(IMDlog, "MYMDD > int array[%d] = %d\n", j, customint[j]);
fprintf(IMDlog, "MYMDD > int array[%d] = %d\n", j, ((int*)(int_get.data))[j]);
}
fprintf(IMDlog, "MYMDD > \n");
}
Expand All @@ -307,6 +311,9 @@ int main()
{
IIMD_send_forces ( &N_forces, atoms_list, forces_list );
}

IIMD_send_custom_float(float_send.dataname, &float_send.nb, float_send.data);
IIMD_send_custom_int(int_send.dataname, &int_send.nb, int_send.data);
}
#if !defined(_WIN32)
// Deals with keyboard events
Expand Down
Loading

0 comments on commit 5b08216

Please sign in to comment.