From f55f466128b805ebfd85fb839cfb901f497d3dbb Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Wed, 22 Feb 2017 16:52:42 -0600
Subject: [PATCH] Copied VMD cvs versions of imd.C and imd.h and cleaned up
 unused variable causing compiler warnings

---
 README                |  5 +--
 src/ComputeForce.cu   | 10 +++---
 src/GrandBrownTown.cu | 15 +++++----
 src/imd.c             | 74 ++++++++++++++++++++++++++++---------------
 src/imd.h             | 48 +++++++++++-----------------
 5 files changed, 80 insertions(+), 72 deletions(-)

diff --git a/README b/README
index 935089f..3abef46 100644
--- a/README
+++ b/README
@@ -111,9 +111,6 @@ Terrance Howard
   particles further than the pairlist distance move to within the
   cutoff
 
-* Failed assertions deliver messages that don't describe where the
-  failure occured
-
 -- Bugs --
 
 * An issue with memory management causes some invalid memory to be
@@ -124,6 +121,6 @@ Terrance Howard
 * A large amount of GPU memory for pairlists is allocated statically,
   which may cause out-of-memory crashes in older hardware
 
-* If the number of pairs in the sytem exceeds the length of the array
+* If the number of pairs in the system exceeds the length of the array
   allocated for pairlists, the non-bonded kernel will try to access
   forbidden regions of memory, causing a crash
