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  tableSize,
int  tableWidth,
T *  h_table,
cudaArray_t &  array,
cudaTextureObject_t &  tableTex 
) [inline]

Definition at line 175 of file CudaNonbondedTables.C.

References cudaCheck.

00176                                                      {
00177 
00178   cudaChannelFormatDesc desc;
00179   memset(&desc, 0, sizeof(desc));
00180   desc.x = sizeof(T)*8;
00181   if (tableWidth >= 2) desc.y = sizeof(T)*8;
00182   if (tableWidth >= 3) desc.z = sizeof(T)*8;
00183   if (tableWidth >= 4) desc.w = sizeof(T)*8;
00184   desc.f = cudaChannelFormatKindFloat;
00185   cudaCheck(cudaMallocArray(&array, &desc, tableSize, 1));
00186   cudaCheck(cudaMemcpyToArray(array, 0, 0, h_table, tableSize*sizeof(T)*tableWidth, cudaMemcpyHostToDevice));
00187 
00188   cudaResourceDesc resDesc;
00189   memset(&resDesc, 0, sizeof(resDesc));
00190   resDesc.resType = cudaResourceTypeArray;
00191   resDesc.res.array.array = array;
00192 
00193   cudaTextureDesc texDesc;
00194   memset(&texDesc, 0, sizeof(texDesc));
00195   texDesc.addressMode[0] = cudaAddressModeClamp;
00196   texDesc.filterMode = cudaFilterModeLinear;
00197   texDesc.normalizedCoords = 1;
00198 
00199   cudaCheck(cudaCreateTextureObject(&tableTex, &resDesc, &texDesc, NULL));
00200 }

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

Definition at line 50 of file CudaNonbondedTables.C.

References cudaCheck.

00050                                                                                                        {
00051   // Copy to device
00052   if ( ! update) {
00053     allocate_device<T>(&d_table, size);
00054   }
00055   else {
00056     cudaCheck(cudaDestroyTextureObject(tex));
00057   }
00058   copy_HtoD_sync<T>(h_table, d_table, size);
00059 
00060   // Create texture object
00061   cudaResourceDesc resDesc;
00062   memset(&resDesc, 0, sizeof(resDesc));
00063   resDesc.resType = cudaResourceTypeLinear;
00064   resDesc.res.linear.devPtr = d_table;
00065   resDesc.res.linear.desc.f = cudaChannelFormatKindFloat;
00066   resDesc.res.linear.desc.x = sizeof(float)*8; // bits per channel
00067   if (sizeof(T) >= sizeof(float)*2) resDesc.res.linear.desc.y = sizeof(float)*8; // bits per channel
00068   if (sizeof(T) >= sizeof(float)*3) resDesc.res.linear.desc.z = sizeof(float)*8; // bits per channel
00069   if (sizeof(T) >= sizeof(float)*4) resDesc.res.linear.desc.w = sizeof(float)*8; // bits per channel
00070   resDesc.res.linear.sizeInBytes = size*sizeof(T);
00071 
00072   cudaTextureDesc texDesc;
00073   memset(&texDesc, 0, sizeof(texDesc));
00074   texDesc.readMode = cudaReadModeElementType;
00075 
00076   cudaCheck(cudaCreateTextureObject(&tex, &resDesc, &texDesc, NULL));
00077 }

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 
) [inline]

Definition at line 128 of file CudaNonbondedTables.C.

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

00129                                                                            {
00130 
00131   const BigReal r2_delta = ComputeNonbondedUtil:: r2_delta;
00132   const int r2_delta_exp = ComputeNonbondedUtil:: r2_delta_exp;
00133   const int r2_delta_expc = 64 * (r2_delta_exp - 1023);
00134 
00135   union { double f; int32 i[2]; } byte_order_test;
00136   byte_order_test.f = 1.0;  // should occupy high-order bits only
00137   int32 *r2iilist = (int32*)r2list + ( byte_order_test.i[0] ? 0 : 1 );
00138 
00139   for ( int i=1; i<tableSize; ++i ) {
00140     double r = ((double) tableSize) / ( (double) i + 0.5 );
00141     int table_i = (r2iilist[2*i] >> 14) + r2_delta_expc;  // table_i >= 0
00142 
00143     if ( r > ComputeNonbondedUtil::cutoff ) {
00144       dst_force[i*dst_stride] = 0.;
00145       dst_energy[i*dst_stride] = 0.;
00146       continue;
00147     }
00148 
00149     BigReal diffa = r2list[i] - ComputeNonbondedUtil::r2_table[table_i];
00150 
00151     BigReal table_a, table_b, table_c, table_d;
00152     if (flip) {
00153       table_a = src_table[4*table_i+3];
00154       table_b = src_table[4*table_i+2];
00155       table_c = src_table[4*table_i+1];
00156       table_d = src_table[4*table_i];
00157     } else {
00158       table_a = src_table[4*table_i];
00159       table_b = src_table[4*table_i+1];
00160       table_c = src_table[4*table_i+2];
00161       table_d = src_table[4*table_i+3];
00162     }
00163 
00164     BigReal grad = ( 3. * table_d * diffa + 2. * table_c ) * diffa + table_b;
00165     dst_force[i*dst_stride] = prefac * 2. * grad;
00166     BigReal ener = table_a + diffa * ( ( table_d * diffa + table_c ) * diffa + table_b);
00167     dst_energy[i*dst_stride] = prefac * ener;
00168   }
00169 
00170   dst_force[0] = 0.;
00171   dst_energy[0] = dst_energy[1*dst_stride];
00172 }


Generated on 17 Sep 2019 for NAMD by  doxygen 1.6.1