NAMD
Functions
CudaNonbondedTables.C File Reference
#include "charm++.h"
#include "NamdTypes.h"
#include "ComputeNonbondedUtil.h"
#include "LJTable.h"
#include "CudaUtils.h"
#include "CudaNonbondedTables.h"

Go to the source code of this file.

Functions

template<typename T >
void bindTextureObject (int size, T *h_table, T *&d_table, cudaTextureObject_t &tex, bool update=false)
 
template<typename T >
void buildForceAndEnergyTable (const int tableSize, const double *r2list, const BigReal *src_table, const bool flip, const BigReal prefac, const int dst_stride, T *dst_force, T *dst_energy)
 
template<typename T >
void bindTextureObject (int tableSize, int tableWidth, T *h_table, cudaArray_t &array, cudaTextureObject_t &tableTex)
 

Function Documentation

template<typename T >
void bindTextureObject ( int  size,
T *  h_table,
T *&  d_table,
cudaTextureObject_t &  tex,
bool  update = false 
)

Definition at line 50 of file CudaNonbondedTables.C.

References cudaCheck.

50  {
51  // Copy to device
52  if ( ! update) {
53  allocate_device<T>(&d_table, size);
54  }
55  else {
56  cudaCheck(cudaDestroyTextureObject(tex));
57  }
58  copy_HtoD_sync<T>(h_table, d_table, size);
59 
60  // Create texture object
61  cudaResourceDesc resDesc;
62  memset(&resDesc, 0, sizeof(resDesc));
63  resDesc.resType = cudaResourceTypeLinear;
64  resDesc.res.linear.devPtr = d_table;
65  resDesc.res.linear.desc.f = cudaChannelFormatKindFloat;
66  resDesc.res.linear.desc.x = sizeof(float)*8; // bits per channel
67  if (sizeof(T) >= sizeof(float)*2) resDesc.res.linear.desc.y = sizeof(float)*8; // bits per channel
68  if (sizeof(T) >= sizeof(float)*3) resDesc.res.linear.desc.z = sizeof(float)*8; // bits per channel
69  if (sizeof(T) >= sizeof(float)*4) resDesc.res.linear.desc.w = sizeof(float)*8; // bits per channel
70  resDesc.res.linear.sizeInBytes = size*sizeof(T);
71 
72  cudaTextureDesc texDesc;
73  memset(&texDesc, 0, sizeof(texDesc));
74  texDesc.readMode = cudaReadModeElementType;
75 
76  cudaCheck(cudaCreateTextureObject(&tex, &resDesc, &texDesc, NULL));
77 }
#define cudaCheck(stmt)
Definition: CudaUtils.h:79
template<typename T >
void bindTextureObject ( int  tableSize,
int  tableWidth,
T *  h_table,
cudaArray_t &  array,
cudaTextureObject_t &  tableTex 
)

Definition at line 175 of file CudaNonbondedTables.C.

References cudaCheck.

176  {
177 
178  cudaChannelFormatDesc desc;
179  memset(&desc, 0, sizeof(desc));
180  desc.x = sizeof(T)*8;
181  if (tableWidth >= 2) desc.y = sizeof(T)*8;
182  if (tableWidth >= 3) desc.z = sizeof(T)*8;
183  if (tableWidth >= 4) desc.w = sizeof(T)*8;
184  desc.f = cudaChannelFormatKindFloat;
185  cudaCheck(cudaMallocArray(&array, &desc, tableSize, 1));
186  cudaCheck(cudaMemcpyToArray(array, 0, 0, h_table, tableSize*sizeof(T)*tableWidth, cudaMemcpyHostToDevice));
187 
188  cudaResourceDesc resDesc;
189  memset(&resDesc, 0, sizeof(resDesc));
190  resDesc.resType = cudaResourceTypeArray;
191  resDesc.res.array.array = array;
192 
193  cudaTextureDesc texDesc;
194  memset(&texDesc, 0, sizeof(texDesc));
195  texDesc.addressMode[0] = cudaAddressModeClamp;
196  texDesc.filterMode = cudaFilterModeLinear;
197  texDesc.normalizedCoords = 1;
198 
199  cudaCheck(cudaCreateTextureObject(&tableTex, &resDesc, &texDesc, NULL));
200 }
#define cudaCheck(stmt)
Definition: CudaUtils.h:79
template<typename T >
void buildForceAndEnergyTable ( const int  tableSize,
const double *  r2list,
const BigReal src_table,
const bool  flip,
const BigReal  prefac,
const int  dst_stride,
T *  dst_force,
T *  dst_energy 
)

Definition at line 128 of file CudaNonbondedTables.C.

References ComputeNonbondedUtil::cutoff, ComputeNonbondedUtil::r2_delta, ComputeNonbondedUtil::r2_delta_exp, and ComputeNonbondedUtil::r2_table.

129  {
130 
131  const BigReal r2_delta = ComputeNonbondedUtil:: r2_delta;
132  const int r2_delta_exp = ComputeNonbondedUtil:: r2_delta_exp;
133  const int r2_delta_expc = 64 * (r2_delta_exp - 1023);
134 
135  union { double f; int32 i[2]; } byte_order_test;
136  byte_order_test.f = 1.0; // should occupy high-order bits only
137  int32 *r2iilist = (int32*)r2list + ( byte_order_test.i[0] ? 0 : 1 );
138 
139  for ( int i=1; i<tableSize; ++i ) {
140  double r = ((double) tableSize) / ( (double) i + 0.5 );
141  int table_i = (r2iilist[2*i] >> 14) + r2_delta_expc; // table_i >= 0
142 
143  if ( r > ComputeNonbondedUtil::cutoff ) {
144  dst_force[i*dst_stride] = 0.;
145  dst_energy[i*dst_stride] = 0.;
146  continue;
147  }
148 
149  BigReal diffa = r2list[i] - ComputeNonbondedUtil::r2_table[table_i];
150 
151  BigReal table_a, table_b, table_c, table_d;
152  if (flip) {
153  table_a = src_table[4*table_i+3];
154  table_b = src_table[4*table_i+2];
155  table_c = src_table[4*table_i+1];
156  table_d = src_table[4*table_i];
157  } else {
158  table_a = src_table[4*table_i];
159  table_b = src_table[4*table_i+1];
160  table_c = src_table[4*table_i+2];
161  table_d = src_table[4*table_i+3];
162  }
163 
164  BigReal grad = ( 3. * table_d * diffa + 2. * table_c ) * diffa + table_b;
165  dst_force[i*dst_stride] = prefac * 2. * grad;
166  BigReal ener = table_a + diffa * ( ( table_d * diffa + table_c ) * diffa + table_b);
167  dst_energy[i*dst_stride] = prefac * ener;
168  }
169 
170  dst_force[0] = 0.;
171  dst_energy[0] = dst_energy[1*dst_stride];
172 }
short int32
Definition: dumpdcd.c:24
double BigReal
Definition: common.h:112