diff --git a/src/ComputeForce.cu b/src/ComputeForce.cu
index 77b439a..54a6ca2 100644
--- a/src/ComputeForce.cu
+++ b/src/ComputeForce.cu
@@ -405,12 +405,10 @@ bool ComputeForce::addDihedralPotential(String fileName, int ind, Dihedral dihed
 
 void ComputeForce::decompose() {
 	gpuErrchk( cudaProfilerStart() );
+
 	// Reset the cell decomposition.
-	bool newDecomp = false;
 	if (decomp_d)
 		cudaFree(decomp_d);
-	else
-		newDecomp = true;
 		
 	decomp.decompose_d(pos_d, num);
 	decomp_d = decomp.copyToCUDA();
@@ -426,15 +424,15 @@ void ComputeForce::decompose() {
 	
 	// initializePairlistArrays
 	int nCells = decomp.nCells.x * decomp.nCells.y * decomp.nCells.z;
-	int blocksPerCell = 10;
+	// int blocksPerCell = 10;
 
 	
 	/* cuMemGetInfo(&free,&total); */
 	/* printf("Free memory: %zu / %zu\n", free, total); */
 	
-	const int NUMTHREADS = 128;
+	// const int NUMTHREADS = 128;
 	//const size_t nBlocks = (num * numReplicas) / NUM_THREADS + 1;
-	const size_t nBlocks = nCells*blocksPerCell;
+	// const size_t nBlocks = nCells*blocksPerCell;
 
 	/* clearPairlists<<< 1, 32 >>>(pos, num, numReplicas, sys_d, decomp_d); */
 	/* gpuErrchk(cudaDeviceSynchronize()); */
diff --git a/src/GrandBrownTown.cu b/src/GrandBrownTown.cu
index eedfdc1..9584117 100644
--- a/src/GrandBrownTown.cu
+++ b/src/GrandBrownTown.cu
@@ -413,7 +413,7 @@ void GrandBrownTown::run() {
 	for (long int s = 1; s < steps; s++) {
 		// Compute the internal forces. Only calculate the energy when we are about to output.
 		bool get_energy = ((s % outputEnergyPeriod) == 0);
-		float energy = 0.0f;
+		// float energy = 0.0f;
 
 		// Set the timer
 		wkf_timer_start(cputimer);
@@ -439,10 +439,11 @@ void GrandBrownTown::run() {
 						
 						//MLog: added Bond* bondList to the list of passed in variables.
 						/*energy = internal->computeTabulated(forceInternal_d, pos_d, type_d, bonds_d, bondMap_d, excludes_d, excludeMap_d,	angles_d, dihedrals_d, get_energy);*/
-						energy = internal -> computeTabulated(get_energy);
+						// energy = internal -> computeTabulated(get_energy);
+						internal -> computeTabulated(get_energy);
 						break;
 					default: // [ N^2 ] interactions, no cutoff | decompositions
-						energy = internal->computeTabulatedFull(get_energy);
+					    internal->computeTabulatedFull(get_energy);
 						break;
 				}
 			
@@ -458,19 +459,19 @@ void GrandBrownTown::run() {
 							internal->decompose();
 							RBC.updateParticleLists( internal->getPos_d() );
 						}
-						energy = internal->compute(get_energy);
+						internal->compute(get_energy);
 						break;
 
 					case 1: // Do not use cutoff
-						energy = internal->computeFull(get_energy);
+						internal->computeFull(get_energy);
 						break;
 
 					case 2: // Compute only softcore forces.
-						energy = internal->computeSoftcoreFull(get_energy);
+						internal->computeSoftcoreFull(get_energy);
 						break;
 
 					case 3: // Compute only electrostatic forces.
-						energy = internal->computeElecFull(get_energy);
+						internal->computeElecFull(get_energy);
 						break;
 				}
 			}
diff --git a/src/imd.c b/src/imd.c
index 0097a98..779ef57 100644
--- a/src/imd.c
+++ b/src/imd.c
@@ -1,11 +1,34 @@
-
-
+/***************************************************************************
+ *cr
+ *cr            (C) Copyright 1995-2016 The Board of Trustees of the
+ *cr                        University of Illinois
+ *cr                         All Rights Reserved
+ *cr
+ ***************************************************************************/
+
+/***************************************************************************
+ * RCS INFORMATION:
+ *
+ *      $RCSfile: imd.C,v $
+ *      $Author: johns $        $Locker:  $             $State: Exp $
+ *      $Revision: 1.19 $       $Date: 2016/11/28 03:05:07 $
+ *
+ ***************************************************************************
+ * DESCRIPTION:
+ *  Lowest level interactive MD communication routines.
+ *
+ * LICENSE:
+ *   UIUC Open Source License
+ *   http://www.ks.uiuc.edu/Research/vmd/plugins/pluginlicense.html
+ *
+ ***************************************************************************/
 #include "imd.h"
 #include "vmdsock.h"
 #include <string.h>
 #include <errno.h>
 #include <stdlib.h>
 
+/// IMD communication protocol message header structure
 typedef struct {
   int32 type;
   int32 length;
@@ -14,20 +37,16 @@ typedef struct {
 #define HEADERSIZE 8
 #define IMDVERSION 2
 
-static void swap4(char *data, int ndata) {
-  int i;
-  char *dataptr;
-  char b0, b1;
-
-  dataptr = data;
-  for (i=0; i<ndata; i+=4) {
-    b0 = dataptr[0];
-    b1 = dataptr[1];
-    dataptr[0] = dataptr[3];
-    dataptr[1] = dataptr[2];
-    dataptr[2] = b1;
-    dataptr[3] = b0;
-    dataptr += 4;
+/* Only works with aligned 4-byte quantities, will cause a bus error */
+/* on some platforms if used on unaligned data.                      */
+void swap4_aligned(void *v, long ndata) {
+  int *data = (int *) v;
+  long i;
+  int *N;
+  for (i=0; i<ndata; i++) {
+    N = data + i;
+    *N=(((*N>>24)&0xff) | ((*N&0xff)<<24) |
+        ((*N>>8)&0xff00) | ((*N&0xff00)<<8));
   }
 }
 
@@ -40,6 +59,7 @@ static int32 imd_htonl(int32 h) {
   return n;
 }
 
+/// structure used to perform byte swapping operations 
 typedef struct {
   unsigned int highest : 8;
   unsigned int high    : 8;
@@ -50,7 +70,8 @@ typedef struct {
 static int32 imd_ntohl(int32 n) {
   int32 h = 0;
   netint net;
-  net = *((netint *)&n);
+
+  memcpy((void *)&net,(void *)&n, sizeof(n));
   h |= net.highest << 24 | net.high << 16 | net.low << 8 | net.lowest;
   return h;
 }
@@ -144,11 +165,11 @@ int imd_trate(void *s, int32 rate) {
 /* Data methods */
 int imd_send_mdcomm(void *s,int32 n,const int32 *indices,const float *forces) {
   int rc;
-  int32 size = HEADERSIZE+16*n;
+  int32 size = HEADERSIZE+16L*n;
   char *buf = (char *) malloc(sizeof(char) * size); 
   fill_header((IMDheader *)buf, IMD_MDCOMM, n);
-  memcpy(buf+HEADERSIZE, indices, 4*n);
-  memcpy(buf+HEADERSIZE+4*n, forces, 12*n);
+  memcpy(buf+HEADERSIZE, indices, 4L*n);
+  memcpy(buf+HEADERSIZE+4*n, forces, 12L*n);
   rc = (imd_writen(s, buf, size) != size);
   free(buf);
   return rc;
@@ -167,10 +188,10 @@ int imd_send_energies(void *s, const IMDEnergies *energies) {
 
 int imd_send_fcoords(void *s, int32 n, const float *coords) {
   int rc;
-  int32 size = HEADERSIZE+12*n;
+  int32 size = HEADERSIZE+12L*n;
   char *buf = (char *) malloc(sizeof(char) * size); 
   fill_header((IMDheader *)buf, IMD_FCOORDS, n);
-  memcpy(buf+HEADERSIZE, coords, 12*n);
+  memcpy(buf+HEADERSIZE, coords, 12L*n);
   rc = (imd_writen(s, buf, size) != size);
   free(buf);
   return rc;
@@ -211,7 +232,8 @@ int imd_recv_handshake(void *s) {
     if (!imd_go(s)) return 0;
     return -1;
   }
-  swap4((char *)&buf, 4);
+
+  swap4_aligned(&buf, 1);
   if (buf == IMDVERSION) {
     if (!imd_go(s)) return 1;
   }
@@ -221,8 +243,8 @@ int imd_recv_handshake(void *s) {
 }
 
 int imd_recv_mdcomm(void *s, int32 n, int32 *indices, float *forces) {
-  if (imd_readn(s, (char *)indices, 4*n) != 4*n) return 1;
-  if (imd_readn(s, (char *)forces, 12*n) != 12*n) return 1;
+  if (imd_readn(s, (char *)indices, 4L*n) != 4L*n) return 1;
+  if (imd_readn(s, (char *)forces, 12L*n) != 12L*n) return 1;
   return 0;
 }
 
@@ -232,6 +254,6 @@ int imd_recv_energies(void *s, IMDEnergies *energies) {
 }
 
 int imd_recv_fcoords(void *s, int32 n, float *coords) {
-  return (imd_readn(s, (char *)coords, 12*n) != 12*n);
+  return (imd_readn(s, (char *)coords, 12L*n) != 12L*n);
 }
 
diff --git a/src/imd.h b/src/imd.h
index 6d722aa..3414cdf 100644
--- a/src/imd.h
+++ b/src/imd.h
@@ -1,6 +1,6 @@
 /***************************************************************************
  *cr
- *cr            (C) Copyright 1995-2003 The Board of Trustees of the
+ *cr            (C) Copyright 1995-2016 The Board of Trustees of the
  *cr                        University of Illinois
  *cr                         All Rights Reserved
  *cr
@@ -11,7 +11,11 @@
  *
  *      $RCSfile: imd.h,v $
  *      $Author: johns $       $Locker:  $             $State: Exp $
- *      $Revision: 1.1 $       $Date: 2003/09/12 18:30:46 $
+ *      $Revision: 1.22 $       $Date: 2016/11/28 03:05:07 $
+ *
+ * LICENSE:
+ *   UIUC Open Source License
+ *   http://www.ks.uiuc.edu/Research/vmd/plugins/pluginlicense.html
  *
  ***************************************************************************/
 
@@ -26,6 +30,7 @@ typedef int     int32;
 typedef short   int32;
 #endif
 
+
 typedef enum IMDType_t {
   IMD_DISCONNECT,   /**< close IMD connection, leaving sim running */
   IMD_ENERGIES,     /**< energy data block                         */
@@ -37,7 +42,8 @@ typedef enum IMDType_t {
   IMD_PAUSE,        /**< pause the running simulation              */
   IMD_TRATE,        /**< set IMD update transmission rate          */
   IMD_IOERROR       /**< indicate an I/O error                     */
-} IMDType;
+} IMDType;          /**< IMD command message type enumerations */
+
 
 typedef struct {
   int32 tstep;      /**< integer timestep index                    */
@@ -50,7 +56,8 @@ typedef struct {
   float Eangle;     /**< Angle energy, Kcal/mol                    */
   float Edihe;      /**< Dihedral energy, Kcal/mol                 */
   float Eimpr;      /**< Improper energy, Kcal/mol                 */
-} IMDEnergies;      /**<                                           */
+} IMDEnergies;      /**< IMD simulation energy report structure    */
+
 
 /* Send control messages - these consist of a header with no subsequent data */
 extern int imd_disconnect(void *);   /**< leave sim running but close IMD  */
@@ -60,21 +67,14 @@ extern int imd_handshake(void *);    /**< check endianness, version compat */
 extern int imd_trate(void *, int32); /**< set IMD update transmission rate */
 
 /* Send data update messages */
-/**
- *  Send MDComm compatible forces
- *  Forces are in Kcal/mol/angstrom
- */
+
+/** Send MDComm compatible forces, units are Kcal/mol/angstrom */
 extern int imd_send_mdcomm(void *, int32, const int32 *, const float *);
 
-/**
- *  Send energies
- */
+/** Send energies */
 extern int imd_send_energies(void *, const IMDEnergies *);
 
-/**
- *  Send atom forces and coordinates
- *  Forces are in Kcal/mol/angstrom
- */
+/** Send atom forces and coordinates, units are Kcal/mol/angstrom */
 extern int imd_send_fcoords(void *, int32, const float *);
 
 /** 
@@ -84,26 +84,16 @@ extern int imd_send_fcoords(void *, int32, const float *);
  */
 extern int imd_recv_handshake(void *);
 
-/**
- *  Receive header and data 
- */
+/** Receive header and data */
 extern IMDType imd_recv_header(void *, int32 *);
 
-/**
- *  Receive MDComm-style forces
- *  Forces are in Kcal/mol/angstrom
- */
+/** Receive MDComm-style forces, units are Kcal/mol/angstrom */
 extern int imd_recv_mdcomm(void *, int32, int32 *, float *);
 
-/**
- *  Receive energies
- */
+/** Receive energies */
 extern int imd_recv_energies(void *, IMDEnergies *);
 
-/**
- *  Receive atom coordinates and forces
- *  Forces are in Kcal/mol/angstrom
- */
+/** Receive atom coordinates and forces, units are Kcal/mol/angstrom */
 extern int imd_recv_fcoords(void *, int32, float *);
 
 #endif
-- 
GitLab