const unsigned int TileSize = 32; const float dielectricOffset = 0.009f; /** * Compute the Born sum. */ __kernel void computeBornSum(int numAtoms, int paddedNumAtoms, __global float* global_bornSum, __local float* local_bornSum, __global float4* posq, __local float4* local_posq, __global float2* global_params, __local float2* local_params, __global unsigned int* tiles, #ifdef USE_CUTOFF float cutoffSquared, float4 periodicBoxSize, __global unsigned int* interactionFlags, __global unsigned int* interactionCount, __local float* tempBuffer) { #else unsigned int numTiles) { #endif #ifdef USE_CUTOFF unsigned int numTiles = interactionCount[0]; #endif unsigned int totalWarps = get_global_size(0)/TileSize; unsigned int warp = get_global_id(0)/TileSize; unsigned int pos = warp*numTiles/totalWarps; unsigned int end = (warp+1)*numTiles/totalWarps; float energy = 0.0f; unsigned int lasty = 0xFFFFFFFF; while (pos < end) { // Extract the coordinates of this tile unsigned int x = tiles[pos]; unsigned int y = ((x >> 2) & 0x7fff)*TileSize; x = (x>>17)*TileSize; unsigned int tgx = get_local_id(0) & (TileSize-1); unsigned int tbx = get_local_id(0) - tgx; unsigned int tj = tgx; unsigned int i = x + tgx; float bornSum = 0.0f; float4 posq1 = posq[i]; float2 params1 = global_params[i]; if (x == y) { // This tile is on the diagonal. local_posq[get_local_id(0)] = posq1; local_params[get_local_id(0)] = params1; unsigned int xi = x/TileSize; unsigned int tile = xi+xi*paddedNumAtoms/TileSize-xi*(xi+1)/2; for (unsigned int j = 0; j < TileSize; j++) { float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f); #ifdef USE_PERIODIC delta.x -= floor(delta.x/periodicBoxSize.x+0.5f)*periodicBoxSize.x; delta.y -= floor(delta.y/periodicBoxSize.y+0.5f)*periodicBoxSize.y; delta.z -= floor(delta.z/periodicBoxSize.z+0.5f)*periodicBoxSize.z; #endif float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z; #ifdef USE_CUTOFF if (i < numAtoms && x+j < numAtoms && r2 < cutoffSquared) { #else if (i < numAtoms && x+j < numAtoms) { #endif float r = sqrt(r2); float invR = 1.0f/r; float2 params2 = local_params[tbx+j]; float rScaledRadiusJ = r+params2.y; if ((j != tgx) && (params1.x < rScaledRadiusJ)) { float l_ij = 1.0f/max(params1.x, fabs(r-params2.y)); float u_ij = 1.0f/rScaledRadiusJ; float l_ij2 = l_ij*l_ij; float u_ij2 = u_ij*u_ij; float ratio = log(u_ij / l_ij); bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) + (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2); if (params1.x < params2.x-r) bornSum += 2.0f*(1.0f/params1.x-l_ij); } } } // Write results #ifdef USE_OUTPUT_BUFFER_PER_BLOCK unsigned int offset = x + tgx + (x/TileSize)*paddedNumAtoms; #else unsigned int offset = x + tgx + warp*paddedNumAtoms; #endif global_bornSum[offset] += bornSum; } else { // This is an off-diagonal tile. if (lasty != y) { unsigned int j = y + tgx; local_posq[get_local_id(0)] = posq[j]; local_params[get_local_id(0)] = global_params[j]; } local_bornSum[get_local_id(0)] = 0.0f; #ifdef USE_CUTOFF unsigned int flags = interactionFlags[pos]; if (flags != 0xFFFFFFFF) { if (flags == 0) { // No interactions in this tile. } else { // Compute only a subset of the interactions in this tile. for (unsigned int j = 0; j < TileSize; j++) { if ((flags&(1<