hip_filename
stringlengths
5
84
hip_content
stringlengths
79
9.69M
cuda_filename
stringlengths
4
83
cuda_content
stringlengths
19
9.69M
aa931d7817b72a3ed35406d362ac247e3a07b380.hip
// !!! This is a file automatically generated by hipify!!! #define TORCH_ASSERT_NO_OPERATORS #include <ATen/Dispatch.h> #include <ATen/native/DispatchStub.h> #include <ATen/native/hip/Loops.cuh> #include <ATen/native/TensorIterator.h> #include <ATen/native/BinaryOps.h> #include <ATen/OpMathType.h> #include <c10/util/MathConstants.h> // NOTE: CUDA on Windows requires that the enclosing function // of a __device__ lambda not have internal linkage. namespace at::native { void logaddexp_kernel_cuda(TensorIteratorBase& iter) { AT_DISPATCH_FLOATING_TYPES_AND2( ScalarType::BFloat16, ScalarType::Half, iter.dtype(), "logaddexp_cuda", [&]() { using opmath_t = at::opmath_type<scalar_t>; gpu_kernel(iter, [] GPU_LAMBDA (scalar_t a_, scalar_t b_) -> scalar_t { const auto a = static_cast<opmath_t>(a_); const auto b = static_cast<opmath_t>(b_); if (::isinf(a) && a == b) { return a; } else { const auto m = ::max(a, b); return m + ::log1p(::exp(-::abs(a - b))); } }); }); } void logaddexp2_kernel_cuda(TensorIteratorBase& iter) { AT_DISPATCH_FLOATING_TYPES_AND( ScalarType::BFloat16, iter.dtype(), "logaddexp2_cuda", [&]() { using opmath_t = at::opmath_type<scalar_t>; const auto inv_log_2 = static_cast<opmath_t>(1.0 / c10::ln_2<double>); gpu_kernel(iter, [inv_log_2] GPU_LAMBDA (scalar_t a_, scalar_t b_) -> scalar_t { const auto a = static_cast<opmath_t>(a_); const auto b = static_cast<opmath_t>(b_); if (::isinf(a) && a == b) { return a; } else { const auto m = ::max(a, b); return m + ::log1p(::exp2(-::abs(a - b))) * inv_log_2; } }); }); } REGISTER_DISPATCH(logaddexp_stub, &logaddexp_kernel_cuda); REGISTER_DISPATCH(logaddexp2_stub, &logaddexp2_kernel_cuda); } // namespace at::native
aa931d7817b72a3ed35406d362ac247e3a07b380.cu
#define TORCH_ASSERT_NO_OPERATORS #include <ATen/Dispatch.h> #include <ATen/native/DispatchStub.h> #include <ATen/native/cuda/Loops.cuh> #include <ATen/native/TensorIterator.h> #include <ATen/native/BinaryOps.h> #include <ATen/OpMathType.h> #include <c10/util/MathConstants.h> // NOTE: CUDA on Windows requires that the enclosing function // of a __device__ lambda not have internal linkage. namespace at::native { void logaddexp_kernel_cuda(TensorIteratorBase& iter) { AT_DISPATCH_FLOATING_TYPES_AND2( ScalarType::BFloat16, ScalarType::Half, iter.dtype(), "logaddexp_cuda", [&]() { using opmath_t = at::opmath_type<scalar_t>; gpu_kernel(iter, [] GPU_LAMBDA (scalar_t a_, scalar_t b_) -> scalar_t { const auto a = static_cast<opmath_t>(a_); const auto b = static_cast<opmath_t>(b_); if (::isinf(a) && a == b) { return a; } else { const auto m = ::max(a, b); return m + ::log1p(::exp(-::abs(a - b))); } }); }); } void logaddexp2_kernel_cuda(TensorIteratorBase& iter) { AT_DISPATCH_FLOATING_TYPES_AND( ScalarType::BFloat16, iter.dtype(), "logaddexp2_cuda", [&]() { using opmath_t = at::opmath_type<scalar_t>; const auto inv_log_2 = static_cast<opmath_t>(1.0 / c10::ln_2<double>); gpu_kernel(iter, [inv_log_2] GPU_LAMBDA (scalar_t a_, scalar_t b_) -> scalar_t { const auto a = static_cast<opmath_t>(a_); const auto b = static_cast<opmath_t>(b_); if (::isinf(a) && a == b) { return a; } else { const auto m = ::max(a, b); return m + ::log1p(::exp2(-::abs(a - b))) * inv_log_2; } }); }); } REGISTER_DISPATCH(logaddexp_stub, &logaddexp_kernel_cuda); REGISTER_DISPATCH(logaddexp2_stub, &logaddexp2_kernel_cuda); } // namespace at::native
412fdd9234eabf1af82a753031a63d39b4ee1ea0.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" //#include "internal_shared.hpp" #include <thrust/sort.h> #include <opencv2/gpu/gpumat.hpp> #include <opencv2/gpu/device/common.hpp> #include "opencv2/gpu/device/limits.hpp" #include "opencv2/gpu/device/saturate_cast.hpp" #include "opencv2/gpu/device/utility.hpp" #include "opencv2/gpu/device/functional.hpp" #include "opencv2/gpu/device/filters.hpp" #include <float.h> namespace btl { namespace device { namespace surf { using namespace cv::gpu; using namespace cv::gpu::device; //////////////////////////////////////////////////////////////////////// // Global parameters // The maximum number of features (before subpixel interpolation) that memory is reserved for. __constant__ int c_max_candidates; // The maximum number of features that memory is reserved for. __constant__ int c_max_features; // The image size. __constant__ int c_img_rows; __constant__ int c_img_cols; // The number of layers. __constant__ int c_nOctaveLayers; // The hessian threshold. __constant__ float c_hessianThreshold; // The current octave. __constant__ int c_octave; // The current layer size. __constant__ int c_layer_rows; __constant__ int c_layer_cols; void loadGlobalConstants(int maxCandidates, int maxFeatures, int img_rows, int img_cols, int nOctaveLayers, float hessianThreshold) { cudaSafeCall( hipMemcpyToSymbol(c_max_candidates, &maxCandidates, sizeof(maxCandidates)) ); cudaSafeCall( hipMemcpyToSymbol(c_max_features, &maxFeatures, sizeof(maxFeatures)) ); cudaSafeCall( hipMemcpyToSymbol(c_img_rows, &img_rows, sizeof(img_rows)) ); cudaSafeCall( hipMemcpyToSymbol(c_img_cols, &img_cols, sizeof(img_cols)) ); cudaSafeCall( hipMemcpyToSymbol(c_nOctaveLayers, &nOctaveLayers, sizeof(nOctaveLayers)) ); cudaSafeCall( hipMemcpyToSymbol(c_hessianThreshold, &hessianThreshold, sizeof(hessianThreshold)) ); } void loadOctaveConstants(int octave, int layer_rows, int layer_cols) { cudaSafeCall( hipMemcpyToSymbol(c_octave, &octave, sizeof(octave)) ); cudaSafeCall( hipMemcpyToSymbol(c_layer_rows, &layer_rows, sizeof(layer_rows)) ); cudaSafeCall( hipMemcpyToSymbol(c_layer_cols, &layer_cols, sizeof(layer_cols)) ); } //////////////////////////////////////////////////////////////////////// // Integral image texture texture<unsigned char, 2, hipReadModeElementType> imgTex(0, hipFilterModePoint, hipAddressModeClamp); texture<unsigned int, 2, hipReadModeElementType> sumTex(0, hipFilterModePoint, hipAddressModeClamp); texture<unsigned int, 2, hipReadModeElementType> maskSumTex(0, hipFilterModePoint, hipAddressModeClamp); void bindImgTex(PtrStepSzb img) { bindTexture(&imgTex, img); } size_t bindSumTex(PtrStepSz<uint> sum) { size_t offset; hipChannelFormatDesc desc_sum = hipCreateChannelDesc<uint>(); cudaSafeCall( hipBindTexture2D(&offset, sumTex, sum.data, desc_sum, sum.cols, sum.rows, sum.step)); return offset / sizeof(uint); } size_t bindMaskSumTex(PtrStepSz<uint> maskSum) { size_t offset; hipChannelFormatDesc desc_sum = hipCreateChannelDesc<uint>(); cudaSafeCall( hipBindTexture2D(&offset, maskSumTex, maskSum.data, desc_sum, maskSum.cols, maskSum.rows, maskSum.step)); return offset / sizeof(uint); } template <int N> __device__ float icvCalcHaarPatternSum(const float src[][5], int oldSize, int newSize, int y, int x) { typedef double real_t; float ratio = (float)newSize / oldSize; real_t d = 0; #pragma unroll for (int k = 0; k < N; ++k) { int dx1 = __float2int_rn(ratio * src[k][0]); int dy1 = __float2int_rn(ratio * src[k][1]); int dx2 = __float2int_rn(ratio * src[k][2]); int dy2 = __float2int_rn(ratio * src[k][3]); real_t t = 0; t += tex2D(sumTex, x + dx1, y + dy1); t -= tex2D(sumTex, x + dx1, y + dy2); t -= tex2D(sumTex, x + dx2, y + dy1); t += tex2D(sumTex, x + dx2, y + dy2); d += t * src[k][4] / ((dx2 - dx1) * (dy2 - dy1)); } return (float)d; } //////////////////////////////////////////////////////////////////////// // Hessian __constant__ float c_DX [3][5] = { {0, 2, 3, 7, 1}, {3, 2, 6, 7, -2}, {6, 2, 9, 7, 1} }; __constant__ float c_DY [3][5] = { {2, 0, 7, 3, 1}, {2, 3, 7, 6, -2}, {2, 6, 7, 9, 1} }; __constant__ float c_DXY[4][5] = { {1, 1, 4, 4, 1}, {5, 1, 8, 4, -1}, {1, 5, 4, 8, -1}, {5, 5, 8, 8, 1} }; __host__ __device__ __forceinline__ int calcSize(int octave, int layer) { /* Wavelet size at first layer of first octave. */ const int HAAR_SIZE0 = 9; /* Wavelet size increment between layers. This should be an even number, such that the wavelet sizes in an octave are either all even or all odd. This ensures that when looking for the neighbours of a sample, the layers above and below are aligned correctly. */ const int HAAR_SIZE_INC = 6; int HAAR_OCTAVE_INC = 0; if(octave > 0 ) HAAR_OCTAVE_INC = (6 << (octave-1)); return HAAR_SIZE0 + HAAR_OCTAVE_INC + (HAAR_SIZE_INC << octave )* layer ; } __global__ void icvCalcLayerDetAndTrace(PtrStepf det, PtrStepf trace, uint sumOffset) { // Determine the indices const int gridDim_y = gridDim.y / (c_nOctaveLayers + 2); const int blockIdx_y = blockIdx.y % gridDim_y; const int blockIdx_z = blockIdx.y / gridDim_y; const int j = threadIdx.x + blockIdx.x * blockDim.x; const int i = threadIdx.y + blockIdx_y * blockDim.y; const int layer = blockIdx_z; const int size = calcSize(c_octave, layer); const int samples_i = 1 + ((c_img_rows - size) >> c_octave); const int samples_j = 1 + ((c_img_cols - size) >> c_octave); // Ignore pixels where some of the kernel is outside the image const int margin = (size >> 1) >> c_octave; if (size <= c_img_rows && size <= c_img_cols && i < samples_i && j < samples_j) { const float dx = icvCalcHaarPatternSum<3>(c_DX , 9, size, (i << c_octave), sumOffset + (j << c_octave)); const float dy = icvCalcHaarPatternSum<3>(c_DY , 9, size, (i << c_octave), sumOffset + (j << c_octave)); const float dxy = icvCalcHaarPatternSum<4>(c_DXY, 9, size, (i << c_octave), sumOffset + (j << c_octave)); det.ptr(layer * c_layer_rows + i + margin)[j + margin] = dx * dy - 0.81f * dxy * dxy; trace.ptr(layer * c_layer_rows + i + margin)[j + margin] = dx + dy; } } void icvCalcLayerDetAndTrace_gpu(const PtrStepf& det, const PtrStepf& trace, int img_rows, int img_cols, int octave, int nOctaveLayers, const size_t sumOffset) { const int min_size = calcSize(octave, 0); const int max_samples_i = 1 + ((img_rows - min_size) >> octave); const int max_samples_j = 1 + ((img_cols - min_size) >> octave); dim3 threads(16, 16); dim3 grid; grid.x = divUp(max_samples_j, threads.x); grid.y = divUp(max_samples_i, threads.y) * (nOctaveLayers + 2); hipLaunchKernelGGL(( icvCalcLayerDetAndTrace), dim3(grid), dim3(threads), 0, 0, det, trace, (uint)sumOffset); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // NONMAX __constant__ float c_DM[5] = {0, 0, 9, 9, 1}; struct WithMask { static __device__ bool check(int sum_i, int sum_j, int size, const uint offset) { float ratio = (float)size / 9.0f; float d = 0; int dx1 = __float2int_rn(ratio * c_DM[0]); int dy1 = __float2int_rn(ratio * c_DM[1]); int dx2 = __float2int_rn(ratio * c_DM[2]); int dy2 = __float2int_rn(ratio * c_DM[3]); float t = 0; t += tex2D(maskSumTex, offset + sum_j + dx1, sum_i + dy1); t -= tex2D(maskSumTex, offset + sum_j + dx1, sum_i + dy2); t -= tex2D(maskSumTex, offset + sum_j + dx2, sum_i + dy1); t += tex2D(maskSumTex, offset + sum_j + dx2, sum_i + dy2); d += t * c_DM[4] / ((dx2 - dx1) * (dy2 - dy1)); return (d >= 0.5f); } }; template <typename Mask> __global__ void icvFindMaximaInLayer(const PtrStepf det, const PtrStepf trace, int4* maxPosBuffer, unsigned int* maxCounter, const uint maskOffset) { extern __shared__ float N9[]; // The hidx variables are the indices to the hessian buffer. const int gridDim_y = gridDim.y / c_nOctaveLayers; const int blockIdx_y = blockIdx.y % gridDim_y; const int blockIdx_z = blockIdx.y / gridDim_y; const int layer = blockIdx_z + 1; const int size = calcSize(c_octave, layer); // Ignore pixels without a 3x3x3 neighbourhood in the layer above const int margin = ((calcSize(c_octave, layer + 1) >> 1) >> c_octave) + 1; const int j = threadIdx.x + blockIdx.x * (blockDim.x - 2) + margin - 1; const int i = threadIdx.y + blockIdx_y * (blockDim.y - 2) + margin - 1; // Is this thread within the hessian buffer? const int zoff = blockDim.x * blockDim.y; const int localLin = threadIdx.x + threadIdx.y * blockDim.x + zoff; N9[localLin - zoff] = det.ptr(c_layer_rows * (layer - 1) + ::min(::max(i, 0), c_img_rows - 1))[::min(::max(j, 0), c_img_cols - 1)]; N9[localLin ] = det.ptr(c_layer_rows * (layer ) + ::min(::max(i, 0), c_img_rows - 1))[::min(::max(j, 0), c_img_cols - 1)]; N9[localLin + zoff] = det.ptr(c_layer_rows * (layer + 1) + ::min(::max(i, 0), c_img_rows - 1))[::min(::max(j, 0), c_img_cols - 1)]; __syncthreads(); if (i < c_layer_rows - margin && j < c_layer_cols - margin && threadIdx.x > 0 && threadIdx.x < blockDim.x - 1 && threadIdx.y > 0 && threadIdx.y < blockDim.y - 1) { float val0 = N9[localLin]; if (val0 > c_hessianThreshold) { // Coordinates for the start of the wavelet in the sum image. There // is some integer division involved, so don't try to simplify this // (cancel out sampleStep) without checking the result is the same const int sum_i = (i - ((size >> 1) >> c_octave)) << c_octave; const int sum_j = (j - ((size >> 1) >> c_octave)) << c_octave; if (Mask::check(sum_i, sum_j, size, maskOffset)) { // Check to see if we have a max (in its 26 neighbours) const bool condmax = val0 > N9[localLin - 1 - blockDim.x - zoff] && val0 > N9[localLin - blockDim.x - zoff] && val0 > N9[localLin + 1 - blockDim.x - zoff] && val0 > N9[localLin - 1 - zoff] && val0 > N9[localLin - zoff] && val0 > N9[localLin + 1 - zoff] && val0 > N9[localLin - 1 + blockDim.x - zoff] && val0 > N9[localLin + blockDim.x - zoff] && val0 > N9[localLin + 1 + blockDim.x - zoff] && val0 > N9[localLin - 1 - blockDim.x] && val0 > N9[localLin - blockDim.x] && val0 > N9[localLin + 1 - blockDim.x] && val0 > N9[localLin - 1 ] && val0 > N9[localLin + 1 ] && val0 > N9[localLin - 1 + blockDim.x] && val0 > N9[localLin + blockDim.x] && val0 > N9[localLin + 1 + blockDim.x] && val0 > N9[localLin - 1 - blockDim.x + zoff] && val0 > N9[localLin - blockDim.x + zoff] && val0 > N9[localLin + 1 - blockDim.x + zoff] && val0 > N9[localLin - 1 + zoff] && val0 > N9[localLin + zoff] && val0 > N9[localLin + 1 + zoff] && val0 > N9[localLin - 1 + blockDim.x + zoff] && val0 > N9[localLin + blockDim.x + zoff] && val0 > N9[localLin + 1 + blockDim.x + zoff] ; if(condmax) { unsigned int ind = atomicInc(maxCounter,(unsigned int) -1); if (ind < c_max_candidates) { const int laplacian = (int) copysignf(1.0f, trace.ptr(layer * c_layer_rows + i)[j]); maxPosBuffer[ind] = make_int4(j, i, layer, laplacian); } } } } } } void icvFindMaximaInLayer_gpu(const PtrStepf& det, const PtrStepf& trace, int4* maxPosBuffer, unsigned int* maxCounter, int img_rows, int img_cols, int octave, bool use_mask, int nOctaveLayers, const size_t maskOffset) { const int layer_rows = img_rows >> octave; const int layer_cols = img_cols >> octave; const int min_margin = ((calcSize(octave, 2) >> 1) >> octave) + 1; dim3 threads(16, 16); dim3 grid; grid.x = divUp(layer_cols - 2 * min_margin, threads.x - 2); grid.y = divUp(layer_rows - 2 * min_margin, threads.y - 2) * nOctaveLayers; const size_t smem_size = threads.x * threads.y * 3 * sizeof(float); hipLaunchKernelGGL(( icvFindMaximaInLayer<WithOutMask>), dim3(grid), dim3(threads), smem_size, 0, det, trace, maxPosBuffer, maxCounter, 0); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // INTERPOLATION __global__ void icvInterpolateKeypoint(const PtrStepf det, const int4* maxPosBuffer, float* featureX, float* featureY, int* featureLaplacian, int* featureOctave, float* featureSize, float* featureHessian, unsigned int* featureCounter) { const int4 maxPos = maxPosBuffer[blockIdx.x]; const int j = maxPos.x - 1 + threadIdx.x; const int i = maxPos.y - 1 + threadIdx.y; const int layer = maxPos.z - 1 + threadIdx.z; __shared__ float N9[3][3][3]; N9[threadIdx.z][threadIdx.y][threadIdx.x] = det.ptr(c_layer_rows * layer + i)[j]; __syncthreads(); if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0) { __shared__ float dD[3]; //dx dD[0] = -0.5f * (N9[1][1][2] - N9[1][1][0]); //dy dD[1] = -0.5f * (N9[1][2][1] - N9[1][0][1]); //ds dD[2] = -0.5f * (N9[2][1][1] - N9[0][1][1]); __shared__ float H[3][3]; //dxx H[0][0] = N9[1][1][0] - 2.0f * N9[1][1][1] + N9[1][1][2]; //dxy H[0][1]= 0.25f * (N9[1][2][2] - N9[1][2][0] - N9[1][0][2] + N9[1][0][0]); //dxs H[0][2]= 0.25f * (N9[2][1][2] - N9[2][1][0] - N9[0][1][2] + N9[0][1][0]); //dyx = dxy H[1][0] = H[0][1]; //dyy H[1][1] = N9[1][0][1] - 2.0f * N9[1][1][1] + N9[1][2][1]; //dys H[1][2]= 0.25f * (N9[2][2][1] - N9[2][0][1] - N9[0][2][1] + N9[0][0][1]); //dsx = dxs H[2][0] = H[0][2]; //dsy = dys H[2][1] = H[1][2]; //dss H[2][2] = N9[0][1][1] - 2.0f * N9[1][1][1] + N9[2][1][1]; __shared__ float x[3]; if (solve3x3(H, dD, x)) { if (::fabs(x[0]) <= 1.f && ::fabs(x[1]) <= 1.f && ::fabs(x[2]) <= 1.f) { // if the step is within the interpolation region, perform it const int size = calcSize(c_octave, maxPos.z); const int sum_i = (maxPos.y - ((size >> 1) >> c_octave)) << c_octave; const int sum_j = (maxPos.x - ((size >> 1) >> c_octave)) << c_octave; const float center_i = sum_i + (float)(size - 1) / 2; const float center_j = sum_j + (float)(size - 1) / 2; const float px = center_j + x[0] * (1 << c_octave); const float py = center_i + x[1] * (1 << c_octave); const int ds = size - calcSize(c_octave, maxPos.z - 1); const float psize = roundf(size + x[2] * ds); /* The sampling intervals and wavelet sized for selecting an orientation and building the keypoint descriptor are defined relative to 's' */ const float s = psize * 1.2f / 9.0f; /* To find the dominant orientation, the gradients in x and y are sampled in a circle of radius 6s using wavelets of size 4s. We ensure the gradient wavelet size is even to ensure the wavelet pattern is balanced and symmetric around its center */ const int grad_wav_size = 2 * __float2int_rn(2.0f * s); // check when grad_wav_size is too big if ((c_img_rows + 1) >= grad_wav_size && (c_img_cols + 1) >= grad_wav_size) { // Get a new feature index. unsigned int ind = atomicInc(featureCounter, (unsigned int)-1); if (ind < c_max_features) { featureX[ind] = px; featureY[ind] = py; featureLaplacian[ind] = maxPos.w; featureOctave[ind] = c_octave; featureSize[ind] = psize; featureHessian[ind] = N9[1][1][1]; } } // grad_wav_size check } // If the subpixel interpolation worked } } // If this is thread 0. } void icvInterpolateKeypoint_gpu(const PtrStepf& det, const int4* maxPosBuffer, unsigned int maxCounter, float* featureX, float* featureY, int* featureLaplacian, int* featureOctave, float* featureSize, float* featureHessian, unsigned int* featureCounter) { dim3 threads; threads.x = 3; threads.y = 3; threads.z = 3; dim3 grid; grid.x = maxCounter; hipLaunchKernelGGL(( icvInterpolateKeypoint), dim3(grid), dim3(threads), 0, 0, det, maxPosBuffer, featureX, featureY, featureLaplacian, featureOctave, featureSize, featureHessian, featureCounter); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // Orientation #define ORI_SEARCH_INC 5 #define ORI_WIN 60 #define ORI_SAMPLES 113 __constant__ float c_aptX[ORI_SAMPLES] = {-6, -5, -5, -5, -5, -5, -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6}; __constant__ float c_aptY[ORI_SAMPLES] = {0, -3, -2, -1, 0, 1, 2, 3, -4, -3, -2, -1, 0, 1, 2, 3, 4, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -4, -3, -2, -1, 0, 1, 2, 3, 4, -3, -2, -1, 0, 1, 2, 3, 0}; __constant__ float c_aptW[ORI_SAMPLES] = {0.001455130288377404f, 0.001707611023448408f, 0.002547456417232752f, 0.003238451667129993f, 0.0035081731621176f, 0.003238451667129993f, 0.002547456417232752f, 0.001707611023448408f, 0.002003900473937392f, 0.0035081731621176f, 0.005233579315245152f, 0.00665318313986063f, 0.00720730796456337f, 0.00665318313986063f, 0.005233579315245152f, 0.0035081731621176f, 0.002003900473937392f, 0.001707611023448408f, 0.0035081731621176f, 0.006141661666333675f, 0.009162282571196556f, 0.01164754293859005f, 0.01261763460934162f, 0.01164754293859005f, 0.009162282571196556f, 0.006141661666333675f, 0.0035081731621176f, 0.001707611023448408f, 0.002547456417232752f, 0.005233579315245152f, 0.009162282571196556f, 0.01366852037608624f, 0.01737609319388866f, 0.0188232995569706f, 0.01737609319388866f, 0.01366852037608624f, 0.009162282571196556f, 0.005233579315245152f, 0.002547456417232752f, 0.003238451667129993f, 0.00665318313986063f, 0.01164754293859005f, 0.01737609319388866f, 0.02208934165537357f, 0.02392910048365593f, 0.02208934165537357f, 0.01737609319388866f, 0.01164754293859005f, 0.00665318313986063f, 0.003238451667129993f, 0.001455130288377404f, 0.0035081731621176f, 0.00720730796456337f, 0.01261763460934162f, 0.0188232995569706f, 0.02392910048365593f, 0.02592208795249462f, 0.02392910048365593f, 0.0188232995569706f, 0.01261763460934162f, 0.00720730796456337f, 0.0035081731621176f, 0.001455130288377404f, 0.003238451667129993f, 0.00665318313986063f, 0.01164754293859005f, 0.01737609319388866f, 0.02208934165537357f, 0.02392910048365593f, 0.02208934165537357f, 0.01737609319388866f, 0.01164754293859005f, 0.00665318313986063f, 0.003238451667129993f, 0.002547456417232752f, 0.005233579315245152f, 0.009162282571196556f, 0.01366852037608624f, 0.01737609319388866f, 0.0188232995569706f, 0.01737609319388866f, 0.01366852037608624f, 0.009162282571196556f, 0.005233579315245152f, 0.002547456417232752f, 0.001707611023448408f, 0.0035081731621176f, 0.006141661666333675f, 0.009162282571196556f, 0.01164754293859005f, 0.01261763460934162f, 0.01164754293859005f, 0.009162282571196556f, 0.006141661666333675f, 0.0035081731621176f, 0.001707611023448408f, 0.002003900473937392f, 0.0035081731621176f, 0.005233579315245152f, 0.00665318313986063f, 0.00720730796456337f, 0.00665318313986063f, 0.005233579315245152f, 0.0035081731621176f, 0.002003900473937392f, 0.001707611023448408f, 0.002547456417232752f, 0.003238451667129993f, 0.0035081731621176f, 0.003238451667129993f, 0.002547456417232752f, 0.001707611023448408f, 0.001455130288377404f}; __constant__ float c_NX[2][5] = {{0, 0, 2, 4, -1}, {2, 0, 4, 4, 1}}; __constant__ float c_NY[2][5] = {{0, 0, 4, 2, 1}, {0, 2, 4, 4, -1}}; __global__ void icvCalcOrientation(const float* featureX, const float* featureY, const float* featureSize, float* featureDir) { __shared__ float s_X[128]; __shared__ float s_Y[128]; __shared__ float s_angle[128]; __shared__ float s_sumx[32 * 4]; __shared__ float s_sumy[32 * 4]; /* The sampling intervals and wavelet sized for selecting an orientation and building the keypoint descriptor are defined relative to 's' */ const float s = featureSize[blockIdx.x] * 1.2f / 9.0f; /* To find the dominant orientation, the gradients in x and y are sampled in a circle of radius 6s using wavelets of size 4s. We ensure the gradient wavelet size is even to ensure the wavelet pattern is balanced and symmetric around its center */ const int grad_wav_size = 2 * __float2int_rn(2.0f * s); // check when grad_wav_size is too big if ((c_img_rows + 1) < grad_wav_size || (c_img_cols + 1) < grad_wav_size) return; // Calc X, Y, angle and store it to shared memory const int tid = threadIdx.y * blockDim.x + threadIdx.x; float X = 0.0f, Y = 0.0f, angle = 0.0f; if (tid < ORI_SAMPLES) { const float margin = (float)(grad_wav_size - 1) / 2.0f; const int x = __float2int_rn(featureX[blockIdx.x] + c_aptX[tid] * s - margin); const int y = __float2int_rn(featureY[blockIdx.x] + c_aptY[tid] * s - margin); if (y >= 0 && y < (c_img_rows + 1) - grad_wav_size && x >= 0 && x < (c_img_cols + 1) - grad_wav_size) { X = c_aptW[tid] * icvCalcHaarPatternSum<2>(c_NX, 4, grad_wav_size, y, x); Y = c_aptW[tid] * icvCalcHaarPatternSum<2>(c_NY, 4, grad_wav_size, y, x); angle = atan2f(Y, X); if (angle < 0) angle += 2.0f * CV_PI_F; angle *= 180.0f / CV_PI_F; } } s_X[tid] = X; s_Y[tid] = Y; s_angle[tid] = angle; __syncthreads(); float bestx = 0, besty = 0, best_mod = 0; #pragma unroll for (int i = 0; i < 18; ++i) { const int dir = (i * 4 + threadIdx.y) * ORI_SEARCH_INC; float sumx = 0.0f, sumy = 0.0f; int d = ::abs(__float2int_rn(s_angle[threadIdx.x]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx = s_X[threadIdx.x]; sumy = s_Y[threadIdx.x]; } d = ::abs(__float2int_rn(s_angle[threadIdx.x + 32]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx += s_X[threadIdx.x + 32]; sumy += s_Y[threadIdx.x + 32]; } d = ::abs(__float2int_rn(s_angle[threadIdx.x + 64]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx += s_X[threadIdx.x + 64]; sumy += s_Y[threadIdx.x + 64]; } d = ::abs(__float2int_rn(s_angle[threadIdx.x + 96]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx += s_X[threadIdx.x + 96]; sumy += s_Y[threadIdx.x + 96]; } cv::gpu::device::reduce<32>(s_sumx + threadIdx.y * 32, sumx, threadIdx.x, plus<volatile float>()); cv::gpu::device::reduce<32>(s_sumy + threadIdx.y * 32, sumy, threadIdx.x, plus<volatile float>()); const float temp_mod = sumx * sumx + sumy * sumy; if (temp_mod > best_mod) { best_mod = temp_mod; bestx = sumx; besty = sumy; } __syncthreads(); } if (threadIdx.x == 0) { s_X[threadIdx.y] = bestx; s_Y[threadIdx.y] = besty; s_angle[threadIdx.y] = best_mod; } __syncthreads(); if (threadIdx.x == 0 && threadIdx.y == 0) { int bestIdx = 0; if (s_angle[1] > s_angle[bestIdx]) bestIdx = 1; if (s_angle[2] > s_angle[bestIdx]) bestIdx = 2; if (s_angle[3] > s_angle[bestIdx]) bestIdx = 3; float kp_dir = atan2f(s_Y[bestIdx], s_X[bestIdx]); if (kp_dir < 0) kp_dir += 2.0f * CV_PI_F; kp_dir *= 180.0f / CV_PI_F; kp_dir = 360.0f - kp_dir; if (::fabs(kp_dir - 360.f) < FLT_EPSILON) kp_dir = 0.f; featureDir[blockIdx.x] = kp_dir; } } #undef ORI_SEARCH_INC #undef ORI_WIN #undef ORI_SAMPLES void icvCalcOrientation_gpu(const float* featureX, const float* featureY, const float* featureSize, float* featureDir, int nFeatures) { dim3 threads; threads.x = 32; threads.y = 4; dim3 grid; grid.x = nFeatures; hipLaunchKernelGGL(( icvCalcOrientation), dim3(grid), dim3(threads), 0, 0, featureX, featureY, featureSize, featureDir); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // Descriptors #define PATCH_SZ 20 __constant__ float c_DW[PATCH_SZ * PATCH_SZ] = { 3.695352233989979e-006f, 8.444558261544444e-006f, 1.760426494001877e-005f, 3.34794785885606e-005f, 5.808438800158911e-005f, 9.193058212986216e-005f, 0.0001327334757661447f, 0.0001748319627949968f, 0.0002100782439811155f, 0.0002302826324012131f, 0.0002302826324012131f, 0.0002100782439811155f, 0.0001748319627949968f, 0.0001327334757661447f, 9.193058212986216e-005f, 5.808438800158911e-005f, 3.34794785885606e-005f, 1.760426494001877e-005f, 8.444558261544444e-006f, 3.695352233989979e-006f, 8.444558261544444e-006f, 1.929736572492402e-005f, 4.022897701361217e-005f, 7.650675252079964e-005f, 0.0001327334903180599f, 0.0002100782585330308f, 0.0003033203829545528f, 0.0003995231236331165f, 0.0004800673632416874f, 0.0005262381164357066f, 0.0005262381164357066f, 0.0004800673632416874f, 0.0003995231236331165f, 0.0003033203829545528f, 0.0002100782585330308f, 0.0001327334903180599f, 7.650675252079964e-005f, 4.022897701361217e-005f, 1.929736572492402e-005f, 8.444558261544444e-006f, 1.760426494001877e-005f, 4.022897701361217e-005f, 8.386484114453197e-005f, 0.0001594926579855382f, 0.0002767078403849155f, 0.0004379475140012801f, 0.0006323281559161842f, 0.0008328808471560478f, 0.001000790391117334f, 0.001097041997127235f, 0.001097041997127235f, 0.001000790391117334f, 0.0008328808471560478f, 0.0006323281559161842f, 0.0004379475140012801f, 0.0002767078403849155f, 0.0001594926579855382f, 8.386484114453197e-005f, 4.022897701361217e-005f, 1.760426494001877e-005f, 3.34794785885606e-005f, 7.650675252079964e-005f, 0.0001594926579855382f, 0.0003033203247468919f, 0.0005262380582280457f, 0.0008328807889483869f, 0.001202550483867526f, 0.001583957928232849f, 0.001903285388834775f, 0.002086334861814976f, 0.002086334861814976f, 0.001903285388834775f, 0.001583957928232849f, 0.001202550483867526f, 0.0008328807889483869f, 0.0005262380582280457f, 0.0003033203247468919f, 0.0001594926579855382f, 7.650675252079964e-005f, 3.34794785885606e-005f, 5.808438800158911e-005f, 0.0001327334903180599f, 0.0002767078403849155f, 0.0005262380582280457f, 0.0009129836107604206f, 0.001444985857233405f, 0.002086335094645619f, 0.002748048631474376f, 0.00330205773934722f, 0.003619635012000799f, 0.003619635012000799f, 0.00330205773934722f, 0.002748048631474376f, 0.002086335094645619f, 0.001444985857233405f, 0.0009129836107604206f, 0.0005262380582280457f, 0.0002767078403849155f, 0.0001327334903180599f, 5.808438800158911e-005f, 9.193058212986216e-005f, 0.0002100782585330308f, 0.0004379475140012801f, 0.0008328807889483869f, 0.001444985857233405f, 0.002286989474669099f, 0.00330205773934722f, 0.004349356517195702f, 0.00522619066759944f, 0.005728822201490402f, 0.005728822201490402f, 0.00522619066759944f, 0.004349356517195702f, 0.00330205773934722f, 0.002286989474669099f, 0.001444985857233405f, 0.0008328807889483869f, 0.0004379475140012801f, 0.0002100782585330308f, 9.193058212986216e-005f, 0.0001327334757661447f, 0.0003033203829545528f, 0.0006323281559161842f, 0.001202550483867526f, 0.002086335094645619f, 0.00330205773934722f, 0.004767658654600382f, 0.006279794964939356f, 0.007545807864516974f, 0.008271530270576477f, 0.008271530270576477f, 0.007545807864516974f, 0.006279794964939356f, 0.004767658654600382f, 0.00330205773934722f, 0.002086335094645619f, 0.001202550483867526f, 0.0006323281559161842f, 0.0003033203829545528f, 0.0001327334757661447f, 0.0001748319627949968f, 0.0003995231236331165f, 0.0008328808471560478f, 0.001583957928232849f, 0.002748048631474376f, 0.004349356517195702f, 0.006279794964939356f, 0.008271529339253902f, 0.009939077310264111f, 0.01089497376233339f, 0.01089497376233339f, 0.009939077310264111f, 0.008271529339253902f, 0.006279794964939356f, 0.004349356517195702f, 0.002748048631474376f, 0.001583957928232849f, 0.0008328808471560478f, 0.0003995231236331165f, 0.0001748319627949968f, 0.0002100782439811155f, 0.0004800673632416874f, 0.001000790391117334f, 0.001903285388834775f, 0.00330205773934722f, 0.00522619066759944f, 0.007545807864516974f, 0.009939077310264111f, 0.01194280479103327f, 0.01309141051024199f, 0.01309141051024199f, 0.01194280479103327f, 0.009939077310264111f, 0.007545807864516974f, 0.00522619066759944f, 0.00330205773934722f, 0.001903285388834775f, 0.001000790391117334f, 0.0004800673632416874f, 0.0002100782439811155f, 0.0002302826324012131f, 0.0005262381164357066f, 0.001097041997127235f, 0.002086334861814976f, 0.003619635012000799f, 0.005728822201490402f, 0.008271530270576477f, 0.01089497376233339f, 0.01309141051024199f, 0.01435048412531614f, 0.01435048412531614f, 0.01309141051024199f, 0.01089497376233339f, 0.008271530270576477f, 0.005728822201490402f, 0.003619635012000799f, 0.002086334861814976f, 0.001097041997127235f, 0.0005262381164357066f, 0.0002302826324012131f, 0.0002302826324012131f, 0.0005262381164357066f, 0.001097041997127235f, 0.002086334861814976f, 0.003619635012000799f, 0.005728822201490402f, 0.008271530270576477f, 0.01089497376233339f, 0.01309141051024199f, 0.01435048412531614f, 0.01435048412531614f, 0.01309141051024199f, 0.01089497376233339f, 0.008271530270576477f, 0.005728822201490402f, 0.003619635012000799f, 0.002086334861814976f, 0.001097041997127235f, 0.0005262381164357066f, 0.0002302826324012131f, 0.0002100782439811155f, 0.0004800673632416874f, 0.001000790391117334f, 0.001903285388834775f, 0.00330205773934722f, 0.00522619066759944f, 0.007545807864516974f, 0.009939077310264111f, 0.01194280479103327f, 0.01309141051024199f, 0.01309141051024199f, 0.01194280479103327f, 0.009939077310264111f, 0.007545807864516974f, 0.00522619066759944f, 0.00330205773934722f, 0.001903285388834775f, 0.001000790391117334f, 0.0004800673632416874f, 0.0002100782439811155f, 0.0001748319627949968f, 0.0003995231236331165f, 0.0008328808471560478f, 0.001583957928232849f, 0.002748048631474376f, 0.004349356517195702f, 0.006279794964939356f, 0.008271529339253902f, 0.009939077310264111f, 0.01089497376233339f, 0.01089497376233339f, 0.009939077310264111f, 0.008271529339253902f, 0.006279794964939356f, 0.004349356517195702f, 0.002748048631474376f, 0.001583957928232849f, 0.0008328808471560478f, 0.0003995231236331165f, 0.0001748319627949968f, 0.0001327334757661447f, 0.0003033203829545528f, 0.0006323281559161842f, 0.001202550483867526f, 0.002086335094645619f, 0.00330205773934722f, 0.004767658654600382f, 0.006279794964939356f, 0.007545807864516974f, 0.008271530270576477f, 0.008271530270576477f, 0.007545807864516974f, 0.006279794964939356f, 0.004767658654600382f, 0.00330205773934722f, 0.002086335094645619f, 0.001202550483867526f, 0.0006323281559161842f, 0.0003033203829545528f, 0.0001327334757661447f, 9.193058212986216e-005f, 0.0002100782585330308f, 0.0004379475140012801f, 0.0008328807889483869f, 0.001444985857233405f, 0.002286989474669099f, 0.00330205773934722f, 0.004349356517195702f, 0.00522619066759944f, 0.005728822201490402f, 0.005728822201490402f, 0.00522619066759944f, 0.004349356517195702f, 0.00330205773934722f, 0.002286989474669099f, 0.001444985857233405f, 0.0008328807889483869f, 0.0004379475140012801f, 0.0002100782585330308f, 9.193058212986216e-005f, 5.808438800158911e-005f, 0.0001327334903180599f, 0.0002767078403849155f, 0.0005262380582280457f, 0.0009129836107604206f, 0.001444985857233405f, 0.002086335094645619f, 0.002748048631474376f, 0.00330205773934722f, 0.003619635012000799f, 0.003619635012000799f, 0.00330205773934722f, 0.002748048631474376f, 0.002086335094645619f, 0.001444985857233405f, 0.0009129836107604206f, 0.0005262380582280457f, 0.0002767078403849155f, 0.0001327334903180599f, 5.808438800158911e-005f, 3.34794785885606e-005f, 7.650675252079964e-005f, 0.0001594926579855382f, 0.0003033203247468919f, 0.0005262380582280457f, 0.0008328807889483869f, 0.001202550483867526f, 0.001583957928232849f, 0.001903285388834775f, 0.002086334861814976f, 0.002086334861814976f, 0.001903285388834775f, 0.001583957928232849f, 0.001202550483867526f, 0.0008328807889483869f, 0.0005262380582280457f, 0.0003033203247468919f, 0.0001594926579855382f, 7.650675252079964e-005f, 3.34794785885606e-005f, 1.760426494001877e-005f, 4.022897701361217e-005f, 8.386484114453197e-005f, 0.0001594926579855382f, 0.0002767078403849155f, 0.0004379475140012801f, 0.0006323281559161842f, 0.0008328808471560478f, 0.001000790391117334f, 0.001097041997127235f, 0.001097041997127235f, 0.001000790391117334f, 0.0008328808471560478f, 0.0006323281559161842f, 0.0004379475140012801f, 0.0002767078403849155f, 0.0001594926579855382f, 8.386484114453197e-005f, 4.022897701361217e-005f, 1.760426494001877e-005f, 8.444558261544444e-006f, 1.929736572492402e-005f, 4.022897701361217e-005f, 7.650675252079964e-005f, 0.0001327334903180599f, 0.0002100782585330308f, 0.0003033203829545528f, 0.0003995231236331165f, 0.0004800673632416874f, 0.0005262381164357066f, 0.0005262381164357066f, 0.0004800673632416874f, 0.0003995231236331165f, 0.0003033203829545528f, 0.0002100782585330308f, 0.0001327334903180599f, 7.650675252079964e-005f, 4.022897701361217e-005f, 1.929736572492402e-005f, 8.444558261544444e-006f, 3.695352233989979e-006f, 8.444558261544444e-006f, 1.760426494001877e-005f, 3.34794785885606e-005f, 5.808438800158911e-005f, 9.193058212986216e-005f, 0.0001327334757661447f, 0.0001748319627949968f, 0.0002100782439811155f, 0.0002302826324012131f, 0.0002302826324012131f, 0.0002100782439811155f, 0.0001748319627949968f, 0.0001327334757661447f, 9.193058212986216e-005f, 5.808438800158911e-005f, 3.34794785885606e-005f, 1.760426494001877e-005f, 8.444558261544444e-006f, 3.695352233989979e-006f }; struct WinReader { typedef uchar elem_type; __device__ __forceinline__ WinReader(float centerX_, float centerY_, float win_offset_, float cos_dir_, float sin_dir_) : centerX(centerX_), centerY(centerY_), win_offset(win_offset_), cos_dir(cos_dir_), sin_dir(sin_dir_) { } __device__ __forceinline__ uchar operator ()(int i, int j) const { float pixel_x = centerX + (win_offset + j) * cos_dir + (win_offset + i) * sin_dir; float pixel_y = centerY - (win_offset + j) * sin_dir + (win_offset + i) * cos_dir; return tex2D(imgTex, pixel_x, pixel_y); } float centerX; float centerY; float win_offset; float cos_dir; float sin_dir; }; __device__ void calc_dx_dy(float s_dx_bin[25], float s_dy_bin[25], const float* featureX, const float* featureY, const float* featureSize, const float* featureDir) { __shared__ float s_PATCH[6][6]; const float centerX = featureX[blockIdx.x]; const float centerY = featureY[blockIdx.x]; const float size = featureSize[blockIdx.x]; float descriptor_dir = 360.0f - featureDir[blockIdx.x]; if (std::abs(descriptor_dir - 360.f) < FLT_EPSILON) descriptor_dir = 0.f; descriptor_dir *= (float)(CV_PI_F / 180.0f); /* The sampling intervals and wavelet sized for selecting an orientation and building the keypoint descriptor are defined relative to 's' */ const float s = size * 1.2f / 9.0f; /* Extract a window of pixels around the keypoint of size 20s */ const int win_size = (int)((PATCH_SZ + 1) * s); float sin_dir; float cos_dir; sincosf(descriptor_dir, &sin_dir, &cos_dir); /* Nearest neighbour version (faster) */ const float win_offset = -(float)(win_size - 1) / 2; // Compute sampling points // since grids are 2D, need to compute xBlock and yBlock indices const int xBlock = (blockIdx.y & 3); // blockIdx.y % 4 const int yBlock = (blockIdx.y >> 2); // floor(blockIdx.y/4) const int xIndex = xBlock * 5 + threadIdx.x; const int yIndex = yBlock * 5 + threadIdx.y; const float icoo = ((float)yIndex / (PATCH_SZ + 1)) * win_size; const float jcoo = ((float)xIndex / (PATCH_SZ + 1)) * win_size; LinearFilter<WinReader> filter(WinReader(centerX, centerY, win_offset, cos_dir, sin_dir)); s_PATCH[threadIdx.y][threadIdx.x] = filter(icoo, jcoo); __syncthreads(); if (threadIdx.x < 5 && threadIdx.y < 5) { const int tid = threadIdx.y * 5 + threadIdx.x; const float dw = c_DW[yIndex * PATCH_SZ + xIndex]; const float vx = (s_PATCH[threadIdx.y ][threadIdx.x + 1] - s_PATCH[threadIdx.y][threadIdx.x] + s_PATCH[threadIdx.y + 1][threadIdx.x + 1] - s_PATCH[threadIdx.y + 1][threadIdx.x ]) * dw; const float vy = (s_PATCH[threadIdx.y + 1][threadIdx.x ] - s_PATCH[threadIdx.y][threadIdx.x] + s_PATCH[threadIdx.y + 1][threadIdx.x + 1] - s_PATCH[threadIdx.y ][threadIdx.x + 1]) * dw; s_dx_bin[tid] = vx; s_dy_bin[tid] = vy; } } __device__ void reduce_sum25(volatile float* sdata1, volatile float* sdata2, volatile float* sdata3, volatile float* sdata4, int tid) { // first step is to reduce from 25 to 16 if (tid < 9) // use 9 threads { sdata1[tid] += sdata1[tid + 16]; sdata2[tid] += sdata2[tid + 16]; sdata3[tid] += sdata3[tid + 16]; sdata4[tid] += sdata4[tid + 16]; } // sum (reduce) from 16 to 1 (unrolled - aligned to a half-warp) if (tid < 8) { sdata1[tid] += sdata1[tid + 8]; sdata1[tid] += sdata1[tid + 4]; sdata1[tid] += sdata1[tid + 2]; sdata1[tid] += sdata1[tid + 1]; sdata2[tid] += sdata2[tid + 8]; sdata2[tid] += sdata2[tid + 4]; sdata2[tid] += sdata2[tid + 2]; sdata2[tid] += sdata2[tid + 1]; sdata3[tid] += sdata3[tid + 8]; sdata3[tid] += sdata3[tid + 4]; sdata3[tid] += sdata3[tid + 2]; sdata3[tid] += sdata3[tid + 1]; sdata4[tid] += sdata4[tid + 8]; sdata4[tid] += sdata4[tid + 4]; sdata4[tid] += sdata4[tid + 2]; sdata4[tid] += sdata4[tid + 1]; } } __global__ void compute_descriptors64(PtrStepf descriptors, const float* featureX, const float* featureY, const float* featureSize, const float* featureDir) { // 2 floats (dx,dy) for each thread (5x5 sample points in each sub-region) __shared__ float sdx[25]; __shared__ float sdy[25]; __shared__ float sdxabs[25]; __shared__ float sdyabs[25]; calc_dx_dy(sdx, sdy, featureX, featureY, featureSize, featureDir); __syncthreads(); const int tid = threadIdx.y * blockDim.x + threadIdx.x; if (tid < 25) { sdxabs[tid] = ::fabs(sdx[tid]); // |dx| array sdyabs[tid] = ::fabs(sdy[tid]); // |dy| array __syncthreads(); reduce_sum25(sdx, sdy, sdxabs, sdyabs, tid); __syncthreads(); float* descriptors_block = descriptors.ptr(blockIdx.x) + (blockIdx.y << 2); // write dx, dy, |dx|, |dy| if (tid == 0) { descriptors_block[0] = sdx[0]; descriptors_block[1] = sdy[0]; descriptors_block[2] = sdxabs[0]; descriptors_block[3] = sdyabs[0]; } } } __global__ void compute_descriptors128(PtrStepf descriptors, const float* featureX, const float* featureY, const float* featureSize, const float* featureDir) { // 2 floats (dx,dy) for each thread (5x5 sample points in each sub-region) __shared__ float sdx[25]; __shared__ float sdy[25]; // sum (reduce) 5x5 area response __shared__ float sd1[25]; __shared__ float sd2[25]; __shared__ float sdabs1[25]; __shared__ float sdabs2[25]; calc_dx_dy(sdx, sdy, featureX, featureY, featureSize, featureDir); __syncthreads(); const int tid = threadIdx.y * blockDim.x + threadIdx.x; if (tid < 25) { if (sdy[tid] >= 0) { sd1[tid] = sdx[tid]; sdabs1[tid] = ::fabs(sdx[tid]); sd2[tid] = 0; sdabs2[tid] = 0; } else { sd1[tid] = 0; sdabs1[tid] = 0; sd2[tid] = sdx[tid]; sdabs2[tid] = ::fabs(sdx[tid]); } __syncthreads(); reduce_sum25(sd1, sd2, sdabs1, sdabs2, tid); __syncthreads(); float* descriptors_block = descriptors.ptr(blockIdx.x) + (blockIdx.y << 3); // write dx (dy >= 0), |dx| (dy >= 0), dx (dy < 0), |dx| (dy < 0) if (tid == 0) { descriptors_block[0] = sd1[0]; descriptors_block[1] = sdabs1[0]; descriptors_block[2] = sd2[0]; descriptors_block[3] = sdabs2[0]; } __syncthreads(); if (sdx[tid] >= 0) { sd1[tid] = sdy[tid]; sdabs1[tid] = ::fabs(sdy[tid]); sd2[tid] = 0; sdabs2[tid] = 0; } else { sd1[tid] = 0; sdabs1[tid] = 0; sd2[tid] = sdy[tid]; sdabs2[tid] = ::fabs(sdy[tid]); } __syncthreads(); reduce_sum25(sd1, sd2, sdabs1, sdabs2, tid); __syncthreads(); // write dy (dx >= 0), |dy| (dx >= 0), dy (dx < 0), |dy| (dx < 0) if (tid == 0) { descriptors_block[4] = sd1[0]; descriptors_block[5] = sdabs1[0]; descriptors_block[6] = sd2[0]; descriptors_block[7] = sdabs2[0]; } } } template <int BLOCK_DIM_X> __global__ void normalize_descriptors(PtrStepf descriptors) { // no need for thread ID float* descriptor_base = descriptors.ptr(blockIdx.x); // read in the unnormalized descriptor values (squared) __shared__ float sqDesc[BLOCK_DIM_X]; const float lookup = descriptor_base[threadIdx.x]; sqDesc[threadIdx.x] = lookup * lookup; __syncthreads(); if (BLOCK_DIM_X >= 128) { if (threadIdx.x < 64) sqDesc[threadIdx.x] += sqDesc[threadIdx.x + 64]; __syncthreads(); } // reduction to get total if (threadIdx.x < 32) { volatile float* smem = sqDesc; smem[threadIdx.x] += smem[threadIdx.x + 32]; smem[threadIdx.x] += smem[threadIdx.x + 16]; smem[threadIdx.x] += smem[threadIdx.x + 8]; smem[threadIdx.x] += smem[threadIdx.x + 4]; smem[threadIdx.x] += smem[threadIdx.x + 2]; smem[threadIdx.x] += smem[threadIdx.x + 1]; } // compute length (square root) __shared__ float len; if (threadIdx.x == 0) { len = sqrtf(sqDesc[0]); } __syncthreads(); // normalize and store in output descriptor_base[threadIdx.x] = lookup / len; } void compute_descriptors_gpu(const PtrStepSzf& descriptors, const float* featureX, const float* featureY, const float* featureSize, const float* featureDir, int nFeatures) { // compute unnormalized descriptors, then normalize them - odd indexing since grid must be 2D if (descriptors.cols == 64) { hipLaunchKernelGGL(( compute_descriptors64), dim3(dim3(nFeatures, 16, 1)), dim3(dim3(6, 6, 1)), 0, 0, descriptors, featureX, featureY, featureSize, featureDir); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); hipLaunchKernelGGL(( normalize_descriptors<64>), dim3(dim3(nFeatures, 1, 1)), dim3(dim3(64, 1, 1)), 0, 0, descriptors); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); } else { hipLaunchKernelGGL(( compute_descriptors128), dim3(dim3(nFeatures, 16, 1)), dim3(dim3(6, 6, 1)), 0, 0, descriptors, featureX, featureY, featureSize, featureDir); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); hipLaunchKernelGGL(( normalize_descriptors<128>), dim3(dim3(nFeatures, 1, 1)), dim3(dim3(128, 1, 1)), 0, 0, descriptors); cudaSafeCall( hipGetLastError() ); cudaSafeCall( hipDeviceSynchronize() ); } } } // namespace surf } // namespace devcie } // namespace btl
412fdd9234eabf1af82a753031a63d39b4ee1ea0.cu
//#include "internal_shared.hpp" #include <thrust/sort.h> #include <opencv2/gpu/gpumat.hpp> #include <opencv2/gpu/device/common.hpp> #include "opencv2/gpu/device/limits.hpp" #include "opencv2/gpu/device/saturate_cast.hpp" #include "opencv2/gpu/device/utility.hpp" #include "opencv2/gpu/device/functional.hpp" #include "opencv2/gpu/device/filters.hpp" #include <float.h> namespace btl { namespace device { namespace surf { using namespace cv::gpu; using namespace cv::gpu::device; //////////////////////////////////////////////////////////////////////// // Global parameters // The maximum number of features (before subpixel interpolation) that memory is reserved for. __constant__ int c_max_candidates; // The maximum number of features that memory is reserved for. __constant__ int c_max_features; // The image size. __constant__ int c_img_rows; __constant__ int c_img_cols; // The number of layers. __constant__ int c_nOctaveLayers; // The hessian threshold. __constant__ float c_hessianThreshold; // The current octave. __constant__ int c_octave; // The current layer size. __constant__ int c_layer_rows; __constant__ int c_layer_cols; void loadGlobalConstants(int maxCandidates, int maxFeatures, int img_rows, int img_cols, int nOctaveLayers, float hessianThreshold) { cudaSafeCall( cudaMemcpyToSymbol(c_max_candidates, &maxCandidates, sizeof(maxCandidates)) ); cudaSafeCall( cudaMemcpyToSymbol(c_max_features, &maxFeatures, sizeof(maxFeatures)) ); cudaSafeCall( cudaMemcpyToSymbol(c_img_rows, &img_rows, sizeof(img_rows)) ); cudaSafeCall( cudaMemcpyToSymbol(c_img_cols, &img_cols, sizeof(img_cols)) ); cudaSafeCall( cudaMemcpyToSymbol(c_nOctaveLayers, &nOctaveLayers, sizeof(nOctaveLayers)) ); cudaSafeCall( cudaMemcpyToSymbol(c_hessianThreshold, &hessianThreshold, sizeof(hessianThreshold)) ); } void loadOctaveConstants(int octave, int layer_rows, int layer_cols) { cudaSafeCall( cudaMemcpyToSymbol(c_octave, &octave, sizeof(octave)) ); cudaSafeCall( cudaMemcpyToSymbol(c_layer_rows, &layer_rows, sizeof(layer_rows)) ); cudaSafeCall( cudaMemcpyToSymbol(c_layer_cols, &layer_cols, sizeof(layer_cols)) ); } //////////////////////////////////////////////////////////////////////// // Integral image texture texture<unsigned char, 2, cudaReadModeElementType> imgTex(0, cudaFilterModePoint, cudaAddressModeClamp); texture<unsigned int, 2, cudaReadModeElementType> sumTex(0, cudaFilterModePoint, cudaAddressModeClamp); texture<unsigned int, 2, cudaReadModeElementType> maskSumTex(0, cudaFilterModePoint, cudaAddressModeClamp); void bindImgTex(PtrStepSzb img) { bindTexture(&imgTex, img); } size_t bindSumTex(PtrStepSz<uint> sum) { size_t offset; cudaChannelFormatDesc desc_sum = cudaCreateChannelDesc<uint>(); cudaSafeCall( cudaBindTexture2D(&offset, sumTex, sum.data, desc_sum, sum.cols, sum.rows, sum.step)); return offset / sizeof(uint); } size_t bindMaskSumTex(PtrStepSz<uint> maskSum) { size_t offset; cudaChannelFormatDesc desc_sum = cudaCreateChannelDesc<uint>(); cudaSafeCall( cudaBindTexture2D(&offset, maskSumTex, maskSum.data, desc_sum, maskSum.cols, maskSum.rows, maskSum.step)); return offset / sizeof(uint); } template <int N> __device__ float icvCalcHaarPatternSum(const float src[][5], int oldSize, int newSize, int y, int x) { typedef double real_t; float ratio = (float)newSize / oldSize; real_t d = 0; #pragma unroll for (int k = 0; k < N; ++k) { int dx1 = __float2int_rn(ratio * src[k][0]); int dy1 = __float2int_rn(ratio * src[k][1]); int dx2 = __float2int_rn(ratio * src[k][2]); int dy2 = __float2int_rn(ratio * src[k][3]); real_t t = 0; t += tex2D(sumTex, x + dx1, y + dy1); t -= tex2D(sumTex, x + dx1, y + dy2); t -= tex2D(sumTex, x + dx2, y + dy1); t += tex2D(sumTex, x + dx2, y + dy2); d += t * src[k][4] / ((dx2 - dx1) * (dy2 - dy1)); } return (float)d; } //////////////////////////////////////////////////////////////////////// // Hessian __constant__ float c_DX [3][5] = { {0, 2, 3, 7, 1}, {3, 2, 6, 7, -2}, {6, 2, 9, 7, 1} }; __constant__ float c_DY [3][5] = { {2, 0, 7, 3, 1}, {2, 3, 7, 6, -2}, {2, 6, 7, 9, 1} }; __constant__ float c_DXY[4][5] = { {1, 1, 4, 4, 1}, {5, 1, 8, 4, -1}, {1, 5, 4, 8, -1}, {5, 5, 8, 8, 1} }; __host__ __device__ __forceinline__ int calcSize(int octave, int layer) { /* Wavelet size at first layer of first octave. */ const int HAAR_SIZE0 = 9; /* Wavelet size increment between layers. This should be an even number, such that the wavelet sizes in an octave are either all even or all odd. This ensures that when looking for the neighbours of a sample, the layers above and below are aligned correctly. */ const int HAAR_SIZE_INC = 6; int HAAR_OCTAVE_INC = 0; if(octave > 0 ) HAAR_OCTAVE_INC = (6 << (octave-1)); return HAAR_SIZE0 + HAAR_OCTAVE_INC + (HAAR_SIZE_INC << octave )* layer ; } __global__ void icvCalcLayerDetAndTrace(PtrStepf det, PtrStepf trace, uint sumOffset) { // Determine the indices const int gridDim_y = gridDim.y / (c_nOctaveLayers + 2); const int blockIdx_y = blockIdx.y % gridDim_y; const int blockIdx_z = blockIdx.y / gridDim_y; const int j = threadIdx.x + blockIdx.x * blockDim.x; const int i = threadIdx.y + blockIdx_y * blockDim.y; const int layer = blockIdx_z; const int size = calcSize(c_octave, layer); const int samples_i = 1 + ((c_img_rows - size) >> c_octave); const int samples_j = 1 + ((c_img_cols - size) >> c_octave); // Ignore pixels where some of the kernel is outside the image const int margin = (size >> 1) >> c_octave; if (size <= c_img_rows && size <= c_img_cols && i < samples_i && j < samples_j) { const float dx = icvCalcHaarPatternSum<3>(c_DX , 9, size, (i << c_octave), sumOffset + (j << c_octave)); const float dy = icvCalcHaarPatternSum<3>(c_DY , 9, size, (i << c_octave), sumOffset + (j << c_octave)); const float dxy = icvCalcHaarPatternSum<4>(c_DXY, 9, size, (i << c_octave), sumOffset + (j << c_octave)); det.ptr(layer * c_layer_rows + i + margin)[j + margin] = dx * dy - 0.81f * dxy * dxy; trace.ptr(layer * c_layer_rows + i + margin)[j + margin] = dx + dy; } } void icvCalcLayerDetAndTrace_gpu(const PtrStepf& det, const PtrStepf& trace, int img_rows, int img_cols, int octave, int nOctaveLayers, const size_t sumOffset) { const int min_size = calcSize(octave, 0); const int max_samples_i = 1 + ((img_rows - min_size) >> octave); const int max_samples_j = 1 + ((img_cols - min_size) >> octave); dim3 threads(16, 16); dim3 grid; grid.x = divUp(max_samples_j, threads.x); grid.y = divUp(max_samples_i, threads.y) * (nOctaveLayers + 2); icvCalcLayerDetAndTrace<<<grid, threads>>>(det, trace, (uint)sumOffset); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // NONMAX __constant__ float c_DM[5] = {0, 0, 9, 9, 1}; struct WithMask { static __device__ bool check(int sum_i, int sum_j, int size, const uint offset) { float ratio = (float)size / 9.0f; float d = 0; int dx1 = __float2int_rn(ratio * c_DM[0]); int dy1 = __float2int_rn(ratio * c_DM[1]); int dx2 = __float2int_rn(ratio * c_DM[2]); int dy2 = __float2int_rn(ratio * c_DM[3]); float t = 0; t += tex2D(maskSumTex, offset + sum_j + dx1, sum_i + dy1); t -= tex2D(maskSumTex, offset + sum_j + dx1, sum_i + dy2); t -= tex2D(maskSumTex, offset + sum_j + dx2, sum_i + dy1); t += tex2D(maskSumTex, offset + sum_j + dx2, sum_i + dy2); d += t * c_DM[4] / ((dx2 - dx1) * (dy2 - dy1)); return (d >= 0.5f); } }; template <typename Mask> __global__ void icvFindMaximaInLayer(const PtrStepf det, const PtrStepf trace, int4* maxPosBuffer, unsigned int* maxCounter, const uint maskOffset) { extern __shared__ float N9[]; // The hidx variables are the indices to the hessian buffer. const int gridDim_y = gridDim.y / c_nOctaveLayers; const int blockIdx_y = blockIdx.y % gridDim_y; const int blockIdx_z = blockIdx.y / gridDim_y; const int layer = blockIdx_z + 1; const int size = calcSize(c_octave, layer); // Ignore pixels without a 3x3x3 neighbourhood in the layer above const int margin = ((calcSize(c_octave, layer + 1) >> 1) >> c_octave) + 1; const int j = threadIdx.x + blockIdx.x * (blockDim.x - 2) + margin - 1; const int i = threadIdx.y + blockIdx_y * (blockDim.y - 2) + margin - 1; // Is this thread within the hessian buffer? const int zoff = blockDim.x * blockDim.y; const int localLin = threadIdx.x + threadIdx.y * blockDim.x + zoff; N9[localLin - zoff] = det.ptr(c_layer_rows * (layer - 1) + ::min(::max(i, 0), c_img_rows - 1))[::min(::max(j, 0), c_img_cols - 1)]; N9[localLin ] = det.ptr(c_layer_rows * (layer ) + ::min(::max(i, 0), c_img_rows - 1))[::min(::max(j, 0), c_img_cols - 1)]; N9[localLin + zoff] = det.ptr(c_layer_rows * (layer + 1) + ::min(::max(i, 0), c_img_rows - 1))[::min(::max(j, 0), c_img_cols - 1)]; __syncthreads(); if (i < c_layer_rows - margin && j < c_layer_cols - margin && threadIdx.x > 0 && threadIdx.x < blockDim.x - 1 && threadIdx.y > 0 && threadIdx.y < blockDim.y - 1) { float val0 = N9[localLin]; if (val0 > c_hessianThreshold) { // Coordinates for the start of the wavelet in the sum image. There // is some integer division involved, so don't try to simplify this // (cancel out sampleStep) without checking the result is the same const int sum_i = (i - ((size >> 1) >> c_octave)) << c_octave; const int sum_j = (j - ((size >> 1) >> c_octave)) << c_octave; if (Mask::check(sum_i, sum_j, size, maskOffset)) { // Check to see if we have a max (in its 26 neighbours) const bool condmax = val0 > N9[localLin - 1 - blockDim.x - zoff] && val0 > N9[localLin - blockDim.x - zoff] && val0 > N9[localLin + 1 - blockDim.x - zoff] && val0 > N9[localLin - 1 - zoff] && val0 > N9[localLin - zoff] && val0 > N9[localLin + 1 - zoff] && val0 > N9[localLin - 1 + blockDim.x - zoff] && val0 > N9[localLin + blockDim.x - zoff] && val0 > N9[localLin + 1 + blockDim.x - zoff] && val0 > N9[localLin - 1 - blockDim.x] && val0 > N9[localLin - blockDim.x] && val0 > N9[localLin + 1 - blockDim.x] && val0 > N9[localLin - 1 ] && val0 > N9[localLin + 1 ] && val0 > N9[localLin - 1 + blockDim.x] && val0 > N9[localLin + blockDim.x] && val0 > N9[localLin + 1 + blockDim.x] && val0 > N9[localLin - 1 - blockDim.x + zoff] && val0 > N9[localLin - blockDim.x + zoff] && val0 > N9[localLin + 1 - blockDim.x + zoff] && val0 > N9[localLin - 1 + zoff] && val0 > N9[localLin + zoff] && val0 > N9[localLin + 1 + zoff] && val0 > N9[localLin - 1 + blockDim.x + zoff] && val0 > N9[localLin + blockDim.x + zoff] && val0 > N9[localLin + 1 + blockDim.x + zoff] ; if(condmax) { unsigned int ind = atomicInc(maxCounter,(unsigned int) -1); if (ind < c_max_candidates) { const int laplacian = (int) copysignf(1.0f, trace.ptr(layer * c_layer_rows + i)[j]); maxPosBuffer[ind] = make_int4(j, i, layer, laplacian); } } } } } } void icvFindMaximaInLayer_gpu(const PtrStepf& det, const PtrStepf& trace, int4* maxPosBuffer, unsigned int* maxCounter, int img_rows, int img_cols, int octave, bool use_mask, int nOctaveLayers, const size_t maskOffset) { const int layer_rows = img_rows >> octave; const int layer_cols = img_cols >> octave; const int min_margin = ((calcSize(octave, 2) >> 1) >> octave) + 1; dim3 threads(16, 16); dim3 grid; grid.x = divUp(layer_cols - 2 * min_margin, threads.x - 2); grid.y = divUp(layer_rows - 2 * min_margin, threads.y - 2) * nOctaveLayers; const size_t smem_size = threads.x * threads.y * 3 * sizeof(float); icvFindMaximaInLayer<WithOutMask><<<grid, threads, smem_size>>>(det, trace, maxPosBuffer, maxCounter, 0); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // INTERPOLATION __global__ void icvInterpolateKeypoint(const PtrStepf det, const int4* maxPosBuffer, float* featureX, float* featureY, int* featureLaplacian, int* featureOctave, float* featureSize, float* featureHessian, unsigned int* featureCounter) { const int4 maxPos = maxPosBuffer[blockIdx.x]; const int j = maxPos.x - 1 + threadIdx.x; const int i = maxPos.y - 1 + threadIdx.y; const int layer = maxPos.z - 1 + threadIdx.z; __shared__ float N9[3][3][3]; N9[threadIdx.z][threadIdx.y][threadIdx.x] = det.ptr(c_layer_rows * layer + i)[j]; __syncthreads(); if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0) { __shared__ float dD[3]; //dx dD[0] = -0.5f * (N9[1][1][2] - N9[1][1][0]); //dy dD[1] = -0.5f * (N9[1][2][1] - N9[1][0][1]); //ds dD[2] = -0.5f * (N9[2][1][1] - N9[0][1][1]); __shared__ float H[3][3]; //dxx H[0][0] = N9[1][1][0] - 2.0f * N9[1][1][1] + N9[1][1][2]; //dxy H[0][1]= 0.25f * (N9[1][2][2] - N9[1][2][0] - N9[1][0][2] + N9[1][0][0]); //dxs H[0][2]= 0.25f * (N9[2][1][2] - N9[2][1][0] - N9[0][1][2] + N9[0][1][0]); //dyx = dxy H[1][0] = H[0][1]; //dyy H[1][1] = N9[1][0][1] - 2.0f * N9[1][1][1] + N9[1][2][1]; //dys H[1][2]= 0.25f * (N9[2][2][1] - N9[2][0][1] - N9[0][2][1] + N9[0][0][1]); //dsx = dxs H[2][0] = H[0][2]; //dsy = dys H[2][1] = H[1][2]; //dss H[2][2] = N9[0][1][1] - 2.0f * N9[1][1][1] + N9[2][1][1]; __shared__ float x[3]; if (solve3x3(H, dD, x)) { if (::fabs(x[0]) <= 1.f && ::fabs(x[1]) <= 1.f && ::fabs(x[2]) <= 1.f) { // if the step is within the interpolation region, perform it const int size = calcSize(c_octave, maxPos.z); const int sum_i = (maxPos.y - ((size >> 1) >> c_octave)) << c_octave; const int sum_j = (maxPos.x - ((size >> 1) >> c_octave)) << c_octave; const float center_i = sum_i + (float)(size - 1) / 2; const float center_j = sum_j + (float)(size - 1) / 2; const float px = center_j + x[0] * (1 << c_octave); const float py = center_i + x[1] * (1 << c_octave); const int ds = size - calcSize(c_octave, maxPos.z - 1); const float psize = roundf(size + x[2] * ds); /* The sampling intervals and wavelet sized for selecting an orientation and building the keypoint descriptor are defined relative to 's' */ const float s = psize * 1.2f / 9.0f; /* To find the dominant orientation, the gradients in x and y are sampled in a circle of radius 6s using wavelets of size 4s. We ensure the gradient wavelet size is even to ensure the wavelet pattern is balanced and symmetric around its center */ const int grad_wav_size = 2 * __float2int_rn(2.0f * s); // check when grad_wav_size is too big if ((c_img_rows + 1) >= grad_wav_size && (c_img_cols + 1) >= grad_wav_size) { // Get a new feature index. unsigned int ind = atomicInc(featureCounter, (unsigned int)-1); if (ind < c_max_features) { featureX[ind] = px; featureY[ind] = py; featureLaplacian[ind] = maxPos.w; featureOctave[ind] = c_octave; featureSize[ind] = psize; featureHessian[ind] = N9[1][1][1]; } } // grad_wav_size check } // If the subpixel interpolation worked } } // If this is thread 0. } void icvInterpolateKeypoint_gpu(const PtrStepf& det, const int4* maxPosBuffer, unsigned int maxCounter, float* featureX, float* featureY, int* featureLaplacian, int* featureOctave, float* featureSize, float* featureHessian, unsigned int* featureCounter) { dim3 threads; threads.x = 3; threads.y = 3; threads.z = 3; dim3 grid; grid.x = maxCounter; icvInterpolateKeypoint<<<grid, threads>>>(det, maxPosBuffer, featureX, featureY, featureLaplacian, featureOctave, featureSize, featureHessian, featureCounter); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // Orientation #define ORI_SEARCH_INC 5 #define ORI_WIN 60 #define ORI_SAMPLES 113 __constant__ float c_aptX[ORI_SAMPLES] = {-6, -5, -5, -5, -5, -5, -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6}; __constant__ float c_aptY[ORI_SAMPLES] = {0, -3, -2, -1, 0, 1, 2, 3, -4, -3, -2, -1, 0, 1, 2, 3, 4, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -4, -3, -2, -1, 0, 1, 2, 3, 4, -3, -2, -1, 0, 1, 2, 3, 0}; __constant__ float c_aptW[ORI_SAMPLES] = {0.001455130288377404f, 0.001707611023448408f, 0.002547456417232752f, 0.003238451667129993f, 0.0035081731621176f, 0.003238451667129993f, 0.002547456417232752f, 0.001707611023448408f, 0.002003900473937392f, 0.0035081731621176f, 0.005233579315245152f, 0.00665318313986063f, 0.00720730796456337f, 0.00665318313986063f, 0.005233579315245152f, 0.0035081731621176f, 0.002003900473937392f, 0.001707611023448408f, 0.0035081731621176f, 0.006141661666333675f, 0.009162282571196556f, 0.01164754293859005f, 0.01261763460934162f, 0.01164754293859005f, 0.009162282571196556f, 0.006141661666333675f, 0.0035081731621176f, 0.001707611023448408f, 0.002547456417232752f, 0.005233579315245152f, 0.009162282571196556f, 0.01366852037608624f, 0.01737609319388866f, 0.0188232995569706f, 0.01737609319388866f, 0.01366852037608624f, 0.009162282571196556f, 0.005233579315245152f, 0.002547456417232752f, 0.003238451667129993f, 0.00665318313986063f, 0.01164754293859005f, 0.01737609319388866f, 0.02208934165537357f, 0.02392910048365593f, 0.02208934165537357f, 0.01737609319388866f, 0.01164754293859005f, 0.00665318313986063f, 0.003238451667129993f, 0.001455130288377404f, 0.0035081731621176f, 0.00720730796456337f, 0.01261763460934162f, 0.0188232995569706f, 0.02392910048365593f, 0.02592208795249462f, 0.02392910048365593f, 0.0188232995569706f, 0.01261763460934162f, 0.00720730796456337f, 0.0035081731621176f, 0.001455130288377404f, 0.003238451667129993f, 0.00665318313986063f, 0.01164754293859005f, 0.01737609319388866f, 0.02208934165537357f, 0.02392910048365593f, 0.02208934165537357f, 0.01737609319388866f, 0.01164754293859005f, 0.00665318313986063f, 0.003238451667129993f, 0.002547456417232752f, 0.005233579315245152f, 0.009162282571196556f, 0.01366852037608624f, 0.01737609319388866f, 0.0188232995569706f, 0.01737609319388866f, 0.01366852037608624f, 0.009162282571196556f, 0.005233579315245152f, 0.002547456417232752f, 0.001707611023448408f, 0.0035081731621176f, 0.006141661666333675f, 0.009162282571196556f, 0.01164754293859005f, 0.01261763460934162f, 0.01164754293859005f, 0.009162282571196556f, 0.006141661666333675f, 0.0035081731621176f, 0.001707611023448408f, 0.002003900473937392f, 0.0035081731621176f, 0.005233579315245152f, 0.00665318313986063f, 0.00720730796456337f, 0.00665318313986063f, 0.005233579315245152f, 0.0035081731621176f, 0.002003900473937392f, 0.001707611023448408f, 0.002547456417232752f, 0.003238451667129993f, 0.0035081731621176f, 0.003238451667129993f, 0.002547456417232752f, 0.001707611023448408f, 0.001455130288377404f}; __constant__ float c_NX[2][5] = {{0, 0, 2, 4, -1}, {2, 0, 4, 4, 1}}; __constant__ float c_NY[2][5] = {{0, 0, 4, 2, 1}, {0, 2, 4, 4, -1}}; __global__ void icvCalcOrientation(const float* featureX, const float* featureY, const float* featureSize, float* featureDir) { __shared__ float s_X[128]; __shared__ float s_Y[128]; __shared__ float s_angle[128]; __shared__ float s_sumx[32 * 4]; __shared__ float s_sumy[32 * 4]; /* The sampling intervals and wavelet sized for selecting an orientation and building the keypoint descriptor are defined relative to 's' */ const float s = featureSize[blockIdx.x] * 1.2f / 9.0f; /* To find the dominant orientation, the gradients in x and y are sampled in a circle of radius 6s using wavelets of size 4s. We ensure the gradient wavelet size is even to ensure the wavelet pattern is balanced and symmetric around its center */ const int grad_wav_size = 2 * __float2int_rn(2.0f * s); // check when grad_wav_size is too big if ((c_img_rows + 1) < grad_wav_size || (c_img_cols + 1) < grad_wav_size) return; // Calc X, Y, angle and store it to shared memory const int tid = threadIdx.y * blockDim.x + threadIdx.x; float X = 0.0f, Y = 0.0f, angle = 0.0f; if (tid < ORI_SAMPLES) { const float margin = (float)(grad_wav_size - 1) / 2.0f; const int x = __float2int_rn(featureX[blockIdx.x] + c_aptX[tid] * s - margin); const int y = __float2int_rn(featureY[blockIdx.x] + c_aptY[tid] * s - margin); if (y >= 0 && y < (c_img_rows + 1) - grad_wav_size && x >= 0 && x < (c_img_cols + 1) - grad_wav_size) { X = c_aptW[tid] * icvCalcHaarPatternSum<2>(c_NX, 4, grad_wav_size, y, x); Y = c_aptW[tid] * icvCalcHaarPatternSum<2>(c_NY, 4, grad_wav_size, y, x); angle = atan2f(Y, X); if (angle < 0) angle += 2.0f * CV_PI_F; angle *= 180.0f / CV_PI_F; } } s_X[tid] = X; s_Y[tid] = Y; s_angle[tid] = angle; __syncthreads(); float bestx = 0, besty = 0, best_mod = 0; #pragma unroll for (int i = 0; i < 18; ++i) { const int dir = (i * 4 + threadIdx.y) * ORI_SEARCH_INC; float sumx = 0.0f, sumy = 0.0f; int d = ::abs(__float2int_rn(s_angle[threadIdx.x]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx = s_X[threadIdx.x]; sumy = s_Y[threadIdx.x]; } d = ::abs(__float2int_rn(s_angle[threadIdx.x + 32]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx += s_X[threadIdx.x + 32]; sumy += s_Y[threadIdx.x + 32]; } d = ::abs(__float2int_rn(s_angle[threadIdx.x + 64]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx += s_X[threadIdx.x + 64]; sumy += s_Y[threadIdx.x + 64]; } d = ::abs(__float2int_rn(s_angle[threadIdx.x + 96]) - dir); if (d < ORI_WIN / 2 || d > 360 - ORI_WIN / 2) { sumx += s_X[threadIdx.x + 96]; sumy += s_Y[threadIdx.x + 96]; } cv::gpu::device::reduce<32>(s_sumx + threadIdx.y * 32, sumx, threadIdx.x, plus<volatile float>()); cv::gpu::device::reduce<32>(s_sumy + threadIdx.y * 32, sumy, threadIdx.x, plus<volatile float>()); const float temp_mod = sumx * sumx + sumy * sumy; if (temp_mod > best_mod) { best_mod = temp_mod; bestx = sumx; besty = sumy; } __syncthreads(); } if (threadIdx.x == 0) { s_X[threadIdx.y] = bestx; s_Y[threadIdx.y] = besty; s_angle[threadIdx.y] = best_mod; } __syncthreads(); if (threadIdx.x == 0 && threadIdx.y == 0) { int bestIdx = 0; if (s_angle[1] > s_angle[bestIdx]) bestIdx = 1; if (s_angle[2] > s_angle[bestIdx]) bestIdx = 2; if (s_angle[3] > s_angle[bestIdx]) bestIdx = 3; float kp_dir = atan2f(s_Y[bestIdx], s_X[bestIdx]); if (kp_dir < 0) kp_dir += 2.0f * CV_PI_F; kp_dir *= 180.0f / CV_PI_F; kp_dir = 360.0f - kp_dir; if (::fabs(kp_dir - 360.f) < FLT_EPSILON) kp_dir = 0.f; featureDir[blockIdx.x] = kp_dir; } } #undef ORI_SEARCH_INC #undef ORI_WIN #undef ORI_SAMPLES void icvCalcOrientation_gpu(const float* featureX, const float* featureY, const float* featureSize, float* featureDir, int nFeatures) { dim3 threads; threads.x = 32; threads.y = 4; dim3 grid; grid.x = nFeatures; icvCalcOrientation<<<grid, threads>>>(featureX, featureY, featureSize, featureDir); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); } //////////////////////////////////////////////////////////////////////// // Descriptors #define PATCH_SZ 20 __constant__ float c_DW[PATCH_SZ * PATCH_SZ] = { 3.695352233989979e-006f, 8.444558261544444e-006f, 1.760426494001877e-005f, 3.34794785885606e-005f, 5.808438800158911e-005f, 9.193058212986216e-005f, 0.0001327334757661447f, 0.0001748319627949968f, 0.0002100782439811155f, 0.0002302826324012131f, 0.0002302826324012131f, 0.0002100782439811155f, 0.0001748319627949968f, 0.0001327334757661447f, 9.193058212986216e-005f, 5.808438800158911e-005f, 3.34794785885606e-005f, 1.760426494001877e-005f, 8.444558261544444e-006f, 3.695352233989979e-006f, 8.444558261544444e-006f, 1.929736572492402e-005f, 4.022897701361217e-005f, 7.650675252079964e-005f, 0.0001327334903180599f, 0.0002100782585330308f, 0.0003033203829545528f, 0.0003995231236331165f, 0.0004800673632416874f, 0.0005262381164357066f, 0.0005262381164357066f, 0.0004800673632416874f, 0.0003995231236331165f, 0.0003033203829545528f, 0.0002100782585330308f, 0.0001327334903180599f, 7.650675252079964e-005f, 4.022897701361217e-005f, 1.929736572492402e-005f, 8.444558261544444e-006f, 1.760426494001877e-005f, 4.022897701361217e-005f, 8.386484114453197e-005f, 0.0001594926579855382f, 0.0002767078403849155f, 0.0004379475140012801f, 0.0006323281559161842f, 0.0008328808471560478f, 0.001000790391117334f, 0.001097041997127235f, 0.001097041997127235f, 0.001000790391117334f, 0.0008328808471560478f, 0.0006323281559161842f, 0.0004379475140012801f, 0.0002767078403849155f, 0.0001594926579855382f, 8.386484114453197e-005f, 4.022897701361217e-005f, 1.760426494001877e-005f, 3.34794785885606e-005f, 7.650675252079964e-005f, 0.0001594926579855382f, 0.0003033203247468919f, 0.0005262380582280457f, 0.0008328807889483869f, 0.001202550483867526f, 0.001583957928232849f, 0.001903285388834775f, 0.002086334861814976f, 0.002086334861814976f, 0.001903285388834775f, 0.001583957928232849f, 0.001202550483867526f, 0.0008328807889483869f, 0.0005262380582280457f, 0.0003033203247468919f, 0.0001594926579855382f, 7.650675252079964e-005f, 3.34794785885606e-005f, 5.808438800158911e-005f, 0.0001327334903180599f, 0.0002767078403849155f, 0.0005262380582280457f, 0.0009129836107604206f, 0.001444985857233405f, 0.002086335094645619f, 0.002748048631474376f, 0.00330205773934722f, 0.003619635012000799f, 0.003619635012000799f, 0.00330205773934722f, 0.002748048631474376f, 0.002086335094645619f, 0.001444985857233405f, 0.0009129836107604206f, 0.0005262380582280457f, 0.0002767078403849155f, 0.0001327334903180599f, 5.808438800158911e-005f, 9.193058212986216e-005f, 0.0002100782585330308f, 0.0004379475140012801f, 0.0008328807889483869f, 0.001444985857233405f, 0.002286989474669099f, 0.00330205773934722f, 0.004349356517195702f, 0.00522619066759944f, 0.005728822201490402f, 0.005728822201490402f, 0.00522619066759944f, 0.004349356517195702f, 0.00330205773934722f, 0.002286989474669099f, 0.001444985857233405f, 0.0008328807889483869f, 0.0004379475140012801f, 0.0002100782585330308f, 9.193058212986216e-005f, 0.0001327334757661447f, 0.0003033203829545528f, 0.0006323281559161842f, 0.001202550483867526f, 0.002086335094645619f, 0.00330205773934722f, 0.004767658654600382f, 0.006279794964939356f, 0.007545807864516974f, 0.008271530270576477f, 0.008271530270576477f, 0.007545807864516974f, 0.006279794964939356f, 0.004767658654600382f, 0.00330205773934722f, 0.002086335094645619f, 0.001202550483867526f, 0.0006323281559161842f, 0.0003033203829545528f, 0.0001327334757661447f, 0.0001748319627949968f, 0.0003995231236331165f, 0.0008328808471560478f, 0.001583957928232849f, 0.002748048631474376f, 0.004349356517195702f, 0.006279794964939356f, 0.008271529339253902f, 0.009939077310264111f, 0.01089497376233339f, 0.01089497376233339f, 0.009939077310264111f, 0.008271529339253902f, 0.006279794964939356f, 0.004349356517195702f, 0.002748048631474376f, 0.001583957928232849f, 0.0008328808471560478f, 0.0003995231236331165f, 0.0001748319627949968f, 0.0002100782439811155f, 0.0004800673632416874f, 0.001000790391117334f, 0.001903285388834775f, 0.00330205773934722f, 0.00522619066759944f, 0.007545807864516974f, 0.009939077310264111f, 0.01194280479103327f, 0.01309141051024199f, 0.01309141051024199f, 0.01194280479103327f, 0.009939077310264111f, 0.007545807864516974f, 0.00522619066759944f, 0.00330205773934722f, 0.001903285388834775f, 0.001000790391117334f, 0.0004800673632416874f, 0.0002100782439811155f, 0.0002302826324012131f, 0.0005262381164357066f, 0.001097041997127235f, 0.002086334861814976f, 0.003619635012000799f, 0.005728822201490402f, 0.008271530270576477f, 0.01089497376233339f, 0.01309141051024199f, 0.01435048412531614f, 0.01435048412531614f, 0.01309141051024199f, 0.01089497376233339f, 0.008271530270576477f, 0.005728822201490402f, 0.003619635012000799f, 0.002086334861814976f, 0.001097041997127235f, 0.0005262381164357066f, 0.0002302826324012131f, 0.0002302826324012131f, 0.0005262381164357066f, 0.001097041997127235f, 0.002086334861814976f, 0.003619635012000799f, 0.005728822201490402f, 0.008271530270576477f, 0.01089497376233339f, 0.01309141051024199f, 0.01435048412531614f, 0.01435048412531614f, 0.01309141051024199f, 0.01089497376233339f, 0.008271530270576477f, 0.005728822201490402f, 0.003619635012000799f, 0.002086334861814976f, 0.001097041997127235f, 0.0005262381164357066f, 0.0002302826324012131f, 0.0002100782439811155f, 0.0004800673632416874f, 0.001000790391117334f, 0.001903285388834775f, 0.00330205773934722f, 0.00522619066759944f, 0.007545807864516974f, 0.009939077310264111f, 0.01194280479103327f, 0.01309141051024199f, 0.01309141051024199f, 0.01194280479103327f, 0.009939077310264111f, 0.007545807864516974f, 0.00522619066759944f, 0.00330205773934722f, 0.001903285388834775f, 0.001000790391117334f, 0.0004800673632416874f, 0.0002100782439811155f, 0.0001748319627949968f, 0.0003995231236331165f, 0.0008328808471560478f, 0.001583957928232849f, 0.002748048631474376f, 0.004349356517195702f, 0.006279794964939356f, 0.008271529339253902f, 0.009939077310264111f, 0.01089497376233339f, 0.01089497376233339f, 0.009939077310264111f, 0.008271529339253902f, 0.006279794964939356f, 0.004349356517195702f, 0.002748048631474376f, 0.001583957928232849f, 0.0008328808471560478f, 0.0003995231236331165f, 0.0001748319627949968f, 0.0001327334757661447f, 0.0003033203829545528f, 0.0006323281559161842f, 0.001202550483867526f, 0.002086335094645619f, 0.00330205773934722f, 0.004767658654600382f, 0.006279794964939356f, 0.007545807864516974f, 0.008271530270576477f, 0.008271530270576477f, 0.007545807864516974f, 0.006279794964939356f, 0.004767658654600382f, 0.00330205773934722f, 0.002086335094645619f, 0.001202550483867526f, 0.0006323281559161842f, 0.0003033203829545528f, 0.0001327334757661447f, 9.193058212986216e-005f, 0.0002100782585330308f, 0.0004379475140012801f, 0.0008328807889483869f, 0.001444985857233405f, 0.002286989474669099f, 0.00330205773934722f, 0.004349356517195702f, 0.00522619066759944f, 0.005728822201490402f, 0.005728822201490402f, 0.00522619066759944f, 0.004349356517195702f, 0.00330205773934722f, 0.002286989474669099f, 0.001444985857233405f, 0.0008328807889483869f, 0.0004379475140012801f, 0.0002100782585330308f, 9.193058212986216e-005f, 5.808438800158911e-005f, 0.0001327334903180599f, 0.0002767078403849155f, 0.0005262380582280457f, 0.0009129836107604206f, 0.001444985857233405f, 0.002086335094645619f, 0.002748048631474376f, 0.00330205773934722f, 0.003619635012000799f, 0.003619635012000799f, 0.00330205773934722f, 0.002748048631474376f, 0.002086335094645619f, 0.001444985857233405f, 0.0009129836107604206f, 0.0005262380582280457f, 0.0002767078403849155f, 0.0001327334903180599f, 5.808438800158911e-005f, 3.34794785885606e-005f, 7.650675252079964e-005f, 0.0001594926579855382f, 0.0003033203247468919f, 0.0005262380582280457f, 0.0008328807889483869f, 0.001202550483867526f, 0.001583957928232849f, 0.001903285388834775f, 0.002086334861814976f, 0.002086334861814976f, 0.001903285388834775f, 0.001583957928232849f, 0.001202550483867526f, 0.0008328807889483869f, 0.0005262380582280457f, 0.0003033203247468919f, 0.0001594926579855382f, 7.650675252079964e-005f, 3.34794785885606e-005f, 1.760426494001877e-005f, 4.022897701361217e-005f, 8.386484114453197e-005f, 0.0001594926579855382f, 0.0002767078403849155f, 0.0004379475140012801f, 0.0006323281559161842f, 0.0008328808471560478f, 0.001000790391117334f, 0.001097041997127235f, 0.001097041997127235f, 0.001000790391117334f, 0.0008328808471560478f, 0.0006323281559161842f, 0.0004379475140012801f, 0.0002767078403849155f, 0.0001594926579855382f, 8.386484114453197e-005f, 4.022897701361217e-005f, 1.760426494001877e-005f, 8.444558261544444e-006f, 1.929736572492402e-005f, 4.022897701361217e-005f, 7.650675252079964e-005f, 0.0001327334903180599f, 0.0002100782585330308f, 0.0003033203829545528f, 0.0003995231236331165f, 0.0004800673632416874f, 0.0005262381164357066f, 0.0005262381164357066f, 0.0004800673632416874f, 0.0003995231236331165f, 0.0003033203829545528f, 0.0002100782585330308f, 0.0001327334903180599f, 7.650675252079964e-005f, 4.022897701361217e-005f, 1.929736572492402e-005f, 8.444558261544444e-006f, 3.695352233989979e-006f, 8.444558261544444e-006f, 1.760426494001877e-005f, 3.34794785885606e-005f, 5.808438800158911e-005f, 9.193058212986216e-005f, 0.0001327334757661447f, 0.0001748319627949968f, 0.0002100782439811155f, 0.0002302826324012131f, 0.0002302826324012131f, 0.0002100782439811155f, 0.0001748319627949968f, 0.0001327334757661447f, 9.193058212986216e-005f, 5.808438800158911e-005f, 3.34794785885606e-005f, 1.760426494001877e-005f, 8.444558261544444e-006f, 3.695352233989979e-006f }; struct WinReader { typedef uchar elem_type; __device__ __forceinline__ WinReader(float centerX_, float centerY_, float win_offset_, float cos_dir_, float sin_dir_) : centerX(centerX_), centerY(centerY_), win_offset(win_offset_), cos_dir(cos_dir_), sin_dir(sin_dir_) { } __device__ __forceinline__ uchar operator ()(int i, int j) const { float pixel_x = centerX + (win_offset + j) * cos_dir + (win_offset + i) * sin_dir; float pixel_y = centerY - (win_offset + j) * sin_dir + (win_offset + i) * cos_dir; return tex2D(imgTex, pixel_x, pixel_y); } float centerX; float centerY; float win_offset; float cos_dir; float sin_dir; }; __device__ void calc_dx_dy(float s_dx_bin[25], float s_dy_bin[25], const float* featureX, const float* featureY, const float* featureSize, const float* featureDir) { __shared__ float s_PATCH[6][6]; const float centerX = featureX[blockIdx.x]; const float centerY = featureY[blockIdx.x]; const float size = featureSize[blockIdx.x]; float descriptor_dir = 360.0f - featureDir[blockIdx.x]; if (std::abs(descriptor_dir - 360.f) < FLT_EPSILON) descriptor_dir = 0.f; descriptor_dir *= (float)(CV_PI_F / 180.0f); /* The sampling intervals and wavelet sized for selecting an orientation and building the keypoint descriptor are defined relative to 's' */ const float s = size * 1.2f / 9.0f; /* Extract a window of pixels around the keypoint of size 20s */ const int win_size = (int)((PATCH_SZ + 1) * s); float sin_dir; float cos_dir; sincosf(descriptor_dir, &sin_dir, &cos_dir); /* Nearest neighbour version (faster) */ const float win_offset = -(float)(win_size - 1) / 2; // Compute sampling points // since grids are 2D, need to compute xBlock and yBlock indices const int xBlock = (blockIdx.y & 3); // blockIdx.y % 4 const int yBlock = (blockIdx.y >> 2); // floor(blockIdx.y/4) const int xIndex = xBlock * 5 + threadIdx.x; const int yIndex = yBlock * 5 + threadIdx.y; const float icoo = ((float)yIndex / (PATCH_SZ + 1)) * win_size; const float jcoo = ((float)xIndex / (PATCH_SZ + 1)) * win_size; LinearFilter<WinReader> filter(WinReader(centerX, centerY, win_offset, cos_dir, sin_dir)); s_PATCH[threadIdx.y][threadIdx.x] = filter(icoo, jcoo); __syncthreads(); if (threadIdx.x < 5 && threadIdx.y < 5) { const int tid = threadIdx.y * 5 + threadIdx.x; const float dw = c_DW[yIndex * PATCH_SZ + xIndex]; const float vx = (s_PATCH[threadIdx.y ][threadIdx.x + 1] - s_PATCH[threadIdx.y][threadIdx.x] + s_PATCH[threadIdx.y + 1][threadIdx.x + 1] - s_PATCH[threadIdx.y + 1][threadIdx.x ]) * dw; const float vy = (s_PATCH[threadIdx.y + 1][threadIdx.x ] - s_PATCH[threadIdx.y][threadIdx.x] + s_PATCH[threadIdx.y + 1][threadIdx.x + 1] - s_PATCH[threadIdx.y ][threadIdx.x + 1]) * dw; s_dx_bin[tid] = vx; s_dy_bin[tid] = vy; } } __device__ void reduce_sum25(volatile float* sdata1, volatile float* sdata2, volatile float* sdata3, volatile float* sdata4, int tid) { // first step is to reduce from 25 to 16 if (tid < 9) // use 9 threads { sdata1[tid] += sdata1[tid + 16]; sdata2[tid] += sdata2[tid + 16]; sdata3[tid] += sdata3[tid + 16]; sdata4[tid] += sdata4[tid + 16]; } // sum (reduce) from 16 to 1 (unrolled - aligned to a half-warp) if (tid < 8) { sdata1[tid] += sdata1[tid + 8]; sdata1[tid] += sdata1[tid + 4]; sdata1[tid] += sdata1[tid + 2]; sdata1[tid] += sdata1[tid + 1]; sdata2[tid] += sdata2[tid + 8]; sdata2[tid] += sdata2[tid + 4]; sdata2[tid] += sdata2[tid + 2]; sdata2[tid] += sdata2[tid + 1]; sdata3[tid] += sdata3[tid + 8]; sdata3[tid] += sdata3[tid + 4]; sdata3[tid] += sdata3[tid + 2]; sdata3[tid] += sdata3[tid + 1]; sdata4[tid] += sdata4[tid + 8]; sdata4[tid] += sdata4[tid + 4]; sdata4[tid] += sdata4[tid + 2]; sdata4[tid] += sdata4[tid + 1]; } } __global__ void compute_descriptors64(PtrStepf descriptors, const float* featureX, const float* featureY, const float* featureSize, const float* featureDir) { // 2 floats (dx,dy) for each thread (5x5 sample points in each sub-region) __shared__ float sdx[25]; __shared__ float sdy[25]; __shared__ float sdxabs[25]; __shared__ float sdyabs[25]; calc_dx_dy(sdx, sdy, featureX, featureY, featureSize, featureDir); __syncthreads(); const int tid = threadIdx.y * blockDim.x + threadIdx.x; if (tid < 25) { sdxabs[tid] = ::fabs(sdx[tid]); // |dx| array sdyabs[tid] = ::fabs(sdy[tid]); // |dy| array __syncthreads(); reduce_sum25(sdx, sdy, sdxabs, sdyabs, tid); __syncthreads(); float* descriptors_block = descriptors.ptr(blockIdx.x) + (blockIdx.y << 2); // write dx, dy, |dx|, |dy| if (tid == 0) { descriptors_block[0] = sdx[0]; descriptors_block[1] = sdy[0]; descriptors_block[2] = sdxabs[0]; descriptors_block[3] = sdyabs[0]; } } } __global__ void compute_descriptors128(PtrStepf descriptors, const float* featureX, const float* featureY, const float* featureSize, const float* featureDir) { // 2 floats (dx,dy) for each thread (5x5 sample points in each sub-region) __shared__ float sdx[25]; __shared__ float sdy[25]; // sum (reduce) 5x5 area response __shared__ float sd1[25]; __shared__ float sd2[25]; __shared__ float sdabs1[25]; __shared__ float sdabs2[25]; calc_dx_dy(sdx, sdy, featureX, featureY, featureSize, featureDir); __syncthreads(); const int tid = threadIdx.y * blockDim.x + threadIdx.x; if (tid < 25) { if (sdy[tid] >= 0) { sd1[tid] = sdx[tid]; sdabs1[tid] = ::fabs(sdx[tid]); sd2[tid] = 0; sdabs2[tid] = 0; } else { sd1[tid] = 0; sdabs1[tid] = 0; sd2[tid] = sdx[tid]; sdabs2[tid] = ::fabs(sdx[tid]); } __syncthreads(); reduce_sum25(sd1, sd2, sdabs1, sdabs2, tid); __syncthreads(); float* descriptors_block = descriptors.ptr(blockIdx.x) + (blockIdx.y << 3); // write dx (dy >= 0), |dx| (dy >= 0), dx (dy < 0), |dx| (dy < 0) if (tid == 0) { descriptors_block[0] = sd1[0]; descriptors_block[1] = sdabs1[0]; descriptors_block[2] = sd2[0]; descriptors_block[3] = sdabs2[0]; } __syncthreads(); if (sdx[tid] >= 0) { sd1[tid] = sdy[tid]; sdabs1[tid] = ::fabs(sdy[tid]); sd2[tid] = 0; sdabs2[tid] = 0; } else { sd1[tid] = 0; sdabs1[tid] = 0; sd2[tid] = sdy[tid]; sdabs2[tid] = ::fabs(sdy[tid]); } __syncthreads(); reduce_sum25(sd1, sd2, sdabs1, sdabs2, tid); __syncthreads(); // write dy (dx >= 0), |dy| (dx >= 0), dy (dx < 0), |dy| (dx < 0) if (tid == 0) { descriptors_block[4] = sd1[0]; descriptors_block[5] = sdabs1[0]; descriptors_block[6] = sd2[0]; descriptors_block[7] = sdabs2[0]; } } } template <int BLOCK_DIM_X> __global__ void normalize_descriptors(PtrStepf descriptors) { // no need for thread ID float* descriptor_base = descriptors.ptr(blockIdx.x); // read in the unnormalized descriptor values (squared) __shared__ float sqDesc[BLOCK_DIM_X]; const float lookup = descriptor_base[threadIdx.x]; sqDesc[threadIdx.x] = lookup * lookup; __syncthreads(); if (BLOCK_DIM_X >= 128) { if (threadIdx.x < 64) sqDesc[threadIdx.x] += sqDesc[threadIdx.x + 64]; __syncthreads(); } // reduction to get total if (threadIdx.x < 32) { volatile float* smem = sqDesc; smem[threadIdx.x] += smem[threadIdx.x + 32]; smem[threadIdx.x] += smem[threadIdx.x + 16]; smem[threadIdx.x] += smem[threadIdx.x + 8]; smem[threadIdx.x] += smem[threadIdx.x + 4]; smem[threadIdx.x] += smem[threadIdx.x + 2]; smem[threadIdx.x] += smem[threadIdx.x + 1]; } // compute length (square root) __shared__ float len; if (threadIdx.x == 0) { len = sqrtf(sqDesc[0]); } __syncthreads(); // normalize and store in output descriptor_base[threadIdx.x] = lookup / len; } void compute_descriptors_gpu(const PtrStepSzf& descriptors, const float* featureX, const float* featureY, const float* featureSize, const float* featureDir, int nFeatures) { // compute unnormalized descriptors, then normalize them - odd indexing since grid must be 2D if (descriptors.cols == 64) { compute_descriptors64<<<dim3(nFeatures, 16, 1), dim3(6, 6, 1)>>>(descriptors, featureX, featureY, featureSize, featureDir); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); normalize_descriptors<64><<<dim3(nFeatures, 1, 1), dim3(64, 1, 1)>>>(descriptors); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); } else { compute_descriptors128<<<dim3(nFeatures, 16, 1), dim3(6, 6, 1)>>>(descriptors, featureX, featureY, featureSize, featureDir); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); normalize_descriptors<128><<<dim3(nFeatures, 1, 1), dim3(128, 1, 1)>>>(descriptors); cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaDeviceSynchronize() ); } } } // namespace surf } // namespace devcie } // namespace btl
d43a9eaacdd86aed7bf67d18e75ada20526a8af4.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" __global__ void kMaximum(float* mat1, float* mat2, float* target, unsigned int len) { const unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x; const unsigned int numThreads = blockDim.x * gridDim.x; for (unsigned int i = idx; i < len; i += numThreads) { target[i] = fmaxf(mat1[i], mat2[i]); } }
d43a9eaacdd86aed7bf67d18e75ada20526a8af4.cu
#include "includes.h" __global__ void kMaximum(float* mat1, float* mat2, float* target, unsigned int len) { const unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x; const unsigned int numThreads = blockDim.x * gridDim.x; for (unsigned int i = idx; i < len; i += numThreads) { target[i] = fmaxf(mat1[i], mat2[i]); } }
5f6e761bc0380f91b3f1257ed3c7b6fc6bac9c2d.hip
// !!! This is a file automatically generated by hipify!!! #ifdef USE_ROCM #include <cmath> #include <cstdlib> #include <cstring> #include <functional> #include "caffe/backend/cuda/cuda_device.hpp" #include "caffe/common.hpp" #include "caffe/backend/backend.hpp" #include "caffe/backend/vptr.hpp" #include "caffe/backend/dev_ptr.hpp" #include "caffe/backend/cuda/caffe_cuda.hpp" #include "caffe/backend/cuda/cuda_dev_ptr.hpp" #include <thrust/device_vector.h> #include <thrust/functional.h> // thrust::plus #include <thrust/reduce.h> namespace caffe { #ifdef USE_SINGLE void CudaDevice::axpy_float(const uint_tp n, const float alpha, vptr<const float> x, vptr<float> y) { CUBLAS_CHECK(hipblasSaxpy(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); } void CudaDevice::axpby_float(const uint_tp n, const float alpha, vptr<const float> x, const float beta, vptr<float> y) { this->scal_float(n, beta, y); this->axpy_float(n, alpha, x, y); } void CudaDevice::scal_float(const uint_tp n, const float alpha, vptr<float> x) { CUBLAS_CHECK(hipblasSscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); } void CudaDevice::dot_float(const uint_tp n, vptr<const float> x, vptr<const float> y, float* out) { CUBLAS_CHECK(hipblasSdot(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1, out)); } void CudaDevice::asum_float(const uint_tp n, vptr<const float> x, float* y) { CUBLAS_CHECK(hipblasSasum(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y)); } void CudaDevice::scale_float(const uint_tp n, const float alpha, vptr<const float> x, vptr<float> y) { CUBLAS_CHECK(hipblasScopy(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); CUBLAS_CHECK(hipblasSscal(Caffe::cublas_handle(), n, &alpha, y.get_cuda_ptr(), 1)); } template <> void CudaDevice::scal_str<float>(const int_tp n, const float alpha, vptr<float> x, hipStream_t str) { hipStream_t initial_stream; CUBLAS_CHECK(hipblasGetStream(Caffe::cublas_handle(), &initial_stream)); CUBLAS_CHECK(hipblasSetStream(Caffe::cublas_handle(), str)); CUBLAS_CHECK(hipblasSscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); CUBLAS_CHECK(hipblasSetStream(Caffe::cublas_handle(), initial_stream)); } #endif // USE_SINGLE #ifdef USE_DOUBLE void CudaDevice::axpy_double(const uint_tp n, const double alpha, vptr<const double> x, vptr<double> y) { CUBLAS_CHECK(hipblasDaxpy(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); } void CudaDevice::axpby_double(const uint_tp n, const double alpha, vptr<const double> x, const double beta, vptr<double> y) { this->scal_double(n, beta, y); this->axpy_double(n, alpha, x, y); } void CudaDevice::scal_double(const uint_tp n, const double alpha, vptr<double> x) { CUBLAS_CHECK(hipblasDscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); } void CudaDevice::dot_double(const uint_tp n, vptr<const double> x, vptr<const double> y, double* out) { CUBLAS_CHECK(hipblasDdot(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1, out)); } void CudaDevice::asum_double(const uint_tp n, vptr<const double> x, double* y) { CUBLAS_CHECK(hipblasDasum(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y)); } void CudaDevice::scale_double(const uint_tp n, const double alpha, vptr<const double> x, vptr<double> y) { CUBLAS_CHECK(hipblasDcopy(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); CUBLAS_CHECK(hipblasDscal(Caffe::cublas_handle(), n, &alpha, y.get_cuda_ptr(), 1)); } template <> void CudaDevice::scal_str<double>(const int_tp n, const double alpha, vptr<double> x, hipStream_t str) { hipStream_t initial_stream; CUBLAS_CHECK(hipblasGetStream(Caffe::cublas_handle(), &initial_stream)); CUBLAS_CHECK(hipblasSetStream(Caffe::cublas_handle(), str)); CUBLAS_CHECK(hipblasDscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); CUBLAS_CHECK(hipblasSetStream(Caffe::cublas_handle(), initial_stream)); } #endif // USE_DOUBLE } // namespace caffe #endif // USE_ROCM
5f6e761bc0380f91b3f1257ed3c7b6fc6bac9c2d.cu
#ifdef USE_CUDA #include <cmath> #include <cstdlib> #include <cstring> #include <functional> #include "caffe/backend/cuda/cuda_device.hpp" #include "caffe/common.hpp" #include "caffe/backend/backend.hpp" #include "caffe/backend/vptr.hpp" #include "caffe/backend/dev_ptr.hpp" #include "caffe/backend/cuda/caffe_cuda.hpp" #include "caffe/backend/cuda/cuda_dev_ptr.hpp" #include <thrust/device_vector.h> #include <thrust/functional.h> // thrust::plus #include <thrust/reduce.h> namespace caffe { #ifdef USE_SINGLE void CudaDevice::axpy_float(const uint_tp n, const float alpha, vptr<const float> x, vptr<float> y) { CUBLAS_CHECK(cublasSaxpy(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); } void CudaDevice::axpby_float(const uint_tp n, const float alpha, vptr<const float> x, const float beta, vptr<float> y) { this->scal_float(n, beta, y); this->axpy_float(n, alpha, x, y); } void CudaDevice::scal_float(const uint_tp n, const float alpha, vptr<float> x) { CUBLAS_CHECK(cublasSscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); } void CudaDevice::dot_float(const uint_tp n, vptr<const float> x, vptr<const float> y, float* out) { CUBLAS_CHECK(cublasSdot(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1, out)); } void CudaDevice::asum_float(const uint_tp n, vptr<const float> x, float* y) { CUBLAS_CHECK(cublasSasum(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y)); } void CudaDevice::scale_float(const uint_tp n, const float alpha, vptr<const float> x, vptr<float> y) { CUBLAS_CHECK(cublasScopy(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); CUBLAS_CHECK(cublasSscal(Caffe::cublas_handle(), n, &alpha, y.get_cuda_ptr(), 1)); } template <> void CudaDevice::scal_str<float>(const int_tp n, const float alpha, vptr<float> x, cudaStream_t str) { cudaStream_t initial_stream; CUBLAS_CHECK(cublasGetStream(Caffe::cublas_handle(), &initial_stream)); CUBLAS_CHECK(cublasSetStream(Caffe::cublas_handle(), str)); CUBLAS_CHECK(cublasSscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); CUBLAS_CHECK(cublasSetStream(Caffe::cublas_handle(), initial_stream)); } #endif // USE_SINGLE #ifdef USE_DOUBLE void CudaDevice::axpy_double(const uint_tp n, const double alpha, vptr<const double> x, vptr<double> y) { CUBLAS_CHECK(cublasDaxpy(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); } void CudaDevice::axpby_double(const uint_tp n, const double alpha, vptr<const double> x, const double beta, vptr<double> y) { this->scal_double(n, beta, y); this->axpy_double(n, alpha, x, y); } void CudaDevice::scal_double(const uint_tp n, const double alpha, vptr<double> x) { CUBLAS_CHECK(cublasDscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); } void CudaDevice::dot_double(const uint_tp n, vptr<const double> x, vptr<const double> y, double* out) { CUBLAS_CHECK(cublasDdot(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1, out)); } void CudaDevice::asum_double(const uint_tp n, vptr<const double> x, double* y) { CUBLAS_CHECK(cublasDasum(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y)); } void CudaDevice::scale_double(const uint_tp n, const double alpha, vptr<const double> x, vptr<double> y) { CUBLAS_CHECK(cublasDcopy(Caffe::cublas_handle(), n, x.get_cuda_ptr(), 1, y.get_cuda_ptr(), 1)); CUBLAS_CHECK(cublasDscal(Caffe::cublas_handle(), n, &alpha, y.get_cuda_ptr(), 1)); } template <> void CudaDevice::scal_str<double>(const int_tp n, const double alpha, vptr<double> x, cudaStream_t str) { cudaStream_t initial_stream; CUBLAS_CHECK(cublasGetStream(Caffe::cublas_handle(), &initial_stream)); CUBLAS_CHECK(cublasSetStream(Caffe::cublas_handle(), str)); CUBLAS_CHECK(cublasDscal(Caffe::cublas_handle(), n, &alpha, x.get_cuda_ptr(), 1)); CUBLAS_CHECK(cublasSetStream(Caffe::cublas_handle(), initial_stream)); } #endif // USE_DOUBLE } // namespace caffe #endif // USE_CUDA
5d9fae4e915a6d01a5c270abdee932738cd2bdf3.hip
// !!! This is a file automatically generated by hipify!!! // // auto-generated by op2.py // //header #include "op_lib_cpp.h" #include "op_cuda_rt_support.h" #include "op_cuda_reduction.h" //global constants #ifndef MAX_CONST_SIZE #define MAX_CONST_SIZE 128 #endif __constant__ float gam; __constant__ float gm1; __constant__ float cfl; __constant__ float eps; __constant__ float mach; __constant__ float alpha; __constant__ float qinf[4]; void op_decl_const_char(int dim, char const *type, int size, char *dat, char const *name){ if (!strcmp(name,"gam")) { cutilSafeCall(hipMemcpyToSymbol(gam, dat, dim*size)); } else if (!strcmp(name,"gm1")) { cutilSafeCall(hipMemcpyToSymbol(gm1, dat, dim*size)); } else if (!strcmp(name,"cfl")) { cutilSafeCall(hipMemcpyToSymbol(cfl, dat, dim*size)); } else if (!strcmp(name,"eps")) { cutilSafeCall(hipMemcpyToSymbol(eps, dat, dim*size)); } else if (!strcmp(name,"mach")) { cutilSafeCall(hipMemcpyToSymbol(mach, dat, dim*size)); } else if (!strcmp(name,"alpha")) { cutilSafeCall(hipMemcpyToSymbol(alpha, dat, dim*size)); } else if (!strcmp(name,"qinf")) { cutilSafeCall(hipMemcpyToSymbol(qinf, dat, dim*size)); } else { printf("error: unknown const name\n"); exit(1); } } //user kernel files #include "save_soln_kernel.cu" #include "adt_calc_kernel.hip" #include "res_calc_kernel.cu" #include "bres_calc_kernel.cu" #include "update_kernel.hip"
5d9fae4e915a6d01a5c270abdee932738cd2bdf3.cu
// // auto-generated by op2.py // //header #include "op_lib_cpp.h" #include "op_cuda_rt_support.h" #include "op_cuda_reduction.h" //global constants #ifndef MAX_CONST_SIZE #define MAX_CONST_SIZE 128 #endif __constant__ float gam; __constant__ float gm1; __constant__ float cfl; __constant__ float eps; __constant__ float mach; __constant__ float alpha; __constant__ float qinf[4]; void op_decl_const_char(int dim, char const *type, int size, char *dat, char const *name){ if (!strcmp(name,"gam")) { cutilSafeCall(cudaMemcpyToSymbol(gam, dat, dim*size)); } else if (!strcmp(name,"gm1")) { cutilSafeCall(cudaMemcpyToSymbol(gm1, dat, dim*size)); } else if (!strcmp(name,"cfl")) { cutilSafeCall(cudaMemcpyToSymbol(cfl, dat, dim*size)); } else if (!strcmp(name,"eps")) { cutilSafeCall(cudaMemcpyToSymbol(eps, dat, dim*size)); } else if (!strcmp(name,"mach")) { cutilSafeCall(cudaMemcpyToSymbol(mach, dat, dim*size)); } else if (!strcmp(name,"alpha")) { cutilSafeCall(cudaMemcpyToSymbol(alpha, dat, dim*size)); } else if (!strcmp(name,"qinf")) { cutilSafeCall(cudaMemcpyToSymbol(qinf, dat, dim*size)); } else { printf("error: unknown const name\n"); exit(1); } } //user kernel files #include "save_soln_kernel.cu" #include "adt_calc_kernel.cu" #include "res_calc_kernel.cu" #include "bres_calc_kernel.cu" #include "update_kernel.cu"
6d47b1924c5a9f0968f075b5e0839112c6b13513.hip
// !!! This is a file automatically generated by hipify!!! #include <stdbool.h> #include <stdio.h> #include <string.h> #include <getopt.h> #include <hiprand/hiprand_kernel.h> #include <stdlib.h> #include <hip/hip_runtime.h> #include <sys/time.h> #include "PolynomialFunctionKernel.cu" #include<chrono> #include<iostream> using namespace std; using namespace std::chrono; int blocks_[20][2] = {{8,8},{16,16},{24,24},{32,32},{1,64},{1,128},{1,192},{1,256},{1,320},{1,384},{1,448},{1,512},{1,576},{1,640},{1,704},{1,768},{1,832},{1,896},{1,960},{1,1024}}; int matrices_[7][2] = {{240,240},{496,496},{784,784},{1016,1016},{1232,1232},{1680,1680},{2024,2024}}; int main(int argc, char **argv) { hipSetDevice(0); char* p;int matrix_len=strtol(argv[1], &p, 10); for(int matrix_looper=0;matrix_looper<matrix_len;matrix_looper++){ for(int block_looper=0;block_looper<20;block_looper++){ int XSIZE=matrices_[matrix_looper][0],YSIZE=matrices_[matrix_looper][1],BLOCKX=blocks_[block_looper][0],BLOCKY=blocks_[block_looper][1]; float a3 = 1; float a2 = 1; float a1 = 1; float a0 = 1; float *input = NULL; hipMalloc(&input, XSIZE*YSIZE); float *output = NULL; hipMalloc(&output, XSIZE*YSIZE); int size = XSIZE*YSIZE; int iXSIZE= XSIZE; int iYSIZE= YSIZE; while(iXSIZE%BLOCKX!=0) { iXSIZE++; } while(iYSIZE%BLOCKY!=0) { iYSIZE++; } dim3 gridBlock(iXSIZE/BLOCKX, iYSIZE/BLOCKY); dim3 threadBlock(BLOCKX, BLOCKY); hipFree(0);hipLaunchKernelGGL(( PolynomialFunctionKernel), dim3(gridBlock),dim3(threadBlock), 0, 0, a3,a2,a1,a0,input,output,size); hipDeviceSynchronize(); for (int loop_counter = 0; loop_counter < 10; ++loop_counter) {hipLaunchKernelGGL(( PolynomialFunctionKernel), dim3(gridBlock),dim3(threadBlock), 0, 0, a3,a2,a1,a0,input,output,size); } auto start = steady_clock::now(); for (int loop_counter = 0; loop_counter < 1000; loop_counter++) {hipLaunchKernelGGL(( PolynomialFunctionKernel), dim3(gridBlock),dim3(threadBlock), 0, 0, a3,a2,a1,a0,input,output,size); } auto end = steady_clock::now(); auto usecs = duration_cast<duration<float, microseconds::period> >(end - start); cout <<'['<<usecs.count()<<','<<'('<<BLOCKX<<','<<BLOCKY<<')' << ','<<'('<<XSIZE<<','<<YSIZE<<')'<<']' << endl; } }}
6d47b1924c5a9f0968f075b5e0839112c6b13513.cu
#include <stdbool.h> #include <stdio.h> #include <string.h> #include <getopt.h> #include <curand_kernel.h> #include <stdlib.h> #include <cuda.h> #include <sys/time.h> #include "PolynomialFunctionKernel.cu" #include<chrono> #include<iostream> using namespace std; using namespace std::chrono; int blocks_[20][2] = {{8,8},{16,16},{24,24},{32,32},{1,64},{1,128},{1,192},{1,256},{1,320},{1,384},{1,448},{1,512},{1,576},{1,640},{1,704},{1,768},{1,832},{1,896},{1,960},{1,1024}}; int matrices_[7][2] = {{240,240},{496,496},{784,784},{1016,1016},{1232,1232},{1680,1680},{2024,2024}}; int main(int argc, char **argv) { cudaSetDevice(0); char* p;int matrix_len=strtol(argv[1], &p, 10); for(int matrix_looper=0;matrix_looper<matrix_len;matrix_looper++){ for(int block_looper=0;block_looper<20;block_looper++){ int XSIZE=matrices_[matrix_looper][0],YSIZE=matrices_[matrix_looper][1],BLOCKX=blocks_[block_looper][0],BLOCKY=blocks_[block_looper][1]; float a3 = 1; float a2 = 1; float a1 = 1; float a0 = 1; float *input = NULL; cudaMalloc(&input, XSIZE*YSIZE); float *output = NULL; cudaMalloc(&output, XSIZE*YSIZE); int size = XSIZE*YSIZE; int iXSIZE= XSIZE; int iYSIZE= YSIZE; while(iXSIZE%BLOCKX!=0) { iXSIZE++; } while(iYSIZE%BLOCKY!=0) { iYSIZE++; } dim3 gridBlock(iXSIZE/BLOCKX, iYSIZE/BLOCKY); dim3 threadBlock(BLOCKX, BLOCKY); cudaFree(0); PolynomialFunctionKernel<<<gridBlock,threadBlock>>>(a3,a2,a1,a0,input,output,size); cudaDeviceSynchronize(); for (int loop_counter = 0; loop_counter < 10; ++loop_counter) { PolynomialFunctionKernel<<<gridBlock,threadBlock>>>(a3,a2,a1,a0,input,output,size); } auto start = steady_clock::now(); for (int loop_counter = 0; loop_counter < 1000; loop_counter++) { PolynomialFunctionKernel<<<gridBlock,threadBlock>>>(a3,a2,a1,a0,input,output,size); } auto end = steady_clock::now(); auto usecs = duration_cast<duration<float, microseconds::period> >(end - start); cout <<'['<<usecs.count()<<','<<'('<<BLOCKX<<','<<BLOCKY<<')' << ','<<'('<<XSIZE<<','<<YSIZE<<')'<<']' << endl; } }}
6d8bc23f2745a14caad3b11061e6b95edb8889ca.hip
// !!! This is a file automatically generated by hipify!!! #include <stdio.h> #include <stdlib.h> #include <cutil.h> // Includes #include <stdio.h> #include "../include/ContAcq-IntClk.h" // includes, project #include "../include/sdkHelper.h" // helper for shared functions common to CUDA SDK samples //#include <shrQATest.h> //#include <shrUtils.h> // includes CUDA #include <hip/hip_runtime.h> #define THREADS_PER_BLOCK 256 #define NUM_OF_BLOCKS 60 #define ITERATIONS 40 // Variables unsigned* h_A; unsigned* h_B; unsigned* h_C; unsigned* d_A; unsigned* d_B; unsigned* d_C; bool noprompt = false; unsigned int my_timer; // Functions void CleanupResources(void); void RandomInit(unsigned*, int); void ParseArguments(int, char**); //////////////////////////////////////////////////////////////////////////////// // These are CUDA Helper functions // This will output the proper CUDA error strings in the event that a CUDA host call returns an error #define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__) inline void __checkCudaErrors(hipError_t err, const char *file, const int line ) { if(hipSuccess != err){ fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",file, line, (int)err, hipGetErrorString( err ) ); exit(-1); } } // This will output the proper error string when calling hipGetLastError #define getLastCudaError(msg) __getLastCudaError (msg, __FILE__, __LINE__) inline void __getLastCudaError(const char *errorMessage, const char *file, const int line ) { hipError_t err = hipGetLastError(); if (hipSuccess != err){ fprintf(stderr, "%s(%i) : getLastCudaError() CUDA error : %s : (%d) %s.\n",file, line, errorMessage, (int)err, hipGetErrorString( err ) ); exit(-1); } } // end of CUDA Helper Functions __global__ void PowerKernal2(const unsigned* A, const unsigned* B, unsigned* C, int N) { int i = blockDim.x * blockIdx.x + threadIdx.x; //Do Some Computation unsigned Value1=1; unsigned Value2=A[i]; unsigned Value3=B[i]; unsigned Value; unsigned I1=A[i]; unsigned I2=B[i]; // Excessive Addition access if( ((i%32)<=11) ){ for(unsigned k=0; k<ITERATIONS;k++) { Value2= I1*Value1; Value3=I2*Value3; Value1*=Value2; Value3*=Value1; Value2*=Value3; Value1*=Value3; // Value2= I1+I2; // Value3=I1-I2; // Value1=I1-Value2; // Value3+=Value1; // Value2-=Value3; // Value1+=Value3; } } __syncthreads(); Value=Value1; C[i]=Value; __syncthreads(); } int main() { printf("Power Microbenchmarks\n"); int N = THREADS_PER_BLOCK*NUM_OF_BLOCKS; size_t size = N * sizeof(unsigned); // Allocate input vectors h_A and h_B in host memory h_A = (unsigned*)malloc(size); if (h_A == 0) CleanupResources(); h_B = (unsigned*)malloc(size); if (h_B == 0) CleanupResources(); h_C = (unsigned*)malloc(size); if (h_C == 0) CleanupResources(); // Initialize input vectors RandomInit(h_A, N); RandomInit(h_B, N); // Allocate vectors in device memory checkCudaErrors( hipMalloc((void**)&d_A, size) ); checkCudaErrors( hipMalloc((void**)&d_B, size) ); checkCudaErrors( hipMalloc((void**)&d_C, size) ); // Copy vectors from host memory to device memory checkCudaErrors( hipMemcpy(d_A, h_A, size, hipMemcpyHostToDevice) ); checkCudaErrors( hipMemcpy(d_B, h_B, size, hipMemcpyHostToDevice) ); //VecAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, N); dim3 dimGrid(NUM_OF_BLOCKS,1); dim3 dimBlock(THREADS_PER_BLOCK,1); dim3 dimGrid2(1,1); dim3 dimBlock2(1,1); CUT_SAFE_CALL(cutCreateTimer(&my_timer)); TaskHandle taskhandle = LaunchDAQ(); CUT_SAFE_CALL(cutStartTimer(my_timer)); printf("execution time = %f\n", cutGetTimerValue(my_timer)); hipLaunchKernelGGL(( PowerKernal2), dim3(dimGrid),dim3(dimBlock), 0, 0, d_A, d_B, d_C, N); CUDA_SAFE_CALL( hipDeviceSynchronize() ); printf("execution time = %f\n", cutGetTimerValue(my_timer)); getLastCudaError("kernel launch failure"); CUDA_SAFE_CALL( hipDeviceSynchronize() ); CUT_SAFE_CALL(cutStopTimer(my_timer)); TurnOffDAQ(taskhandle, cutGetTimerValue(my_timer)); printf("execution time = %f\n", cutGetTimerValue(my_timer)); CUT_SAFE_CALL(cutDeleteTimer(my_timer)); #ifdef _DEBUG checkCudaErrors( hipDeviceSynchronize() ); #endif // Copy result from device memory to host memory // h_C contains the result in host memory checkCudaErrors( hipMemcpy(h_C, d_C, size, hipMemcpyDeviceToHost) ); CleanupResources(); return 0; } void CleanupResources(void) { // Free device memory if (d_A) hipFree(d_A); if (d_B) hipFree(d_B); if (d_C) hipFree(d_C); // Free host memory if (h_A) free(h_A); if (h_B) free(h_B); if (h_C) free(h_C); } // Allocates an array with random float entries. void RandomInit(unsigned* data, int n) { for (int i = 0; i < n; ++i){ srand((unsigned)time(0)); data[i] = rand() / RAND_MAX; } }
6d8bc23f2745a14caad3b11061e6b95edb8889ca.cu
#include <stdio.h> #include <stdlib.h> #include <cutil.h> // Includes #include <stdio.h> #include "../include/ContAcq-IntClk.h" // includes, project #include "../include/sdkHelper.h" // helper for shared functions common to CUDA SDK samples //#include <shrQATest.h> //#include <shrUtils.h> // includes CUDA #include <cuda_runtime.h> #define THREADS_PER_BLOCK 256 #define NUM_OF_BLOCKS 60 #define ITERATIONS 40 // Variables unsigned* h_A; unsigned* h_B; unsigned* h_C; unsigned* d_A; unsigned* d_B; unsigned* d_C; bool noprompt = false; unsigned int my_timer; // Functions void CleanupResources(void); void RandomInit(unsigned*, int); void ParseArguments(int, char**); //////////////////////////////////////////////////////////////////////////////// // These are CUDA Helper functions // This will output the proper CUDA error strings in the event that a CUDA host call returns an error #define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__) inline void __checkCudaErrors(cudaError err, const char *file, const int line ) { if(cudaSuccess != err){ fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",file, line, (int)err, cudaGetErrorString( err ) ); exit(-1); } } // This will output the proper error string when calling cudaGetLastError #define getLastCudaError(msg) __getLastCudaError (msg, __FILE__, __LINE__) inline void __getLastCudaError(const char *errorMessage, const char *file, const int line ) { cudaError_t err = cudaGetLastError(); if (cudaSuccess != err){ fprintf(stderr, "%s(%i) : getLastCudaError() CUDA error : %s : (%d) %s.\n",file, line, errorMessage, (int)err, cudaGetErrorString( err ) ); exit(-1); } } // end of CUDA Helper Functions __global__ void PowerKernal2(const unsigned* A, const unsigned* B, unsigned* C, int N) { int i = blockDim.x * blockIdx.x + threadIdx.x; //Do Some Computation unsigned Value1=1; unsigned Value2=A[i]; unsigned Value3=B[i]; unsigned Value; unsigned I1=A[i]; unsigned I2=B[i]; // Excessive Addition access if( ((i%32)<=11) ){ for(unsigned k=0; k<ITERATIONS;k++) { Value2= I1*Value1; Value3=I2*Value3; Value1*=Value2; Value3*=Value1; Value2*=Value3; Value1*=Value3; // Value2= I1+I2; // Value3=I1-I2; // Value1=I1-Value2; // Value3+=Value1; // Value2-=Value3; // Value1+=Value3; } } __syncthreads(); Value=Value1; C[i]=Value; __syncthreads(); } int main() { printf("Power Microbenchmarks\n"); int N = THREADS_PER_BLOCK*NUM_OF_BLOCKS; size_t size = N * sizeof(unsigned); // Allocate input vectors h_A and h_B in host memory h_A = (unsigned*)malloc(size); if (h_A == 0) CleanupResources(); h_B = (unsigned*)malloc(size); if (h_B == 0) CleanupResources(); h_C = (unsigned*)malloc(size); if (h_C == 0) CleanupResources(); // Initialize input vectors RandomInit(h_A, N); RandomInit(h_B, N); // Allocate vectors in device memory checkCudaErrors( cudaMalloc((void**)&d_A, size) ); checkCudaErrors( cudaMalloc((void**)&d_B, size) ); checkCudaErrors( cudaMalloc((void**)&d_C, size) ); // Copy vectors from host memory to device memory checkCudaErrors( cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice) ); checkCudaErrors( cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice) ); //VecAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, N); dim3 dimGrid(NUM_OF_BLOCKS,1); dim3 dimBlock(THREADS_PER_BLOCK,1); dim3 dimGrid2(1,1); dim3 dimBlock2(1,1); CUT_SAFE_CALL(cutCreateTimer(&my_timer)); TaskHandle taskhandle = LaunchDAQ(); CUT_SAFE_CALL(cutStartTimer(my_timer)); printf("execution time = %f\n", cutGetTimerValue(my_timer)); PowerKernal2<<<dimGrid,dimBlock>>>(d_A, d_B, d_C, N); CUDA_SAFE_CALL( cudaThreadSynchronize() ); printf("execution time = %f\n", cutGetTimerValue(my_timer)); getLastCudaError("kernel launch failure"); CUDA_SAFE_CALL( cudaThreadSynchronize() ); CUT_SAFE_CALL(cutStopTimer(my_timer)); TurnOffDAQ(taskhandle, cutGetTimerValue(my_timer)); printf("execution time = %f\n", cutGetTimerValue(my_timer)); CUT_SAFE_CALL(cutDeleteTimer(my_timer)); #ifdef _DEBUG checkCudaErrors( cudaDeviceSynchronize() ); #endif // Copy result from device memory to host memory // h_C contains the result in host memory checkCudaErrors( cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost) ); CleanupResources(); return 0; } void CleanupResources(void) { // Free device memory if (d_A) cudaFree(d_A); if (d_B) cudaFree(d_B); if (d_C) cudaFree(d_C); // Free host memory if (h_A) free(h_A); if (h_B) free(h_B); if (h_C) free(h_C); } // Allocates an array with random float entries. void RandomInit(unsigned* data, int n) { for (int i = 0; i < n; ++i){ srand((unsigned)time(0)); data[i] = rand() / RAND_MAX; } }
816023177bb7fe1804a4c9d7ad1ab576fe825dfa.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // cudamatrix/cu-kernels.cu // Copyright 2009-2012 Karel Vesely // 2013 Ehsan Variani // 2013 Johns Hopkins University (author: Daniel Povey) // 2013 Hainan Xu // 2013 Xiaohui Zhang // 2013-2015 Guoguo Chen // 2016-2018 Shiyin Kang // 2017 Hossein Hadian, Daniel Galvez // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. // In this file is the CUDA code of the CUDA kernels, plus the ANSI-C wrappers #include <cfloat> #include <limits> #include <math_constants.h> #include "cudamatrix/cu-kernels-ansi.h" /*********************************************************************** * Generic __device__ functions */ template<typename Real> __device__ static Real _sum_reduce(Real buffer[]) { // Total number of active threads int32_cuda nTotalThreads = blockDim.x; __syncthreads(); // perform tree-based reduction (sum) while (nTotalThreads > 1) { int32_cuda halfPoint = ((1 + nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x >= halfPoint) { // was < // Get the shared value stored by another thread Real temp = 0.0; if (threadIdx.x < nTotalThreads) { // was +halfPoint temp = buffer[threadIdx.x]; // was +halfPoint } buffer[threadIdx.x - halfPoint] += temp; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); // divide by two. } // the result return buffer[0]; } /*********************************************************************** * CUDA kernels * the functions are templated to have the float/double operations */ /* * CuMatrix */ template<typename Real> __global__ static void _copy_low_upp(Real* A, MatrixDim dimA) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (i <= j || i >= dimA.rows) return; int index_1 = i * dimA.stride + j; int index_2 = j * dimA.stride + i; A[index_2] = A[index_1]; } template<typename Real> __global__ static void _copy_upp_low(Real* A, MatrixDim dimA) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j <= i || j >= dimA.rows) return; int index_1 = i * dimA.stride + j; int index_2 = j * dimA.stride + i; A[index_2] = A[index_1]; } // mat += diag(vec) * mat2. template<typename Real> __global__ static void _add_diag_vec_mat(Real alpha, Real *mat, MatrixDim mat_dim, const Real *vec, const Real *mat2, int mat2_row_stride, int mat2_col_stride, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = j * mat_dim.stride + i, index2 = j * mat2_row_stride + i * mat2_col_stride; if (i < mat_dim.cols && j < mat_dim.rows) { mat[index] = alpha * vec[j] * mat2[index2] + beta * mat[index]; } } template<typename Real, typename OtherReal> __global__ static void _copy_from_tp(Real* A, const OtherReal* B, MatrixDim dmat) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // col index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dmat.cols && j < dmat.rows) { int32_cuda index_B = (j * (j + 1) / 2) + i; int32_cuda index_A = j * dmat.stride + i; if (i <= j) { A[index_A] = B[index_B]; } else { A[index_A] = 0.0; } } } template<typename Real, typename OtherReal> __global__ static void _copy_from_tp_trans(Real* A, const OtherReal* B, MatrixDim dmat) { // we interpret these indexes oppositely from normal, but it doesn't // matter as it's invoked in a symmetric way. int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // transpose the indices used to index the source TpMatrix. if (i < dmat.rows && j < dmat.cols) { int32_cuda index_B = (j * (j + 1) / 2) + i; int32_cuda index_A = i * dmat.stride + j; if (i <= j) { A[index_A] = B[index_B]; } else { A[index_A] = 0.0; } } } template<typename Real, typename OtherReal> __global__ static void _copy_from_mat(Real* mat_out, const OtherReal* mat_in, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // col-index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row-index. int32_cuda index_out = i + j * d_out.stride; int32_cuda index_in = i + j * d_in.stride; if (i < d_out.cols && j < d_out.rows) mat_out[index_out] = static_cast<Real>(mat_in[index_in]); } template<int TileDim, typename Real, typename OtherReal> __global__ static void _copy_from_mat_trans(Real* mat_out, const OtherReal* mat_in, MatrixDim d_out, MatrixDim d_in) { // Use shared meme to achieve both coalesced memory reading and writing // '+1' to avoid bank conflict when reading sbuf __shared__ Real sbuf[TileDim][TileDim + 1]; const int32_cuda i_in = blockIdx.y * TileDim + threadIdx.y; // row-index const int32_cuda j_in = blockIdx.x * TileDim + threadIdx.x; // col-index const int32_cuda tile_stride_in = CU1DBLOCK / TileDim * d_in.stride; int32_cuda index_in = i_in * d_in.stride + j_in; # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_in + i < d_in.rows && j_in < d_in.cols) { sbuf[threadIdx.y + i][threadIdx.x] = static_cast<Real>(mat_in[index_in]); } index_in += tile_stride_in; } __syncthreads(); // Grid is transposed, but block is not yet. // Warp (blockDim.x) is always along the row-dim. const int32_cuda i_out = blockIdx.x * TileDim + threadIdx.y; const int32_cuda j_out = blockIdx.y * TileDim + threadIdx.x; const int32_cuda tile_stride_out = CU1DBLOCK / TileDim * d_out.stride; int32_cuda index_out = i_out * d_out.stride + j_out; # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_out + i < d_out.rows && j_out < d_out.cols) { // block is tranposed when reading sbuf mat_out[index_out] = sbuf[threadIdx.x][threadIdx.y + i]; } index_out += tile_stride_out; } } // Copy from CSR sparse matrix to dense matrix // // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows. template<typename Real, typename OtherReal> __global__ static void _copy_from_smat(Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const OtherReal* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx if (i < mat_dim.rows) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx mat[i * mat_dim.stride + j] = static_cast<Real>(smat_val[nz_id]); } } } /// Select a subset of the rows of a CSR SparseMatrix. /// Sets 'out' to only the rows of 'in' that are listed /// in 'row_indexes'. 'row_indexes' must be sorted and unique, /// and satisfy 0 <= row_indexes[i] < in.size(). /// /// Note: 'out_row_ptr' is an input parameter that is calculated before /// calling this kernel function /// /// We use warpSize threads per row to access only the nnz elements. /// Every CU1DBLOCK/warpSize rows share one thread block. /// 1D grid to cover all selected rows. template<typename Real> __global__ static void _select_rows(const int* out_row_ptr, int* out_col_idx, Real* out_val, const int* row_indexes, const int num_selected_rows, const int* in_row_ptr, const int* in_col_idx, const Real* in_val) { const int out_i = blockIdx.x * blockDim.y + threadIdx.y; // out row idx if (out_i < num_selected_rows) { const int in_i = row_indexes[out_i]; const int in_row_start = in_row_ptr[in_i]; const int out_row_start = out_row_ptr[out_i]; const int row_length = in_row_ptr[in_i + 1] - in_row_start; for (int k = threadIdx.x; k < row_length; k += warpSize) { const int in_n = in_row_start + k; const int out_n = out_row_start + k; out_col_idx[out_n] = in_col_idx[in_n]; out_val[out_n] = in_val[in_n]; } } } // mat += alpha * smat // // We use warpSize threads per row to access only the nonzero elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _add_smat(Real* mat, MatrixDim mat_dim, Real alpha, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx if (i < mat_dim.rows) { const int row_start = smat_row_ptr[i]; const int row_end = smat_row_ptr[i + 1]; for (int n = row_start + threadIdx.x; n < row_end; n += warpSize) { const int j = smat_col_idx[n]; // col idx of smat mat[i * mat_dim.stride + j] += alpha * smat_val[n]; } } } // mat += alpha * smat^T // // We use warpSize threads per row to access only the nonzero elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _add_smat_trans(Real* mat, MatrixDim mat_dim, Real alpha, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx if (i < mat_dim.cols) { const int row_start = smat_row_ptr[i]; const int row_end = smat_row_ptr[i + 1]; for (int n = row_start + threadIdx.x; n < row_end; n += warpSize) { const int j = smat_col_idx[n]; // col idx of smat mat[j * mat_dim.stride + i] += alpha * smat_val[n]; } } } /// For each element x of the matrix, set it to /// (x < 0 ? exp(x) : x + 1). /// Use block/grid sizes for simple matrix ops template<typename T> __global__ static void _apply_exp_special(T* out, MatrixDim out_dim, const T* in, int in_stride) { const int i = blockIdx.x * blockDim.x + threadIdx.x; const int j = blockIdx.y * blockDim.y + threadIdx.y; if (i < out_dim.rows && j < out_dim.cols) { T x = in[i * in_stride + j]; if (x < T(0)) { out[i * out_dim.stride + j] = exp(x); } else { out[i * out_dim.stride + j] = x + T(1); } } } /// Fill the array 'data' with the sequence [base ... base + length) /// Use 1D block and 1D grid template<typename T> __global__ static void _sequence(T* data, int length, T base) { const int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < length) { data[i] = base + T(i); } } // Copy from CSR sparse matrix to transposed dense matrix // // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows. template<typename Real, typename OtherReal> __global__ static void _copy_from_smat_trans(Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const OtherReal* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx of smat if (i < mat_dim.cols) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx of smat mat[j * mat_dim.stride + i] = static_cast<Real>(smat_val[nz_id]); } } } // First stage of trace(mat * smat^T) // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _trace_mat_smat_trans(const Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val, Real* trace_vec) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx of smat if (i < mat_dim.rows) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx of smat trace_vec[nz_id] = mat[i * mat_dim.stride + j] * smat_val[nz_id]; } } } // First stage of trace(mat * smat) // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _trace_mat_smat(const Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val, Real* trace_vec) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx of smat if (i < mat_dim.cols) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx of smat trace_vec[nz_id] = mat[j * mat_dim.stride + i] * smat_val[nz_id]; } } } template<typename Real> __global__ static void _apply_exp(Real* mat, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) { mat[index] = exp(mat[index]); } } template<typename Real> __global__ static void _apply_fixed(Real* mat, Real resolution, int mode, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride ; Real tmp ; if(i < d.cols && j < d.rows) { if(1 == mode) { if(mat[index] > 0.0){ tmp = int(mat[index]/resolution)*resolution; }else{ tmp = int((mat[index]-resolution)/resolution)*resolution; } if((mat[index]-tmp)/resolution > 0.5){ mat[index] = tmp + resolution ; }else{ mat[index] = tmp ; } } else if (2 == mode) { mat[index] = int(mat[index]/resolution) * resolution; } } } template<typename Real> __global__ static void _apply_exp_limited(Real* mat, MatrixDim d, Real lower_limit, Real upper_limit) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) { Real x = mat[index]; // I'm writing !(x >= lower_limit) instead of (x < lower_limit) so that // nan's will be set to the lower-limit. if (!(x >= lower_limit)) x = lower_limit; else if (x > upper_limit) x = upper_limit; mat[index] = exp(x); } } template<typename Real> __global__ static void _scale_diag_packed(Real* mat, Real value, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { mat[index] = value * mat[index]; } } template<typename Real> __global__ static void _set_diag(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = i + i * d.stride; if (i < d.rows && i < d.cols) { mat[index] = value; } } template<typename Real> __global__ static void _set_diag_packed(Real* mat, Real value, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { mat[index] = value; } } template<typename Real> __global__ static void _add_diag_packed(Real* mat, Real value, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { mat[index] = mat[index] + value; } } template<typename Real> __global__ static void _set_const(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // column int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = value; } template<typename Real> __global__ static void _set_zero_above_diag(Real* mat, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < i) mat[index] = 0.0; } template<typename Real> __global__ static void _add(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = mat[index] + value; } template<typename Real> __global__ static void _scale(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = mat[index] * value; } template<typename Real> __global__ static void _apply_log(Real* mat, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = log(mat[index]); } template<typename Real> __global__ static void _mul_elements(Real* mat, const Real* A, MatrixDim dst_d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j * dst_d.stride, src_index = i + j * src_stride; if (i < dst_d.cols && j < dst_d.rows) mat[dst_index] = mat[dst_index] * A[src_index]; } template<typename Real> __global__ static void _div_elements(Real* mat, const Real* A, MatrixDim dst_d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j * dst_d.stride, src_index = i + j * src_stride; if (i < dst_d.cols && j < dst_d.rows) mat[dst_index] = mat[dst_index] / A[src_index]; } template<typename Real> __global__ static void _max(Real* mat, const Real* A, MatrixDim dst_d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j * dst_d.stride, src_index = i + j * src_stride; if (i < dst_d.cols && j < dst_d.rows) { Real a = mat[dst_index], b = A[src_index]; mat[dst_index] = fmax(a, b); } } template<typename Real> __global__ static void _min(Real* mat, const Real* other, MatrixDim mat_d, int other_stride) { int32_cuda j = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda i = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda mat_index = i * mat_d.stride + j; int32_cuda other_index = i * other_stride + j; if (j < mat_d.cols && i < mat_d.rows) { Real a = mat[mat_index], b = other[other_index]; mat[mat_index] = fmin(a, b); } } template<typename Real> __global__ static void _vec_mul_elements(Real* v, const Real* a, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) v[i] = v[i] * a[i]; } template<typename Real> __global__ static void _mul_cols_vec(Real* mat, const Real* scale, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] *= scale[i]; } template<typename Real> __global__ static void _mul_rows_vec(Real* mat, const Real* scale, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] *= scale[j]; } template<typename Real> __global__ static void _mul_rows_group_mat(Real *y, const Real *x, MatrixDim d, int src_stride, int group_size) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j < d.rows && i < d.cols) { int dst_index = i + j * d.stride; int src_index = i / group_size + j * src_stride; y[dst_index] *= x[src_index]; } } template<typename Real> __global__ void _diff_group_pnorm(Real *id, const Real *iv, const Real *ov, const Real* od, MatrixDim id_dim, int iv_stride, int ov_stride, int od_stride, int group_size, Real power) { const int j = blockIdx.x * blockDim.x + threadIdx.x; if (j < id_dim.cols) { const int grid_stride = gridDim.y * blockDim.y; const int src_j = j / group_size; int i = blockIdx.y * blockDim.y + threadIdx.y; for (; i < id_dim.rows; i += grid_stride) { const int iv_index = j + i * iv_stride; Real iv_ij = iv[iv_index]; Real ans; if (power == Real(2)) { const int ov_index = src_j + i * ov_stride; Real ov_ij = ov[ov_index]; ans = ov_ij <= 0.0 ? 0.0 : iv_ij / ov_ij; } else if (power == Real(1)) { Real iv_ij_sign = (iv_ij >= 0 ? 1 : -1); ans = (iv_ij == Real(0) ? 0.0 : iv_ij_sign); } else if (power == (sizeof(Real) == sizeof(float) ? CUDART_INF_F : CUDART_INF)) { const int ov_index = src_j + i * ov_stride; Real ov_ij = ov[ov_index]; Real iv_ij_sign = (iv_ij >= 0 ? 1 : -1); ans = ov_ij <= 0.0 ? 0.0 : (iv_ij_sign * (abs(iv_ij) == ov_ij ? 1.0 : 0.0)); } else { const int ov_index = src_j + i * ov_stride; Real ov_ij = ov[ov_index]; Real iv_ij_sign = (iv_ij >= 0 ? 1 : -1); if (ov_ij <= 0.0) { ans = 0.0; // The derivative is either 0 or undefined at the origin. } else { ans = iv_ij_sign * pow(std::abs(iv_ij), power - 1) * pow(ov_ij, 1 - power); } } const int od_index = src_j + i * od_stride; const int id_index = j + i * id_dim.stride; id[id_index] = ans * od[od_index]; } } } /// deriv is the derivative we will output; vec is the input we're computing /// the group max on, "maxv" is the previously computed group max. template<typename Real> __global__ static void _calc_group_max_deriv(Real *deriv, const Real *vec, const Real *maxv, MatrixDim deriv_dim, int vec_stride, int maxv_stride, int group_size) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j < deriv_dim.rows && i < deriv_dim.cols) { int deriv_index = i + j * deriv_dim.stride; int vec_index = i + j * vec_stride; int maxv_index = i / group_size + j * maxv_stride; Real vec_element = vec[vec_index], // The element of the original vector. max_element = maxv[maxv_index]; // this is the max value Real ans = (max_element == vec_element ? 1.0 : 0.0); deriv[deriv_index] = ans; } } /// Set each element to y = (x == orig ? changed : x). template<typename Real> __global__ static void _replace_value(Real *vec, int dim, Real orig, Real changed) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) if (vec[i] == orig) vec[i] = changed; } template<typename Real> __global__ static void _div_rows_vec(Real* mat, const Real* vec_div, MatrixDim d) { const int32_cuda i = blockIdx.y * blockDim.y + threadIdx.y; if (i < d.rows) { const int32_cuda start = i * d.stride; const Real scale = Real(1) / vec_div[i]; const int32_cuda grid_stride = blockDim.x * gridDim.x; for (int32_cuda j = blockIdx.x * blockDim.x + threadIdx.x; j < d.cols; j += grid_stride) { mat[start + j] *= scale; } } } template<typename Real> __global__ static void _add_mat(Real alpha, const Real* src, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // column index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row index int32_cuda index = i + j * d.stride; int32_cuda index_src = i + j * src_stride; if (i < d.cols && j < d.rows) dst[index] = alpha * src[index_src] + dst[index]; } template<typename Real> __global__ static void _add_mat_trans(Real alpha, const Real* src, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; int32_cuda index_src = j + i * src_stride; if (i < d.cols && j < d.rows) dst[index] = alpha * src[index_src] + dst[index]; } template<typename Real> __global__ static void _add_mat_blocks(Real alpha, const Real* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; int32_cuda index_src = i + j * src_stride; if (i < d.cols && j < d.rows) for (int32_cuda p = 0; p < num_row_blocks; p++) { for (int32_cuda q = 0; q < num_col_blocks; q++) { dst[index] = alpha * src[index_src + p * src_stride * d.rows + q * d.cols] + dst[index]; } } } template<typename Real> __global__ static void _add_mat_repeated(Real alpha, const Real* src, MatrixDim src_dim, Real* dst, MatrixDim dst_dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda src_i = i % src_dim.cols, src_j = j % src_dim.rows, dst_index = i + j * dst_dim.stride, src_index = src_i + src_j * src_dim.stride; if (i < dst_dim.cols && j < dst_dim.rows) dst[dst_index] += alpha * src[src_index]; } template<typename Real> __global__ static void _add_mat_blocks_trans(Real alpha, const Real* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; int32_cuda index_src = j + i * src_stride; if (i < d.cols && j < d.rows) for (int32_cuda p = 0; p < num_row_blocks; p++) { for (int32_cuda q = 0; q < num_col_blocks; q++) { dst[index] = alpha * src[index_src + p * src_stride * d.cols + q * d.rows] + dst[index]; } } } template<typename Real> __global__ static void _set_mat_mat_div_mat(const Real* A, const Real* B, const Real* C, Real* dst, MatrixDim d, int stride_a, int stride_b, int stride_c) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride, a_index = i + j * stride_a, b_index = i + j * stride_b, c_index = i + j * stride_c; if (i < d.cols && j < d.rows) if (C[c_index] == 0) dst[index] = A[a_index]; else dst[index] = A[a_index] * B[b_index] / C[c_index]; } // Given a matrix input S (not packed!) and a lower-triangular matrix L, this // function does S = beta S + alpha * L^T L. This is used in PSD matrix // inversion. The i index is the row of the destination S and the j the column // (although of course the output is symmetric so it doesn't matter in a sense). // The main point of this is to make use of various symmetries and zero-ness. template<typename Real> __global__ static void _sy_add_tr2(Real alpha, Real beta, const Real *T, MatrixDim tdim, Real *S, MatrixDim sdim) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (i >= sdim.rows || j > i) return; // this thread computes the dot-product of the i'th column of // L with the j'th column of L. The values we're multiplying // are only nonzero for row-index k greater or equal to // max(i, j), which equals i. Real sum = 0.0; for (int k = i; k < sdim.rows; k++) { int i_index = i + tdim.stride * k, j_index = j + tdim.stride * k; sum += T[i_index] * T[j_index]; } int output_index1 = i * sdim.stride + j, output_index2 = j * sdim.stride + i; S[output_index1] = alpha * sum + beta * S[output_index1]; S[output_index2] = alpha * sum + beta * S[output_index2]; } template<typename Real> __global__ static void _add_vec_to_cols(Real alpha, const Real* col, Real beta, Real* dst, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) dst[index] = alpha * col[j] + beta * dst[index]; } template<typename Real> __global__ static void _add_vec_to_rows(Real alpha, const Real* row, Real beta, Real* dst, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) dst[index] = alpha * row[i] + beta * dst[index]; } template<typename Real> __global__ static void _apply_mask(Real* mat, const char* mask, MatrixDim dmat, MatrixDim dmask) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * dmat.stride; int32_cuda index2 = i + j * dmask.stride; if (i < dmat.cols && j < dmat.rows) if (mask[index2] == 0) mat[index] = 0; } template<typename Real> __global__ static void _add_mat_diag_vec(Real alpha, Real *mat, MatrixDim mat_dim, const Real *mat2, int mat2_row_stride, int mat2_col_stride, const Real *vec, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * mat_dim.stride, index2 = i * mat2_col_stride + j * mat2_row_stride; if (j < mat_dim.rows && i < mat_dim.cols) mat[index] = alpha * mat2[index2] * vec[i] + beta * mat[index]; } template<typename Real> __global__ static void _add_mat_mat_elements(Real *data, const Real *srcA_data, const Real *srcB_data, MatrixDim dim, int srcA_stride, int srcB_stride, Real alpha, Real beta) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda tgt_index = i + j * dim.stride; int32_cuda srcA_index = i + j * srcA_stride; int32_cuda srcB_index = i + j * srcB_stride; if (i < dim.cols && j < dim.rows) { data[tgt_index] = alpha * srcA_data[srcA_index] * srcB_data[srcB_index] + beta * data[tgt_index]; } } /* * CuVector */ // very limited application! template<typename Real> __global__ static void _set_bias_params(Real* v, const Real* a, Real param_1, Real param_2, Real param_3, int* flag, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { Real ratio = a[i] / param_3; if ((ratio < 0.0) || (ratio >= 1.01)) { *flag = 1; return; } if (ratio < param_1) { Real factor = ((param_1 / ratio) > param_2) ? param_2 : (param_1 / ratio); v[i] = v[i] / factor; } else if (ratio > param_1) { Real factor = ((ratio / param_1) > param_2) ? param_2 : (ratio / param_1); v[i] = v[i] * factor; } } } template<typename Real, typename OtherReal> __global__ static void _cublas_copy_kaldi(int n, const Real* x, int incx, OtherReal* y, int incy) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) { y[i * incy] = static_cast<OtherReal>(x[i * incx]); } } // This kernel writes a copy of the vector "v_in" to each row of the matrix // "m_out". the dimension of v_in should be equal to the #columns of m_out. template<typename Real> __global__ static void _copy_rows_from_vec(Real* m_out, MatrixDim d, const Real* v_in) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index. int j = blockIdx.y * blockDim.y + threadIdx.y; // row index. if (i < d.cols && j < d.rows) { int index = i + j * d.stride; m_out[index] = v_in[i]; } } // This kernel writes a copy of the vector "v_in" to each col of the matrix // "m_out". the dimension of v_in should be equal to the #row of m_out. template<typename Real> __global__ static void _copy_cols_from_vec(Real* m_out, MatrixDim d, const Real* v_in) { int i = blockIdx.y * blockDim.y + threadIdx.y; // row id int j = blockIdx.x * blockDim.x + threadIdx.x; // col id if (i < d.rows && j < d.cols) { m_out[i * d.stride + j] = v_in[i]; } } // _trace_mat_mat reduce the partial sum to // value[blockIdx.y * gridDim.x + blockIdx.x] // It use shared mem to transpose matrix B to ensure coalesced memory access template<int TileDim, typename Real> __global__ static void _trace_mat_mat(const Real* A, const Real* B, MatrixDim dA, int B_stride, Real* value) { // Reuse shared mem and make indexing easier. "+1" to avoid bank conflict __shared__ union { Real trans[TileDim][TileDim + 1]; Real sum[CU1DBLOCK]; } smem; // linear thread id; const int32_cuda tid = threadIdx.y * blockDim.x + threadIdx.x; const int32_cuda grid_height = gridDim.y * TileDim; const int32_cuda ja = blockIdx.x * TileDim + threadIdx.x; const int32_cuda ib = blockIdx.x * TileDim + threadIdx.y; int32_cuda ia = blockIdx.y * TileDim + threadIdx.y; int32_cuda jb = blockIdx.y * TileDim + threadIdx.x; // Grid reduce Real tsum = Real(0); for (int32_cuda i0 = 0; i0 < dA.rows; i0 += grid_height) { // Load from B, transpose the block and store in shared mem if (jb < dA.rows) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (ib + i < dA.cols) { smem.trans[threadIdx.x][threadIdx.y + i] = B[(ib + i) * B_stride + jb]; } } } __syncthreads(); // Load from A, sum up the product. if (ja < dA.cols) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (ia + i < dA.rows) { tsum += A[(ia + i) * dA.stride + ja] * smem.trans[threadIdx.y + i][threadIdx.x]; } } } __syncthreads(); ia += grid_height; jb += grid_height; } smem.sum[tid] = tsum; __syncthreads(); // Block reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) smem.sum[tid] += smem.sum[tid + shift]; __syncthreads(); } // Warp reduce. Implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { smem.sum[tid] += smem.sum[tid + shift]; } } // output 1 sum per thread block if (tid == 0) { value[blockIdx.y * gridDim.x + blockIdx.x] = smem.sum[0]; } } // _trace_mat_mat_trans reduce the partial sum to // value[blockIdx.y * gridDim.x + blockIdx.x] template<typename Real> __global__ static void _trace_mat_mat_trans(const Real* A, const Real* B, MatrixDim dA, int B_stride, Real* value) { __shared__ Real ssum[CU1DBLOCK]; // linear thread id; const int32_cuda tid = threadIdx.y * blockDim.x + threadIdx.x; const int32_cuda j = blockIdx.x * blockDim.x + threadIdx.x; const int32_cuda grid_height = gridDim.y * blockDim.y; int32_cuda i = blockIdx.y * blockDim.y + threadIdx.y; // Grid reduce Real tsum = Real(0); if (j < dA.cols) { while (i < dA.rows) { tsum += A[i * dA.stride + j] * B[i * B_stride + j]; i += grid_height; } } ssum[tid] = tsum; __syncthreads(); // Block reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) ssum[tid] += ssum[tid + shift]; __syncthreads(); } // Warp reduce. Implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // output 1 sum per thread block if (tid == 0) { value[blockIdx.y * gridDim.x + blockIdx.x] = ssum[0]; } } // v = alpha * diag(M * N^T) + beta * v template<typename Real> __global__ static void _add_diag_mat_mat_MNT(const Real alpha, const Real* M, const MatrixDim dim_M, const Real* N, const int stride_N, const Real beta, Real* v) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int m_start = i * dim_M.stride; const int n_start = i * stride_N; // Loop along the matrix row. Reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < dim_M.cols; j += CU1DBLOCK) { tsum += M[m_start + j] * N[n_start + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) ssum[tid] += ssum[tid + shift]; __syncthreads(); } // Warp reduce to 1 element. Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // output 1 sum per thread block if (tid == 0) { v[i] = alpha * ssum[0] + beta * v[i]; } } // v = alpha * diag(M^T * N) + beta * v template<int TileDim, typename Real> __global__ static void _add_diag_mat_mat_MTN(const Real alpha, const Real* M, const int stride_M, const Real* N, const MatrixDim dim_N, const Real beta, Real* v, const int stride_v) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.y * blockDim.x + threadIdx.x; const int j = blockIdx.x * blockDim.x + threadIdx.x; if (j >= dim_N.cols) return; // Loop along the matrix column. // Reduce to gridDim.y * CU1DBLOCK / TileDim elements per column. Real tsum = Real(0); const int grid_stride_y = blockDim.y * gridDim.y; for (int i = blockIdx.y * blockDim.y + threadIdx.y; i < dim_N.rows; i += grid_stride_y) { tsum += M[i * stride_M + j] * N[i * dim_N.stride + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize / TileDim elements per column. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize && shift >= TileDim; shift >>= 1) { if (tid < shift) { ssum[tid] += ssum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element per column. // Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift >= TileDim; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // output TileDim sums per thread block if (tid < TileDim) { if (beta != Real(0)) { v[blockIdx.y * stride_v + j] = alpha * ssum[tid] + beta * v[blockIdx.y * stride_v + j]; } else { v[blockIdx.y * stride_v + j] = alpha * ssum[tid]; } } } // v = alpha * diag(M * N) + beta * v template<int TileDim, typename Real> __global__ static void _add_diag_mat_mat_MN(const Real alpha, const Real* M, const int stride_M, const Real* N, const MatrixDim dim_N, const Real beta, Real* v) { // Reuse shared mem and make indexing easier. "+1" to avoid bank conflict __shared__ union { Real trans[TileDim][TileDim + 1]; Real sum[CU1DBLOCK]; } smem; const int tid = threadIdx.y * blockDim.x + threadIdx.x; const int i_m = blockIdx.x * TileDim + threadIdx.y; const int j_n = blockIdx.x * TileDim + threadIdx.x; int i_n = threadIdx.y; int j_m = threadIdx.x; // Loop along the matrix column. // Reduce to CU1DBLOCK / TileDim elements per column. Real tsum = Real(0); for (int block_i_n = 0; block_i_n < dim_N.rows; block_i_n += TileDim) { // Load, transpose and store M to shared mem. if (j_m < dim_N.rows) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_m + i < dim_N.cols) { smem.trans[threadIdx.x][threadIdx.y + i] = M[(i_m + i) * stride_M + j_m]; } } } __syncthreads(); // Load N, sum up the product. if (j_n < dim_N.cols) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_n + i < dim_N.rows) { tsum += N[(i_n + i) * dim_N.stride + j_n] * smem.trans[threadIdx.y + i][threadIdx.x]; } } } __syncthreads(); i_n += TileDim; j_m += TileDim; } smem.sum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize / TileDim elements per column. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize && shift >= TileDim; shift >>= 1) { if (tid < shift) { smem.sum[tid] += smem.sum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element per column. // Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift >= TileDim; shift >>= 1) { smem.sum[tid] += smem.sum[tid + shift]; } } // output TileDim sums per thread block if (tid < TileDim && j_n < dim_N.cols) { v[j_n] = alpha * smem.sum[tid] + beta * v[j_n]; } } template<typename Real> __global__ static void _add_vec_vec(Real alpha, Real* v, const Real* x, const Real* y, Real beta, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // if (blockIdx.y > 0) return; if (i < dim) v[i] = alpha * x[i] * y[i] + beta * v[i]; } template<typename Real> __global__ static void _copy_col_from_mat_df(double* v, int col, const Real* mat, MatrixDim dmat, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = col + i * dmat.stride; // if (blockIdx.y > 0) return; if (i < dim) v[i] = (double) mat[index]; } template<typename Real> __global__ static void _copy_col_from_mat_fd(float* v, int col, const Real* mat, MatrixDim dmat, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = col + i * dmat.stride; // if (blockIdx.y > 0) return; if (i < dim) v[i] = (float) mat[index]; } template<typename Real> __global__ static void _vec_apply_exp(Real* v, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // if (blockIdx.y > 0) return; if (i < dim) { v[i] = exp(v[i]); } } template<typename Real> __global__ static void _vec_apply_log(Real* v, Real* flag, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // if (blockIdx.y > 0) return; if (i < dim) { if (v[i] < 0) { *flag = 1; return; } v[i] = log(v[i]); } } template<typename Real> __global__ static void _vec_apply_fixed(Real* v, Real resolution, int mode, int dim){ int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; Real tmp; if(i < dim) { if (1 == mode) { tmp = int(v[i]/resolution) * resolution; if(v[i] > 0){ if((v[i] - tmp) >= 0.5*resolution) v[i] = tmp + resolution; else v[i] = tmp ; }else{ if((tmp - v[i]) >= 0.5 * resolution) v[i] = tmp - resolution ; else v[i] = tmp; } } else if (2 == mode) { v[i] = (v[i]/resolution) * resolution; } } } template<typename Real> __global__ static void _cuda_comp_obj_deriv(MatrixElement<Real> *x, int s, const Real* z, MatrixDim d, Real* z2, MatrixDim d2, Real* t) { int i = threadIdx.x; __shared__ Real tot_objf[CU1DBLOCK]; __shared__ Real tot_weight[CU1DBLOCK]; Real tmp_weight_sum = 0; Real tmp_tot_objf = 0; int size = s / CU1DBLOCK; //the least size in a loop (later part) int threshold = s - size * CU1DBLOCK; //any loop below this number would + 1 int loop_start; int loop_end; if (i < threshold) { loop_start = i * (size + 1); loop_end = (i + 1) * (size + 1); } else { loop_start = threshold + i * size; loop_end = threshold + (i + 1) * size; } for (int j = loop_start; j < loop_end; j++) { //* ((int*) ((size_t)x + j * (2 * sizeof(int) + sizeof(Real) )) ); int m = (x + j)->row; //*(int*) ((size_t)x + j * (2 * sizeof(int) + sizeof(Real) )+ sizeof(int)); int label = (x + j)->column; // *(Real*) ((size_t)x + j*(2*sizeof(int) + sizeof(Real)) + 2*sizeof(int)); Real weight = (x + j)->weight; tmp_weight_sum += weight; Real this_prob = *(z + m * d.stride + label); tmp_tot_objf += weight * log(this_prob); // there might be problems here.... *(z2 + m * d2.stride + label) += weight / this_prob; } tot_objf[i] = tmp_tot_objf; tot_weight[i] = tmp_weight_sum; __syncthreads(); *t = _sum_reduce(tot_objf); __syncthreads(); *(t + 1) = _sum_reduce(tot_weight); return; } template<typename Real> __global__ static void _cuda_vector_copy_elements(Real *data, int dim, const Real *src_mat, int mat_stride, bool transpose, const MatrixIndexT_cuda* elements) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= dim) return; int j = elements[i]; int mat_index; if (transpose) mat_index = i + j * mat_stride; else mat_index = j + i * mat_stride; data[i] = src_mat[mat_index]; } template<typename Real> __global__ static void _cuda_matrix_add_elements(Real *data, MatrixDim dim, Real alpha, MatrixElement<Real>* x, int num_elements) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= num_elements) return; data[x[i].row * dim.stride + x[i].column] += alpha * x[i].weight; } template<typename Real> __global__ static void _cuda_matrix_add_indexed_values(MatrixDim dim, Real alpha, const Int32Pair* indices, const Real* x, int s, Real* data) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= s) return; int data_i = indices[i].first * dim.stride + indices[i].second; data[data_i] += alpha * x[i]; } template<typename Real> __global__ static void _cuda_matrix_add_to_elements(Real alpha, Real* mat, MatrixDim dim, const MatrixIndexT_cuda* elements) { int row = blockIdx.x * blockDim.x + threadIdx.x; if (row < dim.rows) { int col = elements[row]; if (col >= 0) { int index = col + row * dim.stride; mat[index] += alpha; } } } template<typename Real> __global__ static void _matrix_lookup(const Real *data, MatrixDim dim, const Int32Pair *indices, int indices_size, Real *output) { int ind = blockIdx.x * blockDim.x + threadIdx.x; if (ind >= indices_size) return; int data_ind = indices[ind].first * dim.stride + indices[ind].second; output[ind] = data[data_ind]; } template<typename Real> __global__ static void _equal_element_mask(const Real *mat1, const Real *mat2, Real *mask, MatrixDim mat1_dim, int mat2_stride, int mask_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // col int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row int32_cuda index_mat1 = i + j * mat1_dim.stride; int32_cuda index_mat2 = i + j * mat2_stride; int32_cuda index_mask = i + j * mask_stride; if (i < mat1_dim.cols && j < mat1_dim.rows) mask[index_mask] = (mat1[index_mat1] == mat2[index_mat2] ? 1.0 : 0.0); } enum EnumTransformReduce { SUMAB, SUM, ABSMAX, MAX, MIN, LINFNORM, L2NORM, L1NORM, L0NORM, LPNORM }; template<EnumTransformReduce TransReduceType, typename Real> struct TransReduceOp { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return Real(0); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return Real(0); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return Real(0); } }; template<typename Real> struct TransReduceOp<SUMAB, Real> { const Real alpha_; const Real beta_; TransReduceOp(const Real& a, const Real& b) : alpha_(a), beta_(b) { } __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { if (beta_ == Real(0)) { return alpha_ * x; } else { return alpha_ * x + beta_ * output; } } }; template<typename Real> struct TransReduceOp<SUM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<MAX, Real> { __forceinline__ __device__ Real InitValue() const { return sizeof(Real) == sizeof(float) ? -CUDART_INF_F : -CUDART_INF; } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return fmax(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<MIN, Real> { __forceinline__ __device__ Real InitValue() const { return sizeof(Real) == sizeof(float) ? CUDART_INF_F : CUDART_INF; } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return min(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<ABSMAX, Real> { __forceinline__ __device__ Real InitValue() const { return sizeof(Real) == sizeof(float) ? -CUDART_INF_F : -CUDART_INF; } __forceinline__ __device__ Real Transform(const Real& x) const { return fabs(x); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return fmax(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return fabs(x); } }; template<typename Real> struct TransReduceOp<LINFNORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return abs(x); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return fmax(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<L2NORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return x * x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return sqrt(x); } }; template<typename Real> struct TransReduceOp<L1NORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return abs(x); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<L0NORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return Real(x == Real(0) ? 0 : 1); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<LPNORM, Real> { const Real power_; TransReduceOp(const Real& p) : power_(p) { } __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return pow(abs(x), power_); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return pow(x, Real(1) / power_); } }; // Vector reduce. template<EnumTransformReduce TransReduceType, typename Real> __global__ static void _vec_transform_reduce( const Real* v, Real* result, const int dim, const int inc, const TransReduceOp<TransReduceType, Real> op) { __shared__ Real sdata[CU1DBLOCK]; Real tdata = op.InitValue(); const int tid = threadIdx.x; const int vec_len = dim * inc; const int grid_stride = gridDim.x * blockDim.x * inc; int i = (blockIdx.x * blockDim.x + tid) * inc; // Grid reduce. Loop over the whole vector v. for (; i < vec_len; i += grid_stride) { tdata = op.Reduce(tdata, op.Transform(v[i])); } sdata[tid] = tdata; __syncthreads(); // Tree reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); } __syncthreads(); } // Reduce last warp. Threads implicitly synchronized within a warp. if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) { sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); } } // Output to vector result. if (tid == 0) result[blockIdx.x] = op.PostReduce(sdata[0], result[blockIdx.x]); } // Reduce a matrix 'mat' to a column vector 'result' template<EnumTransformReduce TransReduceType, typename Real> __global__ static void _transform_reduce_mat_cols( Real *result, const Real *mat, const MatrixDim d, const TransReduceOp<TransReduceType, Real> op) { __shared__ Real sdata[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int row_start = i * d.stride; Real tdata = op.InitValue(); for (int j = tid; j < d.cols; j += CU1DBLOCK) { tdata = op.Reduce(tdata, op.Transform(mat[row_start + j])); } sdata[tid] = tdata; __syncthreads(); // Tree reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); __syncthreads(); } // Reduce last warp. Threads implicitly synchronized within a warp. if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); } // Output to vector result. if (tid == 0) { result[i] = op.PostReduce(sdata[0], result[i]); } } template<EnumTransformReduce TransReduceType, typename Real> __global__ static void _group_transform_reduce( Real *y, const Real *x, const MatrixDim d, const int src_stride, const int group_size, const TransReduceOp<TransReduceType, Real> op) { __shared__ Real sreduction[CU1DBLOCK]; const int i = blockIdx.x; const int x_start = i * src_stride; const int y_start = i * d.stride; const int threads_per_group = blockDim.x; // Reduce n groups per thread block const int n = blockDim.y; const int len = group_size * n; // linear thread id const int tid = threadIdx.y * threads_per_group + threadIdx.x; int j = threadIdx.y * group_size + threadIdx.x; // col-id of *x int group_id = threadIdx.y; // col-id of *y int group_end = x_start + (group_id + 1) * group_size; while (group_id < d.cols) { // reduce to threads_per_group elements per group int x_idx = x_start + j; Real treduction = op.Transform(x[x_idx]); x_idx += threads_per_group; while (x_idx < group_end) { treduction = op.Reduce(treduction, op.Transform(x[x_idx])); x_idx += threads_per_group; } sreduction[tid] = treduction; if (threads_per_group > warpSize) { __syncthreads(); } // tree-reduce to 2x warpSize elements per group # pragma unroll for (int shift = threads_per_group / 2; shift > warpSize; shift >>= 1) { if (threadIdx.x < shift) { sreduction[tid] = op.Reduce(sreduction[tid], sreduction[tid + shift]); } __syncthreads(); } // Warp-reduce to 1 element per group. // Threads implicitly synchronized within the warp. const int warp_reduce_size = threads_per_group / 2 < warpSize ? threads_per_group / 2 : warpSize; if (threadIdx.x < warp_reduce_size) { # pragma unroll for (int shift = warp_reduce_size; shift > 0; shift >>= 1) { sreduction[tid] = op.Reduce(sreduction[tid], sreduction[tid + shift]); } } // Store the result. if (threadIdx.x == 0) { y[y_start + group_id] = op.PostReduce(sreduction[tid], y[y_start + group_id]); } j += len; group_end += len; group_id += n; } } template<typename Real> __global__ static void _vec_apply_floor(Real *v, Real floor_val, float *count, int dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { if (v[i] < floor_val) { v[i] = floor_val; count[i] = 1; } else { count[i] = 0; } } } template<typename Real> __global__ static void _vec_apply_ceiling(Real *v, Real ceiling_val, float *count, int dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { if (v[i] > ceiling_val) { v[i] = ceiling_val; count[i] = 1; } else { count[i] = 0; } } } template<typename Real> __global__ static void _apply_pow(Real* mat, Real power, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) { if (power == 1.0) return; if (power == 2.0) { mat[index] = mat[index] * mat[index]; } else if (power == 0.5) { if (!(mat[index] >= 0.0)) return; mat[index] = sqrt(mat[index]); } else { mat[index] = pow(mat[index], power); } } } template<typename Real> __global__ static void _apply_pow_abs(Real* mat, Real power, bool include_sign, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) { if (include_sign == true && mat[index] < 0) { if (power == 1.0) mat[index] = -std::abs(mat[index]); if (power == 2.0) { mat[index] = -mat[index] * mat[index]; } else if (power == 0.5) { mat[index] = -sqrt(std::abs(mat[index])); } else { mat[index] = -pow(std::abs(mat[index]), power); } } else { if (power == 1.0) mat[index] = std::abs(mat[index]); if (power == 2.0) { mat[index] = mat[index] * mat[index]; } else if (power == 0.5) { mat[index] = sqrt(std::abs(mat[index])); } else if (power < 0.0 && mat[index] == 0.0) { mat[index] = 0.0; } else { mat[index] = pow(std::abs(mat[index]), power); } } } } template<typename Real> __global__ static void _apply_heaviside(Real* mat, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = (mat[index] > 0.0 ? 1.0 : 0.0); } template<typename Real> __global__ static void _apply_floor(Real* mat, Real floor_val, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) { mat[index] = max(mat[index], floor_val); } } template<typename Real> __global__ static void _copy_cols(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[i], dst_index = j * dst_dim.stride + i; if (index >= 0) { int src_index = j * src_stride + reorder[i]; Real val = src[src_index]; dst[dst_index] = val; } else { dst[dst_index] = 0.0; } } } template<typename Real> __global__ static void _add_cols(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[i], dst_index = j * dst_dim.stride + i; if (index >= 0) { int src_index = j * src_stride + index; Real val = src[src_index]; dst[dst_index] += val; } } } template<typename Real> __global__ static void _copy_rows(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[j], dst_index = j * dst_dim.stride + i; if (index >= 0) { int src_index = reorder[j] * src_stride + i; Real val = src[src_index]; dst[dst_index] = val; } else { //dst[dst_index] = 0; } } } template<typename Real> __global__ static void _copy_rows(Real* dst, const Real * const *src, MatrixDim dst_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; const Real *pointer = src[j]; if (pointer != NULL) { dst[dst_index] = pointer[i]; } else { dst[dst_index] = 0; } } } template<typename Real> __global__ static void _copy_to_rows(Real* const * dst, const Real *src, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < src_dim.cols && j < src_dim.rows) { Real *pointer = dst[j]; if (pointer != NULL) { pointer[i] = src[j * src_dim.stride + i]; } } } template<typename Real> __global__ static void _add_rows(Real alpha, Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (reorder[j] >= 0) { int src_index = reorder[j] * src_stride + i; dst[dst_index] += alpha * src[src_index]; } } } template<typename Real> __global__ static void _mul_rows(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (reorder[j] >= 0) { int src_index = reorder[j] * src_stride + i; dst[dst_index] *= src[src_index]; } } } template<typename Real> __global__ static void _div_rows(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (reorder[j] >= 0) { int src_index = reorder[j] * src_stride + i; if (src[src_index] == 0 && fabs(dst[dst_index]) <= 1e-6) dst[dst_index] = 0; else dst[dst_index] /= src[src_index]; } } } template<typename Real> __global__ static void _add_rows(Real alpha, Real* dst, const Real * const *src, MatrixDim dst_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (src[j] != NULL) { dst[dst_index] += alpha * src[j][i]; } } } template<typename Real> __global__ static void _add_to_rows(Real alpha, Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim src_dim, int dst_stride) { int c = blockIdx.x * blockDim.x + threadIdx.x; // col index int r = blockIdx.y * blockDim.y + threadIdx.y; // row index if (c < src_dim.cols && r < src_dim.rows) { int src_index = r * src_dim.stride + c; if (reorder[r] >= 0) { int dst_index = reorder[r] * dst_stride + c; dst[dst_index] += alpha * src[src_index]; } } } template<typename Real> __global__ static void _add_to_rows(Real alpha, Real* const * dst, const Real *src, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < src_dim.cols && j < src_dim.rows) { if (dst[j] != NULL) { dst[j][i] += alpha * src[j * src_dim.stride + i]; } } } template<typename Real> __global__ static void _apply_ceiling(Real* mat, Real ceiling_val, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index = i + j * d.stride; if (i < d.cols && j < d.rows) { mat[index] = min(mat[index], ceiling_val); } } template<typename Real> __global__ static void _invert_elements(Real* data, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index = i + j * d.stride; if (i < d.cols && j < d.rows) data[index] = 1.0 / data[index]; } // matrix-wise, do data = alpha * data + beta * A * B^T, // where B is a block matrix. template<typename Real> __global__ static void _add_mat_blockmat_trans(Real *data, MatrixDim dim, const Real *A_data, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, Real alpha, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index into "data" int j = blockIdx.y * blockDim.y + threadIdx.y; // block-index into B. if (i >= A_num_rows || j >= B_num_blocks) return; const CuBlockMatrixData &cu_data = B_cu_data[j]; // BT means B transposed. int BT_row_start = cu_data.col_offset, BT_col_start = cu_data.row_offset, BT_num_rows = cu_data.matrix_dim.cols, BT_num_cols = cu_data.matrix_dim.rows, BT_col_stride = cu_data.matrix_dim.stride; // Cast from void; const Real *B_data = static_cast<Real*>(cu_data.matrix_data); // we avoided a bunch of hassle by doing this (relates to Ansi-C requirement). for (int k = 0; k < BT_num_cols; k++) { const Real *this_BT_col = B_data + k * BT_col_stride; const Real *this_A_row = A_data + i * A_row_stride + BT_row_start * A_col_stride; // this_A_row points to the element A[i][BT_row_start], it's really just // part of this row of A. Real sum = 0.0; for (int l = 0; l < BT_num_rows; l++) // l indexes rows of B. sum += this_BT_col[l] * this_A_row[l * A_col_stride]; int index = i * dim.stride + (k + BT_col_start); data[index] = alpha * sum + beta * data[index]; } } template<typename Real> __global__ static void _add_mat_blockmat(Real *data, MatrixDim dim, const Real *A_data, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, Real alpha, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index into "data" int j = blockIdx.y * blockDim.y + threadIdx.y; // block-index into B. if (i >= A_num_rows || j >= B_num_blocks) return; const CuBlockMatrixData &block_data = B_cu_data[j]; int B_row_start = block_data.row_offset, B_col_start = block_data.col_offset, B_num_rows = block_data.matrix_dim.rows, B_num_cols = block_data.matrix_dim.cols, B_row_stride = block_data.matrix_dim.stride; // Cast from void; const Real *B_data = static_cast<Real*>(block_data.matrix_data); // we avoided a bunch of hassle by doing this (relates to Ansi-C requirement). for (int k = 0; k < B_num_cols; k++) { const Real *this_B_col = B_data + k; const Real *this_A_row = A_data + i * A_row_stride + B_row_start * A_col_stride; // this_A_row points to the element A[i][B_row_start], it's really just // part of this row of A. Real sum = 0.0; for (int l = 0; l < B_num_rows; l++) // l indexes rows of B. sum += this_B_col[l * B_row_stride] * this_A_row[l * A_col_stride]; int index = i * dim.stride + (k + B_col_start); data[index] = alpha * sum + beta * data[index]; } } // For a block matrix B, does B = alpha * C * D + beta * B. // the (x,y,z) indices are the block index, then the row // and column indices within the block. Note: transposition of C and D // is handled by swapping the (num_rows,num_cols) and (row_stride,col_stride), // so it's invisible to this code. The num-cols and num-rows of C and D // are only provided to the extent that they are not already determined // by other quantities. template<typename Real> __global__ static void _block_add_mat_mat(CuBlockMatrixData *B_cu_data, int num_blocks, const Real *C_data, int C_num_cols, int C_row_stride, int C_col_stride, const Real *D_data, int D_row_stride, int D_col_stride, Real alpha, Real beta) { int b = blockIdx.x * blockDim.x + threadIdx.x; // block-index into B. int i = blockIdx.y * blockDim.y + threadIdx.y; // row-index into b'th block int j = blockIdx.z * blockDim.z + threadIdx.z; // col-index into b'th block if (b >= num_blocks) return; const CuBlockMatrixData &block_data = B_cu_data[b]; if (i >= block_data.matrix_dim.rows || j >= block_data.matrix_dim.cols) return; // we're outside the dimensions of the b'th block. // B_elem is the element of B we're writing to. Real *B_elem = reinterpret_cast<Real*>(block_data.matrix_data) + i * block_data.matrix_dim.stride + j; Real B_val = *B_elem; // B_row and B_col are the (row, col) index into the full matrix B. int B_row = block_data.row_offset + i, B_col = block_data.col_offset + j; const Real *C_row_data = C_data + C_row_stride * B_row, *D_col_data = D_data + D_col_stride * B_col; Real sum = 0.0; for (int k = 0; k < C_num_cols; k++) { sum += C_row_data[k * C_col_stride] * D_col_data[k * D_row_stride]; } *B_elem = alpha * sum + beta * B_val; } template<typename Real> __global__ static void _blockadd_mat_blockmat_trans(Real *data, MatrixDim dim, const Real *A_data, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, Real alpha, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index into "data" int j = blockIdx.y * blockDim.y + threadIdx.y; // block-index into B. if (i >= A_num_rows || j >= B_num_blocks) return; const CuBlockMatrixData &cu_data = B_cu_data[j]; // BT means B transposed. int BT_row_start = cu_data.col_offset, BT_col_start = cu_data.row_offset, BT_num_rows = cu_data.matrix_dim.cols, BT_num_cols = cu_data.matrix_dim.rows, BT_col_stride = cu_data.matrix_dim.stride; // Cast from void; const Real *B_data = static_cast<Real*>(cu_data.matrix_data); // we avoided a bunch of hassle by doing this (relates to Ansi-C requirement). for (int k = 0; k < BT_num_cols; k++) { const Real *this_BT_col = B_data + k * BT_col_stride; const Real *this_A_row = A_data + i * A_row_stride + BT_row_start * A_col_stride; // this_A_row points to the element A[i][BT_row_start], it's really just // part of this row of A. Real sum = 0.0; for (int l = 0; l < BT_num_rows; l++) // l indexes rows of B. sum += this_BT_col[l] * this_A_row[l * A_col_stride]; int index = i * dim.stride + (k + BT_col_start); data[index] = alpha * sum + beta * data[index]; } } template<typename Real> __global__ static void _sum_column_ranges(Real *data, MatrixDim dim, const Real *src_data, MatrixDim src_dim, const Int32Pair *indices) { int col = blockIdx.x * blockDim.x + threadIdx.x; int row = blockIdx.y * blockDim.y + threadIdx.y; if (row >= dim.rows || col >= dim.cols) return; int dst_index = row * dim.stride + col, src_start_index = row * src_dim.stride + indices[col].first, src_end_index = row * src_dim.stride + indices[col].second; Real sum = 0.0; for (int index = src_start_index; index < src_end_index; index++) sum += src_data[index]; data[dst_index] = sum; } template<typename Real> __global__ static void _add_row_ranges(Real *data, MatrixDim dim, const Real *src_data, MatrixDim src_dim, const Int32Pair *indexes) { int col = blockIdx.x * blockDim.x + threadIdx.x; int row = blockIdx.y * blockDim.y + threadIdx.y; if (row >= dim.rows || col >= dim.cols) return; int dst_index = row * dim.stride + col; int src_index_start = indexes[row].first, src_index_end = indexes[row].second; for (int row_index = src_index_start; row_index < src_index_end; row_index++) data[dst_index] += src_data[row_index * src_dim.stride + col]; } template<typename Real> __global__ static void _soft_hinge(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; // compute the function y[index] = log(1 + exp(x[index])) if (i < d.cols && j < d.rows) { Real val = x[src_index], result; if (val >= 10.0) result = val; // function approaches y=x as x gets large else result = log1p(exp(val)); y[dst_index] = result; } } template<typename Real> __global__ static void _group_pnorm(Real *y, const Real *x, MatrixDim d, int src_stride, int group_size, Real power) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j < d.rows && i < d.cols) { int dst_index = i + j * d.stride; Real tmp = 0; int src_begin_index = i * group_size + j * src_stride; int src_end_index = src_begin_index + group_size; for (int src_index = src_begin_index; src_index < src_end_index; src_index++) { tmp += pow(std::abs(x[src_index]), power); } tmp = pow(tmp, Real(1.0 / power)); if (!isnan(tmp)) { y[dst_index] = tmp; } else { Real max_value = x[src_begin_index], min_value = max_value; for (int src_index = src_begin_index + 1; src_index < src_end_index; src_index++) { if (x[src_index] > max_value) max_value = x[src_index]; if (x[src_index] < min_value) min_value = x[src_index]; } tmp = 0.0; // let max_value be the largest abs(value) Real max_abs_value = (max_value > -min_value ? max_value : -min_value); if (max_abs_value == 0) { y[dst_index] = 0.0; } else { for (int src_index = src_begin_index; src_index < src_end_index; src_index++) { Real x_scaled = x[src_index] / max_abs_value; tmp += pow(std::abs(x_scaled), Real(power)); } y[dst_index] = pow(tmp, Real(1.0 / power)) * max_abs_value; } } } } /* * cu:: */ template<typename Real> __global__ static void _sigmoid(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real res = 1.0 / (1.0 + exp(-x[src_index])); y[dst_index] = res; } } template<typename Real> __global__ static void _diff_sigmoid(Real*eout, const Real*e, const Real*y, MatrixDim d, int e_stride, int y_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride; int e_index = i + j * e_stride; int y_index = i + j * y_stride; if (i < d.cols && j < d.rows) eout[dst_index] = y[y_index] * (1.0 - y[y_index]) * e[e_index]; } template<typename Real> __global__ static void _tanh(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real exp_2x = exp(2.0 * x[src_index]); Real res; if (isinf(exp_2x)) { res = 1.0; } else { res = (exp_2x - 1.0) / (exp_2x + 1.0); } y[dst_index] = res; } } template<typename Real> __global__ static void _diff_tanh(Real*eout, const Real*e, const Real*y, MatrixDim d, int e_stride, int y_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride; int e_index = i + j * e_stride; int y_index = i + j * y_stride; if (i < d.cols && j < d.rows) eout[dst_index] = (1.0 - y[y_index] * y[y_index]) * e[e_index]; } /* This function copies x to y while bounding the elements away from zero using the scalar function: y = x if x <= -epsilon or x >= +epsilon +epsilon if 0 <= x < epsilon -epsilon if -epsilon < x < 0. where: x is the source matrix, of dimension and stride given by d epsilon > 0 y is the destination matrix, with the num-rows and num-cols given by d, but stride given by y_stride. */ template<typename Real> __global__ static void _ensure_nonzero(const Real *x, MatrixDim d, Real epsilon, int y_stride, Real *y) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int x_index = i + j * d.stride, y_index = i + j * y_stride; if (i < d.cols && j < d.rows) { Real src = x[x_index], dst; if (src <= -epsilon || src >= epsilon) dst = src; else if (src >= 0) dst = epsilon; else dst = -epsilon; __syncthreads(); // This allows it to do consolidated write below, which // should improve speed. y[y_index] = dst; } } template<typename Real> __global__ static void _parametric_relu(Real* y, const Real* x, MatrixDim d, int src_stride, const Real* a, const Real* b) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real res = (x[src_index] > 0.0) ? a[i] * x[src_index] : b[i] * x[src_index]; y[dst_index] = res; } } template<typename Real> __global__ static void _diff_parametric_relu(Real* eout, const Real* e, const Real* y, MatrixDim d, int e_stride, int y_stride, const Real* a, const Real* b) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride; int e_index = i + j * e_stride; int y_index = i + j * y_stride; if (i < d.cols && j < d.rows ) eout[dst_index] = (y[y_index] > 0.0 ? a[i] * e[e_index] : b[i] * e[e_index]); } template<typename Real> __global__ static void _heaviside(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real res = (x[src_index] > 0.0 ? 1.0 : 0.0); y[dst_index] = res; } } /* template<typename Real> __global__ static void _softmax_reduce(Real*y, const Real*x, MatrixDim d, int src_stride, Real *logsum) { __shared__ Real smem[CU1DBLOCK]; const int i = blockIdx.x; const int x_start = i * src_stride; const int y_start = i * d.stride; const int tid = threadIdx.x; // find max element of the row // reduce to CU1DBLOCK elements per row. Real tmax = sizeof(Real) == sizeof(float) ? -CUDART_INF_F : -CUDART_INF; for (int j = tid; j < d.cols; j += CU1DBLOCK) { tmax = fmax(tmax, x[x_start + j]); } smem[tid] = tmax; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } } // broadcast max to all threads __syncthreads(); Real max = smem[0]; // sum_j(exp(x(i,j)-max)) // reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < d.cols; j += CU1DBLOCK) { tsum += exp(x[x_start + j] - max); } smem[tid] = tsum; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] += smem[tid + shift]; } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] += smem[tid + shift]; } } // broadcast sum to all threads __syncthreads(); Real inv_sum = Real(1) / smem[0]; // normalize the row for (int j = tid; j < d.cols; j += CU1DBLOCK) { y[y_start + j] = exp(x[x_start + j] - max) * inv_sum; } } */ template<typename Real> __global__ static void _softmax_reduce(Real*y, const Real*x, MatrixDim d, int src_stride, Real *logsum) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = x[threadIdx.x+j*src_stride]; for(int i=1; i<steps; ++i) { if(threadIdx.x+i*THREADS < d.cols && aux[threadIdx.x] < x[threadIdx.x+i*THREADS+j*src_stride]) aux[threadIdx.x] = x[threadIdx.x+i*THREADS+j*src_stride]; } //get the maximum value int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads && aux[threadIdx.x] < aux[threadIdx.x+halfPoint]) aux[threadIdx.x] = aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } Real max = aux[0]; __syncthreads(); // subtract max, apply exp, sum up... y[threadIdx.x+j*d.stride] = exp(x[threadIdx.x+j*src_stride] - max); aux[threadIdx.x] = y[threadIdx.x+j*d.stride]; for(int i=1; i<steps; i++) { if(threadIdx.x+i*THREADS < d.cols) { y[threadIdx.x+i*THREADS+j*d.stride] = exp(x[threadIdx.x+i*THREADS+j*src_stride] - max); aux[threadIdx.x] += y[threadIdx.x+i*THREADS+j*d.stride]; } } nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } Real sum = aux[0]; __syncthreads(); if (threadIdx.x == 0 && logsum != NULL) logsum[j] = log(sum)+max; // denominator log sum //normalize by sum... for(int i=0; i<steps; i++) { if(threadIdx.x+i*THREADS < d.cols) { y[threadIdx.x+i*THREADS+j*d.stride] = y[threadIdx.x+i*THREADS+j*d.stride] / sum; } } } template<typename Real> __global__ static void _log_softmax_reduce(Real *y, const Real *x, MatrixDim d, int src_stride) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; // Maximum step 1: loads input data to <aux>. If <d.cols> is larger than // <blockDim.x>, then we do a first pass filtering and only // keep a <blockDim.x> size array. aux[threadIdx.x] = x[threadIdx.x + j * src_stride]; for (int i = 1; i < steps; ++i) { if (threadIdx.x + i * THREADS < d.cols && aux[threadIdx.x] < x[threadIdx.x + i * THREADS + j * src_stride]) aux[threadIdx.x] = x[threadIdx.x + i * THREADS + j * src_stride]; } // Maximum step 2: the standard max reduce. int nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); if (threadIdx.x < halfPoint) { if (threadIdx.x + halfPoint < nTotalThreads && aux[threadIdx.x] < aux[threadIdx.x + halfPoint]) aux[threadIdx.x] = aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); } Real max = aux[0]; __syncthreads(); // Log sum step 1: substracts max, and takes exponentials. y[threadIdx.x + j * d.stride] = x[threadIdx.x + j * src_stride] - max; aux[threadIdx.x] = exp(y[threadIdx.x + j * d.stride]); for (int i = 1; i < steps; ++i) { if (threadIdx.x + i * THREADS < d.cols) { y[threadIdx.x + i * THREADS + j * d.stride] = x[threadIdx.x + i * THREADS + j * src_stride] - max; aux[threadIdx.x] += exp(y[threadIdx.x + i * THREADS + j * d.stride]); } } // Log sum step 2: comptes summation and then takes logarithm. nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); if (threadIdx.x < halfPoint) { if (threadIdx.x + halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); } Real log_sum = log(aux[0]); __syncthreads(); // Computes log softmax. for (int i = 0; i < steps; ++i) { if (threadIdx.x + i * THREADS < d.cols) { y[threadIdx.x + i * THREADS + j * d.stride] -= log_sum; } } } // The output y_i = scale * x_i, // and we want to RMS value of the y_i to equal target_rms, // so y^t y = D * target_rms^2 (if y is one row of the input). // we need to have scale = 1.0 / sqrt(x^t x / (D * target_rms^2)). // there is also flooring involved, to avoid division-by-zero // problems. It's important for the backprop, that the floor's // square root is exactly representable as float. // If add_log_stddev is true, log(max(epsi, sqrt(x^t x / D))) // is an extra dimension of the output. // // 1D grid is used. Each 256-thread block works on 1 row of the data matrix. // The block is also of 1D. Strided memory access is used if the length of the // row is longer than 256. template<typename Real> __global__ static void _normalize_per_row(Real *y, int y_stride, const Real *x, MatrixDim x_d, Real target_rms, bool add_log_stddev) { const int i = blockIdx.x; const int tid = threadIdx.x; const Real* x_row = x + i * x_d.stride; __shared__ Real ssum[CU1DBLOCK]; // Reduce x_j^2 to CU1DBLOCK elements per row Real tsum = Real(0); for (int j = tid; j < x_d.cols; j += CU1DBLOCK) { tsum += x_row[j] * x_row[j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) ssum[tid] += ssum[tid + shift]; __syncthreads(); } // Reduce last warp to 1 element per row. // Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } const Real kSquaredNormFloor = 1.3552527156068805425e-20; // 2^-66 if (tid == 0) { ssum[0] = sqrt( fmax(ssum[0] / (target_rms * target_rms * x_d.cols), kSquaredNormFloor)); } // Broadcast floored stddev to all threads. __syncthreads(); const Real stddev_div_target_rms = ssum[0]; const Real scale = Real(1) / stddev_div_target_rms; // Store normalized input to output Real* y_row = y + i * y_stride; for (int j = tid; j < x_d.cols; j += CU1DBLOCK) { y_row[j] = x_row[j] * scale; } if (tid == 0 && add_log_stddev) { y_row[x_d.cols] = log(stddev_div_target_rms * target_rms); } } template<typename Real> __global__ static void _diff_normalize_per_row(Real *id, int id_stride, const Real *iv, MatrixDim iv_dim, const Real* od, int od_stride, Real target_rms, bool add_log_stddev) { const Real kSquaredNormFloor = 1.3552527156068805425e-20; // 2^-66 const Real kInvNormFloor = 8589934592.0; const int tid = threadIdx.x; const int i = blockIdx.x; const Real* iv_row = iv + i * iv_dim.stride; const Real* od_row = od + i * od_stride; // reduce to CU1DBLOCK elements per row Real dot_products = Real(0); Real in_norm = Real(0); for (int j = tid; j < iv_dim.cols; j += CU1DBLOCK) { const Real iv_ij = iv_row[j]; dot_products += iv_ij * od_row[j]; in_norm += iv_ij * iv_ij; } __shared__ Real sprod[CU1DBLOCK]; __shared__ Real snorm[CU1DBLOCK]; sprod[tid] = dot_products; snorm[tid] = in_norm; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { sprod[tid] += sprod[tid + shift]; snorm[tid] += snorm[tid + shift]; } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { sprod[tid] += sprod[tid + shift]; snorm[tid] += snorm[tid + shift]; } } // broadcast the sum results __syncthreads(); dot_products = sprod[0]; in_norm = snorm[0]; Real log_stddev_deriv; if (add_log_stddev) { log_stddev_deriv = Real(1) / max(in_norm, iv_dim.cols * kSquaredNormFloor) * od_row[iv_dim.cols]; } const Real inv_d_scaled = Real(1) / (iv_dim.cols * target_rms * target_rms); in_norm = Real(1) / sqrt(max(in_norm * inv_d_scaled, kSquaredNormFloor)); const Real f = in_norm == kInvNormFloor ? Real(0) : in_norm; dot_products *= f * f * f * inv_d_scaled; for (int j = tid; j < iv_dim.cols; j += CU1DBLOCK) { const Real iv_ij = iv_row[j]; Real id_ij = id[i * id_stride + j]; if (add_log_stddev) { id_ij += log_stddev_deriv * iv_ij; } if (id != od) { id_ij += in_norm * od_row[j]; } else { id_ij *= in_norm; } id_ij -= dot_products * iv_ij; id[i * id_stride + j] = id_ij; } } /* // Per-row log-softmax operation on 'x', with writing to 'y'. // note, x and y may point to the same memory. This is equivalent to setting // matrix y to matrix x and then, for each row of y, subtracting the offset that // will make exp(y.row[j]) sum to 1 for each row j. // // It expects to be called with CU1DBLOCK threads. // The number of blocks [i.e. the gridDim] equals to y_dim.rows, // so one block of threads processes each row. x and y are // expected to have the same dimension, but possibly different row strides. template<typename Real> __global__ static void _log_softmax_reduce(Real* y, const Real* x, MatrixDim y_dim, int x_stride) { __shared__ Real smem[CU1DBLOCK]; const int i = blockIdx.x; const int x_start = i * x_stride; const int y_start = i * y_dim.stride; const int tid = threadIdx.x; // find max element of the row // reduce to CU1DBLOCK elements per row. Real tmax = -1e20; for (int j = tid; j < y_dim.cols; j += CU1DBLOCK) { tmax = fmax(tmax, x[x_start + j]); } smem[tid] = tmax; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } } // broadcast max to all threads __syncthreads(); Real max = smem[0]; // sum_j(exp(x(i,j)-max)) // reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < y_dim.cols; j += CU1DBLOCK) { tsum += exp(x[x_start + j] - max); } smem[tid] = tsum; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] += smem[tid + shift]; } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] += smem[tid + shift]; } } // broadcast sum to all threads __syncthreads(); Real log_sum = log(smem[0]); // normalize the row for (int j = tid; j < y_dim.cols; j += CU1DBLOCK) { y[y_start + j] = x[x_start + j] - max - log_sum; } } */ template<typename Real> __global__ static void _splice(Real* y, const Real* x, const int32_cuda* off, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d_out.stride; if (i < d_out.cols && j < d_out.rows) { int32_cuda src_col = i % d_in.cols; int32_cuda src_row = j + off[i / d_in.cols]; if (src_row < 0) src_row = 0; if (src_row >= d_in.rows) src_row = d_in.rows - 1; y[index] = x[src_col + src_row * d_in.stride]; } } template<typename Real> __global__ static void _take_mean(const Real* x, Real* y, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index1 = i + j * d_in.stride; int32_cuda index2 = j + i * d_in.stride; if (i <= j && j < d_in.rows) { int32_cuda index_sp = (j * (j + 1) / 2) + i; y[index_sp] = 0.5 * (x[index1] + x[index2]); } } template<typename Real> __global__ static void _take_lower(const Real* x, Real* y, MatrixDim d_in) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index int j = blockIdx.y * blockDim.y + threadIdx.y; // col-index if (j > i || i >= d_in.rows) return; int index = i * d_in.stride + j; Real val = x[index]; int index_sp = (i * (i + 1) / 2) + j; y[index_sp] = val; } template<typename Real> __global__ static void _take_upper(const Real* x, Real* y, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row-index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // col-index if (j < i || j >= d_in.rows) return; int32_cuda index = i * d_in.stride + j; int32_cuda index_sp = (j * (j + 1) / 2) + i; y[index_sp] = x[index]; } template<typename Real> __global__ static void _vec_copy_diag_from_packed(Real* y, const Real* x, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { y[i] = x[index]; } } template<typename Real> __global__ static void _copy_from_sp(const Real* x, Real* y, MatrixDim dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index int j = blockIdx.y * blockDim.y + threadIdx.y; // if (i < dim.cols && j < dim.rows) { int dst_index = i + j * dim.stride, src_index; if (j <= i) { // no transpose src_index = (i * (i + 1) / 2) + j; } else { // transpose. src_index = (j * (j + 1) / 2) + i; } y[dst_index] = x[src_index]; } } template<typename Real> __global__ static void _copy(Real* y, const Real* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d_out.stride; if (i < d_out.cols && j < d_out.rows) { int32_cuda src_col = copy_from[i]; if (src_col >= 0 && src_col < d_in.cols) { y[index] = x[src_col + j * d_in.stride]; } else { y[index] = 1.0 / 0.0; } } } template<typename Real> __global__ static void _one(Real* x, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { x[i] = 1.0; } } template<typename Real> __global__ static void _randomize(Real* y, const Real* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d_out.stride; if (i < d_out.cols && j < d_out.rows) { int32_cuda src_row = copy_from[j]; y[index] = x[i + src_row * d_in.stride]; } } template<typename Real> __global__ static void _regularize_l1(Real* wei, Real* grad, Real l1, Real lr, MatrixDim d, int stride_grad) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride, grad_index = i + j * stride_grad; if (i < d.cols && j < d.rows) { if (wei[index] == 0.0) return; //skip L1 if zero weight! Real l1_signed = l1; if (wei[index] < 0.0) //flip sign l1_signed = -l1; Real before = wei[index]; //simulate update Real after = wei[index] - lr * grad[grad_index] - l1_signed; if ((after > 0.0) ^ (before > 0.0)) { //sign changed? wei[index] = 0.0; grad[grad_index] = 0.0; } else { wei[index] -= l1_signed; } } } template<typename Real> __global__ static void _find_row_max_id(const Real* mat, Real* vec_val, int32_cuda* vec_id, MatrixDim d) { const int32_cuda i = blockIdx.x; const int32_cuda base = i * d.stride; const int32_cuda tid = threadIdx.x; __shared__ Real smax[CU1DBLOCK]; __shared__ int32_cuda sidx[CU1DBLOCK]; Real tmax = -1e20; int32_cuda tidx = -1; // Loop over blocks for coalesced memory access. for (int32_cuda j = tid; j < d.cols; j += CU1DBLOCK) { const Real val = mat[base + j]; if (val > tmax) { tmax = val; tidx = j; } } smax[tid] = tmax; sidx[tid] = tidx; // Parallel reduce #pragma unroll for (int32_cuda num_working_threads = CU1DBLOCK / 2; num_working_threads >= warpSize; num_working_threads >>= 1) { __syncthreads(); if (tid < num_working_threads) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; sidx[tid] = sidx[tid + num_working_threads]; } } } // Warp reduce without __syncthreads() // (note.: synchronizes implicitly within a warp at the multiprocessor) if (tid < warpSize / 2) { #pragma unroll for (int32_cuda num_working_threads = warpSize / 2; num_working_threads > 0; num_working_threads >>= 1) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; sidx[tid] = sidx[tid + num_working_threads]; } } } if (tid == 0) { if (vec_val) { vec_val[i] = smax[0]; } vec_id[i] = sidx[0]; } } template<typename Real> __global__ static void _find_row_abs_max(const Real* mat, Real* row_max, MatrixDim d) { const int32_cuda i = blockIdx.x; const int32_cuda base = i * d.stride; const int32_cuda tid = threadIdx.x; __shared__ Real smax[CU1DBLOCK]; Real tmax = -1e20; // Loop over blocks for coalesced memory access. for (int32_cuda j = tid; j < d.cols; j += CU1DBLOCK) { const Real val = fabs(mat[base + j]); if (val > tmax) { tmax = val; } } smax[tid] = tmax; // Parallel reduce #pragma unroll for (int32_cuda num_working_threads = CU1DBLOCK / 2; num_working_threads >= warpSize; num_working_threads >>= 1) { __syncthreads(); if (tid < num_working_threads) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; } } } // Warp reduce without __syncthreads() // (note.: synchronizes implicitly within a warp at the multiprocessor) if (tid < warpSize / 2) { #pragma unroll for (int32_cuda num_working_threads = warpSize / 2; num_working_threads > 0; num_working_threads >>= 1) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; } } } if (tid == 0) { row_max[i] = smax[0]; } } template<typename Real> __global__ static void _diff_xent(const int32_cuda* vec_tgt, Real* mat_net_out, Real* vec_log_post, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; if (i > 0) return; if (j < d.rows) { int32_cuda index = vec_tgt[j] + j * d.stride; Real value = mat_net_out[index]; if (value < 1e-20) value = 1e-20; vec_log_post[j] = log(value); mat_net_out[index] -= 1.0; } } template<typename Real> __global__ static void _diff_softmax(Real* x, const MatrixDim dim, const Real* value, const int value_stride, const Real* diff, const int diff_stride) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int value_start = i * value_stride; const int diff_start = i * diff_stride; const int x_start = i * dim.stride; // Loop along the matrix row. Reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < dim.cols; j += CU1DBLOCK) { tsum += value[value_start + j] * diff[diff_start + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { ssum[tid] += ssum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element. Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // Broadcast result to all threads __syncthreads(); const Real pe = ssum[0]; // Apply element-wise x = value * (diff - pe) for (int j = tid; j < dim.cols; j += CU1DBLOCK) { x[x_start + j] = value[value_start + j] * (diff[diff_start + j] - pe); } } // Differentiate backward through the log softmax function. // "out_value" is the log softmax output. Does, for each row i, // in_deriv(i) = out_deriv(i) - sum(out_deriv(i)) .* exp(out_value(i)) // ???(i) is row-vector. // CUDA thread layout: 1 thread block (CU1DBLOCK == 256 threads) per matrix-row. template<typename Real> __global__ static void _diff_log_softmax(const MatrixDim in_deriv_dim, const Real* out_value, const int out_value_stride, const Real* out_deriv, const int out_deriv_stride, Real* in_deriv) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int out_value_start = i * out_value_stride; const int out_deriv_start = i * out_deriv_stride; const int in_deriv_start = i * in_deriv_dim.stride; // Loop along the matrix row. Reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < in_deriv_dim.cols; j += CU1DBLOCK) { tsum += out_deriv[out_deriv_start + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { ssum[tid] += ssum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element. Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // Broadcast result to all threads __syncthreads(); const Real sum_e = ssum[0]; // Apply element-wise x = out_deriv - exp(value) * sum_e for (int j = tid; j < in_deriv_dim.cols; j += CU1DBLOCK) { in_deriv[in_deriv_start + j] = out_deriv[out_deriv_start + j] - exp(out_value[out_value_start + j]) * sum_e; } } /** this function computes the core part of the LSTM nonlinearity. @param [in] in A matrix, of dimension num_rows by 5*cell_dim (i.e. its num-cols must be a multiple of 5). The column-space is interpreted as 5 consecutive blocks, each of dimension cell_dim, which we name: (i_part, f_part, c_part, o_part, c_{t-1}). If 'have_dropout_mask' is nonzero, each row of 'in' will have 3 extra elements, interpreted as dropout masks/scales for i_t, f_t and o_t. @param [in] params A matrix, of dimension 3 by cell_dim, with rows containing the 3 diagonal parameter matrices used in LSTMs, namely w_{ic}, w_{fc} and w_{oc}. @param [out] out A matrix, of dimension num_rows by 2*cell_dim. The quantities c_t and m_t respectively are put there (in two blocks of column-dimension cell_dim), according to the following equations: i_t = Sigmoid(i_part + w_{ic}*c_{t-1}) f_t = Sigmoid(f_part + w_{fc}*c_{t-1}) c_t = f_t*c_{t-1} + i_t * Tanh(c_part) o_t = Sigmoid(o_part + w_{oc}*c_t) m_t = o_t * Tanh(c_t) We use 1D thread block with CU1DBLOCK threads. It works best when cell_dim is a multiple of CU1DBLOCK. We use 1d Grid. Each block is working on one row of the in and out matrices. */ template<typename Real> __global__ static void _lstm_nonlinearity(const Real* in, const int in_stride, const Real* params, const int params_stride, const int out_stride, const int cell_dim, const int have_dropout_mask, const int num_rows, Real* out) { const int tid = threadIdx.x; const int i = blockIdx.x; const Real* i_part = in + i * in_stride; const Real* f_part = in + i * in_stride + cell_dim; const Real* c_part = in + i * in_stride + cell_dim * 2; const Real* o_part = in + i * in_stride + cell_dim * 3; const Real* c_tm1 = in + i * in_stride + cell_dim * 4; const Real* w_ic = params; const Real* w_fc = params + params_stride; const Real* w_oc = params + params_stride * 2; Real* c_t = out + i * out_stride; Real* m_t = out + i * out_stride + cell_dim; Real i_scale = (have_dropout_mask ? in[i * in_stride + cell_dim * 5] : 1), f_scale = (have_dropout_mask ? in[i * in_stride + cell_dim * 5 + 1] : 1), o_scale = (have_dropout_mask ? in[i * in_stride + cell_dim * 5 + 2] : 1); for (int j = tid; j < cell_dim; j += CU1DBLOCK) { Real c_tm1_j = c_tm1[j]; Real i_t_j = Real(1) / (Real(1) + exp(-i_part[j] - w_ic[j] * c_tm1_j)); Real f_t_j = Real(1) / (Real(1) + exp(-f_part[j] - w_fc[j] * c_tm1_j)); Real c_t_j = f_t_j * f_scale * c_tm1_j + i_t_j * i_scale * tanh(c_part[j]); Real o_t_j = Real(1) / (Real(1) + exp(-o_part[j] - w_oc[j] * c_t_j)); c_t[j] = c_t_j; m_t[j] = o_t_j * o_scale * tanh(c_t_j); } } /** This function does the 'backward' pass corresponding to the function ComputeLstmNonlinearity. It's a little more complicated than you might expect because of the 'self-repair' mechanism that we use to prevent the sigmoid and tanh nonlinearities oversaturating, and because of the average-activation and average-derivative stats that we store for these nonlinearites (these stats are used both to control the self-repair mechanism, and for diagnostic purposes). Because the forward pass computes various intermediate values that are not output, this function actually has to do the same computations as the forward pass before it actually does the backprop. In the following description, `C` is for `cell_dim`, `N` is for `num_rows`. @param [in] input The same as in ComputeLstmNonlinearity(). A matrix, of dimension N by 5C (i.e. its num-cols must be a multiple of 5). The column-space is interpreted as 5 consecutive blocks, each of dimension C, which we name: (i_part, f_part, c_part, o_part, c_{t-1}). If 'have_dropout_mask' is nonzero, each row of 'in' will have 3 extra elements, interpreted as dropout masks/scales for i_t, f_t and o_t. @param [in] params The same as in ComputeLstmNonlinearity(). A matrix, of dimension 3 by C, with rows containing the three diagonal parameter matrices used in LSTMs, namely w_{ic}, w_{fc} and w_{oc}. @param [in] output_deriv A matrix, of dimension N by 2C, containing the derivative of the objective function we're backpropagating, w.r.t. the quantities c_t and m_t (in two blocks of column-dimension C). @param [in] deriv_sum_in This is used in the self-repair code to identify oversaturated nonlinearities. It is a matrix, of dimension 5 by C, corresponding to the totals of the derivatives of the 5 sigmoid and tanh nonlinearities, in they order they appear in the equations in the documentation of ComputeLstmNonlinearity() respectively, they appear in the equations for (i_t, f_t, c_t, o_t, m_t). This will be divided by 'count_in' to get the average derivative value so far, for each of the nonlinearities. @param [in] self_repair_config A vector of dimension 10, containing the configuration of the self-repair to be used for the 5 nonlinearities. The first 5 elements are the self_repair_lower_threshold values (typically 0.05 for sigmoid and 0.2 for tanh), and the next 5 elements are the corresponding self-repair-scales (typically 10^-5). @param [in] count_in The data-count that corresponds to the stats in 'deriv_sum_in' at entry to the function. This function should tolerate the count being zero (in that case, it is free to do the self-repair or not, as this should only happen on the 1st minibatch of each training job). @param [out] input_deriv May be NULL; if not, this function writes, to this location, the backpropagated derivative of the objective function w.r.t. the 'input' matrix. This matrix should have the same dimension as 'input' i.e. N by 5C. In addition to the regular backpropagated derivative, the output will include small values relating to 'self-repair'. @param [out] params_deriv May be NULL; if not, this is where this function *writes* [not adds] the backpropagated derivative of the objective function w.r.t. 'params'; it should have the same dimension as 'params' (3 by C). (This matrix will then be processed by the natural gradient code and added to the appropriate copy of the parameter matrix, outside this function). @param [out] value_sum_out Must be NULL if params_deriv is NULL; if not, a matrix of dimension 5 by C. This function *adds* to this location the total value of each of the sigmoid/tanh nonlinearities that it computes (this is for diagnostic purposes). @param [out] deriv_sum_out Must be NULL if params_deriv is NULL; if not, a matrix of dimension 5 by C; this function *adds* to this location the total of the derivative of each of the sigmoid/tanh nonlinearities that it computes (this is for diagnostic purposes and to control the self-repair). This function should tolerate the case when 'deriv_sum_out' points to the same data as 'deriv_sum_in'. @param [out] self_repair_sum_out Must be NULL if params_deriv is NULL; if not, a matrix of dimension 5 by C; this function *writes* to this location the sum of the number of times the self-repair code was activated (integer values 0 <= k <= N). This will be processed outside this function into self-repair stats for diagnostics. // Use 2D block (8x32 threads) as we need to compute column sum. // Use 1D grid to cover the data matrix `cell_dim`. */ template<typename Real> __global__ static void _diff_lstm_nonlinearity(const int cell_dim, const int have_dropout_mask, const int num_rows, const Real* input, const int input_stride, const Real* params, const int params_stride, const Real* output_deriv, const int output_deriv_stride, const double* deriv_sum_in, const int deriv_sum_in_stride, const Real* self_repair_config, double count, Real* input_deriv, const int input_deriv_stride, Real* params_deriv, const int params_deriv_stride, double* value_sum_out, const int value_sum_out_stride, double* deriv_sum_out, const int deriv_sum_out_stride, Real* self_repair_sum_out, const int self_repair_sum_out_stride) { __shared__ Real smem[CU1DBLOCK]; const int j = blockIdx.x * blockDim.x + threadIdx.x; const int tid = threadIdx.y * blockDim.x + threadIdx.x; const int grid_stride = gridDim.y * blockDim.y; const int i0 = blockIdx.y * blockDim.y + threadIdx.y; Real w_ic_deriv_sum = 0; Real w_fc_deriv_sum = 0; Real w_oc_deriv_sum = 0; Real i_t_value_sum = 0, i_t_deriv_sum = 0; Real f_t_value_sum = 0, f_t_deriv_sum = 0; Real c_part_value_sum = 0, c_part_deriv_sum = 0; Real o_t_value_sum = 0, o_t_deriv_sum = 0; Real c_t_value_sum = 0, c_t_deriv_sum = 0; bool update_sr[5]; if (j < cell_dim) { const Real w_ic = params[j]; const Real w_fc = params[params_stride + j]; const Real w_oc = params[2 * params_stride + j]; const Real* sr_config = self_repair_config; # pragma unroll for (int i = 0; i < 5; i++) { update_sr[i] = deriv_sum_in[i * deriv_sum_in_stride + j] < sr_config[i] * count; } const Real i_t_self_repair = (update_sr[0] ? sr_config[5] : 0); const Real f_t_self_repair = (update_sr[1] ? sr_config[6] : 0); const Real c_part_self_repair = (update_sr[2] ? sr_config[7] : 0); const Real o_t_self_repair = (update_sr[3] ? sr_config[8] : 0); const Real c_t_self_repair = (update_sr[4] ? sr_config[9] : 0); for (int i = i0; i < num_rows; i += grid_stride) { const Real i_part = input[i * input_stride + j]; const Real f_part = input[i * input_stride + j + cell_dim]; const Real c_part = input[i * input_stride + j + 2 * cell_dim]; const Real o_part = input[i * input_stride + j + 3 * cell_dim]; const Real c_prev = input[i * input_stride + j + 4 * cell_dim]; const Real i_scale = (have_dropout_mask ? input[i * input_stride + cell_dim * 5] : 1), f_scale = (have_dropout_mask ? input[i * input_stride + cell_dim * 5 + 1] :1), o_scale = (have_dropout_mask ? input[i * input_stride + cell_dim * 5 + 2] :1); const Real i_t = Real(1) / (1 + exp(-i_part - w_ic * c_prev)); const Real f_t = Real(1) / (1 + exp(-f_part - w_fc * c_prev)); const Real tanh_c_part = tanh(c_part); const Real c_t = f_t * f_scale * c_prev + i_t * i_scale * tanh_c_part; const Real o_t = 1 / (1 + exp(-o_part - w_oc * c_t)); const Real tanh_c_t = tanh(c_t); const Real i_t_deriv = i_t * (1 - i_t); const Real f_t_deriv = f_t * (1 - f_t); const Real c_part_deriv = 1 - tanh_c_part * tanh_c_part; const Real o_t_deriv = o_t * (1 - o_t); const Real c_t_deriv = 1 - tanh_c_t * tanh_c_t; if (params_deriv) { i_t_value_sum += i_t; f_t_value_sum += f_t; c_part_value_sum += tanh_c_part; o_t_value_sum += o_t; c_t_value_sum += tanh_c_t; i_t_deriv_sum += i_t_deriv; f_t_deriv_sum += f_t_deriv; c_part_deriv_sum += c_part_deriv; o_t_deriv_sum += o_t_deriv; c_t_deriv_sum += c_t_deriv; } const Real dc_t_out = output_deriv[i * output_deriv_stride + j]; const Real dm_t = output_deriv[i * output_deriv_stride + j + cell_dim]; const Real dtanh_c_t = o_t * o_scale * dm_t; const Real do_t = o_scale * tanh_c_t * dm_t; const Real do_t_input = (o_t_deriv * do_t - (2 * o_t - 1) * o_t_self_repair); const Real dc_t = (c_t_deriv * dtanh_c_t + dc_t_out + do_t_input * w_oc) - tanh_c_t * c_t_self_repair; const Real dtanh_c_part = i_t * i_scale * dc_t; const Real df_t = dc_t * f_scale * c_prev; const Real df_t_input = (df_t * f_t_deriv - (2 * f_t - 1) * f_t_self_repair); const Real di_t = dc_t * i_scale * tanh_c_part; const Real di_t_input = (di_t * i_t_deriv - (2 * i_t - 1) * i_t_self_repair); if (params_deriv) { w_ic_deriv_sum += c_prev * di_t_input; w_fc_deriv_sum += c_prev * df_t_input; w_oc_deriv_sum += c_t * do_t_input; } const Real dc_prev = w_ic * di_t_input + w_fc * df_t_input + f_t * f_scale * dc_t; const Real do_part = do_t_input; const Real dc_part = (c_part_deriv * dtanh_c_part - tanh_c_part * c_part_self_repair); const Real df_part = df_t_input; const Real di_part = di_t_input; if (input_deriv) { input_deriv[i * input_deriv_stride + j] = di_part; input_deriv[i * input_deriv_stride + j + cell_dim] = df_part; input_deriv[i * input_deriv_stride + j + cell_dim * 2] = dc_part; input_deriv[i * input_deriv_stride + j + cell_dim * 3] = do_part; input_deriv[i * input_deriv_stride + j + cell_dim * 4] = dc_prev; } } } if (params_deriv) { // compute params_deriv smem[tid] = w_ic_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { params_deriv[j] = smem[tid]; } __syncthreads(); smem[tid] = w_fc_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { params_deriv[params_deriv_stride + j] = smem[tid]; } __syncthreads(); smem[tid] = w_oc_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { params_deriv[2 * params_deriv_stride + j] = smem[tid]; } // compute value_sum_out __syncthreads(); smem[tid] = i_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[j] += smem[tid]; } __syncthreads(); smem[tid] = f_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[value_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_part_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[2 * value_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = o_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[3 * value_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[4 * value_sum_out_stride + j] += smem[tid]; } // need to update self_repair_sum_out before deriv_sum_out, because // deriv_sum_out and deriv_sum_in might point to the same memory. if (i0 < 5 && j < cell_dim) { self_repair_sum_out[i0 * self_repair_sum_out_stride + j] = update_sr[i0] ? num_rows : 0; } // compute derive_sum_out __syncthreads(); smem[tid] = i_t_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[j] += smem[tid]; } __syncthreads(); smem[tid] = f_t_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[deriv_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_part_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[2 * deriv_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = o_t_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[3 * deriv_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_t_deriv_sum; __syncthreads(); # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[4 * deriv_sum_out_stride + j] += smem[tid]; } } } __global__ static void _cuda_compress_uint8_sign(const BaseFloat *src, MatrixDim dim, unsigned char *dest, int dest_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dest_index = i + j * dest_stride, src_index = i + j * dim.stride; if (i < dim.cols && j < dim.rows) { BaseFloat f = src[src_index]; dest[dest_index] = (f > 0.0 ? (unsigned char)1 : (unsigned char)0); } } // The following inline templated functions are a workaround for the // fact that (I believe) std::numeric_limits is not available in CUDA; // they allow us to access the minimum and maximum elements of certain // types from templated code. template <typename I> __device__ static inline int minimum_integer_value(); template <typename I> __device__ static inline int maximum_integer_value(); template<> __device__ int maximum_integer_value<int8_t>() { return 127; } template<> __device__ int minimum_integer_value<int8_t>() { return -128; } template<> __device__ int maximum_integer_value<uint8_t>() { return 255; } template<> __device__ int minimum_integer_value<uint8_t>() { return 0; } template<> __device__ int maximum_integer_value<int16_t>() { return 32767; } template<> __device__ int minimum_integer_value<int16_t>() { return -32768; } template<> __device__ int maximum_integer_value<uint16_t>() { return 65535; } template<> __device__ int minimum_integer_value<uint16_t>() { return 0; } template <typename I> __global__ static void _cuda_compress_bounds_check(const BaseFloat *src, MatrixDim dim, I *dest, int dest_stride, float inv_scale) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dest_index = i + j * dest_stride, src_index = i + j * dim.stride; const int min_value = minimum_integer_value<I>(), max_value = maximum_integer_value<I>(); int compressed_value; int ok = (i < dim.cols && j < dim.rows); if (ok) { float f = src[src_index]; // note: I'm not sure what __float2int_rn does if input is outside of // integer range, but it doesn't matter much as in the situations where this // type of compression would make sense, the input should be well inside the // range of 'int', and if it fails, we've probably already catastrophically // diverged. int i = __float2int_rn(f * inv_scale); if (i < min_value) compressed_value = min_value; else if (i > max_value) compressed_value = max_value; else compressed_value = i; } __syncthreads(); if (ok) { dest[dest_index] = compressed_value; } } template <typename I> __global__ static void _cuda_compress_no_bounds_check(const BaseFloat *src, MatrixDim dim, I *dest, int dest_stride, float inv_scale) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dest_index = i + j * dest_stride, src_index = i + j * dim.stride; if (i < dim.cols && j < dim.rows) { float f = src[src_index]; int i = __float2int_rn(f * inv_scale); I s = i; dest[dest_index] = s; } } template <typename I> __global__ static void _cuda_uncompress(BaseFloat *dest, MatrixDim dim, const I *src, int src_stride, float scale) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int src_index = i + j * src_stride, dest_index = i + j * dim.stride; if (i < dim.cols && j < dim.rows) { I s = src[src_index]; dest[dest_index] = float(s * scale); } } __global__ static void _noop_kernel() { } /*********************************************************************** * ANSI-C wrappers of CUDA kernels */ /* * "int32" */ void cuda_int32_set_const(dim3 Gr, dim3 Bl, int32_cuda* mat, int32_cuda value, MatrixDim d) { hipLaunchKernelGGL(( _set_const), dim3(Gr),dim3(Bl), 0, 0, mat,value,d); } void cuda_int32_add(dim3 Gr, dim3 Bl, int32_cuda* mat, int32_cuda value, MatrixDim d) { hipLaunchKernelGGL(( _add), dim3(Gr),dim3(Bl), 0, 0, mat,value,d); } void cuda_int32_sequence(dim3 Gr, dim3 Bl, int32_cuda* data, int length, int32_cuda base) { hipLaunchKernelGGL(( _sequence), dim3(Gr), dim3(Bl), 0, 0, data, length, base); } /* * "float" */ /* * CuMatrix */ void cudaF_copy_upp_low(dim3 Gr, dim3 Bl, float* A, MatrixDim dimA) { hipLaunchKernelGGL(( _copy_upp_low), dim3(Gr),dim3(Bl), 0, 0, A,dimA);} void cudaF_copy_low_upp(dim3 Gr, dim3 Bl, float* A, MatrixDim dimA) { hipLaunchKernelGGL(( _copy_low_upp), dim3(Gr),dim3(Bl), 0, 0, A,dimA);} void cudaF_add_diag_vec_mat(dim3 Gr, dim3 Bl, float alpha, float *mat, MatrixDim mat_dim, const float *vec, const float *mat2, int mat2_row_stride, int mat2_col_stride, float beta) { hipLaunchKernelGGL(( _add_diag_vec_mat), dim3(Gr),dim3(Bl), 0, 0, alpha, mat, mat_dim, vec, mat2, mat2_row_stride, mat2_col_stride, beta); } void cudaF_copy_from_tp_trans(dim3 Gr, dim3 Bl, float* A, const float* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp_trans), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaFD_copy_from_tp_trans(dim3 Gr, dim3 Bl, float* A, const double* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp_trans), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaF_copy_from_tp(dim3 Gr, dim3 Bl, float* A, const float* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaFD_copy_from_tp(dim3 Gr, dim3 Bl, float* A, const double* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaF_apply_exp(dim3 Gr, dim3 Bl, float* mat, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _apply_exp), dim3(Gr),dim3(Bl),0,s, mat,d); } void cudaF_apply_exp_limited(dim3 Gr, dim3 Bl, float* mat, MatrixDim d, float lower_limit, float upper_limit) { hipLaunchKernelGGL(( _apply_exp_limited), dim3(Gr),dim3(Bl), 0, 0, mat, d, lower_limit, upper_limit); } void cudaF_apply_pow(dim3 Gr, dim3 Bl, float* mat, float power, MatrixDim d) { hipLaunchKernelGGL(( _apply_pow), dim3(Gr),dim3(Bl), 0, 0, mat, power, d); } void cudaF_apply_pow_abs(dim3 Gr, dim3 Bl, float* mat, float power, bool include_sign, MatrixDim d) { hipLaunchKernelGGL(( _apply_pow_abs), dim3(Gr),dim3(Bl), 0, 0, mat, power, include_sign, d); } void cudaF_apply_heaviside(dim3 Gr, dim3 Bl, float* mat, MatrixDim d) { hipLaunchKernelGGL(( _apply_heaviside), dim3(Gr),dim3(Bl), 0, 0, mat, d); } void cudaF_copy_cols(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _copy_cols), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaF_add_cols(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _add_cols), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaF_copy_rows(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _copy_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaF_copy_rows_direct(dim3 Gr, dim3 Bl, float* dst, const float* const * src, MatrixDim dst_dim) { hipLaunchKernelGGL(( _copy_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, dst_dim); } void cudaF_copy_to_rows_direct(dim3 Gr, dim3 Bl, float* const * dst, const float* src, MatrixDim src_dim) { hipLaunchKernelGGL(( _copy_to_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, src_dim); } void cudaF_add_rows(dim3 Gr, dim3 Bl, float alpha, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _add_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, reorder, dst_dim, src_stride); } void cudaF_mul_rows(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _mul_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaF_div_rows(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _div_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaF_add_rows_direct(dim3 Gr, dim3 Bl, float alpha, float* dst, const float* const * src, MatrixDim dst_dim) { hipLaunchKernelGGL(( _add_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, dst_dim); } void cudaF_add_to_rows(dim3 Gr, dim3 Bl, float alpha, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim src_dim, int dst_stride) { hipLaunchKernelGGL(( _add_to_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, reorder, src_dim, dst_stride); } void cudaF_add_to_rows_direct(dim3 Gr, dim3 Bl, float alpha, float* const * dst, const float* src, MatrixDim src_dim) { hipLaunchKernelGGL(( _add_to_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, src_dim); } void cudaF_apply_floor(dim3 Gr, dim3 Bl, float* mat, float floor_val, MatrixDim d) { hipLaunchKernelGGL(( _apply_floor), dim3(Gr),dim3(Bl), 0, 0, mat, floor_val, d); } void cudaF_apply_ceiling(dim3 Gr, dim3 Bl, float* mat, float ceiling_val, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _apply_ceiling), dim3(Gr),dim3(Bl),0,s, mat, ceiling_val, d); } void cudaF_set_diag(int Gr, int Bl, float* mat, float value, MatrixDim d) { hipLaunchKernelGGL(( _set_diag), dim3(Gr),dim3(Bl), 0, 0, mat,value,d); } void cudaF_set_diag_packed(int Gr, int Bl, float* mat, float value, int dim) { hipLaunchKernelGGL(( _set_diag_packed), dim3(Gr),dim3(Bl), 0, 0, mat,value,dim); } void cudaF_add_diag_packed(int Gr, int Bl, float* mat, float value, int dim) { hipLaunchKernelGGL(( _add_diag_packed), dim3(Gr),dim3(Bl), 0, 0, mat,value,dim); } void cudaF_set_const(dim3 Gr, dim3 Bl, float* mat, float value, MatrixDim d) { hipLaunchKernelGGL(( _set_const), dim3(Gr),dim3(Bl), 0, 0, mat,value,d); } void cudaF_set_zero_above_diag(dim3 Gr, dim3 Bl, float* mat, MatrixDim d) { hipLaunchKernelGGL(( _set_zero_above_diag), dim3(Gr),dim3(Bl), 0, 0, mat, d); } void cudaF_add(dim3 Gr, dim3 Bl, float* mat, float value, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _add), dim3(Gr),dim3(Bl),0,s, mat,value,d); } void cudaF_scale_diag_packed(int Gr, int Bl, float* mat, float value, int dim) { hipLaunchKernelGGL(( _scale_diag_packed), dim3(Gr),dim3(Bl), 0, 0, mat,value,dim); } void cudaF_scale(dim3 Gr, dim3 Bl, float* mat, float value, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _scale), dim3(Gr),dim3(Bl),0,s, mat,value,d); } void cudaF_apply_log(dim3 Gr, dim3 Bl, float* mat, MatrixDim d) { hipLaunchKernelGGL(( _apply_log), dim3(Gr),dim3(Bl), 0, 0, mat,d); } void cudaF_apply_fixed(dim3 Gr, dim3 Bl, float* mat, float resolution, int mode, MatrixDim d){ hipLaunchKernelGGL(( _apply_fixed), dim3(Gr),dim3(Bl), 0, 0, mat, resolution, mode, d); } void cudaF_mul_elements(dim3 Gr, dim3 Bl, float* mat, const float* A, MatrixDim dst_d, int src_stride) { hipLaunchKernelGGL(( _mul_elements), dim3(Gr),dim3(Bl), 0, 0, mat,A,dst_d,src_stride); } void cudaF_div_elements(dim3 Gr, dim3 Bl, float* mat, const float* A, MatrixDim dst_d, int src_stride) { hipLaunchKernelGGL(( _div_elements), dim3(Gr),dim3(Bl), 0, 0, mat,A,dst_d,src_stride); } void cudaF_max(dim3 Gr, dim3 Bl, float* mat, const float* A, MatrixDim dst_d, int src_stride) { hipLaunchKernelGGL(( _max), dim3(Gr),dim3(Bl), 0, 0, mat,A,dst_d,src_stride); } void cudaF_min(dim3 Gr, dim3 Bl, float* mat, const float* other, MatrixDim mat_d, int other_stride) { hipLaunchKernelGGL(( _min), dim3(Gr),dim3(Bl), 0, 0, mat,other,mat_d,other_stride); } void cudaF_mul_cols_vec(dim3 Gr, dim3 Bl, float* mat, const float* scale, MatrixDim d) { hipLaunchKernelGGL(( _mul_cols_vec), dim3(Gr),dim3(Bl), 0, 0, mat,scale,d); } void cudaF_mul_rows_vec(dim3 Gr, dim3 Bl, float* mat, const float* scale, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _mul_rows_vec), dim3(Gr),dim3(Bl),0,s, mat,scale,d); } void cudaF_mul_rows_group_mat(dim3 Gr, dim3 Bl, float *y, const float *x, MatrixDim d, int src_stride, int group_size) { hipLaunchKernelGGL(( _mul_rows_group_mat), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, group_size); } void cudaF_diff_group_pnorm(dim3 Gr, dim3 Bl, float *id, const float *iv, const float *ov, const float* od, MatrixDim id_dim, int iv_stride, int ov_stride, int od_stride, int group_size, float power) { hipLaunchKernelGGL(( _diff_group_pnorm), dim3(Gr), dim3(Bl), 0, 0, id, iv, ov, od, id_dim, iv_stride, ov_stride, od_stride, group_size, power); } void cudaF_calc_group_max_deriv(dim3 Gr, dim3 Bl, float *y, const float *x1, const float *x2, MatrixDim y_dim, int x1_stride, int x2_stride, int group_size) { hipLaunchKernelGGL(( _calc_group_max_deriv), dim3(Gr),dim3(Bl), 0, 0, y, x1, x2, y_dim, x1_stride, x2_stride, group_size); } void cudaF_div_rows_vec(dim3 Gr, dim3 Bl, float* mat, const float* vec_div, MatrixDim d) { hipLaunchKernelGGL(( _div_rows_vec), dim3(Gr),dim3(Bl), 0, 0, mat, vec_div, d); } void cudaF_add_mat(dim3 Gr, dim3 Bl, float alpha, const float* src, float* dst, MatrixDim d, int src_stride, int A_trans, hipStream_t s) { if (A_trans) { hipLaunchKernelGGL(( _add_mat_trans), dim3(Gr),dim3(Bl),0,s, alpha,src,dst,d,src_stride); } else { hipLaunchKernelGGL(( _add_mat), dim3(Gr),dim3(Bl),0,s, alpha,src,dst,d,src_stride); } } void cudaF_add_mat_blocks(dim3 Gr, dim3 Bl, float alpha, const float* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, float* dst, MatrixDim d, int src_stride, int A_trans) { if (A_trans) { hipLaunchKernelGGL(( _add_mat_blocks_trans), dim3(Gr),dim3(Bl), 0, 0, alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } else { hipLaunchKernelGGL(( _add_mat_blocks), dim3(Gr),dim3(Bl), 0, 0, alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } } void cudaF_add_mat_repeated(dim3 Gr, dim3 Bl, float alpha, const float* src, MatrixDim src_dim, float *dst, MatrixDim dst_dim) { hipLaunchKernelGGL(( _add_mat_repeated), dim3(Gr),dim3(Bl), 0, 0, alpha, src, src_dim, dst, dst_dim); } void cudaF_set_mat_mat_div_mat(dim3 Gr, dim3 Bl, const float *A, const float *B, const float *C, float *dst, MatrixDim d, int stride_a, int stride_b, int stride_c) { hipLaunchKernelGGL(( _set_mat_mat_div_mat), dim3(Gr),dim3(Bl), 0, 0, A,B,C,dst,d, stride_a, stride_b, stride_c); } void cudaF_sy_add_tr2(dim3 Gr, dim3 Bl, float alpha, float beta, const float* T, MatrixDim tdim, float *S, MatrixDim sdim) { hipLaunchKernelGGL(( _sy_add_tr2), dim3(Gr),dim3(Bl), 0, 0, alpha, beta, T, tdim, S, sdim); } void cudaF_add_vec_to_cols(dim3 Gr, dim3 Bl, float alpha, const float* col, float beta, float* dst, MatrixDim d) { hipLaunchKernelGGL(( _add_vec_to_cols), dim3(Gr),dim3(Bl), 0, 0, alpha,col,beta,dst,d); } void cudaF_add_vec_to_rows(dim3 Gr, dim3 Bl, float alpha, const float* row, float beta, float* dst, MatrixDim d) { hipLaunchKernelGGL(( _add_vec_to_rows), dim3(Gr),dim3(Bl), 0, 0, alpha,row,beta,dst,d); } void cudaF_add_mat_diag_vec(dim3 Gr, dim3 Bl, float alpha, float *mat, MatrixDim mat_dim, const float *mat2, int mat2_row_stride, int mat2_col_stride, const float *vec, float beta) { hipLaunchKernelGGL(( _add_mat_diag_vec), dim3(Gr),dim3(Bl), 0, 0, alpha, mat, mat_dim, mat2, mat2_row_stride, mat2_col_stride, vec, beta); } void cudaF_add_mat_mat_elements(dim3 Gr, dim3 Bl, float *data, const float *srcA_data, const float *srcB_data, MatrixDim dim, int srcA_stride, int srcB_stride, float alpha, float beta) { hipLaunchKernelGGL(( _add_mat_mat_elements), dim3(Gr), dim3(Bl), 0, 0, data, srcA_data, srcB_data, dim, srcA_stride, srcB_stride, alpha, beta); } // CURRENTLY UNUSED... void cudaF_apply_mask(dim3 Gr, dim3 Bl, float* mat, const char* mask, MatrixDim dmat, MatrixDim dmask) { hipLaunchKernelGGL(( _apply_mask), dim3(Gr),dim3(Bl), 0, 0, mat,mask,dmat,dmask); } /* * CuVector */ void cudaF_max_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<MAX,float>()); } void cudaF_abs_max_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<ABSMAX,float>()); } void cudaF_min_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<MIN,float>()); } void cudaF_sum_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<SUM,float>()); } void cudaF_add_row_sum_mat(float* result, const float* mat, void* scratch, const MatrixDim d, const float alpha, const float beta) { //_strided_reduction_fused(result, mat, scratch, d, alpha, beta); } void cudaF_add_col_sum_mat(int Gr, int Bl, float* result, const float* mat, const MatrixDim d, const float alpha, const float beta, hipStream_t s) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr), dim3(Bl),0,s, result, mat, d, TransReduceOp<SUMAB, float>(alpha, beta)); } void cudaF_replace_value(int Gr, int Bl, float *v, int dim, float orig, float changed) { hipLaunchKernelGGL(( _replace_value), dim3(Gr),dim3(Bl), 0, 0, v, dim, orig, changed); } void cudaF_set_bias_params(int Gr, int Bl, float* v, const float* a, float param_1, float param_2, float param_3, int* flag, int dim) { hipLaunchKernelGGL(( _set_bias_params), dim3(Gr),dim3(Bl), 0, 0, v,a,param_1,param_2,param_3,flag,dim); } void cublas_copy_kaldi_fd(int Gr, int Bl, int n, const float* x, int incx, double* y, int incy) { hipLaunchKernelGGL(( _cublas_copy_kaldi), dim3(Gr),dim3(Bl), 0, 0, n, x, incx, y, incy); } void cublas_copy_kaldi_df(int Gr, int Bl, int n, const double* x, int incx, float* y, int incy) { hipLaunchKernelGGL(( _cublas_copy_kaldi), dim3(Gr),dim3(Bl), 0, 0, n, x, incx, y, incy); } void cudaF_vec_mul_elements(int Gr, int Bl, float* v, const float* a, int dim, hipStream_t s) { hipLaunchKernelGGL(( _vec_mul_elements), dim3(Gr),dim3(Bl),0,s, v, a, dim); } void cudaF_vec_min(int Gr, int Bl, const float* v, float* value, int dim, int inc) { hipLaunchKernelGGL(( _vec_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, v, value, dim, inc, TransReduceOp<MIN, float>()); } void cudaF_vec_max(int Gr, int Bl, const float* v, float* value, int dim, int inc) { hipLaunchKernelGGL(( _vec_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, v, value, dim, inc, TransReduceOp<MAX, float>()); } void cudaF_trace_mat_mat_trans(dim3 Gr, dim3 Bl, const float* A, const float* B, MatrixDim dA, int B_stride, float* value) { hipLaunchKernelGGL(( _trace_mat_mat_trans), dim3(Gr),dim3(Bl), 0, 0, A,B,dA,B_stride,value); } void cudaF_trace_mat_mat(dim3 Gr, dim3 Bl, const float* A, const float* B, MatrixDim dA, int B_stride, float* value) { hipLaunchKernelGGL(( _trace_mat_mat<32>) , dim3(Gr),dim3(Bl), 0, 0, A,B,dA,B_stride,value); } void cudaF_add_diag_mat_mat_MNT(int Gr, int Bl, const float alpha, const float* M, const MatrixDim dim_M, const float* N, const int stride_N, const float beta, float* v) { hipLaunchKernelGGL(( _add_diag_mat_mat_MNT), dim3(Gr),dim3(Bl), 0, 0, alpha,M,dim_M,N,stride_N,beta,v); } void cudaF_add_diag_mat_mat_MTN(dim3 Gr, dim3 Bl, const float alpha, const float* M, const int stride_M, const float* N, const MatrixDim dim_N, const float beta, float* v, const int stride_v) { if (Bl.x == 16) { hipLaunchKernelGGL(( _add_diag_mat_mat_MTN<16>) , dim3(Gr), dim3(Bl), 0, 0, alpha, M, stride_M, N, dim_N, beta, v, stride_v); } else if (Bl.x == 32) { hipLaunchKernelGGL(( _add_diag_mat_mat_MTN<32>) , dim3(Gr), dim3(Bl), 0, 0, alpha, M, stride_M, N, dim_N, beta, v, stride_v); } } void cudaF_add_diag_mat_mat_MN(dim3 Gr, dim3 Bl, const float alpha, const float* M, const int stride_M, const float* N, const MatrixDim dim_N, const float beta, float* v) { if (Bl.x == 16) { hipLaunchKernelGGL(( _add_diag_mat_mat_MN<16>) , dim3(Gr),dim3(Bl), 0, 0, alpha,M,stride_M,N,dim_N,beta,v); } else if (Bl.x==32) { hipLaunchKernelGGL(( _add_diag_mat_mat_MN<32>), dim3(Gr),dim3(Bl), 0, 0, alpha,M,stride_M,N,dim_N,beta,v); } } void cudaF_add_vec_vec(int Gr, int Bl, float alpha, float* v, const float* x, const float* y, float beta, int dim) { hipLaunchKernelGGL(( _add_vec_vec), dim3(Gr),dim3(Bl), 0, 0, alpha,v,x,y,beta,dim); } void cudaF_vec_sum(int Gr, int Bl, float* v, float* value, int dim, int inc) { hipLaunchKernelGGL(( _vec_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, v, value, dim, inc, TransReduceOp<SUM, float>()); } void cudaF_matrix_add_elements(dim3 Gr, dim3 Bl, float *data, MatrixDim dim, float alpha, MatrixElement<float>* x, int num_elements) { hipLaunchKernelGGL(( _cuda_matrix_add_elements), dim3(Gr), dim3(Bl), 0, 0, data, dim, alpha, x, num_elements); } void cudaF_matrix_add_indexed_values(dim3 Gr, dim3 Bl, MatrixDim dim, float alpha, const Int32Pair* indices, const float* x, int s, float* data) { hipLaunchKernelGGL(( _cuda_matrix_add_indexed_values), dim3(Gr), dim3(Bl), 0, 0, dim, alpha, indices, x, s, data); } void cudaF_matrix_add_to_elements(dim3 Gr, dim3 Bl, float alpha, float* mat, MatrixDim dim, const MatrixIndexT_cuda* elements) { hipLaunchKernelGGL(( _cuda_matrix_add_to_elements), dim3(Gr), dim3(Bl), 0, 0, alpha, mat, dim, elements); } void cudaF_vector_copy_elements(dim3 Gr, dim3 Bl, float *data, int dim, const float *src_mat, int mat_stride, bool transpose, const MatrixIndexT_cuda* elements) { hipLaunchKernelGGL(( _cuda_vector_copy_elements), dim3(Gr), dim3(Bl), 0, 0, data, dim, src_mat, mat_stride, transpose, elements); } void cudaF_comp_obj_deriv(dim3 Gr, dim3 Bl, MatrixElement<float>* x, int s, const float* z, MatrixDim d, float* z2, MatrixDim d2, float* t) { hipLaunchKernelGGL(( _cuda_comp_obj_deriv), dim3(Gr),dim3(Bl), 0, 0, x,s,z,d,z2,d2,t); } void cudaD_comp_obj_deriv(dim3 Gr, dim3 Bl, MatrixElement<double>* x, int s, const double* z, MatrixDim d, double* z2, MatrixDim d2, double* t) { hipLaunchKernelGGL(( _cuda_comp_obj_deriv), dim3(Gr),dim3(Bl), 0, 0, x,s,z,d,z2,d2,t); } void cudaF_vec_copy_diag_from_packed(int Gr, int Bl, float *dst, const float *src, int dim) { hipLaunchKernelGGL(( _vec_copy_diag_from_packed), dim3(Gr),dim3(Bl), 0, 0, dst,src,dim); } void cudaF_vec_apply_floor(int Gr, int Bl, float* v, float floor_val, float *count, int dim) { hipLaunchKernelGGL(( _vec_apply_floor), dim3(Gr),dim3(Bl), 0, 0, v,floor_val,count,dim); } void cudaF_vec_apply_ceiling(int Gr, int Bl, float* v, float ceiling_val, float *count, int dim) { hipLaunchKernelGGL(( _vec_apply_ceiling), dim3(Gr),dim3(Bl), 0, 0, v, ceiling_val,count,dim); } void cudaF_vec_apply_exp(int Gr, int Bl, float* v, int dim) { hipLaunchKernelGGL(( _vec_apply_exp), dim3(Gr),dim3(Bl), 0, 0, v,dim); } void cudaF_vec_apply_log(int Gr, int Bl, float* v, float* flag, int dim) { hipLaunchKernelGGL(( _vec_apply_log), dim3(Gr),dim3(Bl), 0, 0, v,flag,dim); } void cudaF_vec_apply_fixed(int Gr, int Bl, float* v, float resolution, int mode, int dim){ hipLaunchKernelGGL(( _vec_apply_fixed), dim3(Gr),dim3(Bl), 0, 0, v, resolution, mode, dim); } void cudaF_invert_elements(dim3 Gr, dim3 Bl, float* data, MatrixDim d) { hipLaunchKernelGGL(( _invert_elements), dim3(Gr),dim3(Bl), 0, 0, data, d); } void cudaF_add_mat_blockmat(dim3 Gr, dim3 Bl, float *data, MatrixDim d, const float *Adata, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, float alpha, float beta, int B_trans) { if (B_trans) { hipLaunchKernelGGL(( _add_mat_blockmat_trans), dim3(Gr),dim3(Bl), 0, 0, data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } else { hipLaunchKernelGGL(( _add_mat_blockmat), dim3(Gr),dim3(Bl), 0, 0, data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } } void cudaF_block_add_mat_mat(dim3 Gr, dim3 Bl, CuBlockMatrixData *B_cu_data, int num_blocks, const float *C_data, int C_num_cols, int C_row_stride, int C_col_stride, const float *D_data, int D_row_stride, int D_col_stride, float alpha, float beta) { hipLaunchKernelGGL(( _block_add_mat_mat), dim3(Gr),dim3(Bl), 0, 0, B_cu_data, num_blocks, C_data, C_num_cols, C_row_stride, C_col_stride, D_data, D_row_stride, D_col_stride, alpha, beta); } /* * cu:: */ void cudaF_soft_hinge(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _soft_hinge), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaF_group_pnorm(dim3 Gr, dim3 Bl, float *y, const float *x, MatrixDim d, int src_stride, int group_size, float power) { hipLaunchKernelGGL(( _group_pnorm), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, group_size, power); } void cudaF_group_spec_pnorm(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride, int group_size, float power) { if (power == float(0)) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<L0NORM, float>()); } else if (power == float(1)) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<L1NORM, float>()); } else if (power == float(2)) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<L2NORM, float>()); } else if (power == std::numeric_limits<float>::infinity()) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<LINFNORM, float>()); } else { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<LPNORM, float>(power)); } } void cudaF_group_max(dim3 Gr, dim3 Bl, float *y, const float *x, MatrixDim d, int src_stride, int group_size) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<MAX, float>()); } void cudaF_sigmoid(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _sigmoid), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaF_diff_sigmoid(dim3 Gr, dim3 Bl, float* eout, const float* e, const float* y, MatrixDim d, int e_stride, int y_stride) { hipLaunchKernelGGL(( _diff_sigmoid), dim3(Gr),dim3(Bl), 0, 0, eout, e, y, d, e_stride, y_stride); } void cudaF_tanh(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _tanh), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaF_diff_tanh(dim3 Gr, dim3 Bl, float* eout, const float* e, const float* y, MatrixDim d, int e_stride, int y_stride) { hipLaunchKernelGGL(( _diff_tanh), dim3(Gr),dim3(Bl), 0, 0, eout, e, y, d, e_stride, y_stride); } void cudaF_ensure_nonzero(dim3 Gr, dim3 Bl, const float *x, MatrixDim d, float epsilon, int y_stride, float *y) { hipLaunchKernelGGL(( _ensure_nonzero), dim3(Gr),dim3(Bl), 0, 0, x, d, epsilon, y_stride, y); } void cudaF_parametric_relu(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride, const float* a, const float* b) { hipLaunchKernelGGL(( _parametric_relu), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, a, b); } void cudaF_diff_parametric_relu(dim3 Gr, dim3 Bl, float* eout, const float* e, const float* y, MatrixDim d, int e_stride, int y_stride, const float* a, const float* b) { hipLaunchKernelGGL(( _diff_parametric_relu), dim3(Gr),dim3(Bl), 0, 0, eout, e, y, d, e_stride, y_stride, a, b); } void cudaF_heaviside(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _heaviside), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaF_softmax_reduce(size_t Gr, size_t Bl, float* y, const float* x, MatrixDim d, int src_stride, float *logsum, hipStream_t s) { hipLaunchKernelGGL(( _softmax_reduce), dim3(Gr),dim3(Bl),0,s, y, x, d, src_stride, logsum); } void cudaF_log_softmax_reduce(size_t Gr, size_t Bl, float* y, const float* x, MatrixDim y_dim, int x_stride) { hipLaunchKernelGGL(( _log_softmax_reduce), dim3(Gr),dim3(Bl), 0, 0, y, x, y_dim, x_stride); } void cudaF_splice(dim3 Gr, dim3 Bl, float* y, const float* x, const int32_cuda* off, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _splice), dim3(Gr),dim3(Bl), 0, 0, y,x,off,d_out,d_in); } void cudaF_normalize_per_row(size_t Gr, size_t Bl, float *y, int y_stride, const float *x, MatrixDim x_d, float target_rms, bool add_log_stddev) { hipLaunchKernelGGL(( _normalize_per_row), dim3(Gr), dim3(Bl), 0, 0, y, y_stride, x, x_d, target_rms, add_log_stddev); } void cudaF_one(int Gr, int Bl, float* x, int dim) { hipLaunchKernelGGL(( _one), dim3(Gr),dim3(Bl), 0, 0, x,dim); } void cudaF_take_mean(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim d_in) { hipLaunchKernelGGL(( _take_mean), dim3(Gr),dim3(Bl), 0, 0, x,y,d_in); } void cudaF_take_lower(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim d_in) { hipLaunchKernelGGL(( _take_lower), dim3(Gr),dim3(Bl), 0, 0, x,y,d_in); } void cudaF_take_upper(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim d_in) { hipLaunchKernelGGL(( _take_upper), dim3(Gr),dim3(Bl), 0, 0, x,y,d_in); } void cudaF_copy_from_sp(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim dim) { hipLaunchKernelGGL(( _copy_from_sp), dim3(Gr),dim3(Bl), 0, 0, x, y, dim); } void cudaF_copy(dim3 Gr, dim3 Bl, float* y, const float* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy), dim3(Gr),dim3(Bl), 0, 0, y,x,copy_from,d_out,d_in); } void cudaF_randomize(dim3 Gr, dim3 Bl, float* y, const float* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _randomize), dim3(Gr),dim3(Bl), 0, 0, y,x,copy_from,d_out,d_in); } void cudaF_regularize_l1(dim3 Gr, dim3 Bl, float* wei, float* grad, float l1, float lr, MatrixDim d, int stride_grad) { hipLaunchKernelGGL(( _regularize_l1), dim3(Gr),dim3(Bl), 0, 0, wei,grad,l1,lr,d,stride_grad); } void cudaF_find_row_max_id(dim3 Gr, dim3 Bl, const float* mat, float* vec_val, int32_cuda* vec_id, MatrixDim d) { hipLaunchKernelGGL(( _find_row_max_id), dim3(Gr),dim3(Bl), 0, 0, mat, vec_val, vec_id, d); } void cudaF_find_row_abs_max(dim3 Gr, dim3 Bl, const float* mat, float *row_max, MatrixDim d) { hipLaunchKernelGGL(( _find_row_abs_max), dim3(Gr),dim3(Bl), 0, 0, mat, row_max, d); } void cudaF_diff_xent(dim3 Gr, dim3 Bl, const int32_cuda* vec_tgt, float* mat_net_out, float* vec_log_post, MatrixDim d) { hipLaunchKernelGGL(( _diff_xent), dim3(Gr),dim3(Bl), 0, 0, vec_tgt,mat_net_out,vec_log_post,d); } void cudaF_diff_softmax(dim3 Gr, dim3 Bl, float* x, const MatrixDim dim, const float* value, const int value_stride, const float* diff, const int diff_stride) { hipLaunchKernelGGL(( _diff_softmax), dim3(Gr), dim3(Bl), 0, 0, x, dim, value, value_stride, diff, diff_stride); } void cudaF_copy_rows_from_vec(dim3 Gr, dim3 Bl, float *mat_out, MatrixDim d_out, const float *v_in) { hipLaunchKernelGGL(( _copy_rows_from_vec), dim3(Gr),dim3(Bl), 0, 0, mat_out, d_out, v_in); } void cudaF_diff_log_softmax(dim3 Gr, dim3 Bl, const MatrixDim in_deriv_dim, const float* out_value, const int out_value_stride, const float* out_deriv, const int out_deriv_stride, float* in_deriv) { hipLaunchKernelGGL(( _diff_log_softmax), dim3(Gr), dim3(Bl), 0, 0, in_deriv_dim, out_value, out_value_stride, out_deriv, out_deriv_stride, in_deriv); } void cudaF_copy_col_from_mat_df(int Gr, int Bl, double* v, int col, const float* mat, MatrixDim dmat, int dim) { hipLaunchKernelGGL(( _copy_col_from_mat_df), dim3(Gr),dim3(Bl), 0, 0, v,col,mat,dmat,dim); } void cudaF_copy_col_from_mat_fd(int Gr, int Bl, float* v, int col, const float* mat, MatrixDim dmat, int dim) { hipLaunchKernelGGL(( _copy_col_from_mat_fd), dim3(Gr),dim3(Bl), 0, 0, v,col,mat,dmat,dim); } void cudaF_sum_column_ranges(dim3 Gr, dim3 Bl, float *data, MatrixDim dim, const float *src_data, MatrixDim src_dim, const Int32Pair *indices) { hipLaunchKernelGGL(( _sum_column_ranges), dim3(Gr),dim3(Bl), 0, 0, data, dim, src_data, src_dim, indices); } void cudaF_add_row_ranges(dim3 Gr, dim3 Bl, float *data, MatrixDim dim, const float *src_data, MatrixDim src_dim, const Int32Pair *indexes) { hipLaunchKernelGGL(( _add_row_ranges), dim3(Gr),dim3(Bl), 0, 0, data, dim, src_data, src_dim, indexes); } void cudaF_matrix_lookup(dim3 Gr, dim3 Bl, const float *data, MatrixDim dim, const Int32Pair *indices, int indices_size, float *output) { hipLaunchKernelGGL(( _matrix_lookup), dim3(Gr),dim3(Bl), 0, 0, data, dim, indices, indices_size, output); } void cudaF_equal_element_mask(dim3 Gr, dim3 Bl, const float *mat1, const float *mat2, float *mask, MatrixDim mat1_dim, int mat2_stride, int mask_stride) { hipLaunchKernelGGL(( _equal_element_mask), dim3(Gr),dim3(Bl), 0, 0, mat1, mat2, mask, mat1_dim, mat2_stride, mask_stride); } /* * "double" */ /* * CuMatrix */ void cudaD_copy_upp_low(dim3 Gr, dim3 Bl, double* A, MatrixDim dimA) { hipLaunchKernelGGL(( _copy_upp_low), dim3(Gr),dim3(Bl), 0, 0, A,dimA);} void cudaD_copy_low_upp(dim3 Gr, dim3 Bl, double* A, MatrixDim dimA) { hipLaunchKernelGGL(( _copy_low_upp), dim3(Gr),dim3(Bl), 0, 0, A,dimA);} void cudaD_add_diag_vec_mat(dim3 Gr, dim3 Bl, double alpha, double *mat, MatrixDim mat_dim, const double *vec, const double *mat2, int mat2_row_stride, int mat2_col_stride, double beta) { hipLaunchKernelGGL(( _add_diag_vec_mat), dim3(Gr),dim3(Bl), 0, 0, alpha, mat, mat_dim, vec, mat2, mat2_row_stride, mat2_col_stride, beta); } void cudaD_copy_from_tp_trans(dim3 Gr, dim3 Bl, double* A, const double* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp_trans), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaDF_copy_from_tp_trans(dim3 Gr, dim3 Bl, double* A, const float* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp_trans), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaD_copy_from_tp(dim3 Gr, dim3 Bl, double* A, const double* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaDF_copy_from_tp(dim3 Gr, dim3 Bl, double* A, const float* B, MatrixDim dmat) { hipLaunchKernelGGL(( _copy_from_tp), dim3(Gr),dim3(Bl), 0, 0, A,B,dmat); } void cudaD_apply_exp(dim3 Gr, dim3 Bl, double* mat, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _apply_exp), dim3(Gr),dim3(Bl),0,s, mat,d); } void cudaD_apply_exp_limited(dim3 Gr, dim3 Bl, double* mat, MatrixDim d, double lower_limit, double upper_limit) { hipLaunchKernelGGL(( _apply_exp_limited), dim3(Gr),dim3(Bl), 0, 0, mat, d, lower_limit, upper_limit); } void cudaD_apply_pow(dim3 Gr, dim3 Bl, double* mat, double power, MatrixDim d) { hipLaunchKernelGGL(( _apply_pow), dim3(Gr),dim3(Bl), 0, 0, mat, power, d); } void cudaD_apply_pow_abs(dim3 Gr, dim3 Bl, double* mat, double power, bool include_sign, MatrixDim d) { hipLaunchKernelGGL(( _apply_pow_abs), dim3(Gr),dim3(Bl), 0, 0, mat, power, include_sign, d); } void cudaD_apply_heaviside(dim3 Gr, dim3 Bl, double* mat, MatrixDim d) { hipLaunchKernelGGL(( _apply_heaviside), dim3(Gr),dim3(Bl), 0, 0, mat, d); } void cudaD_apply_fixed(dim3 Gr, dim3 Bl, double* mat, double resolution, int mode, MatrixDim d){ hipLaunchKernelGGL(( _apply_fixed), dim3(Gr),dim3(Bl), 0, 0, mat, resolution, mode, d); } void cudaD_copy_cols(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _copy_cols), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaD_add_cols(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _add_cols), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaD_copy_rows(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _copy_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaD_copy_rows_direct(dim3 Gr, dim3 Bl, double* dst, const double* const * src, MatrixDim dst_dim) { hipLaunchKernelGGL(( _copy_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, dst_dim); } void cudaD_copy_to_rows_direct(dim3 Gr, dim3 Bl, double* const * dst, const double* src, MatrixDim src_dim) { hipLaunchKernelGGL(( _copy_to_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, src_dim); } void cudaD_add_rows(dim3 Gr, dim3 Bl, double alpha, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _add_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, reorder, dst_dim, src_stride); } void cudaD_mul_rows(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _mul_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaD_div_rows(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { hipLaunchKernelGGL(( _div_rows), dim3(Gr),dim3(Bl), 0, 0, dst, src, reorder, dst_dim, src_stride); } void cudaD_add_rows_direct(dim3 Gr, dim3 Bl, double alpha, double* dst, const double* const * src, MatrixDim dst_dim) { hipLaunchKernelGGL(( _add_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, dst_dim); } void cudaD_add_to_rows(dim3 Gr, dim3 Bl, double alpha, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim src_dim, int dst_stride) { hipLaunchKernelGGL(( _add_to_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, reorder, src_dim, dst_stride); } void cudaD_add_to_rows_direct(dim3 Gr, dim3 Bl, double alpha, double* const * dst, const double* src, MatrixDim src_dim) { hipLaunchKernelGGL(( _add_to_rows), dim3(Gr),dim3(Bl), 0, 0, alpha, dst, src, src_dim); } void cudaD_apply_floor(dim3 Gr, dim3 Bl, double* mat, double floor_val, MatrixDim d) { hipLaunchKernelGGL(( _apply_floor), dim3(Gr),dim3(Bl), 0, 0, mat, floor_val, d); } void cudaD_apply_ceiling(dim3 Gr, dim3 Bl, double* mat, double ceiling_val, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _apply_ceiling), dim3(Gr),dim3(Bl),0,s, mat, ceiling_val, d); } void cudaD_set_diag(int Gr, int Bl, double* mat, double value, MatrixDim d) { hipLaunchKernelGGL(( _set_diag), dim3(Gr),dim3(Bl), 0, 0, mat,value,d); } void cudaD_set_diag_packed(int Gr, int Bl, double* mat, double value, int dim) { hipLaunchKernelGGL(( _set_diag_packed), dim3(Gr),dim3(Bl), 0, 0, mat,value,dim); } void cudaD_add_diag_packed(int Gr, int Bl, double* mat, double value, int dim) { hipLaunchKernelGGL(( _add_diag_packed), dim3(Gr),dim3(Bl), 0, 0, mat,value,dim); } void cudaD_set_const(dim3 Gr, dim3 Bl, double* mat, double value, MatrixDim d) { hipLaunchKernelGGL(( _set_const), dim3(Gr),dim3(Bl), 0, 0, mat,value,d); } void cudaD_set_zero_above_diag(dim3 Gr, dim3 Bl, double* mat, MatrixDim d) { hipLaunchKernelGGL(( _set_zero_above_diag), dim3(Gr),dim3(Bl), 0, 0, mat, d); } void cudaD_add(dim3 Gr, dim3 Bl, double* mat, double value, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _add), dim3(Gr),dim3(Bl),0,s, mat,value,d); } void cudaD_scale_diag_packed(int Gr, int Bl, double* mat, double value, int dim) { hipLaunchKernelGGL(( _scale_diag_packed), dim3(Gr),dim3(Bl), 0, 0, mat,value,dim); } void cudaD_scale(dim3 Gr, dim3 Bl, double* mat, double value, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _scale), dim3(Gr),dim3(Bl),0,s, mat,value,d); } void cudaD_apply_log(dim3 Gr, dim3 Bl, double* mat, MatrixDim d) { hipLaunchKernelGGL(( _apply_log), dim3(Gr),dim3(Bl), 0, 0, mat,d); } void cudaD_mul_elements(dim3 Gr, dim3 Bl, double* mat, const double* A, MatrixDim dst_d, int src_stride) { hipLaunchKernelGGL(( _mul_elements), dim3(Gr),dim3(Bl), 0, 0, mat,A,dst_d,src_stride); } void cudaD_div_elements(dim3 Gr, dim3 Bl, double* mat, const double* A, MatrixDim dst_d, int src_stride) { hipLaunchKernelGGL(( _div_elements), dim3(Gr),dim3(Bl), 0, 0, mat,A,dst_d,src_stride); } void cudaD_max(dim3 Gr, dim3 Bl, double* mat, const double* A, MatrixDim dst_d, int src_stride) { hipLaunchKernelGGL(( _max), dim3(Gr),dim3(Bl), 0, 0, mat,A,dst_d,src_stride); } void cudaD_min(dim3 Gr, dim3 Bl, double* mat, const double* other, MatrixDim mat_d, int other_stride) { hipLaunchKernelGGL(( _min), dim3(Gr),dim3(Bl), 0, 0, mat,other,mat_d,other_stride); } void cudaD_mul_cols_vec(dim3 Gr, dim3 Bl, double* mat, const double* scale, MatrixDim d) { hipLaunchKernelGGL(( _mul_cols_vec), dim3(Gr),dim3(Bl), 0, 0, mat,scale,d); } void cudaD_mul_rows_vec(dim3 Gr, dim3 Bl, double* mat, const double* scale, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _mul_rows_vec), dim3(Gr),dim3(Bl),0,s, mat,scale,d); } void cudaD_mul_rows_group_mat(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size) { hipLaunchKernelGGL(( _mul_rows_group_mat), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, group_size); } void cudaD_diff_group_pnorm(dim3 Gr, dim3 Bl, double *id, const double *iv, const double *ov, const double* od, MatrixDim id_dim, int iv_stride, int ov_stride, int od_stride, int group_size, double power) { hipLaunchKernelGGL(( _diff_group_pnorm), dim3(Gr), dim3(Bl), 0, 0, id, iv, ov, od, id_dim, iv_stride, ov_stride, od_stride, group_size, power); } void cudaD_calc_group_max_deriv(dim3 Gr, dim3 Bl, double*y, const double* x1, const double* x2, MatrixDim y_dim, int x1_stride, int x2_stride, int group_size) { hipLaunchKernelGGL(( _calc_group_max_deriv), dim3(Gr),dim3(Bl), 0, 0, y, x1, x2, y_dim, x1_stride, x2_stride, group_size); } void cudaD_div_rows_vec(dim3 Gr, dim3 Bl, double* mat, const double* vec_div, MatrixDim d) { hipLaunchKernelGGL(( _div_rows_vec), dim3(Gr),dim3(Bl), 0, 0, mat, vec_div, d); } void cudaD_add_mat(dim3 Gr, dim3 Bl, double alpha, const double* src, double* dst, MatrixDim d, int src_stride, int A_trans, hipStream_t s) { if (A_trans) { hipLaunchKernelGGL(( _add_mat_trans), dim3(Gr),dim3(Bl),0,s, alpha,src,dst,d,src_stride); } else { hipLaunchKernelGGL(( _add_mat), dim3(Gr),dim3(Bl),0,s, alpha,src,dst,d,src_stride); } } void cudaD_add_mat_blocks(dim3 Gr, dim3 Bl, double alpha, const double* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, double* dst, MatrixDim d, int src_stride, int A_trans) { if (A_trans) { hipLaunchKernelGGL(( _add_mat_blocks_trans), dim3(Gr),dim3(Bl), 0, 0, alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } else { hipLaunchKernelGGL(( _add_mat_blocks), dim3(Gr),dim3(Bl), 0, 0, alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } } void cudaD_add_mat_repeated(dim3 Gr, dim3 Bl, double alpha, const double* src, MatrixDim src_dim, double *dst, MatrixDim dst_dim) { hipLaunchKernelGGL(( _add_mat_repeated), dim3(Gr),dim3(Bl), 0, 0, alpha, src, src_dim, dst, dst_dim); } void cudaD_set_mat_mat_div_mat(dim3 Gr, dim3 Bl, const double *A, const double *B, const double *C, double *dst, MatrixDim d, int stride_a, int stride_b, int stride_c) { hipLaunchKernelGGL(( _set_mat_mat_div_mat), dim3(Gr),dim3(Bl), 0, 0, A,B,C,dst,d,stride_a,stride_b,stride_c); } void cudaD_sy_add_tr2(dim3 Gr, dim3 Bl, double alpha, double beta, const double* T, MatrixDim tdim, double *S, MatrixDim sdim) { hipLaunchKernelGGL(( _sy_add_tr2), dim3(Gr),dim3(Bl), 0, 0, alpha, beta, T, tdim, S, sdim); } void cudaD_add_vec_to_cols(dim3 Gr, dim3 Bl, double alpha, const double* col, double beta, double* dst, MatrixDim d) { hipLaunchKernelGGL(( _add_vec_to_cols), dim3(Gr),dim3(Bl), 0, 0, alpha,col,beta,dst,d); } void cudaD_add_vec_to_rows(dim3 Gr, dim3 Bl, double alpha, const double* row, double beta, double* dst, MatrixDim d) { hipLaunchKernelGGL(( _add_vec_to_rows), dim3(Gr),dim3(Bl), 0, 0, alpha,row,beta,dst,d); } void cudaD_add_mat_diag_vec(dim3 Gr, dim3 Bl, double alpha, double *mat, MatrixDim mat_dim, const double *mat2, int mat2_row_stride, int mat2_col_stride, const double *vec, double beta) { hipLaunchKernelGGL(( _add_mat_diag_vec), dim3(Gr),dim3(Bl), 0, 0, alpha, mat, mat_dim, mat2, mat2_row_stride, mat2_col_stride, vec, beta); } void cudaD_add_mat_mat_elements(dim3 Gr, dim3 Bl, double *data, const double *srcA_data, const double *srcB_data, MatrixDim dim, int srcA_stride, int srcB_stride, double alpha, double beta) { hipLaunchKernelGGL(( _add_mat_mat_elements), dim3(Gr), dim3(Bl), 0, 0, data, srcA_data, srcB_data, dim, srcA_stride, srcB_stride, alpha, beta); } // CURRENTLY UNUSED... void cudaD_apply_mask(dim3 Gr, dim3 Bl, double* mat, const char* mask, MatrixDim dmat, MatrixDim dmask) { hipLaunchKernelGGL(( _apply_mask), dim3(Gr),dim3(Bl), 0, 0, mat,mask,dmat,dmask); } /* * CuVector */ void cudaD_max_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<MAX,double>()); } void cudaD_abs_max_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<ABSMAX,double>()); } void cudaD_min_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<MIN,double>()); } void cudaD_sum_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr),dim3(Bl), 0, 0, result,mat,d, TransReduceOp<SUM,double>()); } void cudaD_add_row_sum_mat(double* result, const double* mat, void* scratch, const MatrixDim d, const double alpha, const double beta, hipStream_t s) { //_strided_reduction_fused(result, mat, scratch, d, alpha, beta); } void cudaD_add_col_sum_mat(int Gr, int Bl, double* result, const double* mat, const MatrixDim d, const double alpha, const double beta, hipStream_t s) { hipLaunchKernelGGL(( _transform_reduce_mat_cols), dim3(Gr), dim3(Bl),0,s, result, mat, d, TransReduceOp<SUMAB, double>(alpha, beta)); } void cudaD_replace_value(int Gr, int Bl, double *v, int dim, double orig, double changed) { hipLaunchKernelGGL(( _replace_value), dim3(Gr),dim3(Bl), 0, 0, v, dim, orig, changed); } void cudaD_set_bias_params(int Gr, int Bl, double* v, const double* a, double param_1, double param_2, double param_3, int* flag, int dim) { hipLaunchKernelGGL(( _set_bias_params), dim3(Gr),dim3(Bl), 0, 0, v,a,param_1,param_2,param_3,flag,dim); } void cudaD_vec_mul_elements(int Gr, int Bl, double* v, const double* a, int dim, hipStream_t s) { hipLaunchKernelGGL(( _vec_mul_elements), dim3(Gr),dim3(Bl),0,s, v, a, dim); } void cudaD_vec_min(int Gr, int Bl, const double* v, double* value, int dim, int inc) { hipLaunchKernelGGL(( _vec_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, v, value, dim, inc, TransReduceOp<MIN, double>()); } void cudaD_vec_max(int Gr, int Bl, const double* v, double* value, int dim, int inc) { hipLaunchKernelGGL(( _vec_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, v, value, dim, inc, TransReduceOp<MAX, double>()); } void cudaD_trace_mat_mat_trans(dim3 Gr, dim3 Bl, const double* A, const double* B, MatrixDim dA, int B_stride, double* value) { hipLaunchKernelGGL(( _trace_mat_mat_trans), dim3(Gr),dim3(Bl), 0, 0, A,B,dA,B_stride,value); } void cudaD_trace_mat_mat(dim3 Gr, dim3 Bl, const double* A, const double* B, MatrixDim dA, int B_stride, double* value) { hipLaunchKernelGGL(( _trace_mat_mat<32>) , dim3(Gr),dim3(Bl), 0, 0, A,B,dA,B_stride,value); } void cudaD_add_diag_mat_mat_MNT(int Gr, int Bl, const double alpha, const double* M, const MatrixDim dim_M, const double* N, const int stride_N, const double beta, double* v) { hipLaunchKernelGGL(( _add_diag_mat_mat_MNT), dim3(Gr),dim3(Bl), 0, 0, alpha,M,dim_M,N,stride_N,beta,v); } void cudaD_add_diag_mat_mat_MTN(dim3 Gr, dim3 Bl, const double alpha, const double* M, const int stride_M, const double* N, const MatrixDim dim_N, const double beta, double* v, const int stride_v) { if (Bl.x == 16) { hipLaunchKernelGGL(( _add_diag_mat_mat_MTN<16>) , dim3(Gr), dim3(Bl), 0, 0, alpha, M, stride_M, N, dim_N, beta, v, stride_v); } else if (Bl.x == 32) { hipLaunchKernelGGL(( _add_diag_mat_mat_MTN<32>) , dim3(Gr), dim3(Bl), 0, 0, alpha, M, stride_M, N, dim_N, beta, v, stride_v); } } void cudaD_add_diag_mat_mat_MN(dim3 Gr, dim3 Bl, const double alpha, const double* M, const int stride_M, const double* N, const MatrixDim dim_N, const double beta, double* v) { if (Bl.x == 16) { hipLaunchKernelGGL(( _add_diag_mat_mat_MN<16>) , dim3(Gr),dim3(Bl), 0, 0, alpha,M,stride_M,N,dim_N,beta,v); } else if (Bl.x==32) { hipLaunchKernelGGL(( _add_diag_mat_mat_MN<32>), dim3(Gr),dim3(Bl), 0, 0, alpha,M,stride_M,N,dim_N,beta,v); } } void cudaD_add_vec_vec(int Gr, int Bl, double alpha, double* v, const double* x, const double* y, double beta, int dim) { hipLaunchKernelGGL(( _add_vec_vec), dim3(Gr),dim3(Bl), 0, 0, alpha,v,x,y,beta,dim); } void cudaD_copy_col_from_mat_df(int Gr, int Bl, double* v, int col, const double* mat, MatrixDim dmat, int dim) { hipLaunchKernelGGL(( _copy_col_from_mat_df), dim3(Gr),dim3(Bl), 0, 0, v,col,mat,dmat,dim); } void cudaD_copy_col_from_mat_fd(int Gr, int Bl, float* v, int col, const double* mat, MatrixDim dmat, int dim) { hipLaunchKernelGGL(( _copy_col_from_mat_fd), dim3(Gr),dim3(Bl), 0, 0, v,col,mat,dmat,dim); } void cudaD_vec_sum(int Gr, int Bl, double* v, double* value, int dim, int inc) { hipLaunchKernelGGL(( _vec_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, v,value,dim,inc, TransReduceOp<SUM, double>()); } void cudaD_matrix_add_elements(dim3 Gr, dim3 Bl, double *data, MatrixDim dim, double alpha, MatrixElement<double>* x, int num_elements) { hipLaunchKernelGGL(( _cuda_matrix_add_elements), dim3(Gr), dim3(Bl), 0, 0, data, dim, alpha, x, num_elements); } void cudaD_vector_copy_elements(dim3 Gr, dim3 Bl, double *data, int dim, const double *src_mat, int mat_stride, bool transpose, const MatrixIndexT_cuda* elements) { hipLaunchKernelGGL(( _cuda_vector_copy_elements), dim3(Gr), dim3(Bl), 0, 0, data, dim, src_mat, mat_stride, transpose, elements); } void cudaD_matrix_add_indexed_values(dim3 Gr, dim3 Bl, MatrixDim dim, double alpha, const Int32Pair* indices, const double* x, int s, double* data) { hipLaunchKernelGGL(( _cuda_matrix_add_indexed_values), dim3(Gr), dim3(Bl), 0, 0, dim, alpha, indices, x, s, data); } void cudaD_matrix_add_to_elements(dim3 Gr, dim3 Bl, double alpha, double* mat, MatrixDim dim, const MatrixIndexT_cuda* elements) { hipLaunchKernelGGL(( _cuda_matrix_add_to_elements), dim3(Gr), dim3(Bl), 0, 0, alpha, mat, dim, elements); } void cudaD_vec_copy_diag_from_packed(int Gr, int Bl, double *dst, const double *src, int dim) { hipLaunchKernelGGL(( _vec_copy_diag_from_packed), dim3(Gr),dim3(Bl), 0, 0, dst,src,dim); } void cudaD_vec_apply_floor(int Gr, int Bl, double* v, double floor_val, float *count, int dim) { hipLaunchKernelGGL(( _vec_apply_floor), dim3(Gr),dim3(Bl), 0, 0, v,floor_val,count,dim); } void cudaD_vec_apply_ceiling(int Gr, int Bl, double* v, double ceiling_val, float *count, int dim) { hipLaunchKernelGGL(( _vec_apply_ceiling), dim3(Gr),dim3(Bl), 0, 0, v,ceiling_val,count,dim); } void cudaD_vec_apply_exp(int Gr, int Bl, double* v, int dim) { hipLaunchKernelGGL(( _vec_apply_exp), dim3(Gr),dim3(Bl), 0, 0, v,dim); } void cudaD_vec_apply_log(int Gr, int Bl, double* v, double* flag, int dim) { hipLaunchKernelGGL(( _vec_apply_log), dim3(Gr),dim3(Bl), 0, 0, v,flag,dim); } void cudaD_invert_elements(dim3 Gr, dim3 Bl, double* data, MatrixDim d) { hipLaunchKernelGGL(( _invert_elements), dim3(Gr),dim3(Bl), 0, 0, data, d); } void cudaD_add_mat_blockmat(dim3 Gr, dim3 Bl, double *data, MatrixDim d, const double *Adata, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, double alpha, double beta, int B_trans) { if (B_trans) { hipLaunchKernelGGL(( _add_mat_blockmat_trans), dim3(Gr),dim3(Bl), 0, 0, data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } else { hipLaunchKernelGGL(( _add_mat_blockmat), dim3(Gr),dim3(Bl), 0, 0, data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } } void cudaD_block_add_mat_mat(dim3 Gr, dim3 Bl, CuBlockMatrixData *B_cu_data, int num_blocks, const double *C_data, int C_num_cols, int C_row_stride, int C_col_stride, const double *D_data, int D_row_stride, int D_col_stride, double alpha, double beta) { hipLaunchKernelGGL(( _block_add_mat_mat), dim3(Gr),dim3(Bl), 0, 0, B_cu_data, num_blocks, C_data, C_num_cols, C_row_stride, C_col_stride, D_data, D_row_stride, D_col_stride, alpha, beta); } /* * cu:: */ void cudaD_soft_hinge(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _soft_hinge), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaD_group_pnorm(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size, double power) { hipLaunchKernelGGL(( _group_pnorm), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, group_size, power); } void cudaD_group_spec_pnorm(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size, double power) { if (power == double(0)) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<L0NORM, double>()); } else if (power == double(1)) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<L1NORM, double>()); } else if (power == double(2)) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<L2NORM, double>()); } else if (power == std::numeric_limits<double>::infinity()) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<LINFNORM, double>()); } else { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr), dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<LPNORM, double>(power)); } } void cudaD_group_max(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size) { hipLaunchKernelGGL(( _group_transform_reduce), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, group_size, TransReduceOp<MAX, double>()); } void cudaD_sigmoid(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _sigmoid), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaD_diff_sigmoid(dim3 Gr, dim3 Bl, double* eout, const double* e, const double* y, MatrixDim d, int e_stride, int y_stride) { hipLaunchKernelGGL(( _diff_sigmoid), dim3(Gr),dim3(Bl), 0, 0, eout, e, y, d, e_stride, y_stride); } void cudaD_tanh(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _tanh), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaD_diff_tanh(dim3 Gr, dim3 Bl, double* eout, const double* e, const double* y, MatrixDim d, int e_stride, int y_stride) { hipLaunchKernelGGL(( _diff_tanh), dim3(Gr),dim3(Bl), 0, 0, eout, e, y, d, e_stride, y_stride); } void cudaD_ensure_nonzero(dim3 Gr, dim3 Bl, const double *x, MatrixDim d, double epsilon, int y_stride, double *y) { hipLaunchKernelGGL(( _ensure_nonzero), dim3(Gr),dim3(Bl), 0, 0, x, d, epsilon, y_stride, y); } void cudaD_parametric_relu(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, const double* a, const double* b) { hipLaunchKernelGGL(( _parametric_relu), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride, a, b); } void cudaD_diff_parametric_relu(dim3 Gr, dim3 Bl, double* eout, const double* e, const double* y, MatrixDim d, int e_stride, int y_stride, const double* a, const double* b) { hipLaunchKernelGGL(( _diff_parametric_relu), dim3(Gr),dim3(Bl), 0, 0, eout, e, y, d, e_stride, y_stride, a, b); } void cudaD_heaviside(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { hipLaunchKernelGGL(( _heaviside), dim3(Gr),dim3(Bl), 0, 0, y, x, d, src_stride); } void cudaD_softmax_reduce(size_t Gr, size_t Bl, double* y, const double* x, MatrixDim d, int src_stride, double *logsum, hipStream_t s) { hipLaunchKernelGGL(( _softmax_reduce), dim3(Gr),dim3(Bl),0,s, y, x, d, src_stride, logsum); } void cudaD_log_softmax_reduce(size_t Gr, size_t Bl, double* y, const double* x, MatrixDim y_dim, int x_stride) { hipLaunchKernelGGL(( _log_softmax_reduce), dim3(Gr),dim3(Bl), 0, 0, y, x, y_dim, x_stride); } void cudaD_normalize_per_row(size_t Gr, size_t Bl, double *y, int y_stride, const double *x, MatrixDim x_d, double target_rms, bool add_log_stddev) { hipLaunchKernelGGL(( _normalize_per_row), dim3(Gr), dim3(Bl), 0, 0, y, y_stride, x, x_d, target_rms, add_log_stddev); } void cudaD_splice(dim3 Gr, dim3 Bl, double* y, const double* x, const int32_cuda* off, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _splice), dim3(Gr),dim3(Bl), 0, 0, y,x,off,d_out,d_in); } void cudaD_one(int Gr, int Bl, double* x, int dim) { hipLaunchKernelGGL(( _one), dim3(Gr),dim3(Bl), 0, 0, x,dim); } void cudaD_take_mean(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_in) { hipLaunchKernelGGL(( _take_mean), dim3(Gr),dim3(Bl), 0, 0, x,y,d_in); } void cudaD_take_lower(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_in) { hipLaunchKernelGGL(( _take_lower), dim3(Gr),dim3(Bl), 0, 0, x,y,d_in); } void cudaD_take_upper(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_in) { hipLaunchKernelGGL(( _take_upper), dim3(Gr),dim3(Bl), 0, 0, x,y,d_in); } void cudaD_copy_from_sp(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_out) { hipLaunchKernelGGL(( _copy_from_sp), dim3(Gr),dim3(Bl), 0, 0, x,y,d_out); } void cudaD_copy(dim3 Gr, dim3 Bl, double* y, const double* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy), dim3(Gr),dim3(Bl), 0, 0, y,x,copy_from,d_out,d_in); } void cudaD_randomize(dim3 Gr, dim3 Bl, double* y, const double* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _randomize), dim3(Gr),dim3(Bl), 0, 0, y,x,copy_from,d_out,d_in); } void cudaD_regularize_l1(dim3 Gr, dim3 Bl, double* wei, double* grad, double l1, double lr, MatrixDim d, int stride_grad) { hipLaunchKernelGGL(( _regularize_l1), dim3(Gr),dim3(Bl), 0, 0, wei,grad,l1,lr,d,stride_grad); } void cudaD_find_row_max_id(dim3 Gr, dim3 Bl, const double* mat, double* vec_val, int32_cuda* vec_id, MatrixDim d) { hipLaunchKernelGGL(( _find_row_max_id), dim3(Gr),dim3(Bl), 0, 0, mat, vec_val, vec_id, d); } void cudaD_find_row_abs_max(dim3 Gr, dim3 Bl, const double* mat, double *row_max, MatrixDim d) { hipLaunchKernelGGL(( _find_row_abs_max), dim3(Gr),dim3(Bl), 0, 0, mat, row_max, d); } void cudaD_diff_xent(dim3 Gr, dim3 Bl, const int32_cuda* vec_tgt, double* mat_net_out, double* vec_log_post, MatrixDim d) { hipLaunchKernelGGL(( _diff_xent), dim3(Gr),dim3(Bl), 0, 0, vec_tgt,mat_net_out,vec_log_post,d); } void cudaD_diff_softmax(dim3 Gr, dim3 Bl, double* x, const MatrixDim dim, const double* value, const int value_stride, const double* diff, const int diff_stride) { hipLaunchKernelGGL(( _diff_softmax), dim3(Gr), dim3(Bl), 0, 0, x, dim, value, value_stride, diff, diff_stride); } void cudaD_diff_log_softmax(dim3 Gr, dim3 Bl, const MatrixDim in_deriv_dim, const double* out_value, const int out_value_stride, const double* out_deriv, const int out_deriv_stride, double* in_deriv) { hipLaunchKernelGGL(( _diff_log_softmax), dim3(Gr), dim3(Bl), 0, 0, in_deriv_dim, out_value, out_value_stride, out_deriv, out_deriv_stride, in_deriv); } void cudaD_copy_rows_from_vec(dim3 Gr, dim3 Bl, double *mat_out, MatrixDim d_out, const double *v_in) { hipLaunchKernelGGL(( _copy_rows_from_vec), dim3(Gr),dim3(Bl), 0, 0, mat_out, d_out, v_in); } void cudaD_sum_column_ranges(dim3 Gr, dim3 Bl, double *data, MatrixDim dim, const double *src_data, MatrixDim src_dim, const Int32Pair *indices) { hipLaunchKernelGGL(( _sum_column_ranges), dim3(Gr),dim3(Bl), 0, 0, data, dim, src_data, src_dim, indices); } void cudaD_add_row_ranges(dim3 Gr, dim3 Bl, double *data, MatrixDim dim, const double *src_data, MatrixDim src_dim, const Int32Pair *indexes) { hipLaunchKernelGGL(( _add_row_ranges), dim3(Gr),dim3(Bl), 0, 0, data, dim, src_data, src_dim, indexes); } void cudaD_matrix_lookup(dim3 Gr, dim3 Bl, const double *data, MatrixDim dim, const Int32Pair *indices, int indices_size, double *output) { hipLaunchKernelGGL(( _matrix_lookup), dim3(Gr),dim3(Bl), 0, 0, data, dim, indices, indices_size, output); } void cudaD_equal_element_mask(dim3 Gr, dim3 Bl, const double *mat1, const double *mat2, double *mask, MatrixDim mat1_dim, int mat2_stride, int mask_stride) { hipLaunchKernelGGL(( _equal_element_mask), dim3(Gr),dim3(Bl), 0, 0, mat1, mat2, mask, mat1_dim, mat2_stride, mask_stride); } // Some conversion kernels for which it's more convenient // to not name them F or D. void cuda_copy_from_mat_df(dim3 Gr, dim3 Bl, double* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat), dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_ff(dim3 Gr, dim3 Bl, float* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat), dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_fd(dim3 Gr, dim3 Bl, float *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat), dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_dd(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat), dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_df_trans(dim3 Gr, dim3 Bl, double* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat_trans<32>) , dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_ff_trans(dim3 Gr, dim3 Bl, float* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat_trans<32>) , dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_fd_trans(dim3 Gr, dim3 Bl, float *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat_trans<32>) , dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_dd_trans(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { hipLaunchKernelGGL(( _copy_from_mat_trans<32>) , dim3(Gr),dim3(Bl), 0, 0, mat_out,mat_in,d_out,d_in); } void cuda_copy_from_smat_ff(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { hipLaunchKernelGGL(( _copy_from_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_fd(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { hipLaunchKernelGGL(( _copy_from_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_df(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { hipLaunchKernelGGL(( _copy_from_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_dd(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { hipLaunchKernelGGL(( _copy_from_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_ff_trans(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { hipLaunchKernelGGL(( _copy_from_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_fd_trans(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { hipLaunchKernelGGL(( _copy_from_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_df_trans(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { hipLaunchKernelGGL(( _copy_from_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_dd_trans(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { hipLaunchKernelGGL(( _copy_from_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cudaF_trace_mat_smat(dim3 Gr, dim3 Bl, const float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val, float* trace_vec) { hipLaunchKernelGGL(( _trace_mat_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaF_trace_mat_smat_trans(dim3 Gr, dim3 Bl, const float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val, float* trace_vec) { hipLaunchKernelGGL(( _trace_mat_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaD_trace_mat_smat(dim3 Gr, dim3 Bl, const double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val, double* trace_vec) { hipLaunchKernelGGL(( _trace_mat_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaD_trace_mat_smat_trans(dim3 Gr, dim3 Bl, const double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val, double* trace_vec) { hipLaunchKernelGGL(( _trace_mat_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaD_lstm_nonlinearity(dim3 Gr, dim3 Bl, const double* in, const int in_stride, const double* params, const int params_stride, const int out_stride, const int cell_dim, const int have_dropout_mask, const int num_rows, double* out) { hipLaunchKernelGGL(( _lstm_nonlinearity), dim3(Gr), dim3(Bl), 0, 0, in, in_stride, params, params_stride, out_stride, cell_dim, have_dropout_mask, num_rows, out); } void cudaF_lstm_nonlinearity(dim3 Gr, dim3 Bl, const float* in, const int in_stride, const float* params, const int params_stride, const int out_stride, const int cell_dim, const int have_dropout_mask, const int num_rows, float* out) { hipLaunchKernelGGL(( _lstm_nonlinearity), dim3(Gr), dim3(Bl), 0, 0, in, in_stride, params, params_stride, out_stride, cell_dim, have_dropout_mask, num_rows, out); } void cudaD_diff_lstm_nonlinearity(dim3 Gr, dim3 Bl, const int cell_dim, const int have_dropout_mask, const int num_rows, const double* input, const int input_stride, const double* params, const int params_stride, const double* output_deriv, const int output_deriv_stride, const double* deriv_sum_in, const int deriv_sum_in_stride, const double* self_repair_config, double count, double* input_deriv, const int input_deriv_stride, double* params_deriv, const int params_deriv_stride, double* value_sum_out, const int value_sum_out_stride, double* deriv_sum_out, const int deriv_sum_out_stride, double* self_repair_sum_out, const int self_repair_sum_out_stride) { hipLaunchKernelGGL(( _diff_lstm_nonlinearity), dim3(Gr), dim3(Bl), 0, 0, cell_dim, have_dropout_mask, num_rows, input, input_stride, params, params_stride, output_deriv, output_deriv_stride, deriv_sum_in, deriv_sum_in_stride, self_repair_config, count, input_deriv, input_deriv_stride, params_deriv, params_deriv_stride, value_sum_out, value_sum_out_stride, deriv_sum_out, deriv_sum_out_stride, self_repair_sum_out, self_repair_sum_out_stride); } void cudaF_diff_lstm_nonlinearity(dim3 Gr, dim3 Bl, const int cell_dim, const int have_dropout_mask, const int num_rows, const float* input, const int input_stride, const float* params, const int params_stride, const float* output_deriv, const int output_deriv_stride, const double* deriv_sum_in, const int deriv_sum_in_stride, const float* self_repair_config, double count, float* input_deriv, const int input_deriv_stride, float* params_deriv, const int params_deriv_stride, double* value_sum_out, const int value_sum_out_stride, double* deriv_sum_out, const int deriv_sum_out_stride, float* self_repair_sum_out, const int self_repair_sum_out_stride) { hipLaunchKernelGGL(( _diff_lstm_nonlinearity), dim3(Gr), dim3(Bl), 0, 0, cell_dim, have_dropout_mask, num_rows, input, input_stride, params, params_stride, output_deriv, output_deriv_stride, deriv_sum_in, deriv_sum_in_stride, self_repair_config, count, input_deriv, input_deriv_stride, params_deriv, params_deriv_stride, value_sum_out, value_sum_out_stride, deriv_sum_out, deriv_sum_out_stride, self_repair_sum_out, self_repair_sum_out_stride); } void cudaD_copy_cols_from_vec(dim3 Gr, dim3 Bl, double *mat_out, MatrixDim d_out, const double *v_in) { hipLaunchKernelGGL(( _copy_cols_from_vec), dim3(Gr), dim3(Bl), 0, 0, mat_out, d_out, v_in); } void cudaF_copy_cols_from_vec(dim3 Gr, dim3 Bl, float *mat_out, MatrixDim d_out, const float *v_in) { hipLaunchKernelGGL(( _copy_cols_from_vec), dim3(Gr), dim3(Bl), 0, 0, mat_out, d_out, v_in); } void cudaF_diff_normalize_per_row(size_t Gr, size_t Bl, float *id, int id_stride, const float *iv, MatrixDim iv_dim, const float* od, int od_stride, float target_rms, bool add_log_stddev) { hipLaunchKernelGGL(( _diff_normalize_per_row), dim3(Gr), dim3(Bl), 0, 0, id, id_stride, iv, iv_dim, od, od_stride, target_rms, add_log_stddev); } void cudaD_diff_normalize_per_row(size_t Gr, size_t Bl, double *id, int id_stride, const double *iv, MatrixDim iv_dim, const double* od, int od_stride, double target_rms, bool add_log_stddev) { hipLaunchKernelGGL(( _diff_normalize_per_row), dim3(Gr), dim3(Bl), 0, 0, id, id_stride, iv, iv_dim, od, od_stride, target_rms, add_log_stddev); } void cudaD_select_rows(dim3 Gr, dim3 Bl, const int* out_row_ptr, int* out_col_idx, double* out_val, const int* row_indexes, const int num_selected_rows, const int* in_row_ptr, const int* in_col_idx, const double* in_val) { hipLaunchKernelGGL(( _select_rows), dim3(Gr), dim3(Bl), 0, 0, out_row_ptr, out_col_idx, out_val, row_indexes, num_selected_rows, in_row_ptr, in_col_idx, in_val); } void cudaF_select_rows(dim3 Gr, dim3 Bl, const int* out_row_ptr, int* out_col_idx, float* out_val, const int* row_indexes, const int num_selected_rows, const int* in_row_ptr, const int* in_col_idx, const float* in_val) { hipLaunchKernelGGL(( _select_rows), dim3(Gr), dim3(Bl), 0, 0, out_row_ptr, out_col_idx, out_val, row_indexes, num_selected_rows, in_row_ptr, in_col_idx, in_val); } void cudaD_add_smat(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, double alpha, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { hipLaunchKernelGGL(( _add_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaF_add_smat(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, float alpha, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { hipLaunchKernelGGL(( _add_smat), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaD_add_smat_trans(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, double alpha, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { hipLaunchKernelGGL(( _add_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaF_add_smat_trans(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, float alpha, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { hipLaunchKernelGGL(( _add_smat_trans), dim3(Gr), dim3(Bl), 0, 0, mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaD_apply_exp_special(dim3 Gr, dim3 Bl, double* out, MatrixDim out_dim, const double* in, int in_stride) { hipLaunchKernelGGL(( _apply_exp_special), dim3(Gr), dim3(Bl), 0, 0, out, out_dim, in, in_stride); } void cudaF_apply_exp_special(dim3 Gr, dim3 Bl, float* out, MatrixDim out_dim, const float* in, int in_stride) { hipLaunchKernelGGL(( _apply_exp_special), dim3(Gr), dim3(Bl), 0, 0, out, out_dim, in, in_stride); } void cuda_compress_uint8_sign(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, unsigned char *dest, int dest_stride) { hipLaunchKernelGGL(( _cuda_compress_uint8_sign), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride); } void cuda_compress_int16(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, int16_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { hipLaunchKernelGGL(( _cuda_compress_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } else { hipLaunchKernelGGL(( _cuda_compress_no_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } } void cuda_compress_uint16(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, uint16_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { hipLaunchKernelGGL(( _cuda_compress_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } else { hipLaunchKernelGGL(( _cuda_compress_no_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } } void cuda_compress_int8(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, int8_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { hipLaunchKernelGGL(( _cuda_compress_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } else { hipLaunchKernelGGL(( _cuda_compress_no_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } } void cuda_compress_uint8(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, uint8_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { hipLaunchKernelGGL(( _cuda_compress_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } else { hipLaunchKernelGGL(( _cuda_compress_no_bounds_check), dim3(Gr), dim3(Bl), 0, 0, src, dim, dest, dest_stride, inv_scale); } } void cuda_uncompress_uint8(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const uint8_t *src, int src_stride, float scale) { hipLaunchKernelGGL(( _cuda_uncompress), dim3(Gr), dim3(Bl), 0, 0, dest, dim, src, src_stride, scale); } void cuda_uncompress_int8(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const int8_t *src, int src_stride, float scale) { hipLaunchKernelGGL(( _cuda_uncompress), dim3(Gr), dim3(Bl), 0, 0, dest, dim, src, src_stride, scale); } void cuda_uncompress_uint16(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const uint16_t *src, int src_stride, float scale) { hipLaunchKernelGGL(( _cuda_uncompress), dim3(Gr), dim3(Bl), 0, 0, dest, dim, src, src_stride, scale); } void cuda_uncompress_int16(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const int16_t *src, int src_stride, float scale) { hipLaunchKernelGGL(( _cuda_uncompress), dim3(Gr), dim3(Bl), 0, 0, dest, dim, src, src_stride, scale); } /* * All the following kernels are written by Wei Deng for LSTM LM and convolution neural network training */ template<typename Real> __global__ static void _convolution_forward_expand_workspace( Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int filt_x_len_, int filt_y_len_, int filt_x_step_, int filt_y_step_, int connect_fmap) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda batchsize = srcdim.rows; int32_cuda out_fmap_index = i/batchsize; int32_cuda batch_index = i%batchsize; int32_cuda index = i * dstdim.stride + j; int32_cuda fmap_m, fmap_n, filt_i, filt_j, src_frame_index, src_index, st; if (i < dstdim.rows && j < dstdim.cols) { //fmap_m = out_fmap_index / (fmap_y_len_-filt_y_len_+1) * filt_x_step_; //fmap_n = out_fmap_index % (fmap_y_len_-filt_y_len_+1) * filt_y_step_; fmap_m = out_fmap_index / ((fmap_y_len_-filt_y_len_) * filt_y_step_+1) * filt_x_step_; fmap_n = out_fmap_index % ((fmap_y_len_-filt_y_len_) * filt_y_step_+1) * filt_y_step_; if (connect_fmap == 1) { st = (fmap_m * fmap_y_len_ + fmap_n) * num_input_fmaps; } else { st = fmap_m * fmap_y_len_ * num_input_fmaps + fmap_n; } filt_i = j/(filt_y_len_*num_input_fmaps); filt_j = j%(filt_y_len_*num_input_fmaps); if (connect_fmap == 1) { src_frame_index = st + filt_i*(num_input_fmaps*fmap_y_len_) + filt_j; } else { src_frame_index = st + filt_i * (num_input_fmaps * fmap_y_len_) + (filt_j / num_input_fmaps) + (filt_j % num_input_fmaps) * fmap_y_len_; } src_index = src_frame_index + batch_index*srcdim.stride; dst[index] = src[src_index]; } } template<typename Real> __global__ static void _convolution_backward_shrink_workspace(Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, const Int32Pair* const* map, const int32_cuda *mapsize) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda batchsize = dstdim.rows; int32_cuda k, src_index; int32_cuda index = i * dstdim.stride + j; if (i < dstdim.rows && j < dstdim.cols) { for (k = 0; k < mapsize[j]; k++) { src_index = (map[j][k].first*batchsize + i) * srcdim.stride + map[j][k].second; dst[index] += src[src_index]; } } } template<typename Real> __global__ static void _max_pooling_forward(Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda out_fmap_index = j/num_input_fmaps; int32_cuda filt_index = j%num_input_fmaps; int32_cuda fmap_m, fmap_n, st, x, y, c, src_index, dst_index, col; dst_index = i * dstdim.stride + j; if (i < dstdim.rows && j < dstdim.cols) { //fmap_m = out_fmap_index / (fmap_y_len_-pool_y_len_+1) * pool_x_step_; //fmap_n = out_fmap_index % (fmap_y_len_-pool_y_len_+1) * pool_y_step_; col = (fmap_y_len_ - pool_y_len_)/pool_y_step_+1; fmap_m = out_fmap_index / col * pool_x_step_; fmap_n = out_fmap_index % col * pool_y_step_; st = (fmap_m * fmap_y_len_ + fmap_n) * num_input_fmaps + filt_index; for (x = 0; x < pool_x_len_; x++) { for (y = 0; y < pool_y_len_; y++) { //c = st + x * (num_input_fmaps * fmap_y_len_) + y * num_input_fmaps; c = st + (x * fmap_y_len_ + y) * num_input_fmaps; src_index = i * srcdim.stride + c; if (src[src_index] > dst[dst_index]) dst[dst_index] = src[src_index]; } } } } template<typename Real> __global__ static void _max_pooling_backward(Real *dst, const Real *in, const Real *out, const Real *out_diff, MatrixDim dstdim, MatrixDim indim, MatrixDim outdim, MatrixDim outdiffdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda out_fmap_index = j/num_input_fmaps; int32_cuda filt_index = j%num_input_fmaps; int32_cuda fmap_m, fmap_n, st, x, y, c, in_index, dst_index, out_index, out_diff_index, col; out_index = i * outdim.stride + j; out_diff_index = i * outdiffdim.stride + j; if (i < dstdim.rows && j < dstdim.cols) { col = (fmap_y_len_ - pool_y_len_)/pool_y_step_+1; fmap_m = out_fmap_index / col * pool_x_step_; fmap_n = out_fmap_index % col * pool_y_step_; st = (fmap_m * fmap_y_len_ + fmap_n) * num_input_fmaps + filt_index; for (x = 0; x < pool_x_len_; x++) { for (y = 0; y < pool_y_len_; y++) { //c = st + x * (num_input_fmaps * fmap_y_len_) + y * num_input_fmaps; c = st + (x * fmap_y_len_ + y) * num_input_fmaps; in_index = i * indim.stride + c; dst_index = i * dstdim.stride + c; if (out[out_index] == in[in_index]) dst[dst_index] += out_diff[out_diff_index]; } } } } template<typename Real> __global__ static void _pad_feature_map(Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len, int fmap_y_len, int pad_x_len, int pad_y_len, int pad_wipe, int connect_fmap) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda fmap_index, fmap_offset, out_fmap_offset, src_index, dst_index, fmap_x, fmap_y, out_fmap_y_len, out_j; out_fmap_y_len = fmap_y_len+2*pad_y_len; //fmap_size = fmap_x_len*fmap_y_len; //out_fmap_size = out_fmap_x_len*out_fmap_y_len; // fmap_index = connect_fmap == 1 ? j%num_input_fmaps : j/fmap_size; // fmap_offset = connect_fmap == 1 ? j/num_input_fmaps : j%fmap_size; if (connect_fmap == 1) { fmap_index = j%num_input_fmaps; fmap_offset = j/num_input_fmaps; fmap_x = fmap_offset/fmap_y_len; fmap_y = fmap_offset%fmap_y_len; out_fmap_offset = (fmap_x+pad_x_len)*out_fmap_y_len + (fmap_y+pad_y_len); //out_j = connect_fmap == 1 ? out_fmap_offset*num_input_fmaps+fmap_index : fmap_index*out_fmap_size+out_fmap_offset; out_j = out_fmap_offset*num_input_fmaps+fmap_index; } else { fmap_x = j/(fmap_y_len*num_input_fmaps); fmap_y = j%(fmap_y_len*num_input_fmaps); fmap_index = fmap_y/fmap_y_len; fmap_offset = fmap_y%fmap_y_len; out_fmap_offset = fmap_index*out_fmap_y_len + (fmap_offset+pad_y_len); out_j = (fmap_x+pad_x_len)*(out_fmap_y_len*num_input_fmaps) + out_fmap_offset; } src_index = pad_wipe == 0 ? i*srcdim.stride+j : i*srcdim.stride + out_j; dst_index = pad_wipe == 0 ? i*dstdim.stride+out_j : i*dstdim.stride+j; if (i < srcdim.rows && i < dstdim.rows && j < srcdim.cols && j < dstdim.cols) dst[dst_index] = src[src_index]; } template<typename Real> __global__ static void _sum_mats(Real* dst, Real **src_array, MatrixDim dstdim, MatrixDim srcdim, int batchcount) { int m = blockIdx.x, n = blockIdx.y; int THREADS = blockDim.x; if (m >= dstdim.rows) return; __shared__ Real aux[CU1DBLOCK*2]; int steps = (batchcount - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = src_array[threadIdx.x][m*srcdim.stride+n]; for(int i=1; i<steps; ++i) { if(threadIdx.x+i*THREADS < batchcount) aux[threadIdx.x] += src_array[i*THREADS+threadIdx.x][m*srcdim.stride+n]; } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); if (threadIdx.x == 0) dst[m*dstdim.stride+n] = aux[0]; } template<typename Real> __global__ static void _copy_col_mats(Real* dst, Real **src_array, MatrixDim dst_dim, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index, src_index, dst_index; if (i < dst_dim.rows && j < dst_dim.cols) { index = j/src_dim.cols; dst_index = i * dst_dim.stride + j; src_index = i * src_dim.stride + j%src_dim.cols; if (index >= 0) dst[dst_index] = src_array[index][src_index]; else dst[dst_index] = 0; } } template<typename Real> __global__ static void _copy_row_mats(Real* dst, Real **src_array, MatrixDim dst_dim, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index, src_index, dst_index; if (i < dst_dim.rows && j < dst_dim.cols) { index = i/src_dim.rows; dst_index = i * dst_dim.stride + j; src_index = (i%src_dim.rows) * src_dim.stride + j; if (index >= 0) dst[dst_index] = src_array[index][src_index]; else dst[dst_index] = 0; } } template<typename Real> __global__ static void _row_sum_reduce(Real alpha, Real *y, const Real *x, Real beta, MatrixDim d) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = x[threadIdx.x+j*d.stride]; for(int i=1; i<steps; i++) { if(threadIdx.x+i*THREADS < d.cols) { aux[threadIdx.x] += x[threadIdx.x+i*THREADS+j*d.stride]; } } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); y[j] = beta*y[j] + alpha*aux[0]; } template<typename Real> __global__ static void _col_sum_reduce(Real alpha, Real *y, const Real *x, Real beta, MatrixDim d) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.cols) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.rows - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = x[threadIdx.x*d.stride+j]; for(int i=1; i<steps; i++) { if(threadIdx.x+i*THREADS < d.rows) { aux[threadIdx.x] += x[(threadIdx.x+i*THREADS)*d.stride+j]; } } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); y[j] = beta*y[j] + alpha*aux[0]; } template<typename Real> __global__ static void _row_max_id(int32_cuda *vec_id, const Real *x, MatrixDim d) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real value[CU1DBLOCK]; __shared__ int32_cuda index[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; //copy input to aux value[threadIdx.x] = x[threadIdx.x+j*d.stride]; index[threadIdx.x] = threadIdx.x; for(int i=1; i<steps; ++i) { if(threadIdx.x+i*THREADS < d.cols && value[threadIdx.x] < x[threadIdx.x+i*THREADS+j*d.stride]) { value[threadIdx.x] = x[threadIdx.x+i*THREADS+j*d.stride]; index[threadIdx.x] = threadIdx.x+i*THREADS; } } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads && value[threadIdx.x] < value[threadIdx.x+halfPoint]) { value[threadIdx.x] = value[threadIdx.x + halfPoint]; index[threadIdx.x] = index[threadIdx.x + halfPoint]; } } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); vec_id[j] = index[0]; } template<typename Real> __global__ static void _gen_tgt(Real *x, const int32_cuda *vec_id, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // column index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row index int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) x[index] = (i==vec_id[j] ? 1.0 : 0.0); } template<typename Real> __global__ static void _copy_row_to_vecid(const Real *mat, MatrixIndexT_cuda *vec_id, MatrixDim dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index if (i < dim.cols) vec_id[i] = (MatrixIndexT_cuda)mat[i]; } template<typename Real> __global__ static void _add_mat_to_rows(Real alpha, Real *dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[j]; int dst_index = index * dst_dim.stride + i; int src_index = j * src_dim.stride + i; dst[dst_index] += index >= 0 ? alpha*src[src_index] : 0; } } template<typename Real> __global__ static void _cross_entropy(Real *xentropy, const Real *nnetout, const Real *target, MatrixDim dst_d, int out_stride, int tgt_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j*dst_d.stride, out_index = i + j*out_stride, tgt_index = i + j*tgt_stride; if (i < dst_d.cols && j < dst_d.rows) { xentropy[dst_index] = nnetout[out_index] + 1e-20; xentropy[dst_index] = log(xentropy[dst_index]); xentropy[dst_index] *= target[tgt_index]; } } template<typename Real> __global__ static void _entropy(Real *entropy, const Real *mat, MatrixDim dst_d, int mat_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j*dst_d.stride, mat_index = i + j*mat_stride; if (i < dst_d.cols && j < dst_d.rows) { entropy[dst_index] = mat[mat_index] + 1e-20; entropy[dst_index] = log(entropy[dst_index]); entropy[dst_index] *= mat[mat_index]; } } void cudaF_convolution_forward_expand_workspace(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int filt_x_len_, int filt_y_len_, int filt_x_step_, int filt_y_step_, int connect_fmap) { hipLaunchKernelGGL(( _convolution_forward_expand_workspace), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_,filt_x_len_, filt_y_len_, filt_x_step_, filt_y_step_, connect_fmap); } void cudaF_convolution_backward_shrink_workspace(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, const Int32Pair* const* map, const int32_cuda *mapsize) { hipLaunchKernelGGL(( _convolution_backward_shrink_workspace), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, map, mapsize); } void cudaF_max_pooling_forward(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { hipLaunchKernelGGL(( _max_pooling_forward), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaF_max_pooling_backward(dim3 Gr, dim3 Bl, float *dst, const float *in, const float *out, const float *out_diff, MatrixDim dstdim, MatrixDim indim, MatrixDim outdim, MatrixDim outdiffdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { hipLaunchKernelGGL(( _max_pooling_backward), dim3(Gr),dim3(Bl), 0, 0, dst, in, out, out_diff, dstdim, indim, outdim, outdiffdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaF_pad_feature_map(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len, int fmap_y_len, int pad_x_len, int pad_y_len, int pad_wipe, int connect_fmap) { hipLaunchKernelGGL(( _pad_feature_map), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len, fmap_y_len, pad_x_len, pad_y_len, pad_wipe, connect_fmap); } void cudaF_sum_mats(dim3 Gr, dim3 Bl, float* dst, float **src_array, MatrixDim dstdim, MatrixDim srcdim, int batchcount) { hipLaunchKernelGGL(( _sum_mats), dim3(Gr),dim3(Bl), 0, 0, dst, src_array, dstdim, srcdim, batchcount); } void cudaF_copy_col_mats(dim3 Gr, dim3 Bl, float* dst, float **src_array, MatrixDim dst_dim, MatrixDim src_dim) { hipLaunchKernelGGL(( _copy_col_mats), dim3(Gr),dim3(Bl), 0, 0, dst, src_array, dst_dim, src_dim); } void cudaF_copy_row_mats(dim3 Gr, dim3 Bl, float* dst, float **src_array, MatrixDim dst_dim, MatrixDim src_dim) { hipLaunchKernelGGL(( _copy_row_mats), dim3(Gr),dim3(Bl), 0, 0, dst, src_array, dst_dim, src_dim); } void cudaF_cross_entropy(dim3 Gr, dim3 Bl, float *xentropy, const float *nnetout, const float *target, MatrixDim d, int out_stride, int tgt_stride, hipStream_t s) { hipLaunchKernelGGL(( _cross_entropy), dim3(Gr),dim3(Bl),0,s, xentropy,nnetout,target,d,out_stride,tgt_stride); } void cudaF_entropy(dim3 Gr, dim3 Bl, float *entropy, const float *mat, MatrixDim d, int mat_stride, hipStream_t s) { hipLaunchKernelGGL(( _entropy), dim3(Gr),dim3(Bl),0,s, entropy,mat,d,mat_stride); } void cudaF_row_sum_reduce(size_t Gr, size_t Bl, float alpha, float *y, const float *x, MatrixDim d, float beta, hipStream_t s) { hipLaunchKernelGGL(( _row_sum_reduce), dim3(Gr),dim3(Bl),0,s, alpha,y,x,beta,d); } void cudaF_col_sum_reduce(size_t Gr, size_t Bl, float alpha, float *y, const float *x, MatrixDim d, float beta, hipStream_t s) { hipLaunchKernelGGL(( _col_sum_reduce), dim3(Gr),dim3(Bl),0,s, alpha,y,x,beta,d); } void cudaD_vec_apply_fixed(int Gr, int Bl, double* v, double resolution, int mode, int dim){ hipLaunchKernelGGL(( _vec_apply_fixed), dim3(Gr),dim3(Bl), 0, 0, v, resolution, mode, dim); } void cudaF_row_max_id(size_t Gr, size_t Bl, int32_cuda *vec_id, const float *x, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _row_max_id), dim3(Gr),dim3(Bl),0,s, vec_id,x,d); } void cudaF_gen_tgt(dim3 Gr, dim3 Bl, float *x, const int32_cuda *vec_id, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _gen_tgt), dim3(Gr),dim3(Bl),0,s, x, vec_id, d); } void cudaF_copy_row_to_vecid(dim3 Gr, dim3 Bl, const float *mat, MatrixIndexT_cuda *vec_id, MatrixDim dim) { hipLaunchKernelGGL(( _copy_row_to_vecid), dim3(Gr), dim3(Bl), 0, 0, mat, vec_id, dim); } void cudaF_add_mat_to_rows(dim3 Gr, dim3 Bl, float alpha, float *dst, const float *src, const MatrixIndexT_cuda *vec_id, MatrixDim dst_dim, MatrixDim src_dim) { hipLaunchKernelGGL(( _add_mat_to_rows), dim3(Gr), dim3(Bl), 0, 0, alpha, dst, src, vec_id, dst_dim, src_dim); } /* * double */ void cudaD_convolution_forward_expand_workspace(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int filt_x_len_, int filt_y_len_, int filt_x_step_, int filt_y_step_, int connect_fmap) { hipLaunchKernelGGL(( _convolution_forward_expand_workspace), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_,filt_x_len_, filt_y_len_, filt_x_step_, filt_y_step_, connect_fmap); } void cudaD_convolution_backward_shrink_workspace(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, const Int32Pair* const* map, const int32_cuda *mapsize) { hipLaunchKernelGGL(( _convolution_backward_shrink_workspace), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, map, mapsize); } void cudaD_max_pooling_forward(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { hipLaunchKernelGGL(( _max_pooling_forward), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaD_max_pooling_backward(dim3 Gr, dim3 Bl, double *dst, const double *in, const double *out, const double *out_diff, MatrixDim dstdim, MatrixDim indim, MatrixDim outdim, MatrixDim outdiffdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { hipLaunchKernelGGL(( _max_pooling_backward), dim3(Gr),dim3(Bl), 0, 0, dst, in, out, out_diff, dstdim, indim, outdim, outdiffdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaD_pad_feature_map(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len, int fmap_y_len, int pad_x_len, int pad_y_len, int pad_wipe, int connect_fmap) { hipLaunchKernelGGL(( _pad_feature_map), dim3(Gr),dim3(Bl), 0, 0, dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len, fmap_y_len, pad_x_len, pad_y_len, pad_wipe, connect_fmap); } void cudaD_sum_mats(dim3 Gr, dim3 Bl, double* dst, double **src_array, MatrixDim dstdim, MatrixDim srcdim, int batchcount) { hipLaunchKernelGGL(( _sum_mats), dim3(Gr),dim3(Bl), 0, 0, dst, src_array, dstdim, srcdim, batchcount); } void cudaD_copy_col_mats(dim3 Gr, dim3 Bl, double* dst, double **src_array, MatrixDim dst_dim, MatrixDim src_dim) { hipLaunchKernelGGL(( _copy_col_mats), dim3(Gr),dim3(Bl), 0, 0, dst, src_array, dst_dim, src_dim); } void cudaD_copy_row_mats(dim3 Gr, dim3 Bl, double* dst, double **src_array, MatrixDim dst_dim, MatrixDim src_dim) { hipLaunchKernelGGL(( _copy_row_mats), dim3(Gr),dim3(Bl), 0, 0, dst, src_array, dst_dim, src_dim); } void cudaD_row_sum_reduce(size_t Gr, size_t Bl, double alpha, double *y, const double *x, MatrixDim d, double beta, hipStream_t s) { hipLaunchKernelGGL(( _row_sum_reduce), dim3(Gr),dim3(Bl),0,s, alpha,y,x,beta,d); } void cudaD_col_sum_reduce(size_t Gr, size_t Bl, double alpha, double *y, const double *x, MatrixDim d, double beta, hipStream_t s) { hipLaunchKernelGGL(( _col_sum_reduce), dim3(Gr),dim3(Bl),0,s, alpha,y,x,beta,d); } void cudaD_row_max_id(size_t Gr, size_t Bl, int32_cuda *vec_id, const double *x, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _row_max_id), dim3(Gr),dim3(Bl),0,s, vec_id,x,d); } void cudaD_gen_tgt(dim3 Gr, dim3 Bl, double *x, const int32_cuda *vec_id, MatrixDim d, hipStream_t s) { hipLaunchKernelGGL(( _gen_tgt), dim3(Gr),dim3(Bl),0,s, x, vec_id, d); } void cudaD_copy_row_to_vecid(dim3 Gr, dim3 Bl, const double *mat, MatrixIndexT_cuda *vec_id, MatrixDim dim) { hipLaunchKernelGGL(( _copy_row_to_vecid), dim3(Gr), dim3(Bl), 0, 0, mat, vec_id, dim); } void cudaD_add_mat_to_rows(dim3 Gr, dim3 Bl, double alpha, double *dst, const double *src, const MatrixIndexT_cuda *vec_id, MatrixDim dst_dim, MatrixDim src_dim) { hipLaunchKernelGGL(( _add_mat_to_rows), dim3(Gr), dim3(Bl), 0, 0, alpha, dst, src, vec_id, dst_dim, src_dim); } void cudaD_cross_entropy(dim3 Gr, dim3 Bl, double *xentropy, const double *nnetout, const double *target, MatrixDim d, int out_stride, int tgt_stride, hipStream_t s) { hipLaunchKernelGGL(( _cross_entropy), dim3(Gr),dim3(Bl),0,s, xentropy,nnetout,target,d,out_stride,tgt_stride); } void cudaD_entropy(dim3 Gr, dim3 Bl, double *entropy, const double *mat, MatrixDim d, int mat_stride, hipStream_t s) { hipLaunchKernelGGL(( _entropy), dim3(Gr),dim3(Bl),0,s, entropy,mat,d,mat_stride); } /* * All the following kernels are written by Yajie Miao for CTC training */ #include "cuPrintf_hip.cuh" #include "cuPrintf.hip" #include "ctc-utils.h" template<typename Real> __global__ static void _compute_ctc_alpha_one_sequence(Real* mat_alpha, int row, MatrixDim dim_alpha, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_alpha.cols; if (i < dim) { int32_cuda index_alpha = i + row * dim_alpha.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_alpha_rm1_i = i + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im1 = (i - 1) + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im2 = (i - 2) + (row - 1) * dim_alpha.stride; if (row == 0) { if (i < 2) mat_alpha[index_alpha] = mat_prob[index_prob]; else mat_alpha[index_alpha] = NumericLimits<Real>::log_zero_; } else { if (i > 1) { if (i % 2 == 0 || labels[i-2] == labels[i]) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { Real tmp = LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i]); mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im2], tmp)); } } else if (i == 1) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], mat_alpha[index_alpha_rm1_i]); } } } } template<typename Real> __global__ static void _compute_ctc_alpha_multiple_sequence(Real* mat_alpha, int sequence_num, int row, MatrixDim dim_alpha, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels, int32_cuda dim_label_stride, const int32_cuda* seq_lengths) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index, that is, the index for sequence int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // label index, cannot exceed 2*|l|+1 int32_cuda dim = dim_alpha.cols; if (j >= dim || i >= sequence_num) return; int32_cuda index_alpha = j + (row * sequence_num + i) * dim_alpha.stride; int32_cuda index_label = j + i * dim_label_stride; int32_cuda class_idx = labels[index_label];// if -1, this is the padding cell;labels now is a matrix which has the same size as mat_alpha if (class_idx == -1 || row >= seq_lengths[i]) { mat_alpha[index_alpha] = NumericLimits<Real>::log_zero_; return; } int32_cuda index_label_m2 = (j-2) + i * dim_label_stride; int32_cuda index_prob = class_idx + (row * sequence_num + i) * dim_prob.stride; int32_cuda index_alpha_rm1_i = j + ((row-1) * sequence_num + i) * dim_alpha.stride; int32_cuda index_alpha_rm1_im1 = (j-1) + ((row-1) * sequence_num + i) * dim_alpha.stride; int32_cuda index_alpha_rm1_im2 = (j-2) + ((row-1) * sequence_num + i) * dim_alpha.stride; if (row == 0) { if (j < 2) mat_alpha[index_alpha] = mat_prob[index_prob]; else mat_alpha[index_alpha] = NumericLimits<Real>::log_zero_; } else { if (j > 1) { if (j % 2 == 0 || labels[index_label_m2] == labels[index_label]) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { Real tmp = LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i]); mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im2], tmp)); } } else if (j == 1) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], mat_alpha[index_alpha_rm1_i]); } } } template<typename Real> __global__ static void _compute_ctc_alpha_one_sequence_rescale(Real* mat_alpha, int row, MatrixDim dim_alpha, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_alpha.cols; if (i < dim) { int32_cuda index_alpha = i + row * dim_alpha.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_alpha_rm1_i = i + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im1 = (i - 1) + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im2 = (i - 2) + (row - 1) * dim_alpha.stride; if (row == 0) { if (i < 2) mat_alpha[index_alpha] = mat_prob[index_prob]; else mat_alpha[index_alpha] = 0.0; } else { if (i > 1) { if (i % 2 == 0 || labels[i-2] == labels[i]) { mat_alpha[index_alpha] = mat_prob[index_prob] * (mat_alpha[index_alpha_rm1_im1] + mat_alpha[index_alpha_rm1_i]); } else { mat_alpha[index_alpha] = mat_prob[index_prob] * (mat_alpha[index_alpha_rm1_im1] + mat_alpha[index_alpha_rm1_i] + mat_alpha[index_alpha_rm1_im2]); } } else if (i == 1) { mat_alpha[index_alpha] = mat_prob[index_prob] * (mat_alpha[index_alpha_rm1_im1] + mat_alpha[index_alpha_rm1_i]); } else { mat_alpha[index_alpha] = mat_prob[index_prob] * mat_alpha[index_alpha_rm1_i]; } } } } template<typename Real> __global__ static void _compute_ctc_beta_one_sequence(Real* mat_beta, int row, MatrixDim dim_beta, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_beta.cols; if (i < dim) { int32_cuda index_beta = i + row * dim_beta.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_beta_rp1_i = i + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip1 = (i + 1) + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip2 = (i + 2) + (row + 1) * dim_beta.stride; int32_cuda row_num = dim_beta.rows; if (row == row_num - 1) { if (i > dim - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = NumericLimits<Real>::log_zero_; } else { if (i < dim - 2) { if (i % 2 == 0 || labels[i+2] == labels[i]) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { Real tmp = LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i]); mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip2], tmp)); } } else if (i == dim - 2) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { mat_beta[index_beta] = AddAB(mat_prob[index_prob], mat_beta[index_beta_rp1_i]); } } } } template<typename Real> __global__ static void _compute_ctc_beta_multiple_sequence(Real* mat_beta, int sequence_num, int row, MatrixDim dim_beta, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels, int32_cuda dim_label_stride, const int32_cuda* seq_lengths, const int32_cuda* label_lengths) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index, that is, the index for sequence int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // label index, cannot exceed 2*|l|+1 int32_cuda dim = dim_beta.cols; if (j >= dim || i >= sequence_num) return; int32_cuda index_beta = j + (row * sequence_num + i) * dim_beta.stride; int32_cuda index_label = j + i * dim_label_stride; int32_cuda class_idx = labels[index_label];// if -1, this is the padding cell;labels now is a matrix which has the same size as mat_alpha if (class_idx == -1 || row >= seq_lengths[i]) { mat_beta[index_beta] = NumericLimits<Real>::log_zero_; return; } int32_cuda index_label_p2 = (j+2) + i * dim_label_stride; int32_cuda index_prob = class_idx + (row * sequence_num + i) * dim_prob.stride; int32_cuda index_beta_rp1_i = j + ((row+1) * sequence_num + i) * dim_beta.stride; int32_cuda index_beta_rp1_ip1 = (j+1) + ((row+1) * sequence_num + i) * dim_beta.stride; int32_cuda index_beta_rp1_ip2 = (j+2) + ((row+1) * sequence_num + i) * dim_beta.stride; int32_cuda row_num = seq_lengths[i]; int32_cuda label_len = label_lengths[i]; /* if (row == row_num - 1) { if (j > dim - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = NumericLimits<Real>::log_zero_; } else { if (j < dim - 2) { if (j % 2 == 0 || labels[index_label_p2] == labels[index_label]) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { Real tmp = LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i]); mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip2], tmp)); } } else if (j == dim - 2) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { mat_beta[index_beta] = AddAB(mat_prob[index_prob], mat_beta[index_beta_rp1_i]); } } */ if (row == row_num - 1) { if (j > label_len - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = NumericLimits<Real>::log_zero_; } else { if (j < label_len - 2) { if (j % 2 == 0 || labels[index_label_p2] == labels[index_label]) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { Real tmp = LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i]); mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip2], tmp)); } } else if (j == label_len - 2) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { mat_beta[index_beta] = AddAB(mat_prob[index_prob], mat_beta[index_beta_rp1_i]); } } } template<typename Real> __global__ static void _compute_ctc_beta_one_sequence_rescale(Real* mat_beta, int row, MatrixDim dim_beta, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_beta.cols; if (i < dim) { int32_cuda index_beta = i + row * dim_beta.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_beta_rp1_i = i + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip1 = (i + 1) + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip2 = (i + 2) + (row + 1) * dim_beta.stride; int32_cuda row_num = dim_beta.rows; if (row == row_num - 1) { if (i > dim - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = 0; } else { if (i < dim - 2) { if (i % 2 == 0 || labels[i+2] == labels[i]) { mat_beta[index_beta] = mat_prob[index_prob] * (mat_beta[index_beta_rp1_ip1] + mat_beta[index_beta_rp1_i]); } else { mat_beta[index_beta] = mat_prob[index_prob] * (mat_beta[index_beta_rp1_ip1] + mat_beta[index_beta_rp1_i] + mat_beta[index_beta_rp1_ip2]); } } else if (i == dim - 2) { mat_beta[index_beta] = mat_prob[index_prob] * (mat_beta[index_beta_rp1_ip1] + mat_beta[index_beta_rp1_i]); } else { mat_beta[index_beta] = mat_prob[index_prob] * mat_beta[index_beta_rp1_i]; } } } } // mat_prob are in probability scale. template<typename Real> __global__ static void _compute_ctc_error_one_sequence(Real* mat_error, MatrixDim dim_error, const Real* mat_alpha, const Real* mat_beta, MatrixDim dim_alpha, const Real* mat_prob, const int32_cuda* labels, Real pzx) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i < dim_error.rows && j < dim_error.cols) { Real err = NumericLimits<Real>::log_zero_; int32_cuda index_error = i * dim_error.stride + j; for(int s = 0; s < dim_alpha.cols; s++) { if (labels[s] == j) { // int32_cuda index_alpha = i * dim_alpha.stride + s; err = LogAPlusB(err, AddAB(mat_alpha[index_alpha], mat_beta[index_alpha])); } } Real val = ExpA(SubAB(err, AddAB(pzx, mat_prob[index_error] == 0? NumericLimits<Real>::log_zero_ : 2*log(mat_prob[index_error])))); mat_error[index_error] = -1.0 * val; } } // mat_prob are in probability scale. template<typename Real> __global__ static void _compute_ctc_error_multiple_sequence(Real* mat_error, int32_cuda sequence_num, MatrixDim dim_error, const Real* mat_alpha, const Real* mat_beta, MatrixDim dim_alpha, const Real* mat_prob, const int32_cuda* labels, int32_cuda dim_label_stride, const int32_cuda* seq_lengths, const Real* pzx) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i >= dim_error.rows || j >= dim_error.cols) return; int32_cuda seqX = i % sequence_num; int32_cuda rowX = i / sequence_num; if (rowX >= seq_lengths[seqX]) return; Real err = NumericLimits<Real>::log_zero_; int32_cuda index_error = i * dim_error.stride + j; for(int s = 0; s < dim_alpha.cols; s++) { int32_cuda index_label = s + seqX * dim_label_stride; if (labels[index_label] == -1) {continue;} if (labels[index_label] == j) { // int32_cuda index_alpha = i * dim_alpha.stride + s; err = LogAPlusB(err, AddAB(mat_alpha[index_alpha], mat_beta[index_alpha])); } } Real val = ExpA(SubAB(err, AddAB(pzx[seqX], mat_prob[index_error] == 0? NumericLimits<Real>::log_zero_ : 2*log(mat_prob[index_error])))); mat_error[index_error] = -1.0 * val; } template<typename Real> __global__ static void _distribute_prob_by_label(Real* mat_prob_dist, MatrixDim dim_prob_dist, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i < dim_prob_dist.rows && j < dim_prob_dist.cols) { int32_cuda index_prob_dist = i * dim_prob_dist.stride + j; int32_cuda index_prob = i * dim_prob.stride + labels[j]; mat_prob_dist[index_prob_dist] = mat_prob[index_prob]; } } // directly get the errors for the prior-softmax values template<typename Real> __global__ static void _compute_ctc_error_one_sequence_rescale(Real* mat_error, MatrixDim dim_error, const Real* mat_alpha, const Real* mat_beta, MatrixDim dim_alpha, const Real* mat_prob, const int32_cuda* labels, const Real* zt) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i < dim_error.rows && j < dim_error.cols) { Real err = 0; int32_cuda index_error = i * dim_error.stride + j; for(int s = 0; s < dim_alpha.cols; s++) { if (labels[s] == j) { // int32_cuda index_alpha = i * dim_alpha.stride + s; err += mat_alpha[index_alpha] * mat_beta[index_alpha]; } } if (mat_prob[index_error] == 0 || zt[i] == 0) { mat_error[index_error] = 0; } else { mat_error[index_error] = mat_prob[index_error] - (err / zt[i]) / mat_prob[index_error]; } } } void cudaF_compute_ctc_alpha(dim3 Gr, dim3 Bl, float *alpha, int row_idx, MatrixDim dim_alpha, const float *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_alpha_one_sequence), dim3(Gr), dim3(Bl), 0, 0, alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaF_compute_ctc_beta(dim3 Gr, dim3 Bl, float *beta, int row_idx, MatrixDim dim_beta, const float *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_beta_one_sequence), dim3(Gr), dim3(Bl), 0, 0, beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaF_compute_ctc_error(dim3 Gr, dim3 Bl, float *error, MatrixDim dim_error, const float *alpha, const float *beta, MatrixDim dim_alpha, const float *prob, const int *labels, float pzx) { hipLaunchKernelGGL(( _compute_ctc_error_one_sequence), dim3(Gr), dim3(Bl), 0, 0, error, dim_error, alpha, beta, dim_alpha, prob, labels, pzx); } void cudaF_compute_ctc_alpha_rescale(dim3 Gr, dim3 Bl, float *alpha, int row_idx, MatrixDim dim_alpha, const float *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_alpha_one_sequence_rescale), dim3(Gr), dim3(Bl), 0, 0, alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaF_compute_ctc_beta_rescale(dim3 Gr, dim3 Bl, float *beta, int row_idx, MatrixDim dim_beta, const float *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_beta_one_sequence_rescale), dim3(Gr), dim3(Bl), 0, 0, beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaF_compute_ctc_error_rescale(dim3 Gr, dim3 Bl, float *error, MatrixDim dim_error, const float *alpha, const float *beta, MatrixDim dim_alpha, const float *prob, const int *labels, const float *zt) { hipLaunchKernelGGL(( _compute_ctc_error_one_sequence_rescale), dim3(Gr), dim3(Bl), 0, 0, error, dim_error, alpha, beta, dim_alpha, prob, labels, zt); } void cudaF_distribute_prob_by_label(dim3 Gr, dim3 Bl, float *prob_dist, MatrixDim dim_prob_dist, const float *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _distribute_prob_by_label), dim3(Gr), dim3(Bl), 0, 0, prob_dist, dim_prob_dist, prob, dim_prob, labels); } void cudaF_compute_ctc_alpha_multiple_sequence(dim3 Gr, dim3 Bl, float *alpha, int seq_num, int row_idx, MatrixDim dim_alpha, const float *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths) { hipLaunchKernelGGL(( _compute_ctc_alpha_multiple_sequence), dim3(Gr), dim3(Bl), 0, 0, alpha, seq_num, row_idx, dim_alpha, prob, dim_prob, labels, dim_label_stride, seq_lengths); } void cudaF_compute_ctc_beta_multiple_sequence(dim3 Gr, dim3 Bl, float *beta, int seq_num, int row_idx, MatrixDim dim_beta, const float *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths, const int *label_lengths) { hipLaunchKernelGGL(( _compute_ctc_beta_multiple_sequence), dim3(Gr), dim3(Bl), 0, 0, beta, seq_num, row_idx, dim_beta, prob, dim_prob, labels, dim_label_stride, seq_lengths, label_lengths); } void cudaF_compute_ctc_error_multiple_sequence(dim3 Gr, dim3 Bl, float *error, int seq_num, MatrixDim dim_error, const float *alpha, const float *beta, MatrixDim dim_alpha, const float *prob, const int *labels, int dim_label_stride, const int *seq_lengths, const float *pzx) { hipLaunchKernelGGL(( _compute_ctc_error_multiple_sequence), dim3(Gr), dim3(Bl), 0, 0, error, seq_num, dim_error, alpha, beta, dim_alpha, prob, labels, dim_label_stride, seq_lengths, pzx); } void cudaD_compute_ctc_alpha(dim3 Gr, dim3 Bl, double *alpha, int row_idx, MatrixDim dim_alpha, const double *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_alpha_one_sequence), dim3(Gr), dim3(Bl), 0, 0, alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaD_compute_ctc_beta(dim3 Gr, dim3 Bl, double *beta, int row_idx, MatrixDim dim_beta, const double *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_beta_one_sequence), dim3(Gr), dim3(Bl), 0, 0, beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaD_compute_ctc_error(dim3 Gr, dim3 Bl, double *error, MatrixDim dim_error, const double *alpha, const double *beta, MatrixDim dim_alpha, const double *prob, const int *labels, double pzx) { hipLaunchKernelGGL(( _compute_ctc_error_one_sequence), dim3(Gr), dim3(Bl), 0, 0, error, dim_error, alpha, beta, dim_alpha, prob, labels, pzx); } void cudaD_compute_ctc_alpha_rescale(dim3 Gr, dim3 Bl, double *alpha, int row_idx, MatrixDim dim_alpha, const double *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_alpha_one_sequence_rescale), dim3(Gr), dim3(Bl), 0, 0, alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaD_compute_ctc_beta_rescale(dim3 Gr, dim3 Bl, double *beta, int row_idx, MatrixDim dim_beta, const double *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _compute_ctc_beta_one_sequence_rescale), dim3(Gr), dim3(Bl), 0, 0, beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaD_compute_ctc_error_rescale(dim3 Gr, dim3 Bl, double *error, MatrixDim dim_error, const double *alpha, const double *beta, MatrixDim dim_alpha, const double *prob, const int *labels, const double *zt) { hipLaunchKernelGGL(( _compute_ctc_error_one_sequence_rescale), dim3(Gr), dim3(Bl), 0, 0, error, dim_error, alpha, beta, dim_alpha, prob, labels, zt); } void cudaD_distribute_prob_by_label(dim3 Gr, dim3 Bl, double *prob_dist, MatrixDim dim_prob_dist, const double *prob, MatrixDim dim_prob, const int *labels) { hipLaunchKernelGGL(( _distribute_prob_by_label), dim3(Gr), dim3(Bl), 0, 0, prob_dist, dim_prob_dist, prob, dim_prob, labels); } void cudaD_compute_ctc_alpha_multiple_sequence(dim3 Gr, dim3 Bl, double *alpha, int seq_num, int row_idx, MatrixDim dim_alpha, const double *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths) { hipLaunchKernelGGL(( _compute_ctc_alpha_multiple_sequence), dim3(Gr), dim3(Bl), 0, 0, alpha, seq_num, row_idx, dim_alpha, prob, dim_prob, labels, dim_label_stride, seq_lengths); } void cudaD_compute_ctc_beta_multiple_sequence(dim3 Gr, dim3 Bl, double *beta, int seq_num, int row_idx, MatrixDim dim_beta, const double *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths, const int *label_lengths) { hipLaunchKernelGGL(( _compute_ctc_beta_multiple_sequence), dim3(Gr), dim3(Bl), 0, 0, beta, seq_num, row_idx, dim_beta, prob, dim_prob, labels, dim_label_stride, seq_lengths, label_lengths); } void cudaD_compute_ctc_error_multiple_sequence(dim3 Gr, dim3 Bl, double *error, int seq_num, MatrixDim dim_error, const double *alpha, const double *beta, MatrixDim dim_alpha, const double *prob, const int *labels, int dim_label_stride, const int *seq_lengths, const double *pzx) { hipLaunchKernelGGL(( _compute_ctc_error_multiple_sequence), dim3(Gr), dim3(Bl), 0, 0, error, seq_num, dim_error, alpha, beta, dim_alpha, prob, labels, dim_label_stride, seq_lengths, pzx); } ////////////////////////////////////////////////////// //// FSMN kernel functions /////// //////////////////////////////////////////////////// template<typename Real> __global__ static void _gen_memory(Real* out, const Real* in, const Real *l_filter, const Real *r_filter, float *flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if(i < d.cols*d.rows) { int row = i/d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = 0; order < l_order; order++) { shift_index = row - order*l_stride; if (shift_index >= 0 && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } for (int order = 1; order <= r_order; order++) { shift_index = row + order*r_stride; if (shift_index < d.rows && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * r_filter[(order - 1)*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _gen_memory_online(Real* out, const Real* in, int start, const Real *l_filter, const Real *r_filter, const int32_cuda* l_valid_frames, const int32_cuda* r_valid_frames, const int32_cuda* stream_state_flag, MatrixDim d_out, MatrixDim d_in, int l_order, int r_order, int l_stride, int r_stride, int nstream) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if(i < d_out.cols*d_out.rows) { int batch_size = d_out.rows/nstream, buffer_size = d_in.rows/nstream; int r_his = r_order*r_stride, stride = d_out.stride; int curt_start, num_valid_frames, l_b, r_b, shift_index; int row = i/d_out.cols; int col = i%d_out.cols; int s = row/batch_size, r = row%batch_size, c = col; curt_start = stream_state_flag[s]==0 ? start+r_his : start; r_b = start + r_his + r_valid_frames[s]; if (stream_state_flag[s] == 0) { num_valid_frames = r_valid_frames[s]-r_his; l_b = start + r_his; } else if (stream_state_flag[s] == 1) { num_valid_frames = r_valid_frames[s]; l_b = 0; } else if (stream_state_flag[s] == 2) { num_valid_frames = r_valid_frames[s]+r_his; l_b = 0; } if (r < num_valid_frames) { int idx = (s*buffer_size+curt_start+r)*stride + c; int odx = (s*batch_size+r)*stride + c; out[odx] = in[idx]; Real value = 0.0; /// left history for (int order = 0; order < l_order; order++) { shift_index = r-order*l_stride+curt_start; if (shift_index >= l_b) value += in[(s*buffer_size+shift_index)*stride + c] * l_filter[order*stride + c]; } /// right history for (int order = 1; order < r_order + 1; order++) { shift_index = r+order*r_stride+curt_start; if (shift_index < r_b) value += in[(s*buffer_size+shift_index)*stride + c] * r_filter[(order - 1)*stride + c]; } out[odx] += value; } } } template<typename Real> __global__ static void _memory_err_back(Real* out, const Real* in, const Real *l_filter, const Real *r_filter, float *flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < d.cols*d.rows) { int row = i/d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = -r_order; order < 0; order++) { shift_index = row + order*r_stride; if (shift_index >= 0 && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * r_filter[(-order - 1)*d.stride + col]; } } for (int order = 0; order < l_order; order++) { shift_index = row + order*l_stride; if (shift_index < d.rows && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _gen_uni_memory(Real* out, const Real* in, const Real *l_filter, float *flags, MatrixDim d, int l_order, int l_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < d.cols*d.rows) { int row = i / d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = 0; order < l_order; order++) { shift_index = row - order*l_stride; if (shift_index >= 0 && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _uni_memory_err_back(Real* out, const Real* in, const Real *l_filter, float *flags, MatrixDim d, int l_order, int l_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < d.cols*d.rows) { int row = i / d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = 0; order < l_order; order++) { shift_index = row + order*l_stride; if (shift_index < d.rows && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _get_l_filter_err(Real* out, const Real* diff, const Real* in, float *flags, MatrixDim d, int l_order, int l_stride, float lr) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.cols*l_order) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.rows - 1)/THREADS + 1; int order = j/d.cols; int col = j%d.cols; int shift = order * l_stride; int index = order*d.stride + col; //copy input to aux int row = threadIdx.x - shift; if (steps > 1) { if (row >= 0 && abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[col + row*d.stride] * diff[col + threadIdx.x*d.stride]; } else { aux[threadIdx.x] = 0; } __syncthreads(); for (int i = 1; i<steps; ++i) { int index = threadIdx.x + i*THREADS; if (index < d.rows && abs(flags[index] - flags[index - shift])<1e-2) aux[threadIdx.x] += in[(index - shift)*d.stride + col] * diff[index*d.stride + col]; } __syncthreads(); } else { if (row >= 0 && threadIdx.x<d.rows && abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[col + row*d.stride] * diff[col + threadIdx.x*d.stride]; } else { aux[threadIdx.x] = 0; } __syncthreads(); } int nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if (threadIdx.x + halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); // divide by two. } Real sum = aux[0]; __syncthreads(); //out[index] = out[index]-lr*sum; out[index] = lr*sum; } template<typename Real> __global__ static void _get_r_filter_err(Real* out, const Real* diff, const Real* in, float *flags, MatrixDim d, int r_order, int r_stride, float lr) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.cols*r_order) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.rows - 1) / THREADS + 1; int order = j/d.cols; int col = j%d.cols; int shift = (order + 1) * r_stride; int index = order*d.stride + col; //copy input to aux int row = threadIdx.x + shift; if (steps > 1) { if (row <d.rows && abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[row*d.stride + col] * diff[threadIdx.x *d.stride + col]; } else { aux[threadIdx.x] = 0; } __syncthreads(); for (int i = 1; i<steps; ++i) { int index = threadIdx.x + i*THREADS; if (index + shift < d.rows && abs(flags[index] - flags[index + shift])<1e-2) aux[threadIdx.x] += in[(index + shift)*d.stride + col] * diff[index*d.stride + col]; } __syncthreads(); } else { if (row <d.rows &&threadIdx.x<d.rows&& abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[row*d.stride + col] * diff[threadIdx.x *d.stride + col]; } else { aux[threadIdx.x] = 0; } __syncthreads(); } int nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if (threadIdx.x + halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); // divide by two. } Real sum = aux[0]; __syncthreads(); //out[index] = out[index]-lr*sum; out[index] = lr*sum; } void cudaF_gen_memory(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, const float* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { hipLaunchKernelGGL(( _gen_memory), dim3(Gr), dim3(Bl) , 0, 0, mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaD_gen_memory(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, const double* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { hipLaunchKernelGGL(( _gen_memory) , Gr, Bl >> >(mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaF_memory_err_back(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, const float* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { _memory_err_back, Gr, Bl , 0, 0, 0, mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaD_memory_err_back(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, const double* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { hipLaunchKernelGGL(( _memory_err_back), dim3(Gr), dim3(Bl) , 0, 0, mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaF_gen_uni_memory(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _gen_uni_memory << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaD_gen_uni_memory(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _gen_uni_memory << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaF_uni_memory_err_back(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _uni_memory_err_back << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaD_uni_memory_err_back(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _uni_memory_err_back << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaF_get_l_filter_err(dim3 Gr, dim3 Bl, float *mat_out, const float *diff, const float* mat_in, float* flags, MatrixDim d, int l_order, int l_stride, float lr) { hipLaunchKernelGGL(( _get_l_filter_err) , dim3(Gr), dim3(Bl) , 0, 0, mat_out, diff, mat_in, flags, d, l_order, l_stride, lr); } void cudaD_get_l_filter_err(dim3 Gr, dim3 Bl, double *mat_out, const double *diff, const double* mat_in, float* flags, MatrixDim d, int l_order, int l_stride, float lr) { hipLaunchKernelGGL(( _get_l_filter_err) , dim3(Gr), dim3(Bl) , 0, 0, mat_out, diff, mat_in, flags, d, l_order, l_stride, lr); } void cudaF_get_r_filter_err(dim3 Gr, dim3 Bl, float *mat_out, const float *diff, const float* mat_in, float* flags, MatrixDim d, int r_order, int r_stride, float lr) { hipLaunchKernelGGL(( _get_r_filter_err) , dim3(Gr), dim3(Bl) , 0, 0, mat_out, diff, mat_in, flags, d, r_order, r_stride, lr); } void cudaD_get_r_filter_err(dim3 Gr, dim3 Bl, double *mat_out, const double *diff, const double* mat_in, float* flags, MatrixDim d, int r_order, int r_stride, float lr) { hipLaunchKernelGGL(( _get_r_filter_err) , dim3(Gr), dim3(Bl) , 0, 0, mat_out, diff, mat_in, flags, d, r_order, r_stride, lr); } void cudaF_gen_memory_online(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, int start, const float *l_filter, const float* r_filter, const int *l_valid, const int *r_valid, const int *stream_state_flag, MatrixDim d_out, MatrixDim d_in, int l_order, int r_order, int l_stride, int r_stride, int nstream) { hipLaunchKernelGGL(( _gen_memory_online) , dim3(Gr), dim3(Bl) , 0, 0, mat_out, mat_in, start, l_filter, r_filter, l_valid, r_valid, stream_state_flag, d_out, d_in, l_order, r_order, l_stride, r_stride, nstream); } void cudaD_gen_memory_online(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, int start, const double *l_filter, const double* r_filter, const int *l_valid, const int *r_valid, const int *stream_state_flag, MatrixDim d_out, MatrixDim d_in, int l_order, int r_order, int l_stride, int r_stride, int nstream) { hipLaunchKernelGGL(( _gen_memory_online) , dim3(Gr), dim3(Bl) , 0, 0, mat_out, mat_in, start, l_filter, r_filter, l_valid, r_valid, stream_state_flag, d_out, d_in, l_order, r_order, l_stride, r_stride, nstream); } // Launches a kernel that does nothing, explicitly using the legacy default stream; // this will synchronize all threads without blocking. void cuda_legacy_noop() { hipLaunchKernelGGL(( _noop_kernel), dim3(1), dim3(1), 0, cudaStreamLegacy, ); }
816023177bb7fe1804a4c9d7ad1ab576fe825dfa.cu
// cudamatrix/cu-kernels.cu // Copyright 2009-2012 Karel Vesely // 2013 Ehsan Variani // 2013 Johns Hopkins University (author: Daniel Povey) // 2013 Hainan Xu // 2013 Xiaohui Zhang // 2013-2015 Guoguo Chen // 2016-2018 Shiyin Kang // 2017 Hossein Hadian, Daniel Galvez // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. // In this file is the CUDA code of the CUDA kernels, plus the ANSI-C wrappers #include <cfloat> #include <limits> #include <math_constants.h> #include "cudamatrix/cu-kernels-ansi.h" /*********************************************************************** * Generic __device__ functions */ template<typename Real> __device__ static Real _sum_reduce(Real buffer[]) { // Total number of active threads int32_cuda nTotalThreads = blockDim.x; __syncthreads(); // perform tree-based reduction (sum) while (nTotalThreads > 1) { int32_cuda halfPoint = ((1 + nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x >= halfPoint) { // was < // Get the shared value stored by another thread Real temp = 0.0; if (threadIdx.x < nTotalThreads) { // was +halfPoint temp = buffer[threadIdx.x]; // was +halfPoint } buffer[threadIdx.x - halfPoint] += temp; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); // divide by two. } // the result return buffer[0]; } /*********************************************************************** * CUDA kernels * the functions are templated to have the float/double operations */ /* * CuMatrix */ template<typename Real> __global__ static void _copy_low_upp(Real* A, MatrixDim dimA) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (i <= j || i >= dimA.rows) return; int index_1 = i * dimA.stride + j; int index_2 = j * dimA.stride + i; A[index_2] = A[index_1]; } template<typename Real> __global__ static void _copy_upp_low(Real* A, MatrixDim dimA) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j <= i || j >= dimA.rows) return; int index_1 = i * dimA.stride + j; int index_2 = j * dimA.stride + i; A[index_2] = A[index_1]; } // mat += diag(vec) * mat2. template<typename Real> __global__ static void _add_diag_vec_mat(Real alpha, Real *mat, MatrixDim mat_dim, const Real *vec, const Real *mat2, int mat2_row_stride, int mat2_col_stride, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = j * mat_dim.stride + i, index2 = j * mat2_row_stride + i * mat2_col_stride; if (i < mat_dim.cols && j < mat_dim.rows) { mat[index] = alpha * vec[j] * mat2[index2] + beta * mat[index]; } } template<typename Real, typename OtherReal> __global__ static void _copy_from_tp(Real* A, const OtherReal* B, MatrixDim dmat) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // col index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dmat.cols && j < dmat.rows) { int32_cuda index_B = (j * (j + 1) / 2) + i; int32_cuda index_A = j * dmat.stride + i; if (i <= j) { A[index_A] = B[index_B]; } else { A[index_A] = 0.0; } } } template<typename Real, typename OtherReal> __global__ static void _copy_from_tp_trans(Real* A, const OtherReal* B, MatrixDim dmat) { // we interpret these indexes oppositely from normal, but it doesn't // matter as it's invoked in a symmetric way. int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // transpose the indices used to index the source TpMatrix. if (i < dmat.rows && j < dmat.cols) { int32_cuda index_B = (j * (j + 1) / 2) + i; int32_cuda index_A = i * dmat.stride + j; if (i <= j) { A[index_A] = B[index_B]; } else { A[index_A] = 0.0; } } } template<typename Real, typename OtherReal> __global__ static void _copy_from_mat(Real* mat_out, const OtherReal* mat_in, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // col-index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row-index. int32_cuda index_out = i + j * d_out.stride; int32_cuda index_in = i + j * d_in.stride; if (i < d_out.cols && j < d_out.rows) mat_out[index_out] = static_cast<Real>(mat_in[index_in]); } template<int TileDim, typename Real, typename OtherReal> __global__ static void _copy_from_mat_trans(Real* mat_out, const OtherReal* mat_in, MatrixDim d_out, MatrixDim d_in) { // Use shared meme to achieve both coalesced memory reading and writing // '+1' to avoid bank conflict when reading sbuf __shared__ Real sbuf[TileDim][TileDim + 1]; const int32_cuda i_in = blockIdx.y * TileDim + threadIdx.y; // row-index const int32_cuda j_in = blockIdx.x * TileDim + threadIdx.x; // col-index const int32_cuda tile_stride_in = CU1DBLOCK / TileDim * d_in.stride; int32_cuda index_in = i_in * d_in.stride + j_in; # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_in + i < d_in.rows && j_in < d_in.cols) { sbuf[threadIdx.y + i][threadIdx.x] = static_cast<Real>(mat_in[index_in]); } index_in += tile_stride_in; } __syncthreads(); // Grid is transposed, but block is not yet. // Warp (blockDim.x) is always along the row-dim. const int32_cuda i_out = blockIdx.x * TileDim + threadIdx.y; const int32_cuda j_out = blockIdx.y * TileDim + threadIdx.x; const int32_cuda tile_stride_out = CU1DBLOCK / TileDim * d_out.stride; int32_cuda index_out = i_out * d_out.stride + j_out; # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_out + i < d_out.rows && j_out < d_out.cols) { // block is tranposed when reading sbuf mat_out[index_out] = sbuf[threadIdx.x][threadIdx.y + i]; } index_out += tile_stride_out; } } // Copy from CSR sparse matrix to dense matrix // // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows. template<typename Real, typename OtherReal> __global__ static void _copy_from_smat(Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const OtherReal* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx if (i < mat_dim.rows) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx mat[i * mat_dim.stride + j] = static_cast<Real>(smat_val[nz_id]); } } } /// Select a subset of the rows of a CSR SparseMatrix. /// Sets 'out' to only the rows of 'in' that are listed /// in 'row_indexes'. 'row_indexes' must be sorted and unique, /// and satisfy 0 <= row_indexes[i] < in.size(). /// /// Note: 'out_row_ptr' is an input parameter that is calculated before /// calling this kernel function /// /// We use warpSize threads per row to access only the nnz elements. /// Every CU1DBLOCK/warpSize rows share one thread block. /// 1D grid to cover all selected rows. template<typename Real> __global__ static void _select_rows(const int* out_row_ptr, int* out_col_idx, Real* out_val, const int* row_indexes, const int num_selected_rows, const int* in_row_ptr, const int* in_col_idx, const Real* in_val) { const int out_i = blockIdx.x * blockDim.y + threadIdx.y; // out row idx if (out_i < num_selected_rows) { const int in_i = row_indexes[out_i]; const int in_row_start = in_row_ptr[in_i]; const int out_row_start = out_row_ptr[out_i]; const int row_length = in_row_ptr[in_i + 1] - in_row_start; for (int k = threadIdx.x; k < row_length; k += warpSize) { const int in_n = in_row_start + k; const int out_n = out_row_start + k; out_col_idx[out_n] = in_col_idx[in_n]; out_val[out_n] = in_val[in_n]; } } } // mat += alpha * smat // // We use warpSize threads per row to access only the nonzero elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _add_smat(Real* mat, MatrixDim mat_dim, Real alpha, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx if (i < mat_dim.rows) { const int row_start = smat_row_ptr[i]; const int row_end = smat_row_ptr[i + 1]; for (int n = row_start + threadIdx.x; n < row_end; n += warpSize) { const int j = smat_col_idx[n]; // col idx of smat mat[i * mat_dim.stride + j] += alpha * smat_val[n]; } } } // mat += alpha * smat^T // // We use warpSize threads per row to access only the nonzero elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _add_smat_trans(Real* mat, MatrixDim mat_dim, Real alpha, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx if (i < mat_dim.cols) { const int row_start = smat_row_ptr[i]; const int row_end = smat_row_ptr[i + 1]; for (int n = row_start + threadIdx.x; n < row_end; n += warpSize) { const int j = smat_col_idx[n]; // col idx of smat mat[j * mat_dim.stride + i] += alpha * smat_val[n]; } } } /// For each element x of the matrix, set it to /// (x < 0 ? exp(x) : x + 1). /// Use block/grid sizes for simple matrix ops template<typename T> __global__ static void _apply_exp_special(T* out, MatrixDim out_dim, const T* in, int in_stride) { const int i = blockIdx.x * blockDim.x + threadIdx.x; const int j = blockIdx.y * blockDim.y + threadIdx.y; if (i < out_dim.rows && j < out_dim.cols) { T x = in[i * in_stride + j]; if (x < T(0)) { out[i * out_dim.stride + j] = exp(x); } else { out[i * out_dim.stride + j] = x + T(1); } } } /// Fill the array 'data' with the sequence [base ... base + length) /// Use 1D block and 1D grid template<typename T> __global__ static void _sequence(T* data, int length, T base) { const int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < length) { data[i] = base + T(i); } } // Copy from CSR sparse matrix to transposed dense matrix // // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows. template<typename Real, typename OtherReal> __global__ static void _copy_from_smat_trans(Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const OtherReal* smat_val) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx of smat if (i < mat_dim.cols) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx of smat mat[j * mat_dim.stride + i] = static_cast<Real>(smat_val[nz_id]); } } } // First stage of trace(mat * smat^T) // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _trace_mat_smat_trans(const Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val, Real* trace_vec) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx of smat if (i < mat_dim.rows) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx of smat trace_vec[nz_id] = mat[i * mat_dim.stride + j] * smat_val[nz_id]; } } } // First stage of trace(mat * smat) // We use warpSize threads per row to access only the nnz elements. // Every CU1DBLOCK/warpSize rows share one thread block. // 1D grid to cover all rows of smat. template<typename Real> __global__ static void _trace_mat_smat(const Real* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const Real* smat_val, Real* trace_vec) { const int i = blockIdx.x * blockDim.y + threadIdx.y; // row idx of smat if (i < mat_dim.cols) { const int nz_start = smat_row_ptr[i]; const int nz_end = smat_row_ptr[i + 1]; for (int nz_id = nz_start + threadIdx.x; nz_id < nz_end; nz_id += warpSize) { const int j = smat_col_idx[nz_id]; // col idx of smat trace_vec[nz_id] = mat[j * mat_dim.stride + i] * smat_val[nz_id]; } } } template<typename Real> __global__ static void _apply_exp(Real* mat, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) { mat[index] = exp(mat[index]); } } template<typename Real> __global__ static void _apply_fixed(Real* mat, Real resolution, int mode, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride ; Real tmp ; if(i < d.cols && j < d.rows) { if(1 == mode) { if(mat[index] > 0.0){ tmp = int(mat[index]/resolution)*resolution; }else{ tmp = int((mat[index]-resolution)/resolution)*resolution; } if((mat[index]-tmp)/resolution > 0.5){ mat[index] = tmp + resolution ; }else{ mat[index] = tmp ; } } else if (2 == mode) { mat[index] = int(mat[index]/resolution) * resolution; } } } template<typename Real> __global__ static void _apply_exp_limited(Real* mat, MatrixDim d, Real lower_limit, Real upper_limit) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) { Real x = mat[index]; // I'm writing !(x >= lower_limit) instead of (x < lower_limit) so that // nan's will be set to the lower-limit. if (!(x >= lower_limit)) x = lower_limit; else if (x > upper_limit) x = upper_limit; mat[index] = exp(x); } } template<typename Real> __global__ static void _scale_diag_packed(Real* mat, Real value, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { mat[index] = value * mat[index]; } } template<typename Real> __global__ static void _set_diag(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = i + i * d.stride; if (i < d.rows && i < d.cols) { mat[index] = value; } } template<typename Real> __global__ static void _set_diag_packed(Real* mat, Real value, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { mat[index] = value; } } template<typename Real> __global__ static void _add_diag_packed(Real* mat, Real value, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { mat[index] = mat[index] + value; } } template<typename Real> __global__ static void _set_const(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // column int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = value; } template<typename Real> __global__ static void _set_zero_above_diag(Real* mat, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < i) mat[index] = 0.0; } template<typename Real> __global__ static void _add(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = mat[index] + value; } template<typename Real> __global__ static void _scale(Real* mat, Real value, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = mat[index] * value; } template<typename Real> __global__ static void _apply_log(Real* mat, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = log(mat[index]); } template<typename Real> __global__ static void _mul_elements(Real* mat, const Real* A, MatrixDim dst_d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j * dst_d.stride, src_index = i + j * src_stride; if (i < dst_d.cols && j < dst_d.rows) mat[dst_index] = mat[dst_index] * A[src_index]; } template<typename Real> __global__ static void _div_elements(Real* mat, const Real* A, MatrixDim dst_d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j * dst_d.stride, src_index = i + j * src_stride; if (i < dst_d.cols && j < dst_d.rows) mat[dst_index] = mat[dst_index] / A[src_index]; } template<typename Real> __global__ static void _max(Real* mat, const Real* A, MatrixDim dst_d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j * dst_d.stride, src_index = i + j * src_stride; if (i < dst_d.cols && j < dst_d.rows) { Real a = mat[dst_index], b = A[src_index]; mat[dst_index] = fmax(a, b); } } template<typename Real> __global__ static void _min(Real* mat, const Real* other, MatrixDim mat_d, int other_stride) { int32_cuda j = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda i = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda mat_index = i * mat_d.stride + j; int32_cuda other_index = i * other_stride + j; if (j < mat_d.cols && i < mat_d.rows) { Real a = mat[mat_index], b = other[other_index]; mat[mat_index] = fmin(a, b); } } template<typename Real> __global__ static void _vec_mul_elements(Real* v, const Real* a, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) v[i] = v[i] * a[i]; } template<typename Real> __global__ static void _mul_cols_vec(Real* mat, const Real* scale, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] *= scale[i]; } template<typename Real> __global__ static void _mul_rows_vec(Real* mat, const Real* scale, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] *= scale[j]; } template<typename Real> __global__ static void _mul_rows_group_mat(Real *y, const Real *x, MatrixDim d, int src_stride, int group_size) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j < d.rows && i < d.cols) { int dst_index = i + j * d.stride; int src_index = i / group_size + j * src_stride; y[dst_index] *= x[src_index]; } } template<typename Real> __global__ void _diff_group_pnorm(Real *id, const Real *iv, const Real *ov, const Real* od, MatrixDim id_dim, int iv_stride, int ov_stride, int od_stride, int group_size, Real power) { const int j = blockIdx.x * blockDim.x + threadIdx.x; if (j < id_dim.cols) { const int grid_stride = gridDim.y * blockDim.y; const int src_j = j / group_size; int i = blockIdx.y * blockDim.y + threadIdx.y; for (; i < id_dim.rows; i += grid_stride) { const int iv_index = j + i * iv_stride; Real iv_ij = iv[iv_index]; Real ans; if (power == Real(2)) { const int ov_index = src_j + i * ov_stride; Real ov_ij = ov[ov_index]; ans = ov_ij <= 0.0 ? 0.0 : iv_ij / ov_ij; } else if (power == Real(1)) { Real iv_ij_sign = (iv_ij >= 0 ? 1 : -1); ans = (iv_ij == Real(0) ? 0.0 : iv_ij_sign); } else if (power == (sizeof(Real) == sizeof(float) ? CUDART_INF_F : CUDART_INF)) { const int ov_index = src_j + i * ov_stride; Real ov_ij = ov[ov_index]; Real iv_ij_sign = (iv_ij >= 0 ? 1 : -1); ans = ov_ij <= 0.0 ? 0.0 : (iv_ij_sign * (abs(iv_ij) == ov_ij ? 1.0 : 0.0)); } else { const int ov_index = src_j + i * ov_stride; Real ov_ij = ov[ov_index]; Real iv_ij_sign = (iv_ij >= 0 ? 1 : -1); if (ov_ij <= 0.0) { ans = 0.0; // The derivative is either 0 or undefined at the origin. } else { ans = iv_ij_sign * pow(std::abs(iv_ij), power - 1) * pow(ov_ij, 1 - power); } } const int od_index = src_j + i * od_stride; const int id_index = j + i * id_dim.stride; id[id_index] = ans * od[od_index]; } } } /// deriv is the derivative we will output; vec is the input we're computing /// the group max on, "maxv" is the previously computed group max. template<typename Real> __global__ static void _calc_group_max_deriv(Real *deriv, const Real *vec, const Real *maxv, MatrixDim deriv_dim, int vec_stride, int maxv_stride, int group_size) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j < deriv_dim.rows && i < deriv_dim.cols) { int deriv_index = i + j * deriv_dim.stride; int vec_index = i + j * vec_stride; int maxv_index = i / group_size + j * maxv_stride; Real vec_element = vec[vec_index], // The element of the original vector. max_element = maxv[maxv_index]; // this is the max value Real ans = (max_element == vec_element ? 1.0 : 0.0); deriv[deriv_index] = ans; } } /// Set each element to y = (x == orig ? changed : x). template<typename Real> __global__ static void _replace_value(Real *vec, int dim, Real orig, Real changed) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) if (vec[i] == orig) vec[i] = changed; } template<typename Real> __global__ static void _div_rows_vec(Real* mat, const Real* vec_div, MatrixDim d) { const int32_cuda i = blockIdx.y * blockDim.y + threadIdx.y; if (i < d.rows) { const int32_cuda start = i * d.stride; const Real scale = Real(1) / vec_div[i]; const int32_cuda grid_stride = blockDim.x * gridDim.x; for (int32_cuda j = blockIdx.x * blockDim.x + threadIdx.x; j < d.cols; j += grid_stride) { mat[start + j] *= scale; } } } template<typename Real> __global__ static void _add_mat(Real alpha, const Real* src, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // column index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row index int32_cuda index = i + j * d.stride; int32_cuda index_src = i + j * src_stride; if (i < d.cols && j < d.rows) dst[index] = alpha * src[index_src] + dst[index]; } template<typename Real> __global__ static void _add_mat_trans(Real alpha, const Real* src, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; int32_cuda index_src = j + i * src_stride; if (i < d.cols && j < d.rows) dst[index] = alpha * src[index_src] + dst[index]; } template<typename Real> __global__ static void _add_mat_blocks(Real alpha, const Real* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; int32_cuda index_src = i + j * src_stride; if (i < d.cols && j < d.rows) for (int32_cuda p = 0; p < num_row_blocks; p++) { for (int32_cuda q = 0; q < num_col_blocks; q++) { dst[index] = alpha * src[index_src + p * src_stride * d.rows + q * d.cols] + dst[index]; } } } template<typename Real> __global__ static void _add_mat_repeated(Real alpha, const Real* src, MatrixDim src_dim, Real* dst, MatrixDim dst_dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda src_i = i % src_dim.cols, src_j = j % src_dim.rows, dst_index = i + j * dst_dim.stride, src_index = src_i + src_j * src_dim.stride; if (i < dst_dim.cols && j < dst_dim.rows) dst[dst_index] += alpha * src[src_index]; } template<typename Real> __global__ static void _add_mat_blocks_trans(Real alpha, const Real* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, Real* dst, MatrixDim d, int src_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; int32_cuda index_src = j + i * src_stride; if (i < d.cols && j < d.rows) for (int32_cuda p = 0; p < num_row_blocks; p++) { for (int32_cuda q = 0; q < num_col_blocks; q++) { dst[index] = alpha * src[index_src + p * src_stride * d.cols + q * d.rows] + dst[index]; } } } template<typename Real> __global__ static void _set_mat_mat_div_mat(const Real* A, const Real* B, const Real* C, Real* dst, MatrixDim d, int stride_a, int stride_b, int stride_c) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride, a_index = i + j * stride_a, b_index = i + j * stride_b, c_index = i + j * stride_c; if (i < d.cols && j < d.rows) if (C[c_index] == 0) dst[index] = A[a_index]; else dst[index] = A[a_index] * B[b_index] / C[c_index]; } // Given a matrix input S (not packed!) and a lower-triangular matrix L, this // function does S = beta S + alpha * L^T L. This is used in PSD matrix // inversion. The i index is the row of the destination S and the j the column // (although of course the output is symmetric so it doesn't matter in a sense). // The main point of this is to make use of various symmetries and zero-ness. template<typename Real> __global__ static void _sy_add_tr2(Real alpha, Real beta, const Real *T, MatrixDim tdim, Real *S, MatrixDim sdim) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (i >= sdim.rows || j > i) return; // this thread computes the dot-product of the i'th column of // L with the j'th column of L. The values we're multiplying // are only nonzero for row-index k greater or equal to // max(i, j), which equals i. Real sum = 0.0; for (int k = i; k < sdim.rows; k++) { int i_index = i + tdim.stride * k, j_index = j + tdim.stride * k; sum += T[i_index] * T[j_index]; } int output_index1 = i * sdim.stride + j, output_index2 = j * sdim.stride + i; S[output_index1] = alpha * sum + beta * S[output_index1]; S[output_index2] = alpha * sum + beta * S[output_index2]; } template<typename Real> __global__ static void _add_vec_to_cols(Real alpha, const Real* col, Real beta, Real* dst, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) dst[index] = alpha * col[j] + beta * dst[index]; } template<typename Real> __global__ static void _add_vec_to_rows(Real alpha, const Real* row, Real beta, Real* dst, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) dst[index] = alpha * row[i] + beta * dst[index]; } template<typename Real> __global__ static void _apply_mask(Real* mat, const char* mask, MatrixDim dmat, MatrixDim dmask) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * dmat.stride; int32_cuda index2 = i + j * dmask.stride; if (i < dmat.cols && j < dmat.rows) if (mask[index2] == 0) mat[index] = 0; } template<typename Real> __global__ static void _add_mat_diag_vec(Real alpha, Real *mat, MatrixDim mat_dim, const Real *mat2, int mat2_row_stride, int mat2_col_stride, const Real *vec, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * mat_dim.stride, index2 = i * mat2_col_stride + j * mat2_row_stride; if (j < mat_dim.rows && i < mat_dim.cols) mat[index] = alpha * mat2[index2] * vec[i] + beta * mat[index]; } template<typename Real> __global__ static void _add_mat_mat_elements(Real *data, const Real *srcA_data, const Real *srcB_data, MatrixDim dim, int srcA_stride, int srcB_stride, Real alpha, Real beta) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda tgt_index = i + j * dim.stride; int32_cuda srcA_index = i + j * srcA_stride; int32_cuda srcB_index = i + j * srcB_stride; if (i < dim.cols && j < dim.rows) { data[tgt_index] = alpha * srcA_data[srcA_index] * srcB_data[srcB_index] + beta * data[tgt_index]; } } /* * CuVector */ // very limited application! template<typename Real> __global__ static void _set_bias_params(Real* v, const Real* a, Real param_1, Real param_2, Real param_3, int* flag, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { Real ratio = a[i] / param_3; if ((ratio < 0.0) || (ratio >= 1.01)) { *flag = 1; return; } if (ratio < param_1) { Real factor = ((param_1 / ratio) > param_2) ? param_2 : (param_1 / ratio); v[i] = v[i] / factor; } else if (ratio > param_1) { Real factor = ((ratio / param_1) > param_2) ? param_2 : (ratio / param_1); v[i] = v[i] * factor; } } } template<typename Real, typename OtherReal> __global__ static void _cublas_copy_kaldi(int n, const Real* x, int incx, OtherReal* y, int incy) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) { y[i * incy] = static_cast<OtherReal>(x[i * incx]); } } // This kernel writes a copy of the vector "v_in" to each row of the matrix // "m_out". the dimension of v_in should be equal to the #columns of m_out. template<typename Real> __global__ static void _copy_rows_from_vec(Real* m_out, MatrixDim d, const Real* v_in) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index. int j = blockIdx.y * blockDim.y + threadIdx.y; // row index. if (i < d.cols && j < d.rows) { int index = i + j * d.stride; m_out[index] = v_in[i]; } } // This kernel writes a copy of the vector "v_in" to each col of the matrix // "m_out". the dimension of v_in should be equal to the #row of m_out. template<typename Real> __global__ static void _copy_cols_from_vec(Real* m_out, MatrixDim d, const Real* v_in) { int i = blockIdx.y * blockDim.y + threadIdx.y; // row id int j = blockIdx.x * blockDim.x + threadIdx.x; // col id if (i < d.rows && j < d.cols) { m_out[i * d.stride + j] = v_in[i]; } } // _trace_mat_mat reduce the partial sum to // value[blockIdx.y * gridDim.x + blockIdx.x] // It use shared mem to transpose matrix B to ensure coalesced memory access template<int TileDim, typename Real> __global__ static void _trace_mat_mat(const Real* A, const Real* B, MatrixDim dA, int B_stride, Real* value) { // Reuse shared mem and make indexing easier. "+1" to avoid bank conflict __shared__ union { Real trans[TileDim][TileDim + 1]; Real sum[CU1DBLOCK]; } smem; // linear thread id; const int32_cuda tid = threadIdx.y * blockDim.x + threadIdx.x; const int32_cuda grid_height = gridDim.y * TileDim; const int32_cuda ja = blockIdx.x * TileDim + threadIdx.x; const int32_cuda ib = blockIdx.x * TileDim + threadIdx.y; int32_cuda ia = blockIdx.y * TileDim + threadIdx.y; int32_cuda jb = blockIdx.y * TileDim + threadIdx.x; // Grid reduce Real tsum = Real(0); for (int32_cuda i0 = 0; i0 < dA.rows; i0 += grid_height) { // Load from B, transpose the block and store in shared mem if (jb < dA.rows) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (ib + i < dA.cols) { smem.trans[threadIdx.x][threadIdx.y + i] = B[(ib + i) * B_stride + jb]; } } } __syncthreads(); // Load from A, sum up the product. if (ja < dA.cols) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (ia + i < dA.rows) { tsum += A[(ia + i) * dA.stride + ja] * smem.trans[threadIdx.y + i][threadIdx.x]; } } } __syncthreads(); ia += grid_height; jb += grid_height; } smem.sum[tid] = tsum; __syncthreads(); // Block reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) smem.sum[tid] += smem.sum[tid + shift]; __syncthreads(); } // Warp reduce. Implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { smem.sum[tid] += smem.sum[tid + shift]; } } // output 1 sum per thread block if (tid == 0) { value[blockIdx.y * gridDim.x + blockIdx.x] = smem.sum[0]; } } // _trace_mat_mat_trans reduce the partial sum to // value[blockIdx.y * gridDim.x + blockIdx.x] template<typename Real> __global__ static void _trace_mat_mat_trans(const Real* A, const Real* B, MatrixDim dA, int B_stride, Real* value) { __shared__ Real ssum[CU1DBLOCK]; // linear thread id; const int32_cuda tid = threadIdx.y * blockDim.x + threadIdx.x; const int32_cuda j = blockIdx.x * blockDim.x + threadIdx.x; const int32_cuda grid_height = gridDim.y * blockDim.y; int32_cuda i = blockIdx.y * blockDim.y + threadIdx.y; // Grid reduce Real tsum = Real(0); if (j < dA.cols) { while (i < dA.rows) { tsum += A[i * dA.stride + j] * B[i * B_stride + j]; i += grid_height; } } ssum[tid] = tsum; __syncthreads(); // Block reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) ssum[tid] += ssum[tid + shift]; __syncthreads(); } // Warp reduce. Implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // output 1 sum per thread block if (tid == 0) { value[blockIdx.y * gridDim.x + blockIdx.x] = ssum[0]; } } // v = alpha * diag(M * N^T) + beta * v template<typename Real> __global__ static void _add_diag_mat_mat_MNT(const Real alpha, const Real* M, const MatrixDim dim_M, const Real* N, const int stride_N, const Real beta, Real* v) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int m_start = i * dim_M.stride; const int n_start = i * stride_N; // Loop along the matrix row. Reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < dim_M.cols; j += CU1DBLOCK) { tsum += M[m_start + j] * N[n_start + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) ssum[tid] += ssum[tid + shift]; __syncthreads(); } // Warp reduce to 1 element. Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // output 1 sum per thread block if (tid == 0) { v[i] = alpha * ssum[0] + beta * v[i]; } } // v = alpha * diag(M^T * N) + beta * v template<int TileDim, typename Real> __global__ static void _add_diag_mat_mat_MTN(const Real alpha, const Real* M, const int stride_M, const Real* N, const MatrixDim dim_N, const Real beta, Real* v, const int stride_v) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.y * blockDim.x + threadIdx.x; const int j = blockIdx.x * blockDim.x + threadIdx.x; if (j >= dim_N.cols) return; // Loop along the matrix column. // Reduce to gridDim.y * CU1DBLOCK / TileDim elements per column. Real tsum = Real(0); const int grid_stride_y = blockDim.y * gridDim.y; for (int i = blockIdx.y * blockDim.y + threadIdx.y; i < dim_N.rows; i += grid_stride_y) { tsum += M[i * stride_M + j] * N[i * dim_N.stride + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize / TileDim elements per column. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize && shift >= TileDim; shift >>= 1) { if (tid < shift) { ssum[tid] += ssum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element per column. // Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift >= TileDim; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // output TileDim sums per thread block if (tid < TileDim) { if (beta != Real(0)) { v[blockIdx.y * stride_v + j] = alpha * ssum[tid] + beta * v[blockIdx.y * stride_v + j]; } else { v[blockIdx.y * stride_v + j] = alpha * ssum[tid]; } } } // v = alpha * diag(M * N) + beta * v template<int TileDim, typename Real> __global__ static void _add_diag_mat_mat_MN(const Real alpha, const Real* M, const int stride_M, const Real* N, const MatrixDim dim_N, const Real beta, Real* v) { // Reuse shared mem and make indexing easier. "+1" to avoid bank conflict __shared__ union { Real trans[TileDim][TileDim + 1]; Real sum[CU1DBLOCK]; } smem; const int tid = threadIdx.y * blockDim.x + threadIdx.x; const int i_m = blockIdx.x * TileDim + threadIdx.y; const int j_n = blockIdx.x * TileDim + threadIdx.x; int i_n = threadIdx.y; int j_m = threadIdx.x; // Loop along the matrix column. // Reduce to CU1DBLOCK / TileDim elements per column. Real tsum = Real(0); for (int block_i_n = 0; block_i_n < dim_N.rows; block_i_n += TileDim) { // Load, transpose and store M to shared mem. if (j_m < dim_N.rows) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_m + i < dim_N.cols) { smem.trans[threadIdx.x][threadIdx.y + i] = M[(i_m + i) * stride_M + j_m]; } } } __syncthreads(); // Load N, sum up the product. if (j_n < dim_N.cols) { # pragma unroll for (int i = 0; i < TileDim; i += CU1DBLOCK / TileDim) { if (i_n + i < dim_N.rows) { tsum += N[(i_n + i) * dim_N.stride + j_n] * smem.trans[threadIdx.y + i][threadIdx.x]; } } } __syncthreads(); i_n += TileDim; j_m += TileDim; } smem.sum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize / TileDim elements per column. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize && shift >= TileDim; shift >>= 1) { if (tid < shift) { smem.sum[tid] += smem.sum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element per column. // Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift >= TileDim; shift >>= 1) { smem.sum[tid] += smem.sum[tid + shift]; } } // output TileDim sums per thread block if (tid < TileDim && j_n < dim_N.cols) { v[j_n] = alpha * smem.sum[tid] + beta * v[j_n]; } } template<typename Real> __global__ static void _add_vec_vec(Real alpha, Real* v, const Real* x, const Real* y, Real beta, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // if (blockIdx.y > 0) return; if (i < dim) v[i] = alpha * x[i] * y[i] + beta * v[i]; } template<typename Real> __global__ static void _copy_col_from_mat_df(double* v, int col, const Real* mat, MatrixDim dmat, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = col + i * dmat.stride; // if (blockIdx.y > 0) return; if (i < dim) v[i] = (double) mat[index]; } template<typename Real> __global__ static void _copy_col_from_mat_fd(float* v, int col, const Real* mat, MatrixDim dmat, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = col + i * dmat.stride; // if (blockIdx.y > 0) return; if (i < dim) v[i] = (float) mat[index]; } template<typename Real> __global__ static void _vec_apply_exp(Real* v, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // if (blockIdx.y > 0) return; if (i < dim) { v[i] = exp(v[i]); } } template<typename Real> __global__ static void _vec_apply_log(Real* v, Real* flag, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // if (blockIdx.y > 0) return; if (i < dim) { if (v[i] < 0) { *flag = 1; return; } v[i] = log(v[i]); } } template<typename Real> __global__ static void _vec_apply_fixed(Real* v, Real resolution, int mode, int dim){ int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; Real tmp; if(i < dim) { if (1 == mode) { tmp = int(v[i]/resolution) * resolution; if(v[i] > 0){ if((v[i] - tmp) >= 0.5*resolution) v[i] = tmp + resolution; else v[i] = tmp ; }else{ if((tmp - v[i]) >= 0.5 * resolution) v[i] = tmp - resolution ; else v[i] = tmp; } } else if (2 == mode) { v[i] = (v[i]/resolution) * resolution; } } } template<typename Real> __global__ static void _cuda_comp_obj_deriv(MatrixElement<Real> *x, int s, const Real* z, MatrixDim d, Real* z2, MatrixDim d2, Real* t) { int i = threadIdx.x; __shared__ Real tot_objf[CU1DBLOCK]; __shared__ Real tot_weight[CU1DBLOCK]; Real tmp_weight_sum = 0; Real tmp_tot_objf = 0; int size = s / CU1DBLOCK; //the least size in a loop (later part) int threshold = s - size * CU1DBLOCK; //any loop below this number would + 1 int loop_start; int loop_end; if (i < threshold) { loop_start = i * (size + 1); loop_end = (i + 1) * (size + 1); } else { loop_start = threshold + i * size; loop_end = threshold + (i + 1) * size; } for (int j = loop_start; j < loop_end; j++) { //* ((int*) ((size_t)x + j * (2 * sizeof(int) + sizeof(Real) )) ); int m = (x + j)->row; //*(int*) ((size_t)x + j * (2 * sizeof(int) + sizeof(Real) )+ sizeof(int)); int label = (x + j)->column; // *(Real*) ((size_t)x + j*(2*sizeof(int) + sizeof(Real)) + 2*sizeof(int)); Real weight = (x + j)->weight; tmp_weight_sum += weight; Real this_prob = *(z + m * d.stride + label); tmp_tot_objf += weight * log(this_prob); // there might be problems here.... *(z2 + m * d2.stride + label) += weight / this_prob; } tot_objf[i] = tmp_tot_objf; tot_weight[i] = tmp_weight_sum; __syncthreads(); *t = _sum_reduce(tot_objf); __syncthreads(); *(t + 1) = _sum_reduce(tot_weight); return; } template<typename Real> __global__ static void _cuda_vector_copy_elements(Real *data, int dim, const Real *src_mat, int mat_stride, bool transpose, const MatrixIndexT_cuda* elements) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= dim) return; int j = elements[i]; int mat_index; if (transpose) mat_index = i + j * mat_stride; else mat_index = j + i * mat_stride; data[i] = src_mat[mat_index]; } template<typename Real> __global__ static void _cuda_matrix_add_elements(Real *data, MatrixDim dim, Real alpha, MatrixElement<Real>* x, int num_elements) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= num_elements) return; data[x[i].row * dim.stride + x[i].column] += alpha * x[i].weight; } template<typename Real> __global__ static void _cuda_matrix_add_indexed_values(MatrixDim dim, Real alpha, const Int32Pair* indices, const Real* x, int s, Real* data) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= s) return; int data_i = indices[i].first * dim.stride + indices[i].second; data[data_i] += alpha * x[i]; } template<typename Real> __global__ static void _cuda_matrix_add_to_elements(Real alpha, Real* mat, MatrixDim dim, const MatrixIndexT_cuda* elements) { int row = blockIdx.x * blockDim.x + threadIdx.x; if (row < dim.rows) { int col = elements[row]; if (col >= 0) { int index = col + row * dim.stride; mat[index] += alpha; } } } template<typename Real> __global__ static void _matrix_lookup(const Real *data, MatrixDim dim, const Int32Pair *indices, int indices_size, Real *output) { int ind = blockIdx.x * blockDim.x + threadIdx.x; if (ind >= indices_size) return; int data_ind = indices[ind].first * dim.stride + indices[ind].second; output[ind] = data[data_ind]; } template<typename Real> __global__ static void _equal_element_mask(const Real *mat1, const Real *mat2, Real *mask, MatrixDim mat1_dim, int mat2_stride, int mask_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // col int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row int32_cuda index_mat1 = i + j * mat1_dim.stride; int32_cuda index_mat2 = i + j * mat2_stride; int32_cuda index_mask = i + j * mask_stride; if (i < mat1_dim.cols && j < mat1_dim.rows) mask[index_mask] = (mat1[index_mat1] == mat2[index_mat2] ? 1.0 : 0.0); } enum EnumTransformReduce { SUMAB, SUM, ABSMAX, MAX, MIN, LINFNORM, L2NORM, L1NORM, L0NORM, LPNORM }; template<EnumTransformReduce TransReduceType, typename Real> struct TransReduceOp { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return Real(0); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return Real(0); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return Real(0); } }; template<typename Real> struct TransReduceOp<SUMAB, Real> { const Real alpha_; const Real beta_; TransReduceOp(const Real& a, const Real& b) : alpha_(a), beta_(b) { } __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { if (beta_ == Real(0)) { return alpha_ * x; } else { return alpha_ * x + beta_ * output; } } }; template<typename Real> struct TransReduceOp<SUM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<MAX, Real> { __forceinline__ __device__ Real InitValue() const { return sizeof(Real) == sizeof(float) ? -CUDART_INF_F : -CUDART_INF; } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return fmax(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<MIN, Real> { __forceinline__ __device__ Real InitValue() const { return sizeof(Real) == sizeof(float) ? CUDART_INF_F : CUDART_INF; } __forceinline__ __device__ Real Transform(const Real& x) const { return x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return min(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<ABSMAX, Real> { __forceinline__ __device__ Real InitValue() const { return sizeof(Real) == sizeof(float) ? -CUDART_INF_F : -CUDART_INF; } __forceinline__ __device__ Real Transform(const Real& x) const { return fabs(x); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return fmax(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return fabs(x); } }; template<typename Real> struct TransReduceOp<LINFNORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return abs(x); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return fmax(a, b); } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<L2NORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return x * x; } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return sqrt(x); } }; template<typename Real> struct TransReduceOp<L1NORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return abs(x); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<L0NORM, Real> { __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return Real(x == Real(0) ? 0 : 1); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return x; } }; template<typename Real> struct TransReduceOp<LPNORM, Real> { const Real power_; TransReduceOp(const Real& p) : power_(p) { } __forceinline__ __device__ Real InitValue() const { return Real(0); } __forceinline__ __device__ Real Transform(const Real& x) const { return pow(abs(x), power_); } __forceinline__ __device__ Real Reduce(const Real& a, const Real& b) const { return a + b; } __forceinline__ __device__ Real PostReduce(const Real& x, const Real& output) const { return pow(x, Real(1) / power_); } }; // Vector reduce. template<EnumTransformReduce TransReduceType, typename Real> __global__ static void _vec_transform_reduce( const Real* v, Real* result, const int dim, const int inc, const TransReduceOp<TransReduceType, Real> op) { __shared__ Real sdata[CU1DBLOCK]; Real tdata = op.InitValue(); const int tid = threadIdx.x; const int vec_len = dim * inc; const int grid_stride = gridDim.x * blockDim.x * inc; int i = (blockIdx.x * blockDim.x + tid) * inc; // Grid reduce. Loop over the whole vector v. for (; i < vec_len; i += grid_stride) { tdata = op.Reduce(tdata, op.Transform(v[i])); } sdata[tid] = tdata; __syncthreads(); // Tree reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); } __syncthreads(); } // Reduce last warp. Threads implicitly synchronized within a warp. if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) { sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); } } // Output to vector result. if (tid == 0) result[blockIdx.x] = op.PostReduce(sdata[0], result[blockIdx.x]); } // Reduce a matrix 'mat' to a column vector 'result' template<EnumTransformReduce TransReduceType, typename Real> __global__ static void _transform_reduce_mat_cols( Real *result, const Real *mat, const MatrixDim d, const TransReduceOp<TransReduceType, Real> op) { __shared__ Real sdata[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int row_start = i * d.stride; Real tdata = op.InitValue(); for (int j = tid; j < d.cols; j += CU1DBLOCK) { tdata = op.Reduce(tdata, op.Transform(mat[row_start + j])); } sdata[tid] = tdata; __syncthreads(); // Tree reduce # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); __syncthreads(); } // Reduce last warp. Threads implicitly synchronized within a warp. if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) sdata[tid] = op.Reduce(sdata[tid], sdata[tid + shift]); } // Output to vector result. if (tid == 0) { result[i] = op.PostReduce(sdata[0], result[i]); } } template<EnumTransformReduce TransReduceType, typename Real> __global__ static void _group_transform_reduce( Real *y, const Real *x, const MatrixDim d, const int src_stride, const int group_size, const TransReduceOp<TransReduceType, Real> op) { __shared__ Real sreduction[CU1DBLOCK]; const int i = blockIdx.x; const int x_start = i * src_stride; const int y_start = i * d.stride; const int threads_per_group = blockDim.x; // Reduce n groups per thread block const int n = blockDim.y; const int len = group_size * n; // linear thread id const int tid = threadIdx.y * threads_per_group + threadIdx.x; int j = threadIdx.y * group_size + threadIdx.x; // col-id of *x int group_id = threadIdx.y; // col-id of *y int group_end = x_start + (group_id + 1) * group_size; while (group_id < d.cols) { // reduce to threads_per_group elements per group int x_idx = x_start + j; Real treduction = op.Transform(x[x_idx]); x_idx += threads_per_group; while (x_idx < group_end) { treduction = op.Reduce(treduction, op.Transform(x[x_idx])); x_idx += threads_per_group; } sreduction[tid] = treduction; if (threads_per_group > warpSize) { __syncthreads(); } // tree-reduce to 2x warpSize elements per group # pragma unroll for (int shift = threads_per_group / 2; shift > warpSize; shift >>= 1) { if (threadIdx.x < shift) { sreduction[tid] = op.Reduce(sreduction[tid], sreduction[tid + shift]); } __syncthreads(); } // Warp-reduce to 1 element per group. // Threads implicitly synchronized within the warp. const int warp_reduce_size = threads_per_group / 2 < warpSize ? threads_per_group / 2 : warpSize; if (threadIdx.x < warp_reduce_size) { # pragma unroll for (int shift = warp_reduce_size; shift > 0; shift >>= 1) { sreduction[tid] = op.Reduce(sreduction[tid], sreduction[tid + shift]); } } // Store the result. if (threadIdx.x == 0) { y[y_start + group_id] = op.PostReduce(sreduction[tid], y[y_start + group_id]); } j += len; group_end += len; group_id += n; } } template<typename Real> __global__ static void _vec_apply_floor(Real *v, Real floor_val, float *count, int dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { if (v[i] < floor_val) { v[i] = floor_val; count[i] = 1; } else { count[i] = 0; } } } template<typename Real> __global__ static void _vec_apply_ceiling(Real *v, Real ceiling_val, float *count, int dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { if (v[i] > ceiling_val) { v[i] = ceiling_val; count[i] = 1; } else { count[i] = 0; } } } template<typename Real> __global__ static void _apply_pow(Real* mat, Real power, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) { if (power == 1.0) return; if (power == 2.0) { mat[index] = mat[index] * mat[index]; } else if (power == 0.5) { if (!(mat[index] >= 0.0)) return; mat[index] = sqrt(mat[index]); } else { mat[index] = pow(mat[index], power); } } } template<typename Real> __global__ static void _apply_pow_abs(Real* mat, Real power, bool include_sign, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) { if (include_sign == true && mat[index] < 0) { if (power == 1.0) mat[index] = -std::abs(mat[index]); if (power == 2.0) { mat[index] = -mat[index] * mat[index]; } else if (power == 0.5) { mat[index] = -sqrt(std::abs(mat[index])); } else { mat[index] = -pow(std::abs(mat[index]), power); } } else { if (power == 1.0) mat[index] = std::abs(mat[index]); if (power == 2.0) { mat[index] = mat[index] * mat[index]; } else if (power == 0.5) { mat[index] = sqrt(std::abs(mat[index])); } else if (power < 0.0 && mat[index] == 0.0) { mat[index] = 0.0; } else { mat[index] = pow(std::abs(mat[index]), power); } } } } template<typename Real> __global__ static void _apply_heaviside(Real* mat, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) mat[index] = (mat[index] > 0.0 ? 1.0 : 0.0); } template<typename Real> __global__ static void _apply_floor(Real* mat, Real floor_val, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index int index = i + j * d.stride; if (i < d.cols && j < d.rows) { mat[index] = max(mat[index], floor_val); } } template<typename Real> __global__ static void _copy_cols(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[i], dst_index = j * dst_dim.stride + i; if (index >= 0) { int src_index = j * src_stride + reorder[i]; Real val = src[src_index]; dst[dst_index] = val; } else { dst[dst_index] = 0.0; } } } template<typename Real> __global__ static void _add_cols(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[i], dst_index = j * dst_dim.stride + i; if (index >= 0) { int src_index = j * src_stride + index; Real val = src[src_index]; dst[dst_index] += val; } } } template<typename Real> __global__ static void _copy_rows(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[j], dst_index = j * dst_dim.stride + i; if (index >= 0) { int src_index = reorder[j] * src_stride + i; Real val = src[src_index]; dst[dst_index] = val; } else { //dst[dst_index] = 0; } } } template<typename Real> __global__ static void _copy_rows(Real* dst, const Real * const *src, MatrixDim dst_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; const Real *pointer = src[j]; if (pointer != NULL) { dst[dst_index] = pointer[i]; } else { dst[dst_index] = 0; } } } template<typename Real> __global__ static void _copy_to_rows(Real* const * dst, const Real *src, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < src_dim.cols && j < src_dim.rows) { Real *pointer = dst[j]; if (pointer != NULL) { pointer[i] = src[j * src_dim.stride + i]; } } } template<typename Real> __global__ static void _add_rows(Real alpha, Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (reorder[j] >= 0) { int src_index = reorder[j] * src_stride + i; dst[dst_index] += alpha * src[src_index]; } } } template<typename Real> __global__ static void _mul_rows(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (reorder[j] >= 0) { int src_index = reorder[j] * src_stride + i; dst[dst_index] *= src[src_index]; } } } template<typename Real> __global__ static void _div_rows(Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (reorder[j] >= 0) { int src_index = reorder[j] * src_stride + i; if (src[src_index] == 0 && fabs(dst[dst_index]) <= 1e-6) dst[dst_index] = 0; else dst[dst_index] /= src[src_index]; } } } template<typename Real> __global__ static void _add_rows(Real alpha, Real* dst, const Real * const *src, MatrixDim dst_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int dst_index = j * dst_dim.stride + i; if (src[j] != NULL) { dst[dst_index] += alpha * src[j][i]; } } } template<typename Real> __global__ static void _add_to_rows(Real alpha, Real* dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim src_dim, int dst_stride) { int c = blockIdx.x * blockDim.x + threadIdx.x; // col index int r = blockIdx.y * blockDim.y + threadIdx.y; // row index if (c < src_dim.cols && r < src_dim.rows) { int src_index = r * src_dim.stride + c; if (reorder[r] >= 0) { int dst_index = reorder[r] * dst_stride + c; dst[dst_index] += alpha * src[src_index]; } } } template<typename Real> __global__ static void _add_to_rows(Real alpha, Real* const * dst, const Real *src, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < src_dim.cols && j < src_dim.rows) { if (dst[j] != NULL) { dst[j][i] += alpha * src[j * src_dim.stride + i]; } } } template<typename Real> __global__ static void _apply_ceiling(Real* mat, Real ceiling_val, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index = i + j * d.stride; if (i < d.cols && j < d.rows) { mat[index] = min(mat[index], ceiling_val); } } template<typename Real> __global__ static void _invert_elements(Real* data, MatrixDim d) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index = i + j * d.stride; if (i < d.cols && j < d.rows) data[index] = 1.0 / data[index]; } // matrix-wise, do data = alpha * data + beta * A * B^T, // where B is a block matrix. template<typename Real> __global__ static void _add_mat_blockmat_trans(Real *data, MatrixDim dim, const Real *A_data, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, Real alpha, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index into "data" int j = blockIdx.y * blockDim.y + threadIdx.y; // block-index into B. if (i >= A_num_rows || j >= B_num_blocks) return; const CuBlockMatrixData &cu_data = B_cu_data[j]; // BT means B transposed. int BT_row_start = cu_data.col_offset, BT_col_start = cu_data.row_offset, BT_num_rows = cu_data.matrix_dim.cols, BT_num_cols = cu_data.matrix_dim.rows, BT_col_stride = cu_data.matrix_dim.stride; // Cast from void; const Real *B_data = static_cast<Real*>(cu_data.matrix_data); // we avoided a bunch of hassle by doing this (relates to Ansi-C requirement). for (int k = 0; k < BT_num_cols; k++) { const Real *this_BT_col = B_data + k * BT_col_stride; const Real *this_A_row = A_data + i * A_row_stride + BT_row_start * A_col_stride; // this_A_row points to the element A[i][BT_row_start], it's really just // part of this row of A. Real sum = 0.0; for (int l = 0; l < BT_num_rows; l++) // l indexes rows of B. sum += this_BT_col[l] * this_A_row[l * A_col_stride]; int index = i * dim.stride + (k + BT_col_start); data[index] = alpha * sum + beta * data[index]; } } template<typename Real> __global__ static void _add_mat_blockmat(Real *data, MatrixDim dim, const Real *A_data, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, Real alpha, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index into "data" int j = blockIdx.y * blockDim.y + threadIdx.y; // block-index into B. if (i >= A_num_rows || j >= B_num_blocks) return; const CuBlockMatrixData &block_data = B_cu_data[j]; int B_row_start = block_data.row_offset, B_col_start = block_data.col_offset, B_num_rows = block_data.matrix_dim.rows, B_num_cols = block_data.matrix_dim.cols, B_row_stride = block_data.matrix_dim.stride; // Cast from void; const Real *B_data = static_cast<Real*>(block_data.matrix_data); // we avoided a bunch of hassle by doing this (relates to Ansi-C requirement). for (int k = 0; k < B_num_cols; k++) { const Real *this_B_col = B_data + k; const Real *this_A_row = A_data + i * A_row_stride + B_row_start * A_col_stride; // this_A_row points to the element A[i][B_row_start], it's really just // part of this row of A. Real sum = 0.0; for (int l = 0; l < B_num_rows; l++) // l indexes rows of B. sum += this_B_col[l * B_row_stride] * this_A_row[l * A_col_stride]; int index = i * dim.stride + (k + B_col_start); data[index] = alpha * sum + beta * data[index]; } } // For a block matrix B, does B = alpha * C * D + beta * B. // the (x,y,z) indices are the block index, then the row // and column indices within the block. Note: transposition of C and D // is handled by swapping the (num_rows,num_cols) and (row_stride,col_stride), // so it's invisible to this code. The num-cols and num-rows of C and D // are only provided to the extent that they are not already determined // by other quantities. template<typename Real> __global__ static void _block_add_mat_mat(CuBlockMatrixData *B_cu_data, int num_blocks, const Real *C_data, int C_num_cols, int C_row_stride, int C_col_stride, const Real *D_data, int D_row_stride, int D_col_stride, Real alpha, Real beta) { int b = blockIdx.x * blockDim.x + threadIdx.x; // block-index into B. int i = blockIdx.y * blockDim.y + threadIdx.y; // row-index into b'th block int j = blockIdx.z * blockDim.z + threadIdx.z; // col-index into b'th block if (b >= num_blocks) return; const CuBlockMatrixData &block_data = B_cu_data[b]; if (i >= block_data.matrix_dim.rows || j >= block_data.matrix_dim.cols) return; // we're outside the dimensions of the b'th block. // B_elem is the element of B we're writing to. Real *B_elem = reinterpret_cast<Real*>(block_data.matrix_data) + i * block_data.matrix_dim.stride + j; Real B_val = *B_elem; // B_row and B_col are the (row, col) index into the full matrix B. int B_row = block_data.row_offset + i, B_col = block_data.col_offset + j; const Real *C_row_data = C_data + C_row_stride * B_row, *D_col_data = D_data + D_col_stride * B_col; Real sum = 0.0; for (int k = 0; k < C_num_cols; k++) { sum += C_row_data[k * C_col_stride] * D_col_data[k * D_row_stride]; } *B_elem = alpha * sum + beta * B_val; } template<typename Real> __global__ static void _blockadd_mat_blockmat_trans(Real *data, MatrixDim dim, const Real *A_data, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, Real alpha, Real beta) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index into "data" int j = blockIdx.y * blockDim.y + threadIdx.y; // block-index into B. if (i >= A_num_rows || j >= B_num_blocks) return; const CuBlockMatrixData &cu_data = B_cu_data[j]; // BT means B transposed. int BT_row_start = cu_data.col_offset, BT_col_start = cu_data.row_offset, BT_num_rows = cu_data.matrix_dim.cols, BT_num_cols = cu_data.matrix_dim.rows, BT_col_stride = cu_data.matrix_dim.stride; // Cast from void; const Real *B_data = static_cast<Real*>(cu_data.matrix_data); // we avoided a bunch of hassle by doing this (relates to Ansi-C requirement). for (int k = 0; k < BT_num_cols; k++) { const Real *this_BT_col = B_data + k * BT_col_stride; const Real *this_A_row = A_data + i * A_row_stride + BT_row_start * A_col_stride; // this_A_row points to the element A[i][BT_row_start], it's really just // part of this row of A. Real sum = 0.0; for (int l = 0; l < BT_num_rows; l++) // l indexes rows of B. sum += this_BT_col[l] * this_A_row[l * A_col_stride]; int index = i * dim.stride + (k + BT_col_start); data[index] = alpha * sum + beta * data[index]; } } template<typename Real> __global__ static void _sum_column_ranges(Real *data, MatrixDim dim, const Real *src_data, MatrixDim src_dim, const Int32Pair *indices) { int col = blockIdx.x * blockDim.x + threadIdx.x; int row = blockIdx.y * blockDim.y + threadIdx.y; if (row >= dim.rows || col >= dim.cols) return; int dst_index = row * dim.stride + col, src_start_index = row * src_dim.stride + indices[col].first, src_end_index = row * src_dim.stride + indices[col].second; Real sum = 0.0; for (int index = src_start_index; index < src_end_index; index++) sum += src_data[index]; data[dst_index] = sum; } template<typename Real> __global__ static void _add_row_ranges(Real *data, MatrixDim dim, const Real *src_data, MatrixDim src_dim, const Int32Pair *indexes) { int col = blockIdx.x * blockDim.x + threadIdx.x; int row = blockIdx.y * blockDim.y + threadIdx.y; if (row >= dim.rows || col >= dim.cols) return; int dst_index = row * dim.stride + col; int src_index_start = indexes[row].first, src_index_end = indexes[row].second; for (int row_index = src_index_start; row_index < src_index_end; row_index++) data[dst_index] += src_data[row_index * src_dim.stride + col]; } template<typename Real> __global__ static void _soft_hinge(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; // compute the function y[index] = log(1 + exp(x[index])) if (i < d.cols && j < d.rows) { Real val = x[src_index], result; if (val >= 10.0) result = val; // function approaches y=x as x gets large else result = log1p(exp(val)); y[dst_index] = result; } } template<typename Real> __global__ static void _group_pnorm(Real *y, const Real *x, MatrixDim d, int src_stride, int group_size, Real power) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; if (j < d.rows && i < d.cols) { int dst_index = i + j * d.stride; Real tmp = 0; int src_begin_index = i * group_size + j * src_stride; int src_end_index = src_begin_index + group_size; for (int src_index = src_begin_index; src_index < src_end_index; src_index++) { tmp += pow(std::abs(x[src_index]), power); } tmp = pow(tmp, Real(1.0 / power)); if (!isnan(tmp)) { y[dst_index] = tmp; } else { Real max_value = x[src_begin_index], min_value = max_value; for (int src_index = src_begin_index + 1; src_index < src_end_index; src_index++) { if (x[src_index] > max_value) max_value = x[src_index]; if (x[src_index] < min_value) min_value = x[src_index]; } tmp = 0.0; // let max_value be the largest abs(value) Real max_abs_value = (max_value > -min_value ? max_value : -min_value); if (max_abs_value == 0) { y[dst_index] = 0.0; } else { for (int src_index = src_begin_index; src_index < src_end_index; src_index++) { Real x_scaled = x[src_index] / max_abs_value; tmp += pow(std::abs(x_scaled), Real(power)); } y[dst_index] = pow(tmp, Real(1.0 / power)) * max_abs_value; } } } } /* * cu:: */ template<typename Real> __global__ static void _sigmoid(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real res = 1.0 / (1.0 + exp(-x[src_index])); y[dst_index] = res; } } template<typename Real> __global__ static void _diff_sigmoid(Real*eout, const Real*e, const Real*y, MatrixDim d, int e_stride, int y_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride; int e_index = i + j * e_stride; int y_index = i + j * y_stride; if (i < d.cols && j < d.rows) eout[dst_index] = y[y_index] * (1.0 - y[y_index]) * e[e_index]; } template<typename Real> __global__ static void _tanh(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real exp_2x = exp(2.0 * x[src_index]); Real res; if (isinf(exp_2x)) { res = 1.0; } else { res = (exp_2x - 1.0) / (exp_2x + 1.0); } y[dst_index] = res; } } template<typename Real> __global__ static void _diff_tanh(Real*eout, const Real*e, const Real*y, MatrixDim d, int e_stride, int y_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride; int e_index = i + j * e_stride; int y_index = i + j * y_stride; if (i < d.cols && j < d.rows) eout[dst_index] = (1.0 - y[y_index] * y[y_index]) * e[e_index]; } /* This function copies x to y while bounding the elements away from zero using the scalar function: y = x if x <= -epsilon or x >= +epsilon +epsilon if 0 <= x < epsilon -epsilon if -epsilon < x < 0. where: x is the source matrix, of dimension and stride given by d epsilon > 0 y is the destination matrix, with the num-rows and num-cols given by d, but stride given by y_stride. */ template<typename Real> __global__ static void _ensure_nonzero(const Real *x, MatrixDim d, Real epsilon, int y_stride, Real *y) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int x_index = i + j * d.stride, y_index = i + j * y_stride; if (i < d.cols && j < d.rows) { Real src = x[x_index], dst; if (src <= -epsilon || src >= epsilon) dst = src; else if (src >= 0) dst = epsilon; else dst = -epsilon; __syncthreads(); // This allows it to do consolidated write below, which // should improve speed. y[y_index] = dst; } } template<typename Real> __global__ static void _parametric_relu(Real* y, const Real* x, MatrixDim d, int src_stride, const Real* a, const Real* b) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real res = (x[src_index] > 0.0) ? a[i] * x[src_index] : b[i] * x[src_index]; y[dst_index] = res; } } template<typename Real> __global__ static void _diff_parametric_relu(Real* eout, const Real* e, const Real* y, MatrixDim d, int e_stride, int y_stride, const Real* a, const Real* b) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride; int e_index = i + j * e_stride; int y_index = i + j * y_stride; if (i < d.cols && j < d.rows ) eout[dst_index] = (y[y_index] > 0.0 ? a[i] * e[e_index] : b[i] * e[e_index]); } template<typename Real> __global__ static void _heaviside(Real*y, const Real*x, MatrixDim d, int src_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dst_index = i + j * d.stride, src_index = i + j * src_stride; if (i < d.cols && j < d.rows) { Real res = (x[src_index] > 0.0 ? 1.0 : 0.0); y[dst_index] = res; } } /* template<typename Real> __global__ static void _softmax_reduce(Real*y, const Real*x, MatrixDim d, int src_stride, Real *logsum) { __shared__ Real smem[CU1DBLOCK]; const int i = blockIdx.x; const int x_start = i * src_stride; const int y_start = i * d.stride; const int tid = threadIdx.x; // find max element of the row // reduce to CU1DBLOCK elements per row. Real tmax = sizeof(Real) == sizeof(float) ? -CUDART_INF_F : -CUDART_INF; for (int j = tid; j < d.cols; j += CU1DBLOCK) { tmax = fmax(tmax, x[x_start + j]); } smem[tid] = tmax; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } } // broadcast max to all threads __syncthreads(); Real max = smem[0]; // sum_j(exp(x(i,j)-max)) // reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < d.cols; j += CU1DBLOCK) { tsum += exp(x[x_start + j] - max); } smem[tid] = tsum; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] += smem[tid + shift]; } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] += smem[tid + shift]; } } // broadcast sum to all threads __syncthreads(); Real inv_sum = Real(1) / smem[0]; // normalize the row for (int j = tid; j < d.cols; j += CU1DBLOCK) { y[y_start + j] = exp(x[x_start + j] - max) * inv_sum; } } */ template<typename Real> __global__ static void _softmax_reduce(Real*y, const Real*x, MatrixDim d, int src_stride, Real *logsum) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = x[threadIdx.x+j*src_stride]; for(int i=1; i<steps; ++i) { if(threadIdx.x+i*THREADS < d.cols && aux[threadIdx.x] < x[threadIdx.x+i*THREADS+j*src_stride]) aux[threadIdx.x] = x[threadIdx.x+i*THREADS+j*src_stride]; } //get the maximum value int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads && aux[threadIdx.x] < aux[threadIdx.x+halfPoint]) aux[threadIdx.x] = aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } Real max = aux[0]; __syncthreads(); // subtract max, apply exp, sum up... y[threadIdx.x+j*d.stride] = exp(x[threadIdx.x+j*src_stride] - max); aux[threadIdx.x] = y[threadIdx.x+j*d.stride]; for(int i=1; i<steps; i++) { if(threadIdx.x+i*THREADS < d.cols) { y[threadIdx.x+i*THREADS+j*d.stride] = exp(x[threadIdx.x+i*THREADS+j*src_stride] - max); aux[threadIdx.x] += y[threadIdx.x+i*THREADS+j*d.stride]; } } nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } Real sum = aux[0]; __syncthreads(); if (threadIdx.x == 0 && logsum != NULL) logsum[j] = log(sum)+max; // denominator log sum //normalize by sum... for(int i=0; i<steps; i++) { if(threadIdx.x+i*THREADS < d.cols) { y[threadIdx.x+i*THREADS+j*d.stride] = y[threadIdx.x+i*THREADS+j*d.stride] / sum; } } } template<typename Real> __global__ static void _log_softmax_reduce(Real *y, const Real *x, MatrixDim d, int src_stride) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; // Maximum step 1: loads input data to <aux>. If <d.cols> is larger than // <blockDim.x>, then we do a first pass filtering and only // keep a <blockDim.x> size array. aux[threadIdx.x] = x[threadIdx.x + j * src_stride]; for (int i = 1; i < steps; ++i) { if (threadIdx.x + i * THREADS < d.cols && aux[threadIdx.x] < x[threadIdx.x + i * THREADS + j * src_stride]) aux[threadIdx.x] = x[threadIdx.x + i * THREADS + j * src_stride]; } // Maximum step 2: the standard max reduce. int nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); if (threadIdx.x < halfPoint) { if (threadIdx.x + halfPoint < nTotalThreads && aux[threadIdx.x] < aux[threadIdx.x + halfPoint]) aux[threadIdx.x] = aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); } Real max = aux[0]; __syncthreads(); // Log sum step 1: substracts max, and takes exponentials. y[threadIdx.x + j * d.stride] = x[threadIdx.x + j * src_stride] - max; aux[threadIdx.x] = exp(y[threadIdx.x + j * d.stride]); for (int i = 1; i < steps; ++i) { if (threadIdx.x + i * THREADS < d.cols) { y[threadIdx.x + i * THREADS + j * d.stride] = x[threadIdx.x + i * THREADS + j * src_stride] - max; aux[threadIdx.x] += exp(y[threadIdx.x + i * THREADS + j * d.stride]); } } // Log sum step 2: comptes summation and then takes logarithm. nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); if (threadIdx.x < halfPoint) { if (threadIdx.x + halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); } Real log_sum = log(aux[0]); __syncthreads(); // Computes log softmax. for (int i = 0; i < steps; ++i) { if (threadIdx.x + i * THREADS < d.cols) { y[threadIdx.x + i * THREADS + j * d.stride] -= log_sum; } } } // The output y_i = scale * x_i, // and we want to RMS value of the y_i to equal target_rms, // so y^t y = D * target_rms^2 (if y is one row of the input). // we need to have scale = 1.0 / sqrt(x^t x / (D * target_rms^2)). // there is also flooring involved, to avoid division-by-zero // problems. It's important for the backprop, that the floor's // square root is exactly representable as float. // If add_log_stddev is true, log(max(epsi, sqrt(x^t x / D))) // is an extra dimension of the output. // // 1D grid is used. Each 256-thread block works on 1 row of the data matrix. // The block is also of 1D. Strided memory access is used if the length of the // row is longer than 256. template<typename Real> __global__ static void _normalize_per_row(Real *y, int y_stride, const Real *x, MatrixDim x_d, Real target_rms, bool add_log_stddev) { const int i = blockIdx.x; const int tid = threadIdx.x; const Real* x_row = x + i * x_d.stride; __shared__ Real ssum[CU1DBLOCK]; // Reduce x_j^2 to CU1DBLOCK elements per row Real tsum = Real(0); for (int j = tid; j < x_d.cols; j += CU1DBLOCK) { tsum += x_row[j] * x_row[j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) ssum[tid] += ssum[tid + shift]; __syncthreads(); } // Reduce last warp to 1 element per row. // Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } const Real kSquaredNormFloor = 1.3552527156068805425e-20; // 2^-66 if (tid == 0) { ssum[0] = sqrt( fmax(ssum[0] / (target_rms * target_rms * x_d.cols), kSquaredNormFloor)); } // Broadcast floored stddev to all threads. __syncthreads(); const Real stddev_div_target_rms = ssum[0]; const Real scale = Real(1) / stddev_div_target_rms; // Store normalized input to output Real* y_row = y + i * y_stride; for (int j = tid; j < x_d.cols; j += CU1DBLOCK) { y_row[j] = x_row[j] * scale; } if (tid == 0 && add_log_stddev) { y_row[x_d.cols] = log(stddev_div_target_rms * target_rms); } } template<typename Real> __global__ static void _diff_normalize_per_row(Real *id, int id_stride, const Real *iv, MatrixDim iv_dim, const Real* od, int od_stride, Real target_rms, bool add_log_stddev) { const Real kSquaredNormFloor = 1.3552527156068805425e-20; // 2^-66 const Real kInvNormFloor = 8589934592.0; const int tid = threadIdx.x; const int i = blockIdx.x; const Real* iv_row = iv + i * iv_dim.stride; const Real* od_row = od + i * od_stride; // reduce to CU1DBLOCK elements per row Real dot_products = Real(0); Real in_norm = Real(0); for (int j = tid; j < iv_dim.cols; j += CU1DBLOCK) { const Real iv_ij = iv_row[j]; dot_products += iv_ij * od_row[j]; in_norm += iv_ij * iv_ij; } __shared__ Real sprod[CU1DBLOCK]; __shared__ Real snorm[CU1DBLOCK]; sprod[tid] = dot_products; snorm[tid] = in_norm; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { sprod[tid] += sprod[tid + shift]; snorm[tid] += snorm[tid + shift]; } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { sprod[tid] += sprod[tid + shift]; snorm[tid] += snorm[tid + shift]; } } // broadcast the sum results __syncthreads(); dot_products = sprod[0]; in_norm = snorm[0]; Real log_stddev_deriv; if (add_log_stddev) { log_stddev_deriv = Real(1) / max(in_norm, iv_dim.cols * kSquaredNormFloor) * od_row[iv_dim.cols]; } const Real inv_d_scaled = Real(1) / (iv_dim.cols * target_rms * target_rms); in_norm = Real(1) / sqrt(max(in_norm * inv_d_scaled, kSquaredNormFloor)); const Real f = in_norm == kInvNormFloor ? Real(0) : in_norm; dot_products *= f * f * f * inv_d_scaled; for (int j = tid; j < iv_dim.cols; j += CU1DBLOCK) { const Real iv_ij = iv_row[j]; Real id_ij = id[i * id_stride + j]; if (add_log_stddev) { id_ij += log_stddev_deriv * iv_ij; } if (id != od) { id_ij += in_norm * od_row[j]; } else { id_ij *= in_norm; } id_ij -= dot_products * iv_ij; id[i * id_stride + j] = id_ij; } } /* // Per-row log-softmax operation on 'x', with writing to 'y'. // note, x and y may point to the same memory. This is equivalent to setting // matrix y to matrix x and then, for each row of y, subtracting the offset that // will make exp(y.row[j]) sum to 1 for each row j. // // It expects to be called with CU1DBLOCK threads. // The number of blocks [i.e. the gridDim] equals to y_dim.rows, // so one block of threads processes each row. x and y are // expected to have the same dimension, but possibly different row strides. template<typename Real> __global__ static void _log_softmax_reduce(Real* y, const Real* x, MatrixDim y_dim, int x_stride) { __shared__ Real smem[CU1DBLOCK]; const int i = blockIdx.x; const int x_start = i * x_stride; const int y_start = i * y_dim.stride; const int tid = threadIdx.x; // find max element of the row // reduce to CU1DBLOCK elements per row. Real tmax = -1e20; for (int j = tid; j < y_dim.cols; j += CU1DBLOCK) { tmax = fmax(tmax, x[x_start + j]); } smem[tid] = tmax; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] = fmax(smem[tid], smem[tid + shift]); } } // broadcast max to all threads __syncthreads(); Real max = smem[0]; // sum_j(exp(x(i,j)-max)) // reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < y_dim.cols; j += CU1DBLOCK) { tsum += exp(x[x_start + j] - max); } smem[tid] = tsum; __syncthreads(); // reduce to 2x warpSize elements per row # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { smem[tid] += smem[tid + shift]; } __syncthreads(); } // reduce to 1 element per row if (tid < warpSize) { for (int shift = warpSize; shift > 0; shift >>= 1) { smem[tid] += smem[tid + shift]; } } // broadcast sum to all threads __syncthreads(); Real log_sum = log(smem[0]); // normalize the row for (int j = tid; j < y_dim.cols; j += CU1DBLOCK) { y[y_start + j] = x[x_start + j] - max - log_sum; } } */ template<typename Real> __global__ static void _splice(Real* y, const Real* x, const int32_cuda* off, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d_out.stride; if (i < d_out.cols && j < d_out.rows) { int32_cuda src_col = i % d_in.cols; int32_cuda src_row = j + off[i / d_in.cols]; if (src_row < 0) src_row = 0; if (src_row >= d_in.rows) src_row = d_in.rows - 1; y[index] = x[src_col + src_row * d_in.stride]; } } template<typename Real> __global__ static void _take_mean(const Real* x, Real* y, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index1 = i + j * d_in.stride; int32_cuda index2 = j + i * d_in.stride; if (i <= j && j < d_in.rows) { int32_cuda index_sp = (j * (j + 1) / 2) + i; y[index_sp] = 0.5 * (x[index1] + x[index2]); } } template<typename Real> __global__ static void _take_lower(const Real* x, Real* y, MatrixDim d_in) { int i = blockIdx.x * blockDim.x + threadIdx.x; // row-index int j = blockIdx.y * blockDim.y + threadIdx.y; // col-index if (j > i || i >= d_in.rows) return; int index = i * d_in.stride + j; Real val = x[index]; int index_sp = (i * (i + 1) / 2) + j; y[index_sp] = val; } template<typename Real> __global__ static void _take_upper(const Real* x, Real* y, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row-index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // col-index if (j < i || j >= d_in.rows) return; int32_cuda index = i * d_in.stride + j; int32_cuda index_sp = (j * (j + 1) / 2) + i; y[index_sp] = x[index]; } template<typename Real> __global__ static void _vec_copy_diag_from_packed(Real* y, const Real* x, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda index = ((i + 1) * (i + 2) / 2) - 1; if (i < dim) { y[i] = x[index]; } } template<typename Real> __global__ static void _copy_from_sp(const Real* x, Real* y, MatrixDim dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // column index int j = blockIdx.y * blockDim.y + threadIdx.y; // if (i < dim.cols && j < dim.rows) { int dst_index = i + j * dim.stride, src_index; if (j <= i) { // no transpose src_index = (i * (i + 1) / 2) + j; } else { // transpose. src_index = (j * (j + 1) / 2) + i; } y[dst_index] = x[src_index]; } } template<typename Real> __global__ static void _copy(Real* y, const Real* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d_out.stride; if (i < d_out.cols && j < d_out.rows) { int32_cuda src_col = copy_from[i]; if (src_col >= 0 && src_col < d_in.cols) { y[index] = x[src_col + j * d_in.stride]; } else { y[index] = 1.0 / 0.0; } } } template<typename Real> __global__ static void _one(Real* x, int dim) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < dim) { x[i] = 1.0; } } template<typename Real> __global__ static void _randomize(Real* y, const Real* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d_out.stride; if (i < d_out.cols && j < d_out.rows) { int32_cuda src_row = copy_from[j]; y[index] = x[i + src_row * d_in.stride]; } } template<typename Real> __global__ static void _regularize_l1(Real* wei, Real* grad, Real l1, Real lr, MatrixDim d, int stride_grad) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda index = i + j * d.stride, grad_index = i + j * stride_grad; if (i < d.cols && j < d.rows) { if (wei[index] == 0.0) return; //skip L1 if zero weight! Real l1_signed = l1; if (wei[index] < 0.0) //flip sign l1_signed = -l1; Real before = wei[index]; //simulate update Real after = wei[index] - lr * grad[grad_index] - l1_signed; if ((after > 0.0) ^ (before > 0.0)) { //sign changed? wei[index] = 0.0; grad[grad_index] = 0.0; } else { wei[index] -= l1_signed; } } } template<typename Real> __global__ static void _find_row_max_id(const Real* mat, Real* vec_val, int32_cuda* vec_id, MatrixDim d) { const int32_cuda i = blockIdx.x; const int32_cuda base = i * d.stride; const int32_cuda tid = threadIdx.x; __shared__ Real smax[CU1DBLOCK]; __shared__ int32_cuda sidx[CU1DBLOCK]; Real tmax = -1e20; int32_cuda tidx = -1; // Loop over blocks for coalesced memory access. for (int32_cuda j = tid; j < d.cols; j += CU1DBLOCK) { const Real val = mat[base + j]; if (val > tmax) { tmax = val; tidx = j; } } smax[tid] = tmax; sidx[tid] = tidx; // Parallel reduce #pragma unroll for (int32_cuda num_working_threads = CU1DBLOCK / 2; num_working_threads >= warpSize; num_working_threads >>= 1) { __syncthreads(); if (tid < num_working_threads) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; sidx[tid] = sidx[tid + num_working_threads]; } } } // Warp reduce without __syncthreads() // (note.: synchronizes implicitly within a warp at the multiprocessor) if (tid < warpSize / 2) { #pragma unroll for (int32_cuda num_working_threads = warpSize / 2; num_working_threads > 0; num_working_threads >>= 1) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; sidx[tid] = sidx[tid + num_working_threads]; } } } if (tid == 0) { if (vec_val) { vec_val[i] = smax[0]; } vec_id[i] = sidx[0]; } } template<typename Real> __global__ static void _find_row_abs_max(const Real* mat, Real* row_max, MatrixDim d) { const int32_cuda i = blockIdx.x; const int32_cuda base = i * d.stride; const int32_cuda tid = threadIdx.x; __shared__ Real smax[CU1DBLOCK]; Real tmax = -1e20; // Loop over blocks for coalesced memory access. for (int32_cuda j = tid; j < d.cols; j += CU1DBLOCK) { const Real val = fabs(mat[base + j]); if (val > tmax) { tmax = val; } } smax[tid] = tmax; // Parallel reduce #pragma unroll for (int32_cuda num_working_threads = CU1DBLOCK / 2; num_working_threads >= warpSize; num_working_threads >>= 1) { __syncthreads(); if (tid < num_working_threads) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; } } } // Warp reduce without __syncthreads() // (note.: synchronizes implicitly within a warp at the multiprocessor) if (tid < warpSize / 2) { #pragma unroll for (int32_cuda num_working_threads = warpSize / 2; num_working_threads > 0; num_working_threads >>= 1) { if (smax[tid + num_working_threads] > smax[tid]) { smax[tid] = smax[tid + num_working_threads]; } } } if (tid == 0) { row_max[i] = smax[0]; } } template<typename Real> __global__ static void _diff_xent(const int32_cuda* vec_tgt, Real* mat_net_out, Real* vec_log_post, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; if (i > 0) return; if (j < d.rows) { int32_cuda index = vec_tgt[j] + j * d.stride; Real value = mat_net_out[index]; if (value < 1e-20) value = 1e-20; vec_log_post[j] = log(value); mat_net_out[index] -= 1.0; } } template<typename Real> __global__ static void _diff_softmax(Real* x, const MatrixDim dim, const Real* value, const int value_stride, const Real* diff, const int diff_stride) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int value_start = i * value_stride; const int diff_start = i * diff_stride; const int x_start = i * dim.stride; // Loop along the matrix row. Reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < dim.cols; j += CU1DBLOCK) { tsum += value[value_start + j] * diff[diff_start + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { ssum[tid] += ssum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element. Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // Broadcast result to all threads __syncthreads(); const Real pe = ssum[0]; // Apply element-wise x = value * (diff - pe) for (int j = tid; j < dim.cols; j += CU1DBLOCK) { x[x_start + j] = value[value_start + j] * (diff[diff_start + j] - pe); } } // Differentiate backward through the log softmax function. // "out_value" is the log softmax output. Does, for each row i, // in_deriv(i) = out_deriv(i) - sum(out_deriv(i)) .* exp(out_value(i)) // ???(i) is row-vector. // CUDA thread layout: 1 thread block (CU1DBLOCK == 256 threads) per matrix-row. template<typename Real> __global__ static void _diff_log_softmax(const MatrixDim in_deriv_dim, const Real* out_value, const int out_value_stride, const Real* out_deriv, const int out_deriv_stride, Real* in_deriv) { __shared__ Real ssum[CU1DBLOCK]; const int tid = threadIdx.x; const int i = blockIdx.x; const int out_value_start = i * out_value_stride; const int out_deriv_start = i * out_deriv_stride; const int in_deriv_start = i * in_deriv_dim.stride; // Loop along the matrix row. Reduce to CU1DBLOCK elements per row. Real tsum = Real(0); for (int j = tid; j < in_deriv_dim.cols; j += CU1DBLOCK) { tsum += out_deriv[out_deriv_start + j]; } ssum[tid] = tsum; __syncthreads(); // Tree reduce to 2x warpSize elements. # pragma unroll for (int shift = CU1DBLOCK / 2; shift > warpSize; shift >>= 1) { if (tid < shift) { ssum[tid] += ssum[tid + shift]; } __syncthreads(); } // Warp reduce to 1 element. Threads implicitly synchronized within a warp. if (tid < warpSize) { # pragma unroll for (int shift = warpSize; shift > 0; shift >>= 1) { ssum[tid] += ssum[tid + shift]; } } // Broadcast result to all threads __syncthreads(); const Real sum_e = ssum[0]; // Apply element-wise x = out_deriv - exp(value) * sum_e for (int j = tid; j < in_deriv_dim.cols; j += CU1DBLOCK) { in_deriv[in_deriv_start + j] = out_deriv[out_deriv_start + j] - exp(out_value[out_value_start + j]) * sum_e; } } /** this function computes the core part of the LSTM nonlinearity. @param [in] in A matrix, of dimension num_rows by 5*cell_dim (i.e. its num-cols must be a multiple of 5). The column-space is interpreted as 5 consecutive blocks, each of dimension cell_dim, which we name: (i_part, f_part, c_part, o_part, c_{t-1}). If 'have_dropout_mask' is nonzero, each row of 'in' will have 3 extra elements, interpreted as dropout masks/scales for i_t, f_t and o_t. @param [in] params A matrix, of dimension 3 by cell_dim, with rows containing the 3 diagonal parameter matrices used in LSTMs, namely w_{ic}, w_{fc} and w_{oc}. @param [out] out A matrix, of dimension num_rows by 2*cell_dim. The quantities c_t and m_t respectively are put there (in two blocks of column-dimension cell_dim), according to the following equations: i_t = Sigmoid(i_part + w_{ic}*c_{t-1}) f_t = Sigmoid(f_part + w_{fc}*c_{t-1}) c_t = f_t*c_{t-1} + i_t * Tanh(c_part) o_t = Sigmoid(o_part + w_{oc}*c_t) m_t = o_t * Tanh(c_t) We use 1D thread block with CU1DBLOCK threads. It works best when cell_dim is a multiple of CU1DBLOCK. We use 1d Grid. Each block is working on one row of the in and out matrices. */ template<typename Real> __global__ static void _lstm_nonlinearity(const Real* in, const int in_stride, const Real* params, const int params_stride, const int out_stride, const int cell_dim, const int have_dropout_mask, const int num_rows, Real* out) { const int tid = threadIdx.x; const int i = blockIdx.x; const Real* i_part = in + i * in_stride; const Real* f_part = in + i * in_stride + cell_dim; const Real* c_part = in + i * in_stride + cell_dim * 2; const Real* o_part = in + i * in_stride + cell_dim * 3; const Real* c_tm1 = in + i * in_stride + cell_dim * 4; const Real* w_ic = params; const Real* w_fc = params + params_stride; const Real* w_oc = params + params_stride * 2; Real* c_t = out + i * out_stride; Real* m_t = out + i * out_stride + cell_dim; Real i_scale = (have_dropout_mask ? in[i * in_stride + cell_dim * 5] : 1), f_scale = (have_dropout_mask ? in[i * in_stride + cell_dim * 5 + 1] : 1), o_scale = (have_dropout_mask ? in[i * in_stride + cell_dim * 5 + 2] : 1); for (int j = tid; j < cell_dim; j += CU1DBLOCK) { Real c_tm1_j = c_tm1[j]; Real i_t_j = Real(1) / (Real(1) + exp(-i_part[j] - w_ic[j] * c_tm1_j)); Real f_t_j = Real(1) / (Real(1) + exp(-f_part[j] - w_fc[j] * c_tm1_j)); Real c_t_j = f_t_j * f_scale * c_tm1_j + i_t_j * i_scale * tanh(c_part[j]); Real o_t_j = Real(1) / (Real(1) + exp(-o_part[j] - w_oc[j] * c_t_j)); c_t[j] = c_t_j; m_t[j] = o_t_j * o_scale * tanh(c_t_j); } } /** This function does the 'backward' pass corresponding to the function ComputeLstmNonlinearity. It's a little more complicated than you might expect because of the 'self-repair' mechanism that we use to prevent the sigmoid and tanh nonlinearities oversaturating, and because of the average-activation and average-derivative stats that we store for these nonlinearites (these stats are used both to control the self-repair mechanism, and for diagnostic purposes). Because the forward pass computes various intermediate values that are not output, this function actually has to do the same computations as the forward pass before it actually does the backprop. In the following description, `C` is for `cell_dim`, `N` is for `num_rows`. @param [in] input The same as in ComputeLstmNonlinearity(). A matrix, of dimension N by 5C (i.e. its num-cols must be a multiple of 5). The column-space is interpreted as 5 consecutive blocks, each of dimension C, which we name: (i_part, f_part, c_part, o_part, c_{t-1}). If 'have_dropout_mask' is nonzero, each row of 'in' will have 3 extra elements, interpreted as dropout masks/scales for i_t, f_t and o_t. @param [in] params The same as in ComputeLstmNonlinearity(). A matrix, of dimension 3 by C, with rows containing the three diagonal parameter matrices used in LSTMs, namely w_{ic}, w_{fc} and w_{oc}. @param [in] output_deriv A matrix, of dimension N by 2C, containing the derivative of the objective function we're backpropagating, w.r.t. the quantities c_t and m_t (in two blocks of column-dimension C). @param [in] deriv_sum_in This is used in the self-repair code to identify oversaturated nonlinearities. It is a matrix, of dimension 5 by C, corresponding to the totals of the derivatives of the 5 sigmoid and tanh nonlinearities, in they order they appear in the equations in the documentation of ComputeLstmNonlinearity() respectively, they appear in the equations for (i_t, f_t, c_t, o_t, m_t). This will be divided by 'count_in' to get the average derivative value so far, for each of the nonlinearities. @param [in] self_repair_config A vector of dimension 10, containing the configuration of the self-repair to be used for the 5 nonlinearities. The first 5 elements are the self_repair_lower_threshold values (typically 0.05 for sigmoid and 0.2 for tanh), and the next 5 elements are the corresponding self-repair-scales (typically 10^-5). @param [in] count_in The data-count that corresponds to the stats in 'deriv_sum_in' at entry to the function. This function should tolerate the count being zero (in that case, it is free to do the self-repair or not, as this should only happen on the 1st minibatch of each training job). @param [out] input_deriv May be NULL; if not, this function writes, to this location, the backpropagated derivative of the objective function w.r.t. the 'input' matrix. This matrix should have the same dimension as 'input' i.e. N by 5C. In addition to the regular backpropagated derivative, the output will include small values relating to 'self-repair'. @param [out] params_deriv May be NULL; if not, this is where this function *writes* [not adds] the backpropagated derivative of the objective function w.r.t. 'params'; it should have the same dimension as 'params' (3 by C). (This matrix will then be processed by the natural gradient code and added to the appropriate copy of the parameter matrix, outside this function). @param [out] value_sum_out Must be NULL if params_deriv is NULL; if not, a matrix of dimension 5 by C. This function *adds* to this location the total value of each of the sigmoid/tanh nonlinearities that it computes (this is for diagnostic purposes). @param [out] deriv_sum_out Must be NULL if params_deriv is NULL; if not, a matrix of dimension 5 by C; this function *adds* to this location the total of the derivative of each of the sigmoid/tanh nonlinearities that it computes (this is for diagnostic purposes and to control the self-repair). This function should tolerate the case when 'deriv_sum_out' points to the same data as 'deriv_sum_in'. @param [out] self_repair_sum_out Must be NULL if params_deriv is NULL; if not, a matrix of dimension 5 by C; this function *writes* to this location the sum of the number of times the self-repair code was activated (integer values 0 <= k <= N). This will be processed outside this function into self-repair stats for diagnostics. // Use 2D block (8x32 threads) as we need to compute column sum. // Use 1D grid to cover the data matrix `cell_dim`. */ template<typename Real> __global__ static void _diff_lstm_nonlinearity(const int cell_dim, const int have_dropout_mask, const int num_rows, const Real* input, const int input_stride, const Real* params, const int params_stride, const Real* output_deriv, const int output_deriv_stride, const double* deriv_sum_in, const int deriv_sum_in_stride, const Real* self_repair_config, double count, Real* input_deriv, const int input_deriv_stride, Real* params_deriv, const int params_deriv_stride, double* value_sum_out, const int value_sum_out_stride, double* deriv_sum_out, const int deriv_sum_out_stride, Real* self_repair_sum_out, const int self_repair_sum_out_stride) { __shared__ Real smem[CU1DBLOCK]; const int j = blockIdx.x * blockDim.x + threadIdx.x; const int tid = threadIdx.y * blockDim.x + threadIdx.x; const int grid_stride = gridDim.y * blockDim.y; const int i0 = blockIdx.y * blockDim.y + threadIdx.y; Real w_ic_deriv_sum = 0; Real w_fc_deriv_sum = 0; Real w_oc_deriv_sum = 0; Real i_t_value_sum = 0, i_t_deriv_sum = 0; Real f_t_value_sum = 0, f_t_deriv_sum = 0; Real c_part_value_sum = 0, c_part_deriv_sum = 0; Real o_t_value_sum = 0, o_t_deriv_sum = 0; Real c_t_value_sum = 0, c_t_deriv_sum = 0; bool update_sr[5]; if (j < cell_dim) { const Real w_ic = params[j]; const Real w_fc = params[params_stride + j]; const Real w_oc = params[2 * params_stride + j]; const Real* sr_config = self_repair_config; # pragma unroll for (int i = 0; i < 5; i++) { update_sr[i] = deriv_sum_in[i * deriv_sum_in_stride + j] < sr_config[i] * count; } const Real i_t_self_repair = (update_sr[0] ? sr_config[5] : 0); const Real f_t_self_repair = (update_sr[1] ? sr_config[6] : 0); const Real c_part_self_repair = (update_sr[2] ? sr_config[7] : 0); const Real o_t_self_repair = (update_sr[3] ? sr_config[8] : 0); const Real c_t_self_repair = (update_sr[4] ? sr_config[9] : 0); for (int i = i0; i < num_rows; i += grid_stride) { const Real i_part = input[i * input_stride + j]; const Real f_part = input[i * input_stride + j + cell_dim]; const Real c_part = input[i * input_stride + j + 2 * cell_dim]; const Real o_part = input[i * input_stride + j + 3 * cell_dim]; const Real c_prev = input[i * input_stride + j + 4 * cell_dim]; const Real i_scale = (have_dropout_mask ? input[i * input_stride + cell_dim * 5] : 1), f_scale = (have_dropout_mask ? input[i * input_stride + cell_dim * 5 + 1] :1), o_scale = (have_dropout_mask ? input[i * input_stride + cell_dim * 5 + 2] :1); const Real i_t = Real(1) / (1 + exp(-i_part - w_ic * c_prev)); const Real f_t = Real(1) / (1 + exp(-f_part - w_fc * c_prev)); const Real tanh_c_part = tanh(c_part); const Real c_t = f_t * f_scale * c_prev + i_t * i_scale * tanh_c_part; const Real o_t = 1 / (1 + exp(-o_part - w_oc * c_t)); const Real tanh_c_t = tanh(c_t); const Real i_t_deriv = i_t * (1 - i_t); const Real f_t_deriv = f_t * (1 - f_t); const Real c_part_deriv = 1 - tanh_c_part * tanh_c_part; const Real o_t_deriv = o_t * (1 - o_t); const Real c_t_deriv = 1 - tanh_c_t * tanh_c_t; if (params_deriv) { i_t_value_sum += i_t; f_t_value_sum += f_t; c_part_value_sum += tanh_c_part; o_t_value_sum += o_t; c_t_value_sum += tanh_c_t; i_t_deriv_sum += i_t_deriv; f_t_deriv_sum += f_t_deriv; c_part_deriv_sum += c_part_deriv; o_t_deriv_sum += o_t_deriv; c_t_deriv_sum += c_t_deriv; } const Real dc_t_out = output_deriv[i * output_deriv_stride + j]; const Real dm_t = output_deriv[i * output_deriv_stride + j + cell_dim]; const Real dtanh_c_t = o_t * o_scale * dm_t; const Real do_t = o_scale * tanh_c_t * dm_t; const Real do_t_input = (o_t_deriv * do_t - (2 * o_t - 1) * o_t_self_repair); const Real dc_t = (c_t_deriv * dtanh_c_t + dc_t_out + do_t_input * w_oc) - tanh_c_t * c_t_self_repair; const Real dtanh_c_part = i_t * i_scale * dc_t; const Real df_t = dc_t * f_scale * c_prev; const Real df_t_input = (df_t * f_t_deriv - (2 * f_t - 1) * f_t_self_repair); const Real di_t = dc_t * i_scale * tanh_c_part; const Real di_t_input = (di_t * i_t_deriv - (2 * i_t - 1) * i_t_self_repair); if (params_deriv) { w_ic_deriv_sum += c_prev * di_t_input; w_fc_deriv_sum += c_prev * df_t_input; w_oc_deriv_sum += c_t * do_t_input; } const Real dc_prev = w_ic * di_t_input + w_fc * df_t_input + f_t * f_scale * dc_t; const Real do_part = do_t_input; const Real dc_part = (c_part_deriv * dtanh_c_part - tanh_c_part * c_part_self_repair); const Real df_part = df_t_input; const Real di_part = di_t_input; if (input_deriv) { input_deriv[i * input_deriv_stride + j] = di_part; input_deriv[i * input_deriv_stride + j + cell_dim] = df_part; input_deriv[i * input_deriv_stride + j + cell_dim * 2] = dc_part; input_deriv[i * input_deriv_stride + j + cell_dim * 3] = do_part; input_deriv[i * input_deriv_stride + j + cell_dim * 4] = dc_prev; } } } if (params_deriv) { // compute params_deriv smem[tid] = w_ic_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { params_deriv[j] = smem[tid]; } __syncthreads(); smem[tid] = w_fc_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { params_deriv[params_deriv_stride + j] = smem[tid]; } __syncthreads(); smem[tid] = w_oc_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { params_deriv[2 * params_deriv_stride + j] = smem[tid]; } // compute value_sum_out __syncthreads(); smem[tid] = i_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[j] += smem[tid]; } __syncthreads(); smem[tid] = f_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[value_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_part_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[2 * value_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = o_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[3 * value_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_t_value_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { value_sum_out[4 * value_sum_out_stride + j] += smem[tid]; } // need to update self_repair_sum_out before deriv_sum_out, because // deriv_sum_out and deriv_sum_in might point to the same memory. if (i0 < 5 && j < cell_dim) { self_repair_sum_out[i0 * self_repair_sum_out_stride + j] = update_sr[i0] ? num_rows : 0; } // compute derive_sum_out __syncthreads(); smem[tid] = i_t_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[j] += smem[tid]; } __syncthreads(); smem[tid] = f_t_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[deriv_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_part_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[2 * deriv_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = o_t_deriv_sum; # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[3 * deriv_sum_out_stride + j] += smem[tid]; } __syncthreads(); smem[tid] = c_t_deriv_sum; __syncthreads(); # pragma unroll for (int shift = CU1DBLOCK / 2; shift >= warpSize; shift >>= 1) { __syncthreads(); if (tid < shift) { smem[tid] += smem[tid + shift]; } } if (tid < warpSize && j < cell_dim) { deriv_sum_out[4 * deriv_sum_out_stride + j] += smem[tid]; } } } __global__ static void _cuda_compress_uint8_sign(const BaseFloat *src, MatrixDim dim, unsigned char *dest, int dest_stride) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dest_index = i + j * dest_stride, src_index = i + j * dim.stride; if (i < dim.cols && j < dim.rows) { BaseFloat f = src[src_index]; dest[dest_index] = (f > 0.0 ? (unsigned char)1 : (unsigned char)0); } } // The following inline templated functions are a workaround for the // fact that (I believe) std::numeric_limits is not available in CUDA; // they allow us to access the minimum and maximum elements of certain // types from templated code. template <typename I> __device__ static inline int minimum_integer_value(); template <typename I> __device__ static inline int maximum_integer_value(); template<> __device__ int maximum_integer_value<int8_t>() { return 127; } template<> __device__ int minimum_integer_value<int8_t>() { return -128; } template<> __device__ int maximum_integer_value<uint8_t>() { return 255; } template<> __device__ int minimum_integer_value<uint8_t>() { return 0; } template<> __device__ int maximum_integer_value<int16_t>() { return 32767; } template<> __device__ int minimum_integer_value<int16_t>() { return -32768; } template<> __device__ int maximum_integer_value<uint16_t>() { return 65535; } template<> __device__ int minimum_integer_value<uint16_t>() { return 0; } template <typename I> __global__ static void _cuda_compress_bounds_check(const BaseFloat *src, MatrixDim dim, I *dest, int dest_stride, float inv_scale) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dest_index = i + j * dest_stride, src_index = i + j * dim.stride; const int min_value = minimum_integer_value<I>(), max_value = maximum_integer_value<I>(); int compressed_value; int ok = (i < dim.cols && j < dim.rows); if (ok) { float f = src[src_index]; // note: I'm not sure what __float2int_rn does if input is outside of // integer range, but it doesn't matter much as in the situations where this // type of compression would make sense, the input should be well inside the // range of 'int', and if it fails, we've probably already catastrophically // diverged. int i = __float2int_rn(f * inv_scale); if (i < min_value) compressed_value = min_value; else if (i > max_value) compressed_value = max_value; else compressed_value = i; } __syncthreads(); if (ok) { dest[dest_index] = compressed_value; } } template <typename I> __global__ static void _cuda_compress_no_bounds_check(const BaseFloat *src, MatrixDim dim, I *dest, int dest_stride, float inv_scale) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int dest_index = i + j * dest_stride, src_index = i + j * dim.stride; if (i < dim.cols && j < dim.rows) { float f = src[src_index]; int i = __float2int_rn(f * inv_scale); I s = i; dest[dest_index] = s; } } template <typename I> __global__ static void _cuda_uncompress(BaseFloat *dest, MatrixDim dim, const I *src, int src_stride, float scale) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int src_index = i + j * src_stride, dest_index = i + j * dim.stride; if (i < dim.cols && j < dim.rows) { I s = src[src_index]; dest[dest_index] = float(s * scale); } } __global__ static void _noop_kernel() { } /*********************************************************************** * ANSI-C wrappers of CUDA kernels */ /* * "int32" */ void cuda_int32_set_const(dim3 Gr, dim3 Bl, int32_cuda* mat, int32_cuda value, MatrixDim d) { _set_const<<<Gr,Bl>>>(mat,value,d); } void cuda_int32_add(dim3 Gr, dim3 Bl, int32_cuda* mat, int32_cuda value, MatrixDim d) { _add<<<Gr,Bl>>>(mat,value,d); } void cuda_int32_sequence(dim3 Gr, dim3 Bl, int32_cuda* data, int length, int32_cuda base) { _sequence<<<Gr, Bl>>>(data, length, base); } /* * "float" */ /* * CuMatrix */ void cudaF_copy_upp_low(dim3 Gr, dim3 Bl, float* A, MatrixDim dimA) { _copy_upp_low<<<Gr,Bl>>>(A,dimA);} void cudaF_copy_low_upp(dim3 Gr, dim3 Bl, float* A, MatrixDim dimA) { _copy_low_upp<<<Gr,Bl>>>(A,dimA);} void cudaF_add_diag_vec_mat(dim3 Gr, dim3 Bl, float alpha, float *mat, MatrixDim mat_dim, const float *vec, const float *mat2, int mat2_row_stride, int mat2_col_stride, float beta) { _add_diag_vec_mat<<<Gr,Bl>>>(alpha, mat, mat_dim, vec, mat2, mat2_row_stride, mat2_col_stride, beta); } void cudaF_copy_from_tp_trans(dim3 Gr, dim3 Bl, float* A, const float* B, MatrixDim dmat) { _copy_from_tp_trans<<<Gr,Bl>>>(A,B,dmat); } void cudaFD_copy_from_tp_trans(dim3 Gr, dim3 Bl, float* A, const double* B, MatrixDim dmat) { _copy_from_tp_trans<<<Gr,Bl>>>(A,B,dmat); } void cudaF_copy_from_tp(dim3 Gr, dim3 Bl, float* A, const float* B, MatrixDim dmat) { _copy_from_tp<<<Gr,Bl>>>(A,B,dmat); } void cudaFD_copy_from_tp(dim3 Gr, dim3 Bl, float* A, const double* B, MatrixDim dmat) { _copy_from_tp<<<Gr,Bl>>>(A,B,dmat); } void cudaF_apply_exp(dim3 Gr, dim3 Bl, float* mat, MatrixDim d, cudaStream_t s) { _apply_exp<<<Gr,Bl,0,s>>>(mat,d); } void cudaF_apply_exp_limited(dim3 Gr, dim3 Bl, float* mat, MatrixDim d, float lower_limit, float upper_limit) { _apply_exp_limited<<<Gr,Bl>>>(mat, d, lower_limit, upper_limit); } void cudaF_apply_pow(dim3 Gr, dim3 Bl, float* mat, float power, MatrixDim d) { _apply_pow<<<Gr,Bl>>>(mat, power, d); } void cudaF_apply_pow_abs(dim3 Gr, dim3 Bl, float* mat, float power, bool include_sign, MatrixDim d) { _apply_pow_abs<<<Gr,Bl>>>(mat, power, include_sign, d); } void cudaF_apply_heaviside(dim3 Gr, dim3 Bl, float* mat, MatrixDim d) { _apply_heaviside<<<Gr,Bl>>>(mat, d); } void cudaF_copy_cols(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _copy_cols<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaF_add_cols(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _add_cols<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaF_copy_rows(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _copy_rows<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaF_copy_rows_direct(dim3 Gr, dim3 Bl, float* dst, const float* const * src, MatrixDim dst_dim) { _copy_rows<<<Gr,Bl>>>(dst, src, dst_dim); } void cudaF_copy_to_rows_direct(dim3 Gr, dim3 Bl, float* const * dst, const float* src, MatrixDim src_dim) { _copy_to_rows<<<Gr,Bl>>>(dst, src, src_dim); } void cudaF_add_rows(dim3 Gr, dim3 Bl, float alpha, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _add_rows<<<Gr,Bl>>>(alpha, dst, src, reorder, dst_dim, src_stride); } void cudaF_mul_rows(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _mul_rows<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaF_div_rows(dim3 Gr, dim3 Bl, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _div_rows<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaF_add_rows_direct(dim3 Gr, dim3 Bl, float alpha, float* dst, const float* const * src, MatrixDim dst_dim) { _add_rows<<<Gr,Bl>>>(alpha, dst, src, dst_dim); } void cudaF_add_to_rows(dim3 Gr, dim3 Bl, float alpha, float* dst, const float* src, const MatrixIndexT_cuda* reorder, MatrixDim src_dim, int dst_stride) { _add_to_rows<<<Gr,Bl>>>(alpha, dst, src, reorder, src_dim, dst_stride); } void cudaF_add_to_rows_direct(dim3 Gr, dim3 Bl, float alpha, float* const * dst, const float* src, MatrixDim src_dim) { _add_to_rows<<<Gr,Bl>>>(alpha, dst, src, src_dim); } void cudaF_apply_floor(dim3 Gr, dim3 Bl, float* mat, float floor_val, MatrixDim d) { _apply_floor<<<Gr,Bl>>>(mat, floor_val, d); } void cudaF_apply_ceiling(dim3 Gr, dim3 Bl, float* mat, float ceiling_val, MatrixDim d, cudaStream_t s) { _apply_ceiling<<<Gr,Bl,0,s>>>(mat, ceiling_val, d); } void cudaF_set_diag(int Gr, int Bl, float* mat, float value, MatrixDim d) { _set_diag<<<Gr,Bl>>>(mat,value,d); } void cudaF_set_diag_packed(int Gr, int Bl, float* mat, float value, int dim) { _set_diag_packed<<<Gr,Bl>>>(mat,value,dim); } void cudaF_add_diag_packed(int Gr, int Bl, float* mat, float value, int dim) { _add_diag_packed<<<Gr,Bl>>>(mat,value,dim); } void cudaF_set_const(dim3 Gr, dim3 Bl, float* mat, float value, MatrixDim d) { _set_const<<<Gr,Bl>>>(mat,value,d); } void cudaF_set_zero_above_diag(dim3 Gr, dim3 Bl, float* mat, MatrixDim d) { _set_zero_above_diag<<<Gr,Bl>>>(mat, d); } void cudaF_add(dim3 Gr, dim3 Bl, float* mat, float value, MatrixDim d, cudaStream_t s) { _add<<<Gr,Bl,0,s>>>(mat,value,d); } void cudaF_scale_diag_packed(int Gr, int Bl, float* mat, float value, int dim) { _scale_diag_packed<<<Gr,Bl>>>(mat,value,dim); } void cudaF_scale(dim3 Gr, dim3 Bl, float* mat, float value, MatrixDim d, cudaStream_t s) { _scale<<<Gr,Bl,0,s>>>(mat,value,d); } void cudaF_apply_log(dim3 Gr, dim3 Bl, float* mat, MatrixDim d) { _apply_log<<<Gr,Bl>>>(mat,d); } void cudaF_apply_fixed(dim3 Gr, dim3 Bl, float* mat, float resolution, int mode, MatrixDim d){ _apply_fixed<<<Gr,Bl>>>(mat, resolution, mode, d); } void cudaF_mul_elements(dim3 Gr, dim3 Bl, float* mat, const float* A, MatrixDim dst_d, int src_stride) { _mul_elements<<<Gr,Bl>>>(mat,A,dst_d,src_stride); } void cudaF_div_elements(dim3 Gr, dim3 Bl, float* mat, const float* A, MatrixDim dst_d, int src_stride) { _div_elements<<<Gr,Bl>>>(mat,A,dst_d,src_stride); } void cudaF_max(dim3 Gr, dim3 Bl, float* mat, const float* A, MatrixDim dst_d, int src_stride) { _max<<<Gr,Bl>>>(mat,A,dst_d,src_stride); } void cudaF_min(dim3 Gr, dim3 Bl, float* mat, const float* other, MatrixDim mat_d, int other_stride) { _min<<<Gr,Bl>>>(mat,other,mat_d,other_stride); } void cudaF_mul_cols_vec(dim3 Gr, dim3 Bl, float* mat, const float* scale, MatrixDim d) { _mul_cols_vec<<<Gr,Bl>>>(mat,scale,d); } void cudaF_mul_rows_vec(dim3 Gr, dim3 Bl, float* mat, const float* scale, MatrixDim d, cudaStream_t s) { _mul_rows_vec<<<Gr,Bl,0,s>>>(mat,scale,d); } void cudaF_mul_rows_group_mat(dim3 Gr, dim3 Bl, float *y, const float *x, MatrixDim d, int src_stride, int group_size) { _mul_rows_group_mat<<<Gr,Bl>>>(y, x, d, src_stride, group_size); } void cudaF_diff_group_pnorm(dim3 Gr, dim3 Bl, float *id, const float *iv, const float *ov, const float* od, MatrixDim id_dim, int iv_stride, int ov_stride, int od_stride, int group_size, float power) { _diff_group_pnorm<<<Gr, Bl>>>(id, iv, ov, od, id_dim, iv_stride, ov_stride, od_stride, group_size, power); } void cudaF_calc_group_max_deriv(dim3 Gr, dim3 Bl, float *y, const float *x1, const float *x2, MatrixDim y_dim, int x1_stride, int x2_stride, int group_size) { _calc_group_max_deriv<<<Gr,Bl>>>(y, x1, x2, y_dim, x1_stride, x2_stride, group_size); } void cudaF_div_rows_vec(dim3 Gr, dim3 Bl, float* mat, const float* vec_div, MatrixDim d) { _div_rows_vec<<<Gr,Bl>>>(mat, vec_div, d); } void cudaF_add_mat(dim3 Gr, dim3 Bl, float alpha, const float* src, float* dst, MatrixDim d, int src_stride, int A_trans, cudaStream_t s) { if (A_trans) { _add_mat_trans<<<Gr,Bl,0,s>>>(alpha,src,dst,d,src_stride); } else { _add_mat<<<Gr,Bl,0,s>>>(alpha,src,dst,d,src_stride); } } void cudaF_add_mat_blocks(dim3 Gr, dim3 Bl, float alpha, const float* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, float* dst, MatrixDim d, int src_stride, int A_trans) { if (A_trans) { _add_mat_blocks_trans<<<Gr,Bl>>>(alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } else { _add_mat_blocks<<<Gr,Bl>>>(alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } } void cudaF_add_mat_repeated(dim3 Gr, dim3 Bl, float alpha, const float* src, MatrixDim src_dim, float *dst, MatrixDim dst_dim) { _add_mat_repeated<<<Gr,Bl>>>(alpha, src, src_dim, dst, dst_dim); } void cudaF_set_mat_mat_div_mat(dim3 Gr, dim3 Bl, const float *A, const float *B, const float *C, float *dst, MatrixDim d, int stride_a, int stride_b, int stride_c) { _set_mat_mat_div_mat<<<Gr,Bl>>>(A,B,C,dst,d, stride_a, stride_b, stride_c); } void cudaF_sy_add_tr2(dim3 Gr, dim3 Bl, float alpha, float beta, const float* T, MatrixDim tdim, float *S, MatrixDim sdim) { _sy_add_tr2<<<Gr,Bl>>>(alpha, beta, T, tdim, S, sdim); } void cudaF_add_vec_to_cols(dim3 Gr, dim3 Bl, float alpha, const float* col, float beta, float* dst, MatrixDim d) { _add_vec_to_cols<<<Gr,Bl>>>(alpha,col,beta,dst,d); } void cudaF_add_vec_to_rows(dim3 Gr, dim3 Bl, float alpha, const float* row, float beta, float* dst, MatrixDim d) { _add_vec_to_rows<<<Gr,Bl>>>(alpha,row,beta,dst,d); } void cudaF_add_mat_diag_vec(dim3 Gr, dim3 Bl, float alpha, float *mat, MatrixDim mat_dim, const float *mat2, int mat2_row_stride, int mat2_col_stride, const float *vec, float beta) { _add_mat_diag_vec<<<Gr,Bl>>>(alpha, mat, mat_dim, mat2, mat2_row_stride, mat2_col_stride, vec, beta); } void cudaF_add_mat_mat_elements(dim3 Gr, dim3 Bl, float *data, const float *srcA_data, const float *srcB_data, MatrixDim dim, int srcA_stride, int srcB_stride, float alpha, float beta) { _add_mat_mat_elements<<<Gr, Bl>>>(data, srcA_data, srcB_data, dim, srcA_stride, srcB_stride, alpha, beta); } // CURRENTLY UNUSED... void cudaF_apply_mask(dim3 Gr, dim3 Bl, float* mat, const char* mask, MatrixDim dmat, MatrixDim dmask) { _apply_mask<<<Gr,Bl>>>(mat,mask,dmat,dmask); } /* * CuVector */ void cudaF_max_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<MAX,float>()); } void cudaF_abs_max_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<ABSMAX,float>()); } void cudaF_min_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<MIN,float>()); } void cudaF_sum_mat_cols(int Gr, int Bl, float* result, const float* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<SUM,float>()); } void cudaF_add_row_sum_mat(float* result, const float* mat, void* scratch, const MatrixDim d, const float alpha, const float beta) { //_strided_reduction_fused(result, mat, scratch, d, alpha, beta); } void cudaF_add_col_sum_mat(int Gr, int Bl, float* result, const float* mat, const MatrixDim d, const float alpha, const float beta, cudaStream_t s) { _transform_reduce_mat_cols<<<Gr, Bl,0,s>>>(result, mat, d, TransReduceOp<SUMAB, float>(alpha, beta)); } void cudaF_replace_value(int Gr, int Bl, float *v, int dim, float orig, float changed) { _replace_value<<<Gr,Bl>>>(v, dim, orig, changed); } void cudaF_set_bias_params(int Gr, int Bl, float* v, const float* a, float param_1, float param_2, float param_3, int* flag, int dim) { _set_bias_params<<<Gr,Bl>>>(v,a,param_1,param_2,param_3,flag,dim); } void cublas_copy_kaldi_fd(int Gr, int Bl, int n, const float* x, int incx, double* y, int incy) { _cublas_copy_kaldi<<<Gr,Bl>>>(n, x, incx, y, incy); } void cublas_copy_kaldi_df(int Gr, int Bl, int n, const double* x, int incx, float* y, int incy) { _cublas_copy_kaldi<<<Gr,Bl>>>(n, x, incx, y, incy); } void cudaF_vec_mul_elements(int Gr, int Bl, float* v, const float* a, int dim, cudaStream_t s) { _vec_mul_elements<<<Gr,Bl,0,s>>>(v, a, dim); } void cudaF_vec_min(int Gr, int Bl, const float* v, float* value, int dim, int inc) { _vec_transform_reduce<<<Gr,Bl>>>(v, value, dim, inc, TransReduceOp<MIN, float>()); } void cudaF_vec_max(int Gr, int Bl, const float* v, float* value, int dim, int inc) { _vec_transform_reduce<<<Gr,Bl>>>(v, value, dim, inc, TransReduceOp<MAX, float>()); } void cudaF_trace_mat_mat_trans(dim3 Gr, dim3 Bl, const float* A, const float* B, MatrixDim dA, int B_stride, float* value) { _trace_mat_mat_trans<<<Gr,Bl>>>(A,B,dA,B_stride,value); } void cudaF_trace_mat_mat(dim3 Gr, dim3 Bl, const float* A, const float* B, MatrixDim dA, int B_stride, float* value) { _trace_mat_mat<32> <<<Gr,Bl>>>(A,B,dA,B_stride,value); } void cudaF_add_diag_mat_mat_MNT(int Gr, int Bl, const float alpha, const float* M, const MatrixDim dim_M, const float* N, const int stride_N, const float beta, float* v) { _add_diag_mat_mat_MNT<<<Gr,Bl>>>(alpha,M,dim_M,N,stride_N,beta,v); } void cudaF_add_diag_mat_mat_MTN(dim3 Gr, dim3 Bl, const float alpha, const float* M, const int stride_M, const float* N, const MatrixDim dim_N, const float beta, float* v, const int stride_v) { if (Bl.x == 16) { _add_diag_mat_mat_MTN<16> <<<Gr, Bl>>>(alpha, M, stride_M, N, dim_N, beta, v, stride_v); } else if (Bl.x == 32) { _add_diag_mat_mat_MTN<32> <<<Gr, Bl>>>(alpha, M, stride_M, N, dim_N, beta, v, stride_v); } } void cudaF_add_diag_mat_mat_MN(dim3 Gr, dim3 Bl, const float alpha, const float* M, const int stride_M, const float* N, const MatrixDim dim_N, const float beta, float* v) { if (Bl.x == 16) { _add_diag_mat_mat_MN<16> <<<Gr,Bl>>>(alpha,M,stride_M,N,dim_N,beta,v); } else if (Bl.x==32) { _add_diag_mat_mat_MN<32><<<Gr,Bl>>>(alpha,M,stride_M,N,dim_N,beta,v); } } void cudaF_add_vec_vec(int Gr, int Bl, float alpha, float* v, const float* x, const float* y, float beta, int dim) { _add_vec_vec<<<Gr,Bl>>>(alpha,v,x,y,beta,dim); } void cudaF_vec_sum(int Gr, int Bl, float* v, float* value, int dim, int inc) { _vec_transform_reduce<<<Gr,Bl>>>(v, value, dim, inc, TransReduceOp<SUM, float>()); } void cudaF_matrix_add_elements(dim3 Gr, dim3 Bl, float *data, MatrixDim dim, float alpha, MatrixElement<float>* x, int num_elements) { _cuda_matrix_add_elements<<<Gr, Bl>>>(data, dim, alpha, x, num_elements); } void cudaF_matrix_add_indexed_values(dim3 Gr, dim3 Bl, MatrixDim dim, float alpha, const Int32Pair* indices, const float* x, int s, float* data) { _cuda_matrix_add_indexed_values<<<Gr, Bl>>>(dim, alpha, indices, x, s, data); } void cudaF_matrix_add_to_elements(dim3 Gr, dim3 Bl, float alpha, float* mat, MatrixDim dim, const MatrixIndexT_cuda* elements) { _cuda_matrix_add_to_elements<<<Gr, Bl>>>(alpha, mat, dim, elements); } void cudaF_vector_copy_elements(dim3 Gr, dim3 Bl, float *data, int dim, const float *src_mat, int mat_stride, bool transpose, const MatrixIndexT_cuda* elements) { _cuda_vector_copy_elements<<<Gr, Bl>>>(data, dim, src_mat, mat_stride, transpose, elements); } void cudaF_comp_obj_deriv(dim3 Gr, dim3 Bl, MatrixElement<float>* x, int s, const float* z, MatrixDim d, float* z2, MatrixDim d2, float* t) { _cuda_comp_obj_deriv<<<Gr,Bl>>>(x,s,z,d,z2,d2,t); } void cudaD_comp_obj_deriv(dim3 Gr, dim3 Bl, MatrixElement<double>* x, int s, const double* z, MatrixDim d, double* z2, MatrixDim d2, double* t) { _cuda_comp_obj_deriv<<<Gr,Bl>>>(x,s,z,d,z2,d2,t); } void cudaF_vec_copy_diag_from_packed(int Gr, int Bl, float *dst, const float *src, int dim) { _vec_copy_diag_from_packed<<<Gr,Bl>>>(dst,src,dim); } void cudaF_vec_apply_floor(int Gr, int Bl, float* v, float floor_val, float *count, int dim) { _vec_apply_floor<<<Gr,Bl>>>(v,floor_val,count,dim); } void cudaF_vec_apply_ceiling(int Gr, int Bl, float* v, float ceiling_val, float *count, int dim) { _vec_apply_ceiling<<<Gr,Bl>>>(v, ceiling_val,count,dim); } void cudaF_vec_apply_exp(int Gr, int Bl, float* v, int dim) { _vec_apply_exp<<<Gr,Bl>>>(v,dim); } void cudaF_vec_apply_log(int Gr, int Bl, float* v, float* flag, int dim) { _vec_apply_log<<<Gr,Bl>>>(v,flag,dim); } void cudaF_vec_apply_fixed(int Gr, int Bl, float* v, float resolution, int mode, int dim){ _vec_apply_fixed<<<Gr,Bl>>>(v, resolution, mode, dim); } void cudaF_invert_elements(dim3 Gr, dim3 Bl, float* data, MatrixDim d) { _invert_elements<<<Gr,Bl>>>(data, d); } void cudaF_add_mat_blockmat(dim3 Gr, dim3 Bl, float *data, MatrixDim d, const float *Adata, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, float alpha, float beta, int B_trans) { if (B_trans) { _add_mat_blockmat_trans<<<Gr,Bl>>>(data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } else { _add_mat_blockmat<<<Gr,Bl>>>(data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } } void cudaF_block_add_mat_mat(dim3 Gr, dim3 Bl, CuBlockMatrixData *B_cu_data, int num_blocks, const float *C_data, int C_num_cols, int C_row_stride, int C_col_stride, const float *D_data, int D_row_stride, int D_col_stride, float alpha, float beta) { _block_add_mat_mat<<<Gr,Bl>>>(B_cu_data, num_blocks, C_data, C_num_cols, C_row_stride, C_col_stride, D_data, D_row_stride, D_col_stride, alpha, beta); } /* * cu:: */ void cudaF_soft_hinge(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { _soft_hinge<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaF_group_pnorm(dim3 Gr, dim3 Bl, float *y, const float *x, MatrixDim d, int src_stride, int group_size, float power) { _group_pnorm<<<Gr,Bl>>>(y, x, d, src_stride, group_size, power); } void cudaF_group_spec_pnorm(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride, int group_size, float power) { if (power == float(0)) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<L0NORM, float>()); } else if (power == float(1)) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<L1NORM, float>()); } else if (power == float(2)) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<L2NORM, float>()); } else if (power == std::numeric_limits<float>::infinity()) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<LINFNORM, float>()); } else { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<LPNORM, float>(power)); } } void cudaF_group_max(dim3 Gr, dim3 Bl, float *y, const float *x, MatrixDim d, int src_stride, int group_size) { _group_transform_reduce<<<Gr,Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<MAX, float>()); } void cudaF_sigmoid(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { _sigmoid<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaF_diff_sigmoid(dim3 Gr, dim3 Bl, float* eout, const float* e, const float* y, MatrixDim d, int e_stride, int y_stride) { _diff_sigmoid<<<Gr,Bl>>>(eout, e, y, d, e_stride, y_stride); } void cudaF_tanh(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { _tanh<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaF_diff_tanh(dim3 Gr, dim3 Bl, float* eout, const float* e, const float* y, MatrixDim d, int e_stride, int y_stride) { _diff_tanh<<<Gr,Bl>>>(eout, e, y, d, e_stride, y_stride); } void cudaF_ensure_nonzero(dim3 Gr, dim3 Bl, const float *x, MatrixDim d, float epsilon, int y_stride, float *y) { _ensure_nonzero<<<Gr,Bl>>>(x, d, epsilon, y_stride, y); } void cudaF_parametric_relu(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride, const float* a, const float* b) { _parametric_relu<<<Gr,Bl>>>(y, x, d, src_stride, a, b); } void cudaF_diff_parametric_relu(dim3 Gr, dim3 Bl, float* eout, const float* e, const float* y, MatrixDim d, int e_stride, int y_stride, const float* a, const float* b) { _diff_parametric_relu<<<Gr,Bl>>>(eout, e, y, d, e_stride, y_stride, a, b); } void cudaF_heaviside(dim3 Gr, dim3 Bl, float* y, const float* x, MatrixDim d, int src_stride) { _heaviside<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaF_softmax_reduce(size_t Gr, size_t Bl, float* y, const float* x, MatrixDim d, int src_stride, float *logsum, cudaStream_t s) { _softmax_reduce<<<Gr,Bl,0,s>>>(y, x, d, src_stride, logsum); } void cudaF_log_softmax_reduce(size_t Gr, size_t Bl, float* y, const float* x, MatrixDim y_dim, int x_stride) { _log_softmax_reduce<<<Gr,Bl>>>(y, x, y_dim, x_stride); } void cudaF_splice(dim3 Gr, dim3 Bl, float* y, const float* x, const int32_cuda* off, MatrixDim d_out, MatrixDim d_in) { _splice<<<Gr,Bl>>>(y,x,off,d_out,d_in); } void cudaF_normalize_per_row(size_t Gr, size_t Bl, float *y, int y_stride, const float *x, MatrixDim x_d, float target_rms, bool add_log_stddev) { _normalize_per_row<<<Gr, Bl>>>(y, y_stride, x, x_d, target_rms, add_log_stddev); } void cudaF_one(int Gr, int Bl, float* x, int dim) { _one<<<Gr,Bl>>>(x,dim); } void cudaF_take_mean(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim d_in) { _take_mean<<<Gr,Bl>>>(x,y,d_in); } void cudaF_take_lower(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim d_in) { _take_lower<<<Gr,Bl>>>(x,y,d_in); } void cudaF_take_upper(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim d_in) { _take_upper<<<Gr,Bl>>>(x,y,d_in); } void cudaF_copy_from_sp(dim3 Gr, dim3 Bl, const float* x, float* y, MatrixDim dim) { _copy_from_sp<<<Gr,Bl>>>(x, y, dim); } void cudaF_copy(dim3 Gr, dim3 Bl, float* y, const float* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { _copy<<<Gr,Bl>>>(y,x,copy_from,d_out,d_in); } void cudaF_randomize(dim3 Gr, dim3 Bl, float* y, const float* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { _randomize<<<Gr,Bl>>>(y,x,copy_from,d_out,d_in); } void cudaF_regularize_l1(dim3 Gr, dim3 Bl, float* wei, float* grad, float l1, float lr, MatrixDim d, int stride_grad) { _regularize_l1<<<Gr,Bl>>>(wei,grad,l1,lr,d,stride_grad); } void cudaF_find_row_max_id(dim3 Gr, dim3 Bl, const float* mat, float* vec_val, int32_cuda* vec_id, MatrixDim d) { _find_row_max_id<<<Gr,Bl>>>(mat, vec_val, vec_id, d); } void cudaF_find_row_abs_max(dim3 Gr, dim3 Bl, const float* mat, float *row_max, MatrixDim d) { _find_row_abs_max<<<Gr,Bl>>>(mat, row_max, d); } void cudaF_diff_xent(dim3 Gr, dim3 Bl, const int32_cuda* vec_tgt, float* mat_net_out, float* vec_log_post, MatrixDim d) { _diff_xent<<<Gr,Bl>>>(vec_tgt,mat_net_out,vec_log_post,d); } void cudaF_diff_softmax(dim3 Gr, dim3 Bl, float* x, const MatrixDim dim, const float* value, const int value_stride, const float* diff, const int diff_stride) { _diff_softmax<<<Gr, Bl>>>(x, dim, value, value_stride, diff, diff_stride); } void cudaF_copy_rows_from_vec(dim3 Gr, dim3 Bl, float *mat_out, MatrixDim d_out, const float *v_in) { _copy_rows_from_vec<<<Gr,Bl>>>(mat_out, d_out, v_in); } void cudaF_diff_log_softmax(dim3 Gr, dim3 Bl, const MatrixDim in_deriv_dim, const float* out_value, const int out_value_stride, const float* out_deriv, const int out_deriv_stride, float* in_deriv) { _diff_log_softmax<<<Gr, Bl>>>(in_deriv_dim, out_value, out_value_stride, out_deriv, out_deriv_stride, in_deriv); } void cudaF_copy_col_from_mat_df(int Gr, int Bl, double* v, int col, const float* mat, MatrixDim dmat, int dim) { _copy_col_from_mat_df<<<Gr,Bl>>>(v,col,mat,dmat,dim); } void cudaF_copy_col_from_mat_fd(int Gr, int Bl, float* v, int col, const float* mat, MatrixDim dmat, int dim) { _copy_col_from_mat_fd<<<Gr,Bl>>>(v,col,mat,dmat,dim); } void cudaF_sum_column_ranges(dim3 Gr, dim3 Bl, float *data, MatrixDim dim, const float *src_data, MatrixDim src_dim, const Int32Pair *indices) { _sum_column_ranges<<<Gr,Bl>>>(data, dim, src_data, src_dim, indices); } void cudaF_add_row_ranges(dim3 Gr, dim3 Bl, float *data, MatrixDim dim, const float *src_data, MatrixDim src_dim, const Int32Pair *indexes) { _add_row_ranges<<<Gr,Bl>>>(data, dim, src_data, src_dim, indexes); } void cudaF_matrix_lookup(dim3 Gr, dim3 Bl, const float *data, MatrixDim dim, const Int32Pair *indices, int indices_size, float *output) { _matrix_lookup<<<Gr,Bl>>>(data, dim, indices, indices_size, output); } void cudaF_equal_element_mask(dim3 Gr, dim3 Bl, const float *mat1, const float *mat2, float *mask, MatrixDim mat1_dim, int mat2_stride, int mask_stride) { _equal_element_mask<<<Gr,Bl>>>(mat1, mat2, mask, mat1_dim, mat2_stride, mask_stride); } /* * "double" */ /* * CuMatrix */ void cudaD_copy_upp_low(dim3 Gr, dim3 Bl, double* A, MatrixDim dimA) { _copy_upp_low<<<Gr,Bl>>>(A,dimA);} void cudaD_copy_low_upp(dim3 Gr, dim3 Bl, double* A, MatrixDim dimA) { _copy_low_upp<<<Gr,Bl>>>(A,dimA);} void cudaD_add_diag_vec_mat(dim3 Gr, dim3 Bl, double alpha, double *mat, MatrixDim mat_dim, const double *vec, const double *mat2, int mat2_row_stride, int mat2_col_stride, double beta) { _add_diag_vec_mat<<<Gr,Bl>>>(alpha, mat, mat_dim, vec, mat2, mat2_row_stride, mat2_col_stride, beta); } void cudaD_copy_from_tp_trans(dim3 Gr, dim3 Bl, double* A, const double* B, MatrixDim dmat) { _copy_from_tp_trans<<<Gr,Bl>>>(A,B,dmat); } void cudaDF_copy_from_tp_trans(dim3 Gr, dim3 Bl, double* A, const float* B, MatrixDim dmat) { _copy_from_tp_trans<<<Gr,Bl>>>(A,B,dmat); } void cudaD_copy_from_tp(dim3 Gr, dim3 Bl, double* A, const double* B, MatrixDim dmat) { _copy_from_tp<<<Gr,Bl>>>(A,B,dmat); } void cudaDF_copy_from_tp(dim3 Gr, dim3 Bl, double* A, const float* B, MatrixDim dmat) { _copy_from_tp<<<Gr,Bl>>>(A,B,dmat); } void cudaD_apply_exp(dim3 Gr, dim3 Bl, double* mat, MatrixDim d, cudaStream_t s) { _apply_exp<<<Gr,Bl,0,s>>>(mat,d); } void cudaD_apply_exp_limited(dim3 Gr, dim3 Bl, double* mat, MatrixDim d, double lower_limit, double upper_limit) { _apply_exp_limited<<<Gr,Bl>>>(mat, d, lower_limit, upper_limit); } void cudaD_apply_pow(dim3 Gr, dim3 Bl, double* mat, double power, MatrixDim d) { _apply_pow<<<Gr,Bl>>>(mat, power, d); } void cudaD_apply_pow_abs(dim3 Gr, dim3 Bl, double* mat, double power, bool include_sign, MatrixDim d) { _apply_pow_abs<<<Gr,Bl>>>(mat, power, include_sign, d); } void cudaD_apply_heaviside(dim3 Gr, dim3 Bl, double* mat, MatrixDim d) { _apply_heaviside<<<Gr,Bl>>>(mat, d); } void cudaD_apply_fixed(dim3 Gr, dim3 Bl, double* mat, double resolution, int mode, MatrixDim d){ _apply_fixed<<<Gr,Bl>>>(mat, resolution, mode, d); } void cudaD_copy_cols(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _copy_cols<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaD_add_cols(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _add_cols<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaD_copy_rows(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _copy_rows<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaD_copy_rows_direct(dim3 Gr, dim3 Bl, double* dst, const double* const * src, MatrixDim dst_dim) { _copy_rows<<<Gr,Bl>>>(dst, src, dst_dim); } void cudaD_copy_to_rows_direct(dim3 Gr, dim3 Bl, double* const * dst, const double* src, MatrixDim src_dim) { _copy_to_rows<<<Gr,Bl>>>(dst, src, src_dim); } void cudaD_add_rows(dim3 Gr, dim3 Bl, double alpha, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _add_rows<<<Gr,Bl>>>(alpha, dst, src, reorder, dst_dim, src_stride); } void cudaD_mul_rows(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _mul_rows<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaD_div_rows(dim3 Gr, dim3 Bl, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, int src_stride) { _div_rows<<<Gr,Bl>>>(dst, src, reorder, dst_dim, src_stride); } void cudaD_add_rows_direct(dim3 Gr, dim3 Bl, double alpha, double* dst, const double* const * src, MatrixDim dst_dim) { _add_rows<<<Gr,Bl>>>(alpha, dst, src, dst_dim); } void cudaD_add_to_rows(dim3 Gr, dim3 Bl, double alpha, double* dst, const double* src, const MatrixIndexT_cuda* reorder, MatrixDim src_dim, int dst_stride) { _add_to_rows<<<Gr,Bl>>>(alpha, dst, src, reorder, src_dim, dst_stride); } void cudaD_add_to_rows_direct(dim3 Gr, dim3 Bl, double alpha, double* const * dst, const double* src, MatrixDim src_dim) { _add_to_rows<<<Gr,Bl>>>(alpha, dst, src, src_dim); } void cudaD_apply_floor(dim3 Gr, dim3 Bl, double* mat, double floor_val, MatrixDim d) { _apply_floor<<<Gr,Bl>>>(mat, floor_val, d); } void cudaD_apply_ceiling(dim3 Gr, dim3 Bl, double* mat, double ceiling_val, MatrixDim d, cudaStream_t s) { _apply_ceiling<<<Gr,Bl,0,s>>>(mat, ceiling_val, d); } void cudaD_set_diag(int Gr, int Bl, double* mat, double value, MatrixDim d) { _set_diag<<<Gr,Bl>>>(mat,value,d); } void cudaD_set_diag_packed(int Gr, int Bl, double* mat, double value, int dim) { _set_diag_packed<<<Gr,Bl>>>(mat,value,dim); } void cudaD_add_diag_packed(int Gr, int Bl, double* mat, double value, int dim) { _add_diag_packed<<<Gr,Bl>>>(mat,value,dim); } void cudaD_set_const(dim3 Gr, dim3 Bl, double* mat, double value, MatrixDim d) { _set_const<<<Gr,Bl>>>(mat,value,d); } void cudaD_set_zero_above_diag(dim3 Gr, dim3 Bl, double* mat, MatrixDim d) { _set_zero_above_diag<<<Gr,Bl>>>(mat, d); } void cudaD_add(dim3 Gr, dim3 Bl, double* mat, double value, MatrixDim d, cudaStream_t s) { _add<<<Gr,Bl,0,s>>>(mat,value,d); } void cudaD_scale_diag_packed(int Gr, int Bl, double* mat, double value, int dim) { _scale_diag_packed<<<Gr,Bl>>>(mat,value,dim); } void cudaD_scale(dim3 Gr, dim3 Bl, double* mat, double value, MatrixDim d, cudaStream_t s) { _scale<<<Gr,Bl,0,s>>>(mat,value,d); } void cudaD_apply_log(dim3 Gr, dim3 Bl, double* mat, MatrixDim d) { _apply_log<<<Gr,Bl>>>(mat,d); } void cudaD_mul_elements(dim3 Gr, dim3 Bl, double* mat, const double* A, MatrixDim dst_d, int src_stride) { _mul_elements<<<Gr,Bl>>>(mat,A,dst_d,src_stride); } void cudaD_div_elements(dim3 Gr, dim3 Bl, double* mat, const double* A, MatrixDim dst_d, int src_stride) { _div_elements<<<Gr,Bl>>>(mat,A,dst_d,src_stride); } void cudaD_max(dim3 Gr, dim3 Bl, double* mat, const double* A, MatrixDim dst_d, int src_stride) { _max<<<Gr,Bl>>>(mat,A,dst_d,src_stride); } void cudaD_min(dim3 Gr, dim3 Bl, double* mat, const double* other, MatrixDim mat_d, int other_stride) { _min<<<Gr,Bl>>>(mat,other,mat_d,other_stride); } void cudaD_mul_cols_vec(dim3 Gr, dim3 Bl, double* mat, const double* scale, MatrixDim d) { _mul_cols_vec<<<Gr,Bl>>>(mat,scale,d); } void cudaD_mul_rows_vec(dim3 Gr, dim3 Bl, double* mat, const double* scale, MatrixDim d, cudaStream_t s) { _mul_rows_vec<<<Gr,Bl,0,s>>>(mat,scale,d); } void cudaD_mul_rows_group_mat(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size) { _mul_rows_group_mat<<<Gr,Bl>>>(y, x, d, src_stride, group_size); } void cudaD_diff_group_pnorm(dim3 Gr, dim3 Bl, double *id, const double *iv, const double *ov, const double* od, MatrixDim id_dim, int iv_stride, int ov_stride, int od_stride, int group_size, double power) { _diff_group_pnorm<<<Gr, Bl>>>(id, iv, ov, od, id_dim, iv_stride, ov_stride, od_stride, group_size, power); } void cudaD_calc_group_max_deriv(dim3 Gr, dim3 Bl, double*y, const double* x1, const double* x2, MatrixDim y_dim, int x1_stride, int x2_stride, int group_size) { _calc_group_max_deriv<<<Gr,Bl>>>(y, x1, x2, y_dim, x1_stride, x2_stride, group_size); } void cudaD_div_rows_vec(dim3 Gr, dim3 Bl, double* mat, const double* vec_div, MatrixDim d) { _div_rows_vec<<<Gr,Bl>>>(mat, vec_div, d); } void cudaD_add_mat(dim3 Gr, dim3 Bl, double alpha, const double* src, double* dst, MatrixDim d, int src_stride, int A_trans, cudaStream_t s) { if (A_trans) { _add_mat_trans<<<Gr,Bl,0,s>>>(alpha,src,dst,d,src_stride); } else { _add_mat<<<Gr,Bl,0,s>>>(alpha,src,dst,d,src_stride); } } void cudaD_add_mat_blocks(dim3 Gr, dim3 Bl, double alpha, const double* src, int32_cuda num_row_blocks, int32_cuda num_col_blocks, double* dst, MatrixDim d, int src_stride, int A_trans) { if (A_trans) { _add_mat_blocks_trans<<<Gr,Bl>>>(alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } else { _add_mat_blocks<<<Gr,Bl>>>(alpha, src, num_row_blocks, num_col_blocks, dst, d, src_stride); } } void cudaD_add_mat_repeated(dim3 Gr, dim3 Bl, double alpha, const double* src, MatrixDim src_dim, double *dst, MatrixDim dst_dim) { _add_mat_repeated<<<Gr,Bl>>>(alpha, src, src_dim, dst, dst_dim); } void cudaD_set_mat_mat_div_mat(dim3 Gr, dim3 Bl, const double *A, const double *B, const double *C, double *dst, MatrixDim d, int stride_a, int stride_b, int stride_c) { _set_mat_mat_div_mat<<<Gr,Bl>>>(A,B,C,dst,d,stride_a,stride_b,stride_c); } void cudaD_sy_add_tr2(dim3 Gr, dim3 Bl, double alpha, double beta, const double* T, MatrixDim tdim, double *S, MatrixDim sdim) { _sy_add_tr2<<<Gr,Bl>>>(alpha, beta, T, tdim, S, sdim); } void cudaD_add_vec_to_cols(dim3 Gr, dim3 Bl, double alpha, const double* col, double beta, double* dst, MatrixDim d) { _add_vec_to_cols<<<Gr,Bl>>>(alpha,col,beta,dst,d); } void cudaD_add_vec_to_rows(dim3 Gr, dim3 Bl, double alpha, const double* row, double beta, double* dst, MatrixDim d) { _add_vec_to_rows<<<Gr,Bl>>>(alpha,row,beta,dst,d); } void cudaD_add_mat_diag_vec(dim3 Gr, dim3 Bl, double alpha, double *mat, MatrixDim mat_dim, const double *mat2, int mat2_row_stride, int mat2_col_stride, const double *vec, double beta) { _add_mat_diag_vec<<<Gr,Bl>>>(alpha, mat, mat_dim, mat2, mat2_row_stride, mat2_col_stride, vec, beta); } void cudaD_add_mat_mat_elements(dim3 Gr, dim3 Bl, double *data, const double *srcA_data, const double *srcB_data, MatrixDim dim, int srcA_stride, int srcB_stride, double alpha, double beta) { _add_mat_mat_elements<<<Gr, Bl>>>(data, srcA_data, srcB_data, dim, srcA_stride, srcB_stride, alpha, beta); } // CURRENTLY UNUSED... void cudaD_apply_mask(dim3 Gr, dim3 Bl, double* mat, const char* mask, MatrixDim dmat, MatrixDim dmask) { _apply_mask<<<Gr,Bl>>>(mat,mask,dmat,dmask); } /* * CuVector */ void cudaD_max_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<MAX,double>()); } void cudaD_abs_max_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<ABSMAX,double>()); } void cudaD_min_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<MIN,double>()); } void cudaD_sum_mat_cols(int Gr, int Bl, double* result, const double* mat, const MatrixDim d) { _transform_reduce_mat_cols<<<Gr,Bl>>>(result,mat,d, TransReduceOp<SUM,double>()); } void cudaD_add_row_sum_mat(double* result, const double* mat, void* scratch, const MatrixDim d, const double alpha, const double beta, cudaStream_t s) { //_strided_reduction_fused(result, mat, scratch, d, alpha, beta); } void cudaD_add_col_sum_mat(int Gr, int Bl, double* result, const double* mat, const MatrixDim d, const double alpha, const double beta, cudaStream_t s) { _transform_reduce_mat_cols<<<Gr, Bl,0,s>>>(result, mat, d, TransReduceOp<SUMAB, double>(alpha, beta)); } void cudaD_replace_value(int Gr, int Bl, double *v, int dim, double orig, double changed) { _replace_value<<<Gr,Bl>>>(v, dim, orig, changed); } void cudaD_set_bias_params(int Gr, int Bl, double* v, const double* a, double param_1, double param_2, double param_3, int* flag, int dim) { _set_bias_params<<<Gr,Bl>>>(v,a,param_1,param_2,param_3,flag,dim); } void cudaD_vec_mul_elements(int Gr, int Bl, double* v, const double* a, int dim, cudaStream_t s) { _vec_mul_elements<<<Gr,Bl,0,s>>>(v, a, dim); } void cudaD_vec_min(int Gr, int Bl, const double* v, double* value, int dim, int inc) { _vec_transform_reduce<<<Gr,Bl>>>(v, value, dim, inc, TransReduceOp<MIN, double>()); } void cudaD_vec_max(int Gr, int Bl, const double* v, double* value, int dim, int inc) { _vec_transform_reduce<<<Gr,Bl>>>(v, value, dim, inc, TransReduceOp<MAX, double>()); } void cudaD_trace_mat_mat_trans(dim3 Gr, dim3 Bl, const double* A, const double* B, MatrixDim dA, int B_stride, double* value) { _trace_mat_mat_trans<<<Gr,Bl>>>(A,B,dA,B_stride,value); } void cudaD_trace_mat_mat(dim3 Gr, dim3 Bl, const double* A, const double* B, MatrixDim dA, int B_stride, double* value) { _trace_mat_mat<32> <<<Gr,Bl>>>(A,B,dA,B_stride,value); } void cudaD_add_diag_mat_mat_MNT(int Gr, int Bl, const double alpha, const double* M, const MatrixDim dim_M, const double* N, const int stride_N, const double beta, double* v) { _add_diag_mat_mat_MNT<<<Gr,Bl>>>(alpha,M,dim_M,N,stride_N,beta,v); } void cudaD_add_diag_mat_mat_MTN(dim3 Gr, dim3 Bl, const double alpha, const double* M, const int stride_M, const double* N, const MatrixDim dim_N, const double beta, double* v, const int stride_v) { if (Bl.x == 16) { _add_diag_mat_mat_MTN<16> <<<Gr, Bl>>>(alpha, M, stride_M, N, dim_N, beta, v, stride_v); } else if (Bl.x == 32) { _add_diag_mat_mat_MTN<32> <<<Gr, Bl>>>(alpha, M, stride_M, N, dim_N, beta, v, stride_v); } } void cudaD_add_diag_mat_mat_MN(dim3 Gr, dim3 Bl, const double alpha, const double* M, const int stride_M, const double* N, const MatrixDim dim_N, const double beta, double* v) { if (Bl.x == 16) { _add_diag_mat_mat_MN<16> <<<Gr,Bl>>>(alpha,M,stride_M,N,dim_N,beta,v); } else if (Bl.x==32) { _add_diag_mat_mat_MN<32><<<Gr,Bl>>>(alpha,M,stride_M,N,dim_N,beta,v); } } void cudaD_add_vec_vec(int Gr, int Bl, double alpha, double* v, const double* x, const double* y, double beta, int dim) { _add_vec_vec<<<Gr,Bl>>>(alpha,v,x,y,beta,dim); } void cudaD_copy_col_from_mat_df(int Gr, int Bl, double* v, int col, const double* mat, MatrixDim dmat, int dim) { _copy_col_from_mat_df<<<Gr,Bl>>>(v,col,mat,dmat,dim); } void cudaD_copy_col_from_mat_fd(int Gr, int Bl, float* v, int col, const double* mat, MatrixDim dmat, int dim) { _copy_col_from_mat_fd<<<Gr,Bl>>>(v,col,mat,dmat,dim); } void cudaD_vec_sum(int Gr, int Bl, double* v, double* value, int dim, int inc) { _vec_transform_reduce<<<Gr,Bl>>>(v,value,dim,inc, TransReduceOp<SUM, double>()); } void cudaD_matrix_add_elements(dim3 Gr, dim3 Bl, double *data, MatrixDim dim, double alpha, MatrixElement<double>* x, int num_elements) { _cuda_matrix_add_elements<<<Gr, Bl>>>(data, dim, alpha, x, num_elements); } void cudaD_vector_copy_elements(dim3 Gr, dim3 Bl, double *data, int dim, const double *src_mat, int mat_stride, bool transpose, const MatrixIndexT_cuda* elements) { _cuda_vector_copy_elements<<<Gr, Bl>>>(data, dim, src_mat, mat_stride, transpose, elements); } void cudaD_matrix_add_indexed_values(dim3 Gr, dim3 Bl, MatrixDim dim, double alpha, const Int32Pair* indices, const double* x, int s, double* data) { _cuda_matrix_add_indexed_values<<<Gr, Bl>>>(dim, alpha, indices, x, s, data); } void cudaD_matrix_add_to_elements(dim3 Gr, dim3 Bl, double alpha, double* mat, MatrixDim dim, const MatrixIndexT_cuda* elements) { _cuda_matrix_add_to_elements<<<Gr, Bl>>>(alpha, mat, dim, elements); } void cudaD_vec_copy_diag_from_packed(int Gr, int Bl, double *dst, const double *src, int dim) { _vec_copy_diag_from_packed<<<Gr,Bl>>>(dst,src,dim); } void cudaD_vec_apply_floor(int Gr, int Bl, double* v, double floor_val, float *count, int dim) { _vec_apply_floor<<<Gr,Bl>>>(v,floor_val,count,dim); } void cudaD_vec_apply_ceiling(int Gr, int Bl, double* v, double ceiling_val, float *count, int dim) { _vec_apply_ceiling<<<Gr,Bl>>>(v,ceiling_val,count,dim); } void cudaD_vec_apply_exp(int Gr, int Bl, double* v, int dim) { _vec_apply_exp<<<Gr,Bl>>>(v,dim); } void cudaD_vec_apply_log(int Gr, int Bl, double* v, double* flag, int dim) { _vec_apply_log<<<Gr,Bl>>>(v,flag,dim); } void cudaD_invert_elements(dim3 Gr, dim3 Bl, double* data, MatrixDim d) { _invert_elements<<<Gr,Bl>>>(data, d); } void cudaD_add_mat_blockmat(dim3 Gr, dim3 Bl, double *data, MatrixDim d, const double *Adata, int A_num_rows, int A_num_cols, int A_row_stride, int A_col_stride, const CuBlockMatrixData *B_cu_data, int B_num_blocks, double alpha, double beta, int B_trans) { if (B_trans) { _add_mat_blockmat_trans<<<Gr,Bl>>>(data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } else { _add_mat_blockmat<<<Gr,Bl>>>(data, d, Adata, A_num_rows, A_num_cols, A_row_stride, A_col_stride, B_cu_data, B_num_blocks, alpha, beta); } } void cudaD_block_add_mat_mat(dim3 Gr, dim3 Bl, CuBlockMatrixData *B_cu_data, int num_blocks, const double *C_data, int C_num_cols, int C_row_stride, int C_col_stride, const double *D_data, int D_row_stride, int D_col_stride, double alpha, double beta) { _block_add_mat_mat<<<Gr,Bl>>>(B_cu_data, num_blocks, C_data, C_num_cols, C_row_stride, C_col_stride, D_data, D_row_stride, D_col_stride, alpha, beta); } /* * cu:: */ void cudaD_soft_hinge(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { _soft_hinge<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaD_group_pnorm(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size, double power) { _group_pnorm<<<Gr,Bl>>>(y, x, d, src_stride, group_size, power); } void cudaD_group_spec_pnorm(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size, double power) { if (power == double(0)) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<L0NORM, double>()); } else if (power == double(1)) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<L1NORM, double>()); } else if (power == double(2)) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<L2NORM, double>()); } else if (power == std::numeric_limits<double>::infinity()) { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<LINFNORM, double>()); } else { _group_transform_reduce<<<Gr, Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<LPNORM, double>(power)); } } void cudaD_group_max(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, int group_size) { _group_transform_reduce<<<Gr,Bl>>>(y, x, d, src_stride, group_size, TransReduceOp<MAX, double>()); } void cudaD_sigmoid(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { _sigmoid<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaD_diff_sigmoid(dim3 Gr, dim3 Bl, double* eout, const double* e, const double* y, MatrixDim d, int e_stride, int y_stride) { _diff_sigmoid<<<Gr,Bl>>>(eout, e, y, d, e_stride, y_stride); } void cudaD_tanh(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { _tanh<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaD_diff_tanh(dim3 Gr, dim3 Bl, double* eout, const double* e, const double* y, MatrixDim d, int e_stride, int y_stride) { _diff_tanh<<<Gr,Bl>>>(eout, e, y, d, e_stride, y_stride); } void cudaD_ensure_nonzero(dim3 Gr, dim3 Bl, const double *x, MatrixDim d, double epsilon, int y_stride, double *y) { _ensure_nonzero<<<Gr,Bl>>>(x, d, epsilon, y_stride, y); } void cudaD_parametric_relu(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride, const double* a, const double* b) { _parametric_relu<<<Gr,Bl>>>(y, x, d, src_stride, a, b); } void cudaD_diff_parametric_relu(dim3 Gr, dim3 Bl, double* eout, const double* e, const double* y, MatrixDim d, int e_stride, int y_stride, const double* a, const double* b) { _diff_parametric_relu<<<Gr,Bl>>>(eout, e, y, d, e_stride, y_stride, a, b); } void cudaD_heaviside(dim3 Gr, dim3 Bl, double* y, const double* x, MatrixDim d, int src_stride) { _heaviside<<<Gr,Bl>>>(y, x, d, src_stride); } void cudaD_softmax_reduce(size_t Gr, size_t Bl, double* y, const double* x, MatrixDim d, int src_stride, double *logsum, cudaStream_t s) { _softmax_reduce<<<Gr,Bl,0,s>>>(y, x, d, src_stride, logsum); } void cudaD_log_softmax_reduce(size_t Gr, size_t Bl, double* y, const double* x, MatrixDim y_dim, int x_stride) { _log_softmax_reduce<<<Gr,Bl>>>(y, x, y_dim, x_stride); } void cudaD_normalize_per_row(size_t Gr, size_t Bl, double *y, int y_stride, const double *x, MatrixDim x_d, double target_rms, bool add_log_stddev) { _normalize_per_row<<<Gr, Bl>>>(y, y_stride, x, x_d, target_rms, add_log_stddev); } void cudaD_splice(dim3 Gr, dim3 Bl, double* y, const double* x, const int32_cuda* off, MatrixDim d_out, MatrixDim d_in) { _splice<<<Gr,Bl>>>(y,x,off,d_out,d_in); } void cudaD_one(int Gr, int Bl, double* x, int dim) { _one<<<Gr,Bl>>>(x,dim); } void cudaD_take_mean(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_in) { _take_mean<<<Gr,Bl>>>(x,y,d_in); } void cudaD_take_lower(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_in) { _take_lower<<<Gr,Bl>>>(x,y,d_in); } void cudaD_take_upper(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_in) { _take_upper<<<Gr,Bl>>>(x,y,d_in); } void cudaD_copy_from_sp(dim3 Gr, dim3 Bl, const double* x, double* y, MatrixDim d_out) { _copy_from_sp<<<Gr,Bl>>>(x,y,d_out); } void cudaD_copy(dim3 Gr, dim3 Bl, double* y, const double* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { _copy<<<Gr,Bl>>>(y,x,copy_from,d_out,d_in); } void cudaD_randomize(dim3 Gr, dim3 Bl, double* y, const double* x, const int32_cuda* copy_from, MatrixDim d_out, MatrixDim d_in) { _randomize<<<Gr,Bl>>>(y,x,copy_from,d_out,d_in); } void cudaD_regularize_l1(dim3 Gr, dim3 Bl, double* wei, double* grad, double l1, double lr, MatrixDim d, int stride_grad) { _regularize_l1<<<Gr,Bl>>>(wei,grad,l1,lr,d,stride_grad); } void cudaD_find_row_max_id(dim3 Gr, dim3 Bl, const double* mat, double* vec_val, int32_cuda* vec_id, MatrixDim d) { _find_row_max_id<<<Gr,Bl>>>(mat, vec_val, vec_id, d); } void cudaD_find_row_abs_max(dim3 Gr, dim3 Bl, const double* mat, double *row_max, MatrixDim d) { _find_row_abs_max<<<Gr,Bl>>>(mat, row_max, d); } void cudaD_diff_xent(dim3 Gr, dim3 Bl, const int32_cuda* vec_tgt, double* mat_net_out, double* vec_log_post, MatrixDim d) { _diff_xent<<<Gr,Bl>>>(vec_tgt,mat_net_out,vec_log_post,d); } void cudaD_diff_softmax(dim3 Gr, dim3 Bl, double* x, const MatrixDim dim, const double* value, const int value_stride, const double* diff, const int diff_stride) { _diff_softmax<<<Gr, Bl>>>(x, dim, value, value_stride, diff, diff_stride); } void cudaD_diff_log_softmax(dim3 Gr, dim3 Bl, const MatrixDim in_deriv_dim, const double* out_value, const int out_value_stride, const double* out_deriv, const int out_deriv_stride, double* in_deriv) { _diff_log_softmax<<<Gr, Bl>>>(in_deriv_dim, out_value, out_value_stride, out_deriv, out_deriv_stride, in_deriv); } void cudaD_copy_rows_from_vec(dim3 Gr, dim3 Bl, double *mat_out, MatrixDim d_out, const double *v_in) { _copy_rows_from_vec<<<Gr,Bl>>>(mat_out, d_out, v_in); } void cudaD_sum_column_ranges(dim3 Gr, dim3 Bl, double *data, MatrixDim dim, const double *src_data, MatrixDim src_dim, const Int32Pair *indices) { _sum_column_ranges<<<Gr,Bl>>>(data, dim, src_data, src_dim, indices); } void cudaD_add_row_ranges(dim3 Gr, dim3 Bl, double *data, MatrixDim dim, const double *src_data, MatrixDim src_dim, const Int32Pair *indexes) { _add_row_ranges<<<Gr,Bl>>>(data, dim, src_data, src_dim, indexes); } void cudaD_matrix_lookup(dim3 Gr, dim3 Bl, const double *data, MatrixDim dim, const Int32Pair *indices, int indices_size, double *output) { _matrix_lookup<<<Gr,Bl>>>(data, dim, indices, indices_size, output); } void cudaD_equal_element_mask(dim3 Gr, dim3 Bl, const double *mat1, const double *mat2, double *mask, MatrixDim mat1_dim, int mat2_stride, int mask_stride) { _equal_element_mask<<<Gr,Bl>>>(mat1, mat2, mask, mat1_dim, mat2_stride, mask_stride); } // Some conversion kernels for which it's more convenient // to not name them F or D. void cuda_copy_from_mat_df(dim3 Gr, dim3 Bl, double* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat<<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_ff(dim3 Gr, dim3 Bl, float* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat<<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_fd(dim3 Gr, dim3 Bl, float *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat<<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_dd(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat<<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_df_trans(dim3 Gr, dim3 Bl, double* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat_trans<32> <<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_ff_trans(dim3 Gr, dim3 Bl, float* mat_out, const float* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat_trans<32> <<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_fd_trans(dim3 Gr, dim3 Bl, float *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat_trans<32> <<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_mat_dd_trans(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, MatrixDim d_out, MatrixDim d_in) { _copy_from_mat_trans<32> <<<Gr,Bl>>>(mat_out,mat_in,d_out,d_in); } void cuda_copy_from_smat_ff(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { _copy_from_smat<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_fd(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { _copy_from_smat<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_df(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { _copy_from_smat<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_dd(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { _copy_from_smat<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_ff_trans(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { _copy_from_smat_trans<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_fd_trans(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { _copy_from_smat_trans<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_df_trans(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { _copy_from_smat_trans<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cuda_copy_from_smat_dd_trans(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { _copy_from_smat_trans<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val); } void cudaF_trace_mat_smat(dim3 Gr, dim3 Bl, const float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val, float* trace_vec) { _trace_mat_smat<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaF_trace_mat_smat_trans(dim3 Gr, dim3 Bl, const float* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val, float* trace_vec) { _trace_mat_smat_trans<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaD_trace_mat_smat(dim3 Gr, dim3 Bl, const double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val, double* trace_vec) { _trace_mat_smat<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaD_trace_mat_smat_trans(dim3 Gr, dim3 Bl, const double* mat, MatrixDim mat_dim, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val, double* trace_vec) { _trace_mat_smat_trans<<<Gr, Bl>>>(mat, mat_dim, smat_row_ptr, smat_col_idx, smat_val, trace_vec); } void cudaD_lstm_nonlinearity(dim3 Gr, dim3 Bl, const double* in, const int in_stride, const double* params, const int params_stride, const int out_stride, const int cell_dim, const int have_dropout_mask, const int num_rows, double* out) { _lstm_nonlinearity<<<Gr, Bl>>>( in, in_stride, params, params_stride, out_stride, cell_dim, have_dropout_mask, num_rows, out); } void cudaF_lstm_nonlinearity(dim3 Gr, dim3 Bl, const float* in, const int in_stride, const float* params, const int params_stride, const int out_stride, const int cell_dim, const int have_dropout_mask, const int num_rows, float* out) { _lstm_nonlinearity<<<Gr, Bl>>>( in, in_stride, params, params_stride, out_stride, cell_dim, have_dropout_mask, num_rows, out); } void cudaD_diff_lstm_nonlinearity(dim3 Gr, dim3 Bl, const int cell_dim, const int have_dropout_mask, const int num_rows, const double* input, const int input_stride, const double* params, const int params_stride, const double* output_deriv, const int output_deriv_stride, const double* deriv_sum_in, const int deriv_sum_in_stride, const double* self_repair_config, double count, double* input_deriv, const int input_deriv_stride, double* params_deriv, const int params_deriv_stride, double* value_sum_out, const int value_sum_out_stride, double* deriv_sum_out, const int deriv_sum_out_stride, double* self_repair_sum_out, const int self_repair_sum_out_stride) { _diff_lstm_nonlinearity<<<Gr, Bl>>>( cell_dim, have_dropout_mask, num_rows, input, input_stride, params, params_stride, output_deriv, output_deriv_stride, deriv_sum_in, deriv_sum_in_stride, self_repair_config, count, input_deriv, input_deriv_stride, params_deriv, params_deriv_stride, value_sum_out, value_sum_out_stride, deriv_sum_out, deriv_sum_out_stride, self_repair_sum_out, self_repair_sum_out_stride); } void cudaF_diff_lstm_nonlinearity(dim3 Gr, dim3 Bl, const int cell_dim, const int have_dropout_mask, const int num_rows, const float* input, const int input_stride, const float* params, const int params_stride, const float* output_deriv, const int output_deriv_stride, const double* deriv_sum_in, const int deriv_sum_in_stride, const float* self_repair_config, double count, float* input_deriv, const int input_deriv_stride, float* params_deriv, const int params_deriv_stride, double* value_sum_out, const int value_sum_out_stride, double* deriv_sum_out, const int deriv_sum_out_stride, float* self_repair_sum_out, const int self_repair_sum_out_stride) { _diff_lstm_nonlinearity<<<Gr, Bl>>>( cell_dim, have_dropout_mask, num_rows, input, input_stride, params, params_stride, output_deriv, output_deriv_stride, deriv_sum_in, deriv_sum_in_stride, self_repair_config, count, input_deriv, input_deriv_stride, params_deriv, params_deriv_stride, value_sum_out, value_sum_out_stride, deriv_sum_out, deriv_sum_out_stride, self_repair_sum_out, self_repair_sum_out_stride); } void cudaD_copy_cols_from_vec(dim3 Gr, dim3 Bl, double *mat_out, MatrixDim d_out, const double *v_in) { _copy_cols_from_vec<<<Gr, Bl>>>(mat_out, d_out, v_in); } void cudaF_copy_cols_from_vec(dim3 Gr, dim3 Bl, float *mat_out, MatrixDim d_out, const float *v_in) { _copy_cols_from_vec<<<Gr, Bl>>>(mat_out, d_out, v_in); } void cudaF_diff_normalize_per_row(size_t Gr, size_t Bl, float *id, int id_stride, const float *iv, MatrixDim iv_dim, const float* od, int od_stride, float target_rms, bool add_log_stddev) { _diff_normalize_per_row<<<Gr, Bl>>>(id, id_stride, iv, iv_dim, od, od_stride, target_rms, add_log_stddev); } void cudaD_diff_normalize_per_row(size_t Gr, size_t Bl, double *id, int id_stride, const double *iv, MatrixDim iv_dim, const double* od, int od_stride, double target_rms, bool add_log_stddev) { _diff_normalize_per_row<<<Gr, Bl>>>(id, id_stride, iv, iv_dim, od, od_stride, target_rms, add_log_stddev); } void cudaD_select_rows(dim3 Gr, dim3 Bl, const int* out_row_ptr, int* out_col_idx, double* out_val, const int* row_indexes, const int num_selected_rows, const int* in_row_ptr, const int* in_col_idx, const double* in_val) { _select_rows<<<Gr, Bl>>>(out_row_ptr, out_col_idx, out_val, row_indexes, num_selected_rows, in_row_ptr, in_col_idx, in_val); } void cudaF_select_rows(dim3 Gr, dim3 Bl, const int* out_row_ptr, int* out_col_idx, float* out_val, const int* row_indexes, const int num_selected_rows, const int* in_row_ptr, const int* in_col_idx, const float* in_val) { _select_rows<<<Gr, Bl>>>(out_row_ptr, out_col_idx, out_val, row_indexes, num_selected_rows, in_row_ptr, in_col_idx, in_val); } void cudaD_add_smat(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, double alpha, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { _add_smat<<<Gr, Bl>>>(mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaF_add_smat(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, float alpha, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { _add_smat<<<Gr, Bl>>>(mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaD_add_smat_trans(dim3 Gr, dim3 Bl, double* mat, MatrixDim mat_dim, double alpha, const int* smat_row_ptr, const int* smat_col_idx, const double* smat_val) { _add_smat_trans<<<Gr, Bl>>>(mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaF_add_smat_trans(dim3 Gr, dim3 Bl, float* mat, MatrixDim mat_dim, float alpha, const int* smat_row_ptr, const int* smat_col_idx, const float* smat_val) { _add_smat_trans<<<Gr, Bl>>>(mat, mat_dim, alpha, smat_row_ptr, smat_col_idx, smat_val); } void cudaD_apply_exp_special(dim3 Gr, dim3 Bl, double* out, MatrixDim out_dim, const double* in, int in_stride) { _apply_exp_special<<<Gr, Bl>>>(out, out_dim, in, in_stride); } void cudaF_apply_exp_special(dim3 Gr, dim3 Bl, float* out, MatrixDim out_dim, const float* in, int in_stride) { _apply_exp_special<<<Gr, Bl>>>(out, out_dim, in, in_stride); } void cuda_compress_uint8_sign(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, unsigned char *dest, int dest_stride) { _cuda_compress_uint8_sign<<<Gr, Bl>>>(src, dim, dest, dest_stride); } void cuda_compress_int16(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, int16_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { _cuda_compress_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } else { _cuda_compress_no_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } } void cuda_compress_uint16(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, uint16_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { _cuda_compress_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } else { _cuda_compress_no_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } } void cuda_compress_int8(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, int8_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { _cuda_compress_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } else { _cuda_compress_no_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } } void cuda_compress_uint8(dim3 Gr, dim3 Bl, const BaseFloat *src, MatrixDim dim, uint8_t *dest, int dest_stride, float inv_scale, bool bounds_check) { if (bounds_check) { _cuda_compress_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } else { _cuda_compress_no_bounds_check<<<Gr, Bl>>>(src, dim, dest, dest_stride, inv_scale); } } void cuda_uncompress_uint8(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const uint8_t *src, int src_stride, float scale) { _cuda_uncompress<<<Gr, Bl>>>(dest, dim, src, src_stride, scale); } void cuda_uncompress_int8(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const int8_t *src, int src_stride, float scale) { _cuda_uncompress<<<Gr, Bl>>>(dest, dim, src, src_stride, scale); } void cuda_uncompress_uint16(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const uint16_t *src, int src_stride, float scale) { _cuda_uncompress<<<Gr, Bl>>>(dest, dim, src, src_stride, scale); } void cuda_uncompress_int16(dim3 Gr, dim3 Bl, BaseFloat *dest, MatrixDim dim, const int16_t *src, int src_stride, float scale) { _cuda_uncompress<<<Gr, Bl>>>(dest, dim, src, src_stride, scale); } /* * All the following kernels are written by Wei Deng for LSTM LM and convolution neural network training */ template<typename Real> __global__ static void _convolution_forward_expand_workspace( Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int filt_x_len_, int filt_y_len_, int filt_x_step_, int filt_y_step_, int connect_fmap) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda batchsize = srcdim.rows; int32_cuda out_fmap_index = i/batchsize; int32_cuda batch_index = i%batchsize; int32_cuda index = i * dstdim.stride + j; int32_cuda fmap_m, fmap_n, filt_i, filt_j, src_frame_index, src_index, st; if (i < dstdim.rows && j < dstdim.cols) { //fmap_m = out_fmap_index / (fmap_y_len_-filt_y_len_+1) * filt_x_step_; //fmap_n = out_fmap_index % (fmap_y_len_-filt_y_len_+1) * filt_y_step_; fmap_m = out_fmap_index / ((fmap_y_len_-filt_y_len_) * filt_y_step_+1) * filt_x_step_; fmap_n = out_fmap_index % ((fmap_y_len_-filt_y_len_) * filt_y_step_+1) * filt_y_step_; if (connect_fmap == 1) { st = (fmap_m * fmap_y_len_ + fmap_n) * num_input_fmaps; } else { st = fmap_m * fmap_y_len_ * num_input_fmaps + fmap_n; } filt_i = j/(filt_y_len_*num_input_fmaps); filt_j = j%(filt_y_len_*num_input_fmaps); if (connect_fmap == 1) { src_frame_index = st + filt_i*(num_input_fmaps*fmap_y_len_) + filt_j; } else { src_frame_index = st + filt_i * (num_input_fmaps * fmap_y_len_) + (filt_j / num_input_fmaps) + (filt_j % num_input_fmaps) * fmap_y_len_; } src_index = src_frame_index + batch_index*srcdim.stride; dst[index] = src[src_index]; } } template<typename Real> __global__ static void _convolution_backward_shrink_workspace(Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, const Int32Pair* const* map, const int32_cuda *mapsize) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda batchsize = dstdim.rows; int32_cuda k, src_index; int32_cuda index = i * dstdim.stride + j; if (i < dstdim.rows && j < dstdim.cols) { for (k = 0; k < mapsize[j]; k++) { src_index = (map[j][k].first*batchsize + i) * srcdim.stride + map[j][k].second; dst[index] += src[src_index]; } } } template<typename Real> __global__ static void _max_pooling_forward(Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda out_fmap_index = j/num_input_fmaps; int32_cuda filt_index = j%num_input_fmaps; int32_cuda fmap_m, fmap_n, st, x, y, c, src_index, dst_index, col; dst_index = i * dstdim.stride + j; if (i < dstdim.rows && j < dstdim.cols) { //fmap_m = out_fmap_index / (fmap_y_len_-pool_y_len_+1) * pool_x_step_; //fmap_n = out_fmap_index % (fmap_y_len_-pool_y_len_+1) * pool_y_step_; col = (fmap_y_len_ - pool_y_len_)/pool_y_step_+1; fmap_m = out_fmap_index / col * pool_x_step_; fmap_n = out_fmap_index % col * pool_y_step_; st = (fmap_m * fmap_y_len_ + fmap_n) * num_input_fmaps + filt_index; for (x = 0; x < pool_x_len_; x++) { for (y = 0; y < pool_y_len_; y++) { //c = st + x * (num_input_fmaps * fmap_y_len_) + y * num_input_fmaps; c = st + (x * fmap_y_len_ + y) * num_input_fmaps; src_index = i * srcdim.stride + c; if (src[src_index] > dst[dst_index]) dst[dst_index] = src[src_index]; } } } } template<typename Real> __global__ static void _max_pooling_backward(Real *dst, const Real *in, const Real *out, const Real *out_diff, MatrixDim dstdim, MatrixDim indim, MatrixDim outdim, MatrixDim outdiffdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda out_fmap_index = j/num_input_fmaps; int32_cuda filt_index = j%num_input_fmaps; int32_cuda fmap_m, fmap_n, st, x, y, c, in_index, dst_index, out_index, out_diff_index, col; out_index = i * outdim.stride + j; out_diff_index = i * outdiffdim.stride + j; if (i < dstdim.rows && j < dstdim.cols) { col = (fmap_y_len_ - pool_y_len_)/pool_y_step_+1; fmap_m = out_fmap_index / col * pool_x_step_; fmap_n = out_fmap_index % col * pool_y_step_; st = (fmap_m * fmap_y_len_ + fmap_n) * num_input_fmaps + filt_index; for (x = 0; x < pool_x_len_; x++) { for (y = 0; y < pool_y_len_; y++) { //c = st + x * (num_input_fmaps * fmap_y_len_) + y * num_input_fmaps; c = st + (x * fmap_y_len_ + y) * num_input_fmaps; in_index = i * indim.stride + c; dst_index = i * dstdim.stride + c; if (out[out_index] == in[in_index]) dst[dst_index] += out_diff[out_diff_index]; } } } } template<typename Real> __global__ static void _pad_feature_map(Real *dst, const Real *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len, int fmap_y_len, int pad_x_len, int pad_y_len, int pad_wipe, int connect_fmap) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda fmap_index, fmap_offset, out_fmap_offset, src_index, dst_index, fmap_x, fmap_y, out_fmap_y_len, out_j; out_fmap_y_len = fmap_y_len+2*pad_y_len; //fmap_size = fmap_x_len*fmap_y_len; //out_fmap_size = out_fmap_x_len*out_fmap_y_len; // fmap_index = connect_fmap == 1 ? j%num_input_fmaps : j/fmap_size; // fmap_offset = connect_fmap == 1 ? j/num_input_fmaps : j%fmap_size; if (connect_fmap == 1) { fmap_index = j%num_input_fmaps; fmap_offset = j/num_input_fmaps; fmap_x = fmap_offset/fmap_y_len; fmap_y = fmap_offset%fmap_y_len; out_fmap_offset = (fmap_x+pad_x_len)*out_fmap_y_len + (fmap_y+pad_y_len); //out_j = connect_fmap == 1 ? out_fmap_offset*num_input_fmaps+fmap_index : fmap_index*out_fmap_size+out_fmap_offset; out_j = out_fmap_offset*num_input_fmaps+fmap_index; } else { fmap_x = j/(fmap_y_len*num_input_fmaps); fmap_y = j%(fmap_y_len*num_input_fmaps); fmap_index = fmap_y/fmap_y_len; fmap_offset = fmap_y%fmap_y_len; out_fmap_offset = fmap_index*out_fmap_y_len + (fmap_offset+pad_y_len); out_j = (fmap_x+pad_x_len)*(out_fmap_y_len*num_input_fmaps) + out_fmap_offset; } src_index = pad_wipe == 0 ? i*srcdim.stride+j : i*srcdim.stride + out_j; dst_index = pad_wipe == 0 ? i*dstdim.stride+out_j : i*dstdim.stride+j; if (i < srcdim.rows && i < dstdim.rows && j < srcdim.cols && j < dstdim.cols) dst[dst_index] = src[src_index]; } template<typename Real> __global__ static void _sum_mats(Real* dst, Real **src_array, MatrixDim dstdim, MatrixDim srcdim, int batchcount) { int m = blockIdx.x, n = blockIdx.y; int THREADS = blockDim.x; if (m >= dstdim.rows) return; __shared__ Real aux[CU1DBLOCK*2]; int steps = (batchcount - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = src_array[threadIdx.x][m*srcdim.stride+n]; for(int i=1; i<steps; ++i) { if(threadIdx.x+i*THREADS < batchcount) aux[threadIdx.x] += src_array[i*THREADS+threadIdx.x][m*srcdim.stride+n]; } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); if (threadIdx.x == 0) dst[m*dstdim.stride+n] = aux[0]; } template<typename Real> __global__ static void _copy_col_mats(Real* dst, Real **src_array, MatrixDim dst_dim, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index, src_index, dst_index; if (i < dst_dim.rows && j < dst_dim.cols) { index = j/src_dim.cols; dst_index = i * dst_dim.stride + j; src_index = i * src_dim.stride + j%src_dim.cols; if (index >= 0) dst[dst_index] = src_array[index][src_index]; else dst[dst_index] = 0; } } template<typename Real> __global__ static void _copy_row_mats(Real* dst, Real **src_array, MatrixDim dst_dim, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int index, src_index, dst_index; if (i < dst_dim.rows && j < dst_dim.cols) { index = i/src_dim.rows; dst_index = i * dst_dim.stride + j; src_index = (i%src_dim.rows) * src_dim.stride + j; if (index >= 0) dst[dst_index] = src_array[index][src_index]; else dst[dst_index] = 0; } } template<typename Real> __global__ static void _row_sum_reduce(Real alpha, Real *y, const Real *x, Real beta, MatrixDim d) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = x[threadIdx.x+j*d.stride]; for(int i=1; i<steps; i++) { if(threadIdx.x+i*THREADS < d.cols) { aux[threadIdx.x] += x[threadIdx.x+i*THREADS+j*d.stride]; } } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); y[j] = beta*y[j] + alpha*aux[0]; } template<typename Real> __global__ static void _col_sum_reduce(Real alpha, Real *y, const Real *x, Real beta, MatrixDim d) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.cols) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.rows - 1) / THREADS + 1; //copy input to aux aux[threadIdx.x] = x[threadIdx.x*d.stride+j]; for(int i=1; i<steps; i++) { if(threadIdx.x+i*THREADS < d.rows) { aux[threadIdx.x] += x[(threadIdx.x+i*THREADS)*d.stride+j]; } } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); y[j] = beta*y[j] + alpha*aux[0]; } template<typename Real> __global__ static void _row_max_id(int32_cuda *vec_id, const Real *x, MatrixDim d) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.rows) return; __shared__ Real value[CU1DBLOCK]; __shared__ int32_cuda index[CU1DBLOCK]; int steps = (d.cols - 1) / THREADS + 1; //copy input to aux value[threadIdx.x] = x[threadIdx.x+j*d.stride]; index[threadIdx.x] = threadIdx.x; for(int i=1; i<steps; ++i) { if(threadIdx.x+i*THREADS < d.cols && value[threadIdx.x] < x[threadIdx.x+i*THREADS+j*d.stride]) { value[threadIdx.x] = x[threadIdx.x+i*THREADS+j*d.stride]; index[threadIdx.x] = threadIdx.x+i*THREADS; } } int nTotalThreads = THREADS; __syncthreads(); while(nTotalThreads > 1) { int halfPoint = ((1+nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if(threadIdx.x+halfPoint < nTotalThreads && value[threadIdx.x] < value[threadIdx.x+halfPoint]) { value[threadIdx.x] = value[threadIdx.x + halfPoint]; index[threadIdx.x] = index[threadIdx.x + halfPoint]; } } __syncthreads(); nTotalThreads = ((1+nTotalThreads) >> 1); // divide by two. } __syncthreads(); vec_id[j] = index[0]; } template<typename Real> __global__ static void _gen_tgt(Real *x, const int32_cuda *vec_id, MatrixDim d) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // column index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // row index int32_cuda index = i + j * d.stride; if (i < d.cols && j < d.rows) x[index] = (i==vec_id[j] ? 1.0 : 0.0); } template<typename Real> __global__ static void _copy_row_to_vecid(const Real *mat, MatrixIndexT_cuda *vec_id, MatrixDim dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index if (i < dim.cols) vec_id[i] = (MatrixIndexT_cuda)mat[i]; } template<typename Real> __global__ static void _add_mat_to_rows(Real alpha, Real *dst, const Real *src, const MatrixIndexT_cuda* reorder, MatrixDim dst_dim, MatrixDim src_dim) { int i = blockIdx.x * blockDim.x + threadIdx.x; // col index int j = blockIdx.y * blockDim.y + threadIdx.y; // row index if (i < dst_dim.cols && j < dst_dim.rows) { int index = reorder[j]; int dst_index = index * dst_dim.stride + i; int src_index = j * src_dim.stride + i; dst[dst_index] += index >= 0 ? alpha*src[src_index] : 0; } } template<typename Real> __global__ static void _cross_entropy(Real *xentropy, const Real *nnetout, const Real *target, MatrixDim dst_d, int out_stride, int tgt_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j*dst_d.stride, out_index = i + j*out_stride, tgt_index = i + j*tgt_stride; if (i < dst_d.cols && j < dst_d.rows) { xentropy[dst_index] = nnetout[out_index] + 1e-20; xentropy[dst_index] = log(xentropy[dst_index]); xentropy[dst_index] *= target[tgt_index]; } } template<typename Real> __global__ static void _entropy(Real *entropy, const Real *mat, MatrixDim dst_d, int mat_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; int32_cuda dst_index = i + j*dst_d.stride, mat_index = i + j*mat_stride; if (i < dst_d.cols && j < dst_d.rows) { entropy[dst_index] = mat[mat_index] + 1e-20; entropy[dst_index] = log(entropy[dst_index]); entropy[dst_index] *= mat[mat_index]; } } void cudaF_convolution_forward_expand_workspace(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int filt_x_len_, int filt_y_len_, int filt_x_step_, int filt_y_step_, int connect_fmap) { _convolution_forward_expand_workspace<<<Gr,Bl>>>(dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_,filt_x_len_, filt_y_len_, filt_x_step_, filt_y_step_, connect_fmap); } void cudaF_convolution_backward_shrink_workspace(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, const Int32Pair* const* map, const int32_cuda *mapsize) { _convolution_backward_shrink_workspace<<<Gr,Bl>>>(dst, src, dstdim, srcdim, map, mapsize); } void cudaF_max_pooling_forward(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { _max_pooling_forward<<<Gr,Bl>>>(dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaF_max_pooling_backward(dim3 Gr, dim3 Bl, float *dst, const float *in, const float *out, const float *out_diff, MatrixDim dstdim, MatrixDim indim, MatrixDim outdim, MatrixDim outdiffdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { _max_pooling_backward<<<Gr,Bl>>>(dst, in, out, out_diff, dstdim, indim, outdim, outdiffdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaF_pad_feature_map(dim3 Gr, dim3 Bl, float *dst, const float *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len, int fmap_y_len, int pad_x_len, int pad_y_len, int pad_wipe, int connect_fmap) { _pad_feature_map<<<Gr,Bl>>>(dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len, fmap_y_len, pad_x_len, pad_y_len, pad_wipe, connect_fmap); } void cudaF_sum_mats(dim3 Gr, dim3 Bl, float* dst, float **src_array, MatrixDim dstdim, MatrixDim srcdim, int batchcount) { _sum_mats<<<Gr,Bl>>>(dst, src_array, dstdim, srcdim, batchcount); } void cudaF_copy_col_mats(dim3 Gr, dim3 Bl, float* dst, float **src_array, MatrixDim dst_dim, MatrixDim src_dim) { _copy_col_mats<<<Gr,Bl>>>(dst, src_array, dst_dim, src_dim); } void cudaF_copy_row_mats(dim3 Gr, dim3 Bl, float* dst, float **src_array, MatrixDim dst_dim, MatrixDim src_dim) { _copy_row_mats<<<Gr,Bl>>>(dst, src_array, dst_dim, src_dim); } void cudaF_cross_entropy(dim3 Gr, dim3 Bl, float *xentropy, const float *nnetout, const float *target, MatrixDim d, int out_stride, int tgt_stride, cudaStream_t s) { _cross_entropy<<<Gr,Bl,0,s>>>(xentropy,nnetout,target,d,out_stride,tgt_stride); } void cudaF_entropy(dim3 Gr, dim3 Bl, float *entropy, const float *mat, MatrixDim d, int mat_stride, cudaStream_t s) { _entropy<<<Gr,Bl,0,s>>>(entropy,mat,d,mat_stride); } void cudaF_row_sum_reduce(size_t Gr, size_t Bl, float alpha, float *y, const float *x, MatrixDim d, float beta, cudaStream_t s) { _row_sum_reduce<<<Gr,Bl,0,s>>>(alpha,y,x,beta,d); } void cudaF_col_sum_reduce(size_t Gr, size_t Bl, float alpha, float *y, const float *x, MatrixDim d, float beta, cudaStream_t s) { _col_sum_reduce<<<Gr,Bl,0,s>>>(alpha,y,x,beta,d); } void cudaD_vec_apply_fixed(int Gr, int Bl, double* v, double resolution, int mode, int dim){ _vec_apply_fixed<<<Gr,Bl>>>(v, resolution, mode, dim); } void cudaF_row_max_id(size_t Gr, size_t Bl, int32_cuda *vec_id, const float *x, MatrixDim d, cudaStream_t s) { _row_max_id<<<Gr,Bl,0,s>>>(vec_id,x,d); } void cudaF_gen_tgt(dim3 Gr, dim3 Bl, float *x, const int32_cuda *vec_id, MatrixDim d, cudaStream_t s) { _gen_tgt<<<Gr,Bl,0,s>>>(x, vec_id, d); } void cudaF_copy_row_to_vecid(dim3 Gr, dim3 Bl, const float *mat, MatrixIndexT_cuda *vec_id, MatrixDim dim) { _copy_row_to_vecid<<<Gr, Bl>>>(mat, vec_id, dim); } void cudaF_add_mat_to_rows(dim3 Gr, dim3 Bl, float alpha, float *dst, const float *src, const MatrixIndexT_cuda *vec_id, MatrixDim dst_dim, MatrixDim src_dim) { _add_mat_to_rows<<<Gr, Bl>>>(alpha, dst, src, vec_id, dst_dim, src_dim); } /* * double */ void cudaD_convolution_forward_expand_workspace(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int filt_x_len_, int filt_y_len_, int filt_x_step_, int filt_y_step_, int connect_fmap) { _convolution_forward_expand_workspace<<<Gr,Bl>>>(dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_,filt_x_len_, filt_y_len_, filt_x_step_, filt_y_step_, connect_fmap); } void cudaD_convolution_backward_shrink_workspace(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, const Int32Pair* const* map, const int32_cuda *mapsize) { _convolution_backward_shrink_workspace<<<Gr,Bl>>>(dst, src, dstdim, srcdim, map, mapsize); } void cudaD_max_pooling_forward(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { _max_pooling_forward<<<Gr,Bl>>>(dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaD_max_pooling_backward(dim3 Gr, dim3 Bl, double *dst, const double *in, const double *out, const double *out_diff, MatrixDim dstdim, MatrixDim indim, MatrixDim outdim, MatrixDim outdiffdim, int num_input_fmaps, int fmap_x_len_, int fmap_y_len_, int pool_x_len_, int pool_y_len_, int pool_x_step_, int pool_y_step_) { _max_pooling_backward<<<Gr,Bl>>>(dst, in, out, out_diff, dstdim, indim, outdim, outdiffdim, num_input_fmaps, fmap_x_len_, fmap_y_len_, pool_x_len_, pool_y_len_, pool_x_step_, pool_y_step_); } void cudaD_pad_feature_map(dim3 Gr, dim3 Bl, double *dst, const double *src, MatrixDim dstdim, MatrixDim srcdim, int num_input_fmaps, int fmap_x_len, int fmap_y_len, int pad_x_len, int pad_y_len, int pad_wipe, int connect_fmap) { _pad_feature_map<<<Gr,Bl>>>(dst, src, dstdim, srcdim, num_input_fmaps, fmap_x_len, fmap_y_len, pad_x_len, pad_y_len, pad_wipe, connect_fmap); } void cudaD_sum_mats(dim3 Gr, dim3 Bl, double* dst, double **src_array, MatrixDim dstdim, MatrixDim srcdim, int batchcount) { _sum_mats<<<Gr,Bl>>>(dst, src_array, dstdim, srcdim, batchcount); } void cudaD_copy_col_mats(dim3 Gr, dim3 Bl, double* dst, double **src_array, MatrixDim dst_dim, MatrixDim src_dim) { _copy_col_mats<<<Gr,Bl>>>(dst, src_array, dst_dim, src_dim); } void cudaD_copy_row_mats(dim3 Gr, dim3 Bl, double* dst, double **src_array, MatrixDim dst_dim, MatrixDim src_dim) { _copy_row_mats<<<Gr,Bl>>>(dst, src_array, dst_dim, src_dim); } void cudaD_row_sum_reduce(size_t Gr, size_t Bl, double alpha, double *y, const double *x, MatrixDim d, double beta, cudaStream_t s) { _row_sum_reduce<<<Gr,Bl,0,s>>>(alpha,y,x,beta,d); } void cudaD_col_sum_reduce(size_t Gr, size_t Bl, double alpha, double *y, const double *x, MatrixDim d, double beta, cudaStream_t s) { _col_sum_reduce<<<Gr,Bl,0,s>>>(alpha,y,x,beta,d); } void cudaD_row_max_id(size_t Gr, size_t Bl, int32_cuda *vec_id, const double *x, MatrixDim d, cudaStream_t s) { _row_max_id<<<Gr,Bl,0,s>>>(vec_id,x,d); } void cudaD_gen_tgt(dim3 Gr, dim3 Bl, double *x, const int32_cuda *vec_id, MatrixDim d, cudaStream_t s) { _gen_tgt<<<Gr,Bl,0,s>>>(x, vec_id, d); } void cudaD_copy_row_to_vecid(dim3 Gr, dim3 Bl, const double *mat, MatrixIndexT_cuda *vec_id, MatrixDim dim) { _copy_row_to_vecid<<<Gr, Bl>>>(mat, vec_id, dim); } void cudaD_add_mat_to_rows(dim3 Gr, dim3 Bl, double alpha, double *dst, const double *src, const MatrixIndexT_cuda *vec_id, MatrixDim dst_dim, MatrixDim src_dim) { _add_mat_to_rows<<<Gr, Bl>>>(alpha, dst, src, vec_id, dst_dim, src_dim); } void cudaD_cross_entropy(dim3 Gr, dim3 Bl, double *xentropy, const double *nnetout, const double *target, MatrixDim d, int out_stride, int tgt_stride, cudaStream_t s) { _cross_entropy<<<Gr,Bl,0,s>>>(xentropy,nnetout,target,d,out_stride,tgt_stride); } void cudaD_entropy(dim3 Gr, dim3 Bl, double *entropy, const double *mat, MatrixDim d, int mat_stride, cudaStream_t s) { _entropy<<<Gr,Bl,0,s>>>(entropy,mat,d,mat_stride); } /* * All the following kernels are written by Yajie Miao for CTC training */ #include "cuPrintf.cuh" #include "cuPrintf.cu" #include "ctc-utils.h" template<typename Real> __global__ static void _compute_ctc_alpha_one_sequence(Real* mat_alpha, int row, MatrixDim dim_alpha, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_alpha.cols; if (i < dim) { int32_cuda index_alpha = i + row * dim_alpha.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_alpha_rm1_i = i + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im1 = (i - 1) + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im2 = (i - 2) + (row - 1) * dim_alpha.stride; if (row == 0) { if (i < 2) mat_alpha[index_alpha] = mat_prob[index_prob]; else mat_alpha[index_alpha] = NumericLimits<Real>::log_zero_; } else { if (i > 1) { if (i % 2 == 0 || labels[i-2] == labels[i]) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { Real tmp = LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i]); mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im2], tmp)); } } else if (i == 1) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], mat_alpha[index_alpha_rm1_i]); } } } } template<typename Real> __global__ static void _compute_ctc_alpha_multiple_sequence(Real* mat_alpha, int sequence_num, int row, MatrixDim dim_alpha, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels, int32_cuda dim_label_stride, const int32_cuda* seq_lengths) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index, that is, the index for sequence int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // label index, cannot exceed 2*|l|+1 int32_cuda dim = dim_alpha.cols; if (j >= dim || i >= sequence_num) return; int32_cuda index_alpha = j + (row * sequence_num + i) * dim_alpha.stride; int32_cuda index_label = j + i * dim_label_stride; int32_cuda class_idx = labels[index_label];// if -1, this is the padding cell;labels now is a matrix which has the same size as mat_alpha if (class_idx == -1 || row >= seq_lengths[i]) { mat_alpha[index_alpha] = NumericLimits<Real>::log_zero_; return; } int32_cuda index_label_m2 = (j-2) + i * dim_label_stride; int32_cuda index_prob = class_idx + (row * sequence_num + i) * dim_prob.stride; int32_cuda index_alpha_rm1_i = j + ((row-1) * sequence_num + i) * dim_alpha.stride; int32_cuda index_alpha_rm1_im1 = (j-1) + ((row-1) * sequence_num + i) * dim_alpha.stride; int32_cuda index_alpha_rm1_im2 = (j-2) + ((row-1) * sequence_num + i) * dim_alpha.stride; if (row == 0) { if (j < 2) mat_alpha[index_alpha] = mat_prob[index_prob]; else mat_alpha[index_alpha] = NumericLimits<Real>::log_zero_; } else { if (j > 1) { if (j % 2 == 0 || labels[index_label_m2] == labels[index_label]) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { Real tmp = LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i]); mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im2], tmp)); } } else if (j == 1) { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], LogAPlusB(mat_alpha[index_alpha_rm1_im1], mat_alpha[index_alpha_rm1_i])); } else { mat_alpha[index_alpha] = AddAB(mat_prob[index_prob], mat_alpha[index_alpha_rm1_i]); } } } template<typename Real> __global__ static void _compute_ctc_alpha_one_sequence_rescale(Real* mat_alpha, int row, MatrixDim dim_alpha, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_alpha.cols; if (i < dim) { int32_cuda index_alpha = i + row * dim_alpha.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_alpha_rm1_i = i + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im1 = (i - 1) + (row - 1) * dim_alpha.stride; int32_cuda index_alpha_rm1_im2 = (i - 2) + (row - 1) * dim_alpha.stride; if (row == 0) { if (i < 2) mat_alpha[index_alpha] = mat_prob[index_prob]; else mat_alpha[index_alpha] = 0.0; } else { if (i > 1) { if (i % 2 == 0 || labels[i-2] == labels[i]) { mat_alpha[index_alpha] = mat_prob[index_prob] * (mat_alpha[index_alpha_rm1_im1] + mat_alpha[index_alpha_rm1_i]); } else { mat_alpha[index_alpha] = mat_prob[index_prob] * (mat_alpha[index_alpha_rm1_im1] + mat_alpha[index_alpha_rm1_i] + mat_alpha[index_alpha_rm1_im2]); } } else if (i == 1) { mat_alpha[index_alpha] = mat_prob[index_prob] * (mat_alpha[index_alpha_rm1_im1] + mat_alpha[index_alpha_rm1_i]); } else { mat_alpha[index_alpha] = mat_prob[index_prob] * mat_alpha[index_alpha_rm1_i]; } } } } template<typename Real> __global__ static void _compute_ctc_beta_one_sequence(Real* mat_beta, int row, MatrixDim dim_beta, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_beta.cols; if (i < dim) { int32_cuda index_beta = i + row * dim_beta.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_beta_rp1_i = i + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip1 = (i + 1) + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip2 = (i + 2) + (row + 1) * dim_beta.stride; int32_cuda row_num = dim_beta.rows; if (row == row_num - 1) { if (i > dim - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = NumericLimits<Real>::log_zero_; } else { if (i < dim - 2) { if (i % 2 == 0 || labels[i+2] == labels[i]) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { Real tmp = LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i]); mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip2], tmp)); } } else if (i == dim - 2) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { mat_beta[index_beta] = AddAB(mat_prob[index_prob], mat_beta[index_beta_rp1_i]); } } } } template<typename Real> __global__ static void _compute_ctc_beta_multiple_sequence(Real* mat_beta, int sequence_num, int row, MatrixDim dim_beta, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels, int32_cuda dim_label_stride, const int32_cuda* seq_lengths, const int32_cuda* label_lengths) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index, that is, the index for sequence int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // label index, cannot exceed 2*|l|+1 int32_cuda dim = dim_beta.cols; if (j >= dim || i >= sequence_num) return; int32_cuda index_beta = j + (row * sequence_num + i) * dim_beta.stride; int32_cuda index_label = j + i * dim_label_stride; int32_cuda class_idx = labels[index_label];// if -1, this is the padding cell;labels now is a matrix which has the same size as mat_alpha if (class_idx == -1 || row >= seq_lengths[i]) { mat_beta[index_beta] = NumericLimits<Real>::log_zero_; return; } int32_cuda index_label_p2 = (j+2) + i * dim_label_stride; int32_cuda index_prob = class_idx + (row * sequence_num + i) * dim_prob.stride; int32_cuda index_beta_rp1_i = j + ((row+1) * sequence_num + i) * dim_beta.stride; int32_cuda index_beta_rp1_ip1 = (j+1) + ((row+1) * sequence_num + i) * dim_beta.stride; int32_cuda index_beta_rp1_ip2 = (j+2) + ((row+1) * sequence_num + i) * dim_beta.stride; int32_cuda row_num = seq_lengths[i]; int32_cuda label_len = label_lengths[i]; /* if (row == row_num - 1) { if (j > dim - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = NumericLimits<Real>::log_zero_; } else { if (j < dim - 2) { if (j % 2 == 0 || labels[index_label_p2] == labels[index_label]) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { Real tmp = LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i]); mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip2], tmp)); } } else if (j == dim - 2) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { mat_beta[index_beta] = AddAB(mat_prob[index_prob], mat_beta[index_beta_rp1_i]); } } */ if (row == row_num - 1) { if (j > label_len - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = NumericLimits<Real>::log_zero_; } else { if (j < label_len - 2) { if (j % 2 == 0 || labels[index_label_p2] == labels[index_label]) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { Real tmp = LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i]); mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip2], tmp)); } } else if (j == label_len - 2) { mat_beta[index_beta] = AddAB(mat_prob[index_prob], LogAPlusB(mat_beta[index_beta_rp1_ip1], mat_beta[index_beta_rp1_i])); } else { mat_beta[index_beta] = AddAB(mat_prob[index_prob], mat_beta[index_beta_rp1_i]); } } } template<typename Real> __global__ static void _compute_ctc_beta_one_sequence_rescale(Real* mat_beta, int row, MatrixDim dim_beta, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; int32_cuda dim = dim_beta.cols; if (i < dim) { int32_cuda index_beta = i + row * dim_beta.stride; int32_cuda class_idx = labels[i]; int32_cuda index_prob = class_idx + row * dim_prob.stride; int32_cuda index_beta_rp1_i = i + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip1 = (i + 1) + (row + 1) * dim_beta.stride; int32_cuda index_beta_rp1_ip2 = (i + 2) + (row + 1) * dim_beta.stride; int32_cuda row_num = dim_beta.rows; if (row == row_num - 1) { if (i > dim - 3) mat_beta[index_beta] = mat_prob[index_prob]; else mat_beta[index_beta] = 0; } else { if (i < dim - 2) { if (i % 2 == 0 || labels[i+2] == labels[i]) { mat_beta[index_beta] = mat_prob[index_prob] * (mat_beta[index_beta_rp1_ip1] + mat_beta[index_beta_rp1_i]); } else { mat_beta[index_beta] = mat_prob[index_prob] * (mat_beta[index_beta_rp1_ip1] + mat_beta[index_beta_rp1_i] + mat_beta[index_beta_rp1_ip2]); } } else if (i == dim - 2) { mat_beta[index_beta] = mat_prob[index_prob] * (mat_beta[index_beta_rp1_ip1] + mat_beta[index_beta_rp1_i]); } else { mat_beta[index_beta] = mat_prob[index_prob] * mat_beta[index_beta_rp1_i]; } } } } // mat_prob are in probability scale. template<typename Real> __global__ static void _compute_ctc_error_one_sequence(Real* mat_error, MatrixDim dim_error, const Real* mat_alpha, const Real* mat_beta, MatrixDim dim_alpha, const Real* mat_prob, const int32_cuda* labels, Real pzx) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i < dim_error.rows && j < dim_error.cols) { Real err = NumericLimits<Real>::log_zero_; int32_cuda index_error = i * dim_error.stride + j; for(int s = 0; s < dim_alpha.cols; s++) { if (labels[s] == j) { // int32_cuda index_alpha = i * dim_alpha.stride + s; err = LogAPlusB(err, AddAB(mat_alpha[index_alpha], mat_beta[index_alpha])); } } Real val = ExpA(SubAB(err, AddAB(pzx, mat_prob[index_error] == 0? NumericLimits<Real>::log_zero_ : 2*log(mat_prob[index_error])))); mat_error[index_error] = -1.0 * val; } } // mat_prob are in probability scale. template<typename Real> __global__ static void _compute_ctc_error_multiple_sequence(Real* mat_error, int32_cuda sequence_num, MatrixDim dim_error, const Real* mat_alpha, const Real* mat_beta, MatrixDim dim_alpha, const Real* mat_prob, const int32_cuda* labels, int32_cuda dim_label_stride, const int32_cuda* seq_lengths, const Real* pzx) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i >= dim_error.rows || j >= dim_error.cols) return; int32_cuda seqX = i % sequence_num; int32_cuda rowX = i / sequence_num; if (rowX >= seq_lengths[seqX]) return; Real err = NumericLimits<Real>::log_zero_; int32_cuda index_error = i * dim_error.stride + j; for(int s = 0; s < dim_alpha.cols; s++) { int32_cuda index_label = s + seqX * dim_label_stride; if (labels[index_label] == -1) {continue;} if (labels[index_label] == j) { // int32_cuda index_alpha = i * dim_alpha.stride + s; err = LogAPlusB(err, AddAB(mat_alpha[index_alpha], mat_beta[index_alpha])); } } Real val = ExpA(SubAB(err, AddAB(pzx[seqX], mat_prob[index_error] == 0? NumericLimits<Real>::log_zero_ : 2*log(mat_prob[index_error])))); mat_error[index_error] = -1.0 * val; } template<typename Real> __global__ static void _distribute_prob_by_label(Real* mat_prob_dist, MatrixDim dim_prob_dist, const Real* mat_prob, MatrixDim dim_prob, const int32_cuda* labels) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i < dim_prob_dist.rows && j < dim_prob_dist.cols) { int32_cuda index_prob_dist = i * dim_prob_dist.stride + j; int32_cuda index_prob = i * dim_prob.stride + labels[j]; mat_prob_dist[index_prob_dist] = mat_prob[index_prob]; } } // directly get the errors for the prior-softmax values template<typename Real> __global__ static void _compute_ctc_error_one_sequence_rescale(Real* mat_error, MatrixDim dim_error, const Real* mat_alpha, const Real* mat_beta, MatrixDim dim_alpha, const Real* mat_prob, const int32_cuda* labels, const Real* zt) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; // row index int32_cuda j = blockIdx.y * blockDim.y + threadIdx.y; // column index if (i < dim_error.rows && j < dim_error.cols) { Real err = 0; int32_cuda index_error = i * dim_error.stride + j; for(int s = 0; s < dim_alpha.cols; s++) { if (labels[s] == j) { // int32_cuda index_alpha = i * dim_alpha.stride + s; err += mat_alpha[index_alpha] * mat_beta[index_alpha]; } } if (mat_prob[index_error] == 0 || zt[i] == 0) { mat_error[index_error] = 0; } else { mat_error[index_error] = mat_prob[index_error] - (err / zt[i]) / mat_prob[index_error]; } } } void cudaF_compute_ctc_alpha(dim3 Gr, dim3 Bl, float *alpha, int row_idx, MatrixDim dim_alpha, const float *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_alpha_one_sequence<<<Gr, Bl>>>(alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaF_compute_ctc_beta(dim3 Gr, dim3 Bl, float *beta, int row_idx, MatrixDim dim_beta, const float *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_beta_one_sequence<<<Gr, Bl>>>(beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaF_compute_ctc_error(dim3 Gr, dim3 Bl, float *error, MatrixDim dim_error, const float *alpha, const float *beta, MatrixDim dim_alpha, const float *prob, const int *labels, float pzx) { _compute_ctc_error_one_sequence<<<Gr, Bl>>>(error, dim_error, alpha, beta, dim_alpha, prob, labels, pzx); } void cudaF_compute_ctc_alpha_rescale(dim3 Gr, dim3 Bl, float *alpha, int row_idx, MatrixDim dim_alpha, const float *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_alpha_one_sequence_rescale<<<Gr, Bl>>>(alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaF_compute_ctc_beta_rescale(dim3 Gr, dim3 Bl, float *beta, int row_idx, MatrixDim dim_beta, const float *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_beta_one_sequence_rescale<<<Gr, Bl>>>(beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaF_compute_ctc_error_rescale(dim3 Gr, dim3 Bl, float *error, MatrixDim dim_error, const float *alpha, const float *beta, MatrixDim dim_alpha, const float *prob, const int *labels, const float *zt) { _compute_ctc_error_one_sequence_rescale<<<Gr, Bl>>>(error, dim_error, alpha, beta, dim_alpha, prob, labels, zt); } void cudaF_distribute_prob_by_label(dim3 Gr, dim3 Bl, float *prob_dist, MatrixDim dim_prob_dist, const float *prob, MatrixDim dim_prob, const int *labels) { _distribute_prob_by_label<<<Gr, Bl>>>(prob_dist, dim_prob_dist, prob, dim_prob, labels); } void cudaF_compute_ctc_alpha_multiple_sequence(dim3 Gr, dim3 Bl, float *alpha, int seq_num, int row_idx, MatrixDim dim_alpha, const float *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths) { _compute_ctc_alpha_multiple_sequence<<<Gr, Bl>>>(alpha, seq_num, row_idx, dim_alpha, prob, dim_prob, labels, dim_label_stride, seq_lengths); } void cudaF_compute_ctc_beta_multiple_sequence(dim3 Gr, dim3 Bl, float *beta, int seq_num, int row_idx, MatrixDim dim_beta, const float *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths, const int *label_lengths) { _compute_ctc_beta_multiple_sequence<<<Gr, Bl>>>(beta, seq_num, row_idx, dim_beta, prob, dim_prob, labels, dim_label_stride, seq_lengths, label_lengths); } void cudaF_compute_ctc_error_multiple_sequence(dim3 Gr, dim3 Bl, float *error, int seq_num, MatrixDim dim_error, const float *alpha, const float *beta, MatrixDim dim_alpha, const float *prob, const int *labels, int dim_label_stride, const int *seq_lengths, const float *pzx) { _compute_ctc_error_multiple_sequence<<<Gr, Bl>>>(error, seq_num, dim_error, alpha, beta, dim_alpha, prob, labels, dim_label_stride, seq_lengths, pzx); } void cudaD_compute_ctc_alpha(dim3 Gr, dim3 Bl, double *alpha, int row_idx, MatrixDim dim_alpha, const double *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_alpha_one_sequence<<<Gr, Bl>>>(alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaD_compute_ctc_beta(dim3 Gr, dim3 Bl, double *beta, int row_idx, MatrixDim dim_beta, const double *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_beta_one_sequence<<<Gr, Bl>>>(beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaD_compute_ctc_error(dim3 Gr, dim3 Bl, double *error, MatrixDim dim_error, const double *alpha, const double *beta, MatrixDim dim_alpha, const double *prob, const int *labels, double pzx) { _compute_ctc_error_one_sequence<<<Gr, Bl>>>(error, dim_error, alpha, beta, dim_alpha, prob, labels, pzx); } void cudaD_compute_ctc_alpha_rescale(dim3 Gr, dim3 Bl, double *alpha, int row_idx, MatrixDim dim_alpha, const double *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_alpha_one_sequence_rescale<<<Gr, Bl>>>(alpha, row_idx, dim_alpha, prob, dim_prob, labels); } void cudaD_compute_ctc_beta_rescale(dim3 Gr, dim3 Bl, double *beta, int row_idx, MatrixDim dim_beta, const double *prob, MatrixDim dim_prob, const int *labels) { _compute_ctc_beta_one_sequence_rescale<<<Gr, Bl>>>(beta, row_idx, dim_beta, prob, dim_prob, labels); } void cudaD_compute_ctc_error_rescale(dim3 Gr, dim3 Bl, double *error, MatrixDim dim_error, const double *alpha, const double *beta, MatrixDim dim_alpha, const double *prob, const int *labels, const double *zt) { _compute_ctc_error_one_sequence_rescale<<<Gr, Bl>>>(error, dim_error, alpha, beta, dim_alpha, prob, labels, zt); } void cudaD_distribute_prob_by_label(dim3 Gr, dim3 Bl, double *prob_dist, MatrixDim dim_prob_dist, const double *prob, MatrixDim dim_prob, const int *labels) { _distribute_prob_by_label<<<Gr, Bl>>>(prob_dist, dim_prob_dist, prob, dim_prob, labels); } void cudaD_compute_ctc_alpha_multiple_sequence(dim3 Gr, dim3 Bl, double *alpha, int seq_num, int row_idx, MatrixDim dim_alpha, const double *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths) { _compute_ctc_alpha_multiple_sequence<<<Gr, Bl>>>(alpha, seq_num, row_idx, dim_alpha, prob, dim_prob, labels, dim_label_stride, seq_lengths); } void cudaD_compute_ctc_beta_multiple_sequence(dim3 Gr, dim3 Bl, double *beta, int seq_num, int row_idx, MatrixDim dim_beta, const double *prob, MatrixDim dim_prob, const int *labels, int dim_label_stride, const int *seq_lengths, const int *label_lengths) { _compute_ctc_beta_multiple_sequence<<<Gr, Bl>>>(beta, seq_num, row_idx, dim_beta, prob, dim_prob, labels, dim_label_stride, seq_lengths, label_lengths); } void cudaD_compute_ctc_error_multiple_sequence(dim3 Gr, dim3 Bl, double *error, int seq_num, MatrixDim dim_error, const double *alpha, const double *beta, MatrixDim dim_alpha, const double *prob, const int *labels, int dim_label_stride, const int *seq_lengths, const double *pzx) { _compute_ctc_error_multiple_sequence<<<Gr, Bl>>>(error, seq_num, dim_error, alpha, beta, dim_alpha, prob, labels, dim_label_stride, seq_lengths, pzx); } ////////////////////////////////////////////////////// //// FSMN kernel functions /////// //////////////////////////////////////////////////// template<typename Real> __global__ static void _gen_memory(Real* out, const Real* in, const Real *l_filter, const Real *r_filter, float *flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if(i < d.cols*d.rows) { int row = i/d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = 0; order < l_order; order++) { shift_index = row - order*l_stride; if (shift_index >= 0 && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } for (int order = 1; order <= r_order; order++) { shift_index = row + order*r_stride; if (shift_index < d.rows && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * r_filter[(order - 1)*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _gen_memory_online(Real* out, const Real* in, int start, const Real *l_filter, const Real *r_filter, const int32_cuda* l_valid_frames, const int32_cuda* r_valid_frames, const int32_cuda* stream_state_flag, MatrixDim d_out, MatrixDim d_in, int l_order, int r_order, int l_stride, int r_stride, int nstream) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if(i < d_out.cols*d_out.rows) { int batch_size = d_out.rows/nstream, buffer_size = d_in.rows/nstream; int r_his = r_order*r_stride, stride = d_out.stride; int curt_start, num_valid_frames, l_b, r_b, shift_index; int row = i/d_out.cols; int col = i%d_out.cols; int s = row/batch_size, r = row%batch_size, c = col; curt_start = stream_state_flag[s]==0 ? start+r_his : start; r_b = start + r_his + r_valid_frames[s]; if (stream_state_flag[s] == 0) { num_valid_frames = r_valid_frames[s]-r_his; l_b = start + r_his; } else if (stream_state_flag[s] == 1) { num_valid_frames = r_valid_frames[s]; l_b = 0; } else if (stream_state_flag[s] == 2) { num_valid_frames = r_valid_frames[s]+r_his; l_b = 0; } if (r < num_valid_frames) { int idx = (s*buffer_size+curt_start+r)*stride + c; int odx = (s*batch_size+r)*stride + c; out[odx] = in[idx]; Real value = 0.0; /// left history for (int order = 0; order < l_order; order++) { shift_index = r-order*l_stride+curt_start; if (shift_index >= l_b) value += in[(s*buffer_size+shift_index)*stride + c] * l_filter[order*stride + c]; } /// right history for (int order = 1; order < r_order + 1; order++) { shift_index = r+order*r_stride+curt_start; if (shift_index < r_b) value += in[(s*buffer_size+shift_index)*stride + c] * r_filter[(order - 1)*stride + c]; } out[odx] += value; } } } template<typename Real> __global__ static void _memory_err_back(Real* out, const Real* in, const Real *l_filter, const Real *r_filter, float *flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < d.cols*d.rows) { int row = i/d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = -r_order; order < 0; order++) { shift_index = row + order*r_stride; if (shift_index >= 0 && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * r_filter[(-order - 1)*d.stride + col]; } } for (int order = 0; order < l_order; order++) { shift_index = row + order*l_stride; if (shift_index < d.rows && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _gen_uni_memory(Real* out, const Real* in, const Real *l_filter, float *flags, MatrixDim d, int l_order, int l_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < d.cols*d.rows) { int row = i / d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = 0; order < l_order; order++) { shift_index = row - order*l_stride; if (shift_index >= 0 && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _uni_memory_err_back(Real* out, const Real* in, const Real *l_filter, float *flags, MatrixDim d, int l_order, int l_stride) { int32_cuda i = blockIdx.x * blockDim.x + threadIdx.x; if (i < d.cols*d.rows) { int row = i / d.cols; int col = i%d.cols; Real value = 0.0; int shift_index = 0; int index = row*d.stride + col; out[index] = in[index]; for (int order = 0; order < l_order; order++) { shift_index = row + order*l_stride; if (shift_index < d.rows && flags[shift_index] == flags[row]) { value += in[shift_index*d.stride + col] * l_filter[order*d.stride + col]; } } out[index] += value; } } template<typename Real> __global__ static void _get_l_filter_err(Real* out, const Real* diff, const Real* in, float *flags, MatrixDim d, int l_order, int l_stride, float lr) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.cols*l_order) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.rows - 1)/THREADS + 1; int order = j/d.cols; int col = j%d.cols; int shift = order * l_stride; int index = order*d.stride + col; //copy input to aux int row = threadIdx.x - shift; if (steps > 1) { if (row >= 0 && abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[col + row*d.stride] * diff[col + threadIdx.x*d.stride]; } else { aux[threadIdx.x] = 0; } __syncthreads(); for (int i = 1; i<steps; ++i) { int index = threadIdx.x + i*THREADS; if (index < d.rows && abs(flags[index] - flags[index - shift])<1e-2) aux[threadIdx.x] += in[(index - shift)*d.stride + col] * diff[index*d.stride + col]; } __syncthreads(); } else { if (row >= 0 && threadIdx.x<d.rows && abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[col + row*d.stride] * diff[col + threadIdx.x*d.stride]; } else { aux[threadIdx.x] = 0; } __syncthreads(); } int nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if (threadIdx.x + halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); // divide by two. } Real sum = aux[0]; __syncthreads(); //out[index] = out[index]-lr*sum; out[index] = lr*sum; } template<typename Real> __global__ static void _get_r_filter_err(Real* out, const Real* diff, const Real* in, float *flags, MatrixDim d, int r_order, int r_stride, float lr) { int j = blockIdx.x; int THREADS = blockDim.x; if (j >= d.cols*r_order) return; __shared__ Real aux[CU1DBLOCK]; int steps = (d.rows - 1) / THREADS + 1; int order = j/d.cols; int col = j%d.cols; int shift = (order + 1) * r_stride; int index = order*d.stride + col; //copy input to aux int row = threadIdx.x + shift; if (steps > 1) { if (row <d.rows && abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[row*d.stride + col] * diff[threadIdx.x *d.stride + col]; } else { aux[threadIdx.x] = 0; } __syncthreads(); for (int i = 1; i<steps; ++i) { int index = threadIdx.x + i*THREADS; if (index + shift < d.rows && abs(flags[index] - flags[index + shift])<1e-2) aux[threadIdx.x] += in[(index + shift)*d.stride + col] * diff[index*d.stride + col]; } __syncthreads(); } else { if (row <d.rows &&threadIdx.x<d.rows&& abs(flags[threadIdx.x] - flags[row])<1e-2) { aux[threadIdx.x] = in[row*d.stride + col] * diff[threadIdx.x *d.stride + col]; } else { aux[threadIdx.x] = 0; } __syncthreads(); } int nTotalThreads = THREADS; __syncthreads(); while (nTotalThreads > 1) { int halfPoint = ((1 + nTotalThreads) >> 1); // divide by two // only the first half of the threads will be active. if (threadIdx.x < halfPoint) { // Get the shared value stored by another thread if (threadIdx.x + halfPoint < nTotalThreads) aux[threadIdx.x] += aux[threadIdx.x + halfPoint]; } __syncthreads(); nTotalThreads = ((1 + nTotalThreads) >> 1); // divide by two. } Real sum = aux[0]; __syncthreads(); //out[index] = out[index]-lr*sum; out[index] = lr*sum; } void cudaF_gen_memory(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, const float* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { _gen_memory<<<Gr, Bl >>>(mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaD_gen_memory(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, const double* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { _gen_memory <<<Gr, Bl >> >(mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaF_memory_err_back(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, const float* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { _memory_err_back<<<Gr, Bl >>>(mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaD_memory_err_back(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, const double* r_filter, float* flags, MatrixDim d, int l_order, int r_order, int l_stride, int r_stride) { _memory_err_back<<<Gr, Bl >>>(mat_out, mat_in, l_filter, r_filter, flags, d, l_order, r_order, l_stride, r_stride); } void cudaF_gen_uni_memory(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _gen_uni_memory << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaD_gen_uni_memory(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _gen_uni_memory << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaF_uni_memory_err_back(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, const float *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _uni_memory_err_back << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaD_uni_memory_err_back(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, const double *l_filter, float* flags, MatrixDim d, int l_order, int l_stride) { _uni_memory_err_back << <Gr, Bl >> >(mat_out, mat_in, l_filter, flags, d, l_order, l_stride); } void cudaF_get_l_filter_err(dim3 Gr, dim3 Bl, float *mat_out, const float *diff, const float* mat_in, float* flags, MatrixDim d, int l_order, int l_stride, float lr) { _get_l_filter_err <<<Gr, Bl >>>(mat_out, diff, mat_in, flags, d, l_order, l_stride, lr); } void cudaD_get_l_filter_err(dim3 Gr, dim3 Bl, double *mat_out, const double *diff, const double* mat_in, float* flags, MatrixDim d, int l_order, int l_stride, float lr) { _get_l_filter_err <<<Gr, Bl >>>(mat_out, diff, mat_in, flags, d, l_order, l_stride, lr); } void cudaF_get_r_filter_err(dim3 Gr, dim3 Bl, float *mat_out, const float *diff, const float* mat_in, float* flags, MatrixDim d, int r_order, int r_stride, float lr) { _get_r_filter_err <<<Gr, Bl >>>(mat_out, diff, mat_in, flags, d, r_order, r_stride, lr); } void cudaD_get_r_filter_err(dim3 Gr, dim3 Bl, double *mat_out, const double *diff, const double* mat_in, float* flags, MatrixDim d, int r_order, int r_stride, float lr) { _get_r_filter_err <<<Gr, Bl >>>(mat_out, diff, mat_in, flags, d, r_order, r_stride, lr); } void cudaF_gen_memory_online(dim3 Gr, dim3 Bl, float *mat_out, const float* mat_in, int start, const float *l_filter, const float* r_filter, const int *l_valid, const int *r_valid, const int *stream_state_flag, MatrixDim d_out, MatrixDim d_in, int l_order, int r_order, int l_stride, int r_stride, int nstream) { _gen_memory_online <<<Gr, Bl >>>(mat_out, mat_in, start, l_filter, r_filter, l_valid, r_valid, stream_state_flag, d_out, d_in, l_order, r_order, l_stride, r_stride, nstream); } void cudaD_gen_memory_online(dim3 Gr, dim3 Bl, double *mat_out, const double* mat_in, int start, const double *l_filter, const double* r_filter, const int *l_valid, const int *r_valid, const int *stream_state_flag, MatrixDim d_out, MatrixDim d_in, int l_order, int r_order, int l_stride, int r_stride, int nstream) { _gen_memory_online <<<Gr, Bl >>>(mat_out, mat_in, start, l_filter, r_filter, l_valid, r_valid, stream_state_flag, d_out, d_in, l_order, r_order, l_stride, r_stride, nstream); } // Launches a kernel that does nothing, explicitly using the legacy default stream; // this will synchronize all threads without blocking. void cuda_legacy_noop() { _noop_kernel<<<1, 1, 0, cudaStreamLegacy>>>(); }
e9e55c64f8fae8a2a80add2be7575cd7ff46ec13.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // ------------------------------------------------------------- // cuDPP -- CUDA Data Parallel Primitives library // ------------------------------------------------------------- // $Revision: 3049 $ // $Date: 2007-02-26 10:42:36 -0800 (Mon, 26 Feb 2007) $ // ------------------------------------------------------------- // This source code is distributed under the terms of license.txt in // the root directory of this source distribution. // ------------------------------------------------------------- /** * @file * compact_app.cu * * @brief CUDPP application-level compact routines */ #include "cudpp_globals.h" #include "cudpp_util.h" #include "cudpp.h" #include "cudpp_plan.h" #include "cudpp_scan.h" #include "kernel/compact_kernel.cu" #include <cutil.h> #include <cstdlib> #include <cstdio> #include <assert.h> /** \addtogroup cudpp_app * @{ */ /** @name Compact Functions * @{ */ /** @brief Calculate launch parameters for compactArray(). * * Calculates the block size and number of blocks from the total * number of elements and the maximum threads per block. Called by * compactArray(). * * The calculation is pretty straightforward - the number of blocks * is calculated by dividing the number of input elements by the product * of the number of threads in each CTA and the number of elements each thread * will process. numThreads and numEltsPerBlock are also simple to * calculate. Please note that in cases where numElements is not an exact * multiple of SCAN_ELTS_PER_THREAD * CTA_SIZE we would have threads * which do nothing or have a thread which will process less than * SCAN_ELTS_PER_THREAD elements. * * * @param[in] numElements Number of elements to sort * @param[out] numThreads Number of threads in each block * @param[out] numBlocks Number of blocks * @param[out] numEltsPerBlock Number of elements processed per block * */ void calculatCompactLaunchParams(const unsigned int numElements, unsigned int &numThreads, unsigned int &numBlocks, unsigned int &numEltsPerBlock) { numBlocks = max(1, (int)ceil((float)numElements / ((float)SCAN_ELTS_PER_THREAD * CTA_SIZE))); if (numBlocks > 1) { numThreads = CTA_SIZE; } else { numThreads = (unsigned int)ceil((float)numElements / (float)SCAN_ELTS_PER_THREAD); } numEltsPerBlock = numThreads * SCAN_ELTS_PER_THREAD; } /** @brief Compact the non-zero elements of an array. * * Given an input array \a d_in, compactArray() outputs a compacted version * which does not have null (zero) elements. Also ouputs the number of non-zero * elements in the compacted array. Called by ::cudppCompactDispatch(). * * The algorithm is straightforward, involving two steps (most of the * complexity is hidden in scan, invoked with cudppScanDispatch() ). * * -# scanArray() performs a prefix sum on \a d_isValid to compute output * indices. * -# compactData() takes \a d_in and an intermediate array of output indices * as input and writes the values with valid flags in \a d_isValid into * \a d_out using the output indices. * * @param[out] d_out Array of compacted non-null elements * @param[out] d_numValidElements Pointer to unsigned int to store number of * non-null elements * @param[in] d_in Input array * @param[out] d_isValid Array of flags, 1 for each non-null element, 0 * for each null element. Same length as \a d_in * @param[in] numElements Number of elements in input array * @param[in] plan Pointer to the plan object used for this compact * */ template<class T> void compactArray(T *d_out, size_t *d_numValidElements, const T *d_in, const unsigned int *d_isValid, size_t numElements, const CUDPPCompactPlan *plan) { unsigned int numThreads = 0; unsigned int numBlocks = 0; unsigned int numEltsPerBlock = 0; // Calculate CUDA launch parameters - number of blocks, number of threads // @todo What is numEltsPerBlock doing here? calculatCompactLaunchParams(numElements, numThreads, numBlocks, numEltsPerBlock); // Run prefix sum on isValid array to find the addresses in the compacted // output array where each non-null element of d_in will go to cudppScanDispatch((void*)plan->m_d_outputIndices, (void*)d_isValid, numElements, 1, plan->m_scanPlan); // For every non-null element in d_in write it to its proper place in the // d_out. This is indicated by the corresponding element in isValid array if (plan->m_config.options & CUDPP_OPTION_INDEX) { if (plan->m_config.options & CUDPP_OPTION_BACKWARD) hipLaunchKernelGGL(( compactDataIdx<T, true>), dim3(numBlocks), dim3(numThreads), 0, 0, d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); else hipLaunchKernelGGL(( compactDataIdx<T, false>), dim3(numBlocks), dim3(numThreads), 0, 0, d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); } else { if (plan->m_config.options & CUDPP_OPTION_BACKWARD) hipLaunchKernelGGL(( compactData<T, true>), dim3(numBlocks), dim3(numThreads), 0, 0, d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); else hipLaunchKernelGGL(( compactData<T, false>), dim3(numBlocks), dim3(numThreads), 0, 0, d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); } CUT_CHECK_ERROR("compactArray -- compactData"); } #ifdef __cplusplus extern "C" { #endif /** @brief Allocate intermediate arrays used by cudppCompact(). * * In addition to the internal CUDPPScanPlan contained in CUDPPCompactPlan, * CUDPPCompact also needs a temporary device array of output indices, which * is allocated by this function. * * @param plan Pointer to CUDPPCompactPlan object within which intermediate * storage is allocated. */ void allocCompactStorage(CUDPPCompactPlan *plan) { CUDA_SAFE_CALL( hipMalloc((void**)&plan->m_d_outputIndices, sizeof(unsigned int) * plan->m_numElements) ); } /** @brief Deallocate intermediate storage used by cudppCompact(). * * Deallocates the output indices array allocated by allocCompactStorage(). * * @param plan Pointer to CUDPPCompactPlan object initialized by allocCompactStorage(). */ void freeCompactStorage(CUDPPCompactPlan *plan) { CUDA_SAFE_CALL( hipFree(plan->m_d_outputIndices)); } /** @brief Dispatch compactArray for the specified datatype. * * A thin wrapper on top of compactArray which calls compactArray() for the data type * specified in \a config. This is the app-level interface to compact used by * cudppCompact(). * * @param[out] d_out Compacted array of non-zero elements * @param[out] d_numValidElements Pointer to an unsigned int to store the * number of non-zero elements * @param[in] d_in Input array * @param[in] d_isValid Array of boolean valid flags with same length as * \a d_in * @param[in] numElements Number of elements to compact * @param[in] plan Pointer to plan object for this compact */ void cudppCompactDispatch(void *d_out, size_t *d_numValidElements, const void *d_in, const unsigned int *d_isValid, size_t numElements, const CUDPPCompactPlan *plan) { switch (plan->m_config.datatype) { case CUDPP_CHAR: compactArray<char>((char*)d_out, d_numValidElements, (const char*)d_in, d_isValid, numElements, plan); break; case CUDPP_UCHAR: compactArray<unsigned char>((unsigned char*)d_out, d_numValidElements, (const unsigned char*)d_in, d_isValid, numElements, plan); break; case CUDPP_INT: compactArray<int>((int*)d_out, d_numValidElements, (const int*)d_in, d_isValid, numElements, plan); break; case CUDPP_UINT: compactArray<unsigned int>((unsigned int*)d_out, d_numValidElements, (const unsigned int*)d_in, d_isValid, numElements, plan); break; case CUDPP_FLOAT: compactArray<float>((float*)d_out, d_numValidElements, (const float*)d_in, d_isValid, numElements, plan); break; default: break; } } #ifdef __cplusplus } #endif /** @} */ // end compact functions /** @} */ // end cudpp_app
e9e55c64f8fae8a2a80add2be7575cd7ff46ec13.cu
// ------------------------------------------------------------- // cuDPP -- CUDA Data Parallel Primitives library // ------------------------------------------------------------- // $Revision: 3049 $ // $Date: 2007-02-26 10:42:36 -0800 (Mon, 26 Feb 2007) $ // ------------------------------------------------------------- // This source code is distributed under the terms of license.txt in // the root directory of this source distribution. // ------------------------------------------------------------- /** * @file * compact_app.cu * * @brief CUDPP application-level compact routines */ #include "cudpp_globals.h" #include "cudpp_util.h" #include "cudpp.h" #include "cudpp_plan.h" #include "cudpp_scan.h" #include "kernel/compact_kernel.cu" #include <cutil.h> #include <cstdlib> #include <cstdio> #include <assert.h> /** \addtogroup cudpp_app * @{ */ /** @name Compact Functions * @{ */ /** @brief Calculate launch parameters for compactArray(). * * Calculates the block size and number of blocks from the total * number of elements and the maximum threads per block. Called by * compactArray(). * * The calculation is pretty straightforward - the number of blocks * is calculated by dividing the number of input elements by the product * of the number of threads in each CTA and the number of elements each thread * will process. numThreads and numEltsPerBlock are also simple to * calculate. Please note that in cases where numElements is not an exact * multiple of SCAN_ELTS_PER_THREAD * CTA_SIZE we would have threads * which do nothing or have a thread which will process less than * SCAN_ELTS_PER_THREAD elements. * * * @param[in] numElements Number of elements to sort * @param[out] numThreads Number of threads in each block * @param[out] numBlocks Number of blocks * @param[out] numEltsPerBlock Number of elements processed per block * */ void calculatCompactLaunchParams(const unsigned int numElements, unsigned int &numThreads, unsigned int &numBlocks, unsigned int &numEltsPerBlock) { numBlocks = max(1, (int)ceil((float)numElements / ((float)SCAN_ELTS_PER_THREAD * CTA_SIZE))); if (numBlocks > 1) { numThreads = CTA_SIZE; } else { numThreads = (unsigned int)ceil((float)numElements / (float)SCAN_ELTS_PER_THREAD); } numEltsPerBlock = numThreads * SCAN_ELTS_PER_THREAD; } /** @brief Compact the non-zero elements of an array. * * Given an input array \a d_in, compactArray() outputs a compacted version * which does not have null (zero) elements. Also ouputs the number of non-zero * elements in the compacted array. Called by ::cudppCompactDispatch(). * * The algorithm is straightforward, involving two steps (most of the * complexity is hidden in scan, invoked with cudppScanDispatch() ). * * -# scanArray() performs a prefix sum on \a d_isValid to compute output * indices. * -# compactData() takes \a d_in and an intermediate array of output indices * as input and writes the values with valid flags in \a d_isValid into * \a d_out using the output indices. * * @param[out] d_out Array of compacted non-null elements * @param[out] d_numValidElements Pointer to unsigned int to store number of * non-null elements * @param[in] d_in Input array * @param[out] d_isValid Array of flags, 1 for each non-null element, 0 * for each null element. Same length as \a d_in * @param[in] numElements Number of elements in input array * @param[in] plan Pointer to the plan object used for this compact * */ template<class T> void compactArray(T *d_out, size_t *d_numValidElements, const T *d_in, const unsigned int *d_isValid, size_t numElements, const CUDPPCompactPlan *plan) { unsigned int numThreads = 0; unsigned int numBlocks = 0; unsigned int numEltsPerBlock = 0; // Calculate CUDA launch parameters - number of blocks, number of threads // @todo What is numEltsPerBlock doing here? calculatCompactLaunchParams(numElements, numThreads, numBlocks, numEltsPerBlock); // Run prefix sum on isValid array to find the addresses in the compacted // output array where each non-null element of d_in will go to cudppScanDispatch((void*)plan->m_d_outputIndices, (void*)d_isValid, numElements, 1, plan->m_scanPlan); // For every non-null element in d_in write it to its proper place in the // d_out. This is indicated by the corresponding element in isValid array if (plan->m_config.options & CUDPP_OPTION_INDEX) { if (plan->m_config.options & CUDPP_OPTION_BACKWARD) compactDataIdx<T, true><<<numBlocks, numThreads>>>(d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); else compactDataIdx<T, false><<<numBlocks, numThreads>>>(d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); } else { if (plan->m_config.options & CUDPP_OPTION_BACKWARD) compactData<T, true><<<numBlocks, numThreads>>>(d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); else compactData<T, false><<<numBlocks, numThreads>>>(d_out, d_numValidElements, plan->m_d_outputIndices, d_isValid, d_in, numElements); } CUT_CHECK_ERROR("compactArray -- compactData"); } #ifdef __cplusplus extern "C" { #endif /** @brief Allocate intermediate arrays used by cudppCompact(). * * In addition to the internal CUDPPScanPlan contained in CUDPPCompactPlan, * CUDPPCompact also needs a temporary device array of output indices, which * is allocated by this function. * * @param plan Pointer to CUDPPCompactPlan object within which intermediate * storage is allocated. */ void allocCompactStorage(CUDPPCompactPlan *plan) { CUDA_SAFE_CALL( cudaMalloc((void**)&plan->m_d_outputIndices, sizeof(unsigned int) * plan->m_numElements) ); } /** @brief Deallocate intermediate storage used by cudppCompact(). * * Deallocates the output indices array allocated by allocCompactStorage(). * * @param plan Pointer to CUDPPCompactPlan object initialized by allocCompactStorage(). */ void freeCompactStorage(CUDPPCompactPlan *plan) { CUDA_SAFE_CALL( cudaFree(plan->m_d_outputIndices)); } /** @brief Dispatch compactArray for the specified datatype. * * A thin wrapper on top of compactArray which calls compactArray() for the data type * specified in \a config. This is the app-level interface to compact used by * cudppCompact(). * * @param[out] d_out Compacted array of non-zero elements * @param[out] d_numValidElements Pointer to an unsigned int to store the * number of non-zero elements * @param[in] d_in Input array * @param[in] d_isValid Array of boolean valid flags with same length as * \a d_in * @param[in] numElements Number of elements to compact * @param[in] plan Pointer to plan object for this compact */ void cudppCompactDispatch(void *d_out, size_t *d_numValidElements, const void *d_in, const unsigned int *d_isValid, size_t numElements, const CUDPPCompactPlan *plan) { switch (plan->m_config.datatype) { case CUDPP_CHAR: compactArray<char>((char*)d_out, d_numValidElements, (const char*)d_in, d_isValid, numElements, plan); break; case CUDPP_UCHAR: compactArray<unsigned char>((unsigned char*)d_out, d_numValidElements, (const unsigned char*)d_in, d_isValid, numElements, plan); break; case CUDPP_INT: compactArray<int>((int*)d_out, d_numValidElements, (const int*)d_in, d_isValid, numElements, plan); break; case CUDPP_UINT: compactArray<unsigned int>((unsigned int*)d_out, d_numValidElements, (const unsigned int*)d_in, d_isValid, numElements, plan); break; case CUDPP_FLOAT: compactArray<float>((float*)d_out, d_numValidElements, (const float*)d_in, d_isValid, numElements, plan); break; default: break; } } #ifdef __cplusplus } #endif /** @} */ // end compact functions /** @} */ // end cudpp_app
5fa1c2c5c3d2b48895b729a50f37a110a153b7f0.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" __global__ void aproximarPi( float *x, float *y, int *z, int tam) { int i = threadIdx.x + blockIdx.x*blockDim.x; // 0 - 2047 int j = threadIdx.y + blockIdx.y*blockDim.y; // 0 - 2047 int index = j + i*tam; // 0 - 4194303 if( (x[index] * x[index] + y[index] * y[index]) <= 1.0f){ atomicAdd(z, 1); } }
5fa1c2c5c3d2b48895b729a50f37a110a153b7f0.cu
#include "includes.h" __global__ void aproximarPi( float *x, float *y, int *z, int tam) { int i = threadIdx.x + blockIdx.x*blockDim.x; // 0 - 2047 int j = threadIdx.y + blockIdx.y*blockDim.y; // 0 - 2047 int index = j + i*tam; // 0 - 4194303 if( (x[index] * x[index] + y[index] * y[index]) <= 1.0f){ atomicAdd(z, 1); } }
022498ea58a89a361eb496161c3b012872c03fb3.hip
// !!! This is a file automatically generated by hipify!!! #ifndef SIZE #define SIZE 64 #endif #ifndef REPETITIONS #define REPETITIONS 5 #endif #include <iostream> #include "ChunkTesting.cuh" float getSum(float* array, unsigned int size) { float sum = 0; for (unsigned int i = 0 ; i < size ; ++i ) { sum += array[i]; } return sum; } float getMax(float* array, unsigned int size) { float max = 0; for (unsigned int i = 0 ; i < size ; ++i) { if (array[i] > max) { max = array[i]; } } return max; } float getMin(float* array, unsigned int size) { float min = array[0]; for (unsigned int i = 0 ; i < size ; ++i) { if (array[i] < min) { min = array[i]; } } return min; } float performTest(unsigned int mults) { float results[REPETITIONS] = {0}; for (unsigned int i = 0 ; i < REPETITIONS ; ++i) { results[i] = testChunksMultiply<SIZE>(mults); } if (REPETITIONS >= 3) { float choppedSum = getSum(results, REPETITIONS) - getMax(results, REPETITIONS) - getMin(results, REPETITIONS); return choppedSum/(REPETITIONS - 2); } return 0; } int main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage: " << argv[0] << " <log of minimal multiplications> <log of maximal multiplications>" << std::endl; } int minimalMults = atoi(argv[1]); int maximalMults = atoi(argv[2]); hipSetDevice(0); for (unsigned int i = minimalMults ; i <= maximalMults ; ++i) { std::cout << performTest((1<<i)) << std::endl; } }
022498ea58a89a361eb496161c3b012872c03fb3.cu
#ifndef SIZE #define SIZE 64 #endif #ifndef REPETITIONS #define REPETITIONS 5 #endif #include <iostream> #include "ChunkTesting.cuh" float getSum(float* array, unsigned int size) { float sum = 0; for (unsigned int i = 0 ; i < size ; ++i ) { sum += array[i]; } return sum; } float getMax(float* array, unsigned int size) { float max = 0; for (unsigned int i = 0 ; i < size ; ++i) { if (array[i] > max) { max = array[i]; } } return max; } float getMin(float* array, unsigned int size) { float min = array[0]; for (unsigned int i = 0 ; i < size ; ++i) { if (array[i] < min) { min = array[i]; } } return min; } float performTest(unsigned int mults) { float results[REPETITIONS] = {0}; for (unsigned int i = 0 ; i < REPETITIONS ; ++i) { results[i] = testChunksMultiply<SIZE>(mults); } if (REPETITIONS >= 3) { float choppedSum = getSum(results, REPETITIONS) - getMax(results, REPETITIONS) - getMin(results, REPETITIONS); return choppedSum/(REPETITIONS - 2); } return 0; } int main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage: " << argv[0] << " <log of minimal multiplications> <log of maximal multiplications>" << std::endl; } int minimalMults = atoi(argv[1]); int maximalMults = atoi(argv[2]); cudaSetDevice(0); for (unsigned int i = minimalMults ; i <= maximalMults ; ++i) { std::cout << performTest((1<<i)) << std::endl; } }
159e1e249a704e7be2a055bc159b8f34336aca10.hip
// !!! This is a file automatically generated by hipify!!! #include <hip/hip_runtime_api.h> #include <stdint.h> #define OFFSET_BANK(idx) ({ __typeof__ (idx) _idx = idx; ((_idx) + ((_idx) / 32)); }) __global__ void im2col_gpu_kernel( const int n, const float* data_im, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, const int height_col, const int width_col, float* data_col) { int index = threadIdx.x + blockDim.x * blockIdx.x; if (index < n) { const int h_index = index / width_col; const int h_col = h_index % height_col; const int w_col = index % width_col; const int c_im = h_index / height_col; const int c_col = c_im * kernel_h * kernel_w; const int h_offset = h_col * stride_h - pad_h; const int w_offset = w_col * stride_w - pad_w; float* data_col_ptr = data_col; data_col_ptr += (c_col * height_col + h_col) * width_col + w_col; const float* data_im_ptr = data_im; data_im_ptr += (c_im * height + h_offset) * width + w_offset; for (int i = 0; i < kernel_h; ++i) { for (int j = 0; j < kernel_w; ++j) { int h_im = h_offset + i * dilation_h; int w_im = w_offset + j * dilation_w; *data_col_ptr = (h_im >= 0 && w_im >= 0 && h_im < height && w_im < width) ? data_im_ptr[i * dilation_h * width + j * dilation_w] : 0; data_col_ptr += height_col * width_col; } } } } extern "C" void neuralops_cuda_caffe_im2col( const float* data_im, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, float* data_col, hipStream_t stream) { // We are going to launch channels * height_col * width_col kernels, each // kernel responsible for copying a single-channel grid. int height_col = (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1; int width_col = (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1; int num_kernels = channels * height_col * width_col; // NOLINT_NEXT_LINE(whitespace/operators) hipLaunchKernelGGL(( im2col_gpu_kernel), dim3((num_kernels+1024-1)/1024), dim3(1024), 0, stream, num_kernels, data_im, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, height_col, width_col, data_col); } __global__ void col2im_gpu_kernel( const int n, const float* data_col, const int height, const int width, const int channels, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, const int height_col, const int width_col, float* data_im) { int index = threadIdx.x + blockDim.x * blockIdx.x; if (index < n) { float val = 0; const int w_im = index % width + pad_w; const int h_im = (index / width) % height + pad_h; const int c_im = index / (width * height); int kernel_extent_w = (kernel_w - 1) * dilation_w + 1; int kernel_extent_h = (kernel_h - 1) * dilation_h + 1; // compute the start and end of the output const int w_col_start = (w_im < kernel_extent_w) ? 0 : (w_im - kernel_extent_w) / stride_w + 1; const int w_col_end = min(w_im / stride_w + 1, width_col); const int h_col_start = (h_im < kernel_extent_h) ? 0 : (h_im - kernel_extent_h) / stride_h + 1; const int h_col_end = min(h_im / stride_h + 1, height_col); // TODO: use LCM of stride and dilation to avoid unnecessary loops for (int h_col = h_col_start; h_col < h_col_end; h_col += 1) { for (int w_col = w_col_start; w_col < w_col_end; w_col += 1) { int h_k = (h_im - h_col * stride_h); int w_k = (w_im - w_col * stride_w); if (h_k % dilation_h == 0 && w_k % dilation_w == 0) { h_k /= dilation_h; w_k /= dilation_w; int data_col_index = (((c_im * kernel_h + h_k) * kernel_w + w_k) * height_col + h_col) * width_col + w_col; val += data_col[data_col_index]; } } } data_im[index] = val; } } extern "C" void neuralops_cuda_caffe_col2im( const float* data_col, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, float* data_im, hipStream_t stream) { int height_col = (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1; int width_col = (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1; int num_kernels = channels * height * width; // To avoid involving atomic operations, we will launch one kernel per // bottom dimension, and then in the kernel add up the top dimensions. // NOLINT_NEXT_LINE(whitespace/operators) hipLaunchKernelGGL(( col2im_gpu_kernel), dim3((num_kernels+1024-1)/1024), dim3(1024), 0, stream, num_kernels, data_col, height, width, channels, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, height_col, width_col, data_im); } __global__ void conv_diag_affine_fwd_batch_kernel( const float *in_act, int spatial_dim, int num_channels, int batch_size, const float *scale, const float *bias, float *out_act) { int idx = threadIdx.x + blockIdx.x * blockDim.x; int u = idx % spatial_dim; int c = (idx / spatial_dim) % num_channels; int batch_idx = idx / (spatial_dim * num_channels); if (u < spatial_dim && c < num_channels && batch_idx < batch_size) { float gamma = scale[c]; float beta = bias[c]; float y = gamma * in_act[idx] + beta; out_act[idx] = y; } } extern "C" void neuralops_cuda_conv2d_scale_fwd( const float *in_act, size_t spatial_dim, size_t num_channels, size_t batch_size, const float *scale, const float *bias, float *out_act, hipStream_t stream) { int n = spatial_dim * num_channels * batch_size; hipLaunchKernelGGL(( conv_diag_affine_fwd_batch_kernel), dim3((n+1024-1)/1024), dim3(1024), 0, stream, in_act, spatial_dim, num_channels, batch_size, scale, bias, out_act); } __global__ void conv_diag_affine_bwd_batch_kernel( const float *in_act, int spatial_dim, int num_channels, int batch_size, const float *out_delta, const float *scale, float *scale_grad, float *bias_grad, float *in_delta) { __shared__ float scale_grad_cache[1024+32]; __shared__ float bias_grad_cache[1024+32]; int idx = threadIdx.x + blockIdx.x * blockDim.x; int bank_idx = OFFSET_BANK(threadIdx.x); int block_spatial_dim = (spatial_dim+16*32-1)/(16*32); int warp_idx = idx % 32; int c = (idx / 32) % num_channels; int u0 = warp_idx + ((idx / (32 * num_channels)) % block_spatial_dim) * (16*32); int batch_idx = idx / (32 * num_channels * block_spatial_dim); if (c < num_channels && u0 < spatial_dim && batch_idx < batch_size) { float gamma = scale[c]; float d_gamma = 0.0f; float d_beta = 0.0f; int i0 = c * spatial_dim + batch_idx * spatial_dim * num_channels; int u_limit = min(spatial_dim, u0 + 16*32); for (int u = u0; u < u_limit; u += 32) { int i = i0 + u; float dy = out_delta[i]; d_gamma += dy * in_act[i]; d_beta += dy; in_delta[i] = dy * gamma; //in_delta[i] += dy * gamma; //atomicAdd(&in_delta[i], dy * gamma); } scale_grad_cache[bank_idx] = d_gamma; bias_grad_cache[bank_idx] = d_beta; } else { scale_grad_cache[bank_idx] = 0.0f; bias_grad_cache[bank_idx] = 0.0f; } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 2 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+1]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+1]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 4 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+2]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+2]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 8 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+4]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+4]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 16 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+8]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+8]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 32 == 0 && u0 < spatial_dim) { float d_gamma = scale_grad_cache[bank_idx] + scale_grad_cache[bank_idx+16]; atomicAdd(&scale_grad[c], d_gamma); float d_beta = bias_grad_cache[bank_idx] + bias_grad_cache[bank_idx+16]; atomicAdd(&bias_grad[c], d_beta); } } } extern "C" void neuralops_cuda_conv2d_scale_bwd( const float *in_act, size_t spatial_dim, size_t num_channels, size_t batch_size, const float *out_delta, const float *scale, float *scale_grad, float *bias_grad, float *in_delta, hipStream_t stream) { int block_spatial_dim = (spatial_dim+16*32-1)/(16*32); int n = 32 * num_channels * block_spatial_dim * batch_size; hipLaunchKernelGGL(( conv_diag_affine_bwd_batch_kernel), dim3((n+1024-1)/1024), dim3(1024), 0, stream, in_act, spatial_dim, num_channels, batch_size, out_delta, scale, scale_grad, bias_grad, in_delta); } __global__ void conv_scale_rfwd_kernel( const float *in_val, uint32_t spatial_dim, uint32_t num_channels, uint32_t batch_size, const float *in_r_val, const float *scale, const float *scale_r_dir, const float *bias_r_dir, float *out_r_val) { uint32_t idx = threadIdx.x + blockIdx.x * blockDim.x; uint32_t u = idx % spatial_dim; uint32_t c = (idx / spatial_dim) % num_channels; uint32_t batch_idx = idx / (spatial_dim * num_channels); if (u < spatial_dim && c < num_channels && batch_idx < batch_size) { float alpha = scale[c]; float r_alpha = scale_r_dir[c]; float r_beta = bias_r_dir[c]; float r_y = alpha * in_r_val[idx] + r_alpha * in_val[idx] + r_beta; out_r_val[idx] = r_y; } } extern "C" void neuralops_cuda_conv_scale_rfwd( const float *in_val, size_t spatial_dim, size_t num_channels, size_t batch_size, const float *in_r_val, const float *scale, const float *scale_r_dir, const float *bias_r_dir, float *out_r_val, hipStream_t stream) { uint32_t n = spatial_dim * num_channels * batch_size; hipLaunchKernelGGL(( conv_scale_rfwd_kernel), dim3((n+1024-1)/1024), dim3(1024), 0, stream, in_val, spatial_dim, num_channels, batch_size, in_r_val, scale, scale_r_dir, bias_r_dir, out_r_val); }
159e1e249a704e7be2a055bc159b8f34336aca10.cu
#include <cuda_runtime_api.h> #include <stdint.h> #define OFFSET_BANK(idx) ({ __typeof__ (idx) _idx = idx; ((_idx) + ((_idx) / 32)); }) __global__ void im2col_gpu_kernel( const int n, const float* data_im, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, const int height_col, const int width_col, float* data_col) { int index = threadIdx.x + blockDim.x * blockIdx.x; if (index < n) { const int h_index = index / width_col; const int h_col = h_index % height_col; const int w_col = index % width_col; const int c_im = h_index / height_col; const int c_col = c_im * kernel_h * kernel_w; const int h_offset = h_col * stride_h - pad_h; const int w_offset = w_col * stride_w - pad_w; float* data_col_ptr = data_col; data_col_ptr += (c_col * height_col + h_col) * width_col + w_col; const float* data_im_ptr = data_im; data_im_ptr += (c_im * height + h_offset) * width + w_offset; for (int i = 0; i < kernel_h; ++i) { for (int j = 0; j < kernel_w; ++j) { int h_im = h_offset + i * dilation_h; int w_im = w_offset + j * dilation_w; *data_col_ptr = (h_im >= 0 && w_im >= 0 && h_im < height && w_im < width) ? data_im_ptr[i * dilation_h * width + j * dilation_w] : 0; data_col_ptr += height_col * width_col; } } } } extern "C" void neuralops_cuda_caffe_im2col( const float* data_im, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, float* data_col, cudaStream_t stream) { // We are going to launch channels * height_col * width_col kernels, each // kernel responsible for copying a single-channel grid. int height_col = (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1; int width_col = (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1; int num_kernels = channels * height_col * width_col; // NOLINT_NEXT_LINE(whitespace/operators) im2col_gpu_kernel<<<(num_kernels+1024-1)/1024, 1024, 0, stream>>>( num_kernels, data_im, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, height_col, width_col, data_col); } __global__ void col2im_gpu_kernel( const int n, const float* data_col, const int height, const int width, const int channels, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, const int height_col, const int width_col, float* data_im) { int index = threadIdx.x + blockDim.x * blockIdx.x; if (index < n) { float val = 0; const int w_im = index % width + pad_w; const int h_im = (index / width) % height + pad_h; const int c_im = index / (width * height); int kernel_extent_w = (kernel_w - 1) * dilation_w + 1; int kernel_extent_h = (kernel_h - 1) * dilation_h + 1; // compute the start and end of the output const int w_col_start = (w_im < kernel_extent_w) ? 0 : (w_im - kernel_extent_w) / stride_w + 1; const int w_col_end = min(w_im / stride_w + 1, width_col); const int h_col_start = (h_im < kernel_extent_h) ? 0 : (h_im - kernel_extent_h) / stride_h + 1; const int h_col_end = min(h_im / stride_h + 1, height_col); // TODO: use LCM of stride and dilation to avoid unnecessary loops for (int h_col = h_col_start; h_col < h_col_end; h_col += 1) { for (int w_col = w_col_start; w_col < w_col_end; w_col += 1) { int h_k = (h_im - h_col * stride_h); int w_k = (w_im - w_col * stride_w); if (h_k % dilation_h == 0 && w_k % dilation_w == 0) { h_k /= dilation_h; w_k /= dilation_w; int data_col_index = (((c_im * kernel_h + h_k) * kernel_w + w_k) * height_col + h_col) * width_col + w_col; val += data_col[data_col_index]; } } } data_im[index] = val; } } extern "C" void neuralops_cuda_caffe_col2im( const float* data_col, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, float* data_im, cudaStream_t stream) { int height_col = (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1; int width_col = (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1; int num_kernels = channels * height * width; // To avoid involving atomic operations, we will launch one kernel per // bottom dimension, and then in the kernel add up the top dimensions. // NOLINT_NEXT_LINE(whitespace/operators) col2im_gpu_kernel<<<(num_kernels+1024-1)/1024, 1024, 0, stream>>>( num_kernels, data_col, height, width, channels, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, height_col, width_col, data_im); } __global__ void conv_diag_affine_fwd_batch_kernel( const float *in_act, int spatial_dim, int num_channels, int batch_size, const float *scale, const float *bias, float *out_act) { int idx = threadIdx.x + blockIdx.x * blockDim.x; int u = idx % spatial_dim; int c = (idx / spatial_dim) % num_channels; int batch_idx = idx / (spatial_dim * num_channels); if (u < spatial_dim && c < num_channels && batch_idx < batch_size) { float gamma = scale[c]; float beta = bias[c]; float y = gamma * in_act[idx] + beta; out_act[idx] = y; } } extern "C" void neuralops_cuda_conv2d_scale_fwd( const float *in_act, size_t spatial_dim, size_t num_channels, size_t batch_size, const float *scale, const float *bias, float *out_act, cudaStream_t stream) { int n = spatial_dim * num_channels * batch_size; conv_diag_affine_fwd_batch_kernel<<<(n+1024-1)/1024, 1024, 0, stream>>>( in_act, spatial_dim, num_channels, batch_size, scale, bias, out_act); } __global__ void conv_diag_affine_bwd_batch_kernel( const float *in_act, int spatial_dim, int num_channels, int batch_size, const float *out_delta, const float *scale, float *scale_grad, float *bias_grad, float *in_delta) { __shared__ float scale_grad_cache[1024+32]; __shared__ float bias_grad_cache[1024+32]; int idx = threadIdx.x + blockIdx.x * blockDim.x; int bank_idx = OFFSET_BANK(threadIdx.x); int block_spatial_dim = (spatial_dim+16*32-1)/(16*32); int warp_idx = idx % 32; int c = (idx / 32) % num_channels; int u0 = warp_idx + ((idx / (32 * num_channels)) % block_spatial_dim) * (16*32); int batch_idx = idx / (32 * num_channels * block_spatial_dim); if (c < num_channels && u0 < spatial_dim && batch_idx < batch_size) { float gamma = scale[c]; float d_gamma = 0.0f; float d_beta = 0.0f; int i0 = c * spatial_dim + batch_idx * spatial_dim * num_channels; int u_limit = min(spatial_dim, u0 + 16*32); for (int u = u0; u < u_limit; u += 32) { int i = i0 + u; float dy = out_delta[i]; d_gamma += dy * in_act[i]; d_beta += dy; in_delta[i] = dy * gamma; //in_delta[i] += dy * gamma; //atomicAdd(&in_delta[i], dy * gamma); } scale_grad_cache[bank_idx] = d_gamma; bias_grad_cache[bank_idx] = d_beta; } else { scale_grad_cache[bank_idx] = 0.0f; bias_grad_cache[bank_idx] = 0.0f; } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 2 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+1]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+1]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 4 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+2]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+2]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 8 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+4]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+4]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 16 == 0) { scale_grad_cache[bank_idx] += scale_grad_cache[bank_idx+8]; bias_grad_cache[bank_idx] += bias_grad_cache[bank_idx+8]; } } __syncthreads(); if (c < num_channels && batch_idx < batch_size) { if (threadIdx.x % 32 == 0 && u0 < spatial_dim) { float d_gamma = scale_grad_cache[bank_idx] + scale_grad_cache[bank_idx+16]; atomicAdd(&scale_grad[c], d_gamma); float d_beta = bias_grad_cache[bank_idx] + bias_grad_cache[bank_idx+16]; atomicAdd(&bias_grad[c], d_beta); } } } extern "C" void neuralops_cuda_conv2d_scale_bwd( const float *in_act, size_t spatial_dim, size_t num_channels, size_t batch_size, const float *out_delta, const float *scale, float *scale_grad, float *bias_grad, float *in_delta, cudaStream_t stream) { int block_spatial_dim = (spatial_dim+16*32-1)/(16*32); int n = 32 * num_channels * block_spatial_dim * batch_size; conv_diag_affine_bwd_batch_kernel<<<(n+1024-1)/1024, 1024, 0, stream>>>( in_act, spatial_dim, num_channels, batch_size, out_delta, scale, scale_grad, bias_grad, in_delta); } __global__ void conv_scale_rfwd_kernel( const float *in_val, uint32_t spatial_dim, uint32_t num_channels, uint32_t batch_size, const float *in_r_val, const float *scale, const float *scale_r_dir, const float *bias_r_dir, float *out_r_val) { uint32_t idx = threadIdx.x + blockIdx.x * blockDim.x; uint32_t u = idx % spatial_dim; uint32_t c = (idx / spatial_dim) % num_channels; uint32_t batch_idx = idx / (spatial_dim * num_channels); if (u < spatial_dim && c < num_channels && batch_idx < batch_size) { float alpha = scale[c]; float r_alpha = scale_r_dir[c]; float r_beta = bias_r_dir[c]; float r_y = alpha * in_r_val[idx] + r_alpha * in_val[idx] + r_beta; out_r_val[idx] = r_y; } } extern "C" void neuralops_cuda_conv_scale_rfwd( const float *in_val, size_t spatial_dim, size_t num_channels, size_t batch_size, const float *in_r_val, const float *scale, const float *scale_r_dir, const float *bias_r_dir, float *out_r_val, cudaStream_t stream) { uint32_t n = spatial_dim * num_channels * batch_size; conv_scale_rfwd_kernel<<<(n+1024-1)/1024, 1024, 0, stream>>>( in_val, spatial_dim, num_channels, batch_size, in_r_val, scale, scale_r_dir, bias_r_dir, out_r_val); }
dd716adb5c2e5f645fffeece869aecdb0531ca55.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // fourierEquationSolving.cpp : Defines the entry point for the console application. // #include "stdafx.h" __global__ void kernalStepSymplectic41(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= 0.67560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; Q[i] += 1.3512071919596576340476878089715 * P[i] * dt; } } __global__ void kernalStepSymplectic42(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= -0.17560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; Q[i] += -1.702414383919315268095375617943 * P[i] * dt; } } __global__ void kernalStepSymplectic43(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= -0.17560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; Q[i] += 1.3512071919596576340476878089715 * P[i] * dt; } } __global__ void kernalStepSymplectic44(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= 0.67560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; } } __global__ void kernel_Phi4_Phi6(const int N, double *t, double *q, const double lambda, const double g) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { t[i] = q[i] * q[i] * q[i] * (lambda + g * q[i] * q[i]); } } __global__ void kernelAddMullSqr(const int N, double* S, double* A, double m) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { S[i] += m * A[i] * A[i]; } } __global__ void kernelCulcRhoReal(const int N, double *rho, double *q, double *p, const double lambda, const double g) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { double qi = q[i]; double pi = p[i]; rho[i] = 0.5 * qi * qi; rho[i] += 0.5 * pi * pi; rho[i] += (lambda / 4.0) * qi * qi * qi * qi; rho[i] += (g / 6.0) * qi * qi * qi * qi * qi * qi; } } __global__ void kernelDer(const int N, complex* T, double *k, complex *Q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = complex(0, 1) * k[i] * Q[i]; } } __global__ void kernelSyncBuf(double *A, double *A0) { const int i = threadIdx.x; const int j = threadIdx.y; const int k = threadIdx.z; const int N1 = blockDim.x; const int N2 = blockDim.y; const int N3 = blockDim.z; const int iB = blockIdx.x; const int jB = blockIdx.y; const int kB = blockIdx.z; //const int N1B = gridDim.x; //just never used const int N2B = gridDim.y; const int N3B = gridDim.z; const int iG = i + iB * N1; const int jG = j + jB * N2; const int kG = k + kB * N3; //const int N1G = N1 * N1B; //just never used const int N2G = N2 * N2B; const int N3G = N3 * N3B; const int indB = k + N3 * (j + N2 * i); const int indA = kB + N3B * (jB + N2B * iB); const int indA0 = kG + N3G * (jG + N2G * iG); extern __shared__ double B[]; B[indB] = A0[indA0]; __syncthreads(); int numOfElem = N1 * N2 * N3; int step = 1; while (numOfElem > 1) { if (indB % (2*step) == 0) { B[indB] = B[indB] + B[indB + step]; } __syncthreads(); numOfElem /= 2; step *= 2; } if (indB == 0) { A[indA] = B[0] / (N1 * N2 * N3); } } __global__ void kernelGetOmega(const int N, double *omega, double *kSqr, const double sigma2, const double sigma4, const double lambda, const double g) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { omega[i] = sqrt(1 + kSqr[i] + 3 * lambda * sigma2 + 15 * g * sigma4); } } __global__ void kernelSetRhoK(complex *T, double m, double *k_sqr, complex *Q, complex *P) { int i = blockIdx.x * blockDim.x + threadIdx.x; T[i] = m * (P[i] * P[i].get_conj() + (1 + k_sqr[i]) * Q[i] * Q[i].get_conj()); } __global__ void kernelAddRhoK(double m, complex *Q, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; T[i] = m * Q[i] * T[i].get_conj(); } __global__ void kernelGetPhi2(const int N, double *T, double *q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = q[i] * q[i]; } } __global__ void kernelGetPhi3(const int N, double *T, double *q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = q[i] * q[i] * q[i]; } } __global__ void kernelGetPhi5(const int N, double *T, double *q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = q[i] * q[i] * q[i] * q[i] * q[i]; } }
dd716adb5c2e5f645fffeece869aecdb0531ca55.cu
// fourierEquationSolving.cpp : Defines the entry point for the console application. // #include "stdafx.h" __global__ void kernalStepSymplectic41(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= 0.67560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; Q[i] += 1.3512071919596576340476878089715 * P[i] * dt; } } __global__ void kernalStepSymplectic42(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= -0.17560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; Q[i] += -1.702414383919315268095375617943 * P[i] * dt; } } __global__ void kernalStepSymplectic43(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= -0.17560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; Q[i] += 1.3512071919596576340476878089715 * P[i] * dt; } } __global__ void kernalStepSymplectic44(const int N, const double dt, double *k_sqr, complex *Q, complex *P, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { P[i] -= 0.67560359597982881702384390448573 * (k_sqr[i] * Q[i] + Q[i] + T[i]) * dt; } } __global__ void kernel_Phi4_Phi6(const int N, double *t, double *q, const double lambda, const double g) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { t[i] = q[i] * q[i] * q[i] * (lambda + g * q[i] * q[i]); } } __global__ void kernelAddMullSqr(const int N, double* S, double* A, double m) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { S[i] += m * A[i] * A[i]; } } __global__ void kernelCulcRhoReal(const int N, double *rho, double *q, double *p, const double lambda, const double g) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { double qi = q[i]; double pi = p[i]; rho[i] = 0.5 * qi * qi; rho[i] += 0.5 * pi * pi; rho[i] += (lambda / 4.0) * qi * qi * qi * qi; rho[i] += (g / 6.0) * qi * qi * qi * qi * qi * qi; } } __global__ void kernelDer(const int N, complex* T, double *k, complex *Q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = complex(0, 1) * k[i] * Q[i]; } } __global__ void kernelSyncBuf(double *A, double *A0) { const int i = threadIdx.x; const int j = threadIdx.y; const int k = threadIdx.z; const int N1 = blockDim.x; const int N2 = blockDim.y; const int N3 = blockDim.z; const int iB = blockIdx.x; const int jB = blockIdx.y; const int kB = blockIdx.z; //const int N1B = gridDim.x; //just never used const int N2B = gridDim.y; const int N3B = gridDim.z; const int iG = i + iB * N1; const int jG = j + jB * N2; const int kG = k + kB * N3; //const int N1G = N1 * N1B; //just never used const int N2G = N2 * N2B; const int N3G = N3 * N3B; const int indB = k + N3 * (j + N2 * i); const int indA = kB + N3B * (jB + N2B * iB); const int indA0 = kG + N3G * (jG + N2G * iG); extern __shared__ double B[]; B[indB] = A0[indA0]; __syncthreads(); int numOfElem = N1 * N2 * N3; int step = 1; while (numOfElem > 1) { if (indB % (2*step) == 0) { B[indB] = B[indB] + B[indB + step]; } __syncthreads(); numOfElem /= 2; step *= 2; } if (indB == 0) { A[indA] = B[0] / (N1 * N2 * N3); } } __global__ void kernelGetOmega(const int N, double *omega, double *kSqr, const double sigma2, const double sigma4, const double lambda, const double g) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { omega[i] = sqrt(1 + kSqr[i] + 3 * lambda * sigma2 + 15 * g * sigma4); } } __global__ void kernelSetRhoK(complex *T, double m, double *k_sqr, complex *Q, complex *P) { int i = blockIdx.x * blockDim.x + threadIdx.x; T[i] = m * (P[i] * P[i].get_conj() + (1 + k_sqr[i]) * Q[i] * Q[i].get_conj()); } __global__ void kernelAddRhoK(double m, complex *Q, complex *T) { int i = blockIdx.x * blockDim.x + threadIdx.x; T[i] = m * Q[i] * T[i].get_conj(); } __global__ void kernelGetPhi2(const int N, double *T, double *q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = q[i] * q[i]; } } __global__ void kernelGetPhi3(const int N, double *T, double *q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = q[i] * q[i] * q[i]; } } __global__ void kernelGetPhi5(const int N, double *T, double *q) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { T[i] = q[i] * q[i] * q[i] * q[i] * q[i]; } }
91b32f2b25104b48387d0c07ef55551a3f93dac8.hip
// !!! This is a file automatically generated by hipify!!! #include <hip/hip_runtime.h> #include <hip/hip_runtime.h> #include <hip/hip_runtime_api.h> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #define WIDTH 7 #define HEIGHT 3 #define THREADS_PER_BLOCK 32 #define CUDA_CHECK(err) if(err != hipSuccess)\ {\ printf("hipMalloc returned error %s (code %d) (file %s) (line %d)\n", hipGetErrorString(err), err, __FILE__, __LINE__);\ }\ __global__ void transpose(int *input, int *output, int width, int height) { __shared__ int temp[THREADS_PER_BLOCK][THREADS_PER_BLOCK]; int xIndex = blockIdx.x*blockDim.x + threadIdx.x; int yIndex = blockIdx.y*blockDim.y + threadIdx.y; if((xIndex < width) && (yIndex < height)) { int id_in = yIndex * width + xIndex; temp[threadIdx.y][threadIdx.x] = input[id_in]; } __syncthreads(); xIndex = blockIdx.y * blockDim.y + threadIdx.x; yIndex = blockIdx.x * blockDim.x + threadIdx.y; if((xIndex < height) && (yIndex < width)) { int id_out = yIndex * height + xIndex; output[id_out] = temp[threadIdx.x][threadIdx.y]; } } inline __device__ void PrefixSum(int* output, int* input, int w, int nextpow2) { extern __shared__ int temp[]; const int tdx = threadIdx.x; int offset = 1; const int tdx2 = 2*tdx; const int tdx2p = tdx2 + 1; temp[tdx2] = tdx2 < w ? input[tdx2] : 0; temp[tdx2p] = tdx2p < w ? input[tdx2p] : 0; for(int d = nextpow2>>1; d > 0; d >>= 1) { __syncthreads(); if(tdx < d) { int ai = offset*(tdx2p)-1; int bi = offset*(tdx2+2)-1; temp[bi] += temp[ai]; } offset *= 2; } int last = temp[nextpow2 - 1]; if(tdx == 0) temp[nextpow2 - 1] = 0; for(int d = 1; d < nextpow2; d *= 2) { offset >>= 1; __syncthreads(); if(tdx < d ) { int ai = offset*(tdx2p)-1; int bi = offset*(tdx2+2)-1; int t = temp[ai]; temp[ai] = temp[bi]; temp[bi] += t; } } __syncthreads(); if(tdx2 < w) output[tdx2] = temp[tdx2]; if(tdx2p < w) output[tdx2p] = temp[tdx2p]; if(tdx2p < w) output[w] = last; } __global__ void KernPrefixSumRows(int *out, int *in, int height, int width) { const int row = blockIdx.y; PrefixSum(out+row*width-1, in+row*width, width, 2*blockDim.x ); } __global__ void KernPrefixSumRowsTrans(int *out, int *in, int height, int width) { const int row = blockIdx.y; PrefixSum(out+row*(width+1)+(width+1), in+row*width, width, 2*blockDim.x ); } void PrefixSumRows(int *out, int *in, int *outT, int height, int width) { dim3 blockDim = dim3( 1, 1); while(blockDim.x < ceil(width/2.0f)) blockDim.x <<= 1; dim3 gridDim = dim3( 1, height ); hipLaunchKernelGGL(( KernPrefixSumRows), dim3(gridDim),dim3(blockDim),2*sizeof(int)*blockDim.x, 0, out,in,height,width); hipDeviceSynchronize(); dim3 gridSize, blockSize; gridSize.x = (int)((width + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); gridSize.y = (int)((height + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); blockSize.x = THREADS_PER_BLOCK; blockSize.y = THREADS_PER_BLOCK; hipLaunchKernelGGL(( transpose), dim3(gridSize), dim3(blockSize), 0, 0, out, outT, width, height); hipDeviceSynchronize(); memset(out, 0, (HEIGHT+1)*sizeof(int)); blockDim = dim3( 1, 1); while(blockDim.x < ceil((height)/2.0f)) blockDim.x <<= 1; gridDim = dim3( 1, width ); hipLaunchKernelGGL(( KernPrefixSumRowsTrans), dim3(gridDim),dim3(blockDim),2*sizeof(int)*blockDim.x, 0, out,outT,width,height); hipDeviceSynchronize(); gridSize.x = (int)((height+1 + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); gridSize.y = (int)((width+1 + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); blockSize.x = THREADS_PER_BLOCK; blockSize.y = THREADS_PER_BLOCK; hipLaunchKernelGGL(( transpose), dim3(gridSize), dim3(blockSize), 0, 0, out, outT, height+1, width+1); hipDeviceSynchronize(); } void ComputeIntegrals(const unsigned char *Img, int *Integral) { const int SUM_WIDTH_STEP = (WIDTH+1); #define SUM_TYPE int int iW = WIDTH; // image dimensions int iH = HEIGHT; int sW = WIDTH+1; // sum dimensions unsigned char *ImgPtr = 0; SUM_TYPE *IntegPtr = 0; // write zeros to first row memset(Integral, 0, (WIDTH+1)*sizeof(int)); //#if WITH_CUDA // CudaComputeIntegralImages(Img, Integral, TiltedIntegral, SUM_WIDTH_STEP, cudaComputeStream); //#else { int yy=1; ImgPtr = (unsigned char *)(Img + WIDTH*(yy-1)); IntegPtr = (SUM_TYPE *)(Integral + SUM_WIDTH_STEP*yy); SUM_TYPE *IntegPtrA = IntegPtr - 1; SUM_TYPE *IntegPtrB = IntegPtr - sW - 1; SUM_TYPE *IntegPtrC = IntegPtr - sW; *IntegPtr++ = (SUM_TYPE)0.0; IntegPtrA++; IntegPtrB++; IntegPtrC++; for(int xx=1; xx<iW; xx++){ SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr++); *IntegPtr++ = fTemp + *IntegPtrA++ - *IntegPtrB++ + *IntegPtrC++; } SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr); *IntegPtr = fTemp + *IntegPtrA - *IntegPtrB + *IntegPtrC; } // compute regular integral and first pass of tilted for(int yy=2; yy<=iH; yy++){ ImgPtr = (unsigned char *)(Img + WIDTH*(yy-1)); IntegPtr = (SUM_TYPE *)(Integral + SUM_WIDTH_STEP*yy); SUM_TYPE *IntegPtrA = IntegPtr - 1; SUM_TYPE *IntegPtrB = IntegPtr - sW - 1; SUM_TYPE *IntegPtrC = IntegPtr - sW; *IntegPtr++ = (SUM_TYPE)0.0; IntegPtrA++; IntegPtrB++; IntegPtrC++; for(int xx=1; xx<iW; xx++){ SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr++); *IntegPtr++ = fTemp + *IntegPtrA++ - *IntegPtrB++ + *IntegPtrC++; } SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr); *IntegPtr = fTemp + *IntegPtrA - *IntegPtrB + *IntegPtrC; } printf("\n\n"); //#endif } int main() { unsigned char *Img=0; int *ImgInt=0; int *Integral=0; int *IntegralTransposed=0; clock_t start, end; CUDA_CHECK( hipMallocManaged((void **) &Img, WIDTH*HEIGHT) ); CUDA_CHECK( hipMallocManaged((void **) &ImgInt, WIDTH*HEIGHT*sizeof(int)) ); CUDA_CHECK( hipMallocManaged((void **) &Integral, (WIDTH+1)*(HEIGHT+1)*sizeof(int)) ); CUDA_CHECK( hipMallocManaged((void **) &IntegralTransposed, (WIDTH+1)*(HEIGHT+1)*sizeof(int)) ); for (int i=0; i<WIDTH*HEIGHT; i++) Img[i] = 1; for (int i=0; i<WIDTH*HEIGHT; i++) ImgInt[i] = 1; for (int i=0; i<(WIDTH+1)*(HEIGHT+1); i++) Integral[i] = 1; for (int i=0; i<(WIDTH+1)*(HEIGHT+1); i++) IntegralTransposed[i] = 1; start = clock(); ComputeIntegrals(Img, Integral); end = clock(); printf("CPU Time Taken: %f\n", ((double)(end-start))/CLOCKS_PER_SEC); int *IntegPtr; unsigned char *ImgPtr; // input printf("Input\n\n"); for (int i=0; i<HEIGHT; i++) { for (int j=0; j<WIDTH; j++) { ImgPtr = Img + i * WIDTH + j; printf("%d ", *ImgPtr); } printf("\n"); } printf("\n\n"); printf("Output CPU"); printf("\n\n"); for (int i=0; i<(HEIGHT+1); i++) { for (int j=0; j<(WIDTH+1); j++) { IntegPtr = Integral + i * (WIDTH+1) + j; printf("%d ", *IntegPtr); } printf("\n"); } printf("\n\n"); printf("OUTPUT GPU"); printf("\n\n"); for (int i=0; i<(WIDTH+1)*(HEIGHT+1); i++) Integral[i] = 0; //CudaComputeIntegralImages(ImgInt, Integral, IntegralTransposed); start = clock(); PrefixSumRows(Integral, ImgInt, IntegralTransposed, HEIGHT, WIDTH); end = clock(); printf("GPU Time Taken: %f\n", ((double)(end-start))/CLOCKS_PER_SEC); for (int i=0; i<(HEIGHT+1); i++) { for (int j=0; j<(WIDTH+1); j++) { IntegPtr = IntegralTransposed + i * (WIDTH+1) + j; printf("%d ", *IntegPtr); } printf("\n"); } hipFree(Img); hipFree(ImgInt); hipFree(Integral); hipFree(IntegralTransposed); return 0; }
91b32f2b25104b48387d0c07ef55551a3f93dac8.cu
#include <cuda.h> #include <cuda_runtime.h> #include <cuda_runtime_api.h> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #define WIDTH 7 #define HEIGHT 3 #define THREADS_PER_BLOCK 32 #define CUDA_CHECK(err) if(err != cudaSuccess)\ {\ printf("cudaMalloc returned error %s (code %d) (file %s) (line %d)\n", cudaGetErrorString(err), err, __FILE__, __LINE__);\ }\ __global__ void transpose(int *input, int *output, int width, int height) { __shared__ int temp[THREADS_PER_BLOCK][THREADS_PER_BLOCK]; int xIndex = blockIdx.x*blockDim.x + threadIdx.x; int yIndex = blockIdx.y*blockDim.y + threadIdx.y; if((xIndex < width) && (yIndex < height)) { int id_in = yIndex * width + xIndex; temp[threadIdx.y][threadIdx.x] = input[id_in]; } __syncthreads(); xIndex = blockIdx.y * blockDim.y + threadIdx.x; yIndex = blockIdx.x * blockDim.x + threadIdx.y; if((xIndex < height) && (yIndex < width)) { int id_out = yIndex * height + xIndex; output[id_out] = temp[threadIdx.x][threadIdx.y]; } } inline __device__ void PrefixSum(int* output, int* input, int w, int nextpow2) { extern __shared__ int temp[]; const int tdx = threadIdx.x; int offset = 1; const int tdx2 = 2*tdx; const int tdx2p = tdx2 + 1; temp[tdx2] = tdx2 < w ? input[tdx2] : 0; temp[tdx2p] = tdx2p < w ? input[tdx2p] : 0; for(int d = nextpow2>>1; d > 0; d >>= 1) { __syncthreads(); if(tdx < d) { int ai = offset*(tdx2p)-1; int bi = offset*(tdx2+2)-1; temp[bi] += temp[ai]; } offset *= 2; } int last = temp[nextpow2 - 1]; if(tdx == 0) temp[nextpow2 - 1] = 0; for(int d = 1; d < nextpow2; d *= 2) { offset >>= 1; __syncthreads(); if(tdx < d ) { int ai = offset*(tdx2p)-1; int bi = offset*(tdx2+2)-1; int t = temp[ai]; temp[ai] = temp[bi]; temp[bi] += t; } } __syncthreads(); if(tdx2 < w) output[tdx2] = temp[tdx2]; if(tdx2p < w) output[tdx2p] = temp[tdx2p]; if(tdx2p < w) output[w] = last; } __global__ void KernPrefixSumRows(int *out, int *in, int height, int width) { const int row = blockIdx.y; PrefixSum(out+row*width-1, in+row*width, width, 2*blockDim.x ); } __global__ void KernPrefixSumRowsTrans(int *out, int *in, int height, int width) { const int row = blockIdx.y; PrefixSum(out+row*(width+1)+(width+1), in+row*width, width, 2*blockDim.x ); } void PrefixSumRows(int *out, int *in, int *outT, int height, int width) { dim3 blockDim = dim3( 1, 1); while(blockDim.x < ceil(width/2.0f)) blockDim.x <<= 1; dim3 gridDim = dim3( 1, height ); KernPrefixSumRows<<<gridDim,blockDim,2*sizeof(int)*blockDim.x>>>(out,in,height,width); cudaDeviceSynchronize(); dim3 gridSize, blockSize; gridSize.x = (int)((width + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); gridSize.y = (int)((height + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); blockSize.x = THREADS_PER_BLOCK; blockSize.y = THREADS_PER_BLOCK; transpose<<<gridSize, blockSize>>>(out, outT, width, height); cudaDeviceSynchronize(); memset(out, 0, (HEIGHT+1)*sizeof(int)); blockDim = dim3( 1, 1); while(blockDim.x < ceil((height)/2.0f)) blockDim.x <<= 1; gridDim = dim3( 1, width ); KernPrefixSumRowsTrans<<<gridDim,blockDim,2*sizeof(int)*blockDim.x>>>(out,outT,width,height); cudaDeviceSynchronize(); gridSize.x = (int)((height+1 + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); gridSize.y = (int)((width+1 + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK); blockSize.x = THREADS_PER_BLOCK; blockSize.y = THREADS_PER_BLOCK; transpose<<<gridSize, blockSize>>>(out, outT, height+1, width+1); cudaDeviceSynchronize(); } void ComputeIntegrals(const unsigned char *Img, int *Integral) { const int SUM_WIDTH_STEP = (WIDTH+1); #define SUM_TYPE int int iW = WIDTH; // image dimensions int iH = HEIGHT; int sW = WIDTH+1; // sum dimensions unsigned char *ImgPtr = 0; SUM_TYPE *IntegPtr = 0; // write zeros to first row memset(Integral, 0, (WIDTH+1)*sizeof(int)); //#if WITH_CUDA // CudaComputeIntegralImages(Img, Integral, TiltedIntegral, SUM_WIDTH_STEP, cudaComputeStream); //#else { int yy=1; ImgPtr = (unsigned char *)(Img + WIDTH*(yy-1)); IntegPtr = (SUM_TYPE *)(Integral + SUM_WIDTH_STEP*yy); SUM_TYPE *IntegPtrA = IntegPtr - 1; SUM_TYPE *IntegPtrB = IntegPtr - sW - 1; SUM_TYPE *IntegPtrC = IntegPtr - sW; *IntegPtr++ = (SUM_TYPE)0.0; IntegPtrA++; IntegPtrB++; IntegPtrC++; for(int xx=1; xx<iW; xx++){ SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr++); *IntegPtr++ = fTemp + *IntegPtrA++ - *IntegPtrB++ + *IntegPtrC++; } SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr); *IntegPtr = fTemp + *IntegPtrA - *IntegPtrB + *IntegPtrC; } // compute regular integral and first pass of tilted for(int yy=2; yy<=iH; yy++){ ImgPtr = (unsigned char *)(Img + WIDTH*(yy-1)); IntegPtr = (SUM_TYPE *)(Integral + SUM_WIDTH_STEP*yy); SUM_TYPE *IntegPtrA = IntegPtr - 1; SUM_TYPE *IntegPtrB = IntegPtr - sW - 1; SUM_TYPE *IntegPtrC = IntegPtr - sW; *IntegPtr++ = (SUM_TYPE)0.0; IntegPtrA++; IntegPtrB++; IntegPtrC++; for(int xx=1; xx<iW; xx++){ SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr++); *IntegPtr++ = fTemp + *IntegPtrA++ - *IntegPtrB++ + *IntegPtrC++; } SUM_TYPE fTemp = (SUM_TYPE)*(ImgPtr); *IntegPtr = fTemp + *IntegPtrA - *IntegPtrB + *IntegPtrC; } printf("\n\n"); //#endif } int main() { unsigned char *Img=0; int *ImgInt=0; int *Integral=0; int *IntegralTransposed=0; clock_t start, end; CUDA_CHECK( cudaMallocManaged((void **) &Img, WIDTH*HEIGHT) ); CUDA_CHECK( cudaMallocManaged((void **) &ImgInt, WIDTH*HEIGHT*sizeof(int)) ); CUDA_CHECK( cudaMallocManaged((void **) &Integral, (WIDTH+1)*(HEIGHT+1)*sizeof(int)) ); CUDA_CHECK( cudaMallocManaged((void **) &IntegralTransposed, (WIDTH+1)*(HEIGHT+1)*sizeof(int)) ); for (int i=0; i<WIDTH*HEIGHT; i++) Img[i] = 1; for (int i=0; i<WIDTH*HEIGHT; i++) ImgInt[i] = 1; for (int i=0; i<(WIDTH+1)*(HEIGHT+1); i++) Integral[i] = 1; for (int i=0; i<(WIDTH+1)*(HEIGHT+1); i++) IntegralTransposed[i] = 1; start = clock(); ComputeIntegrals(Img, Integral); end = clock(); printf("CPU Time Taken: %f\n", ((double)(end-start))/CLOCKS_PER_SEC); int *IntegPtr; unsigned char *ImgPtr; // input printf("Input\n\n"); for (int i=0; i<HEIGHT; i++) { for (int j=0; j<WIDTH; j++) { ImgPtr = Img + i * WIDTH + j; printf("%d ", *ImgPtr); } printf("\n"); } printf("\n\n"); printf("Output CPU"); printf("\n\n"); for (int i=0; i<(HEIGHT+1); i++) { for (int j=0; j<(WIDTH+1); j++) { IntegPtr = Integral + i * (WIDTH+1) + j; printf("%d ", *IntegPtr); } printf("\n"); } printf("\n\n"); printf("OUTPUT GPU"); printf("\n\n"); for (int i=0; i<(WIDTH+1)*(HEIGHT+1); i++) Integral[i] = 0; //CudaComputeIntegralImages(ImgInt, Integral, IntegralTransposed); start = clock(); PrefixSumRows(Integral, ImgInt, IntegralTransposed, HEIGHT, WIDTH); end = clock(); printf("GPU Time Taken: %f\n", ((double)(end-start))/CLOCKS_PER_SEC); for (int i=0; i<(HEIGHT+1); i++) { for (int j=0; j<(WIDTH+1); j++) { IntegPtr = IntegralTransposed + i * (WIDTH+1) + j; printf("%d ", *IntegPtr); } printf("\n"); } cudaFree(Img); cudaFree(ImgInt); cudaFree(Integral); cudaFree(IntegralTransposed); return 0; }
44334324229fe14a435708784a94f02db5c797dd.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #if !defined CUDA_DISABLER #include "opencv2/core/cuda/common.hpp" #include "opencv2/core/cuda/vec_traits.hpp" #include "opencv2/core/cuda/vec_math.hpp" #include "opencv2/core/cuda/limits.hpp" #include "mog2.hpp" namespace cv { namespace cuda { namespace device { namespace mog2 { /////////////////////////////////////////////////////////////// // Utility __device__ __forceinline__ float cvt(uchar val) { return val; } __device__ __forceinline__ float3 cvt(const uchar3 &val) { return make_float3(val.x, val.y, val.z); } __device__ __forceinline__ float4 cvt(const uchar4 &val) { return make_float4(val.x, val.y, val.z, val.w); } __device__ __forceinline__ float sqr(float val) { return val * val; } __device__ __forceinline__ float sqr(const float3 &val) { return val.x * val.x + val.y * val.y + val.z * val.z; } __device__ __forceinline__ float sqr(const float4 &val) { return val.x * val.x + val.y * val.y + val.z * val.z; } __device__ __forceinline__ float sum(float val) { return val; } __device__ __forceinline__ float sum(const float3 &val) { return val.x + val.y + val.z; } __device__ __forceinline__ float sum(const float4 &val) { return val.x + val.y + val.z; } template <class Ptr2D> __device__ __forceinline__ void swap(Ptr2D &ptr, int x, int y, int k, int rows) { typename Ptr2D::elem_type val = ptr(k * rows + y, x); ptr(k * rows + y, x) = ptr((k + 1) * rows + y, x); ptr((k + 1) * rows + y, x) = val; } /////////////////////////////////////////////////////////////// // MOG2 template <bool detectShadows, typename SrcT, typename WorkT> __global__ void mog2(const PtrStepSz<SrcT> frame, PtrStepb fgmask, PtrStepb modesUsed, PtrStepf gmm_weight, PtrStepf gmm_variance, PtrStep<WorkT> gmm_mean, const float alphaT, const float alpha1, const float prune, const Constants *const constants) { const int x = blockIdx.x * blockDim.x + threadIdx.x; const int y = blockIdx.y * blockDim.y + threadIdx.y; if (x < frame.cols && y < frame.rows) { WorkT pix = cvt(frame(y, x)); //calculate distances to the modes (+ sort) //here we need to go in descending order!!! bool background = false; // true - the pixel classified as background //internal: bool fitsPDF = false; //if it remains zero a new GMM mode will be added int nmodes = modesUsed(y, x); const int nNewModes = nmodes; //current number of modes in GMM float totalWeight = 0.0f; //go through all modes for (int mode = 0; mode < nmodes; ++mode) { //need only weight if fit is found float weight = alpha1 * gmm_weight(mode * frame.rows + y, x) + prune; int swap_count = 0; //fit not found yet if (!fitsPDF) { //check if it belongs to some of the remaining modes const float var = gmm_variance(mode * frame.rows + y, x); const WorkT mean = gmm_mean(mode * frame.rows + y, x); //calculate difference and distance const WorkT diff = mean - pix; const float dist2 = sqr(diff); //background? - Tb - usually larger than Tg if (totalWeight < constants->TB_ && dist2 < constants->Tb_ * var) background = true; //check fit if (dist2 < constants->Tg_ * var) { //belongs to the mode fitsPDF = true; //update distribution //update weight weight += alphaT; float k = alphaT / weight; //update mean gmm_mean(mode * frame.rows + y, x) = mean - k * diff; //update variance float varnew = var + k * (dist2 - var); //limit the variance varnew = ::fmaxf(varnew, constants->varMin_); varnew = ::fminf(varnew, constants->varMax_); gmm_variance(mode * frame.rows + y, x) = varnew; //sort //all other weights are at the same place and //only the matched (iModes) is higher -> just find the new place for it for (int i = mode; i > 0; --i) { //check one up if (weight < gmm_weight((i - 1) * frame.rows + y, x)) break; swap_count++; //swap one up swap(gmm_weight, x, y, i - 1, frame.rows); swap(gmm_variance, x, y, i - 1, frame.rows); swap(gmm_mean, x, y, i - 1, frame.rows); } //belongs to the mode - bFitsPDF becomes 1 } } // !fitsPDF //check prune if (weight < -prune) { weight = 0.0f; nmodes--; } gmm_weight((mode - swap_count) * frame.rows + y, x) = weight; //update weight by the calculated value totalWeight += weight; } //renormalize weights totalWeight = 1.f / totalWeight; for (int mode = 0; mode < nmodes; ++mode) gmm_weight(mode * frame.rows + y, x) *= totalWeight; nmodes = nNewModes; //make new mode if needed and exit if (!fitsPDF) { // replace the weakest or add a new one const int mode = nmodes == constants->nmixtures_ ? constants->nmixtures_ - 1 : nmodes++; if (nmodes == 1) gmm_weight(mode * frame.rows + y, x) = 1.f; else { gmm_weight(mode * frame.rows + y, x) = alphaT; // renormalize all other weights for (int i = 0; i < nmodes - 1; ++i) gmm_weight(i * frame.rows + y, x) *= alpha1; } // init gmm_mean(mode * frame.rows + y, x) = pix; gmm_variance(mode * frame.rows + y, x) = constants->varInit_; //sort //find the new place for it for (int i = nmodes - 1; i > 0; --i) { // check one up if (alphaT < gmm_weight((i - 1) * frame.rows + y, x)) break; //swap one up swap(gmm_weight, x, y, i - 1, frame.rows); swap(gmm_variance, x, y, i - 1, frame.rows); swap(gmm_mean, x, y, i - 1, frame.rows); } } //set the number of modes modesUsed(y, x) = nmodes; bool isShadow = false; if (detectShadows && !background) { float tWeight = 0.0f; // check all the components marked as background: for (int mode = 0; mode < nmodes; ++mode) { const WorkT mean = gmm_mean(mode * frame.rows + y, x); const WorkT pix_mean = pix * mean; const float numerator = sum(pix_mean); const float denominator = sqr(mean); // no division by zero allowed if (denominator == 0) break; // if tau < a < 1 then also check the color distortion else if (numerator <= denominator && numerator >= constants->tau_ * denominator) { const float a = numerator / denominator; WorkT dD = a * mean - pix; if (sqr(dD) < constants->Tb_ * gmm_variance(mode * frame.rows + y, x) * a * a) { isShadow = true; break; } }; tWeight += gmm_weight(mode * frame.rows + y, x); if (tWeight > constants->TB_) break; } } fgmask(y, x) = background ? 0 : isShadow ? constants->shadowVal_ : 255; } } template <typename SrcT, typename WorkT> void mog2_caller(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, hipStream_t stream) { dim3 block(32, 8); dim3 grid(divUp(frame.cols, block.x), divUp(frame.rows, block.y)); const float alpha1 = 1.0f - alphaT; if (detectShadows) { cudaSafeCall(hipFuncSetCacheConfig(mog2<true, SrcT, WorkT>, hipFuncCachePreferL1)); hipLaunchKernelGGL(( mog2<true, SrcT, WorkT>), dim3(grid), dim3(block), 0, stream, (PtrStepSz<SrcT>)frame, fgmask, modesUsed, weight, variance, (PtrStepSz<WorkT>)mean, alphaT, alpha1, prune, constants); } else { cudaSafeCall(hipFuncSetCacheConfig(mog2<false, SrcT, WorkT>, hipFuncCachePreferL1)); hipLaunchKernelGGL(( mog2<false, SrcT, WorkT>), dim3(grid), dim3(block), 0, stream, (PtrStepSz<SrcT>)frame, fgmask, modesUsed, weight, variance, (PtrStepSz<WorkT>)mean, alphaT, alpha1, prune, constants); } cudaSafeCall(hipGetLastError()); if (stream == 0) cudaSafeCall(hipDeviceSynchronize()); } void mog2_gpu(PtrStepSzb frame, int cn, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, hipStream_t stream) { typedef void (*func_t)(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, hipStream_t stream); static const func_t funcs[] = { 0, mog2_caller<uchar, float>, 0, mog2_caller<uchar3, float3>, mog2_caller<uchar4, float4>}; funcs[cn](frame, fgmask, modesUsed, weight, variance, mean, alphaT, prune, detectShadows, constants, stream); } template <typename WorkT, typename OutT> __global__ void getBackgroundImage2(const PtrStepSzb modesUsed, const PtrStepf gmm_weight, const PtrStep<WorkT> gmm_mean, PtrStep<OutT> dst, const Constants *const constants) { const int x = blockIdx.x * blockDim.x + threadIdx.x; const int y = blockIdx.y * blockDim.y + threadIdx.y; if (x >= modesUsed.cols || y >= modesUsed.rows) return; int nmodes = modesUsed(y, x); WorkT meanVal = VecTraits<WorkT>::all(0.0f); float totalWeight = 0.0f; for (int mode = 0; mode < nmodes; ++mode) { float weight = gmm_weight(mode * modesUsed.rows + y, x); WorkT mean = gmm_mean(mode * modesUsed.rows + y, x); meanVal = meanVal + weight * mean; totalWeight += weight; if (totalWeight > constants->TB_) break; } meanVal = meanVal * (1.f / totalWeight); dst(y, x) = saturate_cast<OutT>(meanVal); } template <typename WorkT, typename OutT> void getBackgroundImage2_caller(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, hipStream_t stream) { dim3 block(32, 8); dim3 grid(divUp(modesUsed.cols, block.x), divUp(modesUsed.rows, block.y)); cudaSafeCall(hipFuncSetCacheConfig(getBackgroundImage2<WorkT, OutT>, hipFuncCachePreferL1)); hipLaunchKernelGGL(( getBackgroundImage2<WorkT, OutT>), dim3(grid), dim3(block), 0, stream, modesUsed, weight, (PtrStepSz<WorkT>)mean, (PtrStepSz<OutT>)dst, constants); cudaSafeCall(hipGetLastError()); if (stream == 0) cudaSafeCall(hipDeviceSynchronize()); } void getBackgroundImage2_gpu(int cn, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, hipStream_t stream) { typedef void (*func_t)(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, hipStream_t stream); static const func_t funcs[] = { 0, getBackgroundImage2_caller<float, uchar>, 0, getBackgroundImage2_caller<float3, uchar3>, getBackgroundImage2_caller<float4, uchar4>}; funcs[cn](modesUsed, weight, mean, dst, constants, stream); } } // namespace mog2 } // namespace device } // namespace cuda } // namespace cv #endif /* CUDA_DISABLER */
44334324229fe14a435708784a94f02db5c797dd.cu
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #if !defined CUDA_DISABLER #include "opencv2/core/cuda/common.hpp" #include "opencv2/core/cuda/vec_traits.hpp" #include "opencv2/core/cuda/vec_math.hpp" #include "opencv2/core/cuda/limits.hpp" #include "mog2.hpp" namespace cv { namespace cuda { namespace device { namespace mog2 { /////////////////////////////////////////////////////////////// // Utility __device__ __forceinline__ float cvt(uchar val) { return val; } __device__ __forceinline__ float3 cvt(const uchar3 &val) { return make_float3(val.x, val.y, val.z); } __device__ __forceinline__ float4 cvt(const uchar4 &val) { return make_float4(val.x, val.y, val.z, val.w); } __device__ __forceinline__ float sqr(float val) { return val * val; } __device__ __forceinline__ float sqr(const float3 &val) { return val.x * val.x + val.y * val.y + val.z * val.z; } __device__ __forceinline__ float sqr(const float4 &val) { return val.x * val.x + val.y * val.y + val.z * val.z; } __device__ __forceinline__ float sum(float val) { return val; } __device__ __forceinline__ float sum(const float3 &val) { return val.x + val.y + val.z; } __device__ __forceinline__ float sum(const float4 &val) { return val.x + val.y + val.z; } template <class Ptr2D> __device__ __forceinline__ void swap(Ptr2D &ptr, int x, int y, int k, int rows) { typename Ptr2D::elem_type val = ptr(k * rows + y, x); ptr(k * rows + y, x) = ptr((k + 1) * rows + y, x); ptr((k + 1) * rows + y, x) = val; } /////////////////////////////////////////////////////////////// // MOG2 template <bool detectShadows, typename SrcT, typename WorkT> __global__ void mog2(const PtrStepSz<SrcT> frame, PtrStepb fgmask, PtrStepb modesUsed, PtrStepf gmm_weight, PtrStepf gmm_variance, PtrStep<WorkT> gmm_mean, const float alphaT, const float alpha1, const float prune, const Constants *const constants) { const int x = blockIdx.x * blockDim.x + threadIdx.x; const int y = blockIdx.y * blockDim.y + threadIdx.y; if (x < frame.cols && y < frame.rows) { WorkT pix = cvt(frame(y, x)); //calculate distances to the modes (+ sort) //here we need to go in descending order!!! bool background = false; // true - the pixel classified as background //internal: bool fitsPDF = false; //if it remains zero a new GMM mode will be added int nmodes = modesUsed(y, x); const int nNewModes = nmodes; //current number of modes in GMM float totalWeight = 0.0f; //go through all modes for (int mode = 0; mode < nmodes; ++mode) { //need only weight if fit is found float weight = alpha1 * gmm_weight(mode * frame.rows + y, x) + prune; int swap_count = 0; //fit not found yet if (!fitsPDF) { //check if it belongs to some of the remaining modes const float var = gmm_variance(mode * frame.rows + y, x); const WorkT mean = gmm_mean(mode * frame.rows + y, x); //calculate difference and distance const WorkT diff = mean - pix; const float dist2 = sqr(diff); //background? - Tb - usually larger than Tg if (totalWeight < constants->TB_ && dist2 < constants->Tb_ * var) background = true; //check fit if (dist2 < constants->Tg_ * var) { //belongs to the mode fitsPDF = true; //update distribution //update weight weight += alphaT; float k = alphaT / weight; //update mean gmm_mean(mode * frame.rows + y, x) = mean - k * diff; //update variance float varnew = var + k * (dist2 - var); //limit the variance varnew = ::fmaxf(varnew, constants->varMin_); varnew = ::fminf(varnew, constants->varMax_); gmm_variance(mode * frame.rows + y, x) = varnew; //sort //all other weights are at the same place and //only the matched (iModes) is higher -> just find the new place for it for (int i = mode; i > 0; --i) { //check one up if (weight < gmm_weight((i - 1) * frame.rows + y, x)) break; swap_count++; //swap one up swap(gmm_weight, x, y, i - 1, frame.rows); swap(gmm_variance, x, y, i - 1, frame.rows); swap(gmm_mean, x, y, i - 1, frame.rows); } //belongs to the mode - bFitsPDF becomes 1 } } // !fitsPDF //check prune if (weight < -prune) { weight = 0.0f; nmodes--; } gmm_weight((mode - swap_count) * frame.rows + y, x) = weight; //update weight by the calculated value totalWeight += weight; } //renormalize weights totalWeight = 1.f / totalWeight; for (int mode = 0; mode < nmodes; ++mode) gmm_weight(mode * frame.rows + y, x) *= totalWeight; nmodes = nNewModes; //make new mode if needed and exit if (!fitsPDF) { // replace the weakest or add a new one const int mode = nmodes == constants->nmixtures_ ? constants->nmixtures_ - 1 : nmodes++; if (nmodes == 1) gmm_weight(mode * frame.rows + y, x) = 1.f; else { gmm_weight(mode * frame.rows + y, x) = alphaT; // renormalize all other weights for (int i = 0; i < nmodes - 1; ++i) gmm_weight(i * frame.rows + y, x) *= alpha1; } // init gmm_mean(mode * frame.rows + y, x) = pix; gmm_variance(mode * frame.rows + y, x) = constants->varInit_; //sort //find the new place for it for (int i = nmodes - 1; i > 0; --i) { // check one up if (alphaT < gmm_weight((i - 1) * frame.rows + y, x)) break; //swap one up swap(gmm_weight, x, y, i - 1, frame.rows); swap(gmm_variance, x, y, i - 1, frame.rows); swap(gmm_mean, x, y, i - 1, frame.rows); } } //set the number of modes modesUsed(y, x) = nmodes; bool isShadow = false; if (detectShadows && !background) { float tWeight = 0.0f; // check all the components marked as background: for (int mode = 0; mode < nmodes; ++mode) { const WorkT mean = gmm_mean(mode * frame.rows + y, x); const WorkT pix_mean = pix * mean; const float numerator = sum(pix_mean); const float denominator = sqr(mean); // no division by zero allowed if (denominator == 0) break; // if tau < a < 1 then also check the color distortion else if (numerator <= denominator && numerator >= constants->tau_ * denominator) { const float a = numerator / denominator; WorkT dD = a * mean - pix; if (sqr(dD) < constants->Tb_ * gmm_variance(mode * frame.rows + y, x) * a * a) { isShadow = true; break; } }; tWeight += gmm_weight(mode * frame.rows + y, x); if (tWeight > constants->TB_) break; } } fgmask(y, x) = background ? 0 : isShadow ? constants->shadowVal_ : 255; } } template <typename SrcT, typename WorkT> void mog2_caller(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, cudaStream_t stream) { dim3 block(32, 8); dim3 grid(divUp(frame.cols, block.x), divUp(frame.rows, block.y)); const float alpha1 = 1.0f - alphaT; if (detectShadows) { cudaSafeCall(cudaFuncSetCacheConfig(mog2<true, SrcT, WorkT>, cudaFuncCachePreferL1)); mog2<true, SrcT, WorkT><<<grid, block, 0, stream>>>((PtrStepSz<SrcT>)frame, fgmask, modesUsed, weight, variance, (PtrStepSz<WorkT>)mean, alphaT, alpha1, prune, constants); } else { cudaSafeCall(cudaFuncSetCacheConfig(mog2<false, SrcT, WorkT>, cudaFuncCachePreferL1)); mog2<false, SrcT, WorkT><<<grid, block, 0, stream>>>((PtrStepSz<SrcT>)frame, fgmask, modesUsed, weight, variance, (PtrStepSz<WorkT>)mean, alphaT, alpha1, prune, constants); } cudaSafeCall(cudaGetLastError()); if (stream == 0) cudaSafeCall(cudaDeviceSynchronize()); } void mog2_gpu(PtrStepSzb frame, int cn, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, cudaStream_t stream) { typedef void (*func_t)(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, cudaStream_t stream); static const func_t funcs[] = { 0, mog2_caller<uchar, float>, 0, mog2_caller<uchar3, float3>, mog2_caller<uchar4, float4>}; funcs[cn](frame, fgmask, modesUsed, weight, variance, mean, alphaT, prune, detectShadows, constants, stream); } template <typename WorkT, typename OutT> __global__ void getBackgroundImage2(const PtrStepSzb modesUsed, const PtrStepf gmm_weight, const PtrStep<WorkT> gmm_mean, PtrStep<OutT> dst, const Constants *const constants) { const int x = blockIdx.x * blockDim.x + threadIdx.x; const int y = blockIdx.y * blockDim.y + threadIdx.y; if (x >= modesUsed.cols || y >= modesUsed.rows) return; int nmodes = modesUsed(y, x); WorkT meanVal = VecTraits<WorkT>::all(0.0f); float totalWeight = 0.0f; for (int mode = 0; mode < nmodes; ++mode) { float weight = gmm_weight(mode * modesUsed.rows + y, x); WorkT mean = gmm_mean(mode * modesUsed.rows + y, x); meanVal = meanVal + weight * mean; totalWeight += weight; if (totalWeight > constants->TB_) break; } meanVal = meanVal * (1.f / totalWeight); dst(y, x) = saturate_cast<OutT>(meanVal); } template <typename WorkT, typename OutT> void getBackgroundImage2_caller(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, cudaStream_t stream) { dim3 block(32, 8); dim3 grid(divUp(modesUsed.cols, block.x), divUp(modesUsed.rows, block.y)); cudaSafeCall(cudaFuncSetCacheConfig(getBackgroundImage2<WorkT, OutT>, cudaFuncCachePreferL1)); getBackgroundImage2<WorkT, OutT><<<grid, block, 0, stream>>>(modesUsed, weight, (PtrStepSz<WorkT>)mean, (PtrStepSz<OutT>)dst, constants); cudaSafeCall(cudaGetLastError()); if (stream == 0) cudaSafeCall(cudaDeviceSynchronize()); } void getBackgroundImage2_gpu(int cn, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, cudaStream_t stream) { typedef void (*func_t)(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, cudaStream_t stream); static const func_t funcs[] = { 0, getBackgroundImage2_caller<float, uchar>, 0, getBackgroundImage2_caller<float3, uchar3>, getBackgroundImage2_caller<float4, uchar4>}; funcs[cn](modesUsed, weight, mean, dst, constants, stream); } } // namespace mog2 } // namespace device } // namespace cuda } // namespace cv #endif /* CUDA_DISABLER */
5510e6d13595b853f9f73cc4ccfbbcf13d80f7c5.hip
// !!! This is a file automatically generated by hipify!!! /* This file contains routines for Parallel vector operations. */ #define PETSC_SKIP_SPINLOCK #define PETSC_SKIP_CXX_COMPLEX_FIX #include <petscconf.h> #include <../src/vec/vec/impls/mpi/pvecimpl.h> /*I "petscvec.h" I*/ #include <petsc/private/cudavecimpl.h> /*MC VECCUDA - VECCUDA = "cuda" - A VECSEQCUDA on a single-process communicator, and VECMPICUDA otherwise. Options Database Keys: . -vec_type cuda - sets the vector type to VECCUDA during a call to VecSetFromOptions() Level: beginner .seealso: VecCreate(), VecSetType(), VecSetFromOptions(), VecCreateMPIWithArray(), VECSEQCUDA, VECMPICUDA, VECSTANDARD, VecType, VecCreateMPI(), VecSetPinnedMemoryMin() M*/ PetscErrorCode VecDestroy_MPICUDA(Vec v) { Vec_MPI *vecmpi = (Vec_MPI*)v->data; Vec_CUDA *veccuda; PetscErrorCode ierr; hipError_t err; PetscFunctionBegin; if (v->spptr) { veccuda = (Vec_CUDA*)v->spptr; if (veccuda->GPUarray_allocated) { err = hipFree(((Vec_CUDA*)v->spptr)->GPUarray_allocated);CHKERRCUDA(err); veccuda->GPUarray_allocated = NULL; } if (veccuda->stream) { err = hipStreamDestroy(((Vec_CUDA*)v->spptr)->stream);CHKERRCUDA(err); } if (v->pinned_memory) { ierr = PetscMallocSetCUDAHost();CHKERRQ(ierr); ierr = PetscFree(vecmpi->array_allocated);CHKERRQ(ierr); ierr = PetscMallocResetCUDAHost();CHKERRQ(ierr); v->pinned_memory = PETSC_FALSE; } ierr = PetscFree(v->spptr);CHKERRQ(ierr); } ierr = VecDestroy_MPI(v);CHKERRQ(ierr); PetscFunctionReturn(0); } PetscErrorCode VecNorm_MPICUDA(Vec xin,NormType type,PetscReal *z) { PetscReal sum,work = 0.0; PetscErrorCode ierr; PetscFunctionBegin; if (type == NORM_2 || type == NORM_FROBENIUS) { ierr = VecNorm_SeqCUDA(xin,NORM_2,&work); work *= work; ierr = MPIU_Allreduce(&work,&sum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); *z = PetscSqrtReal(sum); } else if (type == NORM_1) { /* Find the local part */ ierr = VecNorm_SeqCUDA(xin,NORM_1,&work);CHKERRQ(ierr); /* Find the global max */ ierr = MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); } else if (type == NORM_INFINITY) { /* Find the local max */ ierr = VecNorm_SeqCUDA(xin,NORM_INFINITY,&work);CHKERRQ(ierr); /* Find the global max */ ierr = MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); } else if (type == NORM_1_AND_2) { PetscReal temp[2]; ierr = VecNorm_SeqCUDA(xin,NORM_1,temp);CHKERRQ(ierr); ierr = VecNorm_SeqCUDA(xin,NORM_2,temp+1);CHKERRQ(ierr); temp[1] = temp[1]*temp[1]; ierr = MPIU_Allreduce(temp,z,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); z[1] = PetscSqrtReal(z[1]); } PetscFunctionReturn(0); } PetscErrorCode VecDot_MPICUDA(Vec xin,Vec yin,PetscScalar *z) { PetscScalar sum,work; PetscErrorCode ierr; PetscFunctionBegin; ierr = VecDot_SeqCUDA(xin,yin,&work);CHKERRQ(ierr); ierr = MPIU_Allreduce(&work,&sum,1,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); *z = sum; PetscFunctionReturn(0); } PetscErrorCode VecTDot_MPICUDA(Vec xin,Vec yin,PetscScalar *z) { PetscScalar sum,work; PetscErrorCode ierr; PetscFunctionBegin; ierr = VecTDot_SeqCUDA(xin,yin,&work);CHKERRQ(ierr); ierr = MPIU_Allreduce(&work,&sum,1,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); *z = sum; PetscFunctionReturn(0); } PetscErrorCode VecMDot_MPICUDA(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z) { PetscScalar awork[128],*work = awork; PetscErrorCode ierr; PetscFunctionBegin; if (nv > 128) { ierr = PetscMalloc1(nv,&work);CHKERRQ(ierr); } ierr = VecMDot_SeqCUDA(xin,nv,y,work);CHKERRQ(ierr); ierr = MPIU_Allreduce(work,z,nv,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); if (nv > 128) { ierr = PetscFree(work);CHKERRQ(ierr); } PetscFunctionReturn(0); } /*MC VECMPICUDA - VECMPICUDA = "mpicuda" - The basic parallel vector, modified to use CUDA Options Database Keys: . -vec_type mpicuda - sets the vector type to VECMPICUDA during a call to VecSetFromOptions() Level: beginner .seealso: VecCreate(), VecSetType(), VecSetFromOptions(), VecCreateMPIWithArray(), VECMPI, VecType, VecCreateMPI(), VecSetPinnedMemoryMin() M*/ PetscErrorCode VecDuplicate_MPICUDA(Vec win,Vec *v) { PetscErrorCode ierr; Vec_MPI *vw,*w = (Vec_MPI*)win->data; PetscScalar *array; PetscFunctionBegin; ierr = VecCreate(PetscObjectComm((PetscObject)win),v);CHKERRQ(ierr); ierr = PetscLayoutReference(win->map,&(*v)->map);CHKERRQ(ierr); ierr = VecCreate_MPICUDA_Private(*v,PETSC_TRUE,w->nghost,0);CHKERRQ(ierr); vw = (Vec_MPI*)(*v)->data; ierr = PetscMemcpy((*v)->ops,win->ops,sizeof(struct _VecOps));CHKERRQ(ierr); /* save local representation of the parallel vector (and scatter) if it exists */ if (w->localrep) { ierr = VecGetArray(*v,&array);CHKERRQ(ierr); ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,1,win->map->n+w->nghost,array,&vw->localrep);CHKERRQ(ierr); ierr = PetscMemcpy(vw->localrep->ops,w->localrep->ops,sizeof(struct _VecOps));CHKERRQ(ierr); ierr = VecRestoreArray(*v,&array);CHKERRQ(ierr); ierr = PetscLogObjectParent((PetscObject)*v,(PetscObject)vw->localrep);CHKERRQ(ierr); vw->localupdate = w->localupdate; if (vw->localupdate) { ierr = PetscObjectReference((PetscObject)vw->localupdate);CHKERRQ(ierr); } } /* New vector should inherit stashing property of parent */ (*v)->stash.donotstash = win->stash.donotstash; (*v)->stash.ignorenegidx = win->stash.ignorenegidx; /* change type_name appropriately */ ierr = VecCUDAAllocateCheck(*v);CHKERRQ(ierr); ierr = PetscObjectChangeTypeName((PetscObject)(*v),VECMPICUDA);CHKERRQ(ierr); ierr = PetscObjectListDuplicate(((PetscObject)win)->olist,&((PetscObject)(*v))->olist);CHKERRQ(ierr); ierr = PetscFunctionListDuplicate(((PetscObject)win)->qlist,&((PetscObject)(*v))->qlist);CHKERRQ(ierr); (*v)->map->bs = PetscAbs(win->map->bs); (*v)->bstash.bs = win->bstash.bs; PetscFunctionReturn(0); } PetscErrorCode VecDotNorm2_MPICUDA(Vec s,Vec t,PetscScalar *dp,PetscScalar *nm) { PetscErrorCode ierr; PetscScalar work[2],sum[2]; PetscFunctionBegin; ierr = VecDotNorm2_SeqCUDA(s,t,work,work+1);CHKERRQ(ierr); ierr = MPIU_Allreduce(&work,&sum,2,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)s));CHKERRQ(ierr); *dp = sum[0]; *nm = sum[1]; PetscFunctionReturn(0); } PetscErrorCode VecCreate_MPICUDA(Vec vv) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscCUDAInitializeCheck();CHKERRQ(ierr); ierr = PetscLayoutSetUp(vv->map);CHKERRQ(ierr); ierr = VecCUDAAllocateCheck(vv);CHKERRQ(ierr); ierr = VecCreate_MPICUDA_Private(vv,PETSC_FALSE,0,((Vec_CUDA*)vv->spptr)->GPUarray_allocated);CHKERRQ(ierr); ierr = VecCUDAAllocateCheckHost(vv);CHKERRQ(ierr); ierr = VecSet(vv,0.0);CHKERRQ(ierr); ierr = VecSet_Seq(vv,0.0);CHKERRQ(ierr); vv->offloadmask = PETSC_OFFLOAD_BOTH; PetscFunctionReturn(0); } PetscErrorCode VecCreate_CUDA(Vec v) { PetscErrorCode ierr; PetscMPIInt size; PetscFunctionBegin; ierr = MPI_Comm_size(PetscObjectComm((PetscObject)v),&size);CHKERRMPI(ierr); if (size == 1) { ierr = VecSetType(v,VECSEQCUDA);CHKERRQ(ierr); } else { ierr = VecSetType(v,VECMPICUDA);CHKERRQ(ierr); } PetscFunctionReturn(0); } /*@C VecCreateMPICUDAWithArray - Creates a parallel, array-style vector, where the user provides the GPU array space to store the vector values. Collective Input Parameters: + comm - the MPI communicator to use . bs - block size, same meaning as VecSetBlockSize() . n - local vector length, cannot be PETSC_DECIDE . N - global vector length (or PETSC_DECIDE to have calculated) - array - the user provided GPU array to store the vector values Output Parameter: . vv - the vector Notes: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the same type as an existing vector. If the user-provided array is NULL, then VecCUDAPlaceArray() can be used at a later stage to SET the array for storing the vector values. PETSc does NOT free the array when the vector is destroyed via VecDestroy(). The user should not free the array until the vector is destroyed. Level: intermediate .seealso: VecCreateSeqCUDAWithArray(), VecCreateMPIWithArray(), VecCreateSeqWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost(), VecCreateMPI(), VecCreateGhostWithArray(), VecPlaceArray() @*/ PetscErrorCode VecCreateMPICUDAWithArray(MPI_Comm comm,PetscInt bs,PetscInt n,PetscInt N,const PetscScalar array[],Vec *vv) { PetscErrorCode ierr; PetscFunctionBegin; if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must set local size of vector"); ierr = PetscCUDAInitializeCheck();CHKERRQ(ierr); ierr = VecCreate(comm,vv);CHKERRQ(ierr); ierr = VecSetSizes(*vv,n,N);CHKERRQ(ierr); ierr = VecSetBlockSize(*vv,bs);CHKERRQ(ierr); ierr = VecCreate_MPICUDA_Private(*vv,PETSC_FALSE,0,array);CHKERRQ(ierr); PetscFunctionReturn(0); } /*@C VecCreateMPICUDAWithArrays - Creates a parallel, array-style vector, where the user provides the GPU array space to store the vector values. Collective Input Parameters: + comm - the MPI communicator to use . bs - block size, same meaning as VecSetBlockSize() . n - local vector length, cannot be PETSC_DECIDE . N - global vector length (or PETSC_DECIDE to have calculated) - cpuarray - the user provided CPU array to store the vector values - gpuarray - the user provided GPU array to store the vector values Output Parameter: . vv - the vector Notes: If both cpuarray and gpuarray are provided, the caller must ensure that the provided arrays have identical values. Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the same type as an existing vector. PETSc does NOT free the provided arrays when the vector is destroyed via VecDestroy(). The user should not free the array until the vector is destroyed. Level: intermediate .seealso: VecCreateSeqCUDAWithArrays(), VecCreateMPIWithArray(), VecCreateSeqWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost(), VecCreateMPI(), VecCreateGhostWithArray(), VecCUDAPlaceArray(), VecPlaceArray(), VecCUDAAllocateCheckHost() @*/ PetscErrorCode VecCreateMPICUDAWithArrays(MPI_Comm comm,PetscInt bs,PetscInt n,PetscInt N,const PetscScalar cpuarray[],const PetscScalar gpuarray[],Vec *vv) { PetscErrorCode ierr; PetscFunctionBegin; ierr = VecCreateMPICUDAWithArray(comm,bs,n,N,gpuarray,vv);CHKERRQ(ierr); if (cpuarray && gpuarray) { Vec_MPI *s = (Vec_MPI*)((*vv)->data); s->array = (PetscScalar*)cpuarray; (*vv)->offloadmask = PETSC_OFFLOAD_BOTH; } else if (cpuarray) { Vec_MPI *s = (Vec_MPI*)((*vv)->data); s->array = (PetscScalar*)cpuarray; (*vv)->offloadmask = PETSC_OFFLOAD_CPU; } else if (gpuarray) { (*vv)->offloadmask = PETSC_OFFLOAD_GPU; } else { (*vv)->offloadmask = PETSC_OFFLOAD_UNALLOCATED; } PetscFunctionReturn(0); } PetscErrorCode VecBindToCPU_MPICUDA(Vec V,PetscBool pin) { PetscErrorCode ierr; PetscFunctionBegin; V->boundtocpu = pin; if (pin) { ierr = VecCUDACopyFromGPU(V);CHKERRQ(ierr); V->offloadmask = PETSC_OFFLOAD_CPU; /* since the CPU code will likely change values in the vector */ V->ops->dotnorm2 = NULL; V->ops->waxpy = VecWAXPY_Seq; V->ops->dot = VecDot_MPI; V->ops->mdot = VecMDot_MPI; V->ops->tdot = VecTDot_MPI; V->ops->norm = VecNorm_MPI; V->ops->scale = VecScale_Seq; V->ops->copy = VecCopy_Seq; V->ops->set = VecSet_Seq; V->ops->swap = VecSwap_Seq; V->ops->axpy = VecAXPY_Seq; V->ops->axpby = VecAXPBY_Seq; V->ops->maxpy = VecMAXPY_Seq; V->ops->aypx = VecAYPX_Seq; V->ops->axpbypcz = VecAXPBYPCZ_Seq; V->ops->pointwisemult = VecPointwiseMult_Seq; V->ops->setrandom = VecSetRandom_Seq; V->ops->placearray = VecPlaceArray_Seq; V->ops->replacearray = VecReplaceArray_SeqCUDA; V->ops->resetarray = VecResetArray_Seq; V->ops->dot_local = VecDot_Seq; V->ops->tdot_local = VecTDot_Seq; V->ops->norm_local = VecNorm_Seq; V->ops->mdot_local = VecMDot_Seq; V->ops->pointwisedivide = VecPointwiseDivide_Seq; V->ops->getlocalvector = NULL; V->ops->restorelocalvector = NULL; V->ops->getlocalvectorread = NULL; V->ops->restorelocalvectorread = NULL; V->ops->getarraywrite = NULL; } else { V->ops->dotnorm2 = VecDotNorm2_MPICUDA; V->ops->waxpy = VecWAXPY_SeqCUDA; V->ops->duplicate = VecDuplicate_MPICUDA; V->ops->dot = VecDot_MPICUDA; V->ops->mdot = VecMDot_MPICUDA; V->ops->tdot = VecTDot_MPICUDA; V->ops->norm = VecNorm_MPICUDA; V->ops->scale = VecScale_SeqCUDA; V->ops->copy = VecCopy_SeqCUDA; V->ops->set = VecSet_SeqCUDA; V->ops->swap = VecSwap_SeqCUDA; V->ops->axpy = VecAXPY_SeqCUDA; V->ops->axpby = VecAXPBY_SeqCUDA; V->ops->maxpy = VecMAXPY_SeqCUDA; V->ops->aypx = VecAYPX_SeqCUDA; V->ops->axpbypcz = VecAXPBYPCZ_SeqCUDA; V->ops->pointwisemult = VecPointwiseMult_SeqCUDA; V->ops->setrandom = VecSetRandom_SeqCUDA; V->ops->placearray = VecPlaceArray_SeqCUDA; V->ops->replacearray = VecReplaceArray_SeqCUDA; V->ops->resetarray = VecResetArray_SeqCUDA; V->ops->dot_local = VecDot_SeqCUDA; V->ops->tdot_local = VecTDot_SeqCUDA; V->ops->norm_local = VecNorm_SeqCUDA; V->ops->mdot_local = VecMDot_SeqCUDA; V->ops->destroy = VecDestroy_MPICUDA; V->ops->pointwisedivide = VecPointwiseDivide_SeqCUDA; V->ops->getlocalvector = VecGetLocalVector_SeqCUDA; V->ops->restorelocalvector = VecRestoreLocalVector_SeqCUDA; V->ops->getlocalvectorread = VecGetLocalVector_SeqCUDA; V->ops->restorelocalvectorread = VecRestoreLocalVector_SeqCUDA; V->ops->getarraywrite = VecGetArrayWrite_SeqCUDA; V->ops->getarray = VecGetArray_SeqCUDA; V->ops->restorearray = VecRestoreArray_SeqCUDA; V->ops->getarrayandmemtype = VecGetArrayAndMemType_SeqCUDA; V->ops->restorearrayandmemtype = VecRestoreArrayAndMemType_SeqCUDA; } PetscFunctionReturn(0); } PetscErrorCode VecCreate_MPICUDA_Private(Vec vv,PetscBool alloc,PetscInt nghost,const PetscScalar array[]) { PetscErrorCode ierr; Vec_CUDA *veccuda; PetscFunctionBegin; ierr = VecCreate_MPI_Private(vv,PETSC_FALSE,0,0);CHKERRQ(ierr); ierr = PetscObjectChangeTypeName((PetscObject)vv,VECMPICUDA);CHKERRQ(ierr); ierr = VecBindToCPU_MPICUDA(vv,PETSC_FALSE);CHKERRQ(ierr); vv->ops->bindtocpu = VecBindToCPU_MPICUDA; /* Later, functions check for the Vec_CUDA structure existence, so do not create it without array */ if (alloc && !array) { ierr = VecCUDAAllocateCheck(vv);CHKERRQ(ierr); ierr = VecCUDAAllocateCheckHost(vv);CHKERRQ(ierr); ierr = VecSet(vv,0.0);CHKERRQ(ierr); ierr = VecSet_Seq(vv,0.0);CHKERRQ(ierr); vv->offloadmask = PETSC_OFFLOAD_BOTH; } if (array) { if (!vv->spptr) { PetscReal pinned_memory_min; PetscBool flag; /* Cannot use PetscNew() here because spptr is void* */ ierr = PetscMalloc(sizeof(Vec_CUDA),&vv->spptr);CHKERRQ(ierr); veccuda = (Vec_CUDA*)vv->spptr; veccuda->stream = 0; /* using default stream */ veccuda->GPUarray_allocated = 0; vv->offloadmask = PETSC_OFFLOAD_UNALLOCATED; vv->minimum_bytes_pinned_memory = 0; /* Need to parse command line for minimum size to use for pinned memory allocations on host here. Note: This same code duplicated in VecCreate_SeqCUDA_Private() and VecCUDAAllocateCheck(). Is there a good way to avoid this? */ ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)vv),((PetscObject)vv)->prefix,"VECCUDA Options","Vec");CHKERRQ(ierr); pinned_memory_min = vv->minimum_bytes_pinned_memory; ierr = PetscOptionsReal("-vec_pinned_memory_min","Minimum size (in bytes) for an allocation to use pinned memory on host","VecSetPinnedMemoryMin",pinned_memory_min,&pinned_memory_min,&flag);CHKERRQ(ierr); if (flag) vv->minimum_bytes_pinned_memory = pinned_memory_min; ierr = PetscOptionsEnd();CHKERRQ(ierr); } veccuda = (Vec_CUDA*)vv->spptr; veccuda->GPUarray = (PetscScalar*)array; } PetscFunctionReturn(0); }
5510e6d13595b853f9f73cc4ccfbbcf13d80f7c5.cu
/* This file contains routines for Parallel vector operations. */ #define PETSC_SKIP_SPINLOCK #define PETSC_SKIP_CXX_COMPLEX_FIX #include <petscconf.h> #include <../src/vec/vec/impls/mpi/pvecimpl.h> /*I "petscvec.h" I*/ #include <petsc/private/cudavecimpl.h> /*MC VECCUDA - VECCUDA = "cuda" - A VECSEQCUDA on a single-process communicator, and VECMPICUDA otherwise. Options Database Keys: . -vec_type cuda - sets the vector type to VECCUDA during a call to VecSetFromOptions() Level: beginner .seealso: VecCreate(), VecSetType(), VecSetFromOptions(), VecCreateMPIWithArray(), VECSEQCUDA, VECMPICUDA, VECSTANDARD, VecType, VecCreateMPI(), VecSetPinnedMemoryMin() M*/ PetscErrorCode VecDestroy_MPICUDA(Vec v) { Vec_MPI *vecmpi = (Vec_MPI*)v->data; Vec_CUDA *veccuda; PetscErrorCode ierr; cudaError_t err; PetscFunctionBegin; if (v->spptr) { veccuda = (Vec_CUDA*)v->spptr; if (veccuda->GPUarray_allocated) { err = cudaFree(((Vec_CUDA*)v->spptr)->GPUarray_allocated);CHKERRCUDA(err); veccuda->GPUarray_allocated = NULL; } if (veccuda->stream) { err = cudaStreamDestroy(((Vec_CUDA*)v->spptr)->stream);CHKERRCUDA(err); } if (v->pinned_memory) { ierr = PetscMallocSetCUDAHost();CHKERRQ(ierr); ierr = PetscFree(vecmpi->array_allocated);CHKERRQ(ierr); ierr = PetscMallocResetCUDAHost();CHKERRQ(ierr); v->pinned_memory = PETSC_FALSE; } ierr = PetscFree(v->spptr);CHKERRQ(ierr); } ierr = VecDestroy_MPI(v);CHKERRQ(ierr); PetscFunctionReturn(0); } PetscErrorCode VecNorm_MPICUDA(Vec xin,NormType type,PetscReal *z) { PetscReal sum,work = 0.0; PetscErrorCode ierr; PetscFunctionBegin; if (type == NORM_2 || type == NORM_FROBENIUS) { ierr = VecNorm_SeqCUDA(xin,NORM_2,&work); work *= work; ierr = MPIU_Allreduce(&work,&sum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); *z = PetscSqrtReal(sum); } else if (type == NORM_1) { /* Find the local part */ ierr = VecNorm_SeqCUDA(xin,NORM_1,&work);CHKERRQ(ierr); /* Find the global max */ ierr = MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); } else if (type == NORM_INFINITY) { /* Find the local max */ ierr = VecNorm_SeqCUDA(xin,NORM_INFINITY,&work);CHKERRQ(ierr); /* Find the global max */ ierr = MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); } else if (type == NORM_1_AND_2) { PetscReal temp[2]; ierr = VecNorm_SeqCUDA(xin,NORM_1,temp);CHKERRQ(ierr); ierr = VecNorm_SeqCUDA(xin,NORM_2,temp+1);CHKERRQ(ierr); temp[1] = temp[1]*temp[1]; ierr = MPIU_Allreduce(temp,z,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); z[1] = PetscSqrtReal(z[1]); } PetscFunctionReturn(0); } PetscErrorCode VecDot_MPICUDA(Vec xin,Vec yin,PetscScalar *z) { PetscScalar sum,work; PetscErrorCode ierr; PetscFunctionBegin; ierr = VecDot_SeqCUDA(xin,yin,&work);CHKERRQ(ierr); ierr = MPIU_Allreduce(&work,&sum,1,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); *z = sum; PetscFunctionReturn(0); } PetscErrorCode VecTDot_MPICUDA(Vec xin,Vec yin,PetscScalar *z) { PetscScalar sum,work; PetscErrorCode ierr; PetscFunctionBegin; ierr = VecTDot_SeqCUDA(xin,yin,&work);CHKERRQ(ierr); ierr = MPIU_Allreduce(&work,&sum,1,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); *z = sum; PetscFunctionReturn(0); } PetscErrorCode VecMDot_MPICUDA(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z) { PetscScalar awork[128],*work = awork; PetscErrorCode ierr; PetscFunctionBegin; if (nv > 128) { ierr = PetscMalloc1(nv,&work);CHKERRQ(ierr); } ierr = VecMDot_SeqCUDA(xin,nv,y,work);CHKERRQ(ierr); ierr = MPIU_Allreduce(work,z,nv,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr); if (nv > 128) { ierr = PetscFree(work);CHKERRQ(ierr); } PetscFunctionReturn(0); } /*MC VECMPICUDA - VECMPICUDA = "mpicuda" - The basic parallel vector, modified to use CUDA Options Database Keys: . -vec_type mpicuda - sets the vector type to VECMPICUDA during a call to VecSetFromOptions() Level: beginner .seealso: VecCreate(), VecSetType(), VecSetFromOptions(), VecCreateMPIWithArray(), VECMPI, VecType, VecCreateMPI(), VecSetPinnedMemoryMin() M*/ PetscErrorCode VecDuplicate_MPICUDA(Vec win,Vec *v) { PetscErrorCode ierr; Vec_MPI *vw,*w = (Vec_MPI*)win->data; PetscScalar *array; PetscFunctionBegin; ierr = VecCreate(PetscObjectComm((PetscObject)win),v);CHKERRQ(ierr); ierr = PetscLayoutReference(win->map,&(*v)->map);CHKERRQ(ierr); ierr = VecCreate_MPICUDA_Private(*v,PETSC_TRUE,w->nghost,0);CHKERRQ(ierr); vw = (Vec_MPI*)(*v)->data; ierr = PetscMemcpy((*v)->ops,win->ops,sizeof(struct _VecOps));CHKERRQ(ierr); /* save local representation of the parallel vector (and scatter) if it exists */ if (w->localrep) { ierr = VecGetArray(*v,&array);CHKERRQ(ierr); ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,1,win->map->n+w->nghost,array,&vw->localrep);CHKERRQ(ierr); ierr = PetscMemcpy(vw->localrep->ops,w->localrep->ops,sizeof(struct _VecOps));CHKERRQ(ierr); ierr = VecRestoreArray(*v,&array);CHKERRQ(ierr); ierr = PetscLogObjectParent((PetscObject)*v,(PetscObject)vw->localrep);CHKERRQ(ierr); vw->localupdate = w->localupdate; if (vw->localupdate) { ierr = PetscObjectReference((PetscObject)vw->localupdate);CHKERRQ(ierr); } } /* New vector should inherit stashing property of parent */ (*v)->stash.donotstash = win->stash.donotstash; (*v)->stash.ignorenegidx = win->stash.ignorenegidx; /* change type_name appropriately */ ierr = VecCUDAAllocateCheck(*v);CHKERRQ(ierr); ierr = PetscObjectChangeTypeName((PetscObject)(*v),VECMPICUDA);CHKERRQ(ierr); ierr = PetscObjectListDuplicate(((PetscObject)win)->olist,&((PetscObject)(*v))->olist);CHKERRQ(ierr); ierr = PetscFunctionListDuplicate(((PetscObject)win)->qlist,&((PetscObject)(*v))->qlist);CHKERRQ(ierr); (*v)->map->bs = PetscAbs(win->map->bs); (*v)->bstash.bs = win->bstash.bs; PetscFunctionReturn(0); } PetscErrorCode VecDotNorm2_MPICUDA(Vec s,Vec t,PetscScalar *dp,PetscScalar *nm) { PetscErrorCode ierr; PetscScalar work[2],sum[2]; PetscFunctionBegin; ierr = VecDotNorm2_SeqCUDA(s,t,work,work+1);CHKERRQ(ierr); ierr = MPIU_Allreduce(&work,&sum,2,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)s));CHKERRQ(ierr); *dp = sum[0]; *nm = sum[1]; PetscFunctionReturn(0); } PetscErrorCode VecCreate_MPICUDA(Vec vv) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscCUDAInitializeCheck();CHKERRQ(ierr); ierr = PetscLayoutSetUp(vv->map);CHKERRQ(ierr); ierr = VecCUDAAllocateCheck(vv);CHKERRQ(ierr); ierr = VecCreate_MPICUDA_Private(vv,PETSC_FALSE,0,((Vec_CUDA*)vv->spptr)->GPUarray_allocated);CHKERRQ(ierr); ierr = VecCUDAAllocateCheckHost(vv);CHKERRQ(ierr); ierr = VecSet(vv,0.0);CHKERRQ(ierr); ierr = VecSet_Seq(vv,0.0);CHKERRQ(ierr); vv->offloadmask = PETSC_OFFLOAD_BOTH; PetscFunctionReturn(0); } PetscErrorCode VecCreate_CUDA(Vec v) { PetscErrorCode ierr; PetscMPIInt size; PetscFunctionBegin; ierr = MPI_Comm_size(PetscObjectComm((PetscObject)v),&size);CHKERRMPI(ierr); if (size == 1) { ierr = VecSetType(v,VECSEQCUDA);CHKERRQ(ierr); } else { ierr = VecSetType(v,VECMPICUDA);CHKERRQ(ierr); } PetscFunctionReturn(0); } /*@C VecCreateMPICUDAWithArray - Creates a parallel, array-style vector, where the user provides the GPU array space to store the vector values. Collective Input Parameters: + comm - the MPI communicator to use . bs - block size, same meaning as VecSetBlockSize() . n - local vector length, cannot be PETSC_DECIDE . N - global vector length (or PETSC_DECIDE to have calculated) - array - the user provided GPU array to store the vector values Output Parameter: . vv - the vector Notes: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the same type as an existing vector. If the user-provided array is NULL, then VecCUDAPlaceArray() can be used at a later stage to SET the array for storing the vector values. PETSc does NOT free the array when the vector is destroyed via VecDestroy(). The user should not free the array until the vector is destroyed. Level: intermediate .seealso: VecCreateSeqCUDAWithArray(), VecCreateMPIWithArray(), VecCreateSeqWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost(), VecCreateMPI(), VecCreateGhostWithArray(), VecPlaceArray() @*/ PetscErrorCode VecCreateMPICUDAWithArray(MPI_Comm comm,PetscInt bs,PetscInt n,PetscInt N,const PetscScalar array[],Vec *vv) { PetscErrorCode ierr; PetscFunctionBegin; if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must set local size of vector"); ierr = PetscCUDAInitializeCheck();CHKERRQ(ierr); ierr = VecCreate(comm,vv);CHKERRQ(ierr); ierr = VecSetSizes(*vv,n,N);CHKERRQ(ierr); ierr = VecSetBlockSize(*vv,bs);CHKERRQ(ierr); ierr = VecCreate_MPICUDA_Private(*vv,PETSC_FALSE,0,array);CHKERRQ(ierr); PetscFunctionReturn(0); } /*@C VecCreateMPICUDAWithArrays - Creates a parallel, array-style vector, where the user provides the GPU array space to store the vector values. Collective Input Parameters: + comm - the MPI communicator to use . bs - block size, same meaning as VecSetBlockSize() . n - local vector length, cannot be PETSC_DECIDE . N - global vector length (or PETSC_DECIDE to have calculated) - cpuarray - the user provided CPU array to store the vector values - gpuarray - the user provided GPU array to store the vector values Output Parameter: . vv - the vector Notes: If both cpuarray and gpuarray are provided, the caller must ensure that the provided arrays have identical values. Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the same type as an existing vector. PETSc does NOT free the provided arrays when the vector is destroyed via VecDestroy(). The user should not free the array until the vector is destroyed. Level: intermediate .seealso: VecCreateSeqCUDAWithArrays(), VecCreateMPIWithArray(), VecCreateSeqWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost(), VecCreateMPI(), VecCreateGhostWithArray(), VecCUDAPlaceArray(), VecPlaceArray(), VecCUDAAllocateCheckHost() @*/ PetscErrorCode VecCreateMPICUDAWithArrays(MPI_Comm comm,PetscInt bs,PetscInt n,PetscInt N,const PetscScalar cpuarray[],const PetscScalar gpuarray[],Vec *vv) { PetscErrorCode ierr; PetscFunctionBegin; ierr = VecCreateMPICUDAWithArray(comm,bs,n,N,gpuarray,vv);CHKERRQ(ierr); if (cpuarray && gpuarray) { Vec_MPI *s = (Vec_MPI*)((*vv)->data); s->array = (PetscScalar*)cpuarray; (*vv)->offloadmask = PETSC_OFFLOAD_BOTH; } else if (cpuarray) { Vec_MPI *s = (Vec_MPI*)((*vv)->data); s->array = (PetscScalar*)cpuarray; (*vv)->offloadmask = PETSC_OFFLOAD_CPU; } else if (gpuarray) { (*vv)->offloadmask = PETSC_OFFLOAD_GPU; } else { (*vv)->offloadmask = PETSC_OFFLOAD_UNALLOCATED; } PetscFunctionReturn(0); } PetscErrorCode VecBindToCPU_MPICUDA(Vec V,PetscBool pin) { PetscErrorCode ierr; PetscFunctionBegin; V->boundtocpu = pin; if (pin) { ierr = VecCUDACopyFromGPU(V);CHKERRQ(ierr); V->offloadmask = PETSC_OFFLOAD_CPU; /* since the CPU code will likely change values in the vector */ V->ops->dotnorm2 = NULL; V->ops->waxpy = VecWAXPY_Seq; V->ops->dot = VecDot_MPI; V->ops->mdot = VecMDot_MPI; V->ops->tdot = VecTDot_MPI; V->ops->norm = VecNorm_MPI; V->ops->scale = VecScale_Seq; V->ops->copy = VecCopy_Seq; V->ops->set = VecSet_Seq; V->ops->swap = VecSwap_Seq; V->ops->axpy = VecAXPY_Seq; V->ops->axpby = VecAXPBY_Seq; V->ops->maxpy = VecMAXPY_Seq; V->ops->aypx = VecAYPX_Seq; V->ops->axpbypcz = VecAXPBYPCZ_Seq; V->ops->pointwisemult = VecPointwiseMult_Seq; V->ops->setrandom = VecSetRandom_Seq; V->ops->placearray = VecPlaceArray_Seq; V->ops->replacearray = VecReplaceArray_SeqCUDA; V->ops->resetarray = VecResetArray_Seq; V->ops->dot_local = VecDot_Seq; V->ops->tdot_local = VecTDot_Seq; V->ops->norm_local = VecNorm_Seq; V->ops->mdot_local = VecMDot_Seq; V->ops->pointwisedivide = VecPointwiseDivide_Seq; V->ops->getlocalvector = NULL; V->ops->restorelocalvector = NULL; V->ops->getlocalvectorread = NULL; V->ops->restorelocalvectorread = NULL; V->ops->getarraywrite = NULL; } else { V->ops->dotnorm2 = VecDotNorm2_MPICUDA; V->ops->waxpy = VecWAXPY_SeqCUDA; V->ops->duplicate = VecDuplicate_MPICUDA; V->ops->dot = VecDot_MPICUDA; V->ops->mdot = VecMDot_MPICUDA; V->ops->tdot = VecTDot_MPICUDA; V->ops->norm = VecNorm_MPICUDA; V->ops->scale = VecScale_SeqCUDA; V->ops->copy = VecCopy_SeqCUDA; V->ops->set = VecSet_SeqCUDA; V->ops->swap = VecSwap_SeqCUDA; V->ops->axpy = VecAXPY_SeqCUDA; V->ops->axpby = VecAXPBY_SeqCUDA; V->ops->maxpy = VecMAXPY_SeqCUDA; V->ops->aypx = VecAYPX_SeqCUDA; V->ops->axpbypcz = VecAXPBYPCZ_SeqCUDA; V->ops->pointwisemult = VecPointwiseMult_SeqCUDA; V->ops->setrandom = VecSetRandom_SeqCUDA; V->ops->placearray = VecPlaceArray_SeqCUDA; V->ops->replacearray = VecReplaceArray_SeqCUDA; V->ops->resetarray = VecResetArray_SeqCUDA; V->ops->dot_local = VecDot_SeqCUDA; V->ops->tdot_local = VecTDot_SeqCUDA; V->ops->norm_local = VecNorm_SeqCUDA; V->ops->mdot_local = VecMDot_SeqCUDA; V->ops->destroy = VecDestroy_MPICUDA; V->ops->pointwisedivide = VecPointwiseDivide_SeqCUDA; V->ops->getlocalvector = VecGetLocalVector_SeqCUDA; V->ops->restorelocalvector = VecRestoreLocalVector_SeqCUDA; V->ops->getlocalvectorread = VecGetLocalVector_SeqCUDA; V->ops->restorelocalvectorread = VecRestoreLocalVector_SeqCUDA; V->ops->getarraywrite = VecGetArrayWrite_SeqCUDA; V->ops->getarray = VecGetArray_SeqCUDA; V->ops->restorearray = VecRestoreArray_SeqCUDA; V->ops->getarrayandmemtype = VecGetArrayAndMemType_SeqCUDA; V->ops->restorearrayandmemtype = VecRestoreArrayAndMemType_SeqCUDA; } PetscFunctionReturn(0); } PetscErrorCode VecCreate_MPICUDA_Private(Vec vv,PetscBool alloc,PetscInt nghost,const PetscScalar array[]) { PetscErrorCode ierr; Vec_CUDA *veccuda; PetscFunctionBegin; ierr = VecCreate_MPI_Private(vv,PETSC_FALSE,0,0);CHKERRQ(ierr); ierr = PetscObjectChangeTypeName((PetscObject)vv,VECMPICUDA);CHKERRQ(ierr); ierr = VecBindToCPU_MPICUDA(vv,PETSC_FALSE);CHKERRQ(ierr); vv->ops->bindtocpu = VecBindToCPU_MPICUDA; /* Later, functions check for the Vec_CUDA structure existence, so do not create it without array */ if (alloc && !array) { ierr = VecCUDAAllocateCheck(vv);CHKERRQ(ierr); ierr = VecCUDAAllocateCheckHost(vv);CHKERRQ(ierr); ierr = VecSet(vv,0.0);CHKERRQ(ierr); ierr = VecSet_Seq(vv,0.0);CHKERRQ(ierr); vv->offloadmask = PETSC_OFFLOAD_BOTH; } if (array) { if (!vv->spptr) { PetscReal pinned_memory_min; PetscBool flag; /* Cannot use PetscNew() here because spptr is void* */ ierr = PetscMalloc(sizeof(Vec_CUDA),&vv->spptr);CHKERRQ(ierr); veccuda = (Vec_CUDA*)vv->spptr; veccuda->stream = 0; /* using default stream */ veccuda->GPUarray_allocated = 0; vv->offloadmask = PETSC_OFFLOAD_UNALLOCATED; vv->minimum_bytes_pinned_memory = 0; /* Need to parse command line for minimum size to use for pinned memory allocations on host here. Note: This same code duplicated in VecCreate_SeqCUDA_Private() and VecCUDAAllocateCheck(). Is there a good way to avoid this? */ ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)vv),((PetscObject)vv)->prefix,"VECCUDA Options","Vec");CHKERRQ(ierr); pinned_memory_min = vv->minimum_bytes_pinned_memory; ierr = PetscOptionsReal("-vec_pinned_memory_min","Minimum size (in bytes) for an allocation to use pinned memory on host","VecSetPinnedMemoryMin",pinned_memory_min,&pinned_memory_min,&flag);CHKERRQ(ierr); if (flag) vv->minimum_bytes_pinned_memory = pinned_memory_min; ierr = PetscOptionsEnd();CHKERRQ(ierr); } veccuda = (Vec_CUDA*)vv->spptr; veccuda->GPUarray = (PetscScalar*)array; } PetscFunctionReturn(0); }
eb16baf537cb975db561965758e154f4d8495ea4.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" //////////////////////////////////////////////////////////////////////////// // // Copyright 1993-2015 NVIDIA Corporation. All rights reserved. // // Please refer to the NVIDIA end user license agreement (EULA) associated // with this source code for terms and conditions that govern your use of // this software. Any use, reproduction, disclosure, or distribution of // this software and related documentation outside the terms of the EULA // is strictly prohibited. // //////////////////////////////////////////////////////////////////////////// // ---------------------------------------------------------------------------------------- // Transpose // // This file contains both device and host code for transposing a floating-point // matrix. It performs several transpose kernels, which incrementally improve performance // through coalescing, removing shared memory bank conflicts, and eliminating partition // camping. Several of the kernels perform a copy, used to represent the best case // performance that a transpose can achieve. // // Please see the whitepaper in the docs folder of the transpose project for a detailed // description of this performance study. // ---------------------------------------------------------------------------------------- #include <hip/hip_cooperative_groups.h> #include <unistd.h> namespace cg = cooperative_groups; // Utilities and system includes #include <helper_string.h> // helper for string parsing #include <helper_image.h> // helper for image and data comparison #include <helper_cuda.h> // helper for cuda error checking functions #include "kernels.h" const char *sSDKsample = "Transpose"; // Number of repetitions used for timing. Two sets of repetitions are performed: // 1) over kernel launches and 2) inside the kernel over just the loads and stores #define NUM_REPS 10 // --------------------- // host utility routines // --------------------- void computeTransposeGold(std::vector<float>& gold, std::vector<float>& idata, const int size_x, const int size_y) { for (int y = 0; y < size_y; ++y) { for (int x = 0; x < size_x; ++x) { gold[(x * size_y) + y] = idata[(y * size_x) + x]; } } } bool compare_data(std::vector<float>& gold, std::vector<float>& found, float threshold) { for (auto i = 0; i < gold.size(); i++) { float diff = fabs(gold[i] - found[i]); if (diff > threshold) { return false; } } return true; } void print(std::vector<float>& ptr, int x, int y) { for (auto i = 0; i < x; i++) { for (auto j = 0; j < y; j++) { std::cout << ptr[i * x + j] << " "; } std::cout << std::endl; } } void getParams(int argc, char **argv, hipDeviceProp_t &deviceProp, int &size_x, int &size_y, int max_tile_dim) { // set matrix size (if (x,y) dim of matrix is not square, then this will have to be modified if (checkCmdLineFlag(argc, (const char **) argv, "dimX")) { size_x = getCmdLineArgumentInt(argc, (const char **) argv, "dimX"); if (size_x > max_tile_dim) { printf( "> MatrixSize X = %d is greater than the recommended size = %d\n", size_x, max_tile_dim); } else { printf("> MatrixSize X = %d\n", size_x); } } else { size_x = max_tile_dim; size_x = FLOOR(size_x, 512); } if (checkCmdLineFlag(argc, (const char **) argv, "dimY")) { size_y = getCmdLineArgumentInt(argc, (const char **) argv, "dimY"); if (size_y > max_tile_dim) { printf( "> MatrixSize Y = %d is greater than the recommended size = %d\n", size_y, max_tile_dim); } else { printf("> MatrixSize Y = %d\n", size_y); } } else { size_y = max_tile_dim; size_y = FLOOR(size_y, 512); } } void showHelp() { printf("\n%s : Command line options\n", sSDKsample); printf("\t-device=n (where n=0,1,2.... for the GPU device)\n\n"); printf( "> The default matrix size can be overridden with these parameters\n"); printf("\t-dimX=row_dim_size (matrix row dimensions)\n"); printf("\t-dimY=col_dim_size (matrix column dimensions)\n"); } // ---- // main // ---- int main(int argc, char **argv) { // Start logs printf("%s Starting...\n\n", sSDKsample); if (checkCmdLineFlag(argc, (const char **) argv, "help")) { showHelp(); return 0; } int devID = findCudaDevice(argc, (const char **) argv); hipDeviceProp_t deviceProp; // get number of SMs on this GPU checkCudaErrors(hipGetDevice(&devID)); checkCudaErrors(hipGetDeviceProperties(&deviceProp, devID)); // compute the scaling factor (for GPUs with fewer MPs) float scale_factor, total_tiles; scale_factor = max( (192.0f / (_ConvertSMVer2Cores(deviceProp.major, deviceProp.minor) * (float) deviceProp.multiProcessorCount)), 1.0f); printf("> Device %d: \"%s\"\n", devID, deviceProp.name); printf("> SM Capability %d.%d detected:\n", deviceProp.major, deviceProp.minor); // Calculate number of tiles we will run for the Matrix Transpose performance tests int size_x, size_y, max_matrix_dim, matrix_size_test; matrix_size_test = 512; // we round down max_matrix_dim for this perf test total_tiles = (float) MAX_TILES / scale_factor; max_matrix_dim = FLOOR((int)(floor(sqrt(total_tiles))* TILE_DIM), matrix_size_test); // This is the minimum size allowed if (max_matrix_dim == 0) { max_matrix_dim = matrix_size_test; } printf("> [%s] has %d MP(s) x %d (Cores/MP) = %d (Cores)\n", deviceProp.name, deviceProp.multiProcessorCount, _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor), _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor) * deviceProp.multiProcessorCount); printf("> Compute performance scaling factor = %4.2f\n", scale_factor); // Extract parameters if there are any, command line -dimx and -dimy can override // any of these settings getParams(argc, argv, deviceProp, size_x, size_y, max_matrix_dim); if (size_x != size_y) { printf( "\n[%s] does not support non-square matrices (row_dim_size(%d) != col_dim_size(%d))\nExiting...\n\n", sSDKsample, size_x, size_y); exit (EXIT_FAILURE); } if (size_x % TILE_DIM != 0 || size_y % TILE_DIM != 0) { printf( "[%s] Matrix size must be integral multiple of tile size\nExiting...\n\n", sSDKsample); exit (EXIT_FAILURE); } // execution configuration parameters dim3 grid(size_x / TILE_DIM, size_y / TILE_DIM), threads(TILE_DIM, BLOCK_ROWS); if (grid.x < 1 || grid.y < 1) { printf("[%s] grid size computation incorrect in test \nExiting...\n\n", sSDKsample); exit (EXIT_FAILURE); } // size of memory required to store the matrix size_t element_size = size_x * size_y; size_t mem_size = static_cast<size_t>(sizeof(float) * size_x * size_y); if (2 * mem_size > deviceProp.totalGlobalMem) { printf( "Input matrix size is larger than the available device memory!\n"); printf("Please choose a smaller size matrix\n"); exit (EXIT_FAILURE); } // allocate host memory // float *h_idata = (float *) malloc(mem_size); // float *h_odata = (float *) malloc(mem_size); // float *transposeGold = (float *) malloc(mem_size); std::vector<float> h_idata(element_size); std::vector<float> h_odata(element_size); std::vector<float> gold(element_size); // allocate device memory float *d_idata, *d_odata; checkCudaErrors(hipMalloc((void **) &d_idata, mem_size)); checkCudaErrors(hipMalloc((void **) &d_odata, mem_size)); HostPersistentControler main_control(grid); // initialize host data for (int i = 0; i < (size_x * size_y); ++i) { h_idata[i] = i; } // copy host data to device checkCudaErrors( hipMemcpy(d_idata, h_idata.data(), mem_size, hipMemcpyHostToDevice)); // Compute reference transpose solution computeTransposeGold(gold, h_idata, size_x, size_y); std::cout << std::boolalpha; // print out common data for all kernels printf( "\nMatrix size: %dx%d (%dx%d tiles), tile size: %dx%d, block size: %dx%d\n\n", size_x, size_y, size_x / TILE_DIM, size_y / TILE_DIM, TILE_DIM, TILE_DIM, TILE_DIM, BLOCK_ROWS); // Clear error status checkCudaErrors(hipGetLastError()); std::cout << "New stream" << std::endl; std::cout << "Starting running kernel\n"; checkCudaErrors(hipPeekAtLastError()); checkCudaErrors(hipDeviceSynchronize()); std::cout << "GRID " << grid.x << " " << grid.y << " threads " << threads.x << " " << threads.y << std::endl; hipLaunchKernelGGL(( copySharedMem), dim3(grid), dim3(threads), 0, 0, d_odata, d_idata, size_x, size_y); // Ensure no launch failure checkCudaErrors(hipGetLastError()); std::cout << "Trying persistent threads shared memory transpose" << std::endl; size_t num_rep = 0; while (true) { std::cout << "Start processing" << std::endl; main_control.start_processing(); main_control.wait_gpu(); std::cout << "Copy memory back to the host" << std::endl; checkCudaErrors( hipMemcpyAsync(h_odata.data(), d_odata, mem_size, hipMemcpyDeviceToHost, main_control.st)); main_control.sync_stream(); bool res = compare_data(gold, h_odata, 0.0f); num_rep++; if (!res) { std::cout << "Process finished failed, iteration " << num_rep << std::endl; } else { std::cout << "Process finished OK, iteration " << num_rep << std::endl; } if (num_rep > NUM_REPS) break; } main_control.end_kernel(); std::cout << "Releasing memory" << std::endl; hipFree(d_idata); hipFree(d_odata); std::cout << "Synchronizing the device" << std::endl; checkCudaErrors(hipDeviceReset()); printf("Test passed\n"); exit (EXIT_SUCCESS); }
eb16baf537cb975db561965758e154f4d8495ea4.cu
//////////////////////////////////////////////////////////////////////////// // // Copyright 1993-2015 NVIDIA Corporation. All rights reserved. // // Please refer to the NVIDIA end user license agreement (EULA) associated // with this source code for terms and conditions that govern your use of // this software. Any use, reproduction, disclosure, or distribution of // this software and related documentation outside the terms of the EULA // is strictly prohibited. // //////////////////////////////////////////////////////////////////////////// // ---------------------------------------------------------------------------------------- // Transpose // // This file contains both device and host code for transposing a floating-point // matrix. It performs several transpose kernels, which incrementally improve performance // through coalescing, removing shared memory bank conflicts, and eliminating partition // camping. Several of the kernels perform a copy, used to represent the best case // performance that a transpose can achieve. // // Please see the whitepaper in the docs folder of the transpose project for a detailed // description of this performance study. // ---------------------------------------------------------------------------------------- #include <cooperative_groups.h> #include <unistd.h> namespace cg = cooperative_groups; // Utilities and system includes #include <helper_string.h> // helper for string parsing #include <helper_image.h> // helper for image and data comparison #include <helper_cuda.h> // helper for cuda error checking functions #include "kernels.h" const char *sSDKsample = "Transpose"; // Number of repetitions used for timing. Two sets of repetitions are performed: // 1) over kernel launches and 2) inside the kernel over just the loads and stores #define NUM_REPS 10 // --------------------- // host utility routines // --------------------- void computeTransposeGold(std::vector<float>& gold, std::vector<float>& idata, const int size_x, const int size_y) { for (int y = 0; y < size_y; ++y) { for (int x = 0; x < size_x; ++x) { gold[(x * size_y) + y] = idata[(y * size_x) + x]; } } } bool compare_data(std::vector<float>& gold, std::vector<float>& found, float threshold) { for (auto i = 0; i < gold.size(); i++) { float diff = fabs(gold[i] - found[i]); if (diff > threshold) { return false; } } return true; } void print(std::vector<float>& ptr, int x, int y) { for (auto i = 0; i < x; i++) { for (auto j = 0; j < y; j++) { std::cout << ptr[i * x + j] << " "; } std::cout << std::endl; } } void getParams(int argc, char **argv, cudaDeviceProp &deviceProp, int &size_x, int &size_y, int max_tile_dim) { // set matrix size (if (x,y) dim of matrix is not square, then this will have to be modified if (checkCmdLineFlag(argc, (const char **) argv, "dimX")) { size_x = getCmdLineArgumentInt(argc, (const char **) argv, "dimX"); if (size_x > max_tile_dim) { printf( "> MatrixSize X = %d is greater than the recommended size = %d\n", size_x, max_tile_dim); } else { printf("> MatrixSize X = %d\n", size_x); } } else { size_x = max_tile_dim; size_x = FLOOR(size_x, 512); } if (checkCmdLineFlag(argc, (const char **) argv, "dimY")) { size_y = getCmdLineArgumentInt(argc, (const char **) argv, "dimY"); if (size_y > max_tile_dim) { printf( "> MatrixSize Y = %d is greater than the recommended size = %d\n", size_y, max_tile_dim); } else { printf("> MatrixSize Y = %d\n", size_y); } } else { size_y = max_tile_dim; size_y = FLOOR(size_y, 512); } } void showHelp() { printf("\n%s : Command line options\n", sSDKsample); printf("\t-device=n (where n=0,1,2.... for the GPU device)\n\n"); printf( "> The default matrix size can be overridden with these parameters\n"); printf("\t-dimX=row_dim_size (matrix row dimensions)\n"); printf("\t-dimY=col_dim_size (matrix column dimensions)\n"); } // ---- // main // ---- int main(int argc, char **argv) { // Start logs printf("%s Starting...\n\n", sSDKsample); if (checkCmdLineFlag(argc, (const char **) argv, "help")) { showHelp(); return 0; } int devID = findCudaDevice(argc, (const char **) argv); cudaDeviceProp deviceProp; // get number of SMs on this GPU checkCudaErrors(cudaGetDevice(&devID)); checkCudaErrors(cudaGetDeviceProperties(&deviceProp, devID)); // compute the scaling factor (for GPUs with fewer MPs) float scale_factor, total_tiles; scale_factor = max( (192.0f / (_ConvertSMVer2Cores(deviceProp.major, deviceProp.minor) * (float) deviceProp.multiProcessorCount)), 1.0f); printf("> Device %d: \"%s\"\n", devID, deviceProp.name); printf("> SM Capability %d.%d detected:\n", deviceProp.major, deviceProp.minor); // Calculate number of tiles we will run for the Matrix Transpose performance tests int size_x, size_y, max_matrix_dim, matrix_size_test; matrix_size_test = 512; // we round down max_matrix_dim for this perf test total_tiles = (float) MAX_TILES / scale_factor; max_matrix_dim = FLOOR((int)(floor(sqrt(total_tiles))* TILE_DIM), matrix_size_test); // This is the minimum size allowed if (max_matrix_dim == 0) { max_matrix_dim = matrix_size_test; } printf("> [%s] has %d MP(s) x %d (Cores/MP) = %d (Cores)\n", deviceProp.name, deviceProp.multiProcessorCount, _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor), _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor) * deviceProp.multiProcessorCount); printf("> Compute performance scaling factor = %4.2f\n", scale_factor); // Extract parameters if there are any, command line -dimx and -dimy can override // any of these settings getParams(argc, argv, deviceProp, size_x, size_y, max_matrix_dim); if (size_x != size_y) { printf( "\n[%s] does not support non-square matrices (row_dim_size(%d) != col_dim_size(%d))\nExiting...\n\n", sSDKsample, size_x, size_y); exit (EXIT_FAILURE); } if (size_x % TILE_DIM != 0 || size_y % TILE_DIM != 0) { printf( "[%s] Matrix size must be integral multiple of tile size\nExiting...\n\n", sSDKsample); exit (EXIT_FAILURE); } // execution configuration parameters dim3 grid(size_x / TILE_DIM, size_y / TILE_DIM), threads(TILE_DIM, BLOCK_ROWS); if (grid.x < 1 || grid.y < 1) { printf("[%s] grid size computation incorrect in test \nExiting...\n\n", sSDKsample); exit (EXIT_FAILURE); } // size of memory required to store the matrix size_t element_size = size_x * size_y; size_t mem_size = static_cast<size_t>(sizeof(float) * size_x * size_y); if (2 * mem_size > deviceProp.totalGlobalMem) { printf( "Input matrix size is larger than the available device memory!\n"); printf("Please choose a smaller size matrix\n"); exit (EXIT_FAILURE); } // allocate host memory // float *h_idata = (float *) malloc(mem_size); // float *h_odata = (float *) malloc(mem_size); // float *transposeGold = (float *) malloc(mem_size); std::vector<float> h_idata(element_size); std::vector<float> h_odata(element_size); std::vector<float> gold(element_size); // allocate device memory float *d_idata, *d_odata; checkCudaErrors(cudaMalloc((void **) &d_idata, mem_size)); checkCudaErrors(cudaMalloc((void **) &d_odata, mem_size)); HostPersistentControler main_control(grid); // initialize host data for (int i = 0; i < (size_x * size_y); ++i) { h_idata[i] = i; } // copy host data to device checkCudaErrors( cudaMemcpy(d_idata, h_idata.data(), mem_size, cudaMemcpyHostToDevice)); // Compute reference transpose solution computeTransposeGold(gold, h_idata, size_x, size_y); std::cout << std::boolalpha; // print out common data for all kernels printf( "\nMatrix size: %dx%d (%dx%d tiles), tile size: %dx%d, block size: %dx%d\n\n", size_x, size_y, size_x / TILE_DIM, size_y / TILE_DIM, TILE_DIM, TILE_DIM, TILE_DIM, BLOCK_ROWS); // Clear error status checkCudaErrors(cudaGetLastError()); std::cout << "New stream" << std::endl; std::cout << "Starting running kernel\n"; checkCudaErrors(cudaPeekAtLastError()); checkCudaErrors(cudaDeviceSynchronize()); std::cout << "GRID " << grid.x << " " << grid.y << " threads " << threads.x << " " << threads.y << std::endl; copySharedMem<<<grid, threads>>>(d_odata, d_idata, size_x, size_y); // Ensure no launch failure checkCudaErrors(cudaGetLastError()); std::cout << "Trying persistent threads shared memory transpose" << std::endl; size_t num_rep = 0; while (true) { std::cout << "Start processing" << std::endl; main_control.start_processing(); main_control.wait_gpu(); std::cout << "Copy memory back to the host" << std::endl; checkCudaErrors( cudaMemcpyAsync(h_odata.data(), d_odata, mem_size, cudaMemcpyDeviceToHost, main_control.st)); main_control.sync_stream(); bool res = compare_data(gold, h_odata, 0.0f); num_rep++; if (!res) { std::cout << "Process finished failed, iteration " << num_rep << std::endl; } else { std::cout << "Process finished OK, iteration " << num_rep << std::endl; } if (num_rep > NUM_REPS) break; } main_control.end_kernel(); std::cout << "Releasing memory" << std::endl; cudaFree(d_idata); cudaFree(d_odata); std::cout << "Synchronizing the device" << std::endl; checkCudaErrors(cudaDeviceReset()); printf("Test passed\n"); exit (EXIT_SUCCESS); }
f7350091ab0434269a4e2e0441264e19ac60105b.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/math/prelu.h" namespace paddle { namespace operators { namespace math { #define CUDA_NUM_THREADS 1024 inline static int PADDLE_GET_BLOCKS(const int N) { return (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS; } template <typename T> __global__ void PReluChannelFirstWiseKernel(const T *input, const T *alpha, T *output, size_t channel_num, size_t plane_size, size_t numel) { CUDA_KERNEL_LOOP(index, numel) { size_t temp = index / plane_size; size_t channel_index = temp % channel_num; T scale = alpha[channel_index]; T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> __global__ void PReluChannelLastWiseKernel(const T *input, const T *alpha, T *output, size_t channel_num, size_t numel) { CUDA_KERNEL_LOOP(index, numel) { size_t channel_index = index % channel_num; T scale = alpha[channel_index]; T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> __global__ void PReluElementWiseKernel(const T *input, const T *alpha, T *output, size_t spatial_size, size_t numel) { CUDA_KERNEL_LOOP(index, numel) { size_t element_index = index % spatial_size; T scale = alpha[element_index]; T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> __global__ void PReluScalarKernel(const T *input, const T *alpha, T *output, size_t numel) { T scale = alpha[0]; CUDA_KERNEL_LOOP(index, numel) { T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> void PreluChannelWiseDirectCUDAFunctor<T>::operator()( gpuStream_t stream, const T *input, const T *alpha, T *output, size_t batch_size, size_t channel, bool channel_last, size_t numel) { if (channel_last) { hipLaunchKernelGGL(( PReluChannelLastWiseKernel), dim3(PADDLE_GET_BLOCKS(numel)), dim3(CUDA_NUM_THREADS), 0, stream, input, alpha, output, channel, numel); } else { hipLaunchKernelGGL(( PReluChannelFirstWiseKernel), dim3(PADDLE_GET_BLOCKS(numel)), dim3(CUDA_NUM_THREADS), 0, stream, input, alpha, output, channel, numel / batch_size / channel, numel); } } template <typename T> void PreluElementWiseDirectCUDAFunctor<T>::operator()(gpuStream_t stream, const T *input, const T *alpha, T *output, size_t batch_size, size_t numel) { hipLaunchKernelGGL(( PReluElementWiseKernel), dim3(PADDLE_GET_BLOCKS(numel)), dim3(CUDA_NUM_THREADS), 0, stream, input, alpha, output, numel / batch_size, numel); } template <typename T> void PreluScalarDirectCUDAFunctor<T>::operator()(gpuStream_t stream, const T *input, const T *alpha, T *output, size_t numel) { hipLaunchKernelGGL(( PReluScalarKernel), dim3(PADDLE_GET_BLOCKS(numel)), dim3(CUDA_NUM_THREADS), 0, stream, input, alpha, output, numel); } template class PreluChannelWiseDirectCUDAFunctor<float>; template class PreluChannelWiseDirectCUDAFunctor<paddle::platform::float16>; template class PreluChannelWiseDirectCUDAFunctor<double>; template class PreluElementWiseDirectCUDAFunctor<float>; template class PreluElementWiseDirectCUDAFunctor<paddle::platform::float16>; template class PreluElementWiseDirectCUDAFunctor<double>; template class PreluScalarDirectCUDAFunctor<float>; template class PreluScalarDirectCUDAFunctor<paddle::platform::float16>; template class PreluScalarDirectCUDAFunctor<double>; } // namespace math } // namespace operators } // namespace paddle
f7350091ab0434269a4e2e0441264e19ac60105b.cu
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/math/prelu.h" namespace paddle { namespace operators { namespace math { #define CUDA_NUM_THREADS 1024 inline static int PADDLE_GET_BLOCKS(const int N) { return (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS; } template <typename T> __global__ void PReluChannelFirstWiseKernel(const T *input, const T *alpha, T *output, size_t channel_num, size_t plane_size, size_t numel) { CUDA_KERNEL_LOOP(index, numel) { size_t temp = index / plane_size; size_t channel_index = temp % channel_num; T scale = alpha[channel_index]; T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> __global__ void PReluChannelLastWiseKernel(const T *input, const T *alpha, T *output, size_t channel_num, size_t numel) { CUDA_KERNEL_LOOP(index, numel) { size_t channel_index = index % channel_num; T scale = alpha[channel_index]; T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> __global__ void PReluElementWiseKernel(const T *input, const T *alpha, T *output, size_t spatial_size, size_t numel) { CUDA_KERNEL_LOOP(index, numel) { size_t element_index = index % spatial_size; T scale = alpha[element_index]; T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> __global__ void PReluScalarKernel(const T *input, const T *alpha, T *output, size_t numel) { T scale = alpha[0]; CUDA_KERNEL_LOOP(index, numel) { T x = input[index]; T zero = static_cast<T>(0); output[index] = (x > zero) ? x : scale * x; } } template <typename T> void PreluChannelWiseDirectCUDAFunctor<T>::operator()( gpuStream_t stream, const T *input, const T *alpha, T *output, size_t batch_size, size_t channel, bool channel_last, size_t numel) { if (channel_last) { PReluChannelLastWiseKernel<<<PADDLE_GET_BLOCKS(numel), CUDA_NUM_THREADS, 0, stream>>>(input, alpha, output, channel, numel); } else { PReluChannelFirstWiseKernel<<<PADDLE_GET_BLOCKS(numel), CUDA_NUM_THREADS, 0, stream>>>( input, alpha, output, channel, numel / batch_size / channel, numel); } } template <typename T> void PreluElementWiseDirectCUDAFunctor<T>::operator()(gpuStream_t stream, const T *input, const T *alpha, T *output, size_t batch_size, size_t numel) { PReluElementWiseKernel<<<PADDLE_GET_BLOCKS(numel), CUDA_NUM_THREADS, 0, stream>>>(input, alpha, output, numel / batch_size, numel); } template <typename T> void PreluScalarDirectCUDAFunctor<T>::operator()(gpuStream_t stream, const T *input, const T *alpha, T *output, size_t numel) { PReluScalarKernel<<<PADDLE_GET_BLOCKS(numel), CUDA_NUM_THREADS, 0, stream>>>( input, alpha, output, numel); } template class PreluChannelWiseDirectCUDAFunctor<float>; template class PreluChannelWiseDirectCUDAFunctor<paddle::platform::float16>; template class PreluChannelWiseDirectCUDAFunctor<double>; template class PreluElementWiseDirectCUDAFunctor<float>; template class PreluElementWiseDirectCUDAFunctor<paddle::platform::float16>; template class PreluElementWiseDirectCUDAFunctor<double>; template class PreluScalarDirectCUDAFunctor<float>; template class PreluScalarDirectCUDAFunctor<paddle::platform::float16>; template class PreluScalarDirectCUDAFunctor<double>; } // namespace math } // namespace operators } // namespace paddle
d6022925b86139bed9ecf0ce93ccd16f74c89f1f.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* * Copyright (c) 2019, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** @file fil.cu implements forest inference */ #include <thrust/device_ptr.h> #include <thrust/device_vector.h> #include <thrust/host_vector.h> #include <treelite/tree.h> #include <algorithm> #include <cmath> #include <limits> #include <utility> #include <cuml/fil/fil.h> #include <cuml/common/cuml_allocator.hpp> #include "common_hip.cuh" namespace ML { namespace fil { using namespace MLCommon; namespace tl = treelite; void dense_node_init(dense_node_t* n, float output, float thresh, int fid, bool def_left, bool is_leaf) { dense_node dn(output, thresh, fid, def_left, is_leaf); n->bits = dn.bits; n->val = dn.val; } void dense_node_decode(const dense_node_t* n, float* output, float* thresh, int* fid, bool* def_left, bool* is_leaf) { dense_node dn(*n); *output = dn.output(); *thresh = dn.thresh(); *fid = dn.fid(); *def_left = dn.def_left(); *is_leaf = dn.is_leaf(); } void sparse_node_init(sparse_node_t* node, float output, float thresh, int fid, bool def_left, bool is_leaf, int left_index) { sparse_node n(output, thresh, fid, def_left, is_leaf, left_index); node->bits = n.bits; node->val = n.val; node->left_idx = n.left_idx; } /** sparse_node_decode extracts individual members from node */ void sparse_node_decode(const sparse_node_t* node, float* output, float* thresh, int* fid, bool* def_left, bool* is_leaf, int* left_index) { sparse_node n(*node); *output = n.output(); *thresh = n.thresh(); *fid = n.fid(); *def_left = n.def_left(); *is_leaf = n.is_leaf(); *left_index = n.left_index(); } __host__ __device__ float sigmoid(float x) { return 1.0f / (1.0f + expf(-x)); } /** performs additional transformations on the array of forest predictions (preds) of size n; the transformations are defined by output, and include averaging (multiplying by inv_num_trees), adding global_bias (always done), sigmoid and applying threshold */ __global__ void transform_k(float* preds, size_t n, output_t output, float inv_num_trees, float threshold, float global_bias) { size_t i = threadIdx.x + size_t(blockIdx.x) * blockDim.x; if (i >= n) return; float result = preds[i]; if ((output & output_t::AVG) != 0) result *= inv_num_trees; result += global_bias; if ((output & output_t::SIGMOID) != 0) result = sigmoid(result); if ((output & output_t::THRESHOLD) != 0) { result = result > threshold ? 1.0f : 0.0f; } preds[i] = result; } struct forest { void init_max_shm() { int max_shm_std = 48 * 1024; // 48 KiB int device = 0; // TODO(canonizer): use cumlHandle for this CUDA_CHECK(hipGetDevice(&device)); CUDA_CHECK(hipDeviceGetAttribute( &max_shm_, hipDeviceAttributeSharedMemPerBlockOptin, device)); // TODO(canonizer): use >48KiB shared memory if available max_shm_ = ::min(max_shm_, max_shm_std); } void init_common(const forest_params_t* params) { depth_ = params->depth; num_trees_ = params->num_trees; num_cols_ = params->num_cols; algo_ = params->algo; output_ = params->output; threshold_ = params->threshold; global_bias_ = params->global_bias; init_max_shm(); } virtual void infer(predict_params params, hipStream_t stream) = 0; void predict(const cumlHandle& h, float* preds, const float* data, size_t num_rows) { // Initialize prediction parameters. predict_params params; params.num_cols = num_cols_; params.algo = algo_; params.preds = preds; params.data = data; params.num_rows = num_rows; params.max_shm = max_shm_; // Predict using the forest. hipStream_t stream = h.getStream(); infer(params, stream); // Transform the output if necessary. if (output_ != output_t::RAW || global_bias_ != 0.0f) { hipLaunchKernelGGL(( transform_k), dim3(ceildiv(int(num_rows), FIL_TPB)), dim3(FIL_TPB), 0, stream, preds, num_rows, output_, num_trees_ > 0 ? (1.0f / num_trees_) : 1.0f, threshold_, global_bias_); CUDA_CHECK(hipPeekAtLastError()); } } virtual void free(const cumlHandle& h) = 0; virtual ~forest() {} int num_trees_ = 0; int depth_ = 0; int num_cols_ = 0; algo_t algo_ = algo_t::NAIVE; int max_shm_ = 0; output_t output_ = output_t::RAW; float threshold_ = 0.5; float global_bias_ = 0; }; struct dense_forest : forest { void transform_trees(const dense_node_t* nodes) { // populate node information for (int i = 0, gid = 0; i < num_trees_; ++i) { for (int j = 0, nid = 0; j <= depth_; ++j) { for (int k = 0; k < 1 << j; ++k, ++nid, ++gid) { h_nodes_[nid * num_trees_ + i] = dense_node(nodes[gid]); } } } } void init(const cumlHandle& h, const dense_node_t* nodes, const forest_params_t* params) { init_common(params); int num_nodes = forest_num_nodes(num_trees_, depth_); nodes_ = (dense_node*)h.getDeviceAllocator()->allocate( sizeof(dense_node) * num_nodes, h.getStream()); h_nodes_.resize(num_nodes); if (algo_ == algo_t::NAIVE) { std::copy(nodes, nodes + num_nodes, h_nodes_.begin()); } else { transform_trees(nodes); } CUDA_CHECK(hipMemcpyAsync(nodes_, h_nodes_.data(), num_nodes * sizeof(dense_node), hipMemcpyHostToDevice, h.getStream())); // copy must be finished before freeing the host data CUDA_CHECK(hipStreamSynchronize(h.getStream())); h_nodes_.clear(); h_nodes_.shrink_to_fit(); } virtual void infer(predict_params params, hipStream_t stream) override { dense_storage forest(nodes_, num_trees_, algo_ == algo_t::NAIVE ? tree_num_nodes(depth_) : 1, algo_ == algo_t::NAIVE ? 1 : num_trees_); fil::infer(forest, params, stream); } virtual void free(const cumlHandle& h) override { int num_nodes = forest_num_nodes(num_trees_, depth_); h.getDeviceAllocator()->deallocate(nodes_, sizeof(dense_node) * num_nodes, h.getStream()); } dense_node* nodes_ = nullptr; thrust::host_vector<dense_node> h_nodes_; }; struct sparse_forest : forest { void init(const cumlHandle& h, const int* trees, const sparse_node_t* nodes, const forest_params_t* params) { init_common(params); depth_ = 0; // a placeholder value num_nodes_ = params->num_nodes; // trees trees_ = (int*)h.getDeviceAllocator()->allocate(sizeof(int) * num_trees_, h.getStream()); CUDA_CHECK(hipMemcpyAsync(trees_, trees, sizeof(int) * num_trees_, hipMemcpyHostToDevice, h.getStream())); // nodes nodes_ = (sparse_node*)h.getDeviceAllocator()->allocate( sizeof(sparse_node) * num_nodes_, h.getStream()); CUDA_CHECK(hipMemcpyAsync(nodes_, nodes, sizeof(sparse_node) * num_nodes_, hipMemcpyHostToDevice, h.getStream())); } virtual void infer(predict_params params, hipStream_t stream) override { sparse_storage forest(trees_, nodes_, num_trees_); fil::infer(forest, params, stream); } void free(const cumlHandle& h) override { h.getDeviceAllocator()->deallocate(trees_, sizeof(int) * num_trees_, h.getStream()); h.getDeviceAllocator()->deallocate(nodes_, sizeof(sparse_node) * num_nodes_, h.getStream()); } int num_nodes_ = 0; int* trees_ = nullptr; sparse_node* nodes_ = nullptr; }; void check_params(const forest_params_t* params, bool dense) { if (dense) { ASSERT(params->depth >= 0, "depth must be non-negative for dense forests"); } else { ASSERT(params->num_nodes >= 0, "num_nodes must be non-negative for sparse forests"); ASSERT(params->algo == algo_t::NAIVE, "only NAIVE algorithm is supported for sparse forests"); } ASSERT(params->num_trees >= 0, "num_trees must be non-negative"); ASSERT(params->num_cols >= 0, "num_cols must be non-negative"); switch (params->algo) { case algo_t::NAIVE: case algo_t::TREE_REORG: case algo_t::BATCH_TREE_REORG: break; default: ASSERT(false, "aglo should be NAIVE, TREE_REORG or BATCH_TREE_REORG"); } // output_t::RAW == 0, and doesn't have a separate flag output_t all_set = output_t(output_t::AVG | output_t::SIGMOID | output_t::THRESHOLD); if ((params->output & ~all_set) != 0) { ASSERT(false, "output should be a combination of RAW, AVG, SIGMOID and THRESHOLD"); } } // tl_node_at is a checked version of tree[i] const tl::Tree::Node& tl_node_at(const tl::Tree& tree, size_t i) { ASSERT(i < tree.num_nodes, "node index out of range"); return tree[i]; } int tree_root(const tl::Tree& tree) { // find the root int root = -1; for (int i = 0; i < tree.num_nodes; ++i) { if (tl_node_at(tree, i).is_root()) { ASSERT(root == -1, "multi-root trees not supported"); root = i; } } ASSERT(root != -1, "a tree must have a root"); return root; } int max_depth_helper(const tl::Tree& tree, const tl::Tree::Node& node, int limit) { if (node.is_leaf()) return 0; ASSERT(limit > 0, "recursion depth limit reached, might be a cycle in the tree"); return 1 + ::max( max_depth_helper(tree, tl_node_at(tree, node.cleft()), limit - 1), max_depth_helper(tree, tl_node_at(tree, node.cright()), limit - 1)); } int max_depth(const tl::Tree& tree) { // trees of this depth aren't used, so it most likely means bad input data, // e.g. cycles in the forest const int RECURSION_LIMIT = 500; return max_depth_helper(tree, tl_node_at(tree, tree_root(tree)), RECURSION_LIMIT); } void node2fil(std::vector<dense_node_t>* pnodes, int root, int cur, const tl::Tree& tree, const tl::Tree::Node& node) { std::vector<dense_node_t>& nodes = *pnodes; if (node.is_leaf()) { dense_node_init(&nodes[root + cur], node.leaf_value(), 0, 0, false, true); return; } // inner node ASSERT(node.split_type() == tl::SplitFeatureType::kNumerical, "only numerical split nodes are supported"); int left = node.cleft(), right = node.cright(); bool default_left = node.default_left(); float threshold = node.threshold(); // in treelite (take left node if val [op] threshold), // the meaning of the condition is reversed compared to FIL; // thus, "<" in treelite corresonds to comparison ">=" used by FIL // https://github.com/dmlc/treelite/blob/master/include/treelite/tree.h#L243 switch (node.comparison_op()) { case tl::Operator::kLT: break; case tl::Operator::kLE: // x <= y is equivalent to x < y', where y' is the next representable float threshold = std::nextafterf(threshold, std::numeric_limits<float>::infinity()); break; case tl::Operator::kGT: // x > y is equivalent to x >= y', where y' is the next representable float // left and right still need to be swapped threshold = std::nextafterf(threshold, std::numeric_limits<float>::infinity()); case tl::Operator::kGE: // swap left and right std::swap(left, right); default_left = !default_left; break; default: ASSERT(false, "only <, >, <= and >= comparisons are supported"); } dense_node_init(&nodes[root + cur], 0, threshold, node.split_index(), default_left, false); node2fil(pnodes, root, 2 * cur + 1, tree, tl_node_at(tree, left)); node2fil(pnodes, root, 2 * cur + 2, tree, tl_node_at(tree, right)); } void tree2fil(std::vector<dense_node_t>* pnodes, int root, const tl::Tree& tree) { node2fil(pnodes, root, 0, tree, tl_node_at(tree, tree_root(tree))); } // uses treelite model with additional tl_params to initialize FIL params // and nodes (stored in *pnodes) void tl2fil(forest_params_t* params, std::vector<dense_node_t>* pnodes, const tl::Model& model, const treelite_params_t* tl_params) { // fill in forest-indendent params params->algo = tl_params->algo; params->threshold = tl_params->threshold; // fill in forest-dependent params params->num_cols = model.num_feature; ASSERT(model.num_output_group == 1, "multi-class classification not supported"); const tl::ModelParam& param = model.param; ASSERT(param.sigmoid_alpha == 1.0f, "sigmoid_alpha not supported"); params->global_bias = param.global_bias; params->output = output_t::RAW; if (tl_params->output_class) { params->output = output_t(params->output | output_t::THRESHOLD); } // "random forest" in treelite means tree output averaging if (model.random_forest_flag) { params->output = output_t(params->output | output_t::AVG); } if (param.pred_transform == "sigmoid") { params->output = output_t(params->output | output_t::SIGMOID); } else if (param.pred_transform != "identity") { ASSERT(false, "%s: unsupported treelite prediction transform", param.pred_transform.c_str()); } params->num_trees = model.trees.size(); int depth = 0; for (const auto& tree : model.trees) depth = ::max(depth, max_depth(tree)); params->depth = depth; // convert the nodes int num_nodes = forest_num_nodes(params->num_trees, params->depth); pnodes->resize(num_nodes, dense_node_t{0, 0}); for (int i = 0; i < model.trees.size(); ++i) { tree2fil(pnodes, i * tree_num_nodes(params->depth), model.trees[i]); } } void init_dense(const cumlHandle& h, forest_t* pf, const dense_node_t* nodes, const forest_params_t* params) { check_params(params, true); dense_forest* f = new dense_forest; f->init(h, nodes, params); *pf = f; } void init_sparse(const cumlHandle& h, forest_t* pf, const int* trees, const sparse_node_t* nodes, const forest_params_t* params) { check_params(params, false); sparse_forest* f = new sparse_forest; f->init(h, trees, nodes, params); *pf = f; } void from_treelite(const cumlHandle& handle, forest_t* pforest, ModelHandle model, const treelite_params_t* tl_params) { forest_params_t params; std::vector<dense_node_t> nodes; tl2fil(&params, &nodes, *(tl::Model*)model, tl_params); init_dense(handle, pforest, nodes.data(), &params); // sync is necessary as nodes is used in init_dense(), // but destructed at the end of this function CUDA_CHECK(hipStreamSynchronize(handle.getStream())); } void free(const cumlHandle& h, forest_t f) { f->free(h); delete f; } void predict(const cumlHandle& h, forest_t f, float* preds, const float* data, size_t num_rows) { f->predict(h, preds, data, num_rows); } } // namespace fil } // namespace ML
d6022925b86139bed9ecf0ce93ccd16f74c89f1f.cu
/* * Copyright (c) 2019, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** @file fil.cu implements forest inference */ #include <thrust/device_ptr.h> #include <thrust/device_vector.h> #include <thrust/host_vector.h> #include <treelite/tree.h> #include <algorithm> #include <cmath> #include <limits> #include <utility> #include <cuml/fil/fil.h> #include <cuml/common/cuml_allocator.hpp> #include "common.cuh" namespace ML { namespace fil { using namespace MLCommon; namespace tl = treelite; void dense_node_init(dense_node_t* n, float output, float thresh, int fid, bool def_left, bool is_leaf) { dense_node dn(output, thresh, fid, def_left, is_leaf); n->bits = dn.bits; n->val = dn.val; } void dense_node_decode(const dense_node_t* n, float* output, float* thresh, int* fid, bool* def_left, bool* is_leaf) { dense_node dn(*n); *output = dn.output(); *thresh = dn.thresh(); *fid = dn.fid(); *def_left = dn.def_left(); *is_leaf = dn.is_leaf(); } void sparse_node_init(sparse_node_t* node, float output, float thresh, int fid, bool def_left, bool is_leaf, int left_index) { sparse_node n(output, thresh, fid, def_left, is_leaf, left_index); node->bits = n.bits; node->val = n.val; node->left_idx = n.left_idx; } /** sparse_node_decode extracts individual members from node */ void sparse_node_decode(const sparse_node_t* node, float* output, float* thresh, int* fid, bool* def_left, bool* is_leaf, int* left_index) { sparse_node n(*node); *output = n.output(); *thresh = n.thresh(); *fid = n.fid(); *def_left = n.def_left(); *is_leaf = n.is_leaf(); *left_index = n.left_index(); } __host__ __device__ float sigmoid(float x) { return 1.0f / (1.0f + expf(-x)); } /** performs additional transformations on the array of forest predictions (preds) of size n; the transformations are defined by output, and include averaging (multiplying by inv_num_trees), adding global_bias (always done), sigmoid and applying threshold */ __global__ void transform_k(float* preds, size_t n, output_t output, float inv_num_trees, float threshold, float global_bias) { size_t i = threadIdx.x + size_t(blockIdx.x) * blockDim.x; if (i >= n) return; float result = preds[i]; if ((output & output_t::AVG) != 0) result *= inv_num_trees; result += global_bias; if ((output & output_t::SIGMOID) != 0) result = sigmoid(result); if ((output & output_t::THRESHOLD) != 0) { result = result > threshold ? 1.0f : 0.0f; } preds[i] = result; } struct forest { void init_max_shm() { int max_shm_std = 48 * 1024; // 48 KiB int device = 0; // TODO(canonizer): use cumlHandle for this CUDA_CHECK(cudaGetDevice(&device)); CUDA_CHECK(cudaDeviceGetAttribute( &max_shm_, cudaDevAttrMaxSharedMemoryPerBlockOptin, device)); // TODO(canonizer): use >48KiB shared memory if available max_shm_ = std::min(max_shm_, max_shm_std); } void init_common(const forest_params_t* params) { depth_ = params->depth; num_trees_ = params->num_trees; num_cols_ = params->num_cols; algo_ = params->algo; output_ = params->output; threshold_ = params->threshold; global_bias_ = params->global_bias; init_max_shm(); } virtual void infer(predict_params params, cudaStream_t stream) = 0; void predict(const cumlHandle& h, float* preds, const float* data, size_t num_rows) { // Initialize prediction parameters. predict_params params; params.num_cols = num_cols_; params.algo = algo_; params.preds = preds; params.data = data; params.num_rows = num_rows; params.max_shm = max_shm_; // Predict using the forest. cudaStream_t stream = h.getStream(); infer(params, stream); // Transform the output if necessary. if (output_ != output_t::RAW || global_bias_ != 0.0f) { transform_k<<<ceildiv(int(num_rows), FIL_TPB), FIL_TPB, 0, stream>>>( preds, num_rows, output_, num_trees_ > 0 ? (1.0f / num_trees_) : 1.0f, threshold_, global_bias_); CUDA_CHECK(cudaPeekAtLastError()); } } virtual void free(const cumlHandle& h) = 0; virtual ~forest() {} int num_trees_ = 0; int depth_ = 0; int num_cols_ = 0; algo_t algo_ = algo_t::NAIVE; int max_shm_ = 0; output_t output_ = output_t::RAW; float threshold_ = 0.5; float global_bias_ = 0; }; struct dense_forest : forest { void transform_trees(const dense_node_t* nodes) { // populate node information for (int i = 0, gid = 0; i < num_trees_; ++i) { for (int j = 0, nid = 0; j <= depth_; ++j) { for (int k = 0; k < 1 << j; ++k, ++nid, ++gid) { h_nodes_[nid * num_trees_ + i] = dense_node(nodes[gid]); } } } } void init(const cumlHandle& h, const dense_node_t* nodes, const forest_params_t* params) { init_common(params); int num_nodes = forest_num_nodes(num_trees_, depth_); nodes_ = (dense_node*)h.getDeviceAllocator()->allocate( sizeof(dense_node) * num_nodes, h.getStream()); h_nodes_.resize(num_nodes); if (algo_ == algo_t::NAIVE) { std::copy(nodes, nodes + num_nodes, h_nodes_.begin()); } else { transform_trees(nodes); } CUDA_CHECK(cudaMemcpyAsync(nodes_, h_nodes_.data(), num_nodes * sizeof(dense_node), cudaMemcpyHostToDevice, h.getStream())); // copy must be finished before freeing the host data CUDA_CHECK(cudaStreamSynchronize(h.getStream())); h_nodes_.clear(); h_nodes_.shrink_to_fit(); } virtual void infer(predict_params params, cudaStream_t stream) override { dense_storage forest(nodes_, num_trees_, algo_ == algo_t::NAIVE ? tree_num_nodes(depth_) : 1, algo_ == algo_t::NAIVE ? 1 : num_trees_); fil::infer(forest, params, stream); } virtual void free(const cumlHandle& h) override { int num_nodes = forest_num_nodes(num_trees_, depth_); h.getDeviceAllocator()->deallocate(nodes_, sizeof(dense_node) * num_nodes, h.getStream()); } dense_node* nodes_ = nullptr; thrust::host_vector<dense_node> h_nodes_; }; struct sparse_forest : forest { void init(const cumlHandle& h, const int* trees, const sparse_node_t* nodes, const forest_params_t* params) { init_common(params); depth_ = 0; // a placeholder value num_nodes_ = params->num_nodes; // trees trees_ = (int*)h.getDeviceAllocator()->allocate(sizeof(int) * num_trees_, h.getStream()); CUDA_CHECK(cudaMemcpyAsync(trees_, trees, sizeof(int) * num_trees_, cudaMemcpyHostToDevice, h.getStream())); // nodes nodes_ = (sparse_node*)h.getDeviceAllocator()->allocate( sizeof(sparse_node) * num_nodes_, h.getStream()); CUDA_CHECK(cudaMemcpyAsync(nodes_, nodes, sizeof(sparse_node) * num_nodes_, cudaMemcpyHostToDevice, h.getStream())); } virtual void infer(predict_params params, cudaStream_t stream) override { sparse_storage forest(trees_, nodes_, num_trees_); fil::infer(forest, params, stream); } void free(const cumlHandle& h) override { h.getDeviceAllocator()->deallocate(trees_, sizeof(int) * num_trees_, h.getStream()); h.getDeviceAllocator()->deallocate(nodes_, sizeof(sparse_node) * num_nodes_, h.getStream()); } int num_nodes_ = 0; int* trees_ = nullptr; sparse_node* nodes_ = nullptr; }; void check_params(const forest_params_t* params, bool dense) { if (dense) { ASSERT(params->depth >= 0, "depth must be non-negative for dense forests"); } else { ASSERT(params->num_nodes >= 0, "num_nodes must be non-negative for sparse forests"); ASSERT(params->algo == algo_t::NAIVE, "only NAIVE algorithm is supported for sparse forests"); } ASSERT(params->num_trees >= 0, "num_trees must be non-negative"); ASSERT(params->num_cols >= 0, "num_cols must be non-negative"); switch (params->algo) { case algo_t::NAIVE: case algo_t::TREE_REORG: case algo_t::BATCH_TREE_REORG: break; default: ASSERT(false, "aglo should be NAIVE, TREE_REORG or BATCH_TREE_REORG"); } // output_t::RAW == 0, and doesn't have a separate flag output_t all_set = output_t(output_t::AVG | output_t::SIGMOID | output_t::THRESHOLD); if ((params->output & ~all_set) != 0) { ASSERT(false, "output should be a combination of RAW, AVG, SIGMOID and THRESHOLD"); } } // tl_node_at is a checked version of tree[i] const tl::Tree::Node& tl_node_at(const tl::Tree& tree, size_t i) { ASSERT(i < tree.num_nodes, "node index out of range"); return tree[i]; } int tree_root(const tl::Tree& tree) { // find the root int root = -1; for (int i = 0; i < tree.num_nodes; ++i) { if (tl_node_at(tree, i).is_root()) { ASSERT(root == -1, "multi-root trees not supported"); root = i; } } ASSERT(root != -1, "a tree must have a root"); return root; } int max_depth_helper(const tl::Tree& tree, const tl::Tree::Node& node, int limit) { if (node.is_leaf()) return 0; ASSERT(limit > 0, "recursion depth limit reached, might be a cycle in the tree"); return 1 + std::max( max_depth_helper(tree, tl_node_at(tree, node.cleft()), limit - 1), max_depth_helper(tree, tl_node_at(tree, node.cright()), limit - 1)); } int max_depth(const tl::Tree& tree) { // trees of this depth aren't used, so it most likely means bad input data, // e.g. cycles in the forest const int RECURSION_LIMIT = 500; return max_depth_helper(tree, tl_node_at(tree, tree_root(tree)), RECURSION_LIMIT); } void node2fil(std::vector<dense_node_t>* pnodes, int root, int cur, const tl::Tree& tree, const tl::Tree::Node& node) { std::vector<dense_node_t>& nodes = *pnodes; if (node.is_leaf()) { dense_node_init(&nodes[root + cur], node.leaf_value(), 0, 0, false, true); return; } // inner node ASSERT(node.split_type() == tl::SplitFeatureType::kNumerical, "only numerical split nodes are supported"); int left = node.cleft(), right = node.cright(); bool default_left = node.default_left(); float threshold = node.threshold(); // in treelite (take left node if val [op] threshold), // the meaning of the condition is reversed compared to FIL; // thus, "<" in treelite corresonds to comparison ">=" used by FIL // https://github.com/dmlc/treelite/blob/master/include/treelite/tree.h#L243 switch (node.comparison_op()) { case tl::Operator::kLT: break; case tl::Operator::kLE: // x <= y is equivalent to x < y', where y' is the next representable float threshold = std::nextafterf(threshold, std::numeric_limits<float>::infinity()); break; case tl::Operator::kGT: // x > y is equivalent to x >= y', where y' is the next representable float // left and right still need to be swapped threshold = std::nextafterf(threshold, std::numeric_limits<float>::infinity()); case tl::Operator::kGE: // swap left and right std::swap(left, right); default_left = !default_left; break; default: ASSERT(false, "only <, >, <= and >= comparisons are supported"); } dense_node_init(&nodes[root + cur], 0, threshold, node.split_index(), default_left, false); node2fil(pnodes, root, 2 * cur + 1, tree, tl_node_at(tree, left)); node2fil(pnodes, root, 2 * cur + 2, tree, tl_node_at(tree, right)); } void tree2fil(std::vector<dense_node_t>* pnodes, int root, const tl::Tree& tree) { node2fil(pnodes, root, 0, tree, tl_node_at(tree, tree_root(tree))); } // uses treelite model with additional tl_params to initialize FIL params // and nodes (stored in *pnodes) void tl2fil(forest_params_t* params, std::vector<dense_node_t>* pnodes, const tl::Model& model, const treelite_params_t* tl_params) { // fill in forest-indendent params params->algo = tl_params->algo; params->threshold = tl_params->threshold; // fill in forest-dependent params params->num_cols = model.num_feature; ASSERT(model.num_output_group == 1, "multi-class classification not supported"); const tl::ModelParam& param = model.param; ASSERT(param.sigmoid_alpha == 1.0f, "sigmoid_alpha not supported"); params->global_bias = param.global_bias; params->output = output_t::RAW; if (tl_params->output_class) { params->output = output_t(params->output | output_t::THRESHOLD); } // "random forest" in treelite means tree output averaging if (model.random_forest_flag) { params->output = output_t(params->output | output_t::AVG); } if (param.pred_transform == "sigmoid") { params->output = output_t(params->output | output_t::SIGMOID); } else if (param.pred_transform != "identity") { ASSERT(false, "%s: unsupported treelite prediction transform", param.pred_transform.c_str()); } params->num_trees = model.trees.size(); int depth = 0; for (const auto& tree : model.trees) depth = std::max(depth, max_depth(tree)); params->depth = depth; // convert the nodes int num_nodes = forest_num_nodes(params->num_trees, params->depth); pnodes->resize(num_nodes, dense_node_t{0, 0}); for (int i = 0; i < model.trees.size(); ++i) { tree2fil(pnodes, i * tree_num_nodes(params->depth), model.trees[i]); } } void init_dense(const cumlHandle& h, forest_t* pf, const dense_node_t* nodes, const forest_params_t* params) { check_params(params, true); dense_forest* f = new dense_forest; f->init(h, nodes, params); *pf = f; } void init_sparse(const cumlHandle& h, forest_t* pf, const int* trees, const sparse_node_t* nodes, const forest_params_t* params) { check_params(params, false); sparse_forest* f = new sparse_forest; f->init(h, trees, nodes, params); *pf = f; } void from_treelite(const cumlHandle& handle, forest_t* pforest, ModelHandle model, const treelite_params_t* tl_params) { forest_params_t params; std::vector<dense_node_t> nodes; tl2fil(&params, &nodes, *(tl::Model*)model, tl_params); init_dense(handle, pforest, nodes.data(), &params); // sync is necessary as nodes is used in init_dense(), // but destructed at the end of this function CUDA_CHECK(cudaStreamSynchronize(handle.getStream())); } void free(const cumlHandle& h, forest_t f) { f->free(h); delete f; } void predict(const cumlHandle& h, forest_t f, float* preds, const float* data, size_t num_rows) { f->predict(h, preds, data, num_rows); } } // namespace fil } // namespace ML
3be64c7a1f6fd022e9a7ad3878de7750153f5a8e.hip
// !!! This is a file automatically generated by hipify!!! #include "core/builder.h" #include "core/continuous_tree_grower.h" #include "gtest/gtest.h" #include "io/io.h" #include "test_utils.h" #include "thrust/device_vector.h" #include "thrust/host_vector.h" namespace arboretum_test { using arboretum::core::ContinuousTreeGrower; using arboretum::core::GainFunctionParameters; using arboretum::core::my_atomics; TEST(ContinuousTreeGrower, DISABLED_RootSearchCategoryFeature) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 1, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); for (int i = 0; i < size; ++i) { grad[i] = make_float2(float(i), 1.0); fvalue_h[i] = fvalue_d[i] = i / 2; sum += grad[i]; } thrust::device_vector<float2> parent_node_sum(2); parent_node_sum[0] = make_float2(0, 0); parent_node_sum[1] = sum; thrust::device_vector<unsigned int> parent_node_count(2, 0); parent_node_count[0] = 0; parent_node_count[1] = size; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.ProcessCategoryFeature<unsigned int>(row2Node, grad, fvalue_d, fvalue_h, parent_node_sum, parent_node_count, 3, 0, p); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 0); ASSERT_FLOAT_EQ(result_h[0].floats[0], 480.0); } TEST(ContinuousTreeGrower, RootSearchContinuousFeature) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 1, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); for (int i = 0; i < size; ++i) { grad[i] = make_float2(float(i), 1.0); fvalue_h[i] = fvalue_d[i] = i / 4; sum += grad[i]; } thrust::device_vector<float2> parent_node_sum(2); parent_node_sum[0] = make_float2(0, 0); parent_node_sum[1] = sum; thrust::device_vector<unsigned int> parent_node_count(2, 0); parent_node_count[0] = 0; parent_node_count[1] = size; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.ProcessDenseFeature(partitioning_indexes, row2Node, grad, fvalue_d, thrust::raw_pointer_cast(fvalue_h.data()), parent_node_sum, parent_node_count, 3, 0, 1, p, false); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 16); ASSERT_FLOAT_EQ(result_h[0].floats[0], 2048.0); } TEST(ContinuousTreeGrower, FloatUnstableExample) { const unsigned level = 1; const unsigned depth = 4; const size_t size = 10; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float, float>(size, 4, 0, NULL); thrust::host_vector<unsigned int> node(size, 0); thrust::device_vector<unsigned> partitioning_indexes(size, 0); thrust::host_vector<float> grad(size); thrust::host_vector<unsigned short> feature(size); thrust::host_vector<float> parent_node_sum(3, 0.0); thrust::host_vector<unsigned int> parent_node_count(3, 0); grad[0] = 2.350000; feature[0] = 4; node[0] = 0; grad[1] = 0.750004; feature[1] = 3; node[1] = 4; grad[2] = 2.100000; feature[2] = 6; node[2] = 2; grad[3] = -2.100000; feature[3] = 2; node[3] = 4; grad[4] = -1.200003; feature[4] = 1; node[4] = 4; grad[5] = 1.199997; feature[5] = 0; node[5] = 4; grad[6] = -1.199997; feature[6] = 6; node[6] = 6; grad[7] = 1.200003; feature[7] = 6; node[7] = 6; grad[8] = -0.749996; feature[8] = 5; node[8] = 4; grad[9] = -2.350000; feature[9] = 6; node[9] = 4; // sum was computed with a slight error parent_node_sum[0] = 0.000000; parent_node_count[0] = 0; // prcise value 3.1 parent_node_sum[1] = 3.100004; parent_node_count[1] = 2; // prcise value 0.0 parent_node_sum[2] = 0.000008; parent_node_count[2] = 10; auto p = GainFunctionParameters(2, 2, 0, 0, 0, 0); thrust::device_vector<unsigned short> feature_d = feature; grower.CreatePartitioningIndexes(partitioning_indexes, node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, node, grad, feature_d, thrust::raw_pointer_cast(feature.data()), parent_node_sum, parent_node_count, 3, level, depth, p, false); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 0); ASSERT_FLOAT_EQ(result_h[0].floats[0], 0); ASSERT_EQ(result_h[1].ints[1], 7); ASSERT_FLOAT_EQ(result_h[1].floats[0], 5.676724); } TEST(ContinuousTreeGrower, DoubleUnstableExample) { const unsigned level = 1; const unsigned depth = 2; const size_t size = 10; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float, double>(size, 4, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::host_vector<unsigned int> node(size, 0); thrust::host_vector<float> grad(size); thrust::host_vector<unsigned short> feature(size); thrust::host_vector<double> parent_node_sum(3, 0.0); thrust::host_vector<unsigned int> parent_node_count(3, 0); grad[0] = 2.350000; feature[0] = 4; node[0] = 0; grad[1] = 0.750000; feature[1] = 3; node[1] = 4; grad[2] = 2.100000; feature[2] = 6; node[2] = 2; grad[3] = -2.100000; feature[3] = 2; node[3] = 4; grad[4] = -1.199999; feature[4] = 1; node[4] = 4; grad[5] = 1.200001; feature[5] = 0; node[5] = 4; grad[6] = -1.200001; feature[6] = 6; node[6] = 6; grad[7] = 1.199999; feature[7] = 6; node[7] = 6; grad[8] = -0.75; feature[8] = 5; node[8] = 4; grad[9] = -2.350000; feature[9] = 6; node[9] = 4; // precise sums parent_node_sum[0] = 0.0; parent_node_count[0] = 0; parent_node_sum[1] = 3.1; parent_node_count[1] = 2; parent_node_sum[2] = 0.0; parent_node_count[2] = 10; auto p = GainFunctionParameters(2, 2, 0, 0, 0, 0); thrust::device_vector<unsigned short> feature_d = feature; grower.CreatePartitioningIndexes(partitioning_indexes, node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, node, grad, feature_d, thrust::raw_pointer_cast(feature.data()), parent_node_sum, parent_node_count, 3, 1, 4, p, false); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 0); ASSERT_FLOAT_EQ(result_h[0].floats[0], 0); ASSERT_EQ(result_h[1].ints[1], 7); ASSERT_FLOAT_EQ(result_h[1].floats[0], 5.6767569); } TEST(ContinuousTreeGrower, Level1SearchContinuousFeature) { const unsigned level = 1; const unsigned depth = 2; const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>( size, depth, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); thrust::device_vector<float2> parent_node_sum(3, make_float2(0, 0)); thrust::device_vector<unsigned int> parent_node_count(3, 0); for (int i = 0; i < size; ++i) { row2Node[i] = i % 2; grad[i] = make_float2(i * i, 1.0); fvalue_h[i] = fvalue_d[i] = i / 4; parent_node_sum[(i / 16) + 1] += grad[i]; // parent_node_count[(i / 16) + 1] += 1; } parent_node_count[1] += 16; parent_node_count[2] += 32; parent_node_sum[1] += parent_node_sum[0]; parent_node_sum[2] += parent_node_sum[1]; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.CreatePartitioningIndexes(partitioning_indexes, row2Node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, row2Node, grad, fvalue_d, thrust::raw_pointer_cast(fvalue_h.data()), parent_node_sum, parent_node_count, 3, level, depth, p, false); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 10); ASSERT_FLOAT_EQ(result_h[0].floats[0], 64026.672); ASSERT_EQ(result_h[1].ints[1], 24); ASSERT_FLOAT_EQ(result_h[1].floats[0], 565504); ASSERT_EQ(grower.node_fvalue[0], 0); ASSERT_EQ(grower.node_fvalue[1], 0); ASSERT_EQ(grower.node_fvalue[2], 1); ASSERT_EQ(grower.node_fvalue[3], 1); ASSERT_EQ(grower.node_fvalue[4], 2); ASSERT_EQ(grower.node_fvalue[5], 2); ASSERT_EQ(grower.node_fvalue[6], 3); ASSERT_EQ(grower.node_fvalue[7], 3); ASSERT_EQ(grower.node_fvalue[8], 4); ASSERT_EQ(grower.node_fvalue[9], 4); ASSERT_EQ(grower.node_fvalue[10], 5); ASSERT_EQ(grower.node_fvalue[11], 5); ASSERT_EQ(grower.node_fvalue[12], 6); ASSERT_EQ(grower.node_fvalue[13], 6); ASSERT_EQ(grower.node_fvalue[14], 7); ASSERT_EQ(grower.node_fvalue[15], 7); ASSERT_EQ(grower.node_fvalue[16], 7); ASSERT_EQ(grower.node_fvalue[17], 7); ASSERT_EQ(grower.node_fvalue[18], 6); ASSERT_EQ(grower.node_fvalue[19], 6); ASSERT_EQ(grower.node_fvalue[20], 5); ASSERT_EQ(grower.node_fvalue[21], 5); ASSERT_EQ(grower.node_fvalue[22], 4); ASSERT_EQ(grower.node_fvalue[23], 4); ASSERT_EQ(grower.node_fvalue[24], 3); ASSERT_EQ(grower.node_fvalue[25], 3); ASSERT_EQ(grower.node_fvalue[26], 2); ASSERT_EQ(grower.node_fvalue[27], 2); ASSERT_EQ(grower.node_fvalue[28], 1); ASSERT_EQ(grower.node_fvalue[29], 1); ASSERT_EQ(grower.node_fvalue[30], 0); ASSERT_EQ(grower.node_fvalue[31], 0); } TEST(ContinuousTreeGrower, Level1SearchContinuousFeatureDouble) { const unsigned level = 1; const unsigned depth = 2; const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, mydouble2>( size, depth, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); thrust::device_vector<mydouble2> parent_node_sum(3, make_double2(0, 0)); thrust::device_vector<unsigned int> parent_node_count(3, 0); for (int i = 0; i < size; ++i) { row2Node[i] = i % 2; grad[i] = make_float2(i * i, 1.0); fvalue_h[i] = fvalue_d[i] = i / 4; mydouble2 tmp = mydouble2(grad[i]); parent_node_sum[(i / 16) + 1] += tmp; // parent_node_count[(i / 16) + 1] += 1; } parent_node_count[1] += 16; parent_node_count[2] += 32; parent_node_sum[1] += parent_node_sum[0]; parent_node_sum[2] += parent_node_sum[1]; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.CreatePartitioningIndexes(partitioning_indexes, row2Node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, row2Node, grad, fvalue_d, thrust::raw_pointer_cast(fvalue_h.data()), parent_node_sum, parent_node_count, 3, level, depth, p, false); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 10); ASSERT_FLOAT_EQ(result_h[0].floats[0], 64026.672); ASSERT_EQ(result_h[1].ints[1], 24); ASSERT_FLOAT_EQ(result_h[1].floats[0], 565504); ASSERT_EQ(grower.node_fvalue[0], 0); ASSERT_EQ(grower.node_fvalue[1], 0); ASSERT_EQ(grower.node_fvalue[2], 1); ASSERT_EQ(grower.node_fvalue[3], 1); ASSERT_EQ(grower.node_fvalue[4], 2); ASSERT_EQ(grower.node_fvalue[5], 2); ASSERT_EQ(grower.node_fvalue[6], 3); ASSERT_EQ(grower.node_fvalue[7], 3); ASSERT_EQ(grower.node_fvalue[8], 4); ASSERT_EQ(grower.node_fvalue[9], 4); ASSERT_EQ(grower.node_fvalue[10], 5); ASSERT_EQ(grower.node_fvalue[11], 5); ASSERT_EQ(grower.node_fvalue[12], 6); ASSERT_EQ(grower.node_fvalue[13], 6); ASSERT_EQ(grower.node_fvalue[14], 7); ASSERT_EQ(grower.node_fvalue[15], 7); ASSERT_EQ(grower.node_fvalue[16], 7); ASSERT_EQ(grower.node_fvalue[17], 7); ASSERT_EQ(grower.node_fvalue[18], 6); ASSERT_EQ(grower.node_fvalue[19], 6); ASSERT_EQ(grower.node_fvalue[20], 5); ASSERT_EQ(grower.node_fvalue[21], 5); ASSERT_EQ(grower.node_fvalue[22], 4); ASSERT_EQ(grower.node_fvalue[23], 4); ASSERT_EQ(grower.node_fvalue[24], 3); ASSERT_EQ(grower.node_fvalue[25], 3); ASSERT_EQ(grower.node_fvalue[26], 2); ASSERT_EQ(grower.node_fvalue[27], 2); ASSERT_EQ(grower.node_fvalue[28], 1); ASSERT_EQ(grower.node_fvalue[29], 1); ASSERT_EQ(grower.node_fvalue[30], 0); ASSERT_EQ(grower.node_fvalue[31], 0); } TEST(ContinuousTreeGrower, ApplySplitLevel0) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 2, 0); thrust::device_vector<unsigned int> row2Node(size, 0); row2Node[3] = 1; row2Node[4] = 1; row2Node[6] = 1; grower.node_fvalue[0] = 0; grower.node_fvalue[1] = 5; grower.node_fvalue[2] = 2; grower.node_fvalue[3] = 5; grower.node_fvalue[4] = 3; grower.node_fvalue[5] = 4; grower.node_fvalue[6] = 4; grower.ApplySplit(thrust::raw_pointer_cast(row2Node.data()), 0, 4, 0, size); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<unsigned> nodes = row2Node; ASSERT_EQ(nodes[0], 0); ASSERT_EQ(nodes[1], 1); ASSERT_EQ(nodes[2], 0); ASSERT_EQ(nodes[3], 1); ASSERT_EQ(nodes[4], 0); ASSERT_EQ(nodes[5], 1); ASSERT_EQ(nodes[6], 1); for (int i = 7; i < size; ++i) { ASSERT_EQ(nodes[i], 0); } } TEST(ContinuousTreeGrower, ApplySplitLevel1) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 2, 0); thrust::device_vector<unsigned int> row2Node(size, 3); row2Node[3] = 1; row2Node[4] = 1; row2Node[6] = 1; grower.node_fvalue[0] = 0; grower.node_fvalue[1] = 5; grower.node_fvalue[2] = 2; grower.node_fvalue[3] = 5; grower.node_fvalue[4] = 3; grower.node_fvalue[5] = 4; grower.node_fvalue[6] = 4; grower.ApplySplit(thrust::raw_pointer_cast(row2Node.data()), 1, 4, 1, 7); TEST_OK(hipStreamSynchronize(grower.stream)); thrust::host_vector<unsigned> nodes = row2Node; ASSERT_EQ(nodes[0], 3); ASSERT_EQ(nodes[1], 3); ASSERT_EQ(nodes[2], 1); ASSERT_EQ(nodes[3], 3); ASSERT_EQ(nodes[4], 1); ASSERT_EQ(nodes[5], 3); ASSERT_EQ(nodes[6], 3); for (int i = 7; i < size; ++i) { ASSERT_EQ(nodes[i], 3); } } } // namespace arboretum_test
3be64c7a1f6fd022e9a7ad3878de7750153f5a8e.cu
#include "core/builder.h" #include "core/continuous_tree_grower.h" #include "gtest/gtest.h" #include "io/io.h" #include "test_utils.h" #include "thrust/device_vector.h" #include "thrust/host_vector.h" namespace arboretum_test { using arboretum::core::ContinuousTreeGrower; using arboretum::core::GainFunctionParameters; using arboretum::core::my_atomics; TEST(ContinuousTreeGrower, DISABLED_RootSearchCategoryFeature) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 1, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); for (int i = 0; i < size; ++i) { grad[i] = make_float2(float(i), 1.0); fvalue_h[i] = fvalue_d[i] = i / 2; sum += grad[i]; } thrust::device_vector<float2> parent_node_sum(2); parent_node_sum[0] = make_float2(0, 0); parent_node_sum[1] = sum; thrust::device_vector<unsigned int> parent_node_count(2, 0); parent_node_count[0] = 0; parent_node_count[1] = size; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.ProcessCategoryFeature<unsigned int>(row2Node, grad, fvalue_d, fvalue_h, parent_node_sum, parent_node_count, 3, 0, p); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 0); ASSERT_FLOAT_EQ(result_h[0].floats[0], 480.0); } TEST(ContinuousTreeGrower, RootSearchContinuousFeature) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 1, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); for (int i = 0; i < size; ++i) { grad[i] = make_float2(float(i), 1.0); fvalue_h[i] = fvalue_d[i] = i / 4; sum += grad[i]; } thrust::device_vector<float2> parent_node_sum(2); parent_node_sum[0] = make_float2(0, 0); parent_node_sum[1] = sum; thrust::device_vector<unsigned int> parent_node_count(2, 0); parent_node_count[0] = 0; parent_node_count[1] = size; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.ProcessDenseFeature(partitioning_indexes, row2Node, grad, fvalue_d, thrust::raw_pointer_cast(fvalue_h.data()), parent_node_sum, parent_node_count, 3, 0, 1, p, false); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 16); ASSERT_FLOAT_EQ(result_h[0].floats[0], 2048.0); } TEST(ContinuousTreeGrower, FloatUnstableExample) { const unsigned level = 1; const unsigned depth = 4; const size_t size = 10; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float, float>(size, 4, 0, NULL); thrust::host_vector<unsigned int> node(size, 0); thrust::device_vector<unsigned> partitioning_indexes(size, 0); thrust::host_vector<float> grad(size); thrust::host_vector<unsigned short> feature(size); thrust::host_vector<float> parent_node_sum(3, 0.0); thrust::host_vector<unsigned int> parent_node_count(3, 0); grad[0] = 2.350000; feature[0] = 4; node[0] = 0; grad[1] = 0.750004; feature[1] = 3; node[1] = 4; grad[2] = 2.100000; feature[2] = 6; node[2] = 2; grad[3] = -2.100000; feature[3] = 2; node[3] = 4; grad[4] = -1.200003; feature[4] = 1; node[4] = 4; grad[5] = 1.199997; feature[5] = 0; node[5] = 4; grad[6] = -1.199997; feature[6] = 6; node[6] = 6; grad[7] = 1.200003; feature[7] = 6; node[7] = 6; grad[8] = -0.749996; feature[8] = 5; node[8] = 4; grad[9] = -2.350000; feature[9] = 6; node[9] = 4; // sum was computed with a slight error parent_node_sum[0] = 0.000000; parent_node_count[0] = 0; // prcise value 3.1 parent_node_sum[1] = 3.100004; parent_node_count[1] = 2; // prcise value 0.0 parent_node_sum[2] = 0.000008; parent_node_count[2] = 10; auto p = GainFunctionParameters(2, 2, 0, 0, 0, 0); thrust::device_vector<unsigned short> feature_d = feature; grower.CreatePartitioningIndexes(partitioning_indexes, node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, node, grad, feature_d, thrust::raw_pointer_cast(feature.data()), parent_node_sum, parent_node_count, 3, level, depth, p, false); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 0); ASSERT_FLOAT_EQ(result_h[0].floats[0], 0); ASSERT_EQ(result_h[1].ints[1], 7); ASSERT_FLOAT_EQ(result_h[1].floats[0], 5.676724); } TEST(ContinuousTreeGrower, DoubleUnstableExample) { const unsigned level = 1; const unsigned depth = 2; const size_t size = 10; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float, double>(size, 4, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::host_vector<unsigned int> node(size, 0); thrust::host_vector<float> grad(size); thrust::host_vector<unsigned short> feature(size); thrust::host_vector<double> parent_node_sum(3, 0.0); thrust::host_vector<unsigned int> parent_node_count(3, 0); grad[0] = 2.350000; feature[0] = 4; node[0] = 0; grad[1] = 0.750000; feature[1] = 3; node[1] = 4; grad[2] = 2.100000; feature[2] = 6; node[2] = 2; grad[3] = -2.100000; feature[3] = 2; node[3] = 4; grad[4] = -1.199999; feature[4] = 1; node[4] = 4; grad[5] = 1.200001; feature[5] = 0; node[5] = 4; grad[6] = -1.200001; feature[6] = 6; node[6] = 6; grad[7] = 1.199999; feature[7] = 6; node[7] = 6; grad[8] = -0.75; feature[8] = 5; node[8] = 4; grad[9] = -2.350000; feature[9] = 6; node[9] = 4; // precise sums parent_node_sum[0] = 0.0; parent_node_count[0] = 0; parent_node_sum[1] = 3.1; parent_node_count[1] = 2; parent_node_sum[2] = 0.0; parent_node_count[2] = 10; auto p = GainFunctionParameters(2, 2, 0, 0, 0, 0); thrust::device_vector<unsigned short> feature_d = feature; grower.CreatePartitioningIndexes(partitioning_indexes, node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, node, grad, feature_d, thrust::raw_pointer_cast(feature.data()), parent_node_sum, parent_node_count, 3, 1, 4, p, false); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 0); ASSERT_FLOAT_EQ(result_h[0].floats[0], 0); ASSERT_EQ(result_h[1].ints[1], 7); ASSERT_FLOAT_EQ(result_h[1].floats[0], 5.6767569); } TEST(ContinuousTreeGrower, Level1SearchContinuousFeature) { const unsigned level = 1; const unsigned depth = 2; const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>( size, depth, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); thrust::device_vector<float2> parent_node_sum(3, make_float2(0, 0)); thrust::device_vector<unsigned int> parent_node_count(3, 0); for (int i = 0; i < size; ++i) { row2Node[i] = i % 2; grad[i] = make_float2(i * i, 1.0); fvalue_h[i] = fvalue_d[i] = i / 4; parent_node_sum[(i / 16) + 1] += grad[i]; // parent_node_count[(i / 16) + 1] += 1; } parent_node_count[1] += 16; parent_node_count[2] += 32; parent_node_sum[1] += parent_node_sum[0]; parent_node_sum[2] += parent_node_sum[1]; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.CreatePartitioningIndexes(partitioning_indexes, row2Node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, row2Node, grad, fvalue_d, thrust::raw_pointer_cast(fvalue_h.data()), parent_node_sum, parent_node_count, 3, level, depth, p, false); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 10); ASSERT_FLOAT_EQ(result_h[0].floats[0], 64026.672); ASSERT_EQ(result_h[1].ints[1], 24); ASSERT_FLOAT_EQ(result_h[1].floats[0], 565504); ASSERT_EQ(grower.node_fvalue[0], 0); ASSERT_EQ(grower.node_fvalue[1], 0); ASSERT_EQ(grower.node_fvalue[2], 1); ASSERT_EQ(grower.node_fvalue[3], 1); ASSERT_EQ(grower.node_fvalue[4], 2); ASSERT_EQ(grower.node_fvalue[5], 2); ASSERT_EQ(grower.node_fvalue[6], 3); ASSERT_EQ(grower.node_fvalue[7], 3); ASSERT_EQ(grower.node_fvalue[8], 4); ASSERT_EQ(grower.node_fvalue[9], 4); ASSERT_EQ(grower.node_fvalue[10], 5); ASSERT_EQ(grower.node_fvalue[11], 5); ASSERT_EQ(grower.node_fvalue[12], 6); ASSERT_EQ(grower.node_fvalue[13], 6); ASSERT_EQ(grower.node_fvalue[14], 7); ASSERT_EQ(grower.node_fvalue[15], 7); ASSERT_EQ(grower.node_fvalue[16], 7); ASSERT_EQ(grower.node_fvalue[17], 7); ASSERT_EQ(grower.node_fvalue[18], 6); ASSERT_EQ(grower.node_fvalue[19], 6); ASSERT_EQ(grower.node_fvalue[20], 5); ASSERT_EQ(grower.node_fvalue[21], 5); ASSERT_EQ(grower.node_fvalue[22], 4); ASSERT_EQ(grower.node_fvalue[23], 4); ASSERT_EQ(grower.node_fvalue[24], 3); ASSERT_EQ(grower.node_fvalue[25], 3); ASSERT_EQ(grower.node_fvalue[26], 2); ASSERT_EQ(grower.node_fvalue[27], 2); ASSERT_EQ(grower.node_fvalue[28], 1); ASSERT_EQ(grower.node_fvalue[29], 1); ASSERT_EQ(grower.node_fvalue[30], 0); ASSERT_EQ(grower.node_fvalue[31], 0); } TEST(ContinuousTreeGrower, Level1SearchContinuousFeatureDouble) { const unsigned level = 1; const unsigned depth = 2; const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, mydouble2>( size, depth, 0); thrust::device_vector<unsigned int> row2Node(size, 0); thrust::device_vector<unsigned int> partitioning_indexes(size, 0); thrust::device_vector<float2> grad(32); thrust::host_vector<unsigned short> fvalue_h(32); thrust::device_vector<unsigned short> fvalue_d(32); float2 sum = make_float2(0.0, 0.0); thrust::device_vector<mydouble2> parent_node_sum(3, make_double2(0, 0)); thrust::device_vector<unsigned int> parent_node_count(3, 0); for (int i = 0; i < size; ++i) { row2Node[i] = i % 2; grad[i] = make_float2(i * i, 1.0); fvalue_h[i] = fvalue_d[i] = i / 4; mydouble2 tmp = mydouble2(grad[i]); parent_node_sum[(i / 16) + 1] += tmp; // parent_node_count[(i / 16) + 1] += 1; } parent_node_count[1] += 16; parent_node_count[2] += 32; parent_node_sum[1] += parent_node_sum[0]; parent_node_sum[2] += parent_node_sum[1]; auto p = GainFunctionParameters(0, 0, 0, 0, 0, 0); grower.CreatePartitioningIndexes(partitioning_indexes, row2Node, parent_node_count, level, depth); grower.ProcessDenseFeature(partitioning_indexes, row2Node, grad, fvalue_d, thrust::raw_pointer_cast(fvalue_h.data()), parent_node_sum, parent_node_count, 3, level, depth, p, false); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<my_atomics> result_h = grower.result_d; // copy pasted result ASSERT_EQ(result_h[0].ints[1], 10); ASSERT_FLOAT_EQ(result_h[0].floats[0], 64026.672); ASSERT_EQ(result_h[1].ints[1], 24); ASSERT_FLOAT_EQ(result_h[1].floats[0], 565504); ASSERT_EQ(grower.node_fvalue[0], 0); ASSERT_EQ(grower.node_fvalue[1], 0); ASSERT_EQ(grower.node_fvalue[2], 1); ASSERT_EQ(grower.node_fvalue[3], 1); ASSERT_EQ(grower.node_fvalue[4], 2); ASSERT_EQ(grower.node_fvalue[5], 2); ASSERT_EQ(grower.node_fvalue[6], 3); ASSERT_EQ(grower.node_fvalue[7], 3); ASSERT_EQ(grower.node_fvalue[8], 4); ASSERT_EQ(grower.node_fvalue[9], 4); ASSERT_EQ(grower.node_fvalue[10], 5); ASSERT_EQ(grower.node_fvalue[11], 5); ASSERT_EQ(grower.node_fvalue[12], 6); ASSERT_EQ(grower.node_fvalue[13], 6); ASSERT_EQ(grower.node_fvalue[14], 7); ASSERT_EQ(grower.node_fvalue[15], 7); ASSERT_EQ(grower.node_fvalue[16], 7); ASSERT_EQ(grower.node_fvalue[17], 7); ASSERT_EQ(grower.node_fvalue[18], 6); ASSERT_EQ(grower.node_fvalue[19], 6); ASSERT_EQ(grower.node_fvalue[20], 5); ASSERT_EQ(grower.node_fvalue[21], 5); ASSERT_EQ(grower.node_fvalue[22], 4); ASSERT_EQ(grower.node_fvalue[23], 4); ASSERT_EQ(grower.node_fvalue[24], 3); ASSERT_EQ(grower.node_fvalue[25], 3); ASSERT_EQ(grower.node_fvalue[26], 2); ASSERT_EQ(grower.node_fvalue[27], 2); ASSERT_EQ(grower.node_fvalue[28], 1); ASSERT_EQ(grower.node_fvalue[29], 1); ASSERT_EQ(grower.node_fvalue[30], 0); ASSERT_EQ(grower.node_fvalue[31], 0); } TEST(ContinuousTreeGrower, ApplySplitLevel0) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 2, 0); thrust::device_vector<unsigned int> row2Node(size, 0); row2Node[3] = 1; row2Node[4] = 1; row2Node[6] = 1; grower.node_fvalue[0] = 0; grower.node_fvalue[1] = 5; grower.node_fvalue[2] = 2; grower.node_fvalue[3] = 5; grower.node_fvalue[4] = 3; grower.node_fvalue[5] = 4; grower.node_fvalue[6] = 4; grower.ApplySplit(thrust::raw_pointer_cast(row2Node.data()), 0, 4, 0, size); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<unsigned> nodes = row2Node; ASSERT_EQ(nodes[0], 0); ASSERT_EQ(nodes[1], 1); ASSERT_EQ(nodes[2], 0); ASSERT_EQ(nodes[3], 1); ASSERT_EQ(nodes[4], 0); ASSERT_EQ(nodes[5], 1); ASSERT_EQ(nodes[6], 1); for (int i = 7; i < size; ++i) { ASSERT_EQ(nodes[i], 0); } } TEST(ContinuousTreeGrower, ApplySplitLevel1) { const size_t size = 32; auto grower = ContinuousTreeGrower<unsigned int, unsigned short, float2, float2>(size, 2, 0); thrust::device_vector<unsigned int> row2Node(size, 3); row2Node[3] = 1; row2Node[4] = 1; row2Node[6] = 1; grower.node_fvalue[0] = 0; grower.node_fvalue[1] = 5; grower.node_fvalue[2] = 2; grower.node_fvalue[3] = 5; grower.node_fvalue[4] = 3; grower.node_fvalue[5] = 4; grower.node_fvalue[6] = 4; grower.ApplySplit(thrust::raw_pointer_cast(row2Node.data()), 1, 4, 1, 7); TEST_OK(cudaStreamSynchronize(grower.stream)); thrust::host_vector<unsigned> nodes = row2Node; ASSERT_EQ(nodes[0], 3); ASSERT_EQ(nodes[1], 3); ASSERT_EQ(nodes[2], 1); ASSERT_EQ(nodes[3], 3); ASSERT_EQ(nodes[4], 1); ASSERT_EQ(nodes[5], 3); ASSERT_EQ(nodes[6], 3); for (int i = 7; i < size; ++i) { ASSERT_EQ(nodes[i], 3); } } } // namespace arboretum_test
1257ae3bfa8584e770f99ed0c07d69c8efb78d54.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "device_launch_parameters.h" #include "myCuda.cuh" #include <stdio.h> using namespace myCuda::math; int unified_memory() { const int N = 4; float* x; hipMallocManaged(&x, N * sizeof(float)); float* y; hipMallocManaged(&y, N * sizeof(float)); for(int i = 0; i < N; ++i) { x[ i ] = (i - N / 2.) / N; } hipLaunchKernelGGL(( logit) , dim3(1), dim3(N) , 0, 0, y, x, N); hipDeviceSynchronize(); for(int i = 0; i < N; ++i) { printf("logit(%f)=%f\n", x[ i ], y[ i ]); } hipFree(x); hipFree(y); return 0; } #define IDX2F(i,j,ld)((((j)-1)*(ld)+((i)-1))) static __inline__ void modify(hipblasHandle_t handle, float* m, int ldm, int n, int p, int q, float alpha, float beta) { hipblasSscal(handle, n-p+1, &alpha, &m[IDX2F(p,q,ldm)], ldm); hipblasSscal(handle, ldm-p+1, &beta, &m[IDX2F(p,q,ldm)], 1); } int cublas_test() { const int M = 6; const int N = 5; hipError_t cudaStat; hipblasStatus_t stat; hipblasHandle_t handle; int i, j; float* devPtrA; float* a = 0; a = (float*)malloc(M * N * sizeof(*a)); for(j = 1; j <= N; ++j) { for(i = 1; i <= M; ++i) { a[ IDX2F(i, j, M) ] = (float)((i - 1) * M + j); } } return 0; } void test_random_number() { thrust::device_vector<float> d(10); myCuda::ran::runif(d); thrust::copy(d.begin(), d.end(), std::ostream_iterator<float>(std::cout, "\n")); } void test_weaver(); int main() { myCuda::gpuInfo(); test_random_number(); test_weaver(); system("pause"); return 0; }
1257ae3bfa8584e770f99ed0c07d69c8efb78d54.cu
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include "myCuda.cuh" #include <stdio.h> using namespace myCuda::math; int unified_memory() { const int N = 4; float* x; cudaMallocManaged(&x, N * sizeof(float)); float* y; cudaMallocManaged(&y, N * sizeof(float)); for(int i = 0; i < N; ++i) { x[ i ] = (i - N / 2.) / N; } logit <<<1, N >>>(y, x, N); cudaDeviceSynchronize(); for(int i = 0; i < N; ++i) { printf("logit(%f)=%f\n", x[ i ], y[ i ]); } cudaFree(x); cudaFree(y); return 0; } #define IDX2F(i,j,ld)((((j)-1)*(ld)+((i)-1))) static __inline__ void modify(cublasHandle_t handle, float* m, int ldm, int n, int p, int q, float alpha, float beta) { cublasSscal(handle, n-p+1, &alpha, &m[IDX2F(p,q,ldm)], ldm); cublasSscal(handle, ldm-p+1, &beta, &m[IDX2F(p,q,ldm)], 1); } int cublas_test() { const int M = 6; const int N = 5; cudaError_t cudaStat; cublasStatus_t stat; cublasHandle_t handle; int i, j; float* devPtrA; float* a = 0; a = (float*)malloc(M * N * sizeof(*a)); for(j = 1; j <= N; ++j) { for(i = 1; i <= M; ++i) { a[ IDX2F(i, j, M) ] = (float)((i - 1) * M + j); } } return 0; } void test_random_number() { thrust::device_vector<float> d(10); myCuda::ran::runif(d); thrust::copy(d.begin(), d.end(), std::ostream_iterator<float>(std::cout, "\n")); } void test_weaver(); int main() { myCuda::gpuInfo(); test_random_number(); test_weaver(); system("pause"); return 0; }
10d58f5138a5de9885401a7d9bfcef7cd62b48f5.hip
// !!! This is a file automatically generated by hipify!!! /* * PackageBase.cpp * * Created on: Feb 10, 2019 * Author: holgus103 */ #include "PackageBase.h" #include "tests.h" #include <stdlib.h> PackageBase::PackageBase() { //initialize pointers as NULL this->data = NULL; this->compressedData = NULL; this->decompressedData = NULL; } PackageBase::~PackageBase() { free(this->decompressedData); free(this->compressedData); } void PackageBase::startTimer(){ hipEventCreate(&(this->start)); hipEventRecord(this->start,0); } void PackageBase::getTimerMeasurement(float* pOutVal){ hipEventCreate(&(this->stop)); hipEventRecord(this->stop,0); hipEventSynchronize(this->stop); hipEventElapsedTime(pOutVal, this->start,this->stop); } void PackageBase::c_initializeVariables(unsigned int* in_data, unsigned long long int in_size){ this->c_compression = 0; this->c_transferFromDevice = 0; this->c_transferToDevice = 0; this->d_compression = 0; this->d_transferFromDevice = 0; this->d_transferToDevice = 0; this->data = in_data; } void PackageBase::compressData(unsigned int* in_data, unsigned long long int in_size){ if(this->compressedData != NULL){ free(this->compressedData); this->compressedData = NULL; } // template method this->startTimer(); this->c_initializeVariables(in_data, in_size); this->c_allocateMemory(); this->c_copyToDevice(); // get transfer and allocation time this->getTimerMeasurement(&(this->c_transferToDevice)); // restart time measuring this->startTimer(); this->c_runAlgorithm(); // get compression time this->getTimerMeasurement(&(this->c_compression)); // restart time measuring this->startTimer(); this->c_copyFromDevice(); this->c_cleanup(); // get transfer time this->getTimerMeasurement(&(this->c_transferFromDevice)); } void PackageBase::decompressData(){ if(this->decompressedData != NULL){ free(this->decompressedData); this->decompressedData = NULL; } // template method this->startTimer(); this->d_initializeVariables(); this->d_allocateMemory(); this->d_copyToDevice(); this->getTimerMeasurement(&(this->d_transferToDevice)); this->startTimer(); this->d_runAlgorithm(); this->getTimerMeasurement(&(this->d_compression)); this->startTimer(); this->d_copyFromDevice(); this->getTimerMeasurement(&(this->d_transferFromDevice)); this->d_cleanup(); } unsigned int* PackageBase::getCompressed(){ return this->compressedData; } unsigned int* PackageBase::getDecompressed(){ return this->decompressedData; } PackageBase::Times PackageBase::getTimes(){ Times t; t.c_compression = this->c_compression; t.c_transferFromDevice = this->c_transferFromDevice; t.c_transferToDevice = this->c_transferToDevice; t.d_compression = this->d_compression; t.d_transferFromDevice = this->d_transferFromDevice; t.d_transferToDevice = this->d_transferToDevice; return t; }
10d58f5138a5de9885401a7d9bfcef7cd62b48f5.cu
/* * PackageBase.cpp * * Created on: Feb 10, 2019 * Author: holgus103 */ #include "PackageBase.h" #include "tests.h" #include <stdlib.h> PackageBase::PackageBase() { //initialize pointers as NULL this->data = NULL; this->compressedData = NULL; this->decompressedData = NULL; } PackageBase::~PackageBase() { free(this->decompressedData); free(this->compressedData); } void PackageBase::startTimer(){ cudaEventCreate(&(this->start)); cudaEventRecord(this->start,0); } void PackageBase::getTimerMeasurement(float* pOutVal){ cudaEventCreate(&(this->stop)); cudaEventRecord(this->stop,0); cudaEventSynchronize(this->stop); cudaEventElapsedTime(pOutVal, this->start,this->stop); } void PackageBase::c_initializeVariables(unsigned int* in_data, unsigned long long int in_size){ this->c_compression = 0; this->c_transferFromDevice = 0; this->c_transferToDevice = 0; this->d_compression = 0; this->d_transferFromDevice = 0; this->d_transferToDevice = 0; this->data = in_data; } void PackageBase::compressData(unsigned int* in_data, unsigned long long int in_size){ if(this->compressedData != NULL){ free(this->compressedData); this->compressedData = NULL; } // template method this->startTimer(); this->c_initializeVariables(in_data, in_size); this->c_allocateMemory(); this->c_copyToDevice(); // get transfer and allocation time this->getTimerMeasurement(&(this->c_transferToDevice)); // restart time measuring this->startTimer(); this->c_runAlgorithm(); // get compression time this->getTimerMeasurement(&(this->c_compression)); // restart time measuring this->startTimer(); this->c_copyFromDevice(); this->c_cleanup(); // get transfer time this->getTimerMeasurement(&(this->c_transferFromDevice)); } void PackageBase::decompressData(){ if(this->decompressedData != NULL){ free(this->decompressedData); this->decompressedData = NULL; } // template method this->startTimer(); this->d_initializeVariables(); this->d_allocateMemory(); this->d_copyToDevice(); this->getTimerMeasurement(&(this->d_transferToDevice)); this->startTimer(); this->d_runAlgorithm(); this->getTimerMeasurement(&(this->d_compression)); this->startTimer(); this->d_copyFromDevice(); this->getTimerMeasurement(&(this->d_transferFromDevice)); this->d_cleanup(); } unsigned int* PackageBase::getCompressed(){ return this->compressedData; } unsigned int* PackageBase::getDecompressed(){ return this->decompressedData; } PackageBase::Times PackageBase::getTimes(){ Times t; t.c_compression = this->c_compression; t.c_transferFromDevice = this->c_transferFromDevice; t.c_transferToDevice = this->c_transferToDevice; t.d_compression = this->d_compression; t.d_transferFromDevice = this->d_transferFromDevice; t.d_transferToDevice = this->d_transferToDevice; return t; }
6f3deb00cfe8f784e1e29098f287d975488d855e.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // Copyright 2013 Yangqing Jia #include <algorithm> #include <cfloat> #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" #include "caffe/util/math_functions.hpp" #define CAFFE_MAX_POOLING_THRESHOLD 1e-8f using std::max; using std::min; namespace caffe { template <typename Dtype> __global__ void MaxPoolForward(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* top_data) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; int hstart = ph * stride; int hend = min(hstart + ksize, height); int wstart = pw * stride; int wend = min(wstart + ksize, width); Dtype maxval = -FLT_MAX; bottom_data += (n * channels + c) * height * width; for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { maxval = max(maxval, bottom_data[h * width + w]); } } top_data[index] = maxval; } // (if index < nthreads) } template <typename Dtype> __global__ void AvePoolForward(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* top_data) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; int hstart = ph * stride; int hend = min(hstart + ksize, height); int wstart = pw * stride; int wend = min(wstart + ksize, width); Dtype aveval = 0; bottom_data += (n * channels + c) * height * width; for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { aveval += bottom_data[h * width + w]; } } top_data[index] = aveval / (hend - hstart) / (wend - wstart); } // (if index < nthreads) } template <typename Dtype> void PoolingLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) { const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = (*top)[0]->mutable_gpu_data(); int count = (*top)[0]->count(); switch (this->layer_param_.pool()) { case LayerParameter_PoolMethod_MAX: hipLaunchKernelGGL(( MaxPoolForward<Dtype>), dim3(CAFFE_GET_BLOCKS(count)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, count, bottom_data, bottom[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, top_data); break; case LayerParameter_PoolMethod_AVE: hipLaunchKernelGGL(( AvePoolForward<Dtype>), dim3(CAFFE_GET_BLOCKS(count)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, count, bottom_data, bottom[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, top_data); break; default: LOG(FATAL) << "Unknown pooling method."; } CUDA_POST_KERNEL_CHECK; } template <typename Dtype> __global__ void MaxPoolBackward(const int nthreads, const Dtype* bottom_data, const Dtype* top_data, const Dtype* top_diff, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* bottom_diff) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { // find out the local index // find out the local offset int w = index % width; int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; int phend = min(h / stride + 1, pooled_height); int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; Dtype bottom_datum = bottom_data[((n * channels + c) * height + h) * width + w]; top_data += (n * channels + c) * pooled_height * pooled_width; top_diff += (n * channels + c) * pooled_height * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { gradient += top_diff[ph * pooled_width + pw] * (bottom_datum >= top_data[ph * pooled_width + pw] - CAFFE_MAX_POOLING_THRESHOLD); } } bottom_diff[index] = gradient; } // (if index < nthreads) } template <typename Dtype> __global__ void AvePoolBackward(const int nthreads, const Dtype* top_diff, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* bottom_diff) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { // find out the local index // find out the local offset int w = index % width; int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; int phend = min(h / stride + 1, pooled_height); int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; top_diff += (n * channels + c) * pooled_height * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { // figure out the pooling size int poolsize = (min(ph * stride + ksize, height) - ph * stride) * (min(pw * stride + ksize, width) - pw * stride); gradient += top_diff[ph * pooled_width + pw] / poolsize; } } bottom_diff[index] = gradient; } // (if index < nthreads) } template <typename Dtype> Dtype PoolingLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const bool propagate_down, vector<Blob<Dtype>*>* bottom) { if (!propagate_down) { return Dtype(0.); } const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); int count = (*bottom)[0]->count(); switch (this->layer_param_.pool()) { case LayerParameter_PoolMethod_MAX: hipLaunchKernelGGL(( MaxPoolBackward<Dtype>), dim3(CAFFE_GET_BLOCKS(count)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, count, (*bottom)[0]->gpu_data(), top[0]->gpu_data(), top_diff, top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); break; case LayerParameter_PoolMethod_AVE: hipLaunchKernelGGL(( AvePoolBackward<Dtype>), dim3(CAFFE_GET_BLOCKS(count)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, count, top_diff, top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); break; default: LOG(FATAL) << "Unknown pooling method."; } CUDA_POST_KERNEL_CHECK; return Dtype(0.); } INSTANTIATE_CLASS(PoolingLayer); } // namespace caffe
6f3deb00cfe8f784e1e29098f287d975488d855e.cu
// Copyright 2013 Yangqing Jia #include <algorithm> #include <cfloat> #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" #include "caffe/util/math_functions.hpp" #define CAFFE_MAX_POOLING_THRESHOLD 1e-8f using std::max; using std::min; namespace caffe { template <typename Dtype> __global__ void MaxPoolForward(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* top_data) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; int hstart = ph * stride; int hend = min(hstart + ksize, height); int wstart = pw * stride; int wend = min(wstart + ksize, width); Dtype maxval = -FLT_MAX; bottom_data += (n * channels + c) * height * width; for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { maxval = max(maxval, bottom_data[h * width + w]); } } top_data[index] = maxval; } // (if index < nthreads) } template <typename Dtype> __global__ void AvePoolForward(const int nthreads, const Dtype* bottom_data, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* top_data) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; int hstart = ph * stride; int hend = min(hstart + ksize, height); int wstart = pw * stride; int wend = min(wstart + ksize, width); Dtype aveval = 0; bottom_data += (n * channels + c) * height * width; for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { aveval += bottom_data[h * width + w]; } } top_data[index] = aveval / (hend - hstart) / (wend - wstart); } // (if index < nthreads) } template <typename Dtype> void PoolingLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) { const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = (*top)[0]->mutable_gpu_data(); int count = (*top)[0]->count(); switch (this->layer_param_.pool()) { case LayerParameter_PoolMethod_MAX: MaxPoolForward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, bottom_data, bottom[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, top_data); break; case LayerParameter_PoolMethod_AVE: AvePoolForward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, bottom_data, bottom[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, top_data); break; default: LOG(FATAL) << "Unknown pooling method."; } CUDA_POST_KERNEL_CHECK; } template <typename Dtype> __global__ void MaxPoolBackward(const int nthreads, const Dtype* bottom_data, const Dtype* top_data, const Dtype* top_diff, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* bottom_diff) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { // find out the local index // find out the local offset int w = index % width; int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; int phend = min(h / stride + 1, pooled_height); int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; Dtype bottom_datum = bottom_data[((n * channels + c) * height + h) * width + w]; top_data += (n * channels + c) * pooled_height * pooled_width; top_diff += (n * channels + c) * pooled_height * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { gradient += top_diff[ph * pooled_width + pw] * (bottom_datum >= top_data[ph * pooled_width + pw] - CAFFE_MAX_POOLING_THRESHOLD); } } bottom_diff[index] = gradient; } // (if index < nthreads) } template <typename Dtype> __global__ void AvePoolBackward(const int nthreads, const Dtype* top_diff, const int num, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int ksize, const int stride, Dtype* bottom_diff) { int index = threadIdx.x + blockIdx.x * blockDim.x; if (index < nthreads) { // find out the local index // find out the local offset int w = index % width; int h = (index / width) % height; int c = (index / width / height) % channels; int n = index / width / height / channels; int phstart = (h < ksize) ? 0 : (h - ksize) / stride + 1; int phend = min(h / stride + 1, pooled_height); int pwstart = (w < ksize) ? 0 : (w - ksize) / stride + 1; int pwend = min(w / stride + 1, pooled_width); Dtype gradient = 0; top_diff += (n * channels + c) * pooled_height * pooled_width; for (int ph = phstart; ph < phend; ++ph) { for (int pw = pwstart; pw < pwend; ++pw) { // figure out the pooling size int poolsize = (min(ph * stride + ksize, height) - ph * stride) * (min(pw * stride + ksize, width) - pw * stride); gradient += top_diff[ph * pooled_width + pw] / poolsize; } } bottom_diff[index] = gradient; } // (if index < nthreads) } template <typename Dtype> Dtype PoolingLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const bool propagate_down, vector<Blob<Dtype>*>* bottom) { if (!propagate_down) { return Dtype(0.); } const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); int count = (*bottom)[0]->count(); switch (this->layer_param_.pool()) { case LayerParameter_PoolMethod_MAX: MaxPoolBackward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, (*bottom)[0]->gpu_data(), top[0]->gpu_data(), top_diff, top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); break; case LayerParameter_PoolMethod_AVE: AvePoolBackward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, top_diff, top[0]->num(), CHANNELS_, HEIGHT_, WIDTH_, POOLED_HEIGHT_, POOLED_WIDTH_, KSIZE_, STRIDE_, bottom_diff); break; default: LOG(FATAL) << "Unknown pooling method."; } CUDA_POST_KERNEL_CHECK; return Dtype(0.); } INSTANTIATE_CLASS(PoolingLayer); } // namespace caffe
8438fffef76b171277420e118aa1e8e1cb0624c1.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "device_launch_parameters.h" #include "header.h" #include <stdio.h> hipError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size); void kringgpu() { } void coutourgpu() { } bool cudaCapable() { int count = 0; hipError_t error = hipGetDeviceCount(&count); if (error == hipErrorNoDevice) { return false; } hipSetDevice(0); } __global__ void addKernel(int *c, const int *a, const int *b) { int i = threadIdx.x; c[i] = a[i] + b[i]; } int exec() { const int arraySize = 5; const int a[arraySize] = { 1, 2, 3, 4, 5 }; const int b[arraySize] = { 10, 20, 30, 40, 50 }; int c[arraySize] = { 0 }; // Add vectors in parallel. hipError_t cudaStatus = addWithCuda(c, a, b, arraySize); if (cudaStatus != hipSuccess) { fprintf(stderr, "addWithCuda failed!"); return 1; } printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n", c[0], c[1], c[2], c[3], c[4]); // hipDeviceReset must be called before exiting in order for profiling and // tracing tools such as Nsight and Visual Profiler to show complete traces. cudaStatus = hipDeviceReset(); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipDeviceReset failed!"); return 1; } return 0; } // Helper function for using CUDA to add vectors in parallel. hipError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size) { int *dev_a = 0; int *dev_b = 0; int *dev_c = 0; hipError_t cudaStatus; // Choose which GPU to run on, change this on a multi-GPU system. cudaStatus = hipSetDevice(0); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipSetDevice failed! Do you have a CUDA-capable GPU installed?"); goto Error; } // Allocate GPU buffers for three vectors (two input, one output) . cudaStatus = hipMalloc((void**)&dev_c, size * sizeof(int)); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipMalloc failed!"); goto Error; } cudaStatus = hipMalloc((void**)&dev_a, size * sizeof(int)); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipMalloc failed!"); goto Error; } cudaStatus = hipMalloc((void**)&dev_b, size * sizeof(int)); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipMalloc failed!"); goto Error; } // Copy input vectors from host memory to GPU buffers. cudaStatus = hipMemcpy(dev_a, a, size * sizeof(int), hipMemcpyHostToDevice); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipMemcpy failed!"); goto Error; } cudaStatus = hipMemcpy(dev_b, b, size * sizeof(int), hipMemcpyHostToDevice); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipMemcpy failed!"); goto Error; } // Launch a kernel on the GPU with one thread for each element. hipLaunchKernelGGL(( addKernel), dim3(1), dim3(size), 0, 0, dev_c, dev_a, dev_b); // Check for any errors launching the kernel cudaStatus = hipGetLastError(); if (cudaStatus != hipSuccess) { fprintf(stderr, "addKernel launch failed: %s\n", hipGetErrorString(cudaStatus)); goto Error; } // hipDeviceSynchronize waits for the kernel to finish, and returns // any errors encountered during the launch. cudaStatus = hipDeviceSynchronize(); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus); goto Error; } // Copy output vector from GPU buffer to host memory. cudaStatus = hipMemcpy(c, dev_c, size * sizeof(int), hipMemcpyDeviceToHost); if (cudaStatus != hipSuccess) { fprintf(stderr, "hipMemcpy failed!"); goto Error; } Error: hipFree(dev_c); hipFree(dev_a); hipFree(dev_b); return cudaStatus; }
8438fffef76b171277420e118aa1e8e1cb0624c1.cu
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include "header.h" #include <stdio.h> cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size); void kringgpu() { } void coutourgpu() { } bool cudaCapable() { int count = 0; cudaError_t error = cudaGetDeviceCount(&count); if (error == cudaErrorNoDevice) { return false; } cudaSetDevice(0); } __global__ void addKernel(int *c, const int *a, const int *b) { int i = threadIdx.x; c[i] = a[i] + b[i]; } int exec() { const int arraySize = 5; const int a[arraySize] = { 1, 2, 3, 4, 5 }; const int b[arraySize] = { 10, 20, 30, 40, 50 }; int c[arraySize] = { 0 }; // Add vectors in parallel. cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize); if (cudaStatus != cudaSuccess) { fprintf(stderr, "addWithCuda failed!"); return 1; } printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n", c[0], c[1], c[2], c[3], c[4]); // cudaDeviceReset must be called before exiting in order for profiling and // tracing tools such as Nsight and Visual Profiler to show complete traces. cudaStatus = cudaDeviceReset(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaDeviceReset failed!"); return 1; } return 0; } // Helper function for using CUDA to add vectors in parallel. cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size) { int *dev_a = 0; int *dev_b = 0; int *dev_c = 0; cudaError_t cudaStatus; // Choose which GPU to run on, change this on a multi-GPU system. cudaStatus = cudaSetDevice(0); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?"); goto Error; } // Allocate GPU buffers for three vectors (two input, one output) . cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int)); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMalloc failed!"); goto Error; } cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int)); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMalloc failed!"); goto Error; } cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int)); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMalloc failed!"); goto Error; } // Copy input vectors from host memory to GPU buffers. cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMemcpy failed!"); goto Error; } cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMemcpy failed!"); goto Error; } // Launch a kernel on the GPU with one thread for each element. addKernel<<<1, size>>>(dev_c, dev_a, dev_b); // Check for any errors launching the kernel cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus)); goto Error; } // cudaDeviceSynchronize waits for the kernel to finish, and returns // any errors encountered during the launch. cudaStatus = cudaDeviceSynchronize(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus); goto Error; } // Copy output vector from GPU buffer to host memory. cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMemcpy failed!"); goto Error; } Error: cudaFree(dev_c); cudaFree(dev_a); cudaFree(dev_b); return cudaStatus; }
57af2f2045f94a83d41e04366d4c781a5050ede5.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <stdint.h> #include <stdio.h> #include <thrust/copy.h> #include <thrust/device_vector.h> #include <thrust/sort.h> #define CHECK(call) \ { \ const hipError_t error = call; \ if (error != hipSuccess) { \ fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ fprintf(stderr, "code: %d, reason: %s\n", error, \ hipGetErrorString(error)); \ exit(1); \ } \ } struct GpuTimer { hipEvent_t start; hipEvent_t stop; GpuTimer() { hipEventCreate(&start); hipEventCreate(&stop); } ~GpuTimer() { hipEventDestroy(start); hipEventDestroy(stop); } void Start() { hipEventRecord(start, 0); hipEventSynchronize(start); } void Stop() { hipEventRecord(stop, 0); } float Elapsed() { float elapsed; hipEventSynchronize(stop); hipEventElapsedTime(&elapsed, start, stop); return elapsed; } }; void printArray(const uint32_t *a, int n) { for (int i = 0; i < n; i++) printf("%2i ", a[i]); printf("\n"); } void sortByThrust(const uint32_t *in, int n, uint32_t *out, int nBits) { thrust::device_vector<uint32_t> dv_out(in, in + n); thrust::sort(dv_out.begin(), dv_out.end()); thrust::copy(dv_out.begin(), dv_out.end(), out); } __global__ void computeHistKernel(uint32_t *in, int n, uint32_t *hist, int nBins, int bit) { // TODO extern __shared__ int s_hist[]; const int i = blockIdx.x * blockDim.x + threadIdx.x; for (int s_i = threadIdx.x; s_i < nBins; s_i += blockDim.x) { s_hist[s_i] = 0; } __syncthreads(); // Each block computes its local hist using atomic on SMEM if (i < n) { int bin = (in[i] >> bit) & (nBins - 1); atomicAdd(&s_hist[bin], 1); } __syncthreads(); // transpose for (int s_i = threadIdx.x; s_i < nBins; s_i += blockDim.x) { hist[gridDim.x * s_i + blockIdx.x] = s_hist[s_i]; } } __global__ void scanBlkKernel(uint32_t *in, int n, uint32_t *out, uint32_t *blkSums) { // TODO int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= n) { return; } extern __shared__ uint32_t s_in[]; s_in[threadIdx.x] = in[i]; __syncthreads(); for (int stride = 1; stride < blockDim.x; stride *= 2) { int strideVal; if (threadIdx.x >= stride) { strideVal = s_in[threadIdx.x - stride]; } __syncthreads(); if (threadIdx.x >= stride) { s_in[threadIdx.x] += strideVal; } __syncthreads(); } if (blkSums && threadIdx.x == blockDim.x - 1) { blkSums[blockIdx.x] = s_in[threadIdx.x]; } out[i] = s_in[threadIdx.x]; } // TODO: You can define necessary functions here __global__ void addBlkSums(uint32_t *in, int n, uint32_t *blkSums) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= n) { return; } in[i] += blkSums[blockIdx.x]; } __global__ void scatter(uint32_t *in, int n, const uint32_t *histScan, uint32_t *out, int nBins, int bit, int nBits) { const int i1 = 2 * blockIdx.x * blockDim.x + threadIdx.x; const int i2 = i1 + blockDim.x; uint32_t s_n = 2 * blockDim.x; //// init smem extern __shared__ uint32_t s_data[]; uint32_t *s_in = (uint32_t *)s_data; uint32_t *s_inBin = (uint32_t *)(s_in + s_n); uint32_t *s_inBinScan = (uint32_t *)(s_inBin + s_n); uint32_t *s_out = (uint32_t *)(s_inBinScan + s_n); uint32_t *s_outBin = (uint32_t *)(s_out + s_n); uint32_t *s_startIdx = (uint32_t *)(s_outBin + s_n); if (i1 < n) { s_in[threadIdx.x] = in[i1]; s_inBin[threadIdx.x] = (s_in[threadIdx.x] >> bit) & (nBins - 1); } else { s_inBin[threadIdx.x] = nBins - 1; } if (i2 < n) { s_in[threadIdx.x + blockDim.x] = in[i2]; s_inBin[threadIdx.x + blockDim.x] = (s_in[threadIdx.x + blockDim.x] >> bit) & (nBins - 1); } else { s_inBin[threadIdx.x + blockDim.x] = nBins - 1; } __syncthreads(); //// sort smem using radix sort with 1-bit for (int b = 0; b < nBits; ++b) { /* printf("Doing bit: #%d\n", b); */ // exclusive scan s_inBinScan[threadIdx.x] = threadIdx.x == 0 ? 0 : (s_inBin[threadIdx.x - 1] >> b) & 1; s_inBinScan[threadIdx.x + blockDim.x] = (s_inBin[threadIdx.x + blockDim.x - 1] >> b) & 1; __syncthreads(); for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { for (int stride = 1; stride < blockDim.x; stride *= 2) { int strideVal; if (threadIdx.x >= stride) { strideVal = s_inBinScan[s_i - stride]; } __syncthreads(); if (threadIdx.x >= stride) { s_inBinScan[s_i] += strideVal; } __syncthreads(); } } s_inBinScan[threadIdx.x + blockDim.x] += s_inBinScan[blockDim.x - 1]; __syncthreads(); // scatter for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { uint32_t rank; if ((s_inBin[s_i] >> b) & 1) { const uint32_t nZeros = s_n - s_inBinScan[s_n - 1] - ((s_inBin[s_n - 1] >> b) & 1); rank = nZeros + s_inBinScan[s_i]; } else { rank = s_i - s_inBinScan[s_i]; } s_outBin[rank] = s_inBin[s_i]; s_out[rank] = s_in[s_i]; } __syncthreads(); for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { s_inBin[s_i] = s_outBin[s_i]; s_in[s_i] = s_out[s_i]; } __syncthreads(); } for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { //// calculate start index if (s_i == 0 || s_inBin[s_i] != s_inBin[s_i - 1]) { s_startIdx[s_inBin[s_i]] = s_i; } } __syncthreads(); if (i1 >= n) { return; } uint32_t s_i, preCount, rank; s_i = threadIdx.x; //// calculate number of elements at lower index that equals to current //// element preCount = s_i - s_startIdx[s_inBin[s_i]]; //// scatter rank = histScan[gridDim.x * s_inBin[s_i] + blockIdx.x] + preCount; out[rank] = s_in[s_i]; if (i2 >= n) { return; } s_i = threadIdx.x + blockDim.x; //// calculate number of elements at lower index that equals to current //// element preCount = s_i - s_startIdx[s_inBin[s_i]]; //// scatter rank = histScan[gridDim.x * s_inBin[s_i] + blockIdx.x] + preCount; out[rank] = s_in[s_i]; } void printDeviceArray(const uint32_t *d_arr, int n) { const int BYTES = n * sizeof(*d_arr); uint32_t *arr = (uint32_t *)malloc(BYTES); hipMemcpy(arr, d_arr, BYTES, hipMemcpyDeviceToHost); printArray(arr, n); free(arr); } void checkCorrectness(uint32_t *out, uint32_t *correctOut, int n) { for (int i = 0; i < n; i++) { if (out[i] != correctOut[i]) { printf("INCORRECT :( %d/%d\n", i, n); return; } } printf("CORRECT :)\n"); } // (Partially) Parallel radix sort: implement parallel histogram and parallel // scan in counting sort Assume: nBits (k in slides) in {1, 2, 4, 8, 16} Why // "int * blockSizes"? Because we may want different block sizes for diffrent // kernels: // blockSizes[0] for the histogram kernel // blockSizes[1] for the scan kernel void sortByDevice(const uint32_t *in, int n, uint32_t *out, int nBits, int *blockSizes) { // TODO const int nBins = 1 << nBits; const dim3 histBlockSize = dim3(blockSizes[0]); const int histBlockCount = (n - 1) / histBlockSize.x + 1; const dim3 histGridSize = dim3(histBlockCount); const dim3 scanBlockSize = dim3(blockSizes[1]); const int scanBlockCount = (nBins * histBlockCount - 1) / scanBlockSize.x + 1; const dim3 scanGridSize = dim3(scanBlockCount); const dim3 scatterBlockSize = dim3(histBlockSize.x / 2); const dim3 scatterGridSize = dim3((n - 1) / (2 * scatterBlockSize.x) + 1); const size_t ARRAY_BYTES = n * sizeof(uint32_t); const size_t HIST_SMEM_BYTES = nBins * sizeof(uint32_t); const size_t HIST_BYTES = histBlockCount * HIST_SMEM_BYTES; const size_t BLKSUMS_BYTES = scanBlockCount * sizeof(uint32_t); const size_t SCAN_SMEM_BYTES = scanBlockSize.x * sizeof(uint32_t); const size_t SCATTER_SMEM_BYTES = (2 * 5 * scatterBlockSize.x + nBins) * sizeof(uint32_t); uint32_t *d_in; uint32_t *d_out; uint32_t *d_hist; // contains all the transposed local histogram of all blocks uint32_t *d_histScan; uint32_t *d_blkSums; uint32_t *blkSums = (uint32_t *)malloc(BLKSUMS_BYTES); CHECK(hipMalloc(&d_in, ARRAY_BYTES)); CHECK(hipMalloc(&d_out, ARRAY_BYTES)); CHECK(hipMalloc(&d_hist, HIST_BYTES)); CHECK(hipMalloc(&d_histScan, HIST_BYTES)); CHECK(hipMalloc(&d_blkSums, BLKSUMS_BYTES)); CHECK(hipMemcpy(d_in, in, ARRAY_BYTES, hipMemcpyHostToDevice)); /* printf("IN: "); */ /* printArray(in, n); */ GpuTimer timer; for (int bit = 0; bit < 8 * sizeof(uint32_t); bit += nBits) { printf("#%d (iteration):\n", bit / nBits + 1); // Step 1: Calculate local histogram of each block, transpose and copy to // d_hist printf(" + Step 1. Local histogram. "); timer.Start(); hipLaunchKernelGGL(( computeHistKernel), dim3(histGridSize), dim3(histBlockSize), HIST_SMEM_BYTES, 0, d_in, n, d_hist, nBins, bit); CHECK(hipGetLastError()); timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); // Step 2: Scan d_hist printf(" + Step 2. Exclusive scan. "); timer.Start(); // scan per block CHECK(hipMemset(d_histScan, 0, sizeof(uint32_t))); hipLaunchKernelGGL(( scanBlkKernel), dim3(scanGridSize), dim3(scanBlockSize), SCAN_SMEM_BYTES, 0, d_hist, histBlockCount * nBins - 1, d_histScan + 1, d_blkSums); CHECK(hipGetLastError()); // scan blksums: CHECK( hipMemcpy(blkSums, d_blkSums, BLKSUMS_BYTES, hipMemcpyDeviceToHost)); for (int i = 1; i < scanBlockCount; ++i) { blkSums[i] += blkSums[i - 1]; } CHECK( hipMemcpy(d_blkSums, blkSums, BLKSUMS_BYTES, hipMemcpyHostToDevice)); // add scanned blkSums hipLaunchKernelGGL(( addBlkSums), dim3(scanGridSize), dim3(scanBlockSize), 0, 0, d_histScan + scanBlockSize.x + 1, histBlockCount * nBins - scanBlockSize.x - 1, d_blkSums); CHECK(hipGetLastError()); timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); // Step 3: scatter printf(" + Step 3. Scatter. "); timer.Start(); hipLaunchKernelGGL(( scatter), dim3(scatterGridSize), dim3(scatterBlockSize), SCATTER_SMEM_BYTES, 0, d_in, n, d_histScan, d_out, nBins, bit, nBits); CHECK(hipGetLastError()); timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); uint32_t *tmp = d_in; d_in = d_out; d_out = tmp; } hipMemcpy(out, d_in, ARRAY_BYTES, hipMemcpyDeviceToHost); free(blkSums); CHECK(hipFree(d_in)); CHECK(hipFree(d_out)); CHECK(hipFree(d_hist)); CHECK(hipFree(d_histScan)); CHECK(hipFree(d_blkSums)); } // Radix sort void sort(const uint32_t *in, int n, uint32_t *out, int nBits, bool useThrust = false, int *blockSizes = NULL) { GpuTimer timer; timer.Start(); if (useThrust == false) { printf("\nRadix sort by thrust\n"); sortByThrust(in, n, out, nBits); } else // use device { printf("\nRadix sort by device\n"); sortByDevice(in, n, out, nBits, blockSizes); } timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); } void printDeviceInfo() { hipDeviceProp_t devProv; CHECK(hipGetDeviceProperties(&devProv, 0)); printf("**********GPU info**********\n"); printf("Name: %s\n", devProv.name); printf("Compute capability: %d.%d\n", devProv.major, devProv.minor); printf("Num SMs: %d\n", devProv.multiProcessorCount); printf("Max num threads per SM: %d\n", devProv.maxThreadsPerMultiProcessor); printf("Max num warps per SM: %d\n", devProv.maxThreadsPerMultiProcessor / devProv.warpSize); printf("GMEM: %zu byte\n", devProv.totalGlobalMem); printf("SMEM per SM: %zu byte\n", devProv.sharedMemPerMultiprocessor); printf("SMEM per block: %zu byte\n", devProv.sharedMemPerBlock); printf("****************************\n"); } int main(int argc, char **argv) { // PRINT OUT DEVICE INFO printDeviceInfo(); // SET UP INPUT SIZE int n = (1 << 24) + 1; /* n = 17; */ printf("\nInput size: %d\n", n); // ALLOCATE MEMORIES size_t bytes = n * sizeof(uint32_t); uint32_t *in = (uint32_t *)malloc(bytes); uint32_t *out = (uint32_t *)malloc(bytes); // Device result uint32_t *correctOut = (uint32_t *)malloc(bytes); // Thrust result // SET UP INPUT DATA for (int i = 0; i < n; i++) in[i] = rand(); /* in[i] = rand() % 16; */ // SET UP NBITS int nBits = 4; // Default if (argc > 1) nBits = atoi(argv[1]); printf("\nNum bits per digit: %d\n", nBits); // DETERMINE BLOCK SIZES int blockSizes[2] = {512, 512}; // One for histogram, one for scan if (argc == 4) { blockSizes[0] = atoi(argv[2]); blockSizes[1] = atoi(argv[3]); } printf("\nHist block size: %d, scan block size: %d\n", blockSizes[0], blockSizes[1]); // SORT BY THRUST sort(in, n, correctOut, nBits); // SORT BY DEVICE sort(in, n, out, nBits, true, blockSizes); checkCorrectness(out, correctOut, n); // FREE MEMORIES free(in); free(out); free(correctOut); return EXIT_SUCCESS; }
57af2f2045f94a83d41e04366d4c781a5050ede5.cu
#include <stdint.h> #include <stdio.h> #include <thrust/copy.h> #include <thrust/device_vector.h> #include <thrust/sort.h> #define CHECK(call) \ { \ const cudaError_t error = call; \ if (error != cudaSuccess) { \ fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ fprintf(stderr, "code: %d, reason: %s\n", error, \ cudaGetErrorString(error)); \ exit(1); \ } \ } struct GpuTimer { cudaEvent_t start; cudaEvent_t stop; GpuTimer() { cudaEventCreate(&start); cudaEventCreate(&stop); } ~GpuTimer() { cudaEventDestroy(start); cudaEventDestroy(stop); } void Start() { cudaEventRecord(start, 0); cudaEventSynchronize(start); } void Stop() { cudaEventRecord(stop, 0); } float Elapsed() { float elapsed; cudaEventSynchronize(stop); cudaEventElapsedTime(&elapsed, start, stop); return elapsed; } }; void printArray(const uint32_t *a, int n) { for (int i = 0; i < n; i++) printf("%2i ", a[i]); printf("\n"); } void sortByThrust(const uint32_t *in, int n, uint32_t *out, int nBits) { thrust::device_vector<uint32_t> dv_out(in, in + n); thrust::sort(dv_out.begin(), dv_out.end()); thrust::copy(dv_out.begin(), dv_out.end(), out); } __global__ void computeHistKernel(uint32_t *in, int n, uint32_t *hist, int nBins, int bit) { // TODO extern __shared__ int s_hist[]; const int i = blockIdx.x * blockDim.x + threadIdx.x; for (int s_i = threadIdx.x; s_i < nBins; s_i += blockDim.x) { s_hist[s_i] = 0; } __syncthreads(); // Each block computes its local hist using atomic on SMEM if (i < n) { int bin = (in[i] >> bit) & (nBins - 1); atomicAdd(&s_hist[bin], 1); } __syncthreads(); // transpose for (int s_i = threadIdx.x; s_i < nBins; s_i += blockDim.x) { hist[gridDim.x * s_i + blockIdx.x] = s_hist[s_i]; } } __global__ void scanBlkKernel(uint32_t *in, int n, uint32_t *out, uint32_t *blkSums) { // TODO int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= n) { return; } extern __shared__ uint32_t s_in[]; s_in[threadIdx.x] = in[i]; __syncthreads(); for (int stride = 1; stride < blockDim.x; stride *= 2) { int strideVal; if (threadIdx.x >= stride) { strideVal = s_in[threadIdx.x - stride]; } __syncthreads(); if (threadIdx.x >= stride) { s_in[threadIdx.x] += strideVal; } __syncthreads(); } if (blkSums && threadIdx.x == blockDim.x - 1) { blkSums[blockIdx.x] = s_in[threadIdx.x]; } out[i] = s_in[threadIdx.x]; } // TODO: You can define necessary functions here __global__ void addBlkSums(uint32_t *in, int n, uint32_t *blkSums) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i >= n) { return; } in[i] += blkSums[blockIdx.x]; } __global__ void scatter(uint32_t *in, int n, const uint32_t *histScan, uint32_t *out, int nBins, int bit, int nBits) { const int i1 = 2 * blockIdx.x * blockDim.x + threadIdx.x; const int i2 = i1 + blockDim.x; uint32_t s_n = 2 * blockDim.x; //// init smem extern __shared__ uint32_t s_data[]; uint32_t *s_in = (uint32_t *)s_data; uint32_t *s_inBin = (uint32_t *)(s_in + s_n); uint32_t *s_inBinScan = (uint32_t *)(s_inBin + s_n); uint32_t *s_out = (uint32_t *)(s_inBinScan + s_n); uint32_t *s_outBin = (uint32_t *)(s_out + s_n); uint32_t *s_startIdx = (uint32_t *)(s_outBin + s_n); if (i1 < n) { s_in[threadIdx.x] = in[i1]; s_inBin[threadIdx.x] = (s_in[threadIdx.x] >> bit) & (nBins - 1); } else { s_inBin[threadIdx.x] = nBins - 1; } if (i2 < n) { s_in[threadIdx.x + blockDim.x] = in[i2]; s_inBin[threadIdx.x + blockDim.x] = (s_in[threadIdx.x + blockDim.x] >> bit) & (nBins - 1); } else { s_inBin[threadIdx.x + blockDim.x] = nBins - 1; } __syncthreads(); //// sort smem using radix sort with 1-bit for (int b = 0; b < nBits; ++b) { /* printf("Doing bit: #%d\n", b); */ // exclusive scan s_inBinScan[threadIdx.x] = threadIdx.x == 0 ? 0 : (s_inBin[threadIdx.x - 1] >> b) & 1; s_inBinScan[threadIdx.x + blockDim.x] = (s_inBin[threadIdx.x + blockDim.x - 1] >> b) & 1; __syncthreads(); for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { for (int stride = 1; stride < blockDim.x; stride *= 2) { int strideVal; if (threadIdx.x >= stride) { strideVal = s_inBinScan[s_i - stride]; } __syncthreads(); if (threadIdx.x >= stride) { s_inBinScan[s_i] += strideVal; } __syncthreads(); } } s_inBinScan[threadIdx.x + blockDim.x] += s_inBinScan[blockDim.x - 1]; __syncthreads(); // scatter for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { uint32_t rank; if ((s_inBin[s_i] >> b) & 1) { const uint32_t nZeros = s_n - s_inBinScan[s_n - 1] - ((s_inBin[s_n - 1] >> b) & 1); rank = nZeros + s_inBinScan[s_i]; } else { rank = s_i - s_inBinScan[s_i]; } s_outBin[rank] = s_inBin[s_i]; s_out[rank] = s_in[s_i]; } __syncthreads(); for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { s_inBin[s_i] = s_outBin[s_i]; s_in[s_i] = s_out[s_i]; } __syncthreads(); } for (int s_i = threadIdx.x; s_i < s_n; s_i += blockDim.x) { //// calculate start index if (s_i == 0 || s_inBin[s_i] != s_inBin[s_i - 1]) { s_startIdx[s_inBin[s_i]] = s_i; } } __syncthreads(); if (i1 >= n) { return; } uint32_t s_i, preCount, rank; s_i = threadIdx.x; //// calculate number of elements at lower index that equals to current //// element preCount = s_i - s_startIdx[s_inBin[s_i]]; //// scatter rank = histScan[gridDim.x * s_inBin[s_i] + blockIdx.x] + preCount; out[rank] = s_in[s_i]; if (i2 >= n) { return; } s_i = threadIdx.x + blockDim.x; //// calculate number of elements at lower index that equals to current //// element preCount = s_i - s_startIdx[s_inBin[s_i]]; //// scatter rank = histScan[gridDim.x * s_inBin[s_i] + blockIdx.x] + preCount; out[rank] = s_in[s_i]; } void printDeviceArray(const uint32_t *d_arr, int n) { const int BYTES = n * sizeof(*d_arr); uint32_t *arr = (uint32_t *)malloc(BYTES); cudaMemcpy(arr, d_arr, BYTES, cudaMemcpyDeviceToHost); printArray(arr, n); free(arr); } void checkCorrectness(uint32_t *out, uint32_t *correctOut, int n) { for (int i = 0; i < n; i++) { if (out[i] != correctOut[i]) { printf("INCORRECT :( %d/%d\n", i, n); return; } } printf("CORRECT :)\n"); } // (Partially) Parallel radix sort: implement parallel histogram and parallel // scan in counting sort Assume: nBits (k in slides) in {1, 2, 4, 8, 16} Why // "int * blockSizes"? Because we may want different block sizes for diffrent // kernels: // blockSizes[0] for the histogram kernel // blockSizes[1] for the scan kernel void sortByDevice(const uint32_t *in, int n, uint32_t *out, int nBits, int *blockSizes) { // TODO const int nBins = 1 << nBits; const dim3 histBlockSize = dim3(blockSizes[0]); const int histBlockCount = (n - 1) / histBlockSize.x + 1; const dim3 histGridSize = dim3(histBlockCount); const dim3 scanBlockSize = dim3(blockSizes[1]); const int scanBlockCount = (nBins * histBlockCount - 1) / scanBlockSize.x + 1; const dim3 scanGridSize = dim3(scanBlockCount); const dim3 scatterBlockSize = dim3(histBlockSize.x / 2); const dim3 scatterGridSize = dim3((n - 1) / (2 * scatterBlockSize.x) + 1); const size_t ARRAY_BYTES = n * sizeof(uint32_t); const size_t HIST_SMEM_BYTES = nBins * sizeof(uint32_t); const size_t HIST_BYTES = histBlockCount * HIST_SMEM_BYTES; const size_t BLKSUMS_BYTES = scanBlockCount * sizeof(uint32_t); const size_t SCAN_SMEM_BYTES = scanBlockSize.x * sizeof(uint32_t); const size_t SCATTER_SMEM_BYTES = (2 * 5 * scatterBlockSize.x + nBins) * sizeof(uint32_t); uint32_t *d_in; uint32_t *d_out; uint32_t *d_hist; // contains all the transposed local histogram of all blocks uint32_t *d_histScan; uint32_t *d_blkSums; uint32_t *blkSums = (uint32_t *)malloc(BLKSUMS_BYTES); CHECK(cudaMalloc(&d_in, ARRAY_BYTES)); CHECK(cudaMalloc(&d_out, ARRAY_BYTES)); CHECK(cudaMalloc(&d_hist, HIST_BYTES)); CHECK(cudaMalloc(&d_histScan, HIST_BYTES)); CHECK(cudaMalloc(&d_blkSums, BLKSUMS_BYTES)); CHECK(cudaMemcpy(d_in, in, ARRAY_BYTES, cudaMemcpyHostToDevice)); /* printf("IN: "); */ /* printArray(in, n); */ GpuTimer timer; for (int bit = 0; bit < 8 * sizeof(uint32_t); bit += nBits) { printf("#%d (iteration):\n", bit / nBits + 1); // Step 1: Calculate local histogram of each block, transpose and copy to // d_hist printf(" + Step 1. Local histogram. "); timer.Start(); computeHistKernel<<<histGridSize, histBlockSize, HIST_SMEM_BYTES>>>( d_in, n, d_hist, nBins, bit); CHECK(cudaGetLastError()); timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); // Step 2: Scan d_hist printf(" + Step 2. Exclusive scan. "); timer.Start(); // scan per block CHECK(cudaMemset(d_histScan, 0, sizeof(uint32_t))); scanBlkKernel<<<scanGridSize, scanBlockSize, SCAN_SMEM_BYTES>>>( d_hist, histBlockCount * nBins - 1, d_histScan + 1, d_blkSums); CHECK(cudaGetLastError()); // scan blksums: CHECK( cudaMemcpy(blkSums, d_blkSums, BLKSUMS_BYTES, cudaMemcpyDeviceToHost)); for (int i = 1; i < scanBlockCount; ++i) { blkSums[i] += blkSums[i - 1]; } CHECK( cudaMemcpy(d_blkSums, blkSums, BLKSUMS_BYTES, cudaMemcpyHostToDevice)); // add scanned blkSums addBlkSums<<<scanGridSize, scanBlockSize>>>( d_histScan + scanBlockSize.x + 1, histBlockCount * nBins - scanBlockSize.x - 1, d_blkSums); CHECK(cudaGetLastError()); timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); // Step 3: scatter printf(" + Step 3. Scatter. "); timer.Start(); scatter<<<scatterGridSize, scatterBlockSize, SCATTER_SMEM_BYTES>>>( d_in, n, d_histScan, d_out, nBins, bit, nBits); CHECK(cudaGetLastError()); timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); uint32_t *tmp = d_in; d_in = d_out; d_out = tmp; } cudaMemcpy(out, d_in, ARRAY_BYTES, cudaMemcpyDeviceToHost); free(blkSums); CHECK(cudaFree(d_in)); CHECK(cudaFree(d_out)); CHECK(cudaFree(d_hist)); CHECK(cudaFree(d_histScan)); CHECK(cudaFree(d_blkSums)); } // Radix sort void sort(const uint32_t *in, int n, uint32_t *out, int nBits, bool useThrust = false, int *blockSizes = NULL) { GpuTimer timer; timer.Start(); if (useThrust == false) { printf("\nRadix sort by thrust\n"); sortByThrust(in, n, out, nBits); } else // use device { printf("\nRadix sort by device\n"); sortByDevice(in, n, out, nBits, blockSizes); } timer.Stop(); printf("Time: %.3f ms\n", timer.Elapsed()); } void printDeviceInfo() { cudaDeviceProp devProv; CHECK(cudaGetDeviceProperties(&devProv, 0)); printf("**********GPU info**********\n"); printf("Name: %s\n", devProv.name); printf("Compute capability: %d.%d\n", devProv.major, devProv.minor); printf("Num SMs: %d\n", devProv.multiProcessorCount); printf("Max num threads per SM: %d\n", devProv.maxThreadsPerMultiProcessor); printf("Max num warps per SM: %d\n", devProv.maxThreadsPerMultiProcessor / devProv.warpSize); printf("GMEM: %zu byte\n", devProv.totalGlobalMem); printf("SMEM per SM: %zu byte\n", devProv.sharedMemPerMultiprocessor); printf("SMEM per block: %zu byte\n", devProv.sharedMemPerBlock); printf("****************************\n"); } int main(int argc, char **argv) { // PRINT OUT DEVICE INFO printDeviceInfo(); // SET UP INPUT SIZE int n = (1 << 24) + 1; /* n = 17; */ printf("\nInput size: %d\n", n); // ALLOCATE MEMORIES size_t bytes = n * sizeof(uint32_t); uint32_t *in = (uint32_t *)malloc(bytes); uint32_t *out = (uint32_t *)malloc(bytes); // Device result uint32_t *correctOut = (uint32_t *)malloc(bytes); // Thrust result // SET UP INPUT DATA for (int i = 0; i < n; i++) in[i] = rand(); /* in[i] = rand() % 16; */ // SET UP NBITS int nBits = 4; // Default if (argc > 1) nBits = atoi(argv[1]); printf("\nNum bits per digit: %d\n", nBits); // DETERMINE BLOCK SIZES int blockSizes[2] = {512, 512}; // One for histogram, one for scan if (argc == 4) { blockSizes[0] = atoi(argv[2]); blockSizes[1] = atoi(argv[3]); } printf("\nHist block size: %d, scan block size: %d\n", blockSizes[0], blockSizes[1]); // SORT BY THRUST sort(in, n, correctOut, nBits); // SORT BY DEVICE sort(in, n, out, nBits, true, blockSizes); checkCorrectness(out, correctOut, n); // FREE MEMORIES free(in); free(out); free(correctOut); return EXIT_SUCCESS; }
1755013da07d3cba17332d29948eec2750aff119.hip
// !!! This is a file automatically generated by hipify!!! /* * Copyright (c) 2021-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <utilities/base_fixture.hpp> #include <utilities/device_comm_wrapper.hpp> #include <utilities/high_res_clock.h> #include <utilities/test_graphs.hpp> #include <utilities/test_utilities.hpp> #include <utilities/thrust_wrapper.hpp> #include <cugraph/algorithms.hpp> #include <cugraph/partition_manager.hpp> #include <cugraph/utilities/dataframe_buffer.cuh> #include <cuco/detail/hash_functions.cuh> #include <cugraph/edge_partition_view.hpp> #include <cugraph/graph_view.hpp> #include <cugraph/prims/edge_partition_src_dst_property.cuh> #include <cugraph/prims/transform_reduce_v_frontier_outgoing_e_by_dst.cuh> #include <cugraph/prims/update_edge_partition_src_dst_property.cuh> #include <cugraph/prims/vertex_frontier.cuh> #include <raft/comms/comms.hpp> #include <raft/comms/mpi_comms.hpp> #include <raft/handle.hpp> #include <rmm/device_scalar.hpp> #include <rmm/device_uvector.hpp> #include <sstream> #include <thrust/copy.h> #include <thrust/count.h> #include <thrust/equal.h> #include <thrust/iterator/counting_iterator.h> #include <gtest/gtest.h> #include <random> template <typename TupleType, typename T, std::size_t... Is> __device__ __host__ auto make_type_casted_tuple_from_scalar(T val, std::index_sequence<Is...>) { return thrust::make_tuple( static_cast<typename thrust::tuple_element<Is, TupleType>::type>(val)...); } template <typename property_t, typename T> __device__ __host__ auto make_property_value(T val) { property_t ret{}; if constexpr (cugraph::is_thrust_tuple_of_arithmetic<property_t>::value) { ret = make_type_casted_tuple_from_scalar<property_t>( val, std::make_index_sequence<thrust::tuple_size<property_t>::value>{}); } else { ret = static_cast<property_t>(val); } return ret; } template <typename vertex_t, typename property_t> struct property_transform_t { int mod{}; constexpr __device__ property_t operator()(vertex_t const v) const { static_assert(cugraph::is_thrust_tuple_of_arithmetic<property_t>::value || std::is_arithmetic_v<property_t>); cuco::detail::MurmurHash3_32<vertex_t> hash_func{}; return make_property_value<property_t>(hash_func(v) % mod); } }; template <typename key_t, typename vertex_t, typename property_t, typename payload_t> struct e_op_t { __device__ auto operator()(key_t optionally_tagged_src, vertex_t dst, property_t src_val, property_t dst_val) const { if constexpr (std::is_same_v<key_t, vertex_t>) { if constexpr (std::is_same_v<payload_t, void>) { return src_val < dst_val ? thrust::optional<std::byte>{std::byte{0}} /* dummy */ : thrust::nullopt; } else { return src_val < dst_val ? thrust::optional<payload_t>{static_cast<payload_t>(1)} : thrust::nullopt; } } else { auto tag = thrust::get<1>(optionally_tagged_src); if constexpr (std::is_same_v<payload_t, void>) { return src_val < dst_val ? thrust::optional<decltype(tag)>{tag} : thrust::nullopt; } else { return src_val < dst_val ? thrust::optional<thrust::tuple<decltype(tag), payload_t>>{thrust::make_tuple( tag, static_cast<payload_t>(1))} : thrust::nullopt; } } } }; struct Prims_Usecase { bool check_correctness{true}; }; template <typename input_usecase_t> class Tests_MG_TransformReduceVFrontierOutgoingEByDst : public ::testing::TestWithParam<std::tuple<Prims_Usecase, input_usecase_t>> { public: Tests_MG_TransformReduceVFrontierOutgoingEByDst() {} static void SetupTestCase() {} static void TearDownTestCase() {} virtual void SetUp() {} virtual void TearDown() {} // Compare the results of transform_reduce_v_frontier_outgoing_e_by_dst primitive template <typename vertex_t, typename edge_t, typename weight_t, typename tag_t, typename payload_t> void run_current_test(Prims_Usecase const& prims_usecase, input_usecase_t const& input_usecase) { using property_t = int32_t; using key_t = std::conditional_t<std::is_same_v<tag_t, void>, vertex_t, thrust::tuple<vertex_t, tag_t>>; static_assert(std::is_same_v<tag_t, void> || std::is_arithmetic_v<tag_t>); static_assert(std::is_same_v<payload_t, void> || cugraph::is_arithmetic_or_thrust_tuple_of_arithmetic<payload_t>::value); if constexpr (cugraph::is_thrust_tuple<payload_t>::value) { static_assert(thrust::tuple_size<payload_t>::value == size_t{2}); } // 1. initialize handle raft::handle_t handle{}; HighResClock hr_clock{}; raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); auto& comm = handle.get_comms(); auto const comm_size = comm.get_size(); auto const comm_rank = comm.get_rank(); auto row_comm_size = static_cast<int>(sqrt(static_cast<double>(comm_size))); while (comm_size % row_comm_size != 0) { --row_comm_size; } cugraph::partition_2d::subcomm_factory_t<cugraph::partition_2d::key_naming_t, vertex_t> subcomm_factory(handle, row_comm_size); // 2. create MG graph constexpr bool is_multi_gpu = true; constexpr bool renumber = true; // needs to be true for multi gpu case constexpr bool store_transposed = false; // needs to be false for using transform_reduce_v_frontier_outgoing_e_by_dst if (cugraph::test::g_perf) { RAFT_CUDA_TRY(hipDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); hr_clock.start(); } auto [mg_graph, mg_renumber_map_labels] = cugraph::test::construct_graph<vertex_t, edge_t, weight_t, store_transposed, is_multi_gpu>( handle, input_usecase, false, renumber); if (cugraph::test::g_perf) { RAFT_CUDA_TRY(hipDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); double elapsed_time{0.0}; hr_clock.stop(&elapsed_time); std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; } auto mg_graph_view = mg_graph.view(); // 3. run MG transform reduce const int hash_bin_count = 5; auto mg_property_buffer = cugraph::allocate_dataframe_buffer<property_t>( mg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); thrust::transform(handle.get_thrust_policy(), (*mg_renumber_map_labels).begin(), (*mg_renumber_map_labels).end(), cugraph::get_dataframe_buffer_begin(mg_property_buffer), property_transform_t<vertex_t, property_t>{hash_bin_count}); cugraph::edge_partition_src_property_t<decltype(mg_graph_view), property_t> mg_src_properties( handle, mg_graph_view); cugraph::edge_partition_dst_property_t<decltype(mg_graph_view), property_t> mg_dst_properties( handle, mg_graph_view); update_edge_partition_src_property(handle, mg_graph_view, cugraph::get_dataframe_buffer_cbegin(mg_property_buffer), mg_src_properties); update_edge_partition_dst_property(handle, mg_graph_view, cugraph::get_dataframe_buffer_cbegin(mg_property_buffer), mg_dst_properties); auto mg_key_buffer = cugraph::allocate_dataframe_buffer<key_t>( mg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); if constexpr (std::is_same_v<tag_t, void>) { thrust::sequence(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_key_buffer), cugraph::get_dataframe_buffer_end(mg_key_buffer), mg_graph_view.local_vertex_partition_range_first()); } else { thrust::tabulate(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_key_buffer), cugraph::get_dataframe_buffer_end(mg_key_buffer), [mg_renumber_map_labels = (*mg_renumber_map_labels).data(), local_vertex_partition_range_first = mg_graph_view.local_vertex_partition_range_first()] __device__(size_t i) { return thrust::make_tuple( static_cast<vertex_t>(local_vertex_partition_range_first + i), static_cast<tag_t>(*(mg_renumber_map_labels + i) % size_t{10})); }); } constexpr size_t bucket_idx_cur = 0; constexpr size_t num_buckets = 1; cugraph::vertex_frontier_t<vertex_t, tag_t, is_multi_gpu> mg_vertex_frontier(handle, num_buckets); mg_vertex_frontier.bucket(bucket_idx_cur) .insert(cugraph::get_dataframe_buffer_begin(mg_key_buffer), cugraph::get_dataframe_buffer_end(mg_key_buffer)); if (cugraph::test::g_perf) { RAFT_CUDA_TRY(hipDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); hr_clock.start(); } auto mg_new_frontier_key_buffer = cugraph::allocate_dataframe_buffer<key_t>(0, handle.get_stream()); [[maybe_unused]] auto mg_payload_buffer = cugraph::detail::allocate_optional_payload_buffer<payload_t>(0, handle.get_stream()); if constexpr (std::is_same_v<payload_t, void>) { mg_new_frontier_key_buffer = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, mg_graph_view, mg_vertex_frontier, bucket_idx_cur, mg_src_properties.device_view(), mg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::null{}); } else { std::tie(mg_new_frontier_key_buffer, mg_payload_buffer) = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, mg_graph_view, mg_vertex_frontier, bucket_idx_cur, mg_src_properties.device_view(), mg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::plus<payload_t>{}); } if (cugraph::test::g_perf) { RAFT_CUDA_TRY(hipDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); double elapsed_time{0.0}; hr_clock.stop(&elapsed_time); std::cout << "MG transform_reduce_v_frontier_outgoing_e_by_dst took " << elapsed_time * 1e-6 << " s.\n"; } // 4. compare SG & MG results if (prims_usecase.check_correctness) { auto mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( handle, (*mg_renumber_map_labels).data(), (*mg_renumber_map_labels).size()); auto mg_aggregate_new_frontier_key_buffer = cugraph::allocate_dataframe_buffer<key_t>(0, handle.get_stream()); if constexpr (std::is_same_v<key_t, vertex_t>) { mg_aggregate_new_frontier_key_buffer = cugraph::test::device_gatherv( handle, mg_new_frontier_key_buffer.data(), mg_new_frontier_key_buffer.size()); } else { std::get<0>(mg_aggregate_new_frontier_key_buffer) = cugraph::test::device_gatherv(handle, std::get<0>(mg_new_frontier_key_buffer).data(), std::get<0>(mg_new_frontier_key_buffer).size()); std::get<1>(mg_aggregate_new_frontier_key_buffer) = cugraph::test::device_gatherv(handle, std::get<1>(mg_new_frontier_key_buffer).data(), std::get<1>(mg_new_frontier_key_buffer).size()); } [[maybe_unused]] auto mg_aggregate_payload_buffer = cugraph::detail::allocate_optional_payload_buffer<payload_t>(0, handle.get_stream()); if constexpr (!std::is_same_v<payload_t, void>) { if constexpr (std::is_arithmetic_v<payload_t>) { mg_aggregate_payload_buffer = cugraph::test::device_gatherv( handle, mg_payload_buffer.data(), mg_payload_buffer.size()); } else { std::get<0>(mg_aggregate_payload_buffer) = cugraph::test::device_gatherv( handle, std::get<0>(mg_payload_buffer).data(), std::get<0>(mg_payload_buffer).size()); std::get<1>(mg_aggregate_payload_buffer) = cugraph::test::device_gatherv( handle, std::get<1>(mg_payload_buffer).data(), std::get<1>(mg_payload_buffer).size()); } } if (handle.get_comms().get_rank() == int{0}) { if constexpr (std::is_same_v<key_t, vertex_t>) { cugraph::unrenumber_int_vertices<vertex_t, !is_multi_gpu>( handle, mg_aggregate_new_frontier_key_buffer.begin(), mg_aggregate_new_frontier_key_buffer.size(), mg_aggregate_renumber_map_labels.data(), std::vector<vertex_t>{mg_graph_view.number_of_vertices()}); } else { cugraph::unrenumber_int_vertices<vertex_t, !is_multi_gpu>( handle, std::get<0>(mg_aggregate_new_frontier_key_buffer).begin(), std::get<0>(mg_aggregate_new_frontier_key_buffer).size(), mg_aggregate_renumber_map_labels.data(), std::vector<vertex_t>{mg_graph_view.number_of_vertices()}); } if constexpr (std::is_same_v<payload_t, void>) { thrust::sort(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_aggregate_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(mg_aggregate_new_frontier_key_buffer)); } else { thrust::sort_by_key( handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_aggregate_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(mg_aggregate_new_frontier_key_buffer), cugraph::get_dataframe_buffer_begin(mg_aggregate_payload_buffer)); } cugraph::graph_t<vertex_t, edge_t, weight_t, store_transposed, !is_multi_gpu> sg_graph( handle); std::tie(sg_graph, std::ignore) = cugraph::test:: construct_graph<vertex_t, edge_t, weight_t, store_transposed, !is_multi_gpu>( handle, input_usecase, false, false); auto sg_graph_view = sg_graph.view(); auto sg_property_buffer = cugraph::allocate_dataframe_buffer<property_t>( sg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); thrust::transform( handle.get_thrust_policy(), thrust::make_counting_iterator(vertex_t{0}), thrust::make_counting_iterator(sg_graph_view.local_vertex_partition_range_size()), cugraph::get_dataframe_buffer_begin(sg_property_buffer), property_transform_t<vertex_t, property_t>{hash_bin_count}); cugraph::edge_partition_src_property_t<decltype(sg_graph_view), property_t> sg_src_properties(handle, sg_graph_view); cugraph::edge_partition_dst_property_t<decltype(sg_graph_view), property_t> sg_dst_properties(handle, sg_graph_view); update_edge_partition_src_property(handle, sg_graph_view, cugraph::get_dataframe_buffer_cbegin(sg_property_buffer), sg_src_properties); update_edge_partition_dst_property(handle, sg_graph_view, cugraph::get_dataframe_buffer_cbegin(sg_property_buffer), sg_dst_properties); auto sg_key_buffer = cugraph::allocate_dataframe_buffer<key_t>( sg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); if constexpr (std::is_same_v<tag_t, void>) { thrust::sequence(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_key_buffer), cugraph::get_dataframe_buffer_end(sg_key_buffer), sg_graph_view.local_vertex_partition_range_first()); } else { thrust::tabulate(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_key_buffer), cugraph::get_dataframe_buffer_end(sg_key_buffer), [] __device__(size_t i) { return thrust::make_tuple( static_cast<vertex_t>(i), static_cast<tag_t>(static_cast<vertex_t>(i) % size_t{10})); }); } cugraph::vertex_frontier_t<vertex_t, tag_t, !is_multi_gpu> sg_vertex_frontier(handle, num_buckets); sg_vertex_frontier.bucket(bucket_idx_cur) .insert(cugraph::get_dataframe_buffer_begin(sg_key_buffer), cugraph::get_dataframe_buffer_end(sg_key_buffer)); auto sg_new_frontier_key_buffer = cugraph::allocate_dataframe_buffer<key_t>(0, handle.get_stream()); [[maybe_unused]] auto sg_payload_buffer = cugraph::detail::allocate_optional_payload_buffer<payload_t>(0, handle.get_stream()); if constexpr (std::is_same_v<payload_t, void>) { sg_new_frontier_key_buffer = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, sg_graph_view, sg_vertex_frontier, bucket_idx_cur, sg_src_properties.device_view(), sg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::null{}); } else { std::tie(sg_new_frontier_key_buffer, sg_payload_buffer) = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, sg_graph_view, sg_vertex_frontier, bucket_idx_cur, sg_src_properties.device_view(), sg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::plus<payload_t>{}); } if constexpr (std::is_same_v<payload_t, void>) { thrust::sort(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(sg_new_frontier_key_buffer)); } else { thrust::sort_by_key(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_begin(sg_payload_buffer)); } bool key_passed = thrust::equal(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_begin(mg_aggregate_new_frontier_key_buffer)); ASSERT_TRUE(key_passed); if constexpr (!std::is_same_v<payload_t, void>) { bool payload_passed = thrust::equal(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_payload_buffer), cugraph::get_dataframe_buffer_begin(sg_payload_buffer), cugraph::get_dataframe_buffer_end(mg_aggregate_payload_buffer)); ASSERT_TRUE(payload_passed); } } } } }; using Tests_MG_TransformReduceVFrontierOutgoingEByDst_File = Tests_MG_TransformReduceVFrontierOutgoingEByDst<cugraph::test::File_Usecase>; using Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat = Tests_MG_TransformReduceVFrontierOutgoingEByDst<cugraph::test::Rmat_Usecase>; TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatVoidVoid) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, void>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatVoidVoid) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, void>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatVoidInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatVoidInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatVoidTupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, thrust::tuple<float, int32_t>>( std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatVoidTupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, thrust::tuple<float, int32_t>>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatInt32Void) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, void>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatInt32Void) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, void>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatInt32TupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, thrust::tuple<float, int32_t>>( std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatInt32TupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, thrust::tuple<float, int32_t>>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int64_t, float, int32_t, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int64_t, float, int32_t, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt64Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int64_t, int64_t, float, int32_t, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt64Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int64_t, int64_t, float, int32_t, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } INSTANTIATE_TEST_SUITE_P( file_test, Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, ::testing::Combine( ::testing::Values(Prims_Usecase{true}), ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); INSTANTIATE_TEST_SUITE_P( rmat_small_test, Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, ::testing::Combine(::testing::Values(Prims_Usecase{true}), ::testing::Values(cugraph::test::Rmat_Usecase( 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); INSTANTIATE_TEST_SUITE_P( rmat_benchmark_test, /* note that scale & edge factor can be overridden in benchmarking (with --gtest_filter to select only the rmat_benchmark_test with a specific vertex & edge type combination) by command line arguments and do not include more than one Rmat_Usecase that differ only in scale or edge factor (to avoid running same benchmarks more than once) */ Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, ::testing::Combine(::testing::Values(Prims_Usecase{false}), ::testing::Values(cugraph::test::Rmat_Usecase( 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); CUGRAPH_MG_TEST_PROGRAM_MAIN()
1755013da07d3cba17332d29948eec2750aff119.cu
/* * Copyright (c) 2021-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <utilities/base_fixture.hpp> #include <utilities/device_comm_wrapper.hpp> #include <utilities/high_res_clock.h> #include <utilities/test_graphs.hpp> #include <utilities/test_utilities.hpp> #include <utilities/thrust_wrapper.hpp> #include <cugraph/algorithms.hpp> #include <cugraph/partition_manager.hpp> #include <cugraph/utilities/dataframe_buffer.cuh> #include <cuco/detail/hash_functions.cuh> #include <cugraph/edge_partition_view.hpp> #include <cugraph/graph_view.hpp> #include <cugraph/prims/edge_partition_src_dst_property.cuh> #include <cugraph/prims/transform_reduce_v_frontier_outgoing_e_by_dst.cuh> #include <cugraph/prims/update_edge_partition_src_dst_property.cuh> #include <cugraph/prims/vertex_frontier.cuh> #include <raft/comms/comms.hpp> #include <raft/comms/mpi_comms.hpp> #include <raft/handle.hpp> #include <rmm/device_scalar.hpp> #include <rmm/device_uvector.hpp> #include <sstream> #include <thrust/copy.h> #include <thrust/count.h> #include <thrust/equal.h> #include <thrust/iterator/counting_iterator.h> #include <gtest/gtest.h> #include <random> template <typename TupleType, typename T, std::size_t... Is> __device__ __host__ auto make_type_casted_tuple_from_scalar(T val, std::index_sequence<Is...>) { return thrust::make_tuple( static_cast<typename thrust::tuple_element<Is, TupleType>::type>(val)...); } template <typename property_t, typename T> __device__ __host__ auto make_property_value(T val) { property_t ret{}; if constexpr (cugraph::is_thrust_tuple_of_arithmetic<property_t>::value) { ret = make_type_casted_tuple_from_scalar<property_t>( val, std::make_index_sequence<thrust::tuple_size<property_t>::value>{}); } else { ret = static_cast<property_t>(val); } return ret; } template <typename vertex_t, typename property_t> struct property_transform_t { int mod{}; constexpr __device__ property_t operator()(vertex_t const v) const { static_assert(cugraph::is_thrust_tuple_of_arithmetic<property_t>::value || std::is_arithmetic_v<property_t>); cuco::detail::MurmurHash3_32<vertex_t> hash_func{}; return make_property_value<property_t>(hash_func(v) % mod); } }; template <typename key_t, typename vertex_t, typename property_t, typename payload_t> struct e_op_t { __device__ auto operator()(key_t optionally_tagged_src, vertex_t dst, property_t src_val, property_t dst_val) const { if constexpr (std::is_same_v<key_t, vertex_t>) { if constexpr (std::is_same_v<payload_t, void>) { return src_val < dst_val ? thrust::optional<std::byte>{std::byte{0}} /* dummy */ : thrust::nullopt; } else { return src_val < dst_val ? thrust::optional<payload_t>{static_cast<payload_t>(1)} : thrust::nullopt; } } else { auto tag = thrust::get<1>(optionally_tagged_src); if constexpr (std::is_same_v<payload_t, void>) { return src_val < dst_val ? thrust::optional<decltype(tag)>{tag} : thrust::nullopt; } else { return src_val < dst_val ? thrust::optional<thrust::tuple<decltype(tag), payload_t>>{thrust::make_tuple( tag, static_cast<payload_t>(1))} : thrust::nullopt; } } } }; struct Prims_Usecase { bool check_correctness{true}; }; template <typename input_usecase_t> class Tests_MG_TransformReduceVFrontierOutgoingEByDst : public ::testing::TestWithParam<std::tuple<Prims_Usecase, input_usecase_t>> { public: Tests_MG_TransformReduceVFrontierOutgoingEByDst() {} static void SetupTestCase() {} static void TearDownTestCase() {} virtual void SetUp() {} virtual void TearDown() {} // Compare the results of transform_reduce_v_frontier_outgoing_e_by_dst primitive template <typename vertex_t, typename edge_t, typename weight_t, typename tag_t, typename payload_t> void run_current_test(Prims_Usecase const& prims_usecase, input_usecase_t const& input_usecase) { using property_t = int32_t; using key_t = std::conditional_t<std::is_same_v<tag_t, void>, vertex_t, thrust::tuple<vertex_t, tag_t>>; static_assert(std::is_same_v<tag_t, void> || std::is_arithmetic_v<tag_t>); static_assert(std::is_same_v<payload_t, void> || cugraph::is_arithmetic_or_thrust_tuple_of_arithmetic<payload_t>::value); if constexpr (cugraph::is_thrust_tuple<payload_t>::value) { static_assert(thrust::tuple_size<payload_t>::value == size_t{2}); } // 1. initialize handle raft::handle_t handle{}; HighResClock hr_clock{}; raft::comms::initialize_mpi_comms(&handle, MPI_COMM_WORLD); auto& comm = handle.get_comms(); auto const comm_size = comm.get_size(); auto const comm_rank = comm.get_rank(); auto row_comm_size = static_cast<int>(sqrt(static_cast<double>(comm_size))); while (comm_size % row_comm_size != 0) { --row_comm_size; } cugraph::partition_2d::subcomm_factory_t<cugraph::partition_2d::key_naming_t, vertex_t> subcomm_factory(handle, row_comm_size); // 2. create MG graph constexpr bool is_multi_gpu = true; constexpr bool renumber = true; // needs to be true for multi gpu case constexpr bool store_transposed = false; // needs to be false for using transform_reduce_v_frontier_outgoing_e_by_dst if (cugraph::test::g_perf) { RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); hr_clock.start(); } auto [mg_graph, mg_renumber_map_labels] = cugraph::test::construct_graph<vertex_t, edge_t, weight_t, store_transposed, is_multi_gpu>( handle, input_usecase, false, renumber); if (cugraph::test::g_perf) { RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); double elapsed_time{0.0}; hr_clock.stop(&elapsed_time); std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n"; } auto mg_graph_view = mg_graph.view(); // 3. run MG transform reduce const int hash_bin_count = 5; auto mg_property_buffer = cugraph::allocate_dataframe_buffer<property_t>( mg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); thrust::transform(handle.get_thrust_policy(), (*mg_renumber_map_labels).begin(), (*mg_renumber_map_labels).end(), cugraph::get_dataframe_buffer_begin(mg_property_buffer), property_transform_t<vertex_t, property_t>{hash_bin_count}); cugraph::edge_partition_src_property_t<decltype(mg_graph_view), property_t> mg_src_properties( handle, mg_graph_view); cugraph::edge_partition_dst_property_t<decltype(mg_graph_view), property_t> mg_dst_properties( handle, mg_graph_view); update_edge_partition_src_property(handle, mg_graph_view, cugraph::get_dataframe_buffer_cbegin(mg_property_buffer), mg_src_properties); update_edge_partition_dst_property(handle, mg_graph_view, cugraph::get_dataframe_buffer_cbegin(mg_property_buffer), mg_dst_properties); auto mg_key_buffer = cugraph::allocate_dataframe_buffer<key_t>( mg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); if constexpr (std::is_same_v<tag_t, void>) { thrust::sequence(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_key_buffer), cugraph::get_dataframe_buffer_end(mg_key_buffer), mg_graph_view.local_vertex_partition_range_first()); } else { thrust::tabulate(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_key_buffer), cugraph::get_dataframe_buffer_end(mg_key_buffer), [mg_renumber_map_labels = (*mg_renumber_map_labels).data(), local_vertex_partition_range_first = mg_graph_view.local_vertex_partition_range_first()] __device__(size_t i) { return thrust::make_tuple( static_cast<vertex_t>(local_vertex_partition_range_first + i), static_cast<tag_t>(*(mg_renumber_map_labels + i) % size_t{10})); }); } constexpr size_t bucket_idx_cur = 0; constexpr size_t num_buckets = 1; cugraph::vertex_frontier_t<vertex_t, tag_t, is_multi_gpu> mg_vertex_frontier(handle, num_buckets); mg_vertex_frontier.bucket(bucket_idx_cur) .insert(cugraph::get_dataframe_buffer_begin(mg_key_buffer), cugraph::get_dataframe_buffer_end(mg_key_buffer)); if (cugraph::test::g_perf) { RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); hr_clock.start(); } auto mg_new_frontier_key_buffer = cugraph::allocate_dataframe_buffer<key_t>(0, handle.get_stream()); [[maybe_unused]] auto mg_payload_buffer = cugraph::detail::allocate_optional_payload_buffer<payload_t>(0, handle.get_stream()); if constexpr (std::is_same_v<payload_t, void>) { mg_new_frontier_key_buffer = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, mg_graph_view, mg_vertex_frontier, bucket_idx_cur, mg_src_properties.device_view(), mg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::null{}); } else { std::tie(mg_new_frontier_key_buffer, mg_payload_buffer) = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, mg_graph_view, mg_vertex_frontier, bucket_idx_cur, mg_src_properties.device_view(), mg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::plus<payload_t>{}); } if (cugraph::test::g_perf) { RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement handle.get_comms().barrier(); double elapsed_time{0.0}; hr_clock.stop(&elapsed_time); std::cout << "MG transform_reduce_v_frontier_outgoing_e_by_dst took " << elapsed_time * 1e-6 << " s.\n"; } // 4. compare SG & MG results if (prims_usecase.check_correctness) { auto mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv( handle, (*mg_renumber_map_labels).data(), (*mg_renumber_map_labels).size()); auto mg_aggregate_new_frontier_key_buffer = cugraph::allocate_dataframe_buffer<key_t>(0, handle.get_stream()); if constexpr (std::is_same_v<key_t, vertex_t>) { mg_aggregate_new_frontier_key_buffer = cugraph::test::device_gatherv( handle, mg_new_frontier_key_buffer.data(), mg_new_frontier_key_buffer.size()); } else { std::get<0>(mg_aggregate_new_frontier_key_buffer) = cugraph::test::device_gatherv(handle, std::get<0>(mg_new_frontier_key_buffer).data(), std::get<0>(mg_new_frontier_key_buffer).size()); std::get<1>(mg_aggregate_new_frontier_key_buffer) = cugraph::test::device_gatherv(handle, std::get<1>(mg_new_frontier_key_buffer).data(), std::get<1>(mg_new_frontier_key_buffer).size()); } [[maybe_unused]] auto mg_aggregate_payload_buffer = cugraph::detail::allocate_optional_payload_buffer<payload_t>(0, handle.get_stream()); if constexpr (!std::is_same_v<payload_t, void>) { if constexpr (std::is_arithmetic_v<payload_t>) { mg_aggregate_payload_buffer = cugraph::test::device_gatherv( handle, mg_payload_buffer.data(), mg_payload_buffer.size()); } else { std::get<0>(mg_aggregate_payload_buffer) = cugraph::test::device_gatherv( handle, std::get<0>(mg_payload_buffer).data(), std::get<0>(mg_payload_buffer).size()); std::get<1>(mg_aggregate_payload_buffer) = cugraph::test::device_gatherv( handle, std::get<1>(mg_payload_buffer).data(), std::get<1>(mg_payload_buffer).size()); } } if (handle.get_comms().get_rank() == int{0}) { if constexpr (std::is_same_v<key_t, vertex_t>) { cugraph::unrenumber_int_vertices<vertex_t, !is_multi_gpu>( handle, mg_aggregate_new_frontier_key_buffer.begin(), mg_aggregate_new_frontier_key_buffer.size(), mg_aggregate_renumber_map_labels.data(), std::vector<vertex_t>{mg_graph_view.number_of_vertices()}); } else { cugraph::unrenumber_int_vertices<vertex_t, !is_multi_gpu>( handle, std::get<0>(mg_aggregate_new_frontier_key_buffer).begin(), std::get<0>(mg_aggregate_new_frontier_key_buffer).size(), mg_aggregate_renumber_map_labels.data(), std::vector<vertex_t>{mg_graph_view.number_of_vertices()}); } if constexpr (std::is_same_v<payload_t, void>) { thrust::sort(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_aggregate_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(mg_aggregate_new_frontier_key_buffer)); } else { thrust::sort_by_key( handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(mg_aggregate_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(mg_aggregate_new_frontier_key_buffer), cugraph::get_dataframe_buffer_begin(mg_aggregate_payload_buffer)); } cugraph::graph_t<vertex_t, edge_t, weight_t, store_transposed, !is_multi_gpu> sg_graph( handle); std::tie(sg_graph, std::ignore) = cugraph::test:: construct_graph<vertex_t, edge_t, weight_t, store_transposed, !is_multi_gpu>( handle, input_usecase, false, false); auto sg_graph_view = sg_graph.view(); auto sg_property_buffer = cugraph::allocate_dataframe_buffer<property_t>( sg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); thrust::transform( handle.get_thrust_policy(), thrust::make_counting_iterator(vertex_t{0}), thrust::make_counting_iterator(sg_graph_view.local_vertex_partition_range_size()), cugraph::get_dataframe_buffer_begin(sg_property_buffer), property_transform_t<vertex_t, property_t>{hash_bin_count}); cugraph::edge_partition_src_property_t<decltype(sg_graph_view), property_t> sg_src_properties(handle, sg_graph_view); cugraph::edge_partition_dst_property_t<decltype(sg_graph_view), property_t> sg_dst_properties(handle, sg_graph_view); update_edge_partition_src_property(handle, sg_graph_view, cugraph::get_dataframe_buffer_cbegin(sg_property_buffer), sg_src_properties); update_edge_partition_dst_property(handle, sg_graph_view, cugraph::get_dataframe_buffer_cbegin(sg_property_buffer), sg_dst_properties); auto sg_key_buffer = cugraph::allocate_dataframe_buffer<key_t>( sg_graph_view.local_vertex_partition_range_size(), handle.get_stream()); if constexpr (std::is_same_v<tag_t, void>) { thrust::sequence(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_key_buffer), cugraph::get_dataframe_buffer_end(sg_key_buffer), sg_graph_view.local_vertex_partition_range_first()); } else { thrust::tabulate(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_key_buffer), cugraph::get_dataframe_buffer_end(sg_key_buffer), [] __device__(size_t i) { return thrust::make_tuple( static_cast<vertex_t>(i), static_cast<tag_t>(static_cast<vertex_t>(i) % size_t{10})); }); } cugraph::vertex_frontier_t<vertex_t, tag_t, !is_multi_gpu> sg_vertex_frontier(handle, num_buckets); sg_vertex_frontier.bucket(bucket_idx_cur) .insert(cugraph::get_dataframe_buffer_begin(sg_key_buffer), cugraph::get_dataframe_buffer_end(sg_key_buffer)); auto sg_new_frontier_key_buffer = cugraph::allocate_dataframe_buffer<key_t>(0, handle.get_stream()); [[maybe_unused]] auto sg_payload_buffer = cugraph::detail::allocate_optional_payload_buffer<payload_t>(0, handle.get_stream()); if constexpr (std::is_same_v<payload_t, void>) { sg_new_frontier_key_buffer = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, sg_graph_view, sg_vertex_frontier, bucket_idx_cur, sg_src_properties.device_view(), sg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::null{}); } else { std::tie(sg_new_frontier_key_buffer, sg_payload_buffer) = cugraph::transform_reduce_v_frontier_outgoing_e_by_dst( handle, sg_graph_view, sg_vertex_frontier, bucket_idx_cur, sg_src_properties.device_view(), sg_dst_properties.device_view(), e_op_t<key_t, vertex_t, property_t, payload_t>{}, cugraph::reduce_op::plus<payload_t>{}); } if constexpr (std::is_same_v<payload_t, void>) { thrust::sort(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(sg_new_frontier_key_buffer)); } else { thrust::sort_by_key(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_begin(sg_payload_buffer)); } bool key_passed = thrust::equal(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_end(sg_new_frontier_key_buffer), cugraph::get_dataframe_buffer_begin(mg_aggregate_new_frontier_key_buffer)); ASSERT_TRUE(key_passed); if constexpr (!std::is_same_v<payload_t, void>) { bool payload_passed = thrust::equal(handle.get_thrust_policy(), cugraph::get_dataframe_buffer_begin(sg_payload_buffer), cugraph::get_dataframe_buffer_begin(sg_payload_buffer), cugraph::get_dataframe_buffer_end(mg_aggregate_payload_buffer)); ASSERT_TRUE(payload_passed); } } } } }; using Tests_MG_TransformReduceVFrontierOutgoingEByDst_File = Tests_MG_TransformReduceVFrontierOutgoingEByDst<cugraph::test::File_Usecase>; using Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat = Tests_MG_TransformReduceVFrontierOutgoingEByDst<cugraph::test::Rmat_Usecase>; TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatVoidVoid) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, void>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatVoidVoid) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, void>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatVoidInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatVoidInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatVoidTupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, thrust::tuple<float, int32_t>>( std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatVoidTupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, void, thrust::tuple<float, int32_t>>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatInt32Void) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, void>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatInt32Void) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, void>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int32FloatInt32TupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, thrust::tuple<float, int32_t>>( std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int32FloatInt32TupleFloatInt32) { auto param = GetParam(); run_current_test<int32_t, int32_t, float, int32_t, thrust::tuple<float, int32_t>>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt32Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int64_t, float, int32_t, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt32Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int32_t, int64_t, float, int32_t, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, CheckInt64Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int64_t, int64_t, float, int32_t, int32_t>(std::get<0>(param), std::get<1>(param)); } TEST_P(Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, CheckInt64Int64FloatInt32Int32) { auto param = GetParam(); run_current_test<int64_t, int64_t, float, int32_t, int32_t>( std::get<0>(param), cugraph::test::override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param))); } INSTANTIATE_TEST_SUITE_P( file_test, Tests_MG_TransformReduceVFrontierOutgoingEByDst_File, ::testing::Combine( ::testing::Values(Prims_Usecase{true}), ::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"), cugraph::test::File_Usecase("test/datasets/web-Google.mtx"), cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"), cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx")))); INSTANTIATE_TEST_SUITE_P( rmat_small_test, Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, ::testing::Combine(::testing::Values(Prims_Usecase{true}), ::testing::Values(cugraph::test::Rmat_Usecase( 10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); INSTANTIATE_TEST_SUITE_P( rmat_benchmark_test, /* note that scale & edge factor can be overridden in benchmarking (with --gtest_filter to select only the rmat_benchmark_test with a specific vertex & edge type combination) by command line arguments and do not include more than one Rmat_Usecase that differ only in scale or edge factor (to avoid running same benchmarks more than once) */ Tests_MG_TransformReduceVFrontierOutgoingEByDst_Rmat, ::testing::Combine(::testing::Values(Prims_Usecase{false}), ::testing::Values(cugraph::test::Rmat_Usecase( 20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true)))); CUGRAPH_MG_TEST_PROGRAM_MAIN()
7b75a283b7c2a23522140300398878171e30c1e9.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /*WBL 21 March 2009 $Revision: 1.3 $ * based on cuda/sdk/projects/quasirandomGenerator/quasirandomGenerator_SM13.cu */ #define DOUBLE_PRECISION #include "CUDAParkMillerRNG.hpp" //Fast integer multiplication #define MUL(a, b) __umul24(a, b) //////////////////////////////////////////////////////////////////////////////// // Park-Miller quasirandom number generation kernel //////////////////////////////////////////////////////////////////////////////// static __global__ void parkmillerKernel(int *d_Output, unsigned int seed, int cycles, unsigned int N){ unsigned int tid = MUL(blockDim.x, blockIdx.x) + threadIdx.x; unsigned int threadN = MUL(blockDim.x, gridDim.x); double const a = 16807; //ie 7**5 double const m = 2147483647; //ie 2**31-1 double const reciprocal_m = 1.0/m; // W. Langdon cs.ucl.ac.uk 5 May 1994 for(unsigned int pos = tid; pos < N; pos += threadN){ unsigned int result = 0; unsigned int data = seed + pos; for(int i = 0; i < cycles; i++) { double temp = data * a; result = (int) (temp - m * floor ( temp * reciprocal_m )); data = result; } //endfor //d_Output[MUL(threadIdx.y, N) + pos] = (float)(result + 1) * INT_SCALE; //d_Output[MUL(threadIdx.y, N) + pos] = result; d_Output[pos] = result; } } //Host-side interface extern "C" void parkmillerGPU(int *d_Output, unsigned int seed, int cycles, unsigned int grid_size, unsigned int block_size, unsigned int N){ hipLaunchKernelGGL(( parkmillerKernel), dim3(grid_size), dim3(block_size), 0, 0, d_Output, seed, cycles, N); cutilCheckMsg("parkmillerKernel() execution failed.\n"); }
7b75a283b7c2a23522140300398878171e30c1e9.cu
/*WBL 21 March 2009 $Revision: 1.3 $ * based on cuda/sdk/projects/quasirandomGenerator/quasirandomGenerator_SM13.cu */ #define DOUBLE_PRECISION #include "CUDAParkMillerRNG.hpp" //Fast integer multiplication #define MUL(a, b) __umul24(a, b) //////////////////////////////////////////////////////////////////////////////// // Park-Miller quasirandom number generation kernel //////////////////////////////////////////////////////////////////////////////// static __global__ void parkmillerKernel(int *d_Output, unsigned int seed, int cycles, unsigned int N){ unsigned int tid = MUL(blockDim.x, blockIdx.x) + threadIdx.x; unsigned int threadN = MUL(blockDim.x, gridDim.x); double const a = 16807; //ie 7**5 double const m = 2147483647; //ie 2**31-1 double const reciprocal_m = 1.0/m; // W. Langdon cs.ucl.ac.uk 5 May 1994 for(unsigned int pos = tid; pos < N; pos += threadN){ unsigned int result = 0; unsigned int data = seed + pos; for(int i = 0; i < cycles; i++) { double temp = data * a; result = (int) (temp - m * floor ( temp * reciprocal_m )); data = result; } //endfor //d_Output[MUL(threadIdx.y, N) + pos] = (float)(result + 1) * INT_SCALE; //d_Output[MUL(threadIdx.y, N) + pos] = result; d_Output[pos] = result; } } //Host-side interface extern "C" void parkmillerGPU(int *d_Output, unsigned int seed, int cycles, unsigned int grid_size, unsigned int block_size, unsigned int N){ parkmillerKernel<<<grid_size, block_size>>>(d_Output, seed, cycles, N); cutilCheckMsg("parkmillerKernel() execution failed.\n"); }
4b199f223c5fd7e237f0af3a93959abc81b7c9a6.hip
// !!! This is a file automatically generated by hipify!!! /* SAXPY accelerated with CUDA. In this version CUDA error checks and timing calls have been added. Compile with: nvcc -arch=sm_20 -O2 saxpy_cuda_timed.cu */ #include <hip/hip_runtime.h> /* CUDA runtime API */ #include <cstdio> #include <sys/time.h> #include <time.h> int timeval_subtract (double *result, struct timeval *x, struct timeval *y) { struct timeval result0; /* Perform the carry for the later subtraction by updating y. */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (y->tv_usec - x->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* Compute the time remaining to wait. tv_usec is certainly positive. */ result0.tv_sec = x->tv_sec - y->tv_sec; result0.tv_usec = x->tv_usec - y->tv_usec; *result = ((double)result0.tv_usec)/1e6 + (double)result0.tv_sec; /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; } void saxpy_cpu(float *vecY, float *vecX, float alpha, int n) { int i; for (i = 0; i < n; i++) vecY[i] = alpha * vecX[i] + vecY[i]; } __global__ void saxpy_gpu(float *vecY, float *vecX, float alpha ,int n) { int i; i = blockIdx.x * blockDim.x + threadIdx.x; if (i<n) vecY[i] = alpha * vecX[i] + vecY[i]; } int main(int argc, char *argv[]) { float *x_host, *y_host; /* arrays for computation on host*/ float *x_dev, *y_dev; /* arrays for computation on device */ float *y_shadow; /* host-side copy of device results */ int n = 32*1024; float alpha = 0.5f; int nerror; size_t memsize; int i, blockSize, nBlocks; int error; double restime; struct timeval tdr0, tdr1; hipEvent_t start, stop; float kernel_timer; memsize = n * sizeof(float); /* allocate arrays on host */ x_host = (float *)malloc(memsize); y_host = (float *)malloc(memsize); y_shadow = (float *)malloc(memsize); /* allocate arrays on device */ if(error = hipMalloc((void **) &x_dev, memsize)) { printf ("Error in hipMalloc %d\n", error); exit (error); } if(error = hipMalloc((void **) &y_dev, memsize)) { printf ("Error in hipMalloc %d\n", error); exit (error); } /* catch any errors */ /* initialize arrays on host */ for ( i = 0; i < n; i++) { x_host[i] = rand() / (float)RAND_MAX; y_host[i] = rand() / (float)RAND_MAX; } /* copy arrays to device memory (synchronous) */ gettimeofday (&tdr0, NULL); if (error = hipMemcpy(x_dev, x_host, memsize, hipMemcpyHostToDevice)) { printf ("Error %d\n", error); exit (error); } if (error = hipMemcpy(y_dev, y_host, memsize, hipMemcpyHostToDevice)) { printf ("Error %d\n", error); exit (error); } /* set up device execution configuration */ blockSize = 512; nBlocks = n / blockSize + (n % blockSize > 0); /* execute kernel (asynchronous!) */ hipEventCreate(&start); hipEventCreate(&stop); hipEventRecord(start, 0); hipLaunchKernelGGL(( saxpy_gpu), dim3(nBlocks), dim3(blockSize), 0, 0, y_dev, x_dev, alpha, n); hipEventRecord(stop, 0); hipEventSynchronize( stop ); hipEventElapsedTime( &kernel_timer, start, stop ); printf("Test Kernel took %f ms\n",kernel_timer); /* check success of kernel */ if(error = hipGetLastError()) { printf ("Error detected after kernel %d\n", error); exit (error); } hipEventDestroy(start); hipEventDestroy(stop); /* retrieve results from device (synchronous) */ if (error = hipMemcpy(y_shadow, y_dev, memsize, hipMemcpyDeviceToHost)) { printf ("Error %d\n", error); exit (error); } /* check if GPU calculation completed without error */ if (error = hipDeviceSynchronize()) { printf ("Error %d\n", error); exit (error); } gettimeofday (&tdr1, NULL); timeval_subtract (&restime, &tdr1, &tdr0); printf ("gpu kernel and memcopy %e s\n", restime); gettimeofday (&tdr0, NULL); /* execute host version (i.e. baseline reference results) */ saxpy_cpu(y_host, x_host, alpha, n); gettimeofday (&tdr1, NULL); timeval_subtract (&restime, &tdr1, &tdr0); printf ("cpu kernel %e s\n", restime); /* check results */ nerror=0; for(i=0; i < n; i++) { if(y_shadow[i]!=y_host[i]) nerror=nerror+1; } printf("test comparison shows %d errors\n",nerror); /* free memory */ hipFree(x_dev); hipFree(y_dev); free(x_host); free(y_host); free(y_shadow); return 0; }
4b199f223c5fd7e237f0af3a93959abc81b7c9a6.cu
/* SAXPY accelerated with CUDA. In this version CUDA error checks and timing calls have been added. Compile with: nvcc -arch=sm_20 -O2 saxpy_cuda_timed.cu */ #include <cuda.h> /* CUDA runtime API */ #include <cstdio> #include <sys/time.h> #include <time.h> int timeval_subtract (double *result, struct timeval *x, struct timeval *y) { struct timeval result0; /* Perform the carry for the later subtraction by updating y. */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (y->tv_usec - x->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* Compute the time remaining to wait. tv_usec is certainly positive. */ result0.tv_sec = x->tv_sec - y->tv_sec; result0.tv_usec = x->tv_usec - y->tv_usec; *result = ((double)result0.tv_usec)/1e6 + (double)result0.tv_sec; /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; } void saxpy_cpu(float *vecY, float *vecX, float alpha, int n) { int i; for (i = 0; i < n; i++) vecY[i] = alpha * vecX[i] + vecY[i]; } __global__ void saxpy_gpu(float *vecY, float *vecX, float alpha ,int n) { int i; i = blockIdx.x * blockDim.x + threadIdx.x; if (i<n) vecY[i] = alpha * vecX[i] + vecY[i]; } int main(int argc, char *argv[]) { float *x_host, *y_host; /* arrays for computation on host*/ float *x_dev, *y_dev; /* arrays for computation on device */ float *y_shadow; /* host-side copy of device results */ int n = 32*1024; float alpha = 0.5f; int nerror; size_t memsize; int i, blockSize, nBlocks; int error; double restime; struct timeval tdr0, tdr1; cudaEvent_t start, stop; float kernel_timer; memsize = n * sizeof(float); /* allocate arrays on host */ x_host = (float *)malloc(memsize); y_host = (float *)malloc(memsize); y_shadow = (float *)malloc(memsize); /* allocate arrays on device */ if(error = cudaMalloc((void **) &x_dev, memsize)) { printf ("Error in cudaMalloc %d\n", error); exit (error); } if(error = cudaMalloc((void **) &y_dev, memsize)) { printf ("Error in cudaMalloc %d\n", error); exit (error); } /* catch any errors */ /* initialize arrays on host */ for ( i = 0; i < n; i++) { x_host[i] = rand() / (float)RAND_MAX; y_host[i] = rand() / (float)RAND_MAX; } /* copy arrays to device memory (synchronous) */ gettimeofday (&tdr0, NULL); if (error = cudaMemcpy(x_dev, x_host, memsize, cudaMemcpyHostToDevice)) { printf ("Error %d\n", error); exit (error); } if (error = cudaMemcpy(y_dev, y_host, memsize, cudaMemcpyHostToDevice)) { printf ("Error %d\n", error); exit (error); } /* set up device execution configuration */ blockSize = 512; nBlocks = n / blockSize + (n % blockSize > 0); /* execute kernel (asynchronous!) */ cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start, 0); saxpy_gpu<<<nBlocks, blockSize>>>(y_dev, x_dev, alpha, n); cudaEventRecord(stop, 0); cudaEventSynchronize( stop ); cudaEventElapsedTime( &kernel_timer, start, stop ); printf("Test Kernel took %f ms\n",kernel_timer); /* check success of kernel */ if(error = cudaGetLastError()) { printf ("Error detected after kernel %d\n", error); exit (error); } cudaEventDestroy(start); cudaEventDestroy(stop); /* retrieve results from device (synchronous) */ if (error = cudaMemcpy(y_shadow, y_dev, memsize, cudaMemcpyDeviceToHost)) { printf ("Error %d\n", error); exit (error); } /* check if GPU calculation completed without error */ if (error = cudaDeviceSynchronize()) { printf ("Error %d\n", error); exit (error); } gettimeofday (&tdr1, NULL); timeval_subtract (&restime, &tdr1, &tdr0); printf ("gpu kernel and memcopy %e s\n", restime); gettimeofday (&tdr0, NULL); /* execute host version (i.e. baseline reference results) */ saxpy_cpu(y_host, x_host, alpha, n); gettimeofday (&tdr1, NULL); timeval_subtract (&restime, &tdr1, &tdr0); printf ("cpu kernel %e s\n", restime); /* check results */ nerror=0; for(i=0; i < n; i++) { if(y_shadow[i]!=y_host[i]) nerror=nerror+1; } printf("test comparison shows %d errors\n",nerror); /* free memory */ cudaFree(x_dev); cudaFree(y_dev); free(x_host); free(y_host); free(y_shadow); return 0; }
3fc55c6040827c60b8fea0124fd6ae4fe588c462.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" //#define REARRANGED_DOMAIN #ifndef USING_HOST_MACRO #define BLOCK_SIZE 8 #endif __device__ int limit_gradient(double *dqv, double qmin, double qmax, double beta_w) { int i; double r = 1000.0, r0 = 1.0, phi = 1.0; double TINY = 1.0e-100; // to avoid machine accuracy problems. for (i = 0; i < 3; i++) { if (dqv[i]<-TINY) r0 = qmin / dqv[i]; if (dqv[i] > TINY) r0 = qmax / dqv[i]; r = min(r0, r); } phi = min(r*beta_w, 1.0); //for (i=0;i<3;i++) dqv[0] = dqv[0] * phi; dqv[1] = dqv[1] * phi; dqv[2] = dqv[2] * phi; return 0; } __device__ int find_qmin_and_qmax(double dq0, double dq1, double dq2, double *qmin, double *qmax) { if (dq0 >= 0.0) { if (dq1 >= dq2) { if (dq1 >= 0.0) *qmax = dq0 + dq1; else *qmax = dq0; *qmin = dq0 + dq2; if (*qmin >= 0.0) *qmin = 0.0; } else {// dq1<dq2 if (dq2 > 0) *qmax = dq0 + dq2; else *qmax = dq0; *qmin = dq0 + dq1; if (*qmin >= 0.0) *qmin = 0.0; } } else {//dq0<0 if (dq1 <= dq2) { if (dq1 < 0.0) *qmin = dq0 + dq1; else *qmin = dq0; *qmax = dq0 + dq2; if (*qmax <= 0.0) *qmax = 0.0; } else {// dq1>dq2 if (dq2 < 0.0) *qmin = dq0 + dq2; else *qmin = dq0; *qmax = dq0 + dq1; if (*qmax <= 0.0) *qmax = 0.0; } } return 0; } __global__ void extrapolate_velocity_second_order_true( int N, double minimum_allowed_height, double * stage_centroid_values, double * bed_centroid_values, double * xmom_centroid_values, double * xmom_centroid_store, double * ymom_centroid_values, double * ymom_centroid_store ) { const int k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; double dk; if (k >= N ) return; dk = max(stage_centroid_values[k] -bed_centroid_values[k], minimum_allowed_height); //xmom_centroid_store[k] = xmom_centroid_values[k]; //xmom_centroid_values[k] = xmom_centroid_values[k] / dk; xmom_centroid_store[k] = xmom_centroid_values[k] / dk; //ymom_centroid_store[k] = ymom_centroid_values[k]; //ymom_centroid_values[k] = ymom_centroid_values[k] / dk; ymom_centroid_store[k] = ymom_centroid_values[k] / dk; } __global__ void extrapolate_velocity_second_order_true_after( int N, double * xmom_centroid_values, double * xmom_centroid_store, double * ymom_centroid_values, double * ymom_centroid_store ) { const int k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; if (k >= N ) return; xmom_centroid_values[k] = xmom_centroid_store[k]; ymom_centroid_values[k] = ymom_centroid_store[k]; } __global__ void extrapolate_second_order_sw_true ( int N, double epsilon, double minimum_allowed_height, double beta_w, double beta_w_dry, double beta_uh, double beta_uh_dry, double beta_vh, double beta_vh_dry, int optimise_dry_cells, long* surrogate_neighbours, long* number_of_boundaries, double* centroid_coordinates, double* stage_centroid_values, double* bed_centroid_values, double* xmom_centroid_values, double* ymom_centroid_values, double* vertex_coordinates, double* stage_vertex_values, double* bed_vertex_values, double* xmom_vertex_values, double* ymom_vertex_values) { const int k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; if (k >= N ) return; #ifndef REARRANGED_DOMAIN int k3 = 3*k, k6 = 6*k, coord_index; #endif double a, b; int k0, k1, k2; double x, y, x0, y0, x1, y1, x2, y2, xv0, yv0, xv1, yv1, xv2, yv2; double dx1, dx2, dy1, dy2, dxv0, dxv1, dxv2, dyv0, dyv1, dyv2, dq0, dq1, dq2, area2, inv_area2; double dqv[3], qmin, qmax, hmin, hmax; double hc, h0, h1, h2, beta_tmp, hfactor; double dv0, dv1, dv2; // extrapolate_velocity_second_order == 1 // This procedure is done in function extrapolate_velocity_second_order_true //dk = max(stage_centroid_values[k]-bed_centroid_values[k],minimum_allowed_height); //xmom_centroid_store[k] = xmom_centroid_values[k]; //xmom_centroid_values[k] = xmom_centroid_values[k] / dk; //ymom_centroid_store[k] = ymom_centroid_values[k]; //ymom_centroid_values[k] = ymom_centroid_values[k] / dk; // Begin extrapolation routine do { if ( number_of_boundaries[k] == 3 ) { #ifndef REARRANGED_DOMAIN stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 +1] = stage_centroid_values[k]; stage_vertex_values[k3 +2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; #else stage_vertex_values[k] = stage_centroid_values[k]; stage_vertex_values[k +N] = stage_centroid_values[k]; stage_vertex_values[k +2*N] = stage_centroid_values[k]; xmom_vertex_values[k] = xmom_centroid_values[k]; xmom_vertex_values[k + N] = xmom_centroid_values[k]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k]; ymom_vertex_values[k] = ymom_centroid_values[k]; ymom_vertex_values[k + N] = ymom_centroid_values[k]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k]; #endif // continue; break; } else { #ifndef REARRANGED_DOMAIN xv0 = vertex_coordinates[k6]; yv0 = vertex_coordinates[k6 + 1]; xv1 = vertex_coordinates[k6 + 2]; yv1 = vertex_coordinates[k6 + 3]; xv2 = vertex_coordinates[k6 + 4]; yv2 = vertex_coordinates[k6 + 5]; coord_index = 2 * k; x = centroid_coordinates[coord_index]; y = centroid_coordinates[coord_index + 1]; #else xv0 = vertex_coordinates[k]; yv0 = vertex_coordinates[k + N]; xv1 = vertex_coordinates[k + 2*N]; yv1 = vertex_coordinates[k + 3*N]; xv2 = vertex_coordinates[k + 4*N]; yv2 = vertex_coordinates[k + 5*N]; x = centroid_coordinates[k]; y = centroid_coordinates[k + N]; #endif dxv0 = xv0 - x; dxv1 = xv1 - x; dxv2 = xv2 - x; dyv0 = yv0 - y; dyv1 = yv1 - y; dyv2 = yv2 - y; } if ( number_of_boundaries[k] <= 1 ) { #ifndef REARRANGED_DOMAIN k0 = surrogate_neighbours[k3]; k1 = surrogate_neighbours[k3 + 1]; k2 = surrogate_neighbours[k3 + 2]; // Get the auxiliary triangle's vertex coordinates // (really the centroids of neighbouring triangles) coord_index = 2 * k0; x0 = centroid_coordinates[coord_index]; y0 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k2; x2 = centroid_coordinates[coord_index]; y2 = centroid_coordinates[coord_index + 1]; #else k0 = surrogate_neighbours[k]; k1 = surrogate_neighbours[k + N]; k2 = surrogate_neighbours[k + 2*N]; // Get the auxiliary triangle's vertex coordinates // (really the centroids of neighbouring triangles) x0 = centroid_coordinates[k0]; y0 = centroid_coordinates[k0 + N]; x1 = centroid_coordinates[k1]; y1 = centroid_coordinates[k1 + N]; x2 = centroid_coordinates[k2]; y2 = centroid_coordinates[k2 + N]; #endif // Store x- and y- differentials for the vertices // of the auxiliary triangle dx1 = x1 - x0; dx2 = x2 - x0; dy1 = y1 - y0; dy2 = y2 - y0; // Calculate 2*area of the auxiliary triangle // The triangle is guaranteed to be counter-clockwise area2 = dy2 * dx1 - dy1*dx2; if (area2 <= 0) { #ifndef REARRANGED_DOMAIN stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 + 1] = stage_centroid_values[k]; stage_vertex_values[k3 + 2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; #else stage_vertex_values[k] = stage_centroid_values[k]; stage_vertex_values[k + N] = stage_centroid_values[k]; stage_vertex_values[k + 2*N] = stage_centroid_values[k]; xmom_vertex_values[k] = xmom_centroid_values[k]; xmom_vertex_values[k + N] = xmom_centroid_values[k]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k]; ymom_vertex_values[k] = ymom_centroid_values[k]; ymom_vertex_values[k + N] = ymom_centroid_values[k]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k]; #endif //continue; break; } // Calculate heights of neighbouring cells hc = stage_centroid_values[k] - bed_centroid_values[k]; h0 = stage_centroid_values[k0] - bed_centroid_values[k0]; h1 = stage_centroid_values[k1] - bed_centroid_values[k1]; h2 = stage_centroid_values[k2] - bed_centroid_values[k2]; hmin = min(min(h0, min(h1, h2)), hc); //hfactor = hc/(hc + 1.0); hfactor = 0.0; if (hmin > 0.001) { hfactor = (hmin - 0.001) / (hmin + 0.004); } if (optimise_dry_cells) { // Check if linear reconstruction is necessary for triangle k // This check will exclude dry cells. hmax = max(h0, max(h1, h2)); if (hmax < epsilon) { // continue; break; } } //----------------------------------- // stage //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = stage_centroid_values[k0] - stage_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle (centroids of neighbouring triangles) dq1 = stage_centroid_values[k1] - stage_centroid_values[k0]; dq2 = stage_centroid_values[k2] - stage_centroid_values[k0]; inv_area2 = 1.0 / area2; // Calculate the gradient of stage on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); // Playing with dry wet interface //hmin = qmin; //beta_tmp = beta_tmp_dry; //if (hmin>minimum_allowed_height) beta_tmp = beta_w_dry + (beta_w - beta_w_dry) * hfactor; limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i=0;i<3;i++) #ifndef REARRANGED_DOMAIN stage_vertex_values[k3 + 0] = stage_centroid_values[k] +dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] +dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] +dqv[2]; #else stage_vertex_values[k] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k + N] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k + 2*N] = stage_centroid_values[k] + dqv[2]; #endif //----------------------------------- // xmomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = xmom_centroid_values[k0] - xmom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k0]; dq2 = xmom_centroid_values[k2] - xmom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_uh; //if (hmin<minimum_allowed_height) //beta_tmp = beta_uh_dry; beta_tmp = beta_uh_dry + (beta_uh - beta_uh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i = 0; i < 3; i++) { #ifndef REARRANGED_DOMAIN xmom_vertex_values[k3] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k] + dqv[2]; #else xmom_vertex_values[k] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k + N] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k] + dqv[2]; #endif //} //----------------------------------- // ymomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = ymom_centroid_values[k0] - ymom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k0]; dq2 = ymom_centroid_values[k2] - ymom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_vh; // //if (hmin<minimum_allowed_height) //beta_tmp = beta_vh_dry; beta_tmp = beta_vh_dry + (beta_vh - beta_vh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i = 0; i < 3; i++) { #ifndef REARRANGED_DOMAIN ymom_vertex_values[k3 ] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; #else ymom_vertex_values[k] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k + N] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k] + dqv[2]; #endif //} }// End number_of_boundaries <=1 else { //============================================== // Number of boundaries == 2 //============================================== // One internal neighbour and gradient is in direction of the neighbour's centroid // Find the only internal neighbour (k1?) #ifndef REARRANGED_DOMAIN for (k2 = k3; k2 < k3 + 3; k2++) { #else for (k2 = k; k2 <= k+2*N; k2+=N) { #endif // Find internal neighbour of triangle k // k2 indexes the edges of triangle k if (surrogate_neighbours[k2] != k) { break; } } k1 = surrogate_neighbours[k2]; // The coordinates of the triangle are already (x,y). // Get centroid of the neighbour (x1,y1) #ifndef REARRANGED_DOMAIN coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; #else x1 = centroid_coordinates[k1]; y1 = centroid_coordinates[k1 + N]; #endif // Compute x- and y- distances between the centroid of // triangle k and that of its neighbour dx1 = x1 - x; dy1 = y1 - y; // Set area2 as the square of the distance area2 = dx1 * dx1 + dy1*dy1; // Set dx2=(x1-x0)/((x1-x0)^2+(y1-y0)^2) // and dy2=(y1-y0)/((x1-x0)^2+(y1-y0)^2) which // respectively correspond to the x- and y- gradients // of the conserved quantities dx2 = 1.0 / area2; dy2 = dx2*dy1; dx2 *= dx1; //----------------------------------- // stage //----------------------------------- // Compute differentials dq1 = stage_centroid_values[k1] - stage_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0; i < 3; i++) //{ #ifndef REARRANGED_DOMAIN stage_vertex_values[k3] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] +dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] +dqv[2]; #else stage_vertex_values[k] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k + N] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k + 2*N] = stage_centroid_values[k]+dqv[2]; #endif //} //----------------------------------- // xmomentum //----------------------------------- // Compute differentials dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) // xmom_vertex_values[k3 + i] = xmom_centroid_values[k] + dqv[i]; #ifndef REARRANGED_DOMAIN xmom_vertex_values[k3] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k] + dqv[2]; #else xmom_vertex_values[k] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k + N] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k] + dqv[2]; #endif //----------------------------------- // ymomentum //----------------------------------- // Compute differentials dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) #ifndef REARRANGED_DOMAIN ymom_vertex_values[k3] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; #else ymom_vertex_values[k] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k + N] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k] + dqv[2]; #endif } // else [number_of_boundaries==2] } while(0); //if (extrapolate_velocity_second_order == 1) // Convert back from velocity to momentum //for (k = 0; k < number_of_elements; k++) //k3 = 3 * k; //dv0 = max(stage_vertex_values[k3]-bed_vertex_values[k3],minimum_allowed_height); //dv1 = max(stage_vertex_values[k3+1]-bed_vertex_values[k3+1],minimum_allowed_height); //dv2 = max(stage_vertex_values[k3+2]-bed_vertex_values[k3+2],minimum_allowed_height); #ifndef REARRANGED_DOMAIN dv0 = max(stage_vertex_values[k3] - bed_vertex_values[k3], 0.); dv1 = max(stage_vertex_values[k3 + 1] - bed_vertex_values[k3 + 1], 0.); dv2 = max(stage_vertex_values[k3 + 2] - bed_vertex_values[k3 + 2], 0.); //Correct centroid and vertex values //xmom_centroid_values[k] = xmom_centroid_store[k]; xmom_vertex_values[k3] = xmom_vertex_values[k3] * dv0; xmom_vertex_values[k3 + 1] = xmom_vertex_values[k3 + 1] * dv1; xmom_vertex_values[k3 + 2] = xmom_vertex_values[k3 + 2] * dv2; //ymom_centroid_values[k] = ymom_centroid_store[k]; ymom_vertex_values[k3] = ymom_vertex_values[k3] * dv0; ymom_vertex_values[k3 + 1] = ymom_vertex_values[k3 + 1] * dv1; ymom_vertex_values[k3 + 2] = ymom_vertex_values[k3 + 2] * dv2; #else dv0 = max(stage_vertex_values[k] - bed_vertex_values[k], 0.); dv1 = max(stage_vertex_values[k + N] - bed_vertex_values[k + N], 0.); dv2 = max(stage_vertex_values[k + 2*N] -bed_vertex_values[k + 2*N],0.); //Correct centroid and vertex values //xmom_centroid_values[k] = xmom_centroid_store[k]; xmom_vertex_values[k] = xmom_vertex_values[k] * dv0; xmom_vertex_values[k + N] = xmom_vertex_values[k + N] * dv1; xmom_vertex_values[k + 2*N] = xmom_vertex_values[k + 2*N] * dv2; //ymom_centroid_values[k] = ymom_centroid_store[k]; ymom_vertex_values[k] = ymom_vertex_values[k] * dv0; ymom_vertex_values[k + N] = ymom_vertex_values[k + N] * dv1; ymom_vertex_values[k + 2*N] = ymom_vertex_values[k + 2*N] * dv2; #endif } __global__ void extrapolate_second_order_sw_false ( int N, double epsilon, double minimum_allowed_height, double beta_w, double beta_w_dry, double beta_uh, double beta_uh_dry, double beta_vh, double beta_vh_dry, double optimise_dry_cells, long* surrogate_neighbours, long* number_of_boundaries, double* centroid_coordinates, double* stage_centroid_values, double* bed_centroid_values, double* xmom_centroid_values, double* ymom_centroid_values, double* vertex_coordinates, double* stage_vertex_values, double* bed_vertex_values, double* xmom_vertex_values, double* ymom_vertex_values) { const long k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; if ( k >= N ) return; int k3 = 3*k, k6 = 6*k; double a, b; int k0, k1, k2, coord_index, i; double x, y, x0, y0, x1, y1, x2, y2, xv0, yv0, xv1, yv1, xv2, yv2; double dx1, dx2, dy1, dy2, dxv0, dxv1, dxv2, dyv0, dyv1, dyv2, dq0, dq1, dq2, area2, inv_area2; double dqv[3], qmin, qmax, hmin, hmax; double hc, h0, h1, h2, beta_tmp, hfactor; // Begin extrapolation routine if ( number_of_boundaries[k] == 3 ) { stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 +1] = stage_centroid_values[k]; stage_vertex_values[k3 +2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; // continue; return; } else { xv0 = vertex_coordinates[k6]; yv0 = vertex_coordinates[k6 + 1]; xv1 = vertex_coordinates[k6 + 2]; yv1 = vertex_coordinates[k6 + 3]; xv2 = vertex_coordinates[k6 + 4]; yv2 = vertex_coordinates[k6 + 5]; coord_index = 2 * k; x = centroid_coordinates[coord_index]; y = centroid_coordinates[coord_index + 1]; dxv0 = xv0 - x; dxv1 = xv1 - x; dxv2 = xv2 - x; dyv0 = yv0 - y; dyv1 = yv1 - y; dyv2 = yv2 - y; } if ( number_of_boundaries[k] <= 1 ) { k0 = surrogate_neighbours[k3]; k1 = surrogate_neighbours[k3 + 1]; k2 = surrogate_neighbours[k3 + 2]; // Get the auxiliary triangle's vertex coordinates // (really the centroids of neighbouring triangles) coord_index = 2 * k0; x0 = centroid_coordinates[coord_index]; y0 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k2; x2 = centroid_coordinates[coord_index]; y2 = centroid_coordinates[coord_index + 1]; // Store x- and y- differentials for the vertices // of the auxiliary triangle dx1 = x1 - x0; dx2 = x2 - x0; dy1 = y1 - y0; dy2 = y2 - y0; // Calculate 2*area of the auxiliary triangle // The triangle is guaranteed to be counter-clockwise area2 = dy2 * dx1 - dy1*dx2; if (area2 <= 0) { stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 + 1] = stage_centroid_values[k]; stage_vertex_values[k3 + 2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; //continue; return; } // Calculate heights of neighbouring cells hc = stage_centroid_values[k] - bed_centroid_values[k]; h0 = stage_centroid_values[k0] - bed_centroid_values[k0]; h1 = stage_centroid_values[k1] - bed_centroid_values[k1]; h2 = stage_centroid_values[k2] - bed_centroid_values[k2]; hmin = min(min(h0, min(h1, h2)), hc); //hfactor = hc/(hc + 1.0); hfactor = 0.0; if (hmin > 0.001) { hfactor = (hmin - 0.001) / (hmin + 0.004); } if (optimise_dry_cells) { // Check if linear reconstruction is necessary for triangle k // This check will exclude dry cells. hmax = max(h0, max(h1, h2)); if (hmax < epsilon) { // continue; return; } } //----------------------------------- // stage //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = stage_centroid_values[k0] - stage_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle (centroids of neighbouring triangles) dq1 = stage_centroid_values[k1] - stage_centroid_values[k0]; dq2 = stage_centroid_values[k2] - stage_centroid_values[k0]; inv_area2 = 1.0 / area2; // Calculate the gradient of stage on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); // Playing with dry wet interface //hmin = qmin; //beta_tmp = beta_w_dry; //if (hmin>minimum_allowed_height) beta_tmp = beta_w_dry + (beta_w - beta_w_dry) * hfactor; limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i=0;i<3;i++) stage_vertex_values[k3 + 0] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] + dqv[2]; //----------------------------------- // xmomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = xmom_centroid_values[k0] - xmom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k0]; dq2 = xmom_centroid_values[k2] - xmom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_uh; //if (hmin<minimum_allowed_height) //beta_tmp = beta_uh_dry; beta_tmp = beta_uh_dry + (beta_uh - beta_uh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); for (i = 0; i < 3; i++) { xmom_vertex_values[k3 + i] = xmom_centroid_values[k] + dqv[i]; } //----------------------------------- // ymomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = ymom_centroid_values[k0] - ymom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k0]; dq2 = ymom_centroid_values[k2] - ymom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_vh; // //if (hmin<minimum_allowed_height) //beta_tmp = beta_vh_dry; beta_tmp = beta_vh_dry + (beta_vh - beta_vh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); for (i = 0; i < 3; i++) { ymom_vertex_values[k3 + i] = ymom_centroid_values[k] + dqv[i]; } }// End number_of_boundaries <=1 else { //============================================== // Number of boundaries == 2 //============================================== // One internal neighbour and gradient is in direction of the neighbour's centroid // Find the only internal neighbour (k1?) for (k2 = k3; k2 < k3 + 3; k2++) { // Find internal neighbour of triangle k // k2 indexes the edges of triangle k if (surrogate_neighbours[k2] != k) { break; } } k1 = surrogate_neighbours[k2]; // The coordinates of the triangle are already (x,y). // Get centroid of the neighbour (x1,y1) coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; // Compute x- and y- distances between the centroid of // triangle k and that of its neighbour dx1 = x1 - x; dy1 = y1 - y; // Set area2 as the square of the distance area2 = dx1 * dx1 + dy1*dy1; // Set dx2=(x1-x0)/((x1-x0)^2+(y1-y0)^2) // and dy2=(y1-y0)/((x1-x0)^2+(y1-y0)^2) which // respectively correspond to the x- and y- gradients // of the conserved quantities dx2 = 1.0 / area2; dy2 = dx2*dy1; dx2 *= dx1; //----------------------------------- // stage //----------------------------------- // Compute differentials dq1 = stage_centroid_values[k1] - stage_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0; i < 3; i++) //{ stage_vertex_values[k3] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] + dqv[2]; //} //----------------------------------- // xmomentum //----------------------------------- // Compute differentials dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) //xmom_vertex_values[k3] = xmom_centroid_values[k] + dqv[0]; //xmom_vertex_values[k3 + 1] = xmom_centroid_values[k] + dqv[1]; //xmom_vertex_values[k3 + 2] = xmom_centroid_values[k] + dqv[2]; for (i = 0; i < 3; i++) { xmom_vertex_values[k3 + i] = xmom_centroid_values[k] + dqv[i]; } //----------------------------------- // ymomentum //----------------------------------- // Compute differentials dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) //ymom_vertex_values[k3] = ymom_centroid_values[k] + dqv[0]; //ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; //ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; for (i = 0; i < 3; i++) { ymom_vertex_values[k3 + i] = ymom_centroid_values[k] + dqv[i]; } //ymom_vertex_values[k3] = ymom_centroid_values[k] + dqv[0]; //ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; //ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; } // else [number_of_boundaries==2] }
3fc55c6040827c60b8fea0124fd6ae4fe588c462.cu
//#define REARRANGED_DOMAIN #ifndef USING_HOST_MACRO #define BLOCK_SIZE 8 #endif __device__ int limit_gradient(double *dqv, double qmin, double qmax, double beta_w) { int i; double r = 1000.0, r0 = 1.0, phi = 1.0; double TINY = 1.0e-100; // to avoid machine accuracy problems. for (i = 0; i < 3; i++) { if (dqv[i]<-TINY) r0 = qmin / dqv[i]; if (dqv[i] > TINY) r0 = qmax / dqv[i]; r = min(r0, r); } phi = min(r*beta_w, 1.0); //for (i=0;i<3;i++) dqv[0] = dqv[0] * phi; dqv[1] = dqv[1] * phi; dqv[2] = dqv[2] * phi; return 0; } __device__ int find_qmin_and_qmax(double dq0, double dq1, double dq2, double *qmin, double *qmax) { if (dq0 >= 0.0) { if (dq1 >= dq2) { if (dq1 >= 0.0) *qmax = dq0 + dq1; else *qmax = dq0; *qmin = dq0 + dq2; if (*qmin >= 0.0) *qmin = 0.0; } else {// dq1<dq2 if (dq2 > 0) *qmax = dq0 + dq2; else *qmax = dq0; *qmin = dq0 + dq1; if (*qmin >= 0.0) *qmin = 0.0; } } else {//dq0<0 if (dq1 <= dq2) { if (dq1 < 0.0) *qmin = dq0 + dq1; else *qmin = dq0; *qmax = dq0 + dq2; if (*qmax <= 0.0) *qmax = 0.0; } else {// dq1>dq2 if (dq2 < 0.0) *qmin = dq0 + dq2; else *qmin = dq0; *qmax = dq0 + dq1; if (*qmax <= 0.0) *qmax = 0.0; } } return 0; } __global__ void extrapolate_velocity_second_order_true( int N, double minimum_allowed_height, double * stage_centroid_values, double * bed_centroid_values, double * xmom_centroid_values, double * xmom_centroid_store, double * ymom_centroid_values, double * ymom_centroid_store ) { const int k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; double dk; if (k >= N ) return; dk = max(stage_centroid_values[k] -bed_centroid_values[k], minimum_allowed_height); //xmom_centroid_store[k] = xmom_centroid_values[k]; //xmom_centroid_values[k] = xmom_centroid_values[k] / dk; xmom_centroid_store[k] = xmom_centroid_values[k] / dk; //ymom_centroid_store[k] = ymom_centroid_values[k]; //ymom_centroid_values[k] = ymom_centroid_values[k] / dk; ymom_centroid_store[k] = ymom_centroid_values[k] / dk; } __global__ void extrapolate_velocity_second_order_true_after( int N, double * xmom_centroid_values, double * xmom_centroid_store, double * ymom_centroid_values, double * ymom_centroid_store ) { const int k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; if (k >= N ) return; xmom_centroid_values[k] = xmom_centroid_store[k]; ymom_centroid_values[k] = ymom_centroid_store[k]; } __global__ void extrapolate_second_order_sw_true ( int N, double epsilon, double minimum_allowed_height, double beta_w, double beta_w_dry, double beta_uh, double beta_uh_dry, double beta_vh, double beta_vh_dry, int optimise_dry_cells, long* surrogate_neighbours, long* number_of_boundaries, double* centroid_coordinates, double* stage_centroid_values, double* bed_centroid_values, double* xmom_centroid_values, double* ymom_centroid_values, double* vertex_coordinates, double* stage_vertex_values, double* bed_vertex_values, double* xmom_vertex_values, double* ymom_vertex_values) { const int k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; if (k >= N ) return; #ifndef REARRANGED_DOMAIN int k3 = 3*k, k6 = 6*k, coord_index; #endif double a, b; int k0, k1, k2; double x, y, x0, y0, x1, y1, x2, y2, xv0, yv0, xv1, yv1, xv2, yv2; double dx1, dx2, dy1, dy2, dxv0, dxv1, dxv2, dyv0, dyv1, dyv2, dq0, dq1, dq2, area2, inv_area2; double dqv[3], qmin, qmax, hmin, hmax; double hc, h0, h1, h2, beta_tmp, hfactor; double dv0, dv1, dv2; // extrapolate_velocity_second_order == 1 // This procedure is done in function extrapolate_velocity_second_order_true //dk = max(stage_centroid_values[k]-bed_centroid_values[k],minimum_allowed_height); //xmom_centroid_store[k] = xmom_centroid_values[k]; //xmom_centroid_values[k] = xmom_centroid_values[k] / dk; //ymom_centroid_store[k] = ymom_centroid_values[k]; //ymom_centroid_values[k] = ymom_centroid_values[k] / dk; // Begin extrapolation routine do { if ( number_of_boundaries[k] == 3 ) { #ifndef REARRANGED_DOMAIN stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 +1] = stage_centroid_values[k]; stage_vertex_values[k3 +2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; #else stage_vertex_values[k] = stage_centroid_values[k]; stage_vertex_values[k +N] = stage_centroid_values[k]; stage_vertex_values[k +2*N] = stage_centroid_values[k]; xmom_vertex_values[k] = xmom_centroid_values[k]; xmom_vertex_values[k + N] = xmom_centroid_values[k]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k]; ymom_vertex_values[k] = ymom_centroid_values[k]; ymom_vertex_values[k + N] = ymom_centroid_values[k]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k]; #endif // continue; break; } else { #ifndef REARRANGED_DOMAIN xv0 = vertex_coordinates[k6]; yv0 = vertex_coordinates[k6 + 1]; xv1 = vertex_coordinates[k6 + 2]; yv1 = vertex_coordinates[k6 + 3]; xv2 = vertex_coordinates[k6 + 4]; yv2 = vertex_coordinates[k6 + 5]; coord_index = 2 * k; x = centroid_coordinates[coord_index]; y = centroid_coordinates[coord_index + 1]; #else xv0 = vertex_coordinates[k]; yv0 = vertex_coordinates[k + N]; xv1 = vertex_coordinates[k + 2*N]; yv1 = vertex_coordinates[k + 3*N]; xv2 = vertex_coordinates[k + 4*N]; yv2 = vertex_coordinates[k + 5*N]; x = centroid_coordinates[k]; y = centroid_coordinates[k + N]; #endif dxv0 = xv0 - x; dxv1 = xv1 - x; dxv2 = xv2 - x; dyv0 = yv0 - y; dyv1 = yv1 - y; dyv2 = yv2 - y; } if ( number_of_boundaries[k] <= 1 ) { #ifndef REARRANGED_DOMAIN k0 = surrogate_neighbours[k3]; k1 = surrogate_neighbours[k3 + 1]; k2 = surrogate_neighbours[k3 + 2]; // Get the auxiliary triangle's vertex coordinates // (really the centroids of neighbouring triangles) coord_index = 2 * k0; x0 = centroid_coordinates[coord_index]; y0 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k2; x2 = centroid_coordinates[coord_index]; y2 = centroid_coordinates[coord_index + 1]; #else k0 = surrogate_neighbours[k]; k1 = surrogate_neighbours[k + N]; k2 = surrogate_neighbours[k + 2*N]; // Get the auxiliary triangle's vertex coordinates // (really the centroids of neighbouring triangles) x0 = centroid_coordinates[k0]; y0 = centroid_coordinates[k0 + N]; x1 = centroid_coordinates[k1]; y1 = centroid_coordinates[k1 + N]; x2 = centroid_coordinates[k2]; y2 = centroid_coordinates[k2 + N]; #endif // Store x- and y- differentials for the vertices // of the auxiliary triangle dx1 = x1 - x0; dx2 = x2 - x0; dy1 = y1 - y0; dy2 = y2 - y0; // Calculate 2*area of the auxiliary triangle // The triangle is guaranteed to be counter-clockwise area2 = dy2 * dx1 - dy1*dx2; if (area2 <= 0) { #ifndef REARRANGED_DOMAIN stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 + 1] = stage_centroid_values[k]; stage_vertex_values[k3 + 2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; #else stage_vertex_values[k] = stage_centroid_values[k]; stage_vertex_values[k + N] = stage_centroid_values[k]; stage_vertex_values[k + 2*N] = stage_centroid_values[k]; xmom_vertex_values[k] = xmom_centroid_values[k]; xmom_vertex_values[k + N] = xmom_centroid_values[k]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k]; ymom_vertex_values[k] = ymom_centroid_values[k]; ymom_vertex_values[k + N] = ymom_centroid_values[k]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k]; #endif //continue; break; } // Calculate heights of neighbouring cells hc = stage_centroid_values[k] - bed_centroid_values[k]; h0 = stage_centroid_values[k0] - bed_centroid_values[k0]; h1 = stage_centroid_values[k1] - bed_centroid_values[k1]; h2 = stage_centroid_values[k2] - bed_centroid_values[k2]; hmin = min(min(h0, min(h1, h2)), hc); //hfactor = hc/(hc + 1.0); hfactor = 0.0; if (hmin > 0.001) { hfactor = (hmin - 0.001) / (hmin + 0.004); } if (optimise_dry_cells) { // Check if linear reconstruction is necessary for triangle k // This check will exclude dry cells. hmax = max(h0, max(h1, h2)); if (hmax < epsilon) { // continue; break; } } //----------------------------------- // stage //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = stage_centroid_values[k0] - stage_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle (centroids of neighbouring triangles) dq1 = stage_centroid_values[k1] - stage_centroid_values[k0]; dq2 = stage_centroid_values[k2] - stage_centroid_values[k0]; inv_area2 = 1.0 / area2; // Calculate the gradient of stage on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); // Playing with dry wet interface //hmin = qmin; //beta_tmp = beta_tmp_dry; //if (hmin>minimum_allowed_height) beta_tmp = beta_w_dry + (beta_w - beta_w_dry) * hfactor; limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i=0;i<3;i++) #ifndef REARRANGED_DOMAIN stage_vertex_values[k3 + 0] = stage_centroid_values[k] +dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] +dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] +dqv[2]; #else stage_vertex_values[k] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k + N] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k + 2*N] = stage_centroid_values[k] + dqv[2]; #endif //----------------------------------- // xmomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = xmom_centroid_values[k0] - xmom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k0]; dq2 = xmom_centroid_values[k2] - xmom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_uh; //if (hmin<minimum_allowed_height) //beta_tmp = beta_uh_dry; beta_tmp = beta_uh_dry + (beta_uh - beta_uh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i = 0; i < 3; i++) { #ifndef REARRANGED_DOMAIN xmom_vertex_values[k3] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k] + dqv[2]; #else xmom_vertex_values[k] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k + N] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k] + dqv[2]; #endif //} //----------------------------------- // ymomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = ymom_centroid_values[k0] - ymom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k0]; dq2 = ymom_centroid_values[k2] - ymom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_vh; // //if (hmin<minimum_allowed_height) //beta_tmp = beta_vh_dry; beta_tmp = beta_vh_dry + (beta_vh - beta_vh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i = 0; i < 3; i++) { #ifndef REARRANGED_DOMAIN ymom_vertex_values[k3 ] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; #else ymom_vertex_values[k] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k + N] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k] + dqv[2]; #endif //} }// End number_of_boundaries <=1 else { //============================================== // Number of boundaries == 2 //============================================== // One internal neighbour and gradient is in direction of the neighbour's centroid // Find the only internal neighbour (k1?) #ifndef REARRANGED_DOMAIN for (k2 = k3; k2 < k3 + 3; k2++) { #else for (k2 = k; k2 <= k+2*N; k2+=N) { #endif // Find internal neighbour of triangle k // k2 indexes the edges of triangle k if (surrogate_neighbours[k2] != k) { break; } } k1 = surrogate_neighbours[k2]; // The coordinates of the triangle are already (x,y). // Get centroid of the neighbour (x1,y1) #ifndef REARRANGED_DOMAIN coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; #else x1 = centroid_coordinates[k1]; y1 = centroid_coordinates[k1 + N]; #endif // Compute x- and y- distances between the centroid of // triangle k and that of its neighbour dx1 = x1 - x; dy1 = y1 - y; // Set area2 as the square of the distance area2 = dx1 * dx1 + dy1*dy1; // Set dx2=(x1-x0)/((x1-x0)^2+(y1-y0)^2) // and dy2=(y1-y0)/((x1-x0)^2+(y1-y0)^2) which // respectively correspond to the x- and y- gradients // of the conserved quantities dx2 = 1.0 / area2; dy2 = dx2*dy1; dx2 *= dx1; //----------------------------------- // stage //----------------------------------- // Compute differentials dq1 = stage_centroid_values[k1] - stage_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0; i < 3; i++) //{ #ifndef REARRANGED_DOMAIN stage_vertex_values[k3] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] +dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] +dqv[2]; #else stage_vertex_values[k] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k + N] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k + 2*N] = stage_centroid_values[k]+dqv[2]; #endif //} //----------------------------------- // xmomentum //----------------------------------- // Compute differentials dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) // xmom_vertex_values[k3 + i] = xmom_centroid_values[k] + dqv[i]; #ifndef REARRANGED_DOMAIN xmom_vertex_values[k3] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k] + dqv[2]; #else xmom_vertex_values[k] = xmom_centroid_values[k] + dqv[0]; xmom_vertex_values[k + N] = xmom_centroid_values[k] + dqv[1]; xmom_vertex_values[k + 2*N] = xmom_centroid_values[k] + dqv[2]; #endif //----------------------------------- // ymomentum //----------------------------------- // Compute differentials dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) #ifndef REARRANGED_DOMAIN ymom_vertex_values[k3] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; #else ymom_vertex_values[k] = ymom_centroid_values[k] + dqv[0]; ymom_vertex_values[k + N] = ymom_centroid_values[k] + dqv[1]; ymom_vertex_values[k + 2*N] = ymom_centroid_values[k] + dqv[2]; #endif } // else [number_of_boundaries==2] } while(0); //if (extrapolate_velocity_second_order == 1) // Convert back from velocity to momentum //for (k = 0; k < number_of_elements; k++) //k3 = 3 * k; //dv0 = max(stage_vertex_values[k3]-bed_vertex_values[k3],minimum_allowed_height); //dv1 = max(stage_vertex_values[k3+1]-bed_vertex_values[k3+1],minimum_allowed_height); //dv2 = max(stage_vertex_values[k3+2]-bed_vertex_values[k3+2],minimum_allowed_height); #ifndef REARRANGED_DOMAIN dv0 = max(stage_vertex_values[k3] - bed_vertex_values[k3], 0.); dv1 = max(stage_vertex_values[k3 + 1] - bed_vertex_values[k3 + 1], 0.); dv2 = max(stage_vertex_values[k3 + 2] - bed_vertex_values[k3 + 2], 0.); //Correct centroid and vertex values //xmom_centroid_values[k] = xmom_centroid_store[k]; xmom_vertex_values[k3] = xmom_vertex_values[k3] * dv0; xmom_vertex_values[k3 + 1] = xmom_vertex_values[k3 + 1] * dv1; xmom_vertex_values[k3 + 2] = xmom_vertex_values[k3 + 2] * dv2; //ymom_centroid_values[k] = ymom_centroid_store[k]; ymom_vertex_values[k3] = ymom_vertex_values[k3] * dv0; ymom_vertex_values[k3 + 1] = ymom_vertex_values[k3 + 1] * dv1; ymom_vertex_values[k3 + 2] = ymom_vertex_values[k3 + 2] * dv2; #else dv0 = max(stage_vertex_values[k] - bed_vertex_values[k], 0.); dv1 = max(stage_vertex_values[k + N] - bed_vertex_values[k + N], 0.); dv2 = max(stage_vertex_values[k + 2*N] -bed_vertex_values[k + 2*N],0.); //Correct centroid and vertex values //xmom_centroid_values[k] = xmom_centroid_store[k]; xmom_vertex_values[k] = xmom_vertex_values[k] * dv0; xmom_vertex_values[k + N] = xmom_vertex_values[k + N] * dv1; xmom_vertex_values[k + 2*N] = xmom_vertex_values[k + 2*N] * dv2; //ymom_centroid_values[k] = ymom_centroid_store[k]; ymom_vertex_values[k] = ymom_vertex_values[k] * dv0; ymom_vertex_values[k + N] = ymom_vertex_values[k + N] * dv1; ymom_vertex_values[k + 2*N] = ymom_vertex_values[k + 2*N] * dv2; #endif } __global__ void extrapolate_second_order_sw_false ( int N, double epsilon, double minimum_allowed_height, double beta_w, double beta_w_dry, double beta_uh, double beta_uh_dry, double beta_vh, double beta_vh_dry, double optimise_dry_cells, long* surrogate_neighbours, long* number_of_boundaries, double* centroid_coordinates, double* stage_centroid_values, double* bed_centroid_values, double* xmom_centroid_values, double* ymom_centroid_values, double* vertex_coordinates, double* stage_vertex_values, double* bed_vertex_values, double* xmom_vertex_values, double* ymom_vertex_values) { const long k = threadIdx.x + threadIdx.y*blockDim.x + (blockIdx.x+blockIdx.y*gridDim.x)*blockDim.x*blockDim.y; if ( k >= N ) return; int k3 = 3*k, k6 = 6*k; double a, b; int k0, k1, k2, coord_index, i; double x, y, x0, y0, x1, y1, x2, y2, xv0, yv0, xv1, yv1, xv2, yv2; double dx1, dx2, dy1, dy2, dxv0, dxv1, dxv2, dyv0, dyv1, dyv2, dq0, dq1, dq2, area2, inv_area2; double dqv[3], qmin, qmax, hmin, hmax; double hc, h0, h1, h2, beta_tmp, hfactor; // Begin extrapolation routine if ( number_of_boundaries[k] == 3 ) { stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 +1] = stage_centroid_values[k]; stage_vertex_values[k3 +2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; // continue; return; } else { xv0 = vertex_coordinates[k6]; yv0 = vertex_coordinates[k6 + 1]; xv1 = vertex_coordinates[k6 + 2]; yv1 = vertex_coordinates[k6 + 3]; xv2 = vertex_coordinates[k6 + 4]; yv2 = vertex_coordinates[k6 + 5]; coord_index = 2 * k; x = centroid_coordinates[coord_index]; y = centroid_coordinates[coord_index + 1]; dxv0 = xv0 - x; dxv1 = xv1 - x; dxv2 = xv2 - x; dyv0 = yv0 - y; dyv1 = yv1 - y; dyv2 = yv2 - y; } if ( number_of_boundaries[k] <= 1 ) { k0 = surrogate_neighbours[k3]; k1 = surrogate_neighbours[k3 + 1]; k2 = surrogate_neighbours[k3 + 2]; // Get the auxiliary triangle's vertex coordinates // (really the centroids of neighbouring triangles) coord_index = 2 * k0; x0 = centroid_coordinates[coord_index]; y0 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; coord_index = 2 * k2; x2 = centroid_coordinates[coord_index]; y2 = centroid_coordinates[coord_index + 1]; // Store x- and y- differentials for the vertices // of the auxiliary triangle dx1 = x1 - x0; dx2 = x2 - x0; dy1 = y1 - y0; dy2 = y2 - y0; // Calculate 2*area of the auxiliary triangle // The triangle is guaranteed to be counter-clockwise area2 = dy2 * dx1 - dy1*dx2; if (area2 <= 0) { stage_vertex_values[k3] = stage_centroid_values[k]; stage_vertex_values[k3 + 1] = stage_centroid_values[k]; stage_vertex_values[k3 + 2] = stage_centroid_values[k]; xmom_vertex_values[k3] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 1] = xmom_centroid_values[k]; xmom_vertex_values[k3 + 2] = xmom_centroid_values[k]; ymom_vertex_values[k3] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 1] = ymom_centroid_values[k]; ymom_vertex_values[k3 + 2] = ymom_centroid_values[k]; //continue; return; } // Calculate heights of neighbouring cells hc = stage_centroid_values[k] - bed_centroid_values[k]; h0 = stage_centroid_values[k0] - bed_centroid_values[k0]; h1 = stage_centroid_values[k1] - bed_centroid_values[k1]; h2 = stage_centroid_values[k2] - bed_centroid_values[k2]; hmin = min(min(h0, min(h1, h2)), hc); //hfactor = hc/(hc + 1.0); hfactor = 0.0; if (hmin > 0.001) { hfactor = (hmin - 0.001) / (hmin + 0.004); } if (optimise_dry_cells) { // Check if linear reconstruction is necessary for triangle k // This check will exclude dry cells. hmax = max(h0, max(h1, h2)); if (hmax < epsilon) { // continue; return; } } //----------------------------------- // stage //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = stage_centroid_values[k0] - stage_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle (centroids of neighbouring triangles) dq1 = stage_centroid_values[k1] - stage_centroid_values[k0]; dq2 = stage_centroid_values[k2] - stage_centroid_values[k0]; inv_area2 = 1.0 / area2; // Calculate the gradient of stage on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); // Playing with dry wet interface //hmin = qmin; //beta_tmp = beta_w_dry; //if (hmin>minimum_allowed_height) beta_tmp = beta_w_dry + (beta_w - beta_w_dry) * hfactor; limit_gradient(dqv, qmin, qmax, beta_tmp); //for (i=0;i<3;i++) stage_vertex_values[k3 + 0] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] + dqv[2]; //----------------------------------- // xmomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = xmom_centroid_values[k0] - xmom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k0]; dq2 = xmom_centroid_values[k2] - xmom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_uh; //if (hmin<minimum_allowed_height) //beta_tmp = beta_uh_dry; beta_tmp = beta_uh_dry + (beta_uh - beta_uh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); for (i = 0; i < 3; i++) { xmom_vertex_values[k3 + i] = xmom_centroid_values[k] + dqv[i]; } //----------------------------------- // ymomentum //----------------------------------- // Calculate the difference between vertex 0 of the auxiliary // triangle and the centroid of triangle k dq0 = ymom_centroid_values[k0] - ymom_centroid_values[k]; // Calculate differentials between the vertices // of the auxiliary triangle dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k0]; dq2 = ymom_centroid_values[k2] - ymom_centroid_values[k0]; // Calculate the gradient of xmom on the auxiliary triangle a = dy2 * dq1 - dy1*dq2; a *= inv_area2; b = dx1 * dq2 - dx2*dq1; b *= inv_area2; // Calculate provisional jumps in stage from the centroid // of triangle k to its vertices, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now we want to find min and max of the centroid and the // vertices of the auxiliary triangle and compute jumps // from the centroid to the min and max find_qmin_and_qmax(dq0, dq1, dq2, &qmin, &qmax); //beta_tmp = beta_vh; // //if (hmin<minimum_allowed_height) //beta_tmp = beta_vh_dry; beta_tmp = beta_vh_dry + (beta_vh - beta_vh_dry) * hfactor; // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_tmp); for (i = 0; i < 3; i++) { ymom_vertex_values[k3 + i] = ymom_centroid_values[k] + dqv[i]; } }// End number_of_boundaries <=1 else { //============================================== // Number of boundaries == 2 //============================================== // One internal neighbour and gradient is in direction of the neighbour's centroid // Find the only internal neighbour (k1?) for (k2 = k3; k2 < k3 + 3; k2++) { // Find internal neighbour of triangle k // k2 indexes the edges of triangle k if (surrogate_neighbours[k2] != k) { break; } } k1 = surrogate_neighbours[k2]; // The coordinates of the triangle are already (x,y). // Get centroid of the neighbour (x1,y1) coord_index = 2 * k1; x1 = centroid_coordinates[coord_index]; y1 = centroid_coordinates[coord_index + 1]; // Compute x- and y- distances between the centroid of // triangle k and that of its neighbour dx1 = x1 - x; dy1 = y1 - y; // Set area2 as the square of the distance area2 = dx1 * dx1 + dy1*dy1; // Set dx2=(x1-x0)/((x1-x0)^2+(y1-y0)^2) // and dy2=(y1-y0)/((x1-x0)^2+(y1-y0)^2) which // respectively correspond to the x- and y- gradients // of the conserved quantities dx2 = 1.0 / area2; dy2 = dx2*dy1; dx2 *= dx1; //----------------------------------- // stage //----------------------------------- // Compute differentials dq1 = stage_centroid_values[k1] - stage_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0; i < 3; i++) //{ stage_vertex_values[k3] = stage_centroid_values[k] + dqv[0]; stage_vertex_values[k3 + 1] = stage_centroid_values[k] + dqv[1]; stage_vertex_values[k3 + 2] = stage_centroid_values[k] + dqv[2]; //} //----------------------------------- // xmomentum //----------------------------------- // Compute differentials dq1 = xmom_centroid_values[k1] - xmom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) //xmom_vertex_values[k3] = xmom_centroid_values[k] + dqv[0]; //xmom_vertex_values[k3 + 1] = xmom_centroid_values[k] + dqv[1]; //xmom_vertex_values[k3 + 2] = xmom_centroid_values[k] + dqv[2]; for (i = 0; i < 3; i++) { xmom_vertex_values[k3 + i] = xmom_centroid_values[k] + dqv[i]; } //----------------------------------- // ymomentum //----------------------------------- // Compute differentials dq1 = ymom_centroid_values[k1] - ymom_centroid_values[k]; // Calculate the gradient between the centroid of triangle k // and that of its neighbour a = dq1*dx2; b = dq1*dy2; // Calculate provisional vertex jumps, to be limited dqv[0] = a * dxv0 + b*dyv0; dqv[1] = a * dxv1 + b*dyv1; dqv[2] = a * dxv2 + b*dyv2; // Now limit the jumps if (dq1 >= 0.0) { qmin = 0.0; qmax = dq1; } else { qmin = dq1; qmax = 0.0; } // Limit the gradient limit_gradient(dqv, qmin, qmax, beta_w); //for (i=0;i<3;i++) //ymom_vertex_values[k3] = ymom_centroid_values[k] + dqv[0]; //ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; //ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; for (i = 0; i < 3; i++) { ymom_vertex_values[k3 + i] = ymom_centroid_values[k] + dqv[i]; } //ymom_vertex_values[k3] = ymom_centroid_values[k] + dqv[0]; //ymom_vertex_values[k3 + 1] = ymom_centroid_values[k] + dqv[1]; //ymom_vertex_values[k3 + 2] = ymom_centroid_values[k] + dqv[2]; } // else [number_of_boundaries==2] }
e2b936c36b4226850c3e73566cee172623f745e6.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* Copyright 2020 The OneFlow Authors. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifdef WITH_CUDA #include <unordered_map> #include "oneflow/core/framework/framework.h" #include "oneflow/core/device/cudnn_util.h" #include "oneflow/core/kernel/new_kernel_util.h" namespace oneflow { namespace { #if (CUDNN_VERSION >= 7401) #define BN_ENABLE_EX_API class CudnnHandleWrapper { public: CudnnHandleWrapper() { OF_CUDNN_CHECK(cudnnCreate(&handle_)); } ~CudnnHandleWrapper() { OF_CUDNN_CHECK(cudnnDestroy(handle_)); } cudnnHandle_t handle() const { return handle_; } private: cudnnHandle_t handle_; }; cudnnHandle_t GetOrCreateCudnnHandle() { static std::unordered_map<int, CudnnHandleWrapper> device_id2handle; int device_id; OF_CUDA_CHECK(hipGetDevice(&device_id)); // operator [] will create and insert a CudnnHandleWrapper // if key `device_id` does not exist return device_id2handle[device_id].handle(); } #endif void InferDimSizeAndDataFormat(const ShapeView& x_shape, const int32_t axis, int32_t* n, int32_t* c, int32_t* h, int32_t* w, cudnnTensorFormat_t* format) { if (x_shape.Count(axis + 1) == 1) { if (axis == 0) { *n = 1; *h = 1; } else { *n = x_shape.At(0); *h = x_shape.Count(1, axis); } *w = 1; *c = x_shape.At(axis); *format = CUDNN_TENSOR_NHWC; } else { *n = x_shape.Count(0, axis); *c = x_shape.At(axis); *h = x_shape.Count(axis + 1); *w = 1; *format = CUDNN_TENSOR_NCHW; } } void InferXYCudnnTensorDesc(const ShapeView& xy_shape, const DataType& data_type, const int32_t axis, cudnnTensorDescriptor_t xy_desc) { int32_t n, c, h, w; cudnnTensorFormat_t format; InferDimSizeAndDataFormat(xy_shape, axis, &n, &c, &h, &w, &format); OF_CUDNN_CHECK( cudnnSetTensor4dDescriptor(xy_desc, format, GetCudnnDataType(data_type), n, c, h, w)); } void InferParamCudnnTensorDesc(const cudnnTensorDescriptor_t xy_desc, cudnnBatchNormMode_t mode, cudnnTensorDescriptor_t param_desc) { OF_CUDNN_CHECK(cudnnDeriveBNTensorDescriptor(param_desc, xy_desc, mode)); } class CudnnTensorDescHelper final { public: OF_DISALLOW_COPY_AND_MOVE(CudnnTensorDescHelper); CudnnTensorDescHelper(const ShapeView& xy_shape, const DataType& data_type, const int32_t axis, cudnnBatchNormMode_t mode) { OF_CUDNN_CHECK(cudnnCreateTensorDescriptor(&xy_desc_)); InferXYCudnnTensorDesc(xy_shape, data_type, axis, xy_desc_); OF_CUDNN_CHECK(cudnnCreateTensorDescriptor(&param_desc_)); InferParamCudnnTensorDesc(xy_desc_, mode, param_desc_); int n, c, h, w, n_stride, c_stride, h_stride, w_stride; OF_CUDNN_CHECK(cudnnGetTensor4dDescriptor(param_desc_, &param_data_type_, &n, &c, &h, &w, &n_stride, &c_stride, &h_stride, &w_stride)); param_size_ = c; } ~CudnnTensorDescHelper() { OF_CUDNN_CHECK(cudnnDestroyTensorDescriptor(param_desc_)); OF_CUDNN_CHECK(cudnnDestroyTensorDescriptor(xy_desc_)); } cudnnTensorDescriptor_t xy_desc() const { return xy_desc_; } cudnnTensorDescriptor_t param_desc() const { return param_desc_; } void CheckParamTensor(const user_op::Tensor* tensor) const { CHECK_EQ(tensor->shape().NumAxes(), 1); CHECK_EQ(tensor->shape().At(0), param_size_); CHECK_EQ(GetCudnnDataType(tensor->data_type()), param_data_type_); } private: cudnnTensorDescriptor_t xy_desc_ = nullptr; cudnnTensorDescriptor_t param_desc_ = nullptr; cudnnDataType_t param_data_type_; int32_t param_size_ = 0; }; size_t InferTrainWorkspaceSize(const ShapeView& x_shape, const DataType data_type, const int32_t axis) { #if defined(BN_ENABLE_EX_API) const CudnnTensorDescHelper desc_helper(x_shape, data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &size_in_bytes)); return ::max(size_in_bytes, static_cast<size_t>(1)); #else return 1; #endif } size_t InferTrainTmpSize(user_op::InferContext* ctx) { const auto& x = ctx->InputTensorDesc("x", 0); const auto axis = ctx->Attr<int32_t>("axis"); return InferTrainWorkspaceSize(x.shape(), x.data_type(), axis); } size_t InferGradWorkspaceSize(const ShapeView& x_shape, const DataType data_type, const int32_t axis) { #if defined(BN_ENABLE_EX_API) const CudnnTensorDescHelper desc_helper(x_shape, data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &size_in_bytes)); return ::max(size_in_bytes, static_cast<size_t>(1)); #else return 1; #endif } size_t InferGradTmpSize(user_op::InferContext* ctx) { const auto& dy = ctx->InputTensorDesc("dy", 0); const auto axis = ctx->Attr<int32_t>("axis"); size_t tmp_size = 0; if (ctx->op_type_name() == "normalization_add_relu_grad" && !ctx->has_output("addend_diff", 0)) { tmp_size += GetCudaAlignedSize(dy.shape().elem_cnt() * GetSizeOfDataType(dy.data_type())); } tmp_size += GetCudaAlignedSize(InferGradWorkspaceSize(dy.shape(), dy.data_type(), axis)); return tmp_size; } template<typename T> class NormalizationInferenceKernel final : public user_op::OpKernel { public: NormalizationInferenceKernel() = default; ~NormalizationInferenceKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const bool training = ctx->Attr<bool>("training"); CHECK(!training); const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* moving_mean = ctx->Tensor4ArgNameAndIndex("moving_mean", 0); auto* moving_variance = ctx->Tensor4ArgNameAndIndex("moving_variance", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const DataType data_type = x->data_type(); CHECK_EQ(x->shape(), y->shape()); CHECK_EQ(y->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(moving_mean); desc_helper.CheckParamTensor(moving_variance); const void* sp_alpha = CudnnSPOnePtr<T>(); const void* sp_beta; if (ctx->has_input("_add_to_output", 0)) { const user_op::Tensor* add_to_output = ctx->Tensor4ArgNameAndIndex("_add_to_output", 0); CHECK_EQ(add_to_output->data_type(), y->data_type()); CHECK_EQ(add_to_output->shape(), y->shape()); Memcpy<DeviceType::kGPU>( ctx->device_ctx(), y->mut_dptr<void>(), add_to_output->dptr<void>(), add_to_output->shape().elem_cnt() * GetSizeOfDataType(add_to_output->data_type())); sp_beta = CudnnSPOnePtr<T>(); } else { sp_beta = CudnnSPZeroPtr<T>(); } OF_CUDNN_CHECK(cudnnBatchNormalizationForwardInference( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), moving_mean->dptr(), moving_variance->dptr(), epsilon)); } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_BN_INFERENCE_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization") \ .SetCreateFn<NormalizationInferenceKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value) \ & (user_op::HobAttr<bool>("training") == false)) \ .SetInplaceProposalFn([](const user_op::InferContext& ctx, \ user_op::AddInplaceArgPair AddInplaceArgPairFn) -> Maybe<void> { \ if (ctx.has_input("_add_to_output", 0)) { \ OF_RETURN_IF_ERROR(AddInplaceArgPairFn("y", 0, "_add_to_output", 0, true)); \ } \ return Maybe<void>::Ok(); \ }); REGISTER_BN_INFERENCE_KERNEL(float16) REGISTER_BN_INFERENCE_KERNEL(float) REGISTER_BN_INFERENCE_KERNEL(double) #undef REGISTER_BN_INFERENCE_KERNEL constexpr int64_t kCudaWarpSize = 32; template<typename T> __global__ void ReluGpu(int64_t n, const T* x, T* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; CUDA_1D_KERNEL_LOOP(i, n) { const T x_val = x[i]; const bool is_positive = (x_val > 0); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? x_val : 0; } } template<> __global__ void ReluGpu<half>(int64_t n, const half* x, half* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; const half zero = __float2half(0.0f); CUDA_1D_KERNEL_LOOP(i, n) { const half x_val = x[i]; const bool is_positive = __hgt(x_val, zero); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? x_val : zero; } } template<typename T> __global__ void AddReluGpu(int64_t n, const T* x, const T* addend, T* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; CUDA_1D_KERNEL_LOOP(i, n) { const T sum = x[i] + addend[i]; const bool is_positive = (sum > 0); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? sum : 0; } } template<> __global__ void AddReluGpu<half>(int64_t n, const half* x, const half* addend, half* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; const half zero = __float2half(0.0f); CUDA_1D_KERNEL_LOOP(i, n) { const half sum = __hadd(x[i], addend[i]); const bool is_positive = __hgt(sum, zero); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? sum : zero; } } template<typename T> void Relu(DeviceCtx* device_ctx, int64_t n, const T* x, T* y, int32_t* mask) { hipLaunchKernelGGL(( ReluGpu<T>), dim3(BlocksNum4ThreadsNum(n)), dim3(kCudaThreadsNumPerBlock), 0, device_ctx->cuda_stream(), n, x, y, mask); } template<> void Relu<float16>(DeviceCtx* device_ctx, int64_t n, const float16* x, float16* y, int32_t* mask) { Relu<half>(device_ctx, n, reinterpret_cast<const half*>(x), reinterpret_cast<half*>(y), mask); } template<typename T> void AddRelu(DeviceCtx* device_ctx, int64_t n, const T* x, const T* addend, T* y, int32_t* mask) { hipLaunchKernelGGL(( AddReluGpu<T>), dim3(BlocksNum4ThreadsNum(n)), dim3(kCudaThreadsNumPerBlock), 0, device_ctx->cuda_stream(), n, x, addend, y, mask); } template<> void AddRelu<float16>(DeviceCtx* device_ctx, int64_t n, const float16* x, const float16* addend, float16* y, int32_t* mask) { AddRelu<half>(device_ctx, n, reinterpret_cast<const half*>(x), reinterpret_cast<const half*>(addend), reinterpret_cast<half*>(y), mask); } template<typename T> __global__ void ReluBackwardGpu(int64_t n, const int32_t* mask, const T* dy, T* addend_diff) { int32_t lane_id = threadIdx.x % kCudaWarpSize; CUDA_1D_KERNEL_LOOP(i, n) { int32_t mask_val = mask[i / kCudaWarpSize]; bool is_positive = mask_val & (1 << lane_id); addend_diff[i] = static_cast<T>(is_positive) * dy[i]; } } template<typename T> void ReluBackward(DeviceCtx* device_ctx, int64_t n, const int32_t* mask, const T* dy, T* addend_diff) { hipLaunchKernelGGL(( ReluBackwardGpu<T>) , dim3(BlocksNum4ThreadsNum(n)), dim3(kCudaThreadsNumPerBlock), 0, device_ctx->cuda_stream(), n, mask, dy, addend_diff); } template<> void ReluBackward<float16>(DeviceCtx* device_ctx, int64_t n, const int32_t* mask, const float16* dy, float16* addend_diff) { ReluBackward<half>(device_ctx, n, mask, reinterpret_cast<const half*>(dy), reinterpret_cast<half*>(addend_diff)); } template<typename T> class NormalizationTrainKernel final : public user_op::OpKernel { public: NormalizationTrainKernel() = default; ~NormalizationTrainKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { if (ctx->op_type_name() == "normalization") { CHECK(ctx->Attr<bool>("training")); } const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const auto momentum = ctx->Attr<float>("momentum"); const DataType data_type = x->data_type(); CHECK_EQ(x->shape(), y->shape()); CHECK_EQ(y->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); user_op::Tensor* moving_mean = nullptr; user_op::Tensor* moving_variance = nullptr; if (ctx->has_input("moving_mean", 0)) { CHECK(ctx->has_input("moving_variance", 0)); moving_mean = ctx->Tensor4ArgNameAndIndex("moving_mean", 0); moving_variance = ctx->Tensor4ArgNameAndIndex("moving_variance", 0); desc_helper.CheckParamTensor(moving_mean); desc_helper.CheckParamTensor(moving_variance); } const void* sp_alpha = CudnnSPOnePtr<T>(); const void* sp_beta; if (ctx->has_input("_add_to_output", 0)) { const user_op::Tensor* add_to_output = ctx->Tensor4ArgNameAndIndex("_add_to_output", 0); CHECK_EQ(add_to_output->data_type(), y->data_type()); CHECK_EQ(add_to_output->shape(), y->shape()); Memcpy<DeviceType::kGPU>( ctx->device_ctx(), y->mut_dptr<void>(), add_to_output->dptr<void>(), add_to_output->shape().elem_cnt() * GetSizeOfDataType(add_to_output->data_type())); sp_beta = CudnnSPOnePtr<T>(); } else { sp_beta = CudnnSPZeroPtr<T>(); } #if defined(BN_ENABLE_EX_API) size_t workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &workspace_size)); size_t reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, nullptr, desc_helper.xy_desc(), &reserve_space_size)); auto* workspace = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); if (reserve_space_size == 0 && workspace_size <= workspace->shape().elem_cnt()) { OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTrainingEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), nullptr, nullptr, desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean ? moving_mean->mut_dptr() : NULL, moving_variance ? moving_variance->mut_dptr() : NULL, epsilon, mean->mut_dptr(), inv_variance->mut_dptr(), nullptr, workspace->mut_dptr(), workspace->shape().elem_cnt(), nullptr, 0)); } else { OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTraining( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean ? moving_mean->mut_dptr() : NULL, moving_variance ? moving_variance->mut_dptr() : NULL, epsilon, mean->mut_dptr(), inv_variance->mut_dptr())); } #else OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTraining( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean ? moving_mean->mut_dptr() : NULL, moving_variance ? moving_variance->mut_dptr() : NULL, epsilon, mean->mut_dptr(), inv_variance->mut_dptr())); #endif if (ctx->op_type_name() == "normalization_add_relu") { CHECK(!ctx->has_input("_add_to_output", 0)); const int64_t elem_cnt = x->shape().elem_cnt(); auto* mask = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); if (ctx->has_input("addend", 0)) { const auto* addend = ctx->Tensor4ArgNameAndIndex("addend", 0); AddRelu(ctx->device_ctx(), elem_cnt, y->dptr<T>(), addend->dptr<T>(), y->mut_dptr<T>(), mask->mut_dptr<int32_t>()); } else { Relu(ctx->device_ctx(), elem_cnt, y->dptr<T>(), y->mut_dptr<T>(), mask->mut_dptr<int32_t>()); } } } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_BN_TRAIN_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization") \ .SetCreateFn<NormalizationTrainKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value) \ & (user_op::HobAttr<bool>("training") == true)) \ .SetInferTmpSizeFn(InferTrainTmpSize) \ .SetInplaceProposalFn([](const user_op::InferContext& ctx, \ user_op::AddInplaceArgPair AddInplaceArgPairFn) -> Maybe<void> { \ if (ctx.has_input("_add_to_output", 0)) { \ OF_RETURN_IF_ERROR(AddInplaceArgPairFn("y", 0, "_add_to_output", 0, true)); \ } \ return Maybe<void>::Ok(); \ }); REGISTER_BN_TRAIN_KERNEL(float16) REGISTER_BN_TRAIN_KERNEL(float) REGISTER_BN_TRAIN_KERNEL(double) #define REGISTER_BN_ADD_RELU_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization_add_relu") \ .SetCreateFn<NormalizationTrainKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferTrainTmpSize); REGISTER_BN_ADD_RELU_KERNEL(float16) REGISTER_BN_ADD_RELU_KERNEL(float) REGISTER_BN_ADD_RELU_KERNEL(double) template<typename T> class NormalizationGradUserKernel final : public user_op::OpKernel { public: NormalizationGradUserKernel() = default; ~NormalizationGradUserKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* dx = ctx->Tensor4ArgNameAndIndex("dx", 0); const auto* dy = ctx->Tensor4ArgNameAndIndex("dy", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); auto* gamma_diff = ctx->Tensor4ArgNameAndIndex("gamma_diff", 0); auto* beta_diff = ctx->Tensor4ArgNameAndIndex("beta_diff", 0); const auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); const auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); auto* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const DataType data_type = x->data_type(); CHECK_EQ(dy->shape(), x->shape()); CHECK_EQ(dy->data_type(), data_type); CHECK_EQ(dx->shape(), x->shape()); CHECK_EQ(dx->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(gamma_diff); desc_helper.CheckParamTensor(beta_diff); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); void* bn_workspace_ptr; size_t bn_workspace_size; const void* bn_dy_ptr; if (ctx->op_type_name() == "normalization_grad") { bn_workspace_ptr = tmp_buffer->mut_dptr(); bn_workspace_size = tmp_buffer->shape().elem_cnt(); bn_dy_ptr = dy->dptr(); } else if (ctx->op_type_name() == "normalization_add_relu_grad") { const int64_t elem_cnt = dy->shape().elem_cnt(); const auto* mask = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); user_op::Tensor* y = ctx->Tensor4ArgNameAndIndex("y", 0); if (ctx->has_output("addend_diff", 0)) { user_op::Tensor* addend_diff = ctx->Tensor4ArgNameAndIndex("addend_diff", 0); ReluBackward(ctx->device_ctx(), elem_cnt, mask->dptr<int32_t>(), dy->dptr<T>(), addend_diff->mut_dptr<T>()); bn_workspace_ptr = tmp_buffer->mut_dptr(); bn_workspace_size = tmp_buffer->shape().elem_cnt(); bn_dy_ptr = addend_diff->dptr(); } else { const size_t tmp_buffer_size = tmp_buffer->shape().elem_cnt(); const size_t relu_dx_size = GetCudaAlignedSize(dy->shape().elem_cnt() * GetSizeOfDataType(dy->data_type())); CHECK_GE(tmp_buffer_size, relu_dx_size); ReluBackward(ctx->device_ctx(), elem_cnt, mask->dptr<int32_t>(), dy->dptr<T>(), reinterpret_cast<T*>(tmp_buffer->mut_dptr())); bn_workspace_ptr = tmp_buffer->mut_dptr<char>() + relu_dx_size; bn_workspace_size = tmp_buffer_size - relu_dx_size; bn_dy_ptr = tmp_buffer->dptr(); } } else { UNIMPLEMENTED(); } #if defined(BN_ENABLE_EX_API) size_t workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &workspace_size)); size_t reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, nullptr, desc_helper.xy_desc(), &reserve_space_size)); if (reserve_space_size == 0 && workspace_size <= bn_workspace_size) { OF_CUDNN_CHECK(cudnnBatchNormalizationBackwardEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), nullptr, nullptr, desc_helper.xy_desc(), bn_dy_ptr, nullptr, nullptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), nullptr, gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr(), nullptr, bn_workspace_ptr, bn_workspace_size, nullptr, 0)); } else { OF_CUDNN_CHECK(cudnnBatchNormalizationBackward( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), bn_dy_ptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr())); } #else OF_CUDNN_CHECK(cudnnBatchNormalizationBackward( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), bn_dy_ptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr())); #endif } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_BN_GRAD_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization_grad") \ .SetCreateFn<NormalizationGradUserKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("dx", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferGradTmpSize); REGISTER_BN_GRAD_KERNEL(float16) REGISTER_BN_GRAD_KERNEL(float) REGISTER_BN_GRAD_KERNEL(double) #define REGISTER_BN_ADD_RELU_GRAD_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization_add_relu_grad") \ .SetCreateFn<NormalizationGradUserKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("dx", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferGradTmpSize); REGISTER_BN_ADD_RELU_GRAD_KERNEL(float16) REGISTER_BN_ADD_RELU_GRAD_KERNEL(float) REGISTER_BN_ADD_RELU_GRAD_KERNEL(double) #if (CUDNN_VERSION >= 7401) size_t InferFusedNormalizationAddReluTmpSize(user_op::InferContext* ctx) { const auto& x = ctx->InputTensorDesc("x", 0); const auto axis = ctx->Attr<int32_t>("axis"); const CudnnTensorDescHelper desc_helper(x.shape(), x.data_type(), axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnBatchNormOps_t ops; cudnnTensorDescriptor_t z_desc; if (ctx->has_input("addend", 0)) { ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; z_desc = desc_helper.xy_desc(); } else { ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; z_desc = nullptr; } OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), z_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &size_in_bytes)); return ::max(size_in_bytes, static_cast<size_t>(1)); } size_t InferFusedNormalizationAddReluGradTmpSize(user_op::InferContext* ctx) { const auto& x = ctx->InputTensorDesc("x", 0); const auto axis = ctx->Attr<int32_t>("axis"); const CudnnTensorDescHelper desc_helper(x.shape(), x.data_type(), axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnBatchNormOps_t ops; cudnnTensorDescriptor_t z_desc; if (ctx->has_output("addend_diff", 0)) { ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; z_desc = desc_helper.xy_desc(); } else { ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; z_desc = nullptr; } OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), desc_helper.xy_desc(), desc_helper.xy_desc(), z_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &size_in_bytes)); return ::max(size_in_bytes, static_cast<size_t>(1)); } template<typename T> class FusedNormalizationAddReluKernel final : public user_op::OpKernel { public: FusedNormalizationAddReluKernel() = default; ~FusedNormalizationAddReluKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* moving_mean = ctx->Tensor4ArgNameAndIndex("moving_mean", 0); auto* moving_variance = ctx->Tensor4ArgNameAndIndex("moving_variance", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const auto momentum = ctx->Attr<float>("momentum"); auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); auto* reserve_space = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); auto* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); const DataType data_type = x->data_type(); CHECK_EQ(x->shape(), y->shape()); CHECK_EQ(y->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(moving_mean); desc_helper.CheckParamTensor(moving_variance); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnTensorDescriptor_t z_desc; const void* z_ptr; cudnnBatchNormOps_t ops; if (ctx->has_input("addend", 0)) { z_desc = desc_helper.xy_desc(); z_ptr = ctx->Tensor4ArgNameAndIndex("addend", 0)->dptr(); ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; } else { z_desc = nullptr; z_ptr = nullptr; ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; } size_t min_workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), z_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &min_workspace_size)); const size_t workspace_size = tmp_buffer->shape().elem_cnt(); CHECK_GE(workspace_size, min_workspace_size); size_t min_reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, activation_desc.Get(), desc_helper.xy_desc(), &min_reserve_space_size)); const size_t reserve_space_size = reserve_space->shape().elem_cnt(); CHECK_GE(reserve_space_size, min_reserve_space_size); OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTrainingEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), z_desc, z_ptr, desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean->mut_dptr(), moving_variance->mut_dptr(), epsilon, mean->mut_dptr(), inv_variance->mut_dptr(), activation_desc.Get(), tmp_buffer->mut_dptr(), workspace_size, reserve_space->mut_dptr(), reserve_space_size)); } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_FUSED_BN_ADD_RELU_KERNEL(dtype) \ REGISTER_USER_KERNEL("cudnn_fused_normalization_add_relu") \ .SetCreateFn<FusedNormalizationAddReluKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferFusedNormalizationAddReluTmpSize); REGISTER_FUSED_BN_ADD_RELU_KERNEL(float16) template<typename T> class FusedNormalizationAddReluGradUserKernel final : public user_op::OpKernel { public: FusedNormalizationAddReluGradUserKernel() = default; ~FusedNormalizationAddReluGradUserKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); const auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); auto* dx = ctx->Tensor4ArgNameAndIndex("dx", 0); const auto* dy = ctx->Tensor4ArgNameAndIndex("dy", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* gamma_diff = ctx->Tensor4ArgNameAndIndex("gamma_diff", 0); auto* beta_diff = ctx->Tensor4ArgNameAndIndex("beta_diff", 0); const auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); const auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); const auto* reserve_space = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); auto* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const DataType data_type = x->data_type(); CHECK_EQ(dy->shape(), x->shape()); CHECK_EQ(dy->data_type(), data_type); CHECK_EQ(dx->shape(), x->shape()); CHECK_EQ(dx->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(gamma_diff); desc_helper.CheckParamTensor(beta_diff); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnTensorDescriptor_t dz_desc; void* dz_ptr; cudnnBatchNormOps_t ops; if (ctx->has_output("addend_diff", 0)) { dz_desc = desc_helper.xy_desc(); dz_ptr = ctx->Tensor4ArgNameAndIndex("addend_diff", 0)->mut_dptr(); ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; } else { dz_desc = nullptr; dz_ptr = nullptr; ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; } size_t min_workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), desc_helper.xy_desc(), desc_helper.xy_desc(), dz_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &min_workspace_size)); const size_t workspace_size = tmp_buffer->shape().elem_cnt(); CHECK_GE(workspace_size, min_workspace_size); size_t min_reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, activation_desc.Get(), desc_helper.xy_desc(), &min_reserve_space_size)); const size_t reserve_space_size = reserve_space->shape().elem_cnt(); CHECK_GE(reserve_space_size, min_reserve_space_size); OF_CUDNN_CHECK(cudnnBatchNormalizationBackwardEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->dptr(), desc_helper.xy_desc(), dy->dptr(), dz_desc, dz_ptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr(), activation_desc.Get(), tmp_buffer->mut_dptr(), workspace_size, const_cast<void*>(reserve_space->dptr()), reserve_space_size)); } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_FUSED_BN_ADD_RELU_GRAD_KERNEL(dtype) \ REGISTER_USER_KERNEL("cudnn_fused_normalization_add_relu_grad") \ .SetCreateFn<FusedNormalizationAddReluGradUserKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("dx", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferFusedNormalizationAddReluGradTmpSize); REGISTER_FUSED_BN_ADD_RELU_GRAD_KERNEL(float16) #endif } // namespace } // namespace oneflow #endif
e2b936c36b4226850c3e73566cee172623f745e6.cu
/* Copyright 2020 The OneFlow Authors. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifdef WITH_CUDA #include <unordered_map> #include "oneflow/core/framework/framework.h" #include "oneflow/core/device/cudnn_util.h" #include "oneflow/core/kernel/new_kernel_util.h" namespace oneflow { namespace { #if (CUDNN_VERSION >= 7401) #define BN_ENABLE_EX_API class CudnnHandleWrapper { public: CudnnHandleWrapper() { OF_CUDNN_CHECK(cudnnCreate(&handle_)); } ~CudnnHandleWrapper() { OF_CUDNN_CHECK(cudnnDestroy(handle_)); } cudnnHandle_t handle() const { return handle_; } private: cudnnHandle_t handle_; }; cudnnHandle_t GetOrCreateCudnnHandle() { static std::unordered_map<int, CudnnHandleWrapper> device_id2handle; int device_id; OF_CUDA_CHECK(cudaGetDevice(&device_id)); // operator [] will create and insert a CudnnHandleWrapper // if key `device_id` does not exist return device_id2handle[device_id].handle(); } #endif void InferDimSizeAndDataFormat(const ShapeView& x_shape, const int32_t axis, int32_t* n, int32_t* c, int32_t* h, int32_t* w, cudnnTensorFormat_t* format) { if (x_shape.Count(axis + 1) == 1) { if (axis == 0) { *n = 1; *h = 1; } else { *n = x_shape.At(0); *h = x_shape.Count(1, axis); } *w = 1; *c = x_shape.At(axis); *format = CUDNN_TENSOR_NHWC; } else { *n = x_shape.Count(0, axis); *c = x_shape.At(axis); *h = x_shape.Count(axis + 1); *w = 1; *format = CUDNN_TENSOR_NCHW; } } void InferXYCudnnTensorDesc(const ShapeView& xy_shape, const DataType& data_type, const int32_t axis, cudnnTensorDescriptor_t xy_desc) { int32_t n, c, h, w; cudnnTensorFormat_t format; InferDimSizeAndDataFormat(xy_shape, axis, &n, &c, &h, &w, &format); OF_CUDNN_CHECK( cudnnSetTensor4dDescriptor(xy_desc, format, GetCudnnDataType(data_type), n, c, h, w)); } void InferParamCudnnTensorDesc(const cudnnTensorDescriptor_t xy_desc, cudnnBatchNormMode_t mode, cudnnTensorDescriptor_t param_desc) { OF_CUDNN_CHECK(cudnnDeriveBNTensorDescriptor(param_desc, xy_desc, mode)); } class CudnnTensorDescHelper final { public: OF_DISALLOW_COPY_AND_MOVE(CudnnTensorDescHelper); CudnnTensorDescHelper(const ShapeView& xy_shape, const DataType& data_type, const int32_t axis, cudnnBatchNormMode_t mode) { OF_CUDNN_CHECK(cudnnCreateTensorDescriptor(&xy_desc_)); InferXYCudnnTensorDesc(xy_shape, data_type, axis, xy_desc_); OF_CUDNN_CHECK(cudnnCreateTensorDescriptor(&param_desc_)); InferParamCudnnTensorDesc(xy_desc_, mode, param_desc_); int n, c, h, w, n_stride, c_stride, h_stride, w_stride; OF_CUDNN_CHECK(cudnnGetTensor4dDescriptor(param_desc_, &param_data_type_, &n, &c, &h, &w, &n_stride, &c_stride, &h_stride, &w_stride)); param_size_ = c; } ~CudnnTensorDescHelper() { OF_CUDNN_CHECK(cudnnDestroyTensorDescriptor(param_desc_)); OF_CUDNN_CHECK(cudnnDestroyTensorDescriptor(xy_desc_)); } cudnnTensorDescriptor_t xy_desc() const { return xy_desc_; } cudnnTensorDescriptor_t param_desc() const { return param_desc_; } void CheckParamTensor(const user_op::Tensor* tensor) const { CHECK_EQ(tensor->shape().NumAxes(), 1); CHECK_EQ(tensor->shape().At(0), param_size_); CHECK_EQ(GetCudnnDataType(tensor->data_type()), param_data_type_); } private: cudnnTensorDescriptor_t xy_desc_ = nullptr; cudnnTensorDescriptor_t param_desc_ = nullptr; cudnnDataType_t param_data_type_; int32_t param_size_ = 0; }; size_t InferTrainWorkspaceSize(const ShapeView& x_shape, const DataType data_type, const int32_t axis) { #if defined(BN_ENABLE_EX_API) const CudnnTensorDescHelper desc_helper(x_shape, data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &size_in_bytes)); return std::max(size_in_bytes, static_cast<size_t>(1)); #else return 1; #endif } size_t InferTrainTmpSize(user_op::InferContext* ctx) { const auto& x = ctx->InputTensorDesc("x", 0); const auto axis = ctx->Attr<int32_t>("axis"); return InferTrainWorkspaceSize(x.shape(), x.data_type(), axis); } size_t InferGradWorkspaceSize(const ShapeView& x_shape, const DataType data_type, const int32_t axis) { #if defined(BN_ENABLE_EX_API) const CudnnTensorDescHelper desc_helper(x_shape, data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &size_in_bytes)); return std::max(size_in_bytes, static_cast<size_t>(1)); #else return 1; #endif } size_t InferGradTmpSize(user_op::InferContext* ctx) { const auto& dy = ctx->InputTensorDesc("dy", 0); const auto axis = ctx->Attr<int32_t>("axis"); size_t tmp_size = 0; if (ctx->op_type_name() == "normalization_add_relu_grad" && !ctx->has_output("addend_diff", 0)) { tmp_size += GetCudaAlignedSize(dy.shape().elem_cnt() * GetSizeOfDataType(dy.data_type())); } tmp_size += GetCudaAlignedSize(InferGradWorkspaceSize(dy.shape(), dy.data_type(), axis)); return tmp_size; } template<typename T> class NormalizationInferenceKernel final : public user_op::OpKernel { public: NormalizationInferenceKernel() = default; ~NormalizationInferenceKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const bool training = ctx->Attr<bool>("training"); CHECK(!training); const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* moving_mean = ctx->Tensor4ArgNameAndIndex("moving_mean", 0); auto* moving_variance = ctx->Tensor4ArgNameAndIndex("moving_variance", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const DataType data_type = x->data_type(); CHECK_EQ(x->shape(), y->shape()); CHECK_EQ(y->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(moving_mean); desc_helper.CheckParamTensor(moving_variance); const void* sp_alpha = CudnnSPOnePtr<T>(); const void* sp_beta; if (ctx->has_input("_add_to_output", 0)) { const user_op::Tensor* add_to_output = ctx->Tensor4ArgNameAndIndex("_add_to_output", 0); CHECK_EQ(add_to_output->data_type(), y->data_type()); CHECK_EQ(add_to_output->shape(), y->shape()); Memcpy<DeviceType::kGPU>( ctx->device_ctx(), y->mut_dptr<void>(), add_to_output->dptr<void>(), add_to_output->shape().elem_cnt() * GetSizeOfDataType(add_to_output->data_type())); sp_beta = CudnnSPOnePtr<T>(); } else { sp_beta = CudnnSPZeroPtr<T>(); } OF_CUDNN_CHECK(cudnnBatchNormalizationForwardInference( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), moving_mean->dptr(), moving_variance->dptr(), epsilon)); } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_BN_INFERENCE_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization") \ .SetCreateFn<NormalizationInferenceKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value) \ & (user_op::HobAttr<bool>("training") == false)) \ .SetInplaceProposalFn([](const user_op::InferContext& ctx, \ user_op::AddInplaceArgPair AddInplaceArgPairFn) -> Maybe<void> { \ if (ctx.has_input("_add_to_output", 0)) { \ OF_RETURN_IF_ERROR(AddInplaceArgPairFn("y", 0, "_add_to_output", 0, true)); \ } \ return Maybe<void>::Ok(); \ }); REGISTER_BN_INFERENCE_KERNEL(float16) REGISTER_BN_INFERENCE_KERNEL(float) REGISTER_BN_INFERENCE_KERNEL(double) #undef REGISTER_BN_INFERENCE_KERNEL constexpr int64_t kCudaWarpSize = 32; template<typename T> __global__ void ReluGpu(int64_t n, const T* x, T* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; CUDA_1D_KERNEL_LOOP(i, n) { const T x_val = x[i]; const bool is_positive = (x_val > 0); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? x_val : 0; } } template<> __global__ void ReluGpu<half>(int64_t n, const half* x, half* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; const half zero = __float2half(0.0f); CUDA_1D_KERNEL_LOOP(i, n) { const half x_val = x[i]; const bool is_positive = __hgt(x_val, zero); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? x_val : zero; } } template<typename T> __global__ void AddReluGpu(int64_t n, const T* x, const T* addend, T* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; CUDA_1D_KERNEL_LOOP(i, n) { const T sum = x[i] + addend[i]; const bool is_positive = (sum > 0); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? sum : 0; } } template<> __global__ void AddReluGpu<half>(int64_t n, const half* x, const half* addend, half* y, int32_t* mask) { const int32_t lane_id = threadIdx.x % kCudaWarpSize; const half zero = __float2half(0.0f); CUDA_1D_KERNEL_LOOP(i, n) { const half sum = __hadd(x[i], addend[i]); const bool is_positive = __hgt(sum, zero); int32_t warp_mask = __ballot_sync(__activemask(), static_cast<int>(is_positive)); if (lane_id == 0) { mask[i / kCudaWarpSize] = warp_mask; } y[i] = is_positive ? sum : zero; } } template<typename T> void Relu(DeviceCtx* device_ctx, int64_t n, const T* x, T* y, int32_t* mask) { ReluGpu<T><<<BlocksNum4ThreadsNum(n), kCudaThreadsNumPerBlock, 0, device_ctx->cuda_stream()>>>( n, x, y, mask); } template<> void Relu<float16>(DeviceCtx* device_ctx, int64_t n, const float16* x, float16* y, int32_t* mask) { Relu<half>(device_ctx, n, reinterpret_cast<const half*>(x), reinterpret_cast<half*>(y), mask); } template<typename T> void AddRelu(DeviceCtx* device_ctx, int64_t n, const T* x, const T* addend, T* y, int32_t* mask) { AddReluGpu<T><<<BlocksNum4ThreadsNum(n), kCudaThreadsNumPerBlock, 0, device_ctx->cuda_stream()>>>( n, x, addend, y, mask); } template<> void AddRelu<float16>(DeviceCtx* device_ctx, int64_t n, const float16* x, const float16* addend, float16* y, int32_t* mask) { AddRelu<half>(device_ctx, n, reinterpret_cast<const half*>(x), reinterpret_cast<const half*>(addend), reinterpret_cast<half*>(y), mask); } template<typename T> __global__ void ReluBackwardGpu(int64_t n, const int32_t* mask, const T* dy, T* addend_diff) { int32_t lane_id = threadIdx.x % kCudaWarpSize; CUDA_1D_KERNEL_LOOP(i, n) { int32_t mask_val = mask[i / kCudaWarpSize]; bool is_positive = mask_val & (1 << lane_id); addend_diff[i] = static_cast<T>(is_positive) * dy[i]; } } template<typename T> void ReluBackward(DeviceCtx* device_ctx, int64_t n, const int32_t* mask, const T* dy, T* addend_diff) { ReluBackwardGpu<T> <<<BlocksNum4ThreadsNum(n), kCudaThreadsNumPerBlock, 0, device_ctx->cuda_stream()>>>( n, mask, dy, addend_diff); } template<> void ReluBackward<float16>(DeviceCtx* device_ctx, int64_t n, const int32_t* mask, const float16* dy, float16* addend_diff) { ReluBackward<half>(device_ctx, n, mask, reinterpret_cast<const half*>(dy), reinterpret_cast<half*>(addend_diff)); } template<typename T> class NormalizationTrainKernel final : public user_op::OpKernel { public: NormalizationTrainKernel() = default; ~NormalizationTrainKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { if (ctx->op_type_name() == "normalization") { CHECK(ctx->Attr<bool>("training")); } const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const auto momentum = ctx->Attr<float>("momentum"); const DataType data_type = x->data_type(); CHECK_EQ(x->shape(), y->shape()); CHECK_EQ(y->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); user_op::Tensor* moving_mean = nullptr; user_op::Tensor* moving_variance = nullptr; if (ctx->has_input("moving_mean", 0)) { CHECK(ctx->has_input("moving_variance", 0)); moving_mean = ctx->Tensor4ArgNameAndIndex("moving_mean", 0); moving_variance = ctx->Tensor4ArgNameAndIndex("moving_variance", 0); desc_helper.CheckParamTensor(moving_mean); desc_helper.CheckParamTensor(moving_variance); } const void* sp_alpha = CudnnSPOnePtr<T>(); const void* sp_beta; if (ctx->has_input("_add_to_output", 0)) { const user_op::Tensor* add_to_output = ctx->Tensor4ArgNameAndIndex("_add_to_output", 0); CHECK_EQ(add_to_output->data_type(), y->data_type()); CHECK_EQ(add_to_output->shape(), y->shape()); Memcpy<DeviceType::kGPU>( ctx->device_ctx(), y->mut_dptr<void>(), add_to_output->dptr<void>(), add_to_output->shape().elem_cnt() * GetSizeOfDataType(add_to_output->data_type())); sp_beta = CudnnSPOnePtr<T>(); } else { sp_beta = CudnnSPZeroPtr<T>(); } #if defined(BN_ENABLE_EX_API) size_t workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &workspace_size)); size_t reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, nullptr, desc_helper.xy_desc(), &reserve_space_size)); auto* workspace = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); if (reserve_space_size == 0 && workspace_size <= workspace->shape().elem_cnt()) { OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTrainingEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), nullptr, nullptr, desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean ? moving_mean->mut_dptr() : NULL, moving_variance ? moving_variance->mut_dptr() : NULL, epsilon, mean->mut_dptr(), inv_variance->mut_dptr(), nullptr, workspace->mut_dptr(), workspace->shape().elem_cnt(), nullptr, 0)); } else { OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTraining( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean ? moving_mean->mut_dptr() : NULL, moving_variance ? moving_variance->mut_dptr() : NULL, epsilon, mean->mut_dptr(), inv_variance->mut_dptr())); } #else OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTraining( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, sp_alpha, sp_beta, desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean ? moving_mean->mut_dptr() : NULL, moving_variance ? moving_variance->mut_dptr() : NULL, epsilon, mean->mut_dptr(), inv_variance->mut_dptr())); #endif if (ctx->op_type_name() == "normalization_add_relu") { CHECK(!ctx->has_input("_add_to_output", 0)); const int64_t elem_cnt = x->shape().elem_cnt(); auto* mask = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); if (ctx->has_input("addend", 0)) { const auto* addend = ctx->Tensor4ArgNameAndIndex("addend", 0); AddRelu(ctx->device_ctx(), elem_cnt, y->dptr<T>(), addend->dptr<T>(), y->mut_dptr<T>(), mask->mut_dptr<int32_t>()); } else { Relu(ctx->device_ctx(), elem_cnt, y->dptr<T>(), y->mut_dptr<T>(), mask->mut_dptr<int32_t>()); } } } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_BN_TRAIN_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization") \ .SetCreateFn<NormalizationTrainKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value) \ & (user_op::HobAttr<bool>("training") == true)) \ .SetInferTmpSizeFn(InferTrainTmpSize) \ .SetInplaceProposalFn([](const user_op::InferContext& ctx, \ user_op::AddInplaceArgPair AddInplaceArgPairFn) -> Maybe<void> { \ if (ctx.has_input("_add_to_output", 0)) { \ OF_RETURN_IF_ERROR(AddInplaceArgPairFn("y", 0, "_add_to_output", 0, true)); \ } \ return Maybe<void>::Ok(); \ }); REGISTER_BN_TRAIN_KERNEL(float16) REGISTER_BN_TRAIN_KERNEL(float) REGISTER_BN_TRAIN_KERNEL(double) #define REGISTER_BN_ADD_RELU_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization_add_relu") \ .SetCreateFn<NormalizationTrainKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferTrainTmpSize); REGISTER_BN_ADD_RELU_KERNEL(float16) REGISTER_BN_ADD_RELU_KERNEL(float) REGISTER_BN_ADD_RELU_KERNEL(double) template<typename T> class NormalizationGradUserKernel final : public user_op::OpKernel { public: NormalizationGradUserKernel() = default; ~NormalizationGradUserKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* dx = ctx->Tensor4ArgNameAndIndex("dx", 0); const auto* dy = ctx->Tensor4ArgNameAndIndex("dy", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); auto* gamma_diff = ctx->Tensor4ArgNameAndIndex("gamma_diff", 0); auto* beta_diff = ctx->Tensor4ArgNameAndIndex("beta_diff", 0); const auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); const auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); auto* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const DataType data_type = x->data_type(); CHECK_EQ(dy->shape(), x->shape()); CHECK_EQ(dy->data_type(), data_type); CHECK_EQ(dx->shape(), x->shape()); CHECK_EQ(dx->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(gamma_diff); desc_helper.CheckParamTensor(beta_diff); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); void* bn_workspace_ptr; size_t bn_workspace_size; const void* bn_dy_ptr; if (ctx->op_type_name() == "normalization_grad") { bn_workspace_ptr = tmp_buffer->mut_dptr(); bn_workspace_size = tmp_buffer->shape().elem_cnt(); bn_dy_ptr = dy->dptr(); } else if (ctx->op_type_name() == "normalization_add_relu_grad") { const int64_t elem_cnt = dy->shape().elem_cnt(); const auto* mask = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); user_op::Tensor* y = ctx->Tensor4ArgNameAndIndex("y", 0); if (ctx->has_output("addend_diff", 0)) { user_op::Tensor* addend_diff = ctx->Tensor4ArgNameAndIndex("addend_diff", 0); ReluBackward(ctx->device_ctx(), elem_cnt, mask->dptr<int32_t>(), dy->dptr<T>(), addend_diff->mut_dptr<T>()); bn_workspace_ptr = tmp_buffer->mut_dptr(); bn_workspace_size = tmp_buffer->shape().elem_cnt(); bn_dy_ptr = addend_diff->dptr(); } else { const size_t tmp_buffer_size = tmp_buffer->shape().elem_cnt(); const size_t relu_dx_size = GetCudaAlignedSize(dy->shape().elem_cnt() * GetSizeOfDataType(dy->data_type())); CHECK_GE(tmp_buffer_size, relu_dx_size); ReluBackward(ctx->device_ctx(), elem_cnt, mask->dptr<int32_t>(), dy->dptr<T>(), reinterpret_cast<T*>(tmp_buffer->mut_dptr())); bn_workspace_ptr = tmp_buffer->mut_dptr<char>() + relu_dx_size; bn_workspace_size = tmp_buffer_size - relu_dx_size; bn_dy_ptr = tmp_buffer->dptr(); } } else { UNIMPLEMENTED(); } #if defined(BN_ENABLE_EX_API) size_t workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), nullptr, desc_helper.xy_desc(), desc_helper.param_desc(), nullptr, &workspace_size)); size_t reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, nullptr, desc_helper.xy_desc(), &reserve_space_size)); if (reserve_space_size == 0 && workspace_size <= bn_workspace_size) { OF_CUDNN_CHECK(cudnnBatchNormalizationBackwardEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CUDNN_BATCHNORM_OPS_BN, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), nullptr, nullptr, desc_helper.xy_desc(), bn_dy_ptr, nullptr, nullptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), nullptr, gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr(), nullptr, bn_workspace_ptr, bn_workspace_size, nullptr, 0)); } else { OF_CUDNN_CHECK(cudnnBatchNormalizationBackward( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), bn_dy_ptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr())); } #else OF_CUDNN_CHECK(cudnnBatchNormalizationBackward( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), bn_dy_ptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr())); #endif } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_BN_GRAD_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization_grad") \ .SetCreateFn<NormalizationGradUserKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("dx", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferGradTmpSize); REGISTER_BN_GRAD_KERNEL(float16) REGISTER_BN_GRAD_KERNEL(float) REGISTER_BN_GRAD_KERNEL(double) #define REGISTER_BN_ADD_RELU_GRAD_KERNEL(dtype) \ REGISTER_USER_KERNEL("normalization_add_relu_grad") \ .SetCreateFn<NormalizationGradUserKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("dx", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferGradTmpSize); REGISTER_BN_ADD_RELU_GRAD_KERNEL(float16) REGISTER_BN_ADD_RELU_GRAD_KERNEL(float) REGISTER_BN_ADD_RELU_GRAD_KERNEL(double) #if (CUDNN_VERSION >= 7401) size_t InferFusedNormalizationAddReluTmpSize(user_op::InferContext* ctx) { const auto& x = ctx->InputTensorDesc("x", 0); const auto axis = ctx->Attr<int32_t>("axis"); const CudnnTensorDescHelper desc_helper(x.shape(), x.data_type(), axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnBatchNormOps_t ops; cudnnTensorDescriptor_t z_desc; if (ctx->has_input("addend", 0)) { ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; z_desc = desc_helper.xy_desc(); } else { ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; z_desc = nullptr; } OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), z_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &size_in_bytes)); return std::max(size_in_bytes, static_cast<size_t>(1)); } size_t InferFusedNormalizationAddReluGradTmpSize(user_op::InferContext* ctx) { const auto& x = ctx->InputTensorDesc("x", 0); const auto axis = ctx->Attr<int32_t>("axis"); const CudnnTensorDescHelper desc_helper(x.shape(), x.data_type(), axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); size_t size_in_bytes; cudnnHandle_t handle = GetOrCreateCudnnHandle(); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnBatchNormOps_t ops; cudnnTensorDescriptor_t z_desc; if (ctx->has_output("addend_diff", 0)) { ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; z_desc = desc_helper.xy_desc(); } else { ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; z_desc = nullptr; } OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( handle, CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), desc_helper.xy_desc(), desc_helper.xy_desc(), z_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &size_in_bytes)); return std::max(size_in_bytes, static_cast<size_t>(1)); } template<typename T> class FusedNormalizationAddReluKernel final : public user_op::OpKernel { public: FusedNormalizationAddReluKernel() = default; ~FusedNormalizationAddReluKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* moving_mean = ctx->Tensor4ArgNameAndIndex("moving_mean", 0); auto* moving_variance = ctx->Tensor4ArgNameAndIndex("moving_variance", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const auto momentum = ctx->Attr<float>("momentum"); auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); auto* reserve_space = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); auto* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); const DataType data_type = x->data_type(); CHECK_EQ(x->shape(), y->shape()); CHECK_EQ(y->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(moving_mean); desc_helper.CheckParamTensor(moving_variance); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnTensorDescriptor_t z_desc; const void* z_ptr; cudnnBatchNormOps_t ops; if (ctx->has_input("addend", 0)) { z_desc = desc_helper.xy_desc(); z_ptr = ctx->Tensor4ArgNameAndIndex("addend", 0)->dptr(); ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; } else { z_desc = nullptr; z_ptr = nullptr; ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; } size_t min_workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), z_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &min_workspace_size)); const size_t workspace_size = tmp_buffer->shape().elem_cnt(); CHECK_GE(workspace_size, min_workspace_size); size_t min_reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, activation_desc.Get(), desc_helper.xy_desc(), &min_reserve_space_size)); const size_t reserve_space_size = reserve_space->shape().elem_cnt(); CHECK_GE(reserve_space_size, min_reserve_space_size); OF_CUDNN_CHECK(cudnnBatchNormalizationForwardTrainingEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), z_desc, z_ptr, desc_helper.xy_desc(), y->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), 1.0 - momentum, moving_mean->mut_dptr(), moving_variance->mut_dptr(), epsilon, mean->mut_dptr(), inv_variance->mut_dptr(), activation_desc.Get(), tmp_buffer->mut_dptr(), workspace_size, reserve_space->mut_dptr(), reserve_space_size)); } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_FUSED_BN_ADD_RELU_KERNEL(dtype) \ REGISTER_USER_KERNEL("cudnn_fused_normalization_add_relu") \ .SetCreateFn<FusedNormalizationAddReluKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("y", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferFusedNormalizationAddReluTmpSize); REGISTER_FUSED_BN_ADD_RELU_KERNEL(float16) template<typename T> class FusedNormalizationAddReluGradUserKernel final : public user_op::OpKernel { public: FusedNormalizationAddReluGradUserKernel() = default; ~FusedNormalizationAddReluGradUserKernel() override = default; private: void Compute(user_op::KernelComputeContext* ctx) const override { const auto* x = ctx->Tensor4ArgNameAndIndex("x", 0); const auto* y = ctx->Tensor4ArgNameAndIndex("y", 0); auto* dx = ctx->Tensor4ArgNameAndIndex("dx", 0); const auto* dy = ctx->Tensor4ArgNameAndIndex("dy", 0); const auto* gamma = ctx->Tensor4ArgNameAndIndex("gamma", 0); const auto* beta = ctx->Tensor4ArgNameAndIndex("beta", 0); auto* gamma_diff = ctx->Tensor4ArgNameAndIndex("gamma_diff", 0); auto* beta_diff = ctx->Tensor4ArgNameAndIndex("beta_diff", 0); const auto* mean = ctx->Tensor4ArgNameAndIndex("mean", 0); const auto* inv_variance = ctx->Tensor4ArgNameAndIndex("inv_variance", 0); const auto* reserve_space = ctx->Tensor4ArgNameAndIndex("reserve_space", 0); auto* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); const auto axis = ctx->Attr<int32_t>("axis"); const auto epsilon = ctx->Attr<float>("epsilon"); const DataType data_type = x->data_type(); CHECK_EQ(dy->shape(), x->shape()); CHECK_EQ(dy->data_type(), data_type); CHECK_EQ(dx->shape(), x->shape()); CHECK_EQ(dx->data_type(), data_type); CHECK_GE(axis, 0); CHECK_LT(axis, x->shape().NumAxes()); const CudnnTensorDescHelper desc_helper(x->shape(), data_type, axis, CUDNN_BATCHNORM_SPATIAL_PERSISTENT); desc_helper.CheckParamTensor(gamma); desc_helper.CheckParamTensor(beta); desc_helper.CheckParamTensor(gamma_diff); desc_helper.CheckParamTensor(beta_diff); desc_helper.CheckParamTensor(mean); desc_helper.CheckParamTensor(inv_variance); CudnnActivationDesc activation_desc(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, 0); cudnnTensorDescriptor_t dz_desc; void* dz_ptr; cudnnBatchNormOps_t ops; if (ctx->has_output("addend_diff", 0)) { dz_desc = desc_helper.xy_desc(); dz_ptr = ctx->Tensor4ArgNameAndIndex("addend_diff", 0)->mut_dptr(); ops = CUDNN_BATCHNORM_OPS_BN_ADD_ACTIVATION; } else { dz_desc = nullptr; dz_ptr = nullptr; ops = CUDNN_BATCHNORM_OPS_BN_ACTIVATION; } size_t min_workspace_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationBackwardExWorkspaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, desc_helper.xy_desc(), desc_helper.xy_desc(), desc_helper.xy_desc(), dz_desc, desc_helper.xy_desc(), desc_helper.param_desc(), activation_desc.Get(), &min_workspace_size)); const size_t workspace_size = tmp_buffer->shape().elem_cnt(); CHECK_GE(workspace_size, min_workspace_size); size_t min_reserve_space_size; OF_CUDNN_CHECK(cudnnGetBatchNormalizationTrainingExReserveSpaceSize( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, activation_desc.Get(), desc_helper.xy_desc(), &min_reserve_space_size)); const size_t reserve_space_size = reserve_space->shape().elem_cnt(); CHECK_GE(reserve_space_size, min_reserve_space_size); OF_CUDNN_CHECK(cudnnBatchNormalizationBackwardEx( ctx->device_ctx()->cudnn_handle(), CUDNN_BATCHNORM_SPATIAL_PERSISTENT, ops, CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), CudnnSPOnePtr<T>(), CudnnSPZeroPtr<T>(), desc_helper.xy_desc(), x->dptr(), desc_helper.xy_desc(), y->dptr(), desc_helper.xy_desc(), dy->dptr(), dz_desc, dz_ptr, desc_helper.xy_desc(), dx->mut_dptr(), desc_helper.param_desc(), gamma->dptr(), beta->dptr(), gamma_diff->mut_dptr(), beta_diff->mut_dptr(), epsilon, mean->dptr(), inv_variance->dptr(), activation_desc.Get(), tmp_buffer->mut_dptr(), workspace_size, const_cast<void*>(reserve_space->dptr()), reserve_space_size)); } bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } }; #define REGISTER_FUSED_BN_ADD_RELU_GRAD_KERNEL(dtype) \ REGISTER_USER_KERNEL("cudnn_fused_normalization_add_relu_grad") \ .SetCreateFn<FusedNormalizationAddReluGradUserKernel<dtype>>() \ .SetIsMatchedHob((user_op::HobDeviceTag() == "gpu") \ & (user_op::HobDataType("dx", 0) == GetDataType<dtype>::value)) \ .SetInferTmpSizeFn(InferFusedNormalizationAddReluGradTmpSize); REGISTER_FUSED_BN_ADD_RELU_GRAD_KERNEL(float16) #endif } // namespace } // namespace oneflow #endif
f0552dde54cde25a3caffbf90d4aca682f82bb43.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // timestep subroutines #include "interface.h" __device__ static bool is_halo(sim_t &sim, int ibox) { if ((ibox % sim.nx) >= sim.nx-2) return true; if ((ibox / sim.nx) % sim.ny >= sim.ny-2) return true; if ((ibox / sim.nx) / sim.ny >= sim.nz-2) return true; return false; } __global__ void AdvanceVelocity (sim_t sim) { int i_atom = threadIdx.x; for( int ibox = blockIdx.x; ibox < sim.n_cells; ibox += gridDim.x * blockDim.x ) { if (is_halo(sim, ibox)) continue; int offset = N_MAX_ATOMS; real_t dt_local = sim.dt; if (i_atom < sim.grid.n_atoms[ibox]) { sim.p.x[i_atom + offset*ibox] -= dt_local*sim.f.x[i_atom + offset*ibox]; sim.p.y[i_atom + offset*ibox] -= dt_local*sim.f.y[i_atom + offset*ibox]; sim.p.z[i_atom + offset*ibox] -= dt_local*sim.f.z[i_atom + offset*ibox]; } } } extern "C" void advance_velocity(sim_t sim_D) { int grid = sim_D.n_cells; int block = sim_D.max_atoms; hipLaunchKernelGGL(( AdvanceVelocity), dim3(grid),dim3(block), 0, 0, sim_D); hipDeviceSynchronize(); } __global__ void AdvancePositions (sim_t sim) { int i_atom = threadIdx.x; for( int ibox = blockIdx.x; ibox < sim.n_cells; ibox += gridDim.x * blockDim.x ) { if (is_halo(sim, ibox)) continue; int offset = N_MAX_ATOMS; real_t dt_local = (sim.dt/2) / sim.rmass; if (i_atom < sim.grid.n_atoms[ibox]) { sim.r.x[i_atom + offset*ibox] += dt_local*sim.p.x[i_atom + offset*ibox]; sim.r.y[i_atom + offset*ibox] += dt_local*sim.p.y[i_atom + offset*ibox]; sim.r.z[i_atom + offset*ibox] += dt_local*sim.p.z[i_atom + offset*ibox]; } } } extern "C" void advance_position(sim_t sim_D) { int grid = sim_D.n_cells; int block = sim_D.max_atoms; hipLaunchKernelGGL(( AdvancePositions), dim3(grid),dim3(block), 0, 0, sim_D); hipDeviceSynchronize(); }
f0552dde54cde25a3caffbf90d4aca682f82bb43.cu
// timestep subroutines #include "interface.h" __device__ static bool is_halo(sim_t &sim, int ibox) { if ((ibox % sim.nx) >= sim.nx-2) return true; if ((ibox / sim.nx) % sim.ny >= sim.ny-2) return true; if ((ibox / sim.nx) / sim.ny >= sim.nz-2) return true; return false; } __global__ void AdvanceVelocity (sim_t sim) { int i_atom = threadIdx.x; for( int ibox = blockIdx.x; ibox < sim.n_cells; ibox += gridDim.x * blockDim.x ) { if (is_halo(sim, ibox)) continue; int offset = N_MAX_ATOMS; real_t dt_local = sim.dt; if (i_atom < sim.grid.n_atoms[ibox]) { sim.p.x[i_atom + offset*ibox] -= dt_local*sim.f.x[i_atom + offset*ibox]; sim.p.y[i_atom + offset*ibox] -= dt_local*sim.f.y[i_atom + offset*ibox]; sim.p.z[i_atom + offset*ibox] -= dt_local*sim.f.z[i_atom + offset*ibox]; } } } extern "C" void advance_velocity(sim_t sim_D) { int grid = sim_D.n_cells; int block = sim_D.max_atoms; AdvanceVelocity<<<grid,block>>>(sim_D); cudaDeviceSynchronize(); } __global__ void AdvancePositions (sim_t sim) { int i_atom = threadIdx.x; for( int ibox = blockIdx.x; ibox < sim.n_cells; ibox += gridDim.x * blockDim.x ) { if (is_halo(sim, ibox)) continue; int offset = N_MAX_ATOMS; real_t dt_local = (sim.dt/2) / sim.rmass; if (i_atom < sim.grid.n_atoms[ibox]) { sim.r.x[i_atom + offset*ibox] += dt_local*sim.p.x[i_atom + offset*ibox]; sim.r.y[i_atom + offset*ibox] += dt_local*sim.p.y[i_atom + offset*ibox]; sim.r.z[i_atom + offset*ibox] += dt_local*sim.p.z[i_atom + offset*ibox]; } } } extern "C" void advance_position(sim_t sim_D) { int grid = sim_D.n_cells; int block = sim_D.max_atoms; AdvancePositions<<<grid,block>>>(sim_D); cudaDeviceSynchronize(); }
0659f77f8ea73a90920eb406e5af820c569a8f44.hip
// !!! This is a file automatically generated by hipify!!! #include <opencv2/opencv.hpp> #include <thrust/window_2d.h> #include <thrust/window_transform.h> using namespace cv; #define alpha 0.5 class blendFunctor { public: __device__ void operator() (const thrust::window_2d<uchar> &inputWindow1,const thrust::window_2d<uchar> &inputWindow2,const thrust::window_2d<uchar> &outputWindow) const { outputWindow[0][0] = alpha * inputWindow1[make_int2(0,0)]+(1-alpha) * inputWindow2[make_int2(0,0)]; } }; int main(int argc, char const *argv[]) { int dim = 512; if(argc ==2) { dim = atoi(argv[1]); } hipDeviceProp_t dev_prop; hipGetDeviceProperties(&dev_prop,0); Mat input1 = imread("car.jpg",CV_LOAD_IMAGE_GRAYSCALE); Mat input2 = imread("car.jpg",CV_LOAD_IMAGE_GRAYSCALE); Mat temp1; resize(input1,temp1,Size(dim,dim)); input1 = temp1; Mat temp2; resize(input2,temp2,Size(dim,dim)); input2 = temp2; thrust::block_2d<uchar> input_image_block_1 (input1.cols,input1.rows); thrust::block_2d<uchar> input_image_block_2 (input2.cols,input2.rows); thrust::block_2d<uchar> output_image_block (input1.cols,input1.rows); uchar * ucharImageData1 = (uchar * )malloc(sizeof(uchar)*(input1.cols*input1.rows)); uchar * ucharImageData2 = (uchar * )malloc(sizeof(uchar)*(input1.cols*input1.rows)); unsigned char * charImageData = (unsigned char *)malloc(sizeof(unsigned char)*(input_image_block_1.end()-input_image_block_1.begin())); for(int i = 0; i<input1.cols*input1.rows;i++) { ucharImageData1[i]=(uchar)input1.ptr()[i]; } input_image_block_1.upload(ucharImageData1); for(int i = 0; i<input1.cols*input1.rows;i++) { ucharImageData2[i]=(uchar)input2.ptr()[i]; } input_image_block_2.upload(ucharImageData2); thrust::window_vector<uchar> inputWindow1 (&input_image_block_1,1,1,1,1); thrust::window_vector<uchar> inputWindow2 (&input_image_block_2,1,1,1,1); thrust::window_vector<uchar> outputWindow (&output_image_block,1,1,1,1); thrust::transform(thrust::hip::texture,inputWindow1.begin(),inputWindow1.end(),inputWindow2.begin(),outputWindow.begin(),blendFunctor()); output_image_block.download(&charImageData); // for(int i = 0; i<input1.cols*input1.rows;i++) // { // charImageData[i]=(unsigned char)ucharImageData[i]; // } Mat output (Size(input1.cols,input1.rows),CV_8UC1,charImageData); #ifdef OWRITE imwrite("blend-input1.png",input1); imwrite("blend-input2.png",input2); imwrite("blend-output.png",output); #endif #ifdef SHOW imshow("blend-input1.png",input1); imshow("blend-input2.png",input2); imshow("blend-output.png",output); waitKey(0); #endif return 0; }
0659f77f8ea73a90920eb406e5af820c569a8f44.cu
#include <opencv2/opencv.hpp> #include <thrust/window_2d.h> #include <thrust/window_transform.h> using namespace cv; #define alpha 0.5 class blendFunctor { public: __device__ void operator() (const thrust::window_2d<uchar> &inputWindow1,const thrust::window_2d<uchar> &inputWindow2,const thrust::window_2d<uchar> &outputWindow) const { outputWindow[0][0] = alpha * inputWindow1[make_int2(0,0)]+(1-alpha) * inputWindow2[make_int2(0,0)]; } }; int main(int argc, char const *argv[]) { int dim = 512; if(argc ==2) { dim = atoi(argv[1]); } cudaDeviceProp dev_prop; cudaGetDeviceProperties(&dev_prop,0); Mat input1 = imread("car.jpg",CV_LOAD_IMAGE_GRAYSCALE); Mat input2 = imread("car.jpg",CV_LOAD_IMAGE_GRAYSCALE); Mat temp1; resize(input1,temp1,Size(dim,dim)); input1 = temp1; Mat temp2; resize(input2,temp2,Size(dim,dim)); input2 = temp2; thrust::block_2d<uchar> input_image_block_1 (input1.cols,input1.rows); thrust::block_2d<uchar> input_image_block_2 (input2.cols,input2.rows); thrust::block_2d<uchar> output_image_block (input1.cols,input1.rows); uchar * ucharImageData1 = (uchar * )malloc(sizeof(uchar)*(input1.cols*input1.rows)); uchar * ucharImageData2 = (uchar * )malloc(sizeof(uchar)*(input1.cols*input1.rows)); unsigned char * charImageData = (unsigned char *)malloc(sizeof(unsigned char)*(input_image_block_1.end()-input_image_block_1.begin())); for(int i = 0; i<input1.cols*input1.rows;i++) { ucharImageData1[i]=(uchar)input1.ptr()[i]; } input_image_block_1.upload(ucharImageData1); for(int i = 0; i<input1.cols*input1.rows;i++) { ucharImageData2[i]=(uchar)input2.ptr()[i]; } input_image_block_2.upload(ucharImageData2); thrust::window_vector<uchar> inputWindow1 (&input_image_block_1,1,1,1,1); thrust::window_vector<uchar> inputWindow2 (&input_image_block_2,1,1,1,1); thrust::window_vector<uchar> outputWindow (&output_image_block,1,1,1,1); thrust::transform(thrust::cuda::texture,inputWindow1.begin(),inputWindow1.end(),inputWindow2.begin(),outputWindow.begin(),blendFunctor()); output_image_block.download(&charImageData); // for(int i = 0; i<input1.cols*input1.rows;i++) // { // charImageData[i]=(unsigned char)ucharImageData[i]; // } Mat output (Size(input1.cols,input1.rows),CV_8UC1,charImageData); #ifdef OWRITE imwrite("blend-input1.png",input1); imwrite("blend-input2.png",input2); imwrite("blend-output.png",output); #endif #ifdef SHOW imshow("blend-input1.png",input1); imshow("blend-input2.png",input2); imshow("blend-output.png",output); waitKey(0); #endif return 0; }
e4ed0369c70539fb9ef79cf1cd15274a3a8f0209.hip
// !!! This is a file automatically generated by hipify!!! #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <cstring> #include <hip/hip_runtime.h> #include <hip/hip_runtime.h> #include "functions.hpp" //#define MAX_FILE_SIZE 1073741824 // Value in bytes. (1GB) #define MAX_FILE_SIZE 1048576 // Value in bytes. (1MB) #define MAX_LINE_SIZE 200 #define MAX_LINES 1000 #define MAX_LOCATIONS 500 using namespace std; extern const int MAXS; extern const int MAXC; extern int failure_function[MAXS]; extern int goto_function[MAXS][MAXC]; extern int output[MAXS]; int main(int argc, char** argv) { if(argc < 3){ cout << "Usage:\n" << argv[0] << " <keywords file name>" << " <text file name>" << endl; cout << "Example:\n" << argv[0] << " keywords.txt input_text.txt" << endl; exit(1); } char **keywords; int keywords_size, total_length; cout << "Reading keywords..." << endl; keywords = read_keywords(argv[1], keywords_size, total_length); cout << "Keywords:" << endl; for(int i = 0; i < keywords_size; i++) cout << keywords[i] << endl; cout << "Building goto function..." << endl; //int total_states = build_matching_machine(keywords, keywords_size,'a', 'z'); int total_states = build_matching_machine(keywords, keywords_size, '!', '~'); if(total_states > 1) cout << "Goto built!" << endl; else cout << "Could not build goto function" << endl; cout << "\nMatching machine:\n"; //print_matching_machine('a', 'z', total_states); print_matching_machine('!', '~', total_states); cout << "\nFailure function:\n"; print_failure_function(total_states); fstream file; string text; char full_text[MAX_FILE_SIZE], new_line[MAX_LINE_SIZE]; int key_sizes[keywords_size]; // Reading input text file. for(int i = 0; i < keywords_size; i++) key_sizes[i] = strlen(keywords[i]); file.open(argv[2], fstream::in | fstream::out); int idx = 1, pos, line_pos[MAX_LINES], num_lines; full_text[0] = '\0'; pos = num_lines = 0; line_pos[0] = pos; while(!file.fail() && !file.eof() && file.is_open()){ getline(file, text); if(text.size() == 0) continue; text.copy(new_line, text.size(), 0); new_line[text.size()] = '\0'; strcat(full_text, new_line); pos += text.size() + 1; full_text[pos - 1] = '\n'; full_text[pos] = '\0'; line_pos[idx++] = pos; num_lines++; } file.close(); // Making preparations for kernel launch. dim3 grid, block; //block.x = 1024; //grid.x = 15; block.x = 128; grid.x = 1; //location_t locations[MAX_LOCATIONS], *d_locations; location_t *locations, *d_locations; locations = (location_t *)malloc(MAX_LOCATIONS * sizeof(location_t)); int *d_key_sizes, *d_line_pos; int *d_goto_function, *d_failure_function, *d_output; char *d_text; hipMalloc((void**)&d_locations, MAX_LOCATIONS * sizeof(location_t)); hipMalloc((void**)&d_key_sizes, keywords_size * sizeof(int)); hipMalloc((void**)&d_line_pos, MAX_LINES * sizeof(int)); hipMalloc((void**)&d_goto_function, MAXS*MAXC * sizeof(int)); hipMalloc((void**)&d_failure_function, MAXS * sizeof(int)); hipMalloc((void**)&d_output, MAXS * sizeof(int)); hipMalloc((void**)&d_text, MAX_FILE_SIZE * sizeof(char)); hipMemcpy(d_key_sizes, key_sizes, keywords_size * sizeof(int), hipMemcpyHostToDevice); hipMemcpy(d_line_pos, line_pos, num_lines * sizeof(int), hipMemcpyHostToDevice); hipMemcpy(d_failure_function, failure_function, total_states * sizeof(int), hipMemcpyHostToDevice); hipMemcpy(d_output, output, MAXS * sizeof(int), hipMemcpyHostToDevice); hipMemcpy(d_text, full_text, strlen(full_text) * sizeof(char), hipMemcpyHostToDevice); for(int i = 0; i < MAXS; i++) hipMemcpy(&d_goto_function[i*MAXC], goto_function[i], MAXC * sizeof(char), hipMemcpyHostToDevice); cout << "\nSearching occurrences of keywords in the text...\n"; //search_pattern(locations, keywords_size, key_sizes, text, 'a'); hipLaunchKernelGGL(( search_pattern), dim3(grid), dim3(block), 0, 0, d_locations, keywords_size, d_key_sizes, d_text, num_lines, d_line_pos, d_goto_function, d_failure_function, d_output, '!'); hipMemcpy(locations, d_locations, MAX_LOCATIONS * sizeof(location_t), hipMemcpyDeviceToHost); /*int line = 1; if(locations->size == 0){ line++; continue; } cout << "\nOccurrences in line " << line++ << endl;*/ cout << "\nOccurrences:\n"; for(int i = 0; i < 10 && locations[i].keyword < keywords_size; i++){ cout << "Keyword " << keywords[locations[i].keyword] << " appears from " << locations[i].begin << " to " << locations[i].end << endl; } for(int i = 0; i < keywords_size; i++) free(keywords[i]); free(keywords); free(locations); return(0); }
e4ed0369c70539fb9ef79cf1cd15274a3a8f0209.cu
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <cstring> #include <cuda.h> #include <cuda_runtime.h> #include "functions.hpp" //#define MAX_FILE_SIZE 1073741824 // Value in bytes. (1GB) #define MAX_FILE_SIZE 1048576 // Value in bytes. (1MB) #define MAX_LINE_SIZE 200 #define MAX_LINES 1000 #define MAX_LOCATIONS 500 using namespace std; extern const int MAXS; extern const int MAXC; extern int failure_function[MAXS]; extern int goto_function[MAXS][MAXC]; extern int output[MAXS]; int main(int argc, char** argv) { if(argc < 3){ cout << "Usage:\n" << argv[0] << " <keywords file name>" << " <text file name>" << endl; cout << "Example:\n" << argv[0] << " keywords.txt input_text.txt" << endl; exit(1); } char **keywords; int keywords_size, total_length; cout << "Reading keywords..." << endl; keywords = read_keywords(argv[1], keywords_size, total_length); cout << "Keywords:" << endl; for(int i = 0; i < keywords_size; i++) cout << keywords[i] << endl; cout << "Building goto function..." << endl; //int total_states = build_matching_machine(keywords, keywords_size,'a', 'z'); int total_states = build_matching_machine(keywords, keywords_size, '!', '~'); if(total_states > 1) cout << "Goto built!" << endl; else cout << "Could not build goto function" << endl; cout << "\nMatching machine:\n"; //print_matching_machine('a', 'z', total_states); print_matching_machine('!', '~', total_states); cout << "\nFailure function:\n"; print_failure_function(total_states); fstream file; string text; char full_text[MAX_FILE_SIZE], new_line[MAX_LINE_SIZE]; int key_sizes[keywords_size]; // Reading input text file. for(int i = 0; i < keywords_size; i++) key_sizes[i] = strlen(keywords[i]); file.open(argv[2], fstream::in | fstream::out); int idx = 1, pos, line_pos[MAX_LINES], num_lines; full_text[0] = '\0'; pos = num_lines = 0; line_pos[0] = pos; while(!file.fail() && !file.eof() && file.is_open()){ getline(file, text); if(text.size() == 0) continue; text.copy(new_line, text.size(), 0); new_line[text.size()] = '\0'; strcat(full_text, new_line); pos += text.size() + 1; full_text[pos - 1] = '\n'; full_text[pos] = '\0'; line_pos[idx++] = pos; num_lines++; } file.close(); // Making preparations for kernel launch. dim3 grid, block; //block.x = 1024; //grid.x = 15; block.x = 128; grid.x = 1; //location_t locations[MAX_LOCATIONS], *d_locations; location_t *locations, *d_locations; locations = (location_t *)malloc(MAX_LOCATIONS * sizeof(location_t)); int *d_key_sizes, *d_line_pos; int *d_goto_function, *d_failure_function, *d_output; char *d_text; cudaMalloc((void**)&d_locations, MAX_LOCATIONS * sizeof(location_t)); cudaMalloc((void**)&d_key_sizes, keywords_size * sizeof(int)); cudaMalloc((void**)&d_line_pos, MAX_LINES * sizeof(int)); cudaMalloc((void**)&d_goto_function, MAXS*MAXC * sizeof(int)); cudaMalloc((void**)&d_failure_function, MAXS * sizeof(int)); cudaMalloc((void**)&d_output, MAXS * sizeof(int)); cudaMalloc((void**)&d_text, MAX_FILE_SIZE * sizeof(char)); cudaMemcpy(d_key_sizes, key_sizes, keywords_size * sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_line_pos, line_pos, num_lines * sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_failure_function, failure_function, total_states * sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_output, output, MAXS * sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_text, full_text, strlen(full_text) * sizeof(char), cudaMemcpyHostToDevice); for(int i = 0; i < MAXS; i++) cudaMemcpy(&d_goto_function[i*MAXC], goto_function[i], MAXC * sizeof(char), cudaMemcpyHostToDevice); cout << "\nSearching occurrences of keywords in the text...\n"; //search_pattern(locations, keywords_size, key_sizes, text, 'a'); search_pattern<<<grid, block>>>(d_locations, keywords_size, d_key_sizes, d_text, num_lines, d_line_pos, d_goto_function, d_failure_function, d_output, '!'); cudaMemcpy(locations, d_locations, MAX_LOCATIONS * sizeof(location_t), cudaMemcpyDeviceToHost); /*int line = 1; if(locations->size == 0){ line++; continue; } cout << "\nOccurrences in line " << line++ << endl;*/ cout << "\nOccurrences:\n"; for(int i = 0; i < 10 && locations[i].keyword < keywords_size; i++){ cout << "Keyword " << keywords[locations[i].keyword] << " appears from " << locations[i].begin << " to " << locations[i].end << endl; } for(int i = 0; i < keywords_size; i++) free(keywords[i]); free(keywords); free(locations); return(0); }
0971393561f604992b806d42ce37bb0a0c3f7f75.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* * NaiveMatrixMuliplyCuda.cuh * * Created on: Feb 22, 2020 * Author: neville */ #ifndef NAIVEMATRIXMULIPLYCUDA_CUH_ #define NAIVEMATRIXMULIPLYCUDA_CUH_ #include "Matrix.h" #include <stdio.h> /** * CUDA Kernel * Calculates C = A*B * All Matrices are Row-Major Column * Uses only global memory * * * @param A Matrix A * @param B Matrix B * @param C Matrix C * @param init Value used to accumulate the result */ template<typename T, typename E> __global__ void CUDAMatrixMultiply2DBoundChecking_Kernel(const T A, const T B, T C, E init) { int row = blockIdx.y * blockDim.y + threadIdx.y; int col = blockIdx.x * blockDim.x + threadIdx.x; if (row < C.rows && col < C.cols) { auto acc = init; for (int i = 0; i < A.cols; i++) { auto a = A.elems[row * A.cols + i]; auto b = B.elems[i * B.cols + col]; acc += a * b; } C.elems[row * C.cols + col] = acc; } } /** * CUDA Kernel * Calculates C = A*B * All Matrices are Row-Major Column * Uses only global memory, but allocates more memory that is really needed for C, but in return no boundchecking * * * @param A Matrix A * @param B Matrix B * @param C Matrix C * @param init Value used to accumulate the result */ template<class T, typename E> __global__ void CUDAMatrixMultiply2DNonBoundChecking_Kernel(const T A, const T B, T C, E init) { int row = blockIdx.y * blockDim.y + threadIdx.y; int col = blockIdx.x * blockDim.x + threadIdx.x; auto acc = init; for (int i = 0; i < A.cols; i++) { auto a = A.elems[row * A.cols + i]; auto b = B.elems[i * B.cols + col]; acc += a * b; C.elems[row * C.cols + col] = acc; } } #endif /* NAIVEMATRIXMULIPLYCUDA_CUH_ */
0971393561f604992b806d42ce37bb0a0c3f7f75.cu
/* * NaiveMatrixMuliplyCuda.cuh * * Created on: Feb 22, 2020 * Author: neville */ #ifndef NAIVEMATRIXMULIPLYCUDA_CUH_ #define NAIVEMATRIXMULIPLYCUDA_CUH_ #include "Matrix.h" #include <stdio.h> /** * CUDA Kernel * Calculates C = A*B * All Matrices are Row-Major Column * Uses only global memory * * * @param A Matrix A * @param B Matrix B * @param C Matrix C * @param init Value used to accumulate the result */ template<typename T, typename E> __global__ void CUDAMatrixMultiply2DBoundChecking_Kernel(const T A, const T B, T C, E init) { int row = blockIdx.y * blockDim.y + threadIdx.y; int col = blockIdx.x * blockDim.x + threadIdx.x; if (row < C.rows && col < C.cols) { auto acc = init; for (int i = 0; i < A.cols; i++) { auto a = A.elems[row * A.cols + i]; auto b = B.elems[i * B.cols + col]; acc += a * b; } C.elems[row * C.cols + col] = acc; } } /** * CUDA Kernel * Calculates C = A*B * All Matrices are Row-Major Column * Uses only global memory, but allocates more memory that is really needed for C, but in return no boundchecking * * * @param A Matrix A * @param B Matrix B * @param C Matrix C * @param init Value used to accumulate the result */ template<class T, typename E> __global__ void CUDAMatrixMultiply2DNonBoundChecking_Kernel(const T A, const T B, T C, E init) { int row = blockIdx.y * blockDim.y + threadIdx.y; int col = blockIdx.x * blockDim.x + threadIdx.x; auto acc = init; for (int i = 0; i < A.cols; i++) { auto a = A.elems[row * A.cols + i]; auto b = B.elems[i * B.cols + col]; acc += a * b; C.elems[row * C.cols + col] = acc; } } #endif /* NAIVEMATRIXMULIPLYCUDA_CUH_ */
6477d6bac6495b72015c348bcaad031bcbc5d2b8.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <stdio.h> #define N 64 #define TPB 32 // A scaling function to convert integers 0,1,...,N-1 to evenly spaced floats __device__ float scale(int i, int n) { return ((float)i) / (n - 1); } // Compute the distance between 2 points on a line. __device__ float distance(float x1, float x2) { return sqrt((x2 - x1) * (x2 - x1)); } __global__ void distanceKernel(float* d_out, float ref, int len) { const int i = blockIdx.x * blockDim.x + threadIdx.x; const float x = scale(i, len); d_out[i] = distance(x, ref); printf("i = %2d: dist from %f to %f is %f.\n", i, ref, x, d_out[i]); } int main() { // Choose a reference value from which distances are measured. const float ref = 0.5; // Declare a pointer for an array of floats float* d_out = 0; // Allocate device memory for d_out hipMalloc(&d_out, N * sizeof(float)); // Launch kernel to compute, NOTE: it is advicable to replace N/TPB with // (N+TPB-1)/TPB to make sure the number of blocks needed is rounded up. hipLaunchKernelGGL(( distanceKernel), dim3(N/TPB), dim3(TPB), 0, 0, d_out, ref, N); // Wait for device to finish before exiting hipDeviceSynchronize(); // Free the memory (Don't forget!!) hipFree(d_out); return 0; }
6477d6bac6495b72015c348bcaad031bcbc5d2b8.cu
#include <stdio.h> #define N 64 #define TPB 32 // A scaling function to convert integers 0,1,...,N-1 to evenly spaced floats __device__ float scale(int i, int n) { return ((float)i) / (n - 1); } // Compute the distance between 2 points on a line. __device__ float distance(float x1, float x2) { return sqrt((x2 - x1) * (x2 - x1)); } __global__ void distanceKernel(float* d_out, float ref, int len) { const int i = blockIdx.x * blockDim.x + threadIdx.x; const float x = scale(i, len); d_out[i] = distance(x, ref); printf("i = %2d: dist from %f to %f is %f.\n", i, ref, x, d_out[i]); } int main() { // Choose a reference value from which distances are measured. const float ref = 0.5; // Declare a pointer for an array of floats float* d_out = 0; // Allocate device memory for d_out cudaMalloc(&d_out, N * sizeof(float)); // Launch kernel to compute, NOTE: it is advicable to replace N/TPB with // (N+TPB-1)/TPB to make sure the number of blocks needed is rounded up. distanceKernel<<<N/TPB, TPB>>>(d_out, ref, N); // Wait for device to finish before exiting cudaDeviceSynchronize(); // Free the memory (Don't forget!!) cudaFree(d_out); return 0; }
71e01e4bbf0268fc18e4c55ff2a358ec042751c7.hip
// !!! This is a file automatically generated by hipify!!! #include <stdio.h> #include <hip/hip_runtime.h> #include "rocblas.h" #include <hip/hip_runtime.h> //It works on Tesla 2050; void GPU_present() { int deviceCount =0; hipGetDeviceCount(&deviceCount); if (deviceCount ==0) { printf("There is no device supporting Cuda.\n"); } else { hipDeviceReset(); } } __global__ void Times(double *P, double *A, double *B, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]=A[idx]*B[idx]; // P = A*B; } __global__ void add(double *P, double *A, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]+=A[idx]; // P = P+B; } __global__ void AddTimes(double *P, double *A, double *B, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]+=A[idx]*B[idx]; // P += A*B; } __global__ void AddTimesF(double *P, double *A, double *B, double F, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]+=F*A[idx]*B[idx]; // P+=(A*B)*F } __global__ void TimesF(double *P, double *A, double *B, double F, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]=F*A[idx]*B[idx]; // P=(A*B)*F } __global__ void TimesC(double *P,double *A, double C, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]=A[idx]*C; // P = A*C } __global__ void Div(double *P,double *A, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) { if (A[idx]>0) { P[idx]/=A[idx];} else {P[idx]=0.0;} } } __global__ void Norm(double *P, double C, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx] *= C; } __global__ void zero(double *P, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx] = 0.0; } __global__ void Cp (double *P, double *A, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx] = A[idx]; } __global__ void SetBx(double *P,int mmx,int My,int Mz, int bx1, int bxm, int jx, int jy){ int idx; int jx_mmx=jx*mmx; int jx_bxm=jx*bxm; int bx1_jx=bx1*jx; int yi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (yi<My && zi<Mz) { idx=jy*yi+zi; P[idx]=P[bx1_jx+idx]; P[jx_mmx+idx]=P[jx_bxm+idx]; } } __global__ void SetBy(double *P,int Mx,int mmy,int Mz, int by1, int bym, int jx, int jy){ int idx; int jy_mmy=jy*mmy; int jy_bym=jy*bym; int jy_by1=jy*by1; int xi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && zi<Mz) { idx=jx*xi+zi; P[idx]=P[jy_by1+idx]; P[jy_mmy+idx]=P[jy_bym+idx]; } } __global__ void SetBz(double *P,int Mx,int My,int mmz, int bz1, int bzm, int jx, int jy){ int idx; int xi =blockIdx.x*blockDim.x+threadIdx.x; int yi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && yi<My) { idx=jx*xi+jy*yi; P[idx]=P[idx+bz1]; P[idx+mmz]=P[idx+bzm]; } } __global__ void SwitchBx(double *P,double *Q,int mmx,int My,int Mz, int bx1, int bxm, int jx, int jy){ int idx; int jx_mmx=jx*mmx; int jx_bxm=jx*bxm; int bx1_jx=bx1*jx; int yi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (yi<My && zi<Mz) { idx=jy*yi+zi; P[idx]=Q[bx1_jx+idx]; P[jx_mmx+idx]=Q[jx_bxm+idx]; Q[idx]=P[bx1_jx+idx]; Q[jx_mmx+idx]=P[jx_bxm+idx]; } } __global__ void SwitchBy(double *P,double *Q,int Mx,int mmy,int Mz, int by1, int bym, int jx, int jy){ int idx; int jy_mmy=jy*mmy; int jy_bym=jy*bym; int jy_by1=jy*by1; int xi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && zi<Mz) { idx=jx*xi+zi; P[idx]=Q[jy_by1+idx]; P[jy_mmy+idx]=Q[jy_bym+idx]; Q[idx]=P[jy_by1+idx]; Q[jy_mmy+idx]=P[jy_bym+idx]; } } __global__ void SwitchBz(double *P,double *Q,int Mx,int My,int mmz, int bz1, int bzm, int jx, int jy){ int idx; int xi =blockIdx.x*blockDim.x+threadIdx.x; int yi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && yi<My) { idx=jx*xi+jy*yi; P[idx]=Q[idx+bz1]; P[idx+mmz]=Q[idx+bzm]; Q[idx]=P[idx+bz1]; Q[idx+mmz]=P[idx+bzm]; } } double * AllocateMemoryOnDevice(int N) { double *X; if (hipSuccess != hipMalloc((void **) &X, sizeof(double)*N)) printf("Memory allocation on GPU failed.\n Please reduce size of system and/or chain length\n"); return X; } void TransferDataToDevice(int M, double *H, double *D ) { hipMemcpy(D, H, sizeof(double)*M,hipMemcpyHostToDevice); } void InitializeForward(int s, int M, int Seg, double *Dgi, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Cp) , dim3(n_blocks),dim3(block_size) , 0, 0, Dgi+(s-1)*M,Dg+(Seg-1)*M,M); } void InitializeForward(int s, int M, int Seg, int Mem, double *Dgi, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size == 0 ? 0:1); for (int k=0; k<Mem; k++)hipLaunchKernelGGL(( Cp) , dim3(n_blocks),dim3(block_size) , 0, 0, Dgi+((s-1)*Mem+k)*M,Dg+(Seg-1)*M,M); } void InitializeBackward(int M, int Seg, double *Dgi_inv, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size ==0 ? 0:1); hipLaunchKernelGGL(( Cp) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv,Dg+(Seg-1)*M,M); } void InitializeBackward(int M, int Seg, int Mem, double *Dgi_inv, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size ==0 ? 0:1); for (int k=0; k<Mem; k++)hipLaunchKernelGGL(( Cp) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv+k*M,Dg+(Seg-1)*M,M); } void Composition(int s, int M, int Seg, double *Dgi_inv, double *Dgi, double *Dphi){ int block_size=256; int n_blocks=M/block_size + (M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dphi+(Seg-1)*M,Dgi_inv ,Dgi+(s-1)*M,M); } void Composition(int s, int M, int Seg, int Mem, double *Dgi_inv, double *Dgi, double *Dphi){ int block_size=256; int n_blocks=M/block_size + (M%block_size == 0 ? 0:1); for (int k=0; k<Mem; k++)hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dphi+(Seg-1)*M,Dgi_inv+k*M,Dgi+((s-1)*Mem+k)*M,M); } void CorrectDoubleCounting(int M,int Seg,double *Dphi,double *Dg){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Div) , dim3(n_blocks),dim3(block_size), 0, 0, Dphi +(Seg-1)*M,Dg +(Seg-1)*M,M); } void CorrectDoubleCounting(int M,int s,int Seg,double *Dphi,double *Dg){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Div) , dim3(n_blocks),dim3(block_size), 0, 0, Dphi +(s-1)*M,Dg +(Seg-1)*M,M); } void NormPhi(int M,int Seg,double* Dphi,double C){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, Dphi+(Seg-1)*M,C,M); } void Zero(int M,double* DX){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( zero), dim3(n_blocks),dim3(block_size), 0, 0, DX,M); } void Add(int s, int M, int Seg, double* DX, double* Dphi){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( add), dim3(n_blocks),dim3(block_size), 0, 0, Dphi+(Seg-1)*M,DX +(s-1)*M,M); } void TransferDataToHost(int M, double *H, double *D) { hipMemcpy(H, D, sizeof(double)*M,hipMemcpyDeviceToHost); } void SetBoundaries(double *P,int Mx,int My,int Mz,int bx1,int bxm,int by1,int bym,int bz1,int bzm) { int jx =(My+2)*(Mz+2); int jy = Mz+2; dim3 dimBlock(16,16); dim3 dimGridz((Mx+2+dimBlock.x-1)/dimBlock.x,(My+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridy((Mx+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridx((My+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); hipLaunchKernelGGL(( SetBx), dim3(dimGridx),dim3(dimBlock), 0, 0, P,Mx+1,My+2,Mz+2,bx1,bxm,jx,jy); hipLaunchKernelGGL(( SetBy), dim3(dimGridy),dim3(dimBlock), 0, 0, P,Mx+2,My+1,Mz+2,by1,bym,jx,jy); hipLaunchKernelGGL(( SetBz), dim3(dimGridz),dim3(dimBlock), 0, 0, P,Mx+2,My+2,Mz+1,bz1,bzm,jx,jy); } void SwitchBounds(double *P,double *Q,int Mx,int My,int Mz,int bx1,int bxm,int by1,int bym,int bz1,int bzm) { int jx =(My+2)*(Mz+2); int jy = Mz+2; dim3 dimBlock(16,16); dim3 dimGridz((Mx+2+dimBlock.x-1)/dimBlock.x,(My+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridy((Mx+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridx((My+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); hipLaunchKernelGGL(( SwitchBx), dim3(dimGridx),dim3(dimBlock), 0, 0, P,Q,Mx+1,My+2,Mz+2,bx1,bxm,jx,jy); hipLaunchKernelGGL(( SwitchBy), dim3(dimGridy),dim3(dimBlock), 0, 0, P,Q,Mx+2,My+1,Mz+2,by1,bym,jx,jy); hipLaunchKernelGGL(( SwitchBz), dim3(dimGridz),dim3(dimBlock), 0, 0, P,Q,Mx+2,My+2,Mz+1,bz1,bzm,jx,jy); } double Norm2(int M, int s, double *Da) { int s1=(s-1)*M; double *da = &Da[s1]; hipblasHandle_t handle; hipblasCreate(&handle); double result[1]; hipblasDnrm2(handle,M,da,1,result); hipblasDestroy(handle); return result[0]; } void Forward(int s, int M, int Seg, double *Dgi, double *Dg, double S, double f, int *Info) { //second order.... double f1=exp(f); double f_1=exp(-f); //double fnorm = 1.0/(4.0 + f1 + f_1); //force not yet implemented int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = Mz+2; int jz = 1; int s2=(s-2)*M*6; int s1=(s-1)*M*6; int sm=(Seg-1)*M; int block_size=256; int n_blocks; double *gs0 = &Dgi[s1]; //[1][s]; double *gs1 = &Dgi[M+s1]; //[1+M][s]; double *gs2 = &Dgi[2*M+s1]; //[1+2*M][s]; double *gs3 = &Dgi[3*M+s1]; //[1+3*M][s]; double *gs4 = &Dgi[4*M+s1]; //[1+4*M][s]; double *gs5 = &Dgi[5*M+s1]; //[1+5*M][s]; double *gz0 = &Dgi[s2]; //[1][s-1]; double *gz1 = &Dgi[M+s2]; //[1+M][s-1]; double *gz2 = &Dgi[2*M+s2]; //[1+2*M][s-1]; double *gz3 = &Dgi[3*M+s2]; //[1+3*M][s-1]; double *gz4 = &Dgi[4*M+s2]; //[1+4*M][s-1]; double *gz5 = &Dgi[5*M+s2]; //[1+5*M][s-1]; SetBoundaries(gz0,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz1,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[5]<3) SwitchBounds(gz0,gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[7]<3) SwitchBounds(gz1,gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[9]<3) SwitchBounds(gz2,gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); switch (Info[1]) { case 1: if (S<0) { n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz0,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz1,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz2,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz3,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz4,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz1+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz2+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz3+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz4+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz5+jx,Dg+sm,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz0,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz1,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz2,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz3,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz5,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz0+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz2+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz3+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz4+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz5+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz0,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz1,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz2,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz4,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz5,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz0+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz1+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz3+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz4+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz5+jz,Dg+sm,M-jz); n_blocks=6*M/block_size + ((6*M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, gs0,1.0/6.0,6*M); } else { double L=(1.0-S)/4.0; double SoverL=S/L; n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( TimesF), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz0,Dg+sm+jx,SoverL,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz1,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz2,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz3,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs0+jx,gz4,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz1+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz2+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz3+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz4+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gs5,gz5+jx,Dg+sm,SoverL,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz0,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz1,Dg+sm+jy,SoverL,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz2,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz3,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs1+jy,gz5,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz0+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz2+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz3+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz4+jy,Dg+sm,SoverL,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs4,gz5+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz0,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz1,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz2,Dg+sm+jz,SoverL,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz4,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs2+jz,gz5,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz0+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz1+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz3+jz,Dg+sm,SoverL,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz4+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gs3,gz5+jz,Dg+sm,M-jz); n_blocks=(6*M)/block_size + ((6*M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, gs0,L,6*M); } break; default: printf("Unknown LatticeType in GPU module, 2nd order.\n"); break; } } void Forward(int s, int M, int Seg, double *Dgi, double *Dg, double f, int LB, int UB) { //LB = info 5 , UP = info 6 double fnorm = 1.0/6.0; int jz = 1; int s2=(s-2)*M; int s1=(s-1)*M; int sm=(Seg-1)*M; //SetBoundaries(Dgi+s2,M,LB,UB); int block_size=256; int n_blocks; n_blocks=M/block_size + (M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( SetBz), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s2,0,0,M, LB, UB, 0, 0); n_blocks=(M-1)/block_size + ((M-1)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz,Dgi+s2, Dg+sm+jz, M-jz); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz,Dg+sm, M-jz); n_blocks=M/block_size + (M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2,Dg+sm, 4.0, M); hipLaunchKernelGGL(( Norm) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,fnorm,M); } void Forward(int s, int M, int Seg, double *Dgi, double *Dg, double f, int *Info) { if (Info[0]==1) {Forward(s, M, Seg, Dgi, Dg, f, Info[5], Info[6]); } else { double f1=exp(f); double f_1=exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = Mz+2; int jz = 1; int s2=(s-2)*M; int s1=(s-1)*M; int sm=(Seg-1)*M; SetBoundaries(Dgi+s2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); int block_size=256; int n_blocks; switch (Info[1]) { case 1: //Simple cubic n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx,Dgi+s2, Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1 ,Dgi+s2+jx,Dg+sm ,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy,Dgi+s2, Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy,Dg+sm ,M-jy); n_blocks=(M-1)/block_size + ((M-1)%block_size == 0 ? 0:1); if (f !=0) { hipLaunchKernelGGL(( AddTimesF) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz,Dgi+s2, Dg+sm+jz,f1, M-jz); hipLaunchKernelGGL(( AddTimesF) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz,Dg+sm, f_1,M-jz); } else { hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz,Dgi+s2, Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz,Dg+sm, M-jz); } n_blocks=M/block_size + (M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,fnorm,M); break; case 2: //FCC n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx+jy,Dgi+s2, Dg+sm+jx+jy,M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx+jy, Dg+sm, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jx-jy); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); if (f !=0) { hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,f1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy+jz, Dg+sm, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1,M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,f1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx+jz, Dg, f_1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, f1,M-jx-jz); } else { hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy+jz, Dg+sm, M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx+jz, Dg+sm, M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, M-jx-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,1/12.0,M); break; case 3: //HEX n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2, Dg+sm+jx, M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx, Dg+sm, M-jx); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jy-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy, Dg+sm, M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2, Dg+sm+jy , M-jy); if (f!=0) { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1 ,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } else { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1, M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,1/12.0,M); break; default: printf("Unknown LatticeType in GPU module.\n"); break; }} //extra bracket to escape from the call to first order matrix forward; } void Backward(int M, int Seg, double *Dgi_inv, double *Dg, double *Dgx, double f, int LB, int UB) { double fnorm = 1.0/(6.0); int jz = 1; int sm=(Seg-1)*M; //SetBoundaries(Dgi_inv,M,LB,UB); int block_size=256; int n_blocks; n_blocks=M/block_size + (M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( SetBz), dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv,0,0,M, LB, UB, 0, 0); n_blocks=(M-1)/block_size+((M-1)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz,Dgi_inv, Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jz,Dg+sm, M-jz); n_blocks=M/block_size+(M%block_size ==0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv,Dg+sm, 4.0, M); hipLaunchKernelGGL(( TimesC), dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv,Dgx,fnorm,M); } void Backward(int M, int Seg, double *Dgi_inv, double *Dg, double *Dgx, double f, int *Info) { if (Info[0]==1) {Backward(M, Seg, Dgi_inv, Dg, Dgx, f, Info[5], Info[6]);} else { const double f1 = exp(f); const double f_1 = exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = (Mz+2); int jz = 1; int sm=(Seg-1)*M; SetBoundaries(Dgi_inv,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); int block_size=256; int n_blocks; switch (Info[1]){ case 1: //simple cubic n_blocks=(M-jx)/block_size+ ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times) , dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx,Dgi_inv, Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jx,Dg+sm, M-jx); n_blocks=(M-jy)/block_size+ ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy,Dgi_inv, Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jy,Dg+sm, M-jy); n_blocks=(M-1)/block_size+((M-1)%block_size == 0 ? 0:1); if (f !=0) { hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz,Dgi_inv, Dg+sm+jz,f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jz,Dg+sm, f_1,M-jz); } else { hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz,Dgi_inv, Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jz,Dg+sm, M-jz); } n_blocks=M/block_size+(M%block_size ==0 ? 0:1); hipLaunchKernelGGL(( TimesC), dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv,Dgx,fnorm,M); break; case 2: //FCC n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx+jy,Dgi_inv, Dg+sm+jx+jy,M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jx+jy, Dg+sm, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv+jy, Dg+sm+jx, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv+jx, Dg+sm+jy, M-jx-jy); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); if (f !=0) { hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy+jz,Dgi_inv, Dg+sm+jy+jz,f1, M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jy+jz, Dg+sm, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx+jz,Dgi_inv, Dg+sm+jx+jz,f1, M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jx+jz, Dg+sm, f_1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv+jz, Dg+sm+jx, f_1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jx ,Dg+sm+jz, f1, M-jx-jz); } else { hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy+jz,Dgi_inv, Dg+sm+jy+jz,M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jy+jz, Dg+sm, M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv+jz, Dg+sm+jy, M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jy, Dg+sm+jz, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx+jz,Dgi_inv, Dg+sm+jx+jz,M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jx+jz, Dg+sm, M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv+jz, Dg+sm+jx, M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jx ,Dg+sm+jz, M-jx-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( TimesC), dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv,Dgx,1/12.0,M); break; case 3: //HEX n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv, Dg+sm+jx, M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jx, Dg+sm, M-jx); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv+jy, Dg+sm+jx, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv+jx, Dg+sm+jy, M-jy-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jy, Dg+sm, M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv, Dg+sm+jy, M-jy); if (f!=0) { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jx, Dg+sm+jz, f1 ,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv, Dg+sm+jz, f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jz, Dg+sm, f_1,M-jz); } else { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jx, Dg+sm+jz, f1, M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jx, Dgi_inv+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jy, Dgi_inv+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx+jz, Dgi_inv, Dg+sm+jz, f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgx, Dgi_inv+jz, Dg+sm, f_1,M-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( TimesC), dim3(n_blocks),dim3(block_size), 0, 0, Dgi_inv,Dgx,1/12.0,M); break; default: printf("Unknown LatticeType in GPU module.\n"); break; }} } void Backward(int M, int Seg, double *Dgi_inv, double *Dg, double *Dgx, double S, double f, int *Info) { //second order. const double f1 = exp(f); const double f_1 = exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = (Mz+2); int jz = 1; int sm=(Seg-1)*M; double *gz0 = &Dgi_inv[0]; double *gz1 = &Dgi_inv[M]; double *gz2 = &Dgi_inv[2*M]; double *gz3 = &Dgi_inv[3*M]; double *gz4 = &Dgi_inv[4*M]; double *gz5 = &Dgi_inv[5*M]; SetBoundaries(gz0,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz1,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[5]<3) SwitchBounds(gz0,gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[7]<3) SwitchBounds(gz1,gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[9]<3) SwitchBounds(gz2,gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); double *gx0 = &Dgx[0]; double *gx1 = &Dgx[M]; double *gx2 = &Dgx[2*M]; double *gx3 = &Dgx[3*M]; double *gx4 = &Dgx[4*M]; double *gx5 = &Dgx[5*M]; int block_size=256; int n_blocks; switch (Info[1]) { case 1: if (S<0){ n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx0+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx1+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx2+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx3+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx4+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx5+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5,gz0+jx,Dg+sm,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5,gz1+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5,gz2+jz,Dg+sm,M-jz); n_blocks=(6*M)/block_size + ((6*M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( TimesC), dim3(n_blocks),dim3(block_size), 0, 0, gz0,gx0,1.0/6.0,6*M); } else { double L=(1.0-S)/4.0; double SoverL=S/L; n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx1+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx2+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx3+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, gx4+jx,gz5,Dg+sm+jx,M-jx); hipLaunchKernelGGL(( TimesF), dim3(n_blocks),dim3(block_size), 0, 0, gx5+jx,gz5,Dg+sm+jx,SoverL,M-jx); hipLaunchKernelGGL(( TimesF), dim3(n_blocks),dim3(block_size), 0, 0, gx0,gz0+jx,Dg+sm,SoverL,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3,gz0+jx,Dg+sm,M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4,gz0+jx,Dg+sm,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gx4+jy,gz4,Dg+sm+jy,SoverL,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5+jy,gz4,Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gx1,gz1+jy,Dg+sm,SoverL,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx2,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx3,gz1+jy,Dg+sm,M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5,gz1+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gx3+jz,gz3,Dg+sm+jz,SoverL,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5+jz,gz3,Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx0,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx1,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, gx2,gz2+jz,Dg+sm,SoverL,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx4,gz2+jz,Dg+sm,M-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, gx5,gz2+jz,Dg+sm,M-jz); n_blocks=(6*M)/block_size + ((6*M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( TimesC), dim3(n_blocks),dim3(block_size), 0, 0, gz0,gx0,L,6*M); } break; default: printf("Unknown LatticeType in GPU module.\n"); break; } } void Propagate(int s_from, int s_to, int M, int Seg, double *Dgi, double *Dg, double f, int *Info) { double f1=exp(f); double f_1=exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = Mz+2; int jz = 1; int s2=(s_from-1)*M; int s1=(s_to-1)*M; int sm=(Seg-1)*M; SetBoundaries(Dgi+s2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); int block_size=256; int n_blocks; switch (Info[1]) { case 1: //Simple cubic n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx,Dgi+s2, Dg+sm+jx,M-jx); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1 ,Dgi+s2+jx,Dg+sm ,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy,Dgi+s2, Dg+sm+jy,M-jy); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy,Dg+sm ,M-jy); n_blocks=(M-1)/block_size + ((M-1)%block_size == 0 ? 0:1); if (f !=0) { hipLaunchKernelGGL(( AddTimesF) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz,Dgi+s2, Dg+sm+jz,f1, M-jz); hipLaunchKernelGGL(( AddTimesF) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz,Dg+sm, f_1,M-jz); } else { hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz,Dgi+s2, Dg+sm+jz,M-jz); hipLaunchKernelGGL(( AddTimes) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz,Dg+sm, M-jz); } n_blocks=M/block_size + (M%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm) , dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,fnorm,M); break; case 2: //FCC n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx+jy,Dgi+s2, Dg+sm+jx+jy,M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx+jy, Dg+sm, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jx-jy); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); if (f !=0) { hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,f1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy+jz, Dg+sm, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1,M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,f1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx+jz, Dg, f_1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, f1,M-jx-jz); } else { hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy+jz, Dg+sm, M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, M-jy-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx+jz, Dg+sm, M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, M-jx-jz); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, M-jx-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,1/12.0,M); break; case 3: //HEX n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Times), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2, Dg+sm+jx, M-jx); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jx, Dg+sm, M-jx); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jy-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jy, Dg+sm, M-jy); hipLaunchKernelGGL(( AddTimes), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2, Dg+sm+jy , M-jy); if (f!=0) { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1 ,M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } else { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1, M-jx-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); hipLaunchKernelGGL(( AddTimesF), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); hipLaunchKernelGGL(( Norm), dim3(n_blocks),dim3(block_size), 0, 0, Dgi+s1,1/12.0,M); break; default: printf("Unknown LatticeType in GPU module.\n"); break; } } void FreeMemoryOnDevice(double *D) { hipFree(D); } // Info[0]=3; //Dimensions // Info[1]=3; //3=HEX, 2=FCC, 1=simple cubic; // Info[2]=n_layers_x; // Info[3]=n_layers_y; // Info[4]=n_layers_z; //assumption that compranges =1; // Info[5]=Get_BL(1); //x lower bound // Info[6]=Get_BL(2); //x upper bound // Info[7]=Get_BL(3); //y lower bound // Info[8]=Get_BL(4); //y upper bound // Info[9]=Get_BL(5); //z lower bound // Info[10]=Get_BL(6);//z upper bound
71e01e4bbf0268fc18e4c55ff2a358ec042751c7.cu
#include <stdio.h> #include <cuda.h> #include "cublas_v2.h" #include <cuda_runtime.h> //It works on Tesla 2050; void GPU_present() { int deviceCount =0; cuDeviceGetCount(&deviceCount); if (deviceCount ==0) { printf("There is no device supporting Cuda.\n"); } else { cudaDeviceReset(); } } __global__ void Times(double *P, double *A, double *B, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]=A[idx]*B[idx]; // P = A*B; } __global__ void add(double *P, double *A, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]+=A[idx]; // P = P+B; } __global__ void AddTimes(double *P, double *A, double *B, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]+=A[idx]*B[idx]; // P += A*B; } __global__ void AddTimesF(double *P, double *A, double *B, double F, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]+=F*A[idx]*B[idx]; // P+=(A*B)*F } __global__ void TimesF(double *P, double *A, double *B, double F, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]=F*A[idx]*B[idx]; // P=(A*B)*F } __global__ void TimesC(double *P,double *A, double C, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx]=A[idx]*C; // P = A*C } __global__ void Div(double *P,double *A, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) { if (A[idx]>0) { P[idx]/=A[idx];} else {P[idx]=0.0;} } } __global__ void Norm(double *P, double C, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx] *= C; } __global__ void zero(double *P, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx] = 0.0; } __global__ void Cp (double *P, double *A, int M){ int idx = blockIdx.x*blockDim.x+threadIdx.x; if (idx<M) P[idx] = A[idx]; } __global__ void SetBx(double *P,int mmx,int My,int Mz, int bx1, int bxm, int jx, int jy){ int idx; int jx_mmx=jx*mmx; int jx_bxm=jx*bxm; int bx1_jx=bx1*jx; int yi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (yi<My && zi<Mz) { idx=jy*yi+zi; P[idx]=P[bx1_jx+idx]; P[jx_mmx+idx]=P[jx_bxm+idx]; } } __global__ void SetBy(double *P,int Mx,int mmy,int Mz, int by1, int bym, int jx, int jy){ int idx; int jy_mmy=jy*mmy; int jy_bym=jy*bym; int jy_by1=jy*by1; int xi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && zi<Mz) { idx=jx*xi+zi; P[idx]=P[jy_by1+idx]; P[jy_mmy+idx]=P[jy_bym+idx]; } } __global__ void SetBz(double *P,int Mx,int My,int mmz, int bz1, int bzm, int jx, int jy){ int idx; int xi =blockIdx.x*blockDim.x+threadIdx.x; int yi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && yi<My) { idx=jx*xi+jy*yi; P[idx]=P[idx+bz1]; P[idx+mmz]=P[idx+bzm]; } } __global__ void SwitchBx(double *P,double *Q,int mmx,int My,int Mz, int bx1, int bxm, int jx, int jy){ int idx; int jx_mmx=jx*mmx; int jx_bxm=jx*bxm; int bx1_jx=bx1*jx; int yi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (yi<My && zi<Mz) { idx=jy*yi+zi; P[idx]=Q[bx1_jx+idx]; P[jx_mmx+idx]=Q[jx_bxm+idx]; Q[idx]=P[bx1_jx+idx]; Q[jx_mmx+idx]=P[jx_bxm+idx]; } } __global__ void SwitchBy(double *P,double *Q,int Mx,int mmy,int Mz, int by1, int bym, int jx, int jy){ int idx; int jy_mmy=jy*mmy; int jy_bym=jy*bym; int jy_by1=jy*by1; int xi =blockIdx.x*blockDim.x+threadIdx.x; int zi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && zi<Mz) { idx=jx*xi+zi; P[idx]=Q[jy_by1+idx]; P[jy_mmy+idx]=Q[jy_bym+idx]; Q[idx]=P[jy_by1+idx]; Q[jy_mmy+idx]=P[jy_bym+idx]; } } __global__ void SwitchBz(double *P,double *Q,int Mx,int My,int mmz, int bz1, int bzm, int jx, int jy){ int idx; int xi =blockIdx.x*blockDim.x+threadIdx.x; int yi =blockIdx.y*blockDim.y+threadIdx.y; if (xi<Mx && yi<My) { idx=jx*xi+jy*yi; P[idx]=Q[idx+bz1]; P[idx+mmz]=Q[idx+bzm]; Q[idx]=P[idx+bz1]; Q[idx+mmz]=P[idx+bzm]; } } double * AllocateMemoryOnDevice(int N) { double *X; if (cudaSuccess != cudaMalloc((void **) &X, sizeof(double)*N)) printf("Memory allocation on GPU failed.\n Please reduce size of system and/or chain length\n"); return X; } void TransferDataToDevice(int M, double *H, double *D ) { cudaMemcpy(D, H, sizeof(double)*M,cudaMemcpyHostToDevice); } void InitializeForward(int s, int M, int Seg, double *Dgi, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size == 0 ? 0:1); Cp <<< n_blocks,block_size >>> (Dgi+(s-1)*M,Dg+(Seg-1)*M,M); } void InitializeForward(int s, int M, int Seg, int Mem, double *Dgi, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size == 0 ? 0:1); for (int k=0; k<Mem; k++) Cp <<< n_blocks,block_size >>> (Dgi+((s-1)*Mem+k)*M,Dg+(Seg-1)*M,M); } void InitializeBackward(int M, int Seg, double *Dgi_inv, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size ==0 ? 0:1); Cp <<<n_blocks,block_size>>> (Dgi_inv,Dg+(Seg-1)*M,M); } void InitializeBackward(int M, int Seg, int Mem, double *Dgi_inv, double *Dg) { int block_size=256; int n_blocks = M/block_size+(M%block_size ==0 ? 0:1); for (int k=0; k<Mem; k++) Cp <<<n_blocks,block_size>>> (Dgi_inv+k*M,Dg+(Seg-1)*M,M); } void Composition(int s, int M, int Seg, double *Dgi_inv, double *Dgi, double *Dphi){ int block_size=256; int n_blocks=M/block_size + (M%block_size == 0 ? 0:1); AddTimes <<<n_blocks,block_size>>> (Dphi+(Seg-1)*M,Dgi_inv ,Dgi+(s-1)*M,M); } void Composition(int s, int M, int Seg, int Mem, double *Dgi_inv, double *Dgi, double *Dphi){ int block_size=256; int n_blocks=M/block_size + (M%block_size == 0 ? 0:1); for (int k=0; k<Mem; k++) AddTimes <<<n_blocks,block_size>>> (Dphi+(Seg-1)*M,Dgi_inv+k*M,Dgi+((s-1)*Mem+k)*M,M); } void CorrectDoubleCounting(int M,int Seg,double *Dphi,double *Dg){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Div <<<n_blocks,block_size>>>(Dphi +(Seg-1)*M,Dg +(Seg-1)*M,M); } void CorrectDoubleCounting(int M,int s,int Seg,double *Dphi,double *Dg){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Div <<<n_blocks,block_size>>>(Dphi +(s-1)*M,Dg +(Seg-1)*M,M); } void NormPhi(int M,int Seg,double* Dphi,double C){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(Dphi+(Seg-1)*M,C,M); } void Zero(int M,double* DX){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); zero<<<n_blocks,block_size>>>(DX,M); } void Add(int s, int M, int Seg, double* DX, double* Dphi){ int block_size=256; int n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); add<<<n_blocks,block_size>>>(Dphi+(Seg-1)*M,DX +(s-1)*M,M); } void TransferDataToHost(int M, double *H, double *D) { cudaMemcpy(H, D, sizeof(double)*M,cudaMemcpyDeviceToHost); } void SetBoundaries(double *P,int Mx,int My,int Mz,int bx1,int bxm,int by1,int bym,int bz1,int bzm) { int jx =(My+2)*(Mz+2); int jy = Mz+2; dim3 dimBlock(16,16); dim3 dimGridz((Mx+2+dimBlock.x-1)/dimBlock.x,(My+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridy((Mx+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridx((My+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); SetBx<<<dimGridx,dimBlock>>>(P,Mx+1,My+2,Mz+2,bx1,bxm,jx,jy); SetBy<<<dimGridy,dimBlock>>>(P,Mx+2,My+1,Mz+2,by1,bym,jx,jy); SetBz<<<dimGridz,dimBlock>>>(P,Mx+2,My+2,Mz+1,bz1,bzm,jx,jy); } void SwitchBounds(double *P,double *Q,int Mx,int My,int Mz,int bx1,int bxm,int by1,int bym,int bz1,int bzm) { int jx =(My+2)*(Mz+2); int jy = Mz+2; dim3 dimBlock(16,16); dim3 dimGridz((Mx+2+dimBlock.x-1)/dimBlock.x,(My+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridy((Mx+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); dim3 dimGridx((My+2+dimBlock.x-1)/dimBlock.x,(Mz+2+dimBlock.y-1)/dimBlock.y); SwitchBx<<<dimGridx,dimBlock>>>(P,Q,Mx+1,My+2,Mz+2,bx1,bxm,jx,jy); SwitchBy<<<dimGridy,dimBlock>>>(P,Q,Mx+2,My+1,Mz+2,by1,bym,jx,jy); SwitchBz<<<dimGridz,dimBlock>>>(P,Q,Mx+2,My+2,Mz+1,bz1,bzm,jx,jy); } double Norm2(int M, int s, double *Da) { int s1=(s-1)*M; double *da = &Da[s1]; cublasHandle_t handle; cublasCreate(&handle); double result[1]; cublasDnrm2(handle,M,da,1,result); cublasDestroy(handle); return result[0]; } void Forward(int s, int M, int Seg, double *Dgi, double *Dg, double S, double f, int *Info) { //second order.... double f1=exp(f); double f_1=exp(-f); //double fnorm = 1.0/(4.0 + f1 + f_1); //force not yet implemented int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = Mz+2; int jz = 1; int s2=(s-2)*M*6; int s1=(s-1)*M*6; int sm=(Seg-1)*M; int block_size=256; int n_blocks; double *gs0 = &Dgi[s1]; //[1][s]; double *gs1 = &Dgi[M+s1]; //[1+M][s]; double *gs2 = &Dgi[2*M+s1]; //[1+2*M][s]; double *gs3 = &Dgi[3*M+s1]; //[1+3*M][s]; double *gs4 = &Dgi[4*M+s1]; //[1+4*M][s]; double *gs5 = &Dgi[5*M+s1]; //[1+5*M][s]; double *gz0 = &Dgi[s2]; //[1][s-1]; double *gz1 = &Dgi[M+s2]; //[1+M][s-1]; double *gz2 = &Dgi[2*M+s2]; //[1+2*M][s-1]; double *gz3 = &Dgi[3*M+s2]; //[1+3*M][s-1]; double *gz4 = &Dgi[4*M+s2]; //[1+4*M][s-1]; double *gz5 = &Dgi[5*M+s2]; //[1+5*M][s-1]; SetBoundaries(gz0,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz1,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[5]<3) SwitchBounds(gz0,gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[7]<3) SwitchBounds(gz1,gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[9]<3) SwitchBounds(gz2,gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); switch (Info[1]) { case 1: if (S<0) { n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gs0+jx,gz0,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz1,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz2,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz3,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz4,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gs5,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz1+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz2+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz3+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz4+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz5+jx,Dg+sm,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gs1+jy,gz0,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz1,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz2,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz3,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz5,Dg+sm+jy,M-jy); Times<<<n_blocks,block_size>>>(gs4,gz0+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz2+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz3+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz4+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz5+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gs2+jz,gz0,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz1,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz2,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz4,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz5,Dg+sm+jz,M-jz); Times<<<n_blocks,block_size>>>(gs3,gz0+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz1+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz3+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz4+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz5+jz,Dg+sm,M-jz); n_blocks=6*M/block_size + ((6*M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(gs0,1.0/6.0,6*M); } else { double L=(1.0-S)/4.0; double SoverL=S/L; n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); TimesF<<<n_blocks,block_size>>>(gs0+jx,gz0,Dg+sm+jx,SoverL,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz1,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz2,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz3,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gs0+jx,gz4,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gs5,gz1+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz2+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz3+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gs5,gz4+jx,Dg+sm,M-jx); AddTimesF<<<n_blocks,block_size>>>(gs5,gz5+jx,Dg+sm,SoverL,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gs1+jy,gz0,Dg+sm+jy,M-jy); AddTimesF<<<n_blocks,block_size>>>(gs1+jy,gz1,Dg+sm+jy,SoverL,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz2,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz3,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gs1+jy,gz5,Dg+sm+jy,M-jy); Times<<<n_blocks,block_size>>>(gs4,gz0+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz2+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz3+jy,Dg+sm,M-jy); AddTimesF<<<n_blocks,block_size>>>(gs4,gz4+jy,Dg+sm,SoverL,M-jy); AddTimes<<<n_blocks,block_size>>>(gs4,gz5+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gs2+jz,gz0,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz1,Dg+sm+jz,M-jz); AddTimesF<<<n_blocks,block_size>>>(gs2+jz,gz2,Dg+sm+jz,SoverL,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz4,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gs2+jz,gz5,Dg+sm+jz,M-jz); Times<<<n_blocks,block_size>>>(gs3,gz0+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz1+jz,Dg+sm,M-jz); AddTimesF<<<n_blocks,block_size>>>(gs3,gz3+jz,Dg+sm,SoverL,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz4+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gs3,gz5+jz,Dg+sm,M-jz); n_blocks=(6*M)/block_size + ((6*M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(gs0,L,6*M); } break; default: printf("Unknown LatticeType in GPU module, 2nd order.\n"); break; } } void Forward(int s, int M, int Seg, double *Dgi, double *Dg, double f, int LB, int UB) { //LB = info 5 , UP = info 6 double fnorm = 1.0/6.0; int jz = 1; int s2=(s-2)*M; int s1=(s-1)*M; int sm=(Seg-1)*M; //SetBoundaries(Dgi+s2,M,LB,UB); int block_size=256; int n_blocks; n_blocks=M/block_size + (M%block_size == 0 ? 0:1); SetBz<<<n_blocks,block_size>>> (Dgi+s2,0,0,M, LB, UB, 0, 0); n_blocks=(M-1)/block_size + ((M-1)%block_size == 0 ? 0:1); Times <<<n_blocks,block_size>>> (Dgi+s1+jz,Dgi+s2, Dg+sm+jz, M-jz); AddTimes <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jz,Dg+sm, M-jz); n_blocks=M/block_size + (M%block_size == 0 ? 0:1); AddTimesF <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2,Dg+sm, 4.0, M); Norm <<<n_blocks,block_size>>> (Dgi+s1,fnorm,M); } void Forward(int s, int M, int Seg, double *Dgi, double *Dg, double f, int *Info) { if (Info[0]==1) {Forward(s, M, Seg, Dgi, Dg, f, Info[5], Info[6]); } else { double f1=exp(f); double f_1=exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = Mz+2; int jz = 1; int s2=(s-2)*M; int s1=(s-1)*M; int sm=(Seg-1)*M; SetBoundaries(Dgi+s2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); int block_size=256; int n_blocks; switch (Info[1]) { case 1: //Simple cubic n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times <<<n_blocks,block_size>>> (Dgi+s1+jx,Dgi+s2, Dg+sm+jx,M-jx); AddTimes <<<n_blocks,block_size>>> (Dgi+s1 ,Dgi+s2+jx,Dg+sm ,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes <<<n_blocks,block_size>>> (Dgi+s1+jy,Dgi+s2, Dg+sm+jy,M-jy); AddTimes <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jy,Dg+sm ,M-jy); n_blocks=(M-1)/block_size + ((M-1)%block_size == 0 ? 0:1); if (f !=0) { AddTimesF <<<n_blocks,block_size>>> (Dgi+s1+jz,Dgi+s2, Dg+sm+jz,f1, M-jz); AddTimesF <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jz,Dg+sm, f_1,M-jz); } else { AddTimes <<<n_blocks,block_size>>> (Dgi+s1+jz,Dgi+s2, Dg+sm+jz,M-jz); AddTimes <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jz,Dg+sm, M-jz); } n_blocks=M/block_size + (M%block_size == 0 ? 0:1); Norm <<<n_blocks,block_size>>> (Dgi+s1,fnorm,M); break; case 2: //FCC n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>( Dgi+s1+jx+jy,Dgi+s2, Dg+sm+jx+jy,M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx+jy, Dg+sm, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jx-jy); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); if (f !=0) { AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,f1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jy+jz, Dg+sm, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1,M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,f1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx+jz, Dg, f_1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, f1,M-jx-jz); } else { AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jy+jz, Dg+sm, M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx+jz, Dg+sm, M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, M-jx-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(Dgi+s1,1/12.0,M); break; case 3: //HEX n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>( Dgi+s1+jx, Dgi+s2, Dg+sm+jx, M-jx); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx, Dg+sm, M-jx); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jy-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jy, Dg+sm, M-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2, Dg+sm+jy , M-jy); if (f!=0) { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1 ,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } else { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1, M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(Dgi+s1,1/12.0,M); break; default: printf("Unknown LatticeType in GPU module.\n"); break; }} //extra bracket to escape from the call to first order matrix forward; } void Backward(int M, int Seg, double *Dgi_inv, double *Dg, double *Dgx, double f, int LB, int UB) { double fnorm = 1.0/(6.0); int jz = 1; int sm=(Seg-1)*M; //SetBoundaries(Dgi_inv,M,LB,UB); int block_size=256; int n_blocks; n_blocks=M/block_size + (M%block_size == 0 ? 0:1); SetBz<<<n_blocks,block_size>>>(Dgi_inv,0,0,M, LB, UB, 0, 0); n_blocks=(M-1)/block_size+((M-1)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>> (Dgx+jz,Dgi_inv, Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>> (Dgx, Dgi_inv+jz,Dg+sm, M-jz); n_blocks=M/block_size+(M%block_size ==0 ? 0:1); AddTimesF<<<n_blocks,block_size>>> (Dgx, Dgi_inv,Dg+sm, 4.0, M); TimesC<<<n_blocks,block_size>>> (Dgi_inv,Dgx,fnorm,M); } void Backward(int M, int Seg, double *Dgi_inv, double *Dg, double *Dgx, double f, int *Info) { if (Info[0]==1) {Backward(M, Seg, Dgi_inv, Dg, Dgx, f, Info[5], Info[6]);} else { const double f1 = exp(f); const double f_1 = exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = (Mz+2); int jz = 1; int sm=(Seg-1)*M; SetBoundaries(Dgi_inv,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); int block_size=256; int n_blocks; switch (Info[1]){ case 1: //simple cubic n_blocks=(M-jx)/block_size+ ((M-jx)%block_size == 0 ? 0:1); Times <<<n_blocks,block_size>>> (Dgx+jx,Dgi_inv, Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>> (Dgx, Dgi_inv+jx,Dg+sm, M-jx); n_blocks=(M-jy)/block_size+ ((M-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>> (Dgx+jy,Dgi_inv, Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>> (Dgx, Dgi_inv+jy,Dg+sm, M-jy); n_blocks=(M-1)/block_size+((M-1)%block_size == 0 ? 0:1); if (f !=0) { AddTimesF<<<n_blocks,block_size>>> (Dgx+jz,Dgi_inv, Dg+sm+jz,f1, M-jz); AddTimesF<<<n_blocks,block_size>>> (Dgx, Dgi_inv+jz,Dg+sm, f_1,M-jz); } else { AddTimes<<<n_blocks,block_size>>> (Dgx+jz,Dgi_inv, Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>> (Dgx, Dgi_inv+jz,Dg+sm, M-jz); } n_blocks=M/block_size+(M%block_size ==0 ? 0:1); TimesC<<<n_blocks,block_size>>> (Dgi_inv,Dgx,fnorm,M); break; case 2: //FCC n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>( Dgx+jx+jy,Dgi_inv, Dg+sm+jx+jy,M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jx+jy, Dg+sm, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgx+jx, Dgi_inv+jy, Dg+sm+jx, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv+jx, Dg+sm+jy, M-jx-jy); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); if (f !=0) { AddTimesF<<<n_blocks,block_size>>>(Dgx+jy+jz,Dgi_inv, Dg+sm+jy+jz,f1, M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jy+jz, Dg+sm, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jx+jz,Dgi_inv, Dg+sm+jx+jz,f1, M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jx+jz, Dg+sm, f_1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jx, Dgi_inv+jz, Dg+sm+jx, f_1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jx ,Dg+sm+jz, f1, M-jx-jz); } else { AddTimes<<<n_blocks,block_size>>>(Dgx+jy+jz,Dgi_inv, Dg+sm+jy+jz,M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jy+jz, Dg+sm, M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv+jz, Dg+sm+jy, M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jy, Dg+sm+jz, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgx+jx+jz,Dgi_inv, Dg+sm+jx+jz,M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jx+jz, Dg+sm, M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgx+jx, Dgi_inv+jz, Dg+sm+jx, M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jx ,Dg+sm+jz, M-jx-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); TimesC<<<n_blocks,block_size>>>(Dgi_inv,Dgx,1/12.0,M); break; case 3: //HEX n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>( Dgx+jx, Dgi_inv, Dg+sm+jx, M-jx); AddTimes<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jx, Dg+sm, M-jx); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgx+jx, Dgi_inv+jy, Dg+sm+jx, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv+jx, Dg+sm+jy, M-jy-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jy, Dg+sm, M-jy); AddTimes<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv, Dg+sm+jy, M-jy); if (f!=0) { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jx, Dg+sm+jz, f1 ,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jx, Dgi_inv+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv, Dg+sm+jz, f1, M-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jz, Dg+sm, f_1,M-jz); } else { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jx, Dg+sm+jz, f1, M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jx, Dgi_inv+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jy, Dgi_inv+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgx+jz, Dgi_inv, Dg+sm+jz, f1, M-jz); AddTimesF<<<n_blocks,block_size>>>(Dgx, Dgi_inv+jz, Dg+sm, f_1,M-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); TimesC<<<n_blocks,block_size>>>(Dgi_inv,Dgx,1/12.0,M); break; default: printf("Unknown LatticeType in GPU module.\n"); break; }} } void Backward(int M, int Seg, double *Dgi_inv, double *Dg, double *Dgx, double S, double f, int *Info) { //second order. const double f1 = exp(f); const double f_1 = exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = (Mz+2); int jz = 1; int sm=(Seg-1)*M; double *gz0 = &Dgi_inv[0]; double *gz1 = &Dgi_inv[M]; double *gz2 = &Dgi_inv[2*M]; double *gz3 = &Dgi_inv[3*M]; double *gz4 = &Dgi_inv[4*M]; double *gz5 = &Dgi_inv[5*M]; SetBoundaries(gz0,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz1,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); SetBoundaries(gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[5]<3) SwitchBounds(gz0,gz5,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[7]<3) SwitchBounds(gz1,gz4,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); if (Info[9]<3) SwitchBounds(gz2,gz3,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); double *gx0 = &Dgx[0]; double *gx1 = &Dgx[M]; double *gx2 = &Dgx[2*M]; double *gx3 = &Dgx[3*M]; double *gx4 = &Dgx[4*M]; double *gx5 = &Dgx[5*M]; int block_size=256; int n_blocks; switch (Info[1]) { case 1: if (S<0){ n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gx0+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx1+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx2+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx3+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx4+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx5+jx,gz5,Dg+sm+jx,M-jx); AddTimes<<<n_blocks,block_size>>>(gx0,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx1,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx2,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx3,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx4,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx5,gz0+jx,Dg+sm,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(gx0+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx1+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx2+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx3+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx4+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx5+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx0,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx1,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx2,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx3,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx4,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx5,gz1+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(gx0+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx1+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx2+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx3+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx4+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx5+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx0,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx1,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx2,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx3,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx4,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx5,gz2+jz,Dg+sm,M-jz); n_blocks=(6*M)/block_size + ((6*M)%block_size == 0 ? 0:1); TimesC<<<n_blocks,block_size>>>(gz0,gx0,1.0/6.0,6*M); } else { double L=(1.0-S)/4.0; double SoverL=S/L; n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>(gx1+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx2+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx3+jx,gz5,Dg+sm+jx,M-jx); Times<<<n_blocks,block_size>>>(gx4+jx,gz5,Dg+sm+jx,M-jx); TimesF<<<n_blocks,block_size>>>(gx5+jx,gz5,Dg+sm+jx,SoverL,M-jx); TimesF<<<n_blocks,block_size>>>(gx0,gz0+jx,Dg+sm,SoverL,M-jx); AddTimes<<<n_blocks,block_size>>>(gx1,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx2,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx3,gz0+jx,Dg+sm,M-jx); AddTimes<<<n_blocks,block_size>>>(gx4,gz0+jx,Dg+sm,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(gx0+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx2+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx3+jy,gz4,Dg+sm+jy,M-jy); AddTimesF<<<n_blocks,block_size>>>(gx4+jy,gz4,Dg+sm+jy,SoverL,M-jy); AddTimes<<<n_blocks,block_size>>>(gx5+jy,gz4,Dg+sm+jy,M-jy); AddTimes<<<n_blocks,block_size>>>(gx0,gz1+jy,Dg+sm,M-jy); AddTimesF<<<n_blocks,block_size>>>(gx1,gz1+jy,Dg+sm,SoverL,M-jy); AddTimes<<<n_blocks,block_size>>>(gx2,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx3,gz1+jy,Dg+sm,M-jy); AddTimes<<<n_blocks,block_size>>>(gx5,gz1+jy,Dg+sm,M-jy); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(gx0+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx1+jz,gz3,Dg+sm+jz,M-jz); AddTimesF<<<n_blocks,block_size>>>(gx3+jz,gz3,Dg+sm+jz,SoverL,M-jz); AddTimes<<<n_blocks,block_size>>>(gx4+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx5+jz,gz3,Dg+sm+jz,M-jz); AddTimes<<<n_blocks,block_size>>>(gx0,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx1,gz2+jz,Dg+sm,M-jz); AddTimesF<<<n_blocks,block_size>>>(gx2,gz2+jz,Dg+sm,SoverL,M-jz); AddTimes<<<n_blocks,block_size>>>(gx4,gz2+jz,Dg+sm,M-jz); AddTimes<<<n_blocks,block_size>>>(gx5,gz2+jz,Dg+sm,M-jz); n_blocks=(6*M)/block_size + ((6*M)%block_size == 0 ? 0:1); TimesC<<<n_blocks,block_size>>>(gz0,gx0,L,6*M); } break; default: printf("Unknown LatticeType in GPU module.\n"); break; } } void Propagate(int s_from, int s_to, int M, int Seg, double *Dgi, double *Dg, double f, int *Info) { double f1=exp(f); double f_1=exp(-f); double fnorm = 1.0/(4.0 + f1 + f_1); int Mx = Info[2]; int My = Info[3]; int Mz = Info[4]; int jx = (My+2)*(Mz+2); int jy = Mz+2; int jz = 1; int s2=(s_from-1)*M; int s1=(s_to-1)*M; int sm=(Seg-1)*M; SetBoundaries(Dgi+s2,Mx,My,Mz,Info[5],Info[6],Info[7],Info[8],Info[9],Info[10]); int block_size=256; int n_blocks; switch (Info[1]) { case 1: //Simple cubic n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times <<<n_blocks,block_size>>> (Dgi+s1+jx,Dgi+s2, Dg+sm+jx,M-jx); AddTimes <<<n_blocks,block_size>>> (Dgi+s1 ,Dgi+s2+jx,Dg+sm ,M-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes <<<n_blocks,block_size>>> (Dgi+s1+jy,Dgi+s2, Dg+sm+jy,M-jy); AddTimes <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jy,Dg+sm ,M-jy); n_blocks=(M-1)/block_size + ((M-1)%block_size == 0 ? 0:1); if (f !=0) { AddTimesF <<<n_blocks,block_size>>> (Dgi+s1+jz,Dgi+s2, Dg+sm+jz,f1, M-jz); AddTimesF <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jz,Dg+sm, f_1,M-jz); } else { AddTimes <<<n_blocks,block_size>>> (Dgi+s1+jz,Dgi+s2, Dg+sm+jz,M-jz); AddTimes <<<n_blocks,block_size>>> (Dgi+s1, Dgi+s2+jz,Dg+sm, M-jz); } n_blocks=M/block_size + (M%block_size == 0 ? 0:1); Norm <<<n_blocks,block_size>>> (Dgi+s1,fnorm,M); break; case 2: //FCC n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>( Dgi+s1+jx+jy,Dgi+s2, Dg+sm+jx+jy,M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx+jy, Dg+sm, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jx-jy); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); if (f !=0) { AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,f1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jy+jz, Dg+sm, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1,M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,f1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx+jz, Dg, f_1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, f1,M-jx-jz); } else { AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy+jz,Dgi+s2, Dg+sm+jy+jz,M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jy+jz, Dg+sm, M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, M-jy-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, M-jy-jz); n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx+jz,Dgi+s2, Dg+sm+jx+jz,M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx+jz, Dg+sm, M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, M-jx-jz); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx ,Dg+sm+jz, M-jx-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(Dgi+s1,1/12.0,M); break; case 3: //HEX n_blocks=(M-jx)/block_size + ((M-jx)%block_size == 0 ? 0:1); Times<<<n_blocks,block_size>>>( Dgi+s1+jx, Dgi+s2, Dg+sm+jx, M-jx); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jx, Dg+sm, M-jx); n_blocks=(M-jx-jy)/block_size + ((M-jx-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jy, Dg+sm+jx, M-jx-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jx, Dg+sm+jy, M-jy-jx); n_blocks=(M-jy)/block_size + ((M-jy)%block_size == 0 ? 0:1); AddTimes<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jy, Dg+sm, M-jy); AddTimes<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2, Dg+sm+jy , M-jy); if (f!=0) { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1 ,M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } else { n_blocks=(M-jx-jz)/block_size + ((M-jx-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jx, Dg+sm+jz, f1, M-jx-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jx, Dgi+s2+jz, Dg+sm+jx, f_1,M-jx-jz); n_blocks=(M-jy-jz)/block_size + ((M-jy-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jy, Dgi+s2+jz, Dg+sm+jy, f_1,M-jy-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2+jy, Dg+sm+jz, f1, M-jy-jz); n_blocks=(M-jz)/block_size + ((M-jz)%block_size == 0 ? 0:1); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1+jz, Dgi+s2, Dg+sm+jz, f1, M-jz); AddTimesF<<<n_blocks,block_size>>>(Dgi+s1, Dgi+s2+jz, Dg+sm, f_1,M-jz); } n_blocks=(M)/block_size + ((M)%block_size == 0 ? 0:1); Norm<<<n_blocks,block_size>>>(Dgi+s1,1/12.0,M); break; default: printf("Unknown LatticeType in GPU module.\n"); break; } } void FreeMemoryOnDevice(double *D) { cudaFree(D); } // Info[0]=3; //Dimensions // Info[1]=3; //3=HEX, 2=FCC, 1=simple cubic; // Info[2]=n_layers_x; // Info[3]=n_layers_y; // Info[4]=n_layers_z; //assumption that compranges =1; // Info[5]=Get_BL(1); //x lower bound // Info[6]=Get_BL(2); //x upper bound // Info[7]=Get_BL(3); //y lower bound // Info[8]=Get_BL(4); //y upper bound // Info[9]=Get_BL(5); //z lower bound // Info[10]=Get_BL(6);//z upper bound
303b6a0c2edf9b5d5a0190248bc9951635baa9fd.hip
// !!! This is a file automatically generated by hipify!!! #include <iostream> #include <stdlib.h> #include <string> #include <iostream> #include <chrono> #include <fstream> #include "hip/hip_runtime.h" #include "device_launch_parameters.h" #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using namespace cv; #define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value) #define HIST_LENGHT 256 //Check the return value of the CUDA runtime API call and exit the application if the call has failed. static void CheckCudaErrorAux(const char* file, unsigned line, const char* statement, hipError_t err) { if (err == hipSuccess) return; std::cerr << statement << " returned " << hipGetErrorString(err) << "(" << err << ") at " << file << ":" << line << std::endl; exit(1); } inline int cudaDeviceInit() { CUDA_CHECK_RETURN(hipFree(0)); int deviceCount; CUDA_CHECK_RETURN(hipGetDeviceCount(&deviceCount)); if (deviceCount == 0) { std::cerr << "CUDA error: no devices supporting CUDA." << std::endl; exit(EXIT_FAILURE); } int dev = 0; hipDeviceProp_t deviceProp; CUDA_CHECK_RETURN(hipGetDeviceProperties(&deviceProp, dev)); std::cerr << "hipSetDevice GPU" << dev << " = " << deviceProp.name << std::endl; CUDA_CHECK_RETURN(hipSetDevice(dev)); return dev; } __global__ void calcCumulativeHist(int* cumulative_dist, int* histogram, int width, int height) { __shared__ int partialScan[HIST_LENGHT]; int i = threadIdx.x + blockDim.x * blockIdx.x; //load phase, load values on shared memory if (i < HIST_LENGHT) partialScan[i] = histogram[i]; __syncthreads(); //work efficient reduction phase for (unsigned int stride = 1; stride <= HIST_LENGHT / 2; stride *= 2) { unsigned int index = (threadIdx.x + 1) * stride * 2 - 1; if (index < HIST_LENGHT) partialScan[index] += partialScan[index - stride]; __syncthreads(); } //work efficient post reduction phase for (unsigned int stride = HIST_LENGHT / 2; stride > 0; stride /= 2) { __syncthreads(); unsigned int index = (threadIdx.x + 1) * stride * 2 - 1; if (index + stride < HIST_LENGHT) { partialScan[index + stride] += partialScan[index]; } } //back on global memory __syncthreads(); if (i < HIST_LENGHT) { cumulative_dist[i] += partialScan[i]; } } __global__ void convertToYCbCr(unsigned char* image, int width, int height, int* histogram) { int idx = blockIdx.x * blockDim.x + threadIdx.x; long index; __shared__ int hist_shared[HIST_LENGHT]; for (int bin_idx = threadIdx.x; bin_idx < HIST_LENGHT; bin_idx += blockDim.x) { hist_shared[bin_idx] = 0; } __syncthreads(); for (int i = idx; i < width * height; i += blockDim.x * gridDim.x) { index = i * 3; int R = image[index]; int G = image[index + 1]; int B = image[index + 2]; int Y = R * .257000 + G * .504000 + B * .098000 + 16; int Cb = R * -.148000 + G * -.291000 + B * .439000 + 128; int Cr = R * .439000 + G * -.368000 + B * -.071000 + 128; atomicAdd(&(hist_shared[Y]), 1); image[index] = Y; image[index + 1] = Cb; image[index + 2] = Cr; } __syncthreads(); //The shared histograms are added to the global histogram. for (int bin_idx = threadIdx.x; bin_idx < HIST_LENGHT; bin_idx += blockDim.x) { atomicAdd(&(histogram[bin_idx]), hist_shared[bin_idx]); } } __global__ void equalize(int* equalized, int* cumulative_dist, int width, int height) { int idx = blockIdx.x * blockDim.x + threadIdx.x; for (int k = idx; k < HIST_LENGHT; k += blockDim.x * gridDim.x) { equalized[k] = (int)(((float)cumulative_dist[k] - cumulative_dist[0]) / ((float)width * height - 1) * 255); } } __global__ void revertToRGB(unsigned char* image, int* equalized, int width, int height) { long index; int idx = blockIdx.x * blockDim.x + threadIdx.x; for (int i = idx; i < width * height; i += blockDim.x * gridDim.x) { index = i * 3; int Y = equalized[image[index]]; int Cb = image[index + 1]; int Cr = image[index + 2]; unsigned char R = (unsigned char)max(0, min(255, (int)((Y - 16) * 1.164 + 1.596 * (Cr - 128)))); unsigned char G = (unsigned char)max(0, min(255, (int)((Y - 16) * 1.164 - 0.813 * (Cr - 128) - (0.392 * (Cb - 128))))); unsigned char B = (unsigned char)max(0, min(255, (int)((Y - 16) * 1.164 + 2.017 * (Cb - 128)))); image[index] = R; image[index + 1] = G; image[index + 2] = B; } } int main() { // Load the image String folderpath = "img/*.jpg"; vector<String> filenames; double timesAdded = 0; int imageCounter = 0; cudaDeviceInit(); glob(folderpath, filenames); for (size_t i = 0; i < filenames.size(); i++) { Mat im = imread(filenames[i]); resize(im, im, Size(7680, 4800), INTER_NEAREST); //imshow("Original Image", im); //waitKey(); int width = im.cols; int height = im.rows; auto start = chrono::steady_clock::now(); //int host_equalized[HIST_LENGHT]; //cpu equalized histogram //int host_cumulative_dist[HIST_LENGHT] = { 0 }; unsigned char* host_image = im.ptr(); //Mat image to array image //int host_histogram[HIST_LENGHT] = { 0 }; //cpu histogram unsigned char* device_image; //gpu image int* device_histogram; //gpu histogram int* device_equalized; //gpu equalized histogram int* device_cumulative_dist; //gpu cumulative dist. //allocate gpu global memory. CUDA_CHECK_RETURN(hipMalloc((void**)&device_image, sizeof(char) * (width * height * 3))); CUDA_CHECK_RETURN(hipMalloc((void**)&device_histogram, sizeof(int) * 256)); CUDA_CHECK_RETURN(hipMalloc((void**)&device_equalized, sizeof(int) * 256)); CUDA_CHECK_RETURN(hipMalloc((void**)&device_cumulative_dist, sizeof(int) * 256)); //copy the image on global memory. CUDA_CHECK_RETURN(hipMemcpy(device_image, host_image, sizeof(char) * (width * height * 3), hipMemcpyHostToDevice)); //CUDA_CHECK_RETURN(hipMemcpy(device_histogram, host_histogram, sizeof(int) * 256, hipMemcpyHostToDevice)); //initialize gpu hist and cumulative hist CUDA_CHECK_RETURN(hipMemset(device_cumulative_dist, 0, sizeof(int) * 256)); CUDA_CHECK_RETURN(hipMemset(device_histogram, 0, sizeof(int) * 256)); int block_size = HIST_LENGHT; int grid_size = ((width * height + (block_size - 1)) / block_size) / 2; //first kernel to build histogram and convert to YCbCr. convertToYCbCr << <grid_size, block_size >> > (device_image, width, height, device_histogram); //CUDA_CHECK_RETURN(hipMemcpy(host_histogram, device_histogram, sizeof(int) * 256, hipMemcpyDeviceToHost)); /* host_cumulative_dist[0] = host_histogram[0]; for (int i = 1; i < HIST_LENGHT; i++) { host_cumulative_dist[i] = host_histogram[i] + host_cumulative_dist[i - 1]; } */ //CUDA_CHECK_RETURN(hipMemcpy(device_equalized, host_equalized, sizeof(int) * 256, hipMemcpyHostToDevice)); //CUDA_CHECK_RETURN(hipMemcpy(device_cumulative_dist, host_cumulative_dist, sizeof(int) * 256, hipMemcpyHostToDevice)); calcCumulativeHist << <1, block_size >> > (device_cumulative_dist, device_histogram, width, height); //second kernel that do the real equalization. equalize << <1, block_size >> > (device_equalized, device_cumulative_dist, width, height); //third and last kernel to go back to RGB. revertToRGB << <grid_size, block_size >> > (device_image, device_equalized, width, height); //retrieve the equalized image CUDA_CHECK_RETURN(hipMemcpy(host_image, device_image, sizeof(char) * (width * height * 3), hipMemcpyDeviceToHost)); //release gpu memory CUDA_CHECK_RETURN(hipFree(device_image)); CUDA_CHECK_RETURN(hipFree(device_histogram)); CUDA_CHECK_RETURN(hipFree(device_equalized)); CUDA_CHECK_RETURN(hipFree(device_cumulative_dist)); auto end = chrono::steady_clock::now(); double elapsed_time = chrono::duration_cast<chrono::milliseconds>(end - start).count(); cout << "elapsed : " << elapsed_time << " ms "; cout << "correctly freed memory \n"; Mat equalized_image = Mat(Size(width, height), CV_8UC3, host_image); //imwrite(filenames[i] + "Equalized.jpg",final_image); //imshow("Final Image", equalized_image); //waitKey(); timesAdded += elapsed_time; imageCounter += 1; } double meanTimes = timesAdded / imageCounter; cout << "MEAN ELAPSED TIME : " << meanTimes << " ms" << endl; }
303b6a0c2edf9b5d5a0190248bc9951635baa9fd.cu
#include <iostream> #include <stdlib.h> #include <string> #include <iostream> #include <chrono> #include <fstream> #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using namespace cv; #define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value) #define HIST_LENGHT 256 //Check the return value of the CUDA runtime API call and exit the application if the call has failed. static void CheckCudaErrorAux(const char* file, unsigned line, const char* statement, cudaError_t err) { if (err == cudaSuccess) return; std::cerr << statement << " returned " << cudaGetErrorString(err) << "(" << err << ") at " << file << ":" << line << std::endl; exit(1); } inline int cudaDeviceInit() { CUDA_CHECK_RETURN(cudaFree(0)); int deviceCount; CUDA_CHECK_RETURN(cudaGetDeviceCount(&deviceCount)); if (deviceCount == 0) { std::cerr << "CUDA error: no devices supporting CUDA." << std::endl; exit(EXIT_FAILURE); } int dev = 0; cudaDeviceProp deviceProp; CUDA_CHECK_RETURN(cudaGetDeviceProperties(&deviceProp, dev)); std::cerr << "cudaSetDevice GPU" << dev << " = " << deviceProp.name << std::endl; CUDA_CHECK_RETURN(cudaSetDevice(dev)); return dev; } __global__ void calcCumulativeHist(int* cumulative_dist, int* histogram, int width, int height) { __shared__ int partialScan[HIST_LENGHT]; int i = threadIdx.x + blockDim.x * blockIdx.x; //load phase, load values on shared memory if (i < HIST_LENGHT) partialScan[i] = histogram[i]; __syncthreads(); //work efficient reduction phase for (unsigned int stride = 1; stride <= HIST_LENGHT / 2; stride *= 2) { unsigned int index = (threadIdx.x + 1) * stride * 2 - 1; if (index < HIST_LENGHT) partialScan[index] += partialScan[index - stride]; __syncthreads(); } //work efficient post reduction phase for (unsigned int stride = HIST_LENGHT / 2; stride > 0; stride /= 2) { __syncthreads(); unsigned int index = (threadIdx.x + 1) * stride * 2 - 1; if (index + stride < HIST_LENGHT) { partialScan[index + stride] += partialScan[index]; } } //back on global memory __syncthreads(); if (i < HIST_LENGHT) { cumulative_dist[i] += partialScan[i]; } } __global__ void convertToYCbCr(unsigned char* image, int width, int height, int* histogram) { int idx = blockIdx.x * blockDim.x + threadIdx.x; long index; __shared__ int hist_shared[HIST_LENGHT]; for (int bin_idx = threadIdx.x; bin_idx < HIST_LENGHT; bin_idx += blockDim.x) { hist_shared[bin_idx] = 0; } __syncthreads(); for (int i = idx; i < width * height; i += blockDim.x * gridDim.x) { index = i * 3; int R = image[index]; int G = image[index + 1]; int B = image[index + 2]; int Y = R * .257000 + G * .504000 + B * .098000 + 16; int Cb = R * -.148000 + G * -.291000 + B * .439000 + 128; int Cr = R * .439000 + G * -.368000 + B * -.071000 + 128; atomicAdd(&(hist_shared[Y]), 1); image[index] = Y; image[index + 1] = Cb; image[index + 2] = Cr; } __syncthreads(); //The shared histograms are added to the global histogram. for (int bin_idx = threadIdx.x; bin_idx < HIST_LENGHT; bin_idx += blockDim.x) { atomicAdd(&(histogram[bin_idx]), hist_shared[bin_idx]); } } __global__ void equalize(int* equalized, int* cumulative_dist, int width, int height) { int idx = blockIdx.x * blockDim.x + threadIdx.x; for (int k = idx; k < HIST_LENGHT; k += blockDim.x * gridDim.x) { equalized[k] = (int)(((float)cumulative_dist[k] - cumulative_dist[0]) / ((float)width * height - 1) * 255); } } __global__ void revertToRGB(unsigned char* image, int* equalized, int width, int height) { long index; int idx = blockIdx.x * blockDim.x + threadIdx.x; for (int i = idx; i < width * height; i += blockDim.x * gridDim.x) { index = i * 3; int Y = equalized[image[index]]; int Cb = image[index + 1]; int Cr = image[index + 2]; unsigned char R = (unsigned char)max(0, min(255, (int)((Y - 16) * 1.164 + 1.596 * (Cr - 128)))); unsigned char G = (unsigned char)max(0, min(255, (int)((Y - 16) * 1.164 - 0.813 * (Cr - 128) - (0.392 * (Cb - 128))))); unsigned char B = (unsigned char)max(0, min(255, (int)((Y - 16) * 1.164 + 2.017 * (Cb - 128)))); image[index] = R; image[index + 1] = G; image[index + 2] = B; } } int main() { // Load the image String folderpath = "img/*.jpg"; vector<String> filenames; double timesAdded = 0; int imageCounter = 0; cudaDeviceInit(); glob(folderpath, filenames); for (size_t i = 0; i < filenames.size(); i++) { Mat im = imread(filenames[i]); resize(im, im, Size(7680, 4800), INTER_NEAREST); //imshow("Original Image", im); //waitKey(); int width = im.cols; int height = im.rows; auto start = chrono::steady_clock::now(); //int host_equalized[HIST_LENGHT]; //cpu equalized histogram //int host_cumulative_dist[HIST_LENGHT] = { 0 }; unsigned char* host_image = im.ptr(); //Mat image to array image //int host_histogram[HIST_LENGHT] = { 0 }; //cpu histogram unsigned char* device_image; //gpu image int* device_histogram; //gpu histogram int* device_equalized; //gpu equalized histogram int* device_cumulative_dist; //gpu cumulative dist. //allocate gpu global memory. CUDA_CHECK_RETURN(cudaMalloc((void**)&device_image, sizeof(char) * (width * height * 3))); CUDA_CHECK_RETURN(cudaMalloc((void**)&device_histogram, sizeof(int) * 256)); CUDA_CHECK_RETURN(cudaMalloc((void**)&device_equalized, sizeof(int) * 256)); CUDA_CHECK_RETURN(cudaMalloc((void**)&device_cumulative_dist, sizeof(int) * 256)); //copy the image on global memory. CUDA_CHECK_RETURN(cudaMemcpy(device_image, host_image, sizeof(char) * (width * height * 3), cudaMemcpyHostToDevice)); //CUDA_CHECK_RETURN(cudaMemcpy(device_histogram, host_histogram, sizeof(int) * 256, cudaMemcpyHostToDevice)); //initialize gpu hist and cumulative hist CUDA_CHECK_RETURN(cudaMemset(device_cumulative_dist, 0, sizeof(int) * 256)); CUDA_CHECK_RETURN(cudaMemset(device_histogram, 0, sizeof(int) * 256)); int block_size = HIST_LENGHT; int grid_size = ((width * height + (block_size - 1)) / block_size) / 2; //first kernel to build histogram and convert to YCbCr. convertToYCbCr << <grid_size, block_size >> > (device_image, width, height, device_histogram); //CUDA_CHECK_RETURN(cudaMemcpy(host_histogram, device_histogram, sizeof(int) * 256, cudaMemcpyDeviceToHost)); /* host_cumulative_dist[0] = host_histogram[0]; for (int i = 1; i < HIST_LENGHT; i++) { host_cumulative_dist[i] = host_histogram[i] + host_cumulative_dist[i - 1]; } */ //CUDA_CHECK_RETURN(cudaMemcpy(device_equalized, host_equalized, sizeof(int) * 256, cudaMemcpyHostToDevice)); //CUDA_CHECK_RETURN(cudaMemcpy(device_cumulative_dist, host_cumulative_dist, sizeof(int) * 256, cudaMemcpyHostToDevice)); calcCumulativeHist << <1, block_size >> > (device_cumulative_dist, device_histogram, width, height); //second kernel that do the real equalization. equalize << <1, block_size >> > (device_equalized, device_cumulative_dist, width, height); //third and last kernel to go back to RGB. revertToRGB << <grid_size, block_size >> > (device_image, device_equalized, width, height); //retrieve the equalized image CUDA_CHECK_RETURN(cudaMemcpy(host_image, device_image, sizeof(char) * (width * height * 3), cudaMemcpyDeviceToHost)); //release gpu memory CUDA_CHECK_RETURN(cudaFree(device_image)); CUDA_CHECK_RETURN(cudaFree(device_histogram)); CUDA_CHECK_RETURN(cudaFree(device_equalized)); CUDA_CHECK_RETURN(cudaFree(device_cumulative_dist)); auto end = chrono::steady_clock::now(); double elapsed_time = chrono::duration_cast<chrono::milliseconds>(end - start).count(); cout << "elapsed : " << elapsed_time << " ms "; cout << "correctly freed memory \n"; Mat equalized_image = Mat(Size(width, height), CV_8UC3, host_image); //imwrite(filenames[i] + "Equalized.jpg",final_image); //imshow("Final Image", equalized_image); //waitKey(); timesAdded += elapsed_time; imageCounter += 1; } double meanTimes = timesAdded / imageCounter; cout << "MEAN ELAPSED TIME : " << meanTimes << " ms" << endl; }
f6da2a16ccee6bb7eacdd05bd9804f89e5913865.hip
// !!! This is a file automatically generated by hipify!!! #include <stdio.h> // Print device properties void printDevProp(hipDeviceProp_t devProp) { printf("Major revision number: %d\n", devProp.major); printf("Minor revision number: %d\n", devProp.minor); printf("Name: %s\n", devProp.name); printf("Total global memory: %u\n", devProp.totalGlobalMem); printf("Total shared memory per block: %u\n", devProp.sharedMemPerBlock); printf("Total registers per block: %d\n", devProp.regsPerBlock); printf("Warp size: %d\n", devProp.warpSize); printf("Maximum memory pitch: %u\n", devProp.memPitch); printf("Maximum threads per block: %d\n", devProp.maxThreadsPerBlock); for (int i = 0; i < 3; ++i) printf("Maximum dimension %d of block: %d\n", i, devProp.maxThreadsDim[i]); for (int i = 0; i < 3; ++i) printf("Maximum dimension %d of grid: %d\n", i, devProp.maxGridSize[i]); printf("Clock rate: %d\n", devProp.clockRate); printf("Total constant memory: %u\n", devProp.totalConstMem); printf("Texture alignment: %u\n", devProp.textureAlignment); printf("Concurrent copy and execution: %s\n", (devProp.deviceOverlap ? "Yes" : "No")); printf("Number of multiprocessors: %d\n", devProp.multiProcessorCount); printf("Kernel execution timeout: %s\n", (devProp.kernelExecTimeoutEnabled ? "Yes" : "No")); return; } int main() { // Number of CUDA devices int devCount; hipGetDeviceCount(&devCount); printf("CUDA Device Query...\n"); printf("There are %d CUDA devices.\n", devCount); // Iterate through devices for (int i = 0; i < devCount; ++i) { // Get device properties printf("\nCUDA Device #%d\n", i); hipDeviceProp_t devProp; hipGetDeviceProperties(&devProp, i); printDevProp(devProp); } printf("\nPress any key to exit..."); char c; scanf("%c", &c); return 0; }
f6da2a16ccee6bb7eacdd05bd9804f89e5913865.cu
#include <stdio.h> // Print device properties void printDevProp(cudaDeviceProp devProp) { printf("Major revision number: %d\n", devProp.major); printf("Minor revision number: %d\n", devProp.minor); printf("Name: %s\n", devProp.name); printf("Total global memory: %u\n", devProp.totalGlobalMem); printf("Total shared memory per block: %u\n", devProp.sharedMemPerBlock); printf("Total registers per block: %d\n", devProp.regsPerBlock); printf("Warp size: %d\n", devProp.warpSize); printf("Maximum memory pitch: %u\n", devProp.memPitch); printf("Maximum threads per block: %d\n", devProp.maxThreadsPerBlock); for (int i = 0; i < 3; ++i) printf("Maximum dimension %d of block: %d\n", i, devProp.maxThreadsDim[i]); for (int i = 0; i < 3; ++i) printf("Maximum dimension %d of grid: %d\n", i, devProp.maxGridSize[i]); printf("Clock rate: %d\n", devProp.clockRate); printf("Total constant memory: %u\n", devProp.totalConstMem); printf("Texture alignment: %u\n", devProp.textureAlignment); printf("Concurrent copy and execution: %s\n", (devProp.deviceOverlap ? "Yes" : "No")); printf("Number of multiprocessors: %d\n", devProp.multiProcessorCount); printf("Kernel execution timeout: %s\n", (devProp.kernelExecTimeoutEnabled ? "Yes" : "No")); return; } int main() { // Number of CUDA devices int devCount; cudaGetDeviceCount(&devCount); printf("CUDA Device Query...\n"); printf("There are %d CUDA devices.\n", devCount); // Iterate through devices for (int i = 0; i < devCount; ++i) { // Get device properties printf("\nCUDA Device #%d\n", i); cudaDeviceProp devProp; cudaGetDeviceProperties(&devProp, i); printDevProp(devProp); } printf("\nPress any key to exit..."); char c; scanf("%c", &c); return 0; }
196274e936c992467e4a8a251ed178a1e38df789.hip
// !!! This is a file automatically generated by hipify!!! /* * Copyright (c) 2020, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <hip/hip_runtime_api.h> #include <sys/time.h> #include <algorithm> #include <fstream> #include <functional> #include <random> #include "HugeCTR/include/data_generator.hpp" #include "HugeCTR/include/data_readers/data_reader.hpp" #include "HugeCTR/include/embeddings/localized_slot_sparse_embedding_one_hot.hpp" #include "gtest/gtest.h" #include "roctracer/roctx.h" #include "utest/embedding/embedding_test_utils.hpp" #include "utest/embedding/sparse_embedding_hash_cpu.hpp" #include "utest/test_utils.h" using namespace HugeCTR; using namespace embedding_test; namespace { //--------------------------------------------------------------------------------------- // global params for all testing const int train_batch_num = 10; // can not more than 32 const int test_batch_num = 1; const int train_batchsize = 1024; const int test_batchsize = 2560; const int slot_num = 26; const int max_nnz_per_slot = 1; const int max_feature_num = max_nnz_per_slot * slot_num; // max_feature_num in a sample const long long vocabulary_size = slot_num * 100; const int embedding_vec_size = 128; const int combiner = 0; // 0-sum, 1-mean const long long label_dim = 1; const long long dense_dim = 0; typedef long long T; const float scaler = 1.0f; // used in mixed precision training const float lr = 0.01f; // In order to not allocate the total size of hash table on each GPU, the users need to set the // size of max_vocabulary_size_per_gpu, which should be more than vocabulary_size/gpu_count, // eg: 1.25x of that. const int num_chunk_threads = 1; // must be 1 for CPU and GPU results comparation const int num_files = 1; const Check_t CHK = Check_t::Sum; // Check_t::Sum const char *train_file_list_name = "train_file_list.txt"; const char *test_file_list_name = "test_file_list.txt"; const char *prefix = "./data_reader_test_data/temp_dataset_"; #ifndef NCCl_A2A const std::string plan_file(PROJECT_HOME_ + "utest/all2all_plan_dgx_{0,1,2,3,4,5,6,7}.json"); #else const std::string plan_file = ""; #endif const char *hash_table_file_name = "localized_hash_table.bin"; // std::vector<size_t> slot_sizes; // null means use vocabulary_size/gpu_count/load_factor as // max_vocabulary_size_per_gpu // CAUSION: must match vocabulary_size // std::vector<size_t> slot_sizes = {39884406,39043,17289,7420,20263,3,7120,1543,63,38532951, // 2953546,403346,10,2208,11938,155,4,976,14,39979771,25641295,39664984,585935,12972,108,36}; // // for cretio dataset std::vector<size_t> slot_sizes = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; // just for verify //----------------------------------------------------------------------------------------- template <typename TypeEmbeddingComp> void train_and_test(const std::vector<int> &device_list, const Optimizer_t &optimizer, const Update_t &update_type) { OptHyperParams<TypeEmbeddingComp> hyper_params; hyper_params.sgd.atomic_update = true; const OptParams<TypeEmbeddingComp> opt_params = {optimizer, lr, hyper_params, update_type, scaler}; float tolerance; if (std::is_same<TypeEmbeddingComp, __half>::value) { tolerance = 5e-3f; } else { tolerance = 1e-4f; } test::mpi_init(); int numprocs = 1; #ifdef ENABLE_MPI MPI_Comm_size(MPI_COMM_WORLD, &numprocs); #endif // if there are multi-node, we assume each node has the same gpu device_list std::vector<std::vector<int>> vvgpu; for (int i = 0; i < numprocs; i++) { vvgpu.push_back(device_list); } const auto &resource_manager = ResourceManager::create(vvgpu, 0); if (resource_manager->get_pid() == 0) { std::cout << "rank " << resource_manager->get_pid() << " is generating data" << std::endl; { // re-generate the dataset files std::ifstream file(train_file_list_name); if (file.good()) { std::remove(train_file_list_name); } } { // re-generate the dataset files std::ifstream file(test_file_list_name); if (file.good()) { std::remove(test_file_list_name); } } // data generation: key's corresponding slot_id=(key%slot_num) if (slot_sizes.size() > 0) { HugeCTR::data_generation_for_localized_test<T, CHK>( train_file_list_name, prefix, num_files, train_batch_num * train_batchsize, slot_num, vocabulary_size, label_dim, dense_dim, max_nnz_per_slot, slot_sizes); HugeCTR::data_generation_for_localized_test<T, CHK>( test_file_list_name, prefix, num_files, test_batch_num * test_batchsize, slot_num, vocabulary_size, label_dim, dense_dim, max_nnz_per_slot, slot_sizes); } else { CK_THROW_( Error_t::WrongInput, "Must set slot_sizes since there is no hashtable in LocalizedSlotSpasrseEmbeddingOneHot"); } } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); std::cout << "This is rank: " << resource_manager->get_pid() << std::endl; #endif // setup a data reader const DataReaderSparseParam param = {DataReaderSparse_t::Localized, max_nnz_per_slot * slot_num, max_nnz_per_slot, slot_num}; std::vector<DataReaderSparseParam> params; params.push_back(param); std::unique_ptr<DataReader<T>> train_data_reader(new DataReader<T>( train_batchsize, label_dim, dense_dim, params, resource_manager, true, num_chunk_threads)); train_data_reader->create_drwg_norm(train_file_list_name, CHK); std::unique_ptr<DataReader<T>> test_data_reader(new DataReader<T>( test_batchsize, label_dim, dense_dim, params, resource_manager, true, num_chunk_threads)); test_data_reader->create_drwg_norm(test_file_list_name, CHK); // generate hashtable if (resource_manager->get_pid() == 0) { std::cout << "Init hash table"; // init hash table file: <key, solt_id, value> std::ofstream weight_stream(hash_table_file_name); if (!weight_stream.is_open()) { ERROR_MESSAGE_("Error: file not open for writing"); } // UnifiedDataSimulator<T> ldata_sim(0, slot_num-1); // for slot_id test::UniformDataSimulator fdata_sim; // for value std::unique_ptr<float[]> buf(new float[embedding_vec_size]); for (long long i = 0; i < vocabulary_size; i++) { T key = (T)i; // T key = ldata_sim.get_num(); // CAUSION: can not set random keys here, because we need to ensure that: // 1) we can find keys in the data file from this hash table // 2) there are no repeated keys weight_stream.write((char *)&key, sizeof(T)); T slot_id; if (slot_sizes.size() == 0) { // slot_id = key % slot_num; // CAUSION: need to dedicate the slot_id for each key for // // correctness verification CK_THROW_(Error_t::WrongInput, "Must set slot_sizes since there is no hashtable in " "LocalizedSlotSpasrseEmbeddingOneHot"); } else { size_t offset = 0; for (size_t j = 0; j < slot_sizes.size(); j++) { if ((key >= static_cast<T>(offset)) && (key < static_cast<T>(offset + slot_sizes[j]))) { slot_id = (T)j; break; } offset += slot_sizes[j]; } } weight_stream.write((char *)&slot_id, sizeof(T)); // float val = (float)i; // float val = 0.1f; fdata_sim.fill(buf.get(), embedding_vec_size, -0.1f, 0.1f); weight_stream.write(reinterpret_cast<const char *>(buf.get()), embedding_vec_size * sizeof(float)); } weight_stream.close(); std::cout << " Done" << std::endl; } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif const SparseEmbeddingHashParams<TypeEmbeddingComp> embedding_params = { train_batchsize, test_batchsize, 0, slot_sizes, embedding_vec_size, max_feature_num, slot_num, combiner, opt_params}; std::unique_ptr<Embedding<T, TypeEmbeddingComp>> embedding( new LocalizedSlotSparseEmbeddingOneHot<T, TypeEmbeddingComp>( train_data_reader->get_row_offsets_tensors(), train_data_reader->get_value_tensors(), train_data_reader->get_nnz_array(), test_data_reader->get_row_offsets_tensors(), test_data_reader->get_value_tensors(), test_data_reader->get_nnz_array(), embedding_params, plan_file, resource_manager)); { // upload hash table to device std::ifstream fs(hash_table_file_name); embedding->load_parameters(fs); fs.close(); } // for SparseEmbeddingCpu std::unique_ptr<SparseEmbeddingHashCpu<T, TypeEmbeddingComp>> embedding_cpu( new SparseEmbeddingHashCpu<T, TypeEmbeddingComp>( train_batchsize, max_feature_num, vocabulary_size, embedding_vec_size, slot_num, label_dim, dense_dim, CHK, train_batch_num * train_batchsize, combiner, opt_params, train_file_list_name, hash_table_file_name, SparseEmbedding_t::Localized)); TypeEmbeddingComp *embedding_feature_from_cpu = embedding_cpu->get_forward_results(); TypeEmbeddingComp *wgrad_from_cpu = embedding_cpu->get_backward_results(); T *hash_table_key_from_cpu = embedding_cpu->get_hash_table_key_ptr(); float *hash_table_value_from_cpu = embedding_cpu->get_hash_table_value_ptr(); // for results check std::shared_ptr<GeneralBuffer2<HostAllocator>> buf = GeneralBuffer2<HostAllocator>::create(); Tensor2<TypeEmbeddingComp> embedding_feature_from_gpu; buf->reserve({train_batchsize * slot_num * embedding_vec_size}, &embedding_feature_from_gpu); Tensor2<TypeEmbeddingComp> wgrad_from_gpu; buf->reserve({train_batchsize * slot_num * embedding_vec_size}, &wgrad_from_gpu); Tensor2<TypeEmbeddingComp> embedding_feature_from_gpu_eval; buf->reserve({test_batchsize * slot_num * embedding_vec_size}, &embedding_feature_from_gpu_eval); buf->allocate(); typedef struct TypeHashValue_ { float data[embedding_vec_size]; } TypeHashValue; for (int i = 0; i < train_batch_num; i++) { printf("Rank%d: Round %d start training:\n", resource_manager->get_pid(), i); // call read a batch printf("Rank%d: data_reader->read_a_batch_to_device()\n", resource_manager->get_pid()); train_data_reader->read_a_batch_to_device(); // GPU forward printf("Rank%d: embedding->forward()\n", resource_manager->get_pid()); embedding->forward(true); // check the result of forward printf("Rank%d: embedding->get_forward_results()\n", resource_manager->get_pid()); embedding->get_forward_results(true, embedding_feature_from_gpu); // memcpy from GPU to CPU if (resource_manager->get_pid() == 0) { // CPU forward printf("Rank0: embedding_cpu->forward()\n"); embedding_cpu->forward(); printf("Rank0: check forward results\n"); ASSERT_TRUE(compare_embedding_feature(train_batchsize * slot_num * embedding_vec_size, embedding_feature_from_gpu.get_ptr(), embedding_feature_from_cpu, tolerance)); } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif // GPU backward printf("Rank%d: embedding->backward()\n", resource_manager->get_pid()); embedding->backward(); // check the result of backward printf("Rank%d: embedding->get_backward_results()\n", resource_manager->get_pid()); embedding->get_backward_results(wgrad_from_gpu, 0); if (resource_manager->get_pid() == 0) { // CPU backward printf("Rank0: embedding_cpu->backward()\n"); embedding_cpu->backward(); printf("Rank0: check backward results: GPU and CPU\n"); ASSERT_TRUE(compare_wgrad(train_batchsize * slot_num * embedding_vec_size, wgrad_from_gpu.get_ptr(), wgrad_from_cpu, tolerance)); } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif // GPU update_params printf("Rank%d: embedding->update_params()\n", resource_manager->get_pid()); embedding->update_params(); if (resource_manager->get_pid() == 0) { // CPU update_params printf("Rank0: embedding_cpu->update_params()\n"); embedding_cpu->update_params(); } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif printf("Rank%d: Round %d end:\n", resource_manager->get_pid(), i); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // create new obj for eval() { std::ofstream fs(hash_table_file_name); embedding->dump_parameters(fs); fs.close(); } // for SparseEmbeddingCpu eval std::unique_ptr<SparseEmbeddingHashCpu<T, TypeEmbeddingComp>> test_embedding_cpu( new SparseEmbeddingHashCpu<T, TypeEmbeddingComp>( test_batchsize, max_feature_num, vocabulary_size, embedding_vec_size, slot_num, label_dim, dense_dim, CHK, test_batch_num * test_batchsize, combiner, opt_params, test_file_list_name, hash_table_file_name, SparseEmbedding_t::Localized)); TypeEmbeddingComp *embedding_feature_from_cpu_eval = test_embedding_cpu->get_forward_results(); { ///////////////////////////////////////////////////////////////////////////////////////////// // eval printf("\nRank%d: Round start eval:\n", resource_manager->get_pid()); // call read a batch printf("Rank%d: data_reader_eval->read_a_batch_to_device()\n", resource_manager->get_pid()); test_data_reader->read_a_batch_to_device(); // GPU forward printf("Rank%d: embedding_eval->forward()\n", resource_manager->get_pid()); embedding->forward(false); // check the result of forward printf("Rank%d: embedding_eval->get_forward_results()\n", resource_manager->get_pid()); embedding->get_forward_results(false, embedding_feature_from_gpu_eval); // memcpy from GPU to CPU if (resource_manager->get_pid() == 0) { // CPU forward printf("Rank0: embedding_cpu_eval->forward()\n"); test_embedding_cpu->forward(); printf("Rank0: check forward results\n"); ASSERT_TRUE(compare_embedding_feature(test_batchsize * slot_num * embedding_vec_size, embedding_feature_from_gpu_eval.get_ptr(), embedding_feature_from_cpu_eval, tolerance)); } } test::mpi_finalize(); } } // namespace TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_1gpu) { train_and_test<float>({0}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_4gpu) { train_and_test<float>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_global_update_1gpu) { train_and_test<float>({0}, Optimizer_t::SGD, Update_t::Global); } TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_global_update_4gpu) { train_and_test<float>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Global); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_1gpu) { train_and_test<__half>({0}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_4gpu) { train_and_test<__half>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_global_update_1gpu) { train_and_test<__half>({0}, Optimizer_t::SGD, Update_t::Global); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_global_update_4gpu) { train_and_test<__half>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Global); }
196274e936c992467e4a8a251ed178a1e38df789.cu
/* * Copyright (c) 2020, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <cuda_profiler_api.h> #include <sys/time.h> #include <algorithm> #include <fstream> #include <functional> #include <random> #include "HugeCTR/include/data_generator.hpp" #include "HugeCTR/include/data_readers/data_reader.hpp" #include "HugeCTR/include/embeddings/localized_slot_sparse_embedding_one_hot.hpp" #include "gtest/gtest.h" #include "nvToolsExt.h" #include "utest/embedding/embedding_test_utils.hpp" #include "utest/embedding/sparse_embedding_hash_cpu.hpp" #include "utest/test_utils.h" using namespace HugeCTR; using namespace embedding_test; namespace { //--------------------------------------------------------------------------------------- // global params for all testing const int train_batch_num = 10; // can not more than 32 const int test_batch_num = 1; const int train_batchsize = 1024; const int test_batchsize = 2560; const int slot_num = 26; const int max_nnz_per_slot = 1; const int max_feature_num = max_nnz_per_slot * slot_num; // max_feature_num in a sample const long long vocabulary_size = slot_num * 100; const int embedding_vec_size = 128; const int combiner = 0; // 0-sum, 1-mean const long long label_dim = 1; const long long dense_dim = 0; typedef long long T; const float scaler = 1.0f; // used in mixed precision training const float lr = 0.01f; // In order to not allocate the total size of hash table on each GPU, the users need to set the // size of max_vocabulary_size_per_gpu, which should be more than vocabulary_size/gpu_count, // eg: 1.25x of that. const int num_chunk_threads = 1; // must be 1 for CPU and GPU results comparation const int num_files = 1; const Check_t CHK = Check_t::Sum; // Check_t::Sum const char *train_file_list_name = "train_file_list.txt"; const char *test_file_list_name = "test_file_list.txt"; const char *prefix = "./data_reader_test_data/temp_dataset_"; #ifndef NCCl_A2A const std::string plan_file(PROJECT_HOME_ + "utest/all2all_plan_dgx_{0,1,2,3,4,5,6,7}.json"); #else const std::string plan_file = ""; #endif const char *hash_table_file_name = "localized_hash_table.bin"; // std::vector<size_t> slot_sizes; // null means use vocabulary_size/gpu_count/load_factor as // max_vocabulary_size_per_gpu // CAUSION: must match vocabulary_size // std::vector<size_t> slot_sizes = {39884406,39043,17289,7420,20263,3,7120,1543,63,38532951, // 2953546,403346,10,2208,11938,155,4,976,14,39979771,25641295,39664984,585935,12972,108,36}; // // for cretio dataset std::vector<size_t> slot_sizes = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; // just for verify //----------------------------------------------------------------------------------------- template <typename TypeEmbeddingComp> void train_and_test(const std::vector<int> &device_list, const Optimizer_t &optimizer, const Update_t &update_type) { OptHyperParams<TypeEmbeddingComp> hyper_params; hyper_params.sgd.atomic_update = true; const OptParams<TypeEmbeddingComp> opt_params = {optimizer, lr, hyper_params, update_type, scaler}; float tolerance; if (std::is_same<TypeEmbeddingComp, __half>::value) { tolerance = 5e-3f; } else { tolerance = 1e-4f; } test::mpi_init(); int numprocs = 1; #ifdef ENABLE_MPI MPI_Comm_size(MPI_COMM_WORLD, &numprocs); #endif // if there are multi-node, we assume each node has the same gpu device_list std::vector<std::vector<int>> vvgpu; for (int i = 0; i < numprocs; i++) { vvgpu.push_back(device_list); } const auto &resource_manager = ResourceManager::create(vvgpu, 0); if (resource_manager->get_pid() == 0) { std::cout << "rank " << resource_manager->get_pid() << " is generating data" << std::endl; { // re-generate the dataset files std::ifstream file(train_file_list_name); if (file.good()) { std::remove(train_file_list_name); } } { // re-generate the dataset files std::ifstream file(test_file_list_name); if (file.good()) { std::remove(test_file_list_name); } } // data generation: key's corresponding slot_id=(key%slot_num) if (slot_sizes.size() > 0) { HugeCTR::data_generation_for_localized_test<T, CHK>( train_file_list_name, prefix, num_files, train_batch_num * train_batchsize, slot_num, vocabulary_size, label_dim, dense_dim, max_nnz_per_slot, slot_sizes); HugeCTR::data_generation_for_localized_test<T, CHK>( test_file_list_name, prefix, num_files, test_batch_num * test_batchsize, slot_num, vocabulary_size, label_dim, dense_dim, max_nnz_per_slot, slot_sizes); } else { CK_THROW_( Error_t::WrongInput, "Must set slot_sizes since there is no hashtable in LocalizedSlotSpasrseEmbeddingOneHot"); } } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); std::cout << "This is rank: " << resource_manager->get_pid() << std::endl; #endif // setup a data reader const DataReaderSparseParam param = {DataReaderSparse_t::Localized, max_nnz_per_slot * slot_num, max_nnz_per_slot, slot_num}; std::vector<DataReaderSparseParam> params; params.push_back(param); std::unique_ptr<DataReader<T>> train_data_reader(new DataReader<T>( train_batchsize, label_dim, dense_dim, params, resource_manager, true, num_chunk_threads)); train_data_reader->create_drwg_norm(train_file_list_name, CHK); std::unique_ptr<DataReader<T>> test_data_reader(new DataReader<T>( test_batchsize, label_dim, dense_dim, params, resource_manager, true, num_chunk_threads)); test_data_reader->create_drwg_norm(test_file_list_name, CHK); // generate hashtable if (resource_manager->get_pid() == 0) { std::cout << "Init hash table"; // init hash table file: <key, solt_id, value> std::ofstream weight_stream(hash_table_file_name); if (!weight_stream.is_open()) { ERROR_MESSAGE_("Error: file not open for writing"); } // UnifiedDataSimulator<T> ldata_sim(0, slot_num-1); // for slot_id test::UniformDataSimulator fdata_sim; // for value std::unique_ptr<float[]> buf(new float[embedding_vec_size]); for (long long i = 0; i < vocabulary_size; i++) { T key = (T)i; // T key = ldata_sim.get_num(); // CAUSION: can not set random keys here, because we need to ensure that: // 1) we can find keys in the data file from this hash table // 2) there are no repeated keys weight_stream.write((char *)&key, sizeof(T)); T slot_id; if (slot_sizes.size() == 0) { // slot_id = key % slot_num; // CAUSION: need to dedicate the slot_id for each key for // // correctness verification CK_THROW_(Error_t::WrongInput, "Must set slot_sizes since there is no hashtable in " "LocalizedSlotSpasrseEmbeddingOneHot"); } else { size_t offset = 0; for (size_t j = 0; j < slot_sizes.size(); j++) { if ((key >= static_cast<T>(offset)) && (key < static_cast<T>(offset + slot_sizes[j]))) { slot_id = (T)j; break; } offset += slot_sizes[j]; } } weight_stream.write((char *)&slot_id, sizeof(T)); // float val = (float)i; // float val = 0.1f; fdata_sim.fill(buf.get(), embedding_vec_size, -0.1f, 0.1f); weight_stream.write(reinterpret_cast<const char *>(buf.get()), embedding_vec_size * sizeof(float)); } weight_stream.close(); std::cout << " Done" << std::endl; } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif const SparseEmbeddingHashParams<TypeEmbeddingComp> embedding_params = { train_batchsize, test_batchsize, 0, slot_sizes, embedding_vec_size, max_feature_num, slot_num, combiner, opt_params}; std::unique_ptr<Embedding<T, TypeEmbeddingComp>> embedding( new LocalizedSlotSparseEmbeddingOneHot<T, TypeEmbeddingComp>( train_data_reader->get_row_offsets_tensors(), train_data_reader->get_value_tensors(), train_data_reader->get_nnz_array(), test_data_reader->get_row_offsets_tensors(), test_data_reader->get_value_tensors(), test_data_reader->get_nnz_array(), embedding_params, plan_file, resource_manager)); { // upload hash table to device std::ifstream fs(hash_table_file_name); embedding->load_parameters(fs); fs.close(); } // for SparseEmbeddingCpu std::unique_ptr<SparseEmbeddingHashCpu<T, TypeEmbeddingComp>> embedding_cpu( new SparseEmbeddingHashCpu<T, TypeEmbeddingComp>( train_batchsize, max_feature_num, vocabulary_size, embedding_vec_size, slot_num, label_dim, dense_dim, CHK, train_batch_num * train_batchsize, combiner, opt_params, train_file_list_name, hash_table_file_name, SparseEmbedding_t::Localized)); TypeEmbeddingComp *embedding_feature_from_cpu = embedding_cpu->get_forward_results(); TypeEmbeddingComp *wgrad_from_cpu = embedding_cpu->get_backward_results(); T *hash_table_key_from_cpu = embedding_cpu->get_hash_table_key_ptr(); float *hash_table_value_from_cpu = embedding_cpu->get_hash_table_value_ptr(); // for results check std::shared_ptr<GeneralBuffer2<HostAllocator>> buf = GeneralBuffer2<HostAllocator>::create(); Tensor2<TypeEmbeddingComp> embedding_feature_from_gpu; buf->reserve({train_batchsize * slot_num * embedding_vec_size}, &embedding_feature_from_gpu); Tensor2<TypeEmbeddingComp> wgrad_from_gpu; buf->reserve({train_batchsize * slot_num * embedding_vec_size}, &wgrad_from_gpu); Tensor2<TypeEmbeddingComp> embedding_feature_from_gpu_eval; buf->reserve({test_batchsize * slot_num * embedding_vec_size}, &embedding_feature_from_gpu_eval); buf->allocate(); typedef struct TypeHashValue_ { float data[embedding_vec_size]; } TypeHashValue; for (int i = 0; i < train_batch_num; i++) { printf("Rank%d: Round %d start training:\n", resource_manager->get_pid(), i); // call read a batch printf("Rank%d: data_reader->read_a_batch_to_device()\n", resource_manager->get_pid()); train_data_reader->read_a_batch_to_device(); // GPU forward printf("Rank%d: embedding->forward()\n", resource_manager->get_pid()); embedding->forward(true); // check the result of forward printf("Rank%d: embedding->get_forward_results()\n", resource_manager->get_pid()); embedding->get_forward_results(true, embedding_feature_from_gpu); // memcpy from GPU to CPU if (resource_manager->get_pid() == 0) { // CPU forward printf("Rank0: embedding_cpu->forward()\n"); embedding_cpu->forward(); printf("Rank0: check forward results\n"); ASSERT_TRUE(compare_embedding_feature(train_batchsize * slot_num * embedding_vec_size, embedding_feature_from_gpu.get_ptr(), embedding_feature_from_cpu, tolerance)); } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif // GPU backward printf("Rank%d: embedding->backward()\n", resource_manager->get_pid()); embedding->backward(); // check the result of backward printf("Rank%d: embedding->get_backward_results()\n", resource_manager->get_pid()); embedding->get_backward_results(wgrad_from_gpu, 0); if (resource_manager->get_pid() == 0) { // CPU backward printf("Rank0: embedding_cpu->backward()\n"); embedding_cpu->backward(); printf("Rank0: check backward results: GPU and CPU\n"); ASSERT_TRUE(compare_wgrad(train_batchsize * slot_num * embedding_vec_size, wgrad_from_gpu.get_ptr(), wgrad_from_cpu, tolerance)); } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif // GPU update_params printf("Rank%d: embedding->update_params()\n", resource_manager->get_pid()); embedding->update_params(); if (resource_manager->get_pid() == 0) { // CPU update_params printf("Rank0: embedding_cpu->update_params()\n"); embedding_cpu->update_params(); } #ifdef ENABLE_MPI MPI_Barrier(MPI_COMM_WORLD); #endif printf("Rank%d: Round %d end:\n", resource_manager->get_pid(), i); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // create new obj for eval() { std::ofstream fs(hash_table_file_name); embedding->dump_parameters(fs); fs.close(); } // for SparseEmbeddingCpu eval std::unique_ptr<SparseEmbeddingHashCpu<T, TypeEmbeddingComp>> test_embedding_cpu( new SparseEmbeddingHashCpu<T, TypeEmbeddingComp>( test_batchsize, max_feature_num, vocabulary_size, embedding_vec_size, slot_num, label_dim, dense_dim, CHK, test_batch_num * test_batchsize, combiner, opt_params, test_file_list_name, hash_table_file_name, SparseEmbedding_t::Localized)); TypeEmbeddingComp *embedding_feature_from_cpu_eval = test_embedding_cpu->get_forward_results(); { ///////////////////////////////////////////////////////////////////////////////////////////// // eval printf("\nRank%d: Round start eval:\n", resource_manager->get_pid()); // call read a batch printf("Rank%d: data_reader_eval->read_a_batch_to_device()\n", resource_manager->get_pid()); test_data_reader->read_a_batch_to_device(); // GPU forward printf("Rank%d: embedding_eval->forward()\n", resource_manager->get_pid()); embedding->forward(false); // check the result of forward printf("Rank%d: embedding_eval->get_forward_results()\n", resource_manager->get_pid()); embedding->get_forward_results(false, embedding_feature_from_gpu_eval); // memcpy from GPU to CPU if (resource_manager->get_pid() == 0) { // CPU forward printf("Rank0: embedding_cpu_eval->forward()\n"); test_embedding_cpu->forward(); printf("Rank0: check forward results\n"); ASSERT_TRUE(compare_embedding_feature(test_batchsize * slot_num * embedding_vec_size, embedding_feature_from_gpu_eval.get_ptr(), embedding_feature_from_cpu_eval, tolerance)); } } test::mpi_finalize(); } } // namespace TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_1gpu) { train_and_test<float>({0}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_4gpu) { train_and_test<float>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_global_update_1gpu) { train_and_test<float>({0}, Optimizer_t::SGD, Update_t::Global); } TEST(localized_sparse_embedding_one_hot_test, fp32_sgd_global_update_4gpu) { train_and_test<float>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Global); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_1gpu) { train_and_test<__half>({0}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_4gpu) { train_and_test<__half>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Local); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_global_update_1gpu) { train_and_test<__half>({0}, Optimizer_t::SGD, Update_t::Global); } TEST(localized_sparse_embedding_one_hot_test, fp16_sgd_global_update_4gpu) { train_and_test<__half>({0, 1, 2, 3}, Optimizer_t::SGD, Update_t::Global); }
3ef1c4b2497879b368dc0d81a589f421b1e003db.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" // Includes, system // prototype function //int rnd_asg(unsigned short int*, unsigned int*, int, int); //void criterion_part(float*, unsigned short int*, unsigned int*, float*, float*, float*, float*, int, int, int); // ERROR system #define EXIT_OK (0) #define ERROR_HOST_MEM (1) #define ERROR_DEVICE_MEM (2) #define ERROR_DEVICE (3) #define ERROR_INIT (4) #define ERROR_EMPTY (5) #define ERROR_SETDEVICE (6) #define EXIT_DONE (255) // kernel to calculate the exp __global__ void kmeans_exp_kernel(float* DIST, float pw) { register int idx = blockIdx.x * blockDim.x + threadIdx.x; register float arg = DIST[idx] * pw; if (arg < -70) arg = -70; DIST[idx] = exp(arg); }
3ef1c4b2497879b368dc0d81a589f421b1e003db.cu
#include "includes.h" // Includes, system // prototype function //int rnd_asg(unsigned short int*, unsigned int*, int, int); //void criterion_part(float*, unsigned short int*, unsigned int*, float*, float*, float*, float*, int, int, int); // ERROR system #define EXIT_OK (0) #define ERROR_HOST_MEM (1) #define ERROR_DEVICE_MEM (2) #define ERROR_DEVICE (3) #define ERROR_INIT (4) #define ERROR_EMPTY (5) #define ERROR_SETDEVICE (6) #define EXIT_DONE (255) // kernel to calculate the exp __global__ void kmeans_exp_kernel(float* DIST, float pw) { register int idx = blockIdx.x * blockDim.x + threadIdx.x; register float arg = DIST[idx] * pw; if (arg < -70) arg = -70; DIST[idx] = exp(arg); }
641de190ba67e1842fe1ecab5a76b13c7054e0e2.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* Udacity HW5 Histogramming for Speed The goal of this assignment is compute a histogram as fast as possible. We have simplified the problem as much as possible to allow you to focus solely on the histogramming algorithm. The input values that you need to histogram are already the exact bins that need to be updated. This is unlike in HW3 where you needed to compute the range of the data and then do: bin = (val - valMin) / valRange to determine the bin. Here the bin is just: bin = val so the serial histogram calculation looks like: for (i = 0; i < numElems; ++i) histo[val[i]]++; That's it! Your job is to make it run as fast as possible! The values are normally distributed - you may take advantage of this fact in your implementation. */ #include "utils.h" __global__ void yourHisto(const unsigned int* const vals, //INPUT unsigned int* const histo, //OUPUT int numVals) { //TODO fill in this kernel to calculate the histogram //as quickly as possible int myId = blockDim.x * blockIdx.x + threadIdx.x; if(myId >= numVals) return; unsigned int bin = vals[myId]; atomicAdd(&histo[bin],1); //Although we provide only one kernel skeleton, //feel free to use more if it will help you //write faster code } void computeHistogram(const unsigned int* const d_vals, //INPUT unsigned int* const d_histo, //OUTPUT const unsigned int numBins, const unsigned int numElems) { //TODO Launch the yourHisto kernel dim3 blockSize(256, 1, 1); dim3 gridSize((numElems + blockSize.x -1)/ blockSize.x, 1, 1); unsigned int * d_histogram; unsigned int size = sizeof(unsigned int) * numBins; checkCudaErrors(hipMalloc(&d_histogram, size)); checkCudaErrors(hipMemcpy(d_histogram, d_histo, size, hipMemcpyDeviceToDevice)); hipLaunchKernelGGL(( yourHisto), dim3(gridSize), dim3(blockSize), 0, 0, d_vals, d_histogram, numElems); checkCudaErrors(hipMemcpy(d_histo, d_histogram, size, hipMemcpyDeviceToDevice)); //if you want to use/launch more than one kernel, //feel free hipDeviceSynchronize(); checkCudaErrors(hipGetLastError()); }
641de190ba67e1842fe1ecab5a76b13c7054e0e2.cu
/* Udacity HW5 Histogramming for Speed The goal of this assignment is compute a histogram as fast as possible. We have simplified the problem as much as possible to allow you to focus solely on the histogramming algorithm. The input values that you need to histogram are already the exact bins that need to be updated. This is unlike in HW3 where you needed to compute the range of the data and then do: bin = (val - valMin) / valRange to determine the bin. Here the bin is just: bin = val so the serial histogram calculation looks like: for (i = 0; i < numElems; ++i) histo[val[i]]++; That's it! Your job is to make it run as fast as possible! The values are normally distributed - you may take advantage of this fact in your implementation. */ #include "utils.h" __global__ void yourHisto(const unsigned int* const vals, //INPUT unsigned int* const histo, //OUPUT int numVals) { //TODO fill in this kernel to calculate the histogram //as quickly as possible int myId = blockDim.x * blockIdx.x + threadIdx.x; if(myId >= numVals) return; unsigned int bin = vals[myId]; atomicAdd(&histo[bin],1); //Although we provide only one kernel skeleton, //feel free to use more if it will help you //write faster code } void computeHistogram(const unsigned int* const d_vals, //INPUT unsigned int* const d_histo, //OUTPUT const unsigned int numBins, const unsigned int numElems) { //TODO Launch the yourHisto kernel dim3 blockSize(256, 1, 1); dim3 gridSize((numElems + blockSize.x -1)/ blockSize.x, 1, 1); unsigned int * d_histogram; unsigned int size = sizeof(unsigned int) * numBins; checkCudaErrors(cudaMalloc(&d_histogram, size)); checkCudaErrors(cudaMemcpy(d_histogram, d_histo, size, cudaMemcpyDeviceToDevice)); yourHisto<<<gridSize, blockSize>>>(d_vals, d_histogram, numElems); checkCudaErrors(cudaMemcpy(d_histo, d_histogram, size, cudaMemcpyDeviceToDevice)); //if you want to use/launch more than one kernel, //feel free cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError()); }
ee9c19e74ebcb1e4b1660f9be492afac5ee5d0c0.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <paddle/fluid/memory/allocation/allocator.h> #include <stdio.h> #include <string> #include <vector> #include "paddle/fluid/framework/mixed_vector.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/memory/memory.h" #include "paddle/fluid/operators/detection/bbox_util.cu.h" #include "paddle/phi/kernels/funcs/gather.cu.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; using LoDTensor = framework::LoDTensor; namespace { template <typename T> static std::pair<Tensor, Tensor> ProposalForOneImage( const platform::CUDADeviceContext &ctx, const Tensor &im_info, const Tensor &anchors, const Tensor &variances, const Tensor &bbox_deltas, // [M, 4] const Tensor &scores, // [N, 1] int pre_nms_top_n, int post_nms_top_n, float nms_thresh, float min_size, float eta) { // 1. pre nms Tensor scores_sort, index_sort; SortDescending<T>(ctx, scores, &scores_sort, &index_sort); int num = scores.numel(); int pre_nms_num = (pre_nms_top_n <= 0 || pre_nms_top_n > num) ? scores.numel() : pre_nms_top_n; scores_sort.Resize({pre_nms_num, 1}); index_sort.Resize({pre_nms_num, 1}); // 2. box decode and clipping Tensor proposals; proposals.mutable_data<T>({pre_nms_num, 4}, ctx.GetPlace()); { platform::ForRange<platform::CUDADeviceContext> for_range(ctx, pre_nms_num); for_range(BoxDecodeAndClipFunctor<T>{ anchors.data<T>(), bbox_deltas.data<T>(), variances.data<T>(), index_sort.data<int>(), im_info.data<T>(), proposals.data<T>()}); } // 3. filter Tensor keep_index, keep_num_t; keep_index.mutable_data<int>({pre_nms_num}, ctx.GetPlace()); keep_num_t.mutable_data<int>({1}, ctx.GetPlace()); min_size = ::max(min_size, 1.0f); auto stream = ctx.stream(); hipLaunchKernelGGL(( FilterBBoxes<T, 512>), dim3(1), dim3(512), 0, stream, proposals.data<T>(), im_info.data<T>(), min_size, pre_nms_num, keep_num_t.data<int>(), keep_index.data<int>()); int keep_num; const auto gpu_place = ctx.GetPlace(); memory::Copy(platform::CPUPlace(), &keep_num, gpu_place, keep_num_t.data<int>(), sizeof(int), ctx.stream()); ctx.Wait(); keep_index.Resize({keep_num}); Tensor scores_filter, proposals_filter; // Handle the case when there is no keep index left if (keep_num == 0) { phi::funcs::SetConstant<platform::CUDADeviceContext, T> set_zero; proposals_filter.mutable_data<T>({1, 4}, ctx.GetPlace()); scores_filter.mutable_data<T>({1, 1}, ctx.GetPlace()); set_zero(ctx, &proposals_filter, static_cast<T>(0)); set_zero(ctx, &scores_filter, static_cast<T>(0)); return std::make_pair(proposals_filter, scores_filter); } proposals_filter.mutable_data<T>({keep_num, 4}, ctx.GetPlace()); scores_filter.mutable_data<T>({keep_num, 1}, ctx.GetPlace()); phi::funcs::GPUGather<T>(ctx, proposals, keep_index, &proposals_filter); phi::funcs::GPUGather<T>(ctx, scores_sort, keep_index, &scores_filter); if (nms_thresh <= 0) { return std::make_pair(proposals_filter, scores_filter); } // 4. nms Tensor keep_nms; NMS<T>(ctx, proposals_filter, keep_index, nms_thresh, &keep_nms); if (post_nms_top_n > 0 && post_nms_top_n < keep_nms.numel()) { keep_nms.Resize({post_nms_top_n}); } Tensor scores_nms, proposals_nms; proposals_nms.mutable_data<T>({keep_nms.numel(), 4}, ctx.GetPlace()); scores_nms.mutable_data<T>({keep_nms.numel(), 1}, ctx.GetPlace()); phi::funcs::GPUGather<T>(ctx, proposals_filter, keep_nms, &proposals_nms); phi::funcs::GPUGather<T>(ctx, scores_filter, keep_nms, &scores_nms); return std::make_pair(proposals_nms, scores_nms); } } // namespace template <typename DeviceContext, typename T> class CUDAGenerateProposalsKernel : public framework::OpKernel<T> { public: void Compute(const framework::ExecutionContext &context) const override { auto *scores = context.Input<Tensor>("Scores"); auto *bbox_deltas = context.Input<Tensor>("BboxDeltas"); auto *im_info = context.Input<Tensor>("ImInfo"); auto anchors = GET_DATA_SAFELY(context.Input<Tensor>("Anchors"), "Input", "Anchors", "GenerateProposals"); auto variances = GET_DATA_SAFELY(context.Input<Tensor>("Variances"), "Input", "Variances", "GenerateProposals"); auto *rpn_rois = context.Output<LoDTensor>("RpnRois"); auto *rpn_roi_probs = context.Output<LoDTensor>("RpnRoiProbs"); int pre_nms_top_n = context.Attr<int>("pre_nms_topN"); int post_nms_top_n = context.Attr<int>("post_nms_topN"); float nms_thresh = context.Attr<float>("nms_thresh"); float min_size = context.Attr<float>("min_size"); float eta = context.Attr<float>("eta"); PADDLE_ENFORCE_GE(eta, 1., platform::errors::InvalidArgument( "Not support adaptive NMS. The attribute 'eta' " "should not less than 1. But received eta=[%d]", eta)); auto &dev_ctx = context.template device_context<DeviceContext>(); auto scores_dim = scores->dims(); int64_t num = scores_dim[0]; int64_t c_score = scores_dim[1]; int64_t h_score = scores_dim[2]; int64_t w_score = scores_dim[3]; auto bbox_dim = bbox_deltas->dims(); int64_t c_bbox = bbox_dim[1]; int64_t h_bbox = bbox_dim[2]; int64_t w_bbox = bbox_dim[3]; Tensor bbox_deltas_swap, scores_swap; bbox_deltas_swap.mutable_data<T>({num, h_bbox, w_bbox, c_bbox}, dev_ctx.GetPlace()); scores_swap.mutable_data<T>({num, h_score, w_score, c_score}, dev_ctx.GetPlace()); phi::funcs::Transpose<DeviceContext, T, 4> trans; std::vector<int> axis = {0, 2, 3, 1}; trans(dev_ctx, *bbox_deltas, &bbox_deltas_swap, axis); trans(dev_ctx, *scores, &scores_swap, axis); anchors.Resize({anchors.numel() / 4, 4}); variances.Resize({variances.numel() / 4, 4}); rpn_rois->mutable_data<T>({bbox_deltas->numel() / 4, 4}, context.GetPlace()); rpn_roi_probs->mutable_data<T>({scores->numel(), 1}, context.GetPlace()); T *rpn_rois_data = rpn_rois->data<T>(); T *rpn_roi_probs_data = rpn_roi_probs->data<T>(); auto place = dev_ctx.GetPlace(); auto cpu_place = platform::CPUPlace(); int64_t num_proposals = 0; std::vector<size_t> offset(1, 0); std::vector<int> tmp_num; for (int64_t i = 0; i < num; ++i) { Tensor im_info_slice = im_info->Slice(i, i + 1); Tensor bbox_deltas_slice = bbox_deltas_swap.Slice(i, i + 1); Tensor scores_slice = scores_swap.Slice(i, i + 1); bbox_deltas_slice.Resize({h_bbox * w_bbox * c_bbox / 4, 4}); scores_slice.Resize({h_score * w_score * c_score, 1}); std::pair<Tensor, Tensor> box_score_pair = ProposalForOneImage<T>(dev_ctx, im_info_slice, anchors, variances, bbox_deltas_slice, scores_slice, pre_nms_top_n, post_nms_top_n, nms_thresh, min_size, eta); Tensor &proposals = box_score_pair.first; Tensor &scores = box_score_pair.second; memory::Copy(place, rpn_rois_data + num_proposals * 4, place, proposals.data<T>(), sizeof(T) * proposals.numel(), dev_ctx.stream()); memory::Copy(place, rpn_roi_probs_data + num_proposals, place, scores.data<T>(), sizeof(T) * scores.numel(), dev_ctx.stream()); num_proposals += proposals.dims()[0]; offset.emplace_back(num_proposals); tmp_num.push_back(proposals.dims()[0]); } if (context.HasOutput("RpnRoisNum")) { auto *rpn_rois_num = context.Output<Tensor>("RpnRoisNum"); rpn_rois_num->mutable_data<int>({num}, context.GetPlace()); int *num_data = rpn_rois_num->data<int>(); memory::Copy(place, num_data, cpu_place, &tmp_num[0], sizeof(int) * num, dev_ctx.stream()); rpn_rois_num->Resize({num}); } framework::LoD lod; lod.emplace_back(offset); rpn_rois->set_lod(lod); rpn_roi_probs->set_lod(lod); rpn_rois->Resize({num_proposals, 4}); rpn_roi_probs->Resize({num_proposals, 1}); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL(generate_proposals, ops::CUDAGenerateProposalsKernel< paddle::platform::CUDADeviceContext, float>);
ee9c19e74ebcb1e4b1660f9be492afac5ee5d0c0.cu
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <paddle/fluid/memory/allocation/allocator.h> #include <stdio.h> #include <string> #include <vector> #include "paddle/fluid/framework/mixed_vector.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/memory/memory.h" #include "paddle/fluid/operators/detection/bbox_util.cu.h" #include "paddle/phi/kernels/funcs/gather.cu.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; using LoDTensor = framework::LoDTensor; namespace { template <typename T> static std::pair<Tensor, Tensor> ProposalForOneImage( const platform::CUDADeviceContext &ctx, const Tensor &im_info, const Tensor &anchors, const Tensor &variances, const Tensor &bbox_deltas, // [M, 4] const Tensor &scores, // [N, 1] int pre_nms_top_n, int post_nms_top_n, float nms_thresh, float min_size, float eta) { // 1. pre nms Tensor scores_sort, index_sort; SortDescending<T>(ctx, scores, &scores_sort, &index_sort); int num = scores.numel(); int pre_nms_num = (pre_nms_top_n <= 0 || pre_nms_top_n > num) ? scores.numel() : pre_nms_top_n; scores_sort.Resize({pre_nms_num, 1}); index_sort.Resize({pre_nms_num, 1}); // 2. box decode and clipping Tensor proposals; proposals.mutable_data<T>({pre_nms_num, 4}, ctx.GetPlace()); { platform::ForRange<platform::CUDADeviceContext> for_range(ctx, pre_nms_num); for_range(BoxDecodeAndClipFunctor<T>{ anchors.data<T>(), bbox_deltas.data<T>(), variances.data<T>(), index_sort.data<int>(), im_info.data<T>(), proposals.data<T>()}); } // 3. filter Tensor keep_index, keep_num_t; keep_index.mutable_data<int>({pre_nms_num}, ctx.GetPlace()); keep_num_t.mutable_data<int>({1}, ctx.GetPlace()); min_size = std::max(min_size, 1.0f); auto stream = ctx.stream(); FilterBBoxes<T, 512><<<1, 512, 0, stream>>>( proposals.data<T>(), im_info.data<T>(), min_size, pre_nms_num, keep_num_t.data<int>(), keep_index.data<int>()); int keep_num; const auto gpu_place = ctx.GetPlace(); memory::Copy(platform::CPUPlace(), &keep_num, gpu_place, keep_num_t.data<int>(), sizeof(int), ctx.stream()); ctx.Wait(); keep_index.Resize({keep_num}); Tensor scores_filter, proposals_filter; // Handle the case when there is no keep index left if (keep_num == 0) { phi::funcs::SetConstant<platform::CUDADeviceContext, T> set_zero; proposals_filter.mutable_data<T>({1, 4}, ctx.GetPlace()); scores_filter.mutable_data<T>({1, 1}, ctx.GetPlace()); set_zero(ctx, &proposals_filter, static_cast<T>(0)); set_zero(ctx, &scores_filter, static_cast<T>(0)); return std::make_pair(proposals_filter, scores_filter); } proposals_filter.mutable_data<T>({keep_num, 4}, ctx.GetPlace()); scores_filter.mutable_data<T>({keep_num, 1}, ctx.GetPlace()); phi::funcs::GPUGather<T>(ctx, proposals, keep_index, &proposals_filter); phi::funcs::GPUGather<T>(ctx, scores_sort, keep_index, &scores_filter); if (nms_thresh <= 0) { return std::make_pair(proposals_filter, scores_filter); } // 4. nms Tensor keep_nms; NMS<T>(ctx, proposals_filter, keep_index, nms_thresh, &keep_nms); if (post_nms_top_n > 0 && post_nms_top_n < keep_nms.numel()) { keep_nms.Resize({post_nms_top_n}); } Tensor scores_nms, proposals_nms; proposals_nms.mutable_data<T>({keep_nms.numel(), 4}, ctx.GetPlace()); scores_nms.mutable_data<T>({keep_nms.numel(), 1}, ctx.GetPlace()); phi::funcs::GPUGather<T>(ctx, proposals_filter, keep_nms, &proposals_nms); phi::funcs::GPUGather<T>(ctx, scores_filter, keep_nms, &scores_nms); return std::make_pair(proposals_nms, scores_nms); } } // namespace template <typename DeviceContext, typename T> class CUDAGenerateProposalsKernel : public framework::OpKernel<T> { public: void Compute(const framework::ExecutionContext &context) const override { auto *scores = context.Input<Tensor>("Scores"); auto *bbox_deltas = context.Input<Tensor>("BboxDeltas"); auto *im_info = context.Input<Tensor>("ImInfo"); auto anchors = GET_DATA_SAFELY(context.Input<Tensor>("Anchors"), "Input", "Anchors", "GenerateProposals"); auto variances = GET_DATA_SAFELY(context.Input<Tensor>("Variances"), "Input", "Variances", "GenerateProposals"); auto *rpn_rois = context.Output<LoDTensor>("RpnRois"); auto *rpn_roi_probs = context.Output<LoDTensor>("RpnRoiProbs"); int pre_nms_top_n = context.Attr<int>("pre_nms_topN"); int post_nms_top_n = context.Attr<int>("post_nms_topN"); float nms_thresh = context.Attr<float>("nms_thresh"); float min_size = context.Attr<float>("min_size"); float eta = context.Attr<float>("eta"); PADDLE_ENFORCE_GE(eta, 1., platform::errors::InvalidArgument( "Not support adaptive NMS. The attribute 'eta' " "should not less than 1. But received eta=[%d]", eta)); auto &dev_ctx = context.template device_context<DeviceContext>(); auto scores_dim = scores->dims(); int64_t num = scores_dim[0]; int64_t c_score = scores_dim[1]; int64_t h_score = scores_dim[2]; int64_t w_score = scores_dim[3]; auto bbox_dim = bbox_deltas->dims(); int64_t c_bbox = bbox_dim[1]; int64_t h_bbox = bbox_dim[2]; int64_t w_bbox = bbox_dim[3]; Tensor bbox_deltas_swap, scores_swap; bbox_deltas_swap.mutable_data<T>({num, h_bbox, w_bbox, c_bbox}, dev_ctx.GetPlace()); scores_swap.mutable_data<T>({num, h_score, w_score, c_score}, dev_ctx.GetPlace()); phi::funcs::Transpose<DeviceContext, T, 4> trans; std::vector<int> axis = {0, 2, 3, 1}; trans(dev_ctx, *bbox_deltas, &bbox_deltas_swap, axis); trans(dev_ctx, *scores, &scores_swap, axis); anchors.Resize({anchors.numel() / 4, 4}); variances.Resize({variances.numel() / 4, 4}); rpn_rois->mutable_data<T>({bbox_deltas->numel() / 4, 4}, context.GetPlace()); rpn_roi_probs->mutable_data<T>({scores->numel(), 1}, context.GetPlace()); T *rpn_rois_data = rpn_rois->data<T>(); T *rpn_roi_probs_data = rpn_roi_probs->data<T>(); auto place = dev_ctx.GetPlace(); auto cpu_place = platform::CPUPlace(); int64_t num_proposals = 0; std::vector<size_t> offset(1, 0); std::vector<int> tmp_num; for (int64_t i = 0; i < num; ++i) { Tensor im_info_slice = im_info->Slice(i, i + 1); Tensor bbox_deltas_slice = bbox_deltas_swap.Slice(i, i + 1); Tensor scores_slice = scores_swap.Slice(i, i + 1); bbox_deltas_slice.Resize({h_bbox * w_bbox * c_bbox / 4, 4}); scores_slice.Resize({h_score * w_score * c_score, 1}); std::pair<Tensor, Tensor> box_score_pair = ProposalForOneImage<T>(dev_ctx, im_info_slice, anchors, variances, bbox_deltas_slice, scores_slice, pre_nms_top_n, post_nms_top_n, nms_thresh, min_size, eta); Tensor &proposals = box_score_pair.first; Tensor &scores = box_score_pair.second; memory::Copy(place, rpn_rois_data + num_proposals * 4, place, proposals.data<T>(), sizeof(T) * proposals.numel(), dev_ctx.stream()); memory::Copy(place, rpn_roi_probs_data + num_proposals, place, scores.data<T>(), sizeof(T) * scores.numel(), dev_ctx.stream()); num_proposals += proposals.dims()[0]; offset.emplace_back(num_proposals); tmp_num.push_back(proposals.dims()[0]); } if (context.HasOutput("RpnRoisNum")) { auto *rpn_rois_num = context.Output<Tensor>("RpnRoisNum"); rpn_rois_num->mutable_data<int>({num}, context.GetPlace()); int *num_data = rpn_rois_num->data<int>(); memory::Copy(place, num_data, cpu_place, &tmp_num[0], sizeof(int) * num, dev_ctx.stream()); rpn_rois_num->Resize({num}); } framework::LoD lod; lod.emplace_back(offset); rpn_rois->set_lod(lod); rpn_roi_probs->set_lod(lod); rpn_rois->Resize({num_proposals, 4}); rpn_roi_probs->Resize({num_proposals, 1}); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL(generate_proposals, ops::CUDAGenerateProposalsKernel< paddle::platform::CUDADeviceContext, float>);
3272b223bbfd579e98f489009dcb82f8a0d604b6.hip
// !!! This is a file automatically generated by hipify!!! #include <math.h> #include "hip/hip_runtime.h" #include "device_launch_parameters.h" #include "DoCuda.h" #include "Teapot.cuh" __device__ DevTeapot::DevTeapot(DevNode** const root, const Sp Color) : DevNode(root, TEAPOT, Color) { } __device__ bool DevTeapot::IsInside(const Sp& L) const { return (sqrt(L * L) <= 1.0); } __device__ bool DevTeapot::GetInfo(const Sp& K, const Sp& L, DevInfo& info, const DevInfo* pHint, bool fromOutSide) const { float a = K * K; float b = K * L; float c = L * L - 1.0f; float bb_ac = b*b - a*c; if (bb_ac < 0) return FALSE; float t; float t1 = (-b + sqrt(bb_ac)) / a; float t2 = (-b - sqrt(bb_ac)) / a; info.isEnter = 0; if (t1 > 0) { if (t2 > 0) { t = (t1 < t2) ? t1 : t2; info.isEnter = 1; } else t = t1; } else { if (t2 > 0) t = t2; else return FALSE; } info.Cross = info.Vertical = K * t + L; info.Distance = t * sqrt(K * K); float x, y, z, th, phy; x = info.Vertical.x; y = info.Vertical.y; z = info.Vertical.z; th = atan2f(y, sqrtf(x*x + z*z)) / M_PI + .5f; phy = atan2f(x, -z) / (2 * M_PI) + .5f; info.Material = GetPixel(phy, th).getMaterial(); info.pNode = this; return TRUE; } __global__ void newTeapot(DevNode** out, DevNode** const root, const D3DMATERIAL9 Material) { if (blockIdx.x == 0 && blockIdx.y == 0 && blockIdx.z == 0 && threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0) *out = new DevTeapot(root, Sp(Material)); } bool newDevTeapot(DevNode*** out, DevNode** const root, const D3DMATERIAL9 Material) { if (!mallocDev(out)) return false; hipLaunchKernelGGL(( newTeapot), dim3(1), dim3(1), 0, 0, *out, root, Material); // Check for any errors launching the kernel hipError_t cudaStatus = hipGetLastError(); if (cudaStatus != hipSuccess) { return false; } return true; }
3272b223bbfd579e98f489009dcb82f8a0d604b6.cu
#include <math.h> #include "cuda_runtime.h" #include "device_launch_parameters.h" #include "DoCuda.h" #include "Teapot.cuh" __device__ DevTeapot::DevTeapot(DevNode** const root, const Sp Color) : DevNode(root, TEAPOT, Color) { } __device__ bool DevTeapot::IsInside(const Sp& L) const { return (sqrt(L * L) <= 1.0); } __device__ bool DevTeapot::GetInfo(const Sp& K, const Sp& L, DevInfo& info, const DevInfo* pHint, bool fromOutSide) const { float a = K * K; float b = K * L; float c = L * L - 1.0f; float bb_ac = b*b - a*c; if (bb_ac < 0) return FALSE; float t; float t1 = (-b + sqrt(bb_ac)) / a; float t2 = (-b - sqrt(bb_ac)) / a; info.isEnter = 0; if (t1 > 0) { if (t2 > 0) { t = (t1 < t2) ? t1 : t2; info.isEnter = 1; } else t = t1; } else { if (t2 > 0) t = t2; else return FALSE; } info.Cross = info.Vertical = K * t + L; info.Distance = t * sqrt(K * K); float x, y, z, th, phy; x = info.Vertical.x; y = info.Vertical.y; z = info.Vertical.z; th = atan2f(y, sqrtf(x*x + z*z)) / M_PI + .5f; phy = atan2f(x, -z) / (2 * M_PI) + .5f; info.Material = GetPixel(phy, th).getMaterial(); info.pNode = this; return TRUE; } __global__ void newTeapot(DevNode** out, DevNode** const root, const D3DMATERIAL9 Material) { if (blockIdx.x == 0 && blockIdx.y == 0 && blockIdx.z == 0 && threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0) *out = new DevTeapot(root, Sp(Material)); } bool newDevTeapot(DevNode*** out, DevNode** const root, const D3DMATERIAL9 Material) { if (!mallocDev(out)) return false; newTeapot<<<1, 1>>>(*out, root, Material); // Check for any errors launching the kernel cudaError_t cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { return false; } return true; }
4482da6b0826207d25bf3ddc9009e989aff7835e.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /******************************************************************************* * Copyright (c) 2015-2018 Skymind, Inc. * Copyright (c) 2019 Konduit K.K. * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author Yurii Shyrma ([email protected]) // #include <ops/declarable/helpers/convolutions.h> #include <helpers/PointersManager.h> namespace sd { namespace ops { ////////////////////////////////////////////////////////////////////////// template <typename T> __global__ static void upsampling2dBPCuda(const void* vx, const Nd4jLong* xShapeInfo, void* vz, const Nd4jLong* zShapeInfo, const bool isNCHW) { // x (gradO) has shape [bS, iC, factorH*iH, factorW*iW ] (NCHW) or [bS, factorH*iH, factorW*iW, iC] (NHWC) // z (gradI) has shape [bS, iC, iH, iW] (NCHW) or [bS, iH, iW, iC] (NHWC) const T* x = reinterpret_cast<const T*>(vx); T* z = reinterpret_cast<T*>(vz); __shared__ int rank, dimIH; __shared__ uint factorH, factorW; __shared__ Nd4jLong zLen, *sharedMem; if (threadIdx.x == 0) { extern __shared__ unsigned char shmem[]; sharedMem = reinterpret_cast<Nd4jLong*>(shmem); dimIH = isNCHW ? 2 : 1; zLen = shape::length(zShapeInfo); rank = 4; factorH = xShapeInfo[dimIH + 1] / zShapeInfo[dimIH + 1]; factorW = xShapeInfo[dimIH + 2] / zShapeInfo[dimIH + 2]; } __syncthreads(); const auto zInd = threadIdx.x + blockIdx.x * blockDim.x; if(zInd >= zLen) return; auto coords = sharedMem + threadIdx.x * rank; shape::index2coords(zInd, zShapeInfo, coords); const auto zOffset = shape::getOffset(zShapeInfo, coords); z[zOffset] = 0; const Nd4jLong zCoord2 = coords[dimIH] * factorH; const Nd4jLong zCoord3 = coords[dimIH + 1] * factorW; for(coords[dimIH] = zCoord2; coords[dimIH] < zCoord2 + factorH; ++coords[dimIH]) for(coords[dimIH + 1] = zCoord3; coords[dimIH + 1] < zCoord3 + factorW; ++coords[dimIH + 1]) z[zOffset] += x[shape::getOffset(xShapeInfo, coords)]; } ////////////////////////////////////////////////////////////////////////// template <typename T> static void upsampling2dBPCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const int sharedMem, const hipStream_t *stream, const void* vx, const Nd4jLong* xShapeInfo, void* vz, const Nd4jLong* zShapeInfo, const bool isNCHW) { hipLaunchKernelGGL(( upsampling2dBPCuda<T>), dim3(blocksPerGrid), dim3(threadsPerBlock), sharedMem, *stream, vx, xShapeInfo, vz, zShapeInfo, isNCHW); } ////////////////////////////////////////////////////////////////////////// void ConvolutionUtils::upsampling2dBP(sd::graph::Context& block, const NDArray& gradO, NDArray& gradI, const bool isNCHW) { PointersManager manager(block.launchContext(), "upsampling2d_bp"); const int threadsPerBlock = MAX_NUM_THREADS / 2; const int blocksPerGrid = (gradI.lengthOf() + threadsPerBlock - 1) / threadsPerBlock; const int sharedMem = gradI.rankOf() * sizeof(Nd4jLong) * threadsPerBlock + 128; NDArray::prepareSpecialUse({&gradI}, {&gradO}); BUILD_SINGLE_SELECTOR(gradI.dataType(), upsampling2dBPCudaLauncher, (blocksPerGrid, threadsPerBlock, sharedMem, block.launchContext()->getCudaStream(), gradO.specialBuffer(), gradO.specialShapeInfo(), gradI.specialBuffer(), gradI.specialShapeInfo(), isNCHW), FLOAT_TYPES); NDArray::registerSpecialUse({&gradI}, {&gradO}); manager.synchronize(); } } }
4482da6b0826207d25bf3ddc9009e989aff7835e.cu
/******************************************************************************* * Copyright (c) 2015-2018 Skymind, Inc. * Copyright (c) 2019 Konduit K.K. * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author Yurii Shyrma ([email protected]) // #include <ops/declarable/helpers/convolutions.h> #include <helpers/PointersManager.h> namespace sd { namespace ops { ////////////////////////////////////////////////////////////////////////// template <typename T> __global__ static void upsampling2dBPCuda(const void* vx, const Nd4jLong* xShapeInfo, void* vz, const Nd4jLong* zShapeInfo, const bool isNCHW) { // x (gradO) has shape [bS, iC, factorH*iH, factorW*iW ] (NCHW) or [bS, factorH*iH, factorW*iW, iC] (NHWC) // z (gradI) has shape [bS, iC, iH, iW] (NCHW) or [bS, iH, iW, iC] (NHWC) const T* x = reinterpret_cast<const T*>(vx); T* z = reinterpret_cast<T*>(vz); __shared__ int rank, dimIH; __shared__ uint factorH, factorW; __shared__ Nd4jLong zLen, *sharedMem; if (threadIdx.x == 0) { extern __shared__ unsigned char shmem[]; sharedMem = reinterpret_cast<Nd4jLong*>(shmem); dimIH = isNCHW ? 2 : 1; zLen = shape::length(zShapeInfo); rank = 4; factorH = xShapeInfo[dimIH + 1] / zShapeInfo[dimIH + 1]; factorW = xShapeInfo[dimIH + 2] / zShapeInfo[dimIH + 2]; } __syncthreads(); const auto zInd = threadIdx.x + blockIdx.x * blockDim.x; if(zInd >= zLen) return; auto coords = sharedMem + threadIdx.x * rank; shape::index2coords(zInd, zShapeInfo, coords); const auto zOffset = shape::getOffset(zShapeInfo, coords); z[zOffset] = 0; const Nd4jLong zCoord2 = coords[dimIH] * factorH; const Nd4jLong zCoord3 = coords[dimIH + 1] * factorW; for(coords[dimIH] = zCoord2; coords[dimIH] < zCoord2 + factorH; ++coords[dimIH]) for(coords[dimIH + 1] = zCoord3; coords[dimIH + 1] < zCoord3 + factorW; ++coords[dimIH + 1]) z[zOffset] += x[shape::getOffset(xShapeInfo, coords)]; } ////////////////////////////////////////////////////////////////////////// template <typename T> static void upsampling2dBPCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const int sharedMem, const cudaStream_t *stream, const void* vx, const Nd4jLong* xShapeInfo, void* vz, const Nd4jLong* zShapeInfo, const bool isNCHW) { upsampling2dBPCuda<T><<<blocksPerGrid, threadsPerBlock, sharedMem, *stream>>>(vx, xShapeInfo, vz, zShapeInfo, isNCHW); } ////////////////////////////////////////////////////////////////////////// void ConvolutionUtils::upsampling2dBP(sd::graph::Context& block, const NDArray& gradO, NDArray& gradI, const bool isNCHW) { PointersManager manager(block.launchContext(), "upsampling2d_bp"); const int threadsPerBlock = MAX_NUM_THREADS / 2; const int blocksPerGrid = (gradI.lengthOf() + threadsPerBlock - 1) / threadsPerBlock; const int sharedMem = gradI.rankOf() * sizeof(Nd4jLong) * threadsPerBlock + 128; NDArray::prepareSpecialUse({&gradI}, {&gradO}); BUILD_SINGLE_SELECTOR(gradI.dataType(), upsampling2dBPCudaLauncher, (blocksPerGrid, threadsPerBlock, sharedMem, block.launchContext()->getCudaStream(), gradO.specialBuffer(), gradO.specialShapeInfo(), gradI.specialBuffer(), gradI.specialShapeInfo(), isNCHW), FLOAT_TYPES); NDArray::registerSpecialUse({&gradI}, {&gradO}); manager.synchronize(); } } }
1d2157c9411bd678bb3c0236333f38e3ab5a09f1.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // // auto-generated by ops.py // __constant__ int dims_drhoEpudx_kernel [4][1]; static int dims_drhoEpudx_kernel_h [4][1] = {0}; //user function __device__ void drhoEpudx_kernel_gpu(const ACC<double> &rhou_new, const ACC<double>& rho_new, const ACC<double>& rhoE_new, ACC<double> &rhoE_res) { double fni = rhou_new(0) * rhou_new(0) / rho_new(0) ; double p = gam1 * (rhoE_new(0) - 0.5 * fni); fni = (rhoE_new(0) + p) * rhou_new(0) / rho_new(0) ; double fnim1 = rhou_new(-1) * rhou_new(-1) / rho_new(-1); p = gam1 * (rhoE_new(-1) - 0.5 * fnim1); fnim1 = (rhoE_new(-1) + p) * rhou_new(-1) / rho_new(-1); double fnim2 = rhou_new(-2) * rhou_new(-2) / rho_new(-2); p = gam1 * (rhoE_new(-2) - 0.5 * fnim2); fnim2 = (rhoE_new(-2) + p ) * rhou_new(-2) / rho_new(-2); double fnip1 = rhou_new(1) * rhou_new(1) / rho_new(1); p = gam1 * (rhoE_new(1) - 0.5 * fnip1); fnip1 = (rhoE_new(1) + p) * rhou_new(1) / rho_new(1); double fnip2 = rhou_new(2) * rhou_new(2) / rho_new(2); p = gam1 * (rhoE_new(2) - 0.5 * fnip2); fnip2 = (rhoE_new(2) + p) * rhou_new(2) / rho_new(2); double deriv = (fnim2 - fnip2 + 8.0* (fnip1 - fnim1))/(12.00*dx); rhoE_res(0) = deriv; } __global__ void ops_drhoEpudx_kernel( double* __restrict arg0, double* __restrict arg1, double* __restrict arg2, double* __restrict arg3, int size0 ){ int idx_x = blockDim.x * blockIdx.x + threadIdx.x; arg0 += idx_x * 1*1; arg1 += idx_x * 1*1; arg2 += idx_x * 1*1; arg3 += idx_x * 1*1; if (idx_x < size0) { const ACC<double> argp0(arg0); const ACC<double> argp1(arg1); const ACC<double> argp2(arg2); ACC<double> argp3(arg3); drhoEpudx_kernel_gpu(argp0, argp1, argp2, argp3); } } // host stub function #ifndef OPS_LAZY void ops_par_loop_drhoEpudx_kernel(char const *name, ops_block block, int dim, int* range, ops_arg arg0, ops_arg arg1, ops_arg arg2, ops_arg arg3) { #else void ops_par_loop_drhoEpudx_kernel_execute(ops_kernel_descriptor *desc) { int dim = desc->dim; #if OPS_MPI ops_block block = desc->block; #endif int *range = desc->range; ops_arg arg0 = desc->args[0]; ops_arg arg1 = desc->args[1]; ops_arg arg2 = desc->args[2]; ops_arg arg3 = desc->args[3]; #endif //Timing double t1,t2,c1,c2; ops_arg args[4] = { arg0, arg1, arg2, arg3}; #if CHECKPOINTING && !OPS_LAZY if (!ops_checkpointing_before(args,4,range,5)) return; #endif if (OPS_diags > 1) { ops_timing_realloc(5,"drhoEpudx_kernel"); OPS_kernels[5].count++; ops_timers_core(&c1,&t1); } //compute locally allocated range for the sub-block int start[1]; int end[1]; #if OPS_MPI && !OPS_LAZY sub_block_list sb = OPS_sub_block_list[block->index]; #endif //OPS_MPI #ifdef OPS_MPI int arg_idx[1]; #endif #ifdef OPS_MPI if (compute_ranges(args, 4,block, range, start, end, arg_idx) < 0) return; #else //OPS_MPI for ( int n=0; n<1; n++ ){ start[n] = range[2*n];end[n] = range[2*n+1]; } #endif int xdim0 = args[0].dat->size[0]; int xdim1 = args[1].dat->size[0]; int xdim2 = args[2].dat->size[0]; int xdim3 = args[3].dat->size[0]; if (xdim0 != dims_drhoEpudx_kernel_h[0][0] || xdim1 != dims_drhoEpudx_kernel_h[1][0] || xdim2 != dims_drhoEpudx_kernel_h[2][0] || xdim3 != dims_drhoEpudx_kernel_h[3][0]) { dims_drhoEpudx_kernel_h[0][0] = xdim0; dims_drhoEpudx_kernel_h[1][0] = xdim1; dims_drhoEpudx_kernel_h[2][0] = xdim2; dims_drhoEpudx_kernel_h[3][0] = xdim3; cutilSafeCall(hipMemcpyToSymbol( dims_drhoEpudx_kernel, dims_drhoEpudx_kernel_h, sizeof(dims_drhoEpudx_kernel))); } int x_size = MAX(0,end[0]-start[0]); dim3 grid( (x_size-1)/OPS_block_size_x+ 1, 1, 1); dim3 tblock(OPS_block_size_x,1,1); int dat0 = (OPS_soa ? args[0].dat->type_size : args[0].dat->elem_size); int dat1 = (OPS_soa ? args[1].dat->type_size : args[1].dat->elem_size); int dat2 = (OPS_soa ? args[2].dat->type_size : args[2].dat->elem_size); int dat3 = (OPS_soa ? args[3].dat->type_size : args[3].dat->elem_size); char *p_a[4]; //set up initial pointers int base0 = args[0].dat->base_offset + dat0 * 1 * (start[0] * args[0].stencil->stride[0]); p_a[0] = (char *)args[0].data_d + base0; int base1 = args[1].dat->base_offset + dat1 * 1 * (start[0] * args[1].stencil->stride[0]); p_a[1] = (char *)args[1].data_d + base1; int base2 = args[2].dat->base_offset + dat2 * 1 * (start[0] * args[2].stencil->stride[0]); p_a[2] = (char *)args[2].data_d + base2; int base3 = args[3].dat->base_offset + dat3 * 1 * (start[0] * args[3].stencil->stride[0]); p_a[3] = (char *)args[3].data_d + base3; #ifndef OPS_LAZY ops_H_D_exchanges_device(args, 4); ops_halo_exchanges(args,4,range); #endif if (OPS_diags > 1) { ops_timers_core(&c2,&t2); OPS_kernels[5].mpi_time += t2-t1; } //call kernel wrapper function, passing in pointers to data if (x_size > 0) hipLaunchKernelGGL(( ops_drhoEpudx_kernel), dim3(grid), dim3(tblock) , 0, 0, (double *)p_a[0], (double *)p_a[1], (double *)p_a[2], (double *)p_a[3],x_size); cutilSafeCall(hipGetLastError()); if (OPS_diags>1) { cutilSafeCall(hipDeviceSynchronize()); ops_timers_core(&c1,&t1); OPS_kernels[5].time += t1-t2; } #ifndef OPS_LAZY ops_set_dirtybit_device(args, 4); ops_set_halo_dirtybit3(&args[3],range); #endif if (OPS_diags > 1) { //Update kernel record ops_timers_core(&c2,&t2); OPS_kernels[5].mpi_time += t2-t1; OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg0); OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg1); OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg2); OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg3); } } #ifdef OPS_LAZY void ops_par_loop_drhoEpudx_kernel(char const *name, ops_block block, int dim, int* range, ops_arg arg0, ops_arg arg1, ops_arg arg2, ops_arg arg3) { ops_kernel_descriptor *desc = (ops_kernel_descriptor *)malloc(sizeof(ops_kernel_descriptor)); desc->name = name; desc->block = block; desc->dim = dim; desc->device = 1; desc->index = 5; desc->hash = 5381; desc->hash = ((desc->hash << 5) + desc->hash) + 5; for ( int i=0; i<2; i++ ){ desc->range[i] = range[i]; desc->orig_range[i] = range[i]; desc->hash = ((desc->hash << 5) + desc->hash) + range[i]; } desc->nargs = 4; desc->args = (ops_arg*)malloc(4*sizeof(ops_arg)); desc->args[0] = arg0; desc->hash = ((desc->hash << 5) + desc->hash) + arg0.dat->index; desc->args[1] = arg1; desc->hash = ((desc->hash << 5) + desc->hash) + arg1.dat->index; desc->args[2] = arg2; desc->hash = ((desc->hash << 5) + desc->hash) + arg2.dat->index; desc->args[3] = arg3; desc->hash = ((desc->hash << 5) + desc->hash) + arg3.dat->index; desc->function = ops_par_loop_drhoEpudx_kernel_execute; if (OPS_diags > 1) { ops_timing_realloc(5,"drhoEpudx_kernel"); } ops_enqueue_kernel(desc); } #endif
1d2157c9411bd678bb3c0236333f38e3ab5a09f1.cu
// // auto-generated by ops.py // __constant__ int dims_drhoEpudx_kernel [4][1]; static int dims_drhoEpudx_kernel_h [4][1] = {0}; //user function __device__ void drhoEpudx_kernel_gpu(const ACC<double> &rhou_new, const ACC<double>& rho_new, const ACC<double>& rhoE_new, ACC<double> &rhoE_res) { double fni = rhou_new(0) * rhou_new(0) / rho_new(0) ; double p = gam1 * (rhoE_new(0) - 0.5 * fni); fni = (rhoE_new(0) + p) * rhou_new(0) / rho_new(0) ; double fnim1 = rhou_new(-1) * rhou_new(-1) / rho_new(-1); p = gam1 * (rhoE_new(-1) - 0.5 * fnim1); fnim1 = (rhoE_new(-1) + p) * rhou_new(-1) / rho_new(-1); double fnim2 = rhou_new(-2) * rhou_new(-2) / rho_new(-2); p = gam1 * (rhoE_new(-2) - 0.5 * fnim2); fnim2 = (rhoE_new(-2) + p ) * rhou_new(-2) / rho_new(-2); double fnip1 = rhou_new(1) * rhou_new(1) / rho_new(1); p = gam1 * (rhoE_new(1) - 0.5 * fnip1); fnip1 = (rhoE_new(1) + p) * rhou_new(1) / rho_new(1); double fnip2 = rhou_new(2) * rhou_new(2) / rho_new(2); p = gam1 * (rhoE_new(2) - 0.5 * fnip2); fnip2 = (rhoE_new(2) + p) * rhou_new(2) / rho_new(2); double deriv = (fnim2 - fnip2 + 8.0* (fnip1 - fnim1))/(12.00*dx); rhoE_res(0) = deriv; } __global__ void ops_drhoEpudx_kernel( double* __restrict arg0, double* __restrict arg1, double* __restrict arg2, double* __restrict arg3, int size0 ){ int idx_x = blockDim.x * blockIdx.x + threadIdx.x; arg0 += idx_x * 1*1; arg1 += idx_x * 1*1; arg2 += idx_x * 1*1; arg3 += idx_x * 1*1; if (idx_x < size0) { const ACC<double> argp0(arg0); const ACC<double> argp1(arg1); const ACC<double> argp2(arg2); ACC<double> argp3(arg3); drhoEpudx_kernel_gpu(argp0, argp1, argp2, argp3); } } // host stub function #ifndef OPS_LAZY void ops_par_loop_drhoEpudx_kernel(char const *name, ops_block block, int dim, int* range, ops_arg arg0, ops_arg arg1, ops_arg arg2, ops_arg arg3) { #else void ops_par_loop_drhoEpudx_kernel_execute(ops_kernel_descriptor *desc) { int dim = desc->dim; #if OPS_MPI ops_block block = desc->block; #endif int *range = desc->range; ops_arg arg0 = desc->args[0]; ops_arg arg1 = desc->args[1]; ops_arg arg2 = desc->args[2]; ops_arg arg3 = desc->args[3]; #endif //Timing double t1,t2,c1,c2; ops_arg args[4] = { arg0, arg1, arg2, arg3}; #if CHECKPOINTING && !OPS_LAZY if (!ops_checkpointing_before(args,4,range,5)) return; #endif if (OPS_diags > 1) { ops_timing_realloc(5,"drhoEpudx_kernel"); OPS_kernels[5].count++; ops_timers_core(&c1,&t1); } //compute locally allocated range for the sub-block int start[1]; int end[1]; #if OPS_MPI && !OPS_LAZY sub_block_list sb = OPS_sub_block_list[block->index]; #endif //OPS_MPI #ifdef OPS_MPI int arg_idx[1]; #endif #ifdef OPS_MPI if (compute_ranges(args, 4,block, range, start, end, arg_idx) < 0) return; #else //OPS_MPI for ( int n=0; n<1; n++ ){ start[n] = range[2*n];end[n] = range[2*n+1]; } #endif int xdim0 = args[0].dat->size[0]; int xdim1 = args[1].dat->size[0]; int xdim2 = args[2].dat->size[0]; int xdim3 = args[3].dat->size[0]; if (xdim0 != dims_drhoEpudx_kernel_h[0][0] || xdim1 != dims_drhoEpudx_kernel_h[1][0] || xdim2 != dims_drhoEpudx_kernel_h[2][0] || xdim3 != dims_drhoEpudx_kernel_h[3][0]) { dims_drhoEpudx_kernel_h[0][0] = xdim0; dims_drhoEpudx_kernel_h[1][0] = xdim1; dims_drhoEpudx_kernel_h[2][0] = xdim2; dims_drhoEpudx_kernel_h[3][0] = xdim3; cutilSafeCall(cudaMemcpyToSymbol( dims_drhoEpudx_kernel, dims_drhoEpudx_kernel_h, sizeof(dims_drhoEpudx_kernel))); } int x_size = MAX(0,end[0]-start[0]); dim3 grid( (x_size-1)/OPS_block_size_x+ 1, 1, 1); dim3 tblock(OPS_block_size_x,1,1); int dat0 = (OPS_soa ? args[0].dat->type_size : args[0].dat->elem_size); int dat1 = (OPS_soa ? args[1].dat->type_size : args[1].dat->elem_size); int dat2 = (OPS_soa ? args[2].dat->type_size : args[2].dat->elem_size); int dat3 = (OPS_soa ? args[3].dat->type_size : args[3].dat->elem_size); char *p_a[4]; //set up initial pointers int base0 = args[0].dat->base_offset + dat0 * 1 * (start[0] * args[0].stencil->stride[0]); p_a[0] = (char *)args[0].data_d + base0; int base1 = args[1].dat->base_offset + dat1 * 1 * (start[0] * args[1].stencil->stride[0]); p_a[1] = (char *)args[1].data_d + base1; int base2 = args[2].dat->base_offset + dat2 * 1 * (start[0] * args[2].stencil->stride[0]); p_a[2] = (char *)args[2].data_d + base2; int base3 = args[3].dat->base_offset + dat3 * 1 * (start[0] * args[3].stencil->stride[0]); p_a[3] = (char *)args[3].data_d + base3; #ifndef OPS_LAZY ops_H_D_exchanges_device(args, 4); ops_halo_exchanges(args,4,range); #endif if (OPS_diags > 1) { ops_timers_core(&c2,&t2); OPS_kernels[5].mpi_time += t2-t1; } //call kernel wrapper function, passing in pointers to data if (x_size > 0) ops_drhoEpudx_kernel<<<grid, tblock >>> ( (double *)p_a[0], (double *)p_a[1], (double *)p_a[2], (double *)p_a[3],x_size); cutilSafeCall(cudaGetLastError()); if (OPS_diags>1) { cutilSafeCall(cudaDeviceSynchronize()); ops_timers_core(&c1,&t1); OPS_kernels[5].time += t1-t2; } #ifndef OPS_LAZY ops_set_dirtybit_device(args, 4); ops_set_halo_dirtybit3(&args[3],range); #endif if (OPS_diags > 1) { //Update kernel record ops_timers_core(&c2,&t2); OPS_kernels[5].mpi_time += t2-t1; OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg0); OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg1); OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg2); OPS_kernels[5].transfer += ops_compute_transfer(dim, start, end, &arg3); } } #ifdef OPS_LAZY void ops_par_loop_drhoEpudx_kernel(char const *name, ops_block block, int dim, int* range, ops_arg arg0, ops_arg arg1, ops_arg arg2, ops_arg arg3) { ops_kernel_descriptor *desc = (ops_kernel_descriptor *)malloc(sizeof(ops_kernel_descriptor)); desc->name = name; desc->block = block; desc->dim = dim; desc->device = 1; desc->index = 5; desc->hash = 5381; desc->hash = ((desc->hash << 5) + desc->hash) + 5; for ( int i=0; i<2; i++ ){ desc->range[i] = range[i]; desc->orig_range[i] = range[i]; desc->hash = ((desc->hash << 5) + desc->hash) + range[i]; } desc->nargs = 4; desc->args = (ops_arg*)malloc(4*sizeof(ops_arg)); desc->args[0] = arg0; desc->hash = ((desc->hash << 5) + desc->hash) + arg0.dat->index; desc->args[1] = arg1; desc->hash = ((desc->hash << 5) + desc->hash) + arg1.dat->index; desc->args[2] = arg2; desc->hash = ((desc->hash << 5) + desc->hash) + arg2.dat->index; desc->args[3] = arg3; desc->hash = ((desc->hash << 5) + desc->hash) + arg3.dat->index; desc->function = ops_par_loop_drhoEpudx_kernel_execute; if (OPS_diags > 1) { ops_timing_realloc(5,"drhoEpudx_kernel"); } ops_enqueue_kernel(desc); } #endif
320492f742033e3984ffc90feafcc18482298e7a.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <bvh_common.h> #include "bvh_math.cuh" #include "cuPRNG.cuh" __constant__ float3 CMASSWind; __constant__ float3 CMASSWindU; __constant__ float3 CMASSWindV; __constant__ float3 CMASSGravity; __global__ void setWind_kernel(float3 * deltaVel, float * mass, float turbulence, uint windSeed, uint maxInd) { unsigned ind = blockIdx.x*blockDim.x + threadIdx.x; if(ind >= maxInd) return; float m = mass[ind]; if(m > 1e5f) return; unsigned sd = ind + windSeed; float r = HybridTaus(sd); float3 wind = make_float3(CMASSWind.x * r, CMASSWind.y * r, CMASSWind.z * r); float su = (HybridTaus(sd) - .5f) * turbulence; float sv = (HybridTaus(sd) - .5f) * turbulence; float3_add_inplace(wind, make_float3(CMASSWindU.x * su, CMASSWindU.y * su, CMASSWindU.z * su)); float3_add_inplace(wind, make_float3(CMASSWindV.x * sv, CMASSWindV.y * sv, CMASSWindV.z * sv)); float3_add_inplace(deltaVel[ind], wind); } namespace windforce { void setWindVecs(float * u, float * v, float * w) { hipMemcpyToSymbol(CMASSWind, u, 12); hipMemcpyToSymbol(CMASSWindU, v, 12); hipMemcpyToSymbol(CMASSWindV, w, 12); } void setWind(float3 * deltaVel, float * mass, float turbulence, uint windSeed, uint maxInd) { dim3 block(512, 1, 1); unsigned nblk = iDivUp(maxInd, 512); dim3 grid(nblk, 1, 1); hipLaunchKernelGGL(( setWind_kernel), dim3(grid), dim3(block) , 0, 0, deltaVel, mass, turbulence, windSeed, maxInd); } } __global__ void addGravity_kernel(float3 * deltaVel, float * mass, float dt, uint maxInd) { unsigned ind = blockIdx.x*blockDim.x + threadIdx.x; if(ind >= maxInd) return; if(mass[ind] < 1e5f) float3_add_inplace( deltaVel[ind], scale_float3_by(CMASSGravity, dt) ); } namespace gravityforce { void setGravity(float * g) { hipMemcpyToSymbol(CMASSGravity, g, 12); } void addGravity(float3 * deltaVel, float * mass, float dt, uint maxInd) { dim3 block(512, 1, 1); unsigned nblk = iDivUp(maxInd, 512); dim3 grid(nblk, 1, 1); hipLaunchKernelGGL(( addGravity_kernel), dim3(grid), dim3(block) , 0, 0, deltaVel, mass, dt, maxInd); } }
320492f742033e3984ffc90feafcc18482298e7a.cu
#include <bvh_common.h> #include "bvh_math.cuh" #include "cuPRNG.cuh" __constant__ float3 CMASSWind; __constant__ float3 CMASSWindU; __constant__ float3 CMASSWindV; __constant__ float3 CMASSGravity; __global__ void setWind_kernel(float3 * deltaVel, float * mass, float turbulence, uint windSeed, uint maxInd) { unsigned ind = blockIdx.x*blockDim.x + threadIdx.x; if(ind >= maxInd) return; float m = mass[ind]; if(m > 1e5f) return; unsigned sd = ind + windSeed; float r = HybridTaus(sd); float3 wind = make_float3(CMASSWind.x * r, CMASSWind.y * r, CMASSWind.z * r); float su = (HybridTaus(sd) - .5f) * turbulence; float sv = (HybridTaus(sd) - .5f) * turbulence; float3_add_inplace(wind, make_float3(CMASSWindU.x * su, CMASSWindU.y * su, CMASSWindU.z * su)); float3_add_inplace(wind, make_float3(CMASSWindV.x * sv, CMASSWindV.y * sv, CMASSWindV.z * sv)); float3_add_inplace(deltaVel[ind], wind); } namespace windforce { void setWindVecs(float * u, float * v, float * w) { cudaMemcpyToSymbol(CMASSWind, u, 12); cudaMemcpyToSymbol(CMASSWindU, v, 12); cudaMemcpyToSymbol(CMASSWindV, w, 12); } void setWind(float3 * deltaVel, float * mass, float turbulence, uint windSeed, uint maxInd) { dim3 block(512, 1, 1); unsigned nblk = iDivUp(maxInd, 512); dim3 grid(nblk, 1, 1); setWind_kernel<<< grid, block >>>(deltaVel, mass, turbulence, windSeed, maxInd); } } __global__ void addGravity_kernel(float3 * deltaVel, float * mass, float dt, uint maxInd) { unsigned ind = blockIdx.x*blockDim.x + threadIdx.x; if(ind >= maxInd) return; if(mass[ind] < 1e5f) float3_add_inplace( deltaVel[ind], scale_float3_by(CMASSGravity, dt) ); } namespace gravityforce { void setGravity(float * g) { cudaMemcpyToSymbol(CMASSGravity, g, 12); } void addGravity(float3 * deltaVel, float * mass, float dt, uint maxInd) { dim3 block(512, 1, 1); unsigned nblk = iDivUp(maxInd, 512); dim3 grid(nblk, 1, 1); addGravity_kernel<<< grid, block >>>(deltaVel, mass, dt, maxInd); } }
1201f7755f4978f3cf6bf4cad8e4b232cfab6ad2.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" __global__ void _bcnn_vmul_kernel(int n, float *a, float *b, float *y) { int i = (blockIdx.x + blockIdx.y * gridDim.x) * blockDim.x + threadIdx.x; if (i < n) y[i] = a[i] * b[i]; }
1201f7755f4978f3cf6bf4cad8e4b232cfab6ad2.cu
#include "includes.h" __global__ void _bcnn_vmul_kernel(int n, float *a, float *b, float *y) { int i = (blockIdx.x + blockIdx.y * gridDim.x) * blockDim.x + threadIdx.x; if (i < n) y[i] = a[i] * b[i]; }
ca6d3e121943604a7a7094751807ee7ad8b3dbbb.hip
// !!! This is a file automatically generated by hipify!!! // ---------------------------------------------------------------------------- // Gunrock -- High-Performance Graph Primitives on GPU // ---------------------------------------------------------------------------- // This source code is distributed under the terms of LICENSE.TXT // in the root directory of this source distribution. // ---------------------------------------------------------------------------- /** * @file sample_test.cu * * @brief Simple test driver program */ #include <stdio.h> #include <string> #include <iostream> // utilities for correctness checking #include <gunrock/util/test_utils.cuh> // graph construction utilities #include <gunrock/graphio/market.cuh> // primitive-specific headers include #include <gunrock/app/template/sample_enactor.cuh> #include <gunrock/app/template/sample_problem.cuh> #include <gunrock/app/template/sample_functor.cuh> // gunrock abstraction graph operators #include <gunrock/oprtr/advance/kernel.cuh> #include <gunrock/oprtr/filter/kernel.cuh> #include <moderngpu.cuh> using namespace gunrock; using namespace gunrock::util; using namespace gunrock::oprtr; using namespace gunrock::app::sample; // ---------------------------------------------------------------------------- // Housekeeping Routines // ---------------------------------------------------------------------------- void Usage() { printf( " sample_test <graph type> <file name> [--undirected] [--quick]\n" " [--device=<device_index>]\n" " Graph types and arguments:\n" " market <file>\n" " Reads a Matrix-Market coordinate-formatted graph,\n" " edges from STDIN (or from the optionally-specified file)\n" " --device=<device_index> Set GPU device to run. [Default: 0]\n" " --undirected Convert the graph to undirected\n" " --quick Skip the CPU validation [Default: false]\n" " --v Print verbose per iteration debug info\n"); } /** * @brief Displays primitive results. * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] graph Reference to the CSR graph. */ template<typename VertexId, typename SizeT, typename Value> void DisplaySolution(const Csr<VertexId, Value, SizeT> &graph) { printf("==> display solution: (currently missing)\n"); // TODO(developer): code to print out results } /** * @brief Performance / Evaluation statistics. */ struct Stats { const char *name; Statistic num_iterations; Stats() : name(NULL), num_iterations() {} explicit Stats(const char *name) : name(name), num_iterations() {} }; /** * @brief Test_Parameter structure. */ struct Test_Parameter : gunrock::app::TestParameter_Base { public: Test_Parameter() {} ~Test_Parameter() {} void Init(CommandLineArgs &args) { TestParameter_Base::Init(args); } }; /** * @brief Displays timing and correctness statistics. * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] stats Reference to the Stats object. * @param[in] graph Reference to the CSR graph. * @param[in] elapsed Device elapsed running time. * @param[in] iterations Number of iterations of the algorithm. */ template<typename VertexId, typename SizeT, typename Value> void DisplayStats( const Stats& stats, const Csr<VertexId, Value, SizeT>& graph, const float elapsed, const long long iterations) { printf("[%s] finished.\n", stats.name); printf("elapsed: %.4f ms\n", elapsed); printf("num_iterations: %lld\n", iterations); // TODO(developer): code to print statistics } // ---------------------------------------------------------------------------- // Testing Routines // ---------------------------------------------------------------------------- /** * @brief A simple CPU-based reference implementation. * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] graph Reference to the CSR graph we process on. */ template<typename VertexId, typename SizeT, typename Value> void SimpleReference(const Csr<VertexId, Value, SizeT> &graph) { // initialization // perform calculation CpuTimer cpu_timer; cpu_timer.Start(); // TODO(developer): CPU validation code here cpu_timer.Stop(); float cpu_elapsed = cpu_timer.ElapsedMillis(); printf("CPU reference finished in %lf ms.\n\n", cpu_elapsed); } /** * @brief Sample test entry * * @tparam VertexId * @tparam SizeT * @tparam Value * @tparam DEBUG * @tparam SIZE_CHECK * * @param[in] parameter Test parameter settings. */ template < typename VertexId, typename SizeT, typename Value, bool DEBUG, bool SIZE_CHECK > void RunTest(Test_Parameter *parameter) { typedef SampleProblem < VertexId, SizeT, Value, true, // MARK_PREDECESSORS false, // ENABLE_IDEMPOTENCE false > Problem; Csr<VertexId, Value, SizeT>* graph = (Csr<VertexId, Value, SizeT>*)parameter->graph; ContextPtr* context = (ContextPtr*)parameter -> context; std::string partition_method = parameter -> partition_method; int max_grid_size = parameter -> max_grid_size; int num_gpus = parameter -> num_gpus; int* gpu_idx = parameter -> gpu_idx; int iterations = parameter -> iterations; bool g_quick = parameter -> g_quick; bool g_stream_from_host = parameter -> g_stream_from_host; double max_queue_sizing = parameter -> max_queue_sizing; // allocate host-side array (for both reference and GPU-computed results) VertexId *r_labels = (VertexId*)malloc(sizeof(VertexId) * graph->nodes); VertexId *h_labels = (VertexId*)malloc(sizeof(VertexId) * graph->nodes); SampleEnactor < Problem, false, // INSTRUMENT false, // DEBUG true > // SIZE_CHECK enactor(gpu_idx); // allocate primitive enactor map Problem *problem = new Problem; // allocate primitive problem on GPU util::GRError( problem->Init(g_stream_from_host, *graph, num_gpus), "Problem Initialization Failed", __FILE__, __LINE__); Stats *stats = new Stats("GPU Primitive"); // // perform calculation // GpuTimer gpu_timer; float elapsed = 0.0f; for (int iter = 0; iter < iterations; ++iter) { util::GRError( problem->Reset(enactor.GetFrontierType(), max_queue_sizing), "Problem Data Reset Failed", __FILE__, __LINE__); gpu_timer.Start(); util::GRError( enactor.template Enact<Problem>(*context, problem, max_grid_size), "Problem Enact Failed", __FILE__, __LINE__); gpu_timer.Stop(); elapsed += gpu_timer.ElapsedMillis(); } elapsed /= iterations; // extract results util::GRError( problem->Extract(h_labels), "Problem Data Extraction Failed", __FILE__, __LINE__); // compute reference CPU validation solution if (!g_quick) { printf("==> computing reference value ... (currently missing)\n"); SimpleReference<VertexId, SizeT, Value>(graph); printf("==> validation: (currently missing)\n"); } DisplaySolution<VertexId, SizeT, Value>(graph); // display solution // display statistics VertexId num_iteratios = 0; enactor.GetStatistics(num_iteratios); DisplayStats<VertexId, SizeT, Value>(*stats, graph, elapsed, num_iteratios); // clean up if (stats) { delete stats; } if (problem) { delete problem; } if (r_labels) { free(r_labels); } if (h_labels) { free(h_labels); } } /** * @brief RunTests entry * * @tparam VertexId * @tparam Value * @tparam SizeT * @tparam DEBUG * * @param[in] parameter Pointer to test parameter settings */ template < typename VertexId, typename Value, typename SizeT, bool DEBUG > void RunTests_size_check(Test_Parameter *parameter) { if (parameter->size_check) RunTest <VertexId, Value, SizeT, DEBUG, true>(parameter); else RunTest <VertexId, Value, SizeT, DEBUG, false>(parameter); } /** * @brief RunTests entry * * @tparam VertexId * @tparam Value * @tparam SizeT * * @param[in] parameter Pointer to test parameter settings */ template < typename VertexId, typename Value, typename SizeT > void RunTests_debug(Test_Parameter *parameter) { if (parameter->debug) RunTests_size_check <VertexId, Value, SizeT, true>(parameter); else RunTests_size_check <VertexId, Value, SizeT, false>(parameter); } /** * @brief Sample test entry * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] graph Pointer to the CSR graph we process on. * @param[in] args Reference to the command line arguments. * @param[in] num_gpus Number of GPUs. * @param[in] context CudaContext pointer for moderngpu APIs. * @param[in] gpu_idx GPU inddex to run algorithm. * @param[in] streams CUDA streams. */ template < typename VertexId, typename Value, typename SizeT > void RunTest( Csr<VertexId, Value, SizeT>* graph, CommandLineArgs& args, int num_gpus, ContextPtr* context, int* gpu_idx, hipStream_t* streams = NULL) { Test_Parameter *parameter = new Test_Parameter; parameter -> Init(args); parameter -> graph = graph; parameter -> num_gpus = num_gpus; parameter -> context = context; parameter -> gpu_idx = gpu_idx; parameter -> streams = streams; RunTests_debug<VertexId, Value, SizeT>(parameter); } // ---------------------------------------------------------------------------- // Main // ---------------------------------------------------------------------------- int main(int argc, char** argv) { CommandLineArgs args(argc, argv); if ((argc < 2) || (args.CheckCmdLineFlag("help"))) { Usage(); return 1; } int device = 0; args.GetCmdLineArgument("device", device); ContextPtr context = mgpu::CreateCudaDevice(device); // parse graph-construction parameters bool g_undirected = args.CheckCmdLineFlag("undirected"); std::string graph_type = argv[1]; int flags = args.ParsedArgc(); int graph_args = argc - flags - 1; if (graph_args < 1) { Usage(); return 1; } typedef int VertexId; // Use as the vertex identifier typedef int SizeT; // Use as the graph size type typedef int Value; // Use as the value type if (graph_type == "market") { // matrix-market coordinate-formatted graph Csr<VertexId, Value, SizeT> csr(false); char *name = (graph_args == 2) ? argv[2] : NULL; if (graphio::BuildMarketGraph<false>( name, csr, g_undirected, false) != 0) { return 1; } csr.DisplayGraph(); // display graph adjacent list csr.PrintHistogram(); // display graph histogram RunTest<VertexId, Value, SizeT>(&csr, args, 1, &context, &device); } else { fprintf(stderr, "Unspecified graph type\n"); return 1; } return 0; } // Leave this at the end of the file // Local Variables: // mode:c++ // c-file-style: "NVIDIA" // End:
ca6d3e121943604a7a7094751807ee7ad8b3dbbb.cu
// ---------------------------------------------------------------------------- // Gunrock -- High-Performance Graph Primitives on GPU // ---------------------------------------------------------------------------- // This source code is distributed under the terms of LICENSE.TXT // in the root directory of this source distribution. // ---------------------------------------------------------------------------- /** * @file sample_test.cu * * @brief Simple test driver program */ #include <stdio.h> #include <string> #include <iostream> // utilities for correctness checking #include <gunrock/util/test_utils.cuh> // graph construction utilities #include <gunrock/graphio/market.cuh> // primitive-specific headers include #include <gunrock/app/template/sample_enactor.cuh> #include <gunrock/app/template/sample_problem.cuh> #include <gunrock/app/template/sample_functor.cuh> // gunrock abstraction graph operators #include <gunrock/oprtr/advance/kernel.cuh> #include <gunrock/oprtr/filter/kernel.cuh> #include <moderngpu.cuh> using namespace gunrock; using namespace gunrock::util; using namespace gunrock::oprtr; using namespace gunrock::app::sample; // ---------------------------------------------------------------------------- // Housekeeping Routines // ---------------------------------------------------------------------------- void Usage() { printf( " sample_test <graph type> <file name> [--undirected] [--quick]\n" " [--device=<device_index>]\n" " Graph types and arguments:\n" " market <file>\n" " Reads a Matrix-Market coordinate-formatted graph,\n" " edges from STDIN (or from the optionally-specified file)\n" " --device=<device_index> Set GPU device to run. [Default: 0]\n" " --undirected Convert the graph to undirected\n" " --quick Skip the CPU validation [Default: false]\n" " --v Print verbose per iteration debug info\n"); } /** * @brief Displays primitive results. * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] graph Reference to the CSR graph. */ template<typename VertexId, typename SizeT, typename Value> void DisplaySolution(const Csr<VertexId, Value, SizeT> &graph) { printf("==> display solution: (currently missing)\n"); // TODO(developer): code to print out results } /** * @brief Performance / Evaluation statistics. */ struct Stats { const char *name; Statistic num_iterations; Stats() : name(NULL), num_iterations() {} explicit Stats(const char *name) : name(name), num_iterations() {} }; /** * @brief Test_Parameter structure. */ struct Test_Parameter : gunrock::app::TestParameter_Base { public: Test_Parameter() {} ~Test_Parameter() {} void Init(CommandLineArgs &args) { TestParameter_Base::Init(args); } }; /** * @brief Displays timing and correctness statistics. * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] stats Reference to the Stats object. * @param[in] graph Reference to the CSR graph. * @param[in] elapsed Device elapsed running time. * @param[in] iterations Number of iterations of the algorithm. */ template<typename VertexId, typename SizeT, typename Value> void DisplayStats( const Stats& stats, const Csr<VertexId, Value, SizeT>& graph, const float elapsed, const long long iterations) { printf("[%s] finished.\n", stats.name); printf("elapsed: %.4f ms\n", elapsed); printf("num_iterations: %lld\n", iterations); // TODO(developer): code to print statistics } // ---------------------------------------------------------------------------- // Testing Routines // ---------------------------------------------------------------------------- /** * @brief A simple CPU-based reference implementation. * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] graph Reference to the CSR graph we process on. */ template<typename VertexId, typename SizeT, typename Value> void SimpleReference(const Csr<VertexId, Value, SizeT> &graph) { // initialization // perform calculation CpuTimer cpu_timer; cpu_timer.Start(); // TODO(developer): CPU validation code here cpu_timer.Stop(); float cpu_elapsed = cpu_timer.ElapsedMillis(); printf("CPU reference finished in %lf ms.\n\n", cpu_elapsed); } /** * @brief Sample test entry * * @tparam VertexId * @tparam SizeT * @tparam Value * @tparam DEBUG * @tparam SIZE_CHECK * * @param[in] parameter Test parameter settings. */ template < typename VertexId, typename SizeT, typename Value, bool DEBUG, bool SIZE_CHECK > void RunTest(Test_Parameter *parameter) { typedef SampleProblem < VertexId, SizeT, Value, true, // MARK_PREDECESSORS false, // ENABLE_IDEMPOTENCE false > Problem; Csr<VertexId, Value, SizeT>* graph = (Csr<VertexId, Value, SizeT>*)parameter->graph; ContextPtr* context = (ContextPtr*)parameter -> context; std::string partition_method = parameter -> partition_method; int max_grid_size = parameter -> max_grid_size; int num_gpus = parameter -> num_gpus; int* gpu_idx = parameter -> gpu_idx; int iterations = parameter -> iterations; bool g_quick = parameter -> g_quick; bool g_stream_from_host = parameter -> g_stream_from_host; double max_queue_sizing = parameter -> max_queue_sizing; // allocate host-side array (for both reference and GPU-computed results) VertexId *r_labels = (VertexId*)malloc(sizeof(VertexId) * graph->nodes); VertexId *h_labels = (VertexId*)malloc(sizeof(VertexId) * graph->nodes); SampleEnactor < Problem, false, // INSTRUMENT false, // DEBUG true > // SIZE_CHECK enactor(gpu_idx); // allocate primitive enactor map Problem *problem = new Problem; // allocate primitive problem on GPU util::GRError( problem->Init(g_stream_from_host, *graph, num_gpus), "Problem Initialization Failed", __FILE__, __LINE__); Stats *stats = new Stats("GPU Primitive"); // // perform calculation // GpuTimer gpu_timer; float elapsed = 0.0f; for (int iter = 0; iter < iterations; ++iter) { util::GRError( problem->Reset(enactor.GetFrontierType(), max_queue_sizing), "Problem Data Reset Failed", __FILE__, __LINE__); gpu_timer.Start(); util::GRError( enactor.template Enact<Problem>(*context, problem, max_grid_size), "Problem Enact Failed", __FILE__, __LINE__); gpu_timer.Stop(); elapsed += gpu_timer.ElapsedMillis(); } elapsed /= iterations; // extract results util::GRError( problem->Extract(h_labels), "Problem Data Extraction Failed", __FILE__, __LINE__); // compute reference CPU validation solution if (!g_quick) { printf("==> computing reference value ... (currently missing)\n"); SimpleReference<VertexId, SizeT, Value>(graph); printf("==> validation: (currently missing)\n"); } DisplaySolution<VertexId, SizeT, Value>(graph); // display solution // display statistics VertexId num_iteratios = 0; enactor.GetStatistics(num_iteratios); DisplayStats<VertexId, SizeT, Value>(*stats, graph, elapsed, num_iteratios); // clean up if (stats) { delete stats; } if (problem) { delete problem; } if (r_labels) { free(r_labels); } if (h_labels) { free(h_labels); } } /** * @brief RunTests entry * * @tparam VertexId * @tparam Value * @tparam SizeT * @tparam DEBUG * * @param[in] parameter Pointer to test parameter settings */ template < typename VertexId, typename Value, typename SizeT, bool DEBUG > void RunTests_size_check(Test_Parameter *parameter) { if (parameter->size_check) RunTest <VertexId, Value, SizeT, DEBUG, true>(parameter); else RunTest <VertexId, Value, SizeT, DEBUG, false>(parameter); } /** * @brief RunTests entry * * @tparam VertexId * @tparam Value * @tparam SizeT * * @param[in] parameter Pointer to test parameter settings */ template < typename VertexId, typename Value, typename SizeT > void RunTests_debug(Test_Parameter *parameter) { if (parameter->debug) RunTests_size_check <VertexId, Value, SizeT, true>(parameter); else RunTests_size_check <VertexId, Value, SizeT, false>(parameter); } /** * @brief Sample test entry * * @tparam VertexId * @tparam SizeT * @tparam Value * * @param[in] graph Pointer to the CSR graph we process on. * @param[in] args Reference to the command line arguments. * @param[in] num_gpus Number of GPUs. * @param[in] context CudaContext pointer for moderngpu APIs. * @param[in] gpu_idx GPU inddex to run algorithm. * @param[in] streams CUDA streams. */ template < typename VertexId, typename Value, typename SizeT > void RunTest( Csr<VertexId, Value, SizeT>* graph, CommandLineArgs& args, int num_gpus, ContextPtr* context, int* gpu_idx, cudaStream_t* streams = NULL) { Test_Parameter *parameter = new Test_Parameter; parameter -> Init(args); parameter -> graph = graph; parameter -> num_gpus = num_gpus; parameter -> context = context; parameter -> gpu_idx = gpu_idx; parameter -> streams = streams; RunTests_debug<VertexId, Value, SizeT>(parameter); } // ---------------------------------------------------------------------------- // Main // ---------------------------------------------------------------------------- int main(int argc, char** argv) { CommandLineArgs args(argc, argv); if ((argc < 2) || (args.CheckCmdLineFlag("help"))) { Usage(); return 1; } int device = 0; args.GetCmdLineArgument("device", device); ContextPtr context = mgpu::CreateCudaDevice(device); // parse graph-construction parameters bool g_undirected = args.CheckCmdLineFlag("undirected"); std::string graph_type = argv[1]; int flags = args.ParsedArgc(); int graph_args = argc - flags - 1; if (graph_args < 1) { Usage(); return 1; } typedef int VertexId; // Use as the vertex identifier typedef int SizeT; // Use as the graph size type typedef int Value; // Use as the value type if (graph_type == "market") { // matrix-market coordinate-formatted graph Csr<VertexId, Value, SizeT> csr(false); char *name = (graph_args == 2) ? argv[2] : NULL; if (graphio::BuildMarketGraph<false>( name, csr, g_undirected, false) != 0) { return 1; } csr.DisplayGraph(); // display graph adjacent list csr.PrintHistogram(); // display graph histogram RunTest<VertexId, Value, SizeT>(&csr, args, 1, &context, &device); } else { fprintf(stderr, "Unspecified graph type\n"); return 1; } return 0; } // Leave this at the end of the file // Local Variables: // mode:c++ // c-file-style: "NVIDIA" // End:
7207e720297a8408a12b37ce5250c93e7c5c7913.hip
// !!! This is a file automatically generated by hipify!!! #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include <stdio.h> #include <hip/hip_runtime.h> #include <sys/time.h> double cpuSecond() { struct timeval tp; gettimeofday(&tp, NULL); return ((double) tp.tv_sec + (double) tp.tv_usec * 1.e-6); } #define CHECK(call) \ { \ const hipError_t error = call; \ if (error != hipSuccess) { \ fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ fprintf(stderr, "code: %d, reason: %s\n", error, \ hipGetErrorString(error)); \ exit(1); \ } \ } void identityData(int* I, int nElem) { for (int i = 0; i < nElem; i++) { I[i] = i; } } void initialData(float *ip, int size){ time_t t; srand((unsigned int) time (&t)); for (int i = 0; i < size; i++){ ip[i] = (float)(rand() & 0xFF) / 10.0f; } } void initialDataInt(int *ip, int size){ time_t t; srand((unsigned int) time (&t)); for (int i = 0; i < size; i++){ ip[i] = floor((rand() & 0xFF) / 10.0f); } } void sumArraysOnHost(float *A, float *B, float *C, const int N) { for (int idx = 0; idx < N; idx++) { C[idx] = A[idx] + B[idx]; } } __global__ void sumArraysOnGpu(float *A, float *B, float *C, int* I, int* R, int strike, const int N) { __shared__ float smem[512]; // nmero de conflitos int conflicts = 20; int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { smem[threadIdx.x] += i; C[i] = A[i] + B[i] + smem[(threadIdx.x * conflicts) % blockDim.x]; } } int main(int argc, char**argv) { // Configura tamanho dos vetores int nElem = 100 * 1.e6; int strike = 1; // Alocando memoria na CPU size_t nBytes = nElem * sizeof(float); float *h_A, *h_B, *hostRef, *gpuRef; int *R, *I; h_A = (float *) malloc(nBytes); h_B = (float *) malloc(nBytes); R = (int *) malloc(nBytes); I = (int *) malloc(nBytes); hostRef = (float *) malloc(nBytes); gpuRef = (float *) malloc(nBytes); initialData(h_A, nElem); initialData(h_B, nElem); initialDataInt(R, nElem); identityData(I, nElem); // Alocando memoria global (GPU) float *d_A, *d_B, *d_C; CHECK(hipMalloc((float **)&d_A, nBytes)); CHECK(hipMalloc((float **)&d_B, nBytes)); CHECK(hipMalloc((float **)&d_C, nBytes)); // Transferindo dados da CPU pra GPU CHECK(hipMemcpy(d_A, h_A, nBytes, hipMemcpyHostToDevice)); CHECK(hipMemcpy(d_B, h_B, nBytes, hipMemcpyHostToDevice)); // CHECK(hipMemcpy(d_A, h_A, nBytes, hipMemcpyHostToDevice)); // Invocando o Kernel na CPU int iLen = 512; dim3 block(iLen); dim3 grid((nElem + block.x - 1) / block.x); hipLaunchKernelGGL(( sumArraysOnGpu), dim3(grid), dim3(block), 0, 0, d_A, d_B, d_C, I, R, strike, nElem); // Copia os resultados do Kernel de volta pra CPU CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); // Libera memoria da GPU CHECK(hipFree(d_A)); CHECK(hipFree(d_B)); CHECK(hipFree(d_C)); // Libera memria da CPU free(h_A); free(h_B); free(R); free(I); free(hostRef); free(gpuRef); hipDeviceReset(); return 0; }
7207e720297a8408a12b37ce5250c93e7c5c7913.cu
#include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include <stdio.h> #include <cuda_runtime.h> #include <sys/time.h> double cpuSecond() { struct timeval tp; gettimeofday(&tp, NULL); return ((double) tp.tv_sec + (double) tp.tv_usec * 1.e-6); } #define CHECK(call) \ { \ const cudaError_t error = call; \ if (error != cudaSuccess) { \ fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ fprintf(stderr, "code: %d, reason: %s\n", error, \ cudaGetErrorString(error)); \ exit(1); \ } \ } void identityData(int* I, int nElem) { for (int i = 0; i < nElem; i++) { I[i] = i; } } void initialData(float *ip, int size){ time_t t; srand((unsigned int) time (&t)); for (int i = 0; i < size; i++){ ip[i] = (float)(rand() & 0xFF) / 10.0f; } } void initialDataInt(int *ip, int size){ time_t t; srand((unsigned int) time (&t)); for (int i = 0; i < size; i++){ ip[i] = floor((rand() & 0xFF) / 10.0f); } } void sumArraysOnHost(float *A, float *B, float *C, const int N) { for (int idx = 0; idx < N; idx++) { C[idx] = A[idx] + B[idx]; } } __global__ void sumArraysOnGpu(float *A, float *B, float *C, int* I, int* R, int strike, const int N) { __shared__ float smem[512]; // número de conflitos int conflicts = 20; int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { smem[threadIdx.x] += i; C[i] = A[i] + B[i] + smem[(threadIdx.x * conflicts) % blockDim.x]; } } int main(int argc, char**argv) { // Configura tamanho dos vetores int nElem = 100 * 1.e6; int strike = 1; // Alocando memoria na CPU size_t nBytes = nElem * sizeof(float); float *h_A, *h_B, *hostRef, *gpuRef; int *R, *I; h_A = (float *) malloc(nBytes); h_B = (float *) malloc(nBytes); R = (int *) malloc(nBytes); I = (int *) malloc(nBytes); hostRef = (float *) malloc(nBytes); gpuRef = (float *) malloc(nBytes); initialData(h_A, nElem); initialData(h_B, nElem); initialDataInt(R, nElem); identityData(I, nElem); // Alocando memoria global (GPU) float *d_A, *d_B, *d_C; CHECK(cudaMalloc((float **)&d_A, nBytes)); CHECK(cudaMalloc((float **)&d_B, nBytes)); CHECK(cudaMalloc((float **)&d_C, nBytes)); // Transferindo dados da CPU pra GPU CHECK(cudaMemcpy(d_A, h_A, nBytes, cudaMemcpyHostToDevice)); CHECK(cudaMemcpy(d_B, h_B, nBytes, cudaMemcpyHostToDevice)); // CHECK(cudaMemcpy(d_A, h_A, nBytes, cudaMemcpyHostToDevice)); // Invocando o Kernel na CPU int iLen = 512; dim3 block(iLen); dim3 grid((nElem + block.x - 1) / block.x); sumArraysOnGpu<<<grid, block>>>(d_A, d_B, d_C, I, R, strike, nElem); // Copia os resultados do Kernel de volta pra CPU CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); // Libera memoria da GPU CHECK(cudaFree(d_A)); CHECK(cudaFree(d_B)); CHECK(cudaFree(d_C)); // Libera memória da CPU free(h_A); free(h_B); free(R); free(I); free(hostRef); free(gpuRef); cudaDeviceReset(); return 0; }
27688d81a4b09fc80e1e4978a44e0f9211df042e.hip
// !!! This is a file automatically generated by hipify!!! #include <hip/hip_runtime.h> #include <stdio.h> #include <device_launch_parameters.h> #include <cstdlib> /* * An example of using shared memory to transpose rectangular thread coordinates * of a CUDA grid into a global memory array. Different kernels below * demonstrate performing reads and writes with different ordering, as well as * optimizing using memory padding. */ #define BDIMX 32 #define BDIMY 16 #define IPAD 2 #define CHECK(call) \ { \ const hipError_t error = call; \ if (error != hipSuccess) \ { \ fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ fprintf(stderr, "code: %d, reason: %s\n", error, \ hipGetErrorString(error)); \ } \ } void printData(char *msg, int *in, const int size) { printf("%s: ", msg); for (int i = 0; i < size; i++) { printf("%4d", in[i]); fflush(stdout); } printf("\n\n"); } __global__ void setRowReadRow(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX]; // mapping from thread index to global memory index unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // shared memory store operation tile[threadIdx.y][threadIdx.x] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[threadIdx.y][threadIdx.x]; } __global__ void setColReadCol(int *out) { // static shared memory __shared__ int tile[BDIMX][BDIMY]; // mapping from thread index to global memory index unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // shared memory store operation tile[threadIdx.x][threadIdx.y] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[threadIdx.x][threadIdx.y]; } __global__ void setColReadCol2(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX]; // mapping from 2D thread index to linear memory unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed coordinate (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // shared memory store operation tile[icol][irow] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[icol][irow]; } __global__ void setRowReadCol(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX]; // mapping from 2D thread index to linear memory unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed coordinate (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // shared memory store operation tile[threadIdx.y][threadIdx.x] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[icol][irow]; } __global__ void setRowReadColPad(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX + IPAD]; // mapping from 2D thread index to linear memory unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // shared memory store operation tile[threadIdx.y][threadIdx.x] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[icol][irow]; } __global__ void setRowReadColDyn(int *out) { // dynamic shared memory extern __shared__ int tile[]; // mapping from thread index to global memory index unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // convert back to smem idx to access the transposed element unsigned int col_idx = icol * blockDim.x + irow; // shared memory store operation tile[idx] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[col_idx]; } __global__ void setRowReadColDynPad(int *out) { // dynamic shared memory extern __shared__ int tile[]; // mapping from thread index to global memory index unsigned int g_idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed (row, col) unsigned int irow = g_idx / blockDim.y; unsigned int icol = g_idx % blockDim.y; unsigned int row_idx = threadIdx.y * (blockDim.x + IPAD) + threadIdx.x; // convert back to smem idx to access the transposed element unsigned int col_idx = icol * (blockDim.x + IPAD) + irow; // shared memory store operation tile[row_idx] = g_idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[g_idx] = tile[col_idx]; } int main(int argc, char **argv) { // set up device int dev = 0; hipDeviceProp_t deviceProp; CHECK(hipGetDeviceProperties(&deviceProp, dev)); printf("%s at ", argv[0]); printf("device %d: %s ", dev, deviceProp.name); CHECK(hipSetDevice(dev)); hipSharedMemConfig pConfig; CHECK(hipDeviceGetSharedMemConfig(&pConfig)); printf("with Bank Mode:%s ", pConfig == 1 ? "4-Byte" : "8-Byte"); // set up array size int nx = BDIMX; int ny = BDIMY; bool iprintf = 0; if (argc > 1) iprintf = atoi(argv[1]); size_t nBytes = nx * ny * sizeof(int); // execution configuration dim3 block(BDIMX, BDIMY); dim3 grid(1, 1); printf("<<< grid (%d,%d) block (%d,%d)>>>\n", grid.x, grid.y, block.x, block.y); // allocate device memory int *d_C; CHECK(hipMalloc((int**)&d_C, nBytes)); int *gpuRef = (int *)malloc(nBytes); CHECK(hipMemset(d_C, 0, nBytes)); setRowReadRow << <grid, block >> >(d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setRowReadRow ", gpuRef, nx * ny); CHECK(hipMemset(d_C, 0, nBytes)); hipLaunchKernelGGL(( setColReadCol) , dim3(grid), dim3(block) , 0, 0, d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setColReadCol ", gpuRef, nx * ny); CHECK(hipMemset(d_C, 0, nBytes)); setColReadCol2 << <grid, block >> >(d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setColReadCol2 ", gpuRef, nx * ny); CHECK(hipMemset(d_C, 0, nBytes)); setRowReadCol << <grid, block >> >(d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setRowReadCol ", gpuRef, nx * ny); CHECK(hipMemset(d_C, 0, nBytes)); setRowReadColDyn << <grid, block, BDIMX*BDIMY * sizeof(int) >> >(d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setRowReadColDyn ", gpuRef, nx * ny); CHECK(hipMemset(d_C, 0, nBytes)); setRowReadColPad << <grid, block >> >(d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setRowReadColPad ", gpuRef, nx * ny); CHECK(hipMemset(d_C, 0, nBytes)); setRowReadColDynPad << <grid, block, (BDIMX + IPAD)*BDIMY * sizeof(int) >> >(d_C); CHECK(hipMemcpy(gpuRef, d_C, nBytes, hipMemcpyDeviceToHost)); if (iprintf) printData("setRowReadColDynPad ", gpuRef, nx * ny); // free host and device memory CHECK(hipFree(d_C)); free(gpuRef); // reset device CHECK(hipDeviceReset()); system("Pause"); return EXIT_SUCCESS; }
27688d81a4b09fc80e1e4978a44e0f9211df042e.cu
#include <cuda_runtime.h> #include <stdio.h> #include <device_launch_parameters.h> #include <cstdlib> /* * An example of using shared memory to transpose rectangular thread coordinates * of a CUDA grid into a global memory array. Different kernels below * demonstrate performing reads and writes with different ordering, as well as * optimizing using memory padding. */ #define BDIMX 32 #define BDIMY 16 #define IPAD 2 #define CHECK(call) \ { \ const cudaError_t error = call; \ if (error != cudaSuccess) \ { \ fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ fprintf(stderr, "code: %d, reason: %s\n", error, \ cudaGetErrorString(error)); \ } \ } void printData(char *msg, int *in, const int size) { printf("%s: ", msg); for (int i = 0; i < size; i++) { printf("%4d", in[i]); fflush(stdout); } printf("\n\n"); } __global__ void setRowReadRow(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX]; // mapping from thread index to global memory index unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // shared memory store operation tile[threadIdx.y][threadIdx.x] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[threadIdx.y][threadIdx.x]; } __global__ void setColReadCol(int *out) { // static shared memory __shared__ int tile[BDIMX][BDIMY]; // mapping from thread index to global memory index unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // shared memory store operation tile[threadIdx.x][threadIdx.y] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[threadIdx.x][threadIdx.y]; } __global__ void setColReadCol2(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX]; // mapping from 2D thread index to linear memory unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed coordinate (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // shared memory store operation tile[icol][irow] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[icol][irow]; } __global__ void setRowReadCol(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX]; // mapping from 2D thread index to linear memory unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed coordinate (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // shared memory store operation tile[threadIdx.y][threadIdx.x] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[icol][irow]; } __global__ void setRowReadColPad(int *out) { // static shared memory __shared__ int tile[BDIMY][BDIMX + IPAD]; // mapping from 2D thread index to linear memory unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // shared memory store operation tile[threadIdx.y][threadIdx.x] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[icol][irow]; } __global__ void setRowReadColDyn(int *out) { // dynamic shared memory extern __shared__ int tile[]; // mapping from thread index to global memory index unsigned int idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed (row, col) unsigned int irow = idx / blockDim.y; unsigned int icol = idx % blockDim.y; // convert back to smem idx to access the transposed element unsigned int col_idx = icol * blockDim.x + irow; // shared memory store operation tile[idx] = idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[idx] = tile[col_idx]; } __global__ void setRowReadColDynPad(int *out) { // dynamic shared memory extern __shared__ int tile[]; // mapping from thread index to global memory index unsigned int g_idx = threadIdx.y * blockDim.x + threadIdx.x; // convert idx to transposed (row, col) unsigned int irow = g_idx / blockDim.y; unsigned int icol = g_idx % blockDim.y; unsigned int row_idx = threadIdx.y * (blockDim.x + IPAD) + threadIdx.x; // convert back to smem idx to access the transposed element unsigned int col_idx = icol * (blockDim.x + IPAD) + irow; // shared memory store operation tile[row_idx] = g_idx; // wait for all threads to complete __syncthreads(); // shared memory load operation out[g_idx] = tile[col_idx]; } int main(int argc, char **argv) { // set up device int dev = 0; cudaDeviceProp deviceProp; CHECK(cudaGetDeviceProperties(&deviceProp, dev)); printf("%s at ", argv[0]); printf("device %d: %s ", dev, deviceProp.name); CHECK(cudaSetDevice(dev)); cudaSharedMemConfig pConfig; CHECK(cudaDeviceGetSharedMemConfig(&pConfig)); printf("with Bank Mode:%s ", pConfig == 1 ? "4-Byte" : "8-Byte"); // set up array size int nx = BDIMX; int ny = BDIMY; bool iprintf = 0; if (argc > 1) iprintf = atoi(argv[1]); size_t nBytes = nx * ny * sizeof(int); // execution configuration dim3 block(BDIMX, BDIMY); dim3 grid(1, 1); printf("<<< grid (%d,%d) block (%d,%d)>>>\n", grid.x, grid.y, block.x, block.y); // allocate device memory int *d_C; CHECK(cudaMalloc((int**)&d_C, nBytes)); int *gpuRef = (int *)malloc(nBytes); CHECK(cudaMemset(d_C, 0, nBytes)); setRowReadRow << <grid, block >> >(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setRowReadRow ", gpuRef, nx * ny); CHECK(cudaMemset(d_C, 0, nBytes)); setColReadCol <<<grid, block >>>(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setColReadCol ", gpuRef, nx * ny); CHECK(cudaMemset(d_C, 0, nBytes)); setColReadCol2 << <grid, block >> >(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setColReadCol2 ", gpuRef, nx * ny); CHECK(cudaMemset(d_C, 0, nBytes)); setRowReadCol << <grid, block >> >(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setRowReadCol ", gpuRef, nx * ny); CHECK(cudaMemset(d_C, 0, nBytes)); setRowReadColDyn << <grid, block, BDIMX*BDIMY * sizeof(int) >> >(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setRowReadColDyn ", gpuRef, nx * ny); CHECK(cudaMemset(d_C, 0, nBytes)); setRowReadColPad << <grid, block >> >(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setRowReadColPad ", gpuRef, nx * ny); CHECK(cudaMemset(d_C, 0, nBytes)); setRowReadColDynPad << <grid, block, (BDIMX + IPAD)*BDIMY * sizeof(int) >> >(d_C); CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost)); if (iprintf) printData("setRowReadColDynPad ", gpuRef, nx * ny); // free host and device memory CHECK(cudaFree(d_C)); free(gpuRef); // reset device CHECK(cudaDeviceReset()); system("Pause"); return EXIT_SUCCESS; }
33243c3e304a81a5b9453acd792712f672c45391.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" __global__ void convolutionKernel(Matrix M, Matrix N, Matrix P) { __shared__ float N_s[BLOCK_SIZE][BLOCK_SIZE]; int bx = blockIdx.x; int by = blockIdx.y; int tx = threadIdx.x; int ty = threadIdx.y; int n = KERNEL_SIZE / 2; int row_o = by * TILE_SIZE + ty; int row_i = row_o - n; int col_o = bx * TILE_SIZE + tx; int col_i = col_o - n; float output = 0.0; if ((row_i >= 0) && (row_i < N.height) && (col_i >= 0) && (col_i < N.width) ) { N_s[ty][tx] = N.elements[row_i * N.width + col_i]; } else { N_s[ty][tx] = 0.0; } __syncthreads(); if ((ty < TILE_SIZE) && (tx < TILE_SIZE)) { for (int i = 0; i < KERNEL_SIZE; i++) { for (int j = 0; j < KERNEL_SIZE; j++) { output += M_c[i][j] * N_s[i+ty][j+tx]; } __syncthreads(); } if ((row_o < P.height) && (col_o < P.width) ) { P.elements[row_o * P.width + col_o] = output; } } }
33243c3e304a81a5b9453acd792712f672c45391.cu
__global__ void convolutionKernel(Matrix M, Matrix N, Matrix P) { __shared__ float N_s[BLOCK_SIZE][BLOCK_SIZE]; int bx = blockIdx.x; int by = blockIdx.y; int tx = threadIdx.x; int ty = threadIdx.y; int n = KERNEL_SIZE / 2; int row_o = by * TILE_SIZE + ty; int row_i = row_o - n; int col_o = bx * TILE_SIZE + tx; int col_i = col_o - n; float output = 0.0; if ((row_i >= 0) && (row_i < N.height) && (col_i >= 0) && (col_i < N.width) ) { N_s[ty][tx] = N.elements[row_i * N.width + col_i]; } else { N_s[ty][tx] = 0.0; } __syncthreads(); if ((ty < TILE_SIZE) && (tx < TILE_SIZE)) { for (int i = 0; i < KERNEL_SIZE; i++) { for (int j = 0; j < KERNEL_SIZE; j++) { output += M_c[i][j] * N_s[i+ty][j+tx]; } __syncthreads(); } if ((row_o < P.height) && (col_o < P.width) ) { P.elements[row_o * P.width + col_o] = output; } } }
94e144799fb71c4ff8bdefd9ef04dc9ef17fd7b6.hip
// !!! This is a file automatically generated by hipify!!! // CUDA Monte Carlo // System includes #include <stdio.h> #include <assert.h> #include <malloc.h> #include <math.h> #include <stdlib.h> #include <time.h> // CUDA runtime #include <hip/hip_runtime.h> // Helper functions and utilities to work with CUDA #include "helper_functions.h" #include "helper_cuda.h" #ifndef DEBUG #define DEBUG false #endif #ifndef M_PI #define M_PI 3.14f #endif // setting the number of trials in the monte carlo simulation: #ifndef NUMTRIALS #define NUMTRIALS 2048 #endif // !NUMTRIALS #ifndef BLOCKSIZE #define BLOCKSIZE 64 // number of threads in each block #endif // !BLOCKSIZE #ifndef NUMBLOCKS #define NUMBLOCKS NUMTRIALS/BLOCKSIZE #endif // ranges for the random numbers: const float GMIN = 20.0; // ground distance in meters const float GMAX = 30.0; // ground distance in meters const float HMIN = 10.0; // cliff height in meters const float HMAX = 40.0; // cliff height in meters const float DMIN = 10.0; // distance to castle in meters const float DMAX = 20.0; // distance to castle in meters const float VMIN = 30.0; // intial cnnonball velocity in meters / sec const float VMAX = 50.0; // intial cnnonball velocity in meters / sec const float THMIN = 70.0; // cannonball launch angle in degrees const float THMAX = 80.0; // cannonball launch angle in degrees __constant__ float GRAVITY = -9.8; // acceleraion due to gravity in meters / sec^2 __constant__ float TOL = 5.0; // tolerance in cannonball hitting the castle in meters // castle is destroyed if cannonball lands between d-TOL and d+TOL void CudaCheckError() { hipError_t e = hipGetLastError(); if (e != hipSuccess) { fprintf(stderr, "CUDA failure %s:%d: '%s'\n", __FILE__, __LINE__, hipGetErrorString(e)); } } void TimeOfDaySeed() { struct tm y2k = { 0 }; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time_t timer; time(&timer); double seconds = difftime(timer, mktime(&y2k)); unsigned int seed = (unsigned int)(1000. * seconds); // milliseconds srand(seed); } float Ranf(float low, float high) { float r = (float)rand(); // 0 - RAND_MAX float t = r / (float)RAND_MAX; // 0. - 1. return low + t * (high - low); } int Ranf(int ilow, int ihigh) { float low = (float)ilow; float high = ceil((float)ihigh); return (int)Ranf(low, high); } // degrees-to-radians -- callable from the device: __device__ float Radians(float d) { return (M_PI / 180.f) * d; } // the kernel: __global__ void MonteCarlo(float* dvs, float* dths, float* dgs, float* dhs, float* dds, int* dhits) { unsigned int gid = blockIdx.x * blockDim.x + threadIdx.x; // randomize everything: float v = dvs[gid]; float thr = Radians(dths[gid]); float vx = v * cos(thr); float vy = v * sin(thr); float g = dgs[gid]; float h = dhs[gid]; float d = dds[gid]; int numHits = 0; // see if the ball doesn't even reach the cliff: float t = -vy / (0.5 * GRAVITY); float x = vx * t; if (x > g) { t = g / vx; float y = vy * t + 0.5 * GRAVITY * t * t; if (y > h) { float a = 0.5 * GRAVITY; float b = vy; float c = -h; float disc = b * b - 4.f * a * c; if (disc > 0.) { // successfully hits the ground above the cliff: // get the intersection: disc = sqrtf(disc); float t1 = (-b + disc) / (2.f * a); // time to intersect high ground float t2 = (-b - disc) / (2.f * a); // time to intersect high ground // only care about the second intersection float tmax = t1; if (t2 > t1) tmax = t2; // how far does the ball land horizontlly from the edge of the cliff? float upperDist = vx * tmax - g; // see if the ball hits the castle: if (fabs(upperDist - d) < TOL) { numHits += 1; } } } } dhits[gid] = numHits; } // these two #defines are just to label things // other than that, they do nothing: #define IN #define OUT // main program: int main(int argc, char* argv[]) { FILE* file = fopen("project5.csv", "a"); TimeOfDaySeed(); int dev = findCudaDevice(argc, (const char**)argv); // better to define these here so that the rand() calls don't get into the thread timing: float* hvs = new float[NUMTRIALS]; float* hths = new float[NUMTRIALS]; float* hgs = new float[NUMTRIALS]; float* hhs = new float[NUMTRIALS]; float* hds = new float[NUMTRIALS]; int* hhits = new int[NUMTRIALS]; // fill the random-value array: for (int n = 0; n < NUMTRIALS; n++) { hvs[n] = Ranf(VMIN, VMAX); hths[n] = Ranf(THMIN, THMAX); hgs[n] = Ranf(GMIN, GMAX); hhs[n] = Ranf(HMIN, HMAX); hds[n] = Ranf(DMIN, DMAX); } // allocate device memory: float *dvs, *dths, *dgs, *dhs, *dds; int *dhits; hipMalloc(&dvs, NUMTRIALS * sizeof(float)); hipMalloc(&dths, NUMTRIALS * sizeof(float)); hipMalloc(&dgs, NUMTRIALS * sizeof(float)); hipMalloc(&dhs, NUMTRIALS * sizeof(float)); hipMalloc(&dds, NUMTRIALS * sizeof(float)); hipMalloc(&dhits, NUMTRIALS * sizeof(int)); CudaCheckError(); // copy host memory to the device: hipMemcpy(dvs, hvs, NUMTRIALS * sizeof(float), hipMemcpyHostToDevice); hipMemcpy(dths, hths, NUMTRIALS * sizeof(float), hipMemcpyHostToDevice); hipMemcpy(dgs, hgs, NUMTRIALS * sizeof(float), hipMemcpyHostToDevice); hipMemcpy(dhs, hhs, NUMTRIALS * sizeof(float), hipMemcpyHostToDevice); hipMemcpy(dds, hds, NUMTRIALS * sizeof(float), hipMemcpyHostToDevice); CudaCheckError(); // setup the execution parameters: dim3 grid(NUMBLOCKS, 1, 1); dim3 threads(BLOCKSIZE, 1, 1); // allocate the events that we'll use for timing: hipEvent_t start, stop; hipEventCreate(&start); hipEventCreate(&stop); CudaCheckError(); // create and start the timer: hipDeviceSynchronize(); // record the start event: hipEventRecord(start, NULL); CudaCheckError(); // execute the kernel: hipLaunchKernelGGL(( MonteCarlo) , dim3(grid), dim3(threads) , 0, 0, IN dvs, IN dths, IN dgs, IN dhs, IN dds, OUT dhits); // record the stop event: hipEventRecord(stop, NULL); CudaCheckError(); // wait for the stop event to complete: hipDeviceSynchronize(); hipEventSynchronize(stop); CudaCheckError(); float msecTotal; hipEventElapsedTime(&msecTotal, start, stop); CudaCheckError(); // compute and print the performance double secondsTotal = 0.001 * (double)msecTotal; double megaTrialsPerSecond = (double)NUMTRIALS / secondsTotal / 1000000.; // fprintf(stderr, "NUMTRIALS = %d , BLOCKSIZE = %d , megaTrials/Sec = %lf\n", NUMTRIALS, BLOCKSIZE, megaTrialsPerSecond); // copy result from the device to the host: hipMemcpy(hhits, dhits, NUMTRIALS * sizeof(int), hipMemcpyDeviceToHost); CudaCheckError(); // add up the hhits[ ] array: : int totalhits = 0; for (int i = 0; i < NUMTRIALS; i++) { totalhits += hhits[i]; } // compute and print the probability: float probability = (float)totalhits / (float)NUMTRIALS; // fprintf(stderr, "probability = %lf \n", 100. * probability); fprintf(file, "%d,%d,%lf,%lf,%lf\n", NUMTRIALS, BLOCKSIZE, megaTrialsPerSecond, 100. * probability); // clean up host memory: delete[] hvs; delete[] hths; delete[] hgs; delete[] hhs; delete[] hds; delete[] hhits; // clean up device memory: hipFree(dvs); hipFree(dths); hipFree(dgs); hipFree(dhs); hipFree(dds); hipFree(dhits); CudaCheckError(); return 0; }
94e144799fb71c4ff8bdefd9ef04dc9ef17fd7b6.cu
 // CUDA Monte Carlo // System includes #include <stdio.h> #include <assert.h> #include <malloc.h> #include <math.h> #include <stdlib.h> #include <time.h> // CUDA runtime #include <cuda_runtime.h> // Helper functions and utilities to work with CUDA #include "helper_functions.h" #include "helper_cuda.h" #ifndef DEBUG #define DEBUG false #endif #ifndef M_PI #define M_PI 3.14f #endif // setting the number of trials in the monte carlo simulation: #ifndef NUMTRIALS #define NUMTRIALS 2048 #endif // !NUMTRIALS #ifndef BLOCKSIZE #define BLOCKSIZE 64 // number of threads in each block #endif // !BLOCKSIZE #ifndef NUMBLOCKS #define NUMBLOCKS NUMTRIALS/BLOCKSIZE #endif // ranges for the random numbers: const float GMIN = 20.0; // ground distance in meters const float GMAX = 30.0; // ground distance in meters const float HMIN = 10.0; // cliff height in meters const float HMAX = 40.0; // cliff height in meters const float DMIN = 10.0; // distance to castle in meters const float DMAX = 20.0; // distance to castle in meters const float VMIN = 30.0; // intial cnnonball velocity in meters / sec const float VMAX = 50.0; // intial cnnonball velocity in meters / sec const float THMIN = 70.0; // cannonball launch angle in degrees const float THMAX = 80.0; // cannonball launch angle in degrees __constant__ float GRAVITY = -9.8; // acceleraion due to gravity in meters / sec^2 __constant__ float TOL = 5.0; // tolerance in cannonball hitting the castle in meters // castle is destroyed if cannonball lands between d-TOL and d+TOL void CudaCheckError() { cudaError_t e = cudaGetLastError(); if (e != cudaSuccess) { fprintf(stderr, "CUDA failure %s:%d: '%s'\n", __FILE__, __LINE__, cudaGetErrorString(e)); } } void TimeOfDaySeed() { struct tm y2k = { 0 }; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time_t timer; time(&timer); double seconds = difftime(timer, mktime(&y2k)); unsigned int seed = (unsigned int)(1000. * seconds); // milliseconds srand(seed); } float Ranf(float low, float high) { float r = (float)rand(); // 0 - RAND_MAX float t = r / (float)RAND_MAX; // 0. - 1. return low + t * (high - low); } int Ranf(int ilow, int ihigh) { float low = (float)ilow; float high = ceil((float)ihigh); return (int)Ranf(low, high); } // degrees-to-radians -- callable from the device: __device__ float Radians(float d) { return (M_PI / 180.f) * d; } // the kernel: __global__ void MonteCarlo(float* dvs, float* dths, float* dgs, float* dhs, float* dds, int* dhits) { unsigned int gid = blockIdx.x * blockDim.x + threadIdx.x; // randomize everything: float v = dvs[gid]; float thr = Radians(dths[gid]); float vx = v * cos(thr); float vy = v * sin(thr); float g = dgs[gid]; float h = dhs[gid]; float d = dds[gid]; int numHits = 0; // see if the ball doesn't even reach the cliff: float t = -vy / (0.5 * GRAVITY); float x = vx * t; if (x > g) { t = g / vx; float y = vy * t + 0.5 * GRAVITY * t * t; if (y > h) { float a = 0.5 * GRAVITY; float b = vy; float c = -h; float disc = b * b - 4.f * a * c; if (disc > 0.) { // successfully hits the ground above the cliff: // get the intersection: disc = sqrtf(disc); float t1 = (-b + disc) / (2.f * a); // time to intersect high ground float t2 = (-b - disc) / (2.f * a); // time to intersect high ground // only care about the second intersection float tmax = t1; if (t2 > t1) tmax = t2; // how far does the ball land horizontlly from the edge of the cliff? float upperDist = vx * tmax - g; // see if the ball hits the castle: if (fabs(upperDist - d) < TOL) { numHits += 1; } } } } dhits[gid] = numHits; } // these two #defines are just to label things // other than that, they do nothing: #define IN #define OUT // main program: int main(int argc, char* argv[]) { FILE* file = fopen("project5.csv", "a"); TimeOfDaySeed(); int dev = findCudaDevice(argc, (const char**)argv); // better to define these here so that the rand() calls don't get into the thread timing: float* hvs = new float[NUMTRIALS]; float* hths = new float[NUMTRIALS]; float* hgs = new float[NUMTRIALS]; float* hhs = new float[NUMTRIALS]; float* hds = new float[NUMTRIALS]; int* hhits = new int[NUMTRIALS]; // fill the random-value array: for (int n = 0; n < NUMTRIALS; n++) { hvs[n] = Ranf(VMIN, VMAX); hths[n] = Ranf(THMIN, THMAX); hgs[n] = Ranf(GMIN, GMAX); hhs[n] = Ranf(HMIN, HMAX); hds[n] = Ranf(DMIN, DMAX); } // allocate device memory: float *dvs, *dths, *dgs, *dhs, *dds; int *dhits; cudaMalloc(&dvs, NUMTRIALS * sizeof(float)); cudaMalloc(&dths, NUMTRIALS * sizeof(float)); cudaMalloc(&dgs, NUMTRIALS * sizeof(float)); cudaMalloc(&dhs, NUMTRIALS * sizeof(float)); cudaMalloc(&dds, NUMTRIALS * sizeof(float)); cudaMalloc(&dhits, NUMTRIALS * sizeof(int)); CudaCheckError(); // copy host memory to the device: cudaMemcpy(dvs, hvs, NUMTRIALS * sizeof(float), cudaMemcpyHostToDevice); cudaMemcpy(dths, hths, NUMTRIALS * sizeof(float), cudaMemcpyHostToDevice); cudaMemcpy(dgs, hgs, NUMTRIALS * sizeof(float), cudaMemcpyHostToDevice); cudaMemcpy(dhs, hhs, NUMTRIALS * sizeof(float), cudaMemcpyHostToDevice); cudaMemcpy(dds, hds, NUMTRIALS * sizeof(float), cudaMemcpyHostToDevice); CudaCheckError(); // setup the execution parameters: dim3 grid(NUMBLOCKS, 1, 1); dim3 threads(BLOCKSIZE, 1, 1); // allocate the events that we'll use for timing: cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop); CudaCheckError(); // create and start the timer: cudaDeviceSynchronize(); // record the start event: cudaEventRecord(start, NULL); CudaCheckError(); // execute the kernel: MonteCarlo <<< grid, threads >>> (IN dvs, IN dths, IN dgs, IN dhs, IN dds, OUT dhits); // record the stop event: cudaEventRecord(stop, NULL); CudaCheckError(); // wait for the stop event to complete: cudaDeviceSynchronize(); cudaEventSynchronize(stop); CudaCheckError(); float msecTotal; cudaEventElapsedTime(&msecTotal, start, stop); CudaCheckError(); // compute and print the performance double secondsTotal = 0.001 * (double)msecTotal; double megaTrialsPerSecond = (double)NUMTRIALS / secondsTotal / 1000000.; // fprintf(stderr, "NUMTRIALS = %d , BLOCKSIZE = %d , megaTrials/Sec = %lf\n", NUMTRIALS, BLOCKSIZE, megaTrialsPerSecond); // copy result from the device to the host: cudaMemcpy(hhits, dhits, NUMTRIALS * sizeof(int), cudaMemcpyDeviceToHost); CudaCheckError(); // add up the hhits[ ] array: : int totalhits = 0; for (int i = 0; i < NUMTRIALS; i++) { totalhits += hhits[i]; } // compute and print the probability: float probability = (float)totalhits / (float)NUMTRIALS; // fprintf(stderr, "probability = %lf \n", 100. * probability); fprintf(file, "%d,%d,%lf,%lf,%lf\n", NUMTRIALS, BLOCKSIZE, megaTrialsPerSecond, 100. * probability); // clean up host memory: delete[] hvs; delete[] hths; delete[] hgs; delete[] hhs; delete[] hds; delete[] hhits; // clean up device memory: cudaFree(dvs); cudaFree(dths); cudaFree(dgs); cudaFree(dhs); cudaFree(dds); cudaFree(dhits); CudaCheckError(); return 0; }
86cde8cbd794fd09f5ed4a723dcd969149ccac46.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" __global__ void prefSumBinTreeCudaMulti(float *a, int n) { __shared__ float shm[CUDA_THREAD_NUM]; int tid=threadIdx.x; int bid=blockIdx.x; int dot=2;//depth of tree if((tid+1)%dot==0) { shm[tid]=a[CUDA_THREAD_NUM*bid+tid]+a[CUDA_THREAD_NUM*bid+tid-1]; } dot*=2; __syncthreads(); while(dot<=n) { if((tid+1)%dot==0) { shm[tid]=shm[tid]+shm[tid-dot/2]; } dot*=2; __syncthreads(); } dot/=2; while(dot>2) { if((tid+1)%dot==0) { if((tid+1)/dot!=1) { shm[tid-dot/2]=shm[tid-dot/2]+shm[tid-dot]; } } dot/=2; __syncthreads(); } if((tid+1)%2==0) { a[CUDA_THREAD_NUM*bid+tid]=shm[tid]; } else if(tid>0) { a[CUDA_THREAD_NUM*bid+tid]=a[CUDA_THREAD_NUM*bid+tid]+shm[tid-1]; } }
86cde8cbd794fd09f5ed4a723dcd969149ccac46.cu
#include "includes.h" __global__ void prefSumBinTreeCudaMulti(float *a, int n) { __shared__ float shm[CUDA_THREAD_NUM]; int tid=threadIdx.x; int bid=blockIdx.x; int dot=2;//depth of tree if((tid+1)%dot==0) { shm[tid]=a[CUDA_THREAD_NUM*bid+tid]+a[CUDA_THREAD_NUM*bid+tid-1]; } dot*=2; __syncthreads(); while(dot<=n) { if((tid+1)%dot==0) { shm[tid]=shm[tid]+shm[tid-dot/2]; } dot*=2; __syncthreads(); } dot/=2; while(dot>2) { if((tid+1)%dot==0) { if((tid+1)/dot!=1) { shm[tid-dot/2]=shm[tid-dot/2]+shm[tid-dot]; } } dot/=2; __syncthreads(); } if((tid+1)%2==0) { a[CUDA_THREAD_NUM*bid+tid]=shm[tid]; } else if(tid>0) { a[CUDA_THREAD_NUM*bid+tid]=a[CUDA_THREAD_NUM*bid+tid]+shm[tid-1]; } }
0dd77c09cba3cdc3d74627888f8a9bb18939bf58.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" __device__ float maxMetricPoints(const float* g_uquery, const float* g_vpoint, int pointdim, int signallength){ float r_u1; float r_v1; float r_d1,r_dim=0; r_dim=0; for(int d=0; d<pointdim; d++){ r_u1 = *(g_uquery+d*signallength); r_v1 = *(g_vpoint+d*signallength); r_d1 = r_v1 - r_u1; r_d1 = r_d1 < 0? -r_d1: r_d1; //abs r_dim= r_dim < r_d1? r_d1: r_dim; } return r_dim; } __global__ void kernelBFRSMultishared(const float* g_uquery, const float* g_vpointset, int *g_npoints, int pointdim, int triallength, int signallength, int exclude, const float* vecradius) { // shared memory extern __shared__ char array[]; int *s_npointsrange; s_npointsrange = (int*)array; float radius=0; const unsigned int tid = threadIdx.x + blockDim.x*blockIdx.x; const unsigned int itrial = tid / triallength; // indextrial if(tid<signallength){ s_npointsrange[threadIdx.x] = 0; __syncthreads(); radius = *(vecradius+itrial); unsigned int indexi = tid-triallength*itrial; for(int t=0; t<triallength; t++){ int indexu = tid; int indexv = (t + itrial*triallength); int condition1=indexi-exclude; int condition2=indexi+exclude; if((t<condition1)||(t>condition2)){ float temp_dist = maxMetricPoints(g_uquery+indexu, g_vpointset+indexv,pointdim, signallength); if(temp_dist <= radius){ s_npointsrange[threadIdx.x]++; } } } __syncthreads(); //printf("\ntid:%d npoints: %d\n",tid, s_npointsrange[threadIdx.x]); //COPY TO GLOBAL MEMORY g_npoints[tid] = s_npointsrange[threadIdx.x]; } }
0dd77c09cba3cdc3d74627888f8a9bb18939bf58.cu
#include "includes.h" __device__ float maxMetricPoints(const float* g_uquery, const float* g_vpoint, int pointdim, int signallength){ float r_u1; float r_v1; float r_d1,r_dim=0; r_dim=0; for(int d=0; d<pointdim; d++){ r_u1 = *(g_uquery+d*signallength); r_v1 = *(g_vpoint+d*signallength); r_d1 = r_v1 - r_u1; r_d1 = r_d1 < 0? -r_d1: r_d1; //abs r_dim= r_dim < r_d1? r_d1: r_dim; } return r_dim; } __global__ void kernelBFRSMultishared(const float* g_uquery, const float* g_vpointset, int *g_npoints, int pointdim, int triallength, int signallength, int exclude, const float* vecradius) { // shared memory extern __shared__ char array[]; int *s_npointsrange; s_npointsrange = (int*)array; float radius=0; const unsigned int tid = threadIdx.x + blockDim.x*blockIdx.x; const unsigned int itrial = tid / triallength; // indextrial if(tid<signallength){ s_npointsrange[threadIdx.x] = 0; __syncthreads(); radius = *(vecradius+itrial); unsigned int indexi = tid-triallength*itrial; for(int t=0; t<triallength; t++){ int indexu = tid; int indexv = (t + itrial*triallength); int condition1=indexi-exclude; int condition2=indexi+exclude; if((t<condition1)||(t>condition2)){ float temp_dist = maxMetricPoints(g_uquery+indexu, g_vpointset+indexv,pointdim, signallength); if(temp_dist <= radius){ s_npointsrange[threadIdx.x]++; } } } __syncthreads(); //printf("\ntid:%d npoints: %d\n",tid, s_npointsrange[threadIdx.x]); //COPY TO GLOBAL MEMORY g_npoints[tid] = s_npointsrange[threadIdx.x]; } }
942c95b4ffcd39502cf1774fcb46d1e9ad28a186.hip
// !!! This is a file automatically generated by hipify!!! // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pybind11/stl.h" #include "contrib_ops/rocm/bert/batched_gemm_softmax_gemm_permute_pipelines.cuh" #include "core/providers/rocm/tunable/rocm_tunable.h" #include "python/tools/kernel_explorer/device_array.h" #include "python/tools/kernel_explorer/kernel_explorer_interface.h" #include <vector> namespace py = pybind11; using namespace onnxruntime::contrib::rocm; namespace onnxruntime { template <typename T> class IGemmSoftmaxGemmPermuteKernelExplorer : public IKernelExplorer { public: IGemmSoftmaxGemmPermuteKernelExplorer( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) { ROCBLAS_CALL_THROW(rocblas_create_handle(&rocblas_handle_)); attn_.batch_size = batch; attn_.sequence_length = seqlen; attn_.kv_sequence_length = seqlen; // NOTE: not used attn_.past_sequence_length = 0; attn_.original_past_sequence_length = 0; // NOTE: not used attn_.total_sequence_length = total_seqlen; attn_.max_sequence_length = 0; attn_.hidden_size = num_heads * head_size; attn_.head_size = head_size; attn_.v_hidden_size = attn_.hidden_size; // Q,K,V hidden size must agree now attn_.v_head_size = attn_.head_size; // Q,K,V hidden size must agree now attn_.num_heads = num_heads; attn_.is_unidirectional = false; attn_.past_present_share_buffer = false; attn_.do_rotary = false; attn_.mask_filter_value = -10000.0f; attn_.scale = scale; if (mask_dim == 0) { attn_.mask_type = contrib::MASK_NONE; } else if (mask_dim == 2) { attn_.mask_type = contrib::MASK_2D_KEY_PADDING; } else if (mask_dim == 3) { attn_.mask_type = contrib::MASK_3D_ATTENTION; } else if (mask_dim == 4) { attn_.mask_type = contrib::MASK_4D_MEGATRON; } else { ORT_ENFORCE(false, "mask type not supported"); } device_prop = GetEp()->GetDeviceProp(); params_.tuning_ctx = TuningContext(); params_.stream = Stream(); params_.handle = rocblas_handle_; params_.attention = &attn_; params_.device_prop = &device_prop; params_.scale = scale; params_.q_buffer = reinterpret_cast<T*>(Q.ptr()); params_.k_buffer = reinterpret_cast<T*>(K.ptr()); params_.v_buffer = reinterpret_cast<T*>(V.ptr()); if (attn_bias.has_value()) { params_.bias_buffer = reinterpret_cast<T*>(attn_bias->ptr()); } if (attn_mask.has_value()) { params_.mask_index_buffer = reinterpret_cast<int*>(attn_mask->ptr()); if (mask_dim == 2) { params_.mask_index_dims = {batch, total_seqlen}; } else if (mask_dim == 3) { params_.mask_index_dims = {batch, seqlen, total_seqlen}; } else if (mask_dim == 4) { ORT_ENFORCE(max_seqlen.has_value()); attn_.max_sequence_length = max_seqlen.value(); ORT_ENFORCE(attn_.max_sequence_length >= seqlen); attn_.past_sequence_length = attn_.max_sequence_length - seqlen; params_.mask_index_dims = {batch, 1, attn_.max_sequence_length, attn_.max_sequence_length}; } } params_.out_buffer = reinterpret_cast<T*>(out.ptr()); } ~IGemmSoftmaxGemmPermuteKernelExplorer() { ROCBLAS_CALL_THROW(rocblas_destroy_handle(rocblas_handle_)); } void SetWorkspace(size_t num_bytes) { void* ptr; HIP_CALL_THROW(hipMalloc(&ptr, num_bytes)); workspace_.reset(ptr, [](void* ptr) { HIP_CALL_THROW(hipFree(ptr)); }); params_.workspace_buffer = reinterpret_cast<T*>(workspace_.get()); } protected: using ParamsT = contrib::rocm::GemmSoftmaxGemmPermuteParams<T>; rocblas_handle rocblas_handle_; hipDeviceProp_t device_prop; contrib::AttentionParameters attn_; ParamsT params_; std::shared_ptr<void> workspace_; }; // The pipeline composed from rocblas api calls and kernel launches. template <typename T> class GemmSoftmaxGemmPermuteGeneric : public IGemmSoftmaxGemmPermuteKernelExplorer<T> { public: GemmSoftmaxGemmPermuteGeneric( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) : IGemmSoftmaxGemmPermuteKernelExplorer<T>(batch, seqlen, total_seqlen, max_seqlen, num_heads, head_size, mask_dim, scale, Q, K, V, attn_bias, attn_mask, out) { this->SetWorkspace(GemmSoftmaxGemmPermuteGenericPipeline<T>::GetWorkspaceNumBytes(&this->attn_)); } std::vector<std::string> ListOps() const { return {"Generic"}; } bool SelectOp(const std::string&) { return true; } void Run() override { ORT_THROW_IF_ERROR(GemmSoftmaxGemmPermuteGenericPipeline<T>::Run( &this->params_, /*use_persistent_softmax=*/false)); } }; #ifdef USE_COMPOSABLE_KERNEL template <typename T, bool USE_BIAS, bool USE_MASK> class GemmSoftmaxGemmPermuteCK : public IGemmSoftmaxGemmPermuteKernelExplorer<T> { public: GemmSoftmaxGemmPermuteCK( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) : IGemmSoftmaxGemmPermuteKernelExplorer<T>(batch, seqlen, total_seqlen, max_seqlen, num_heads, head_size, mask_dim, scale, Q, K, V, attn_bias, attn_mask, out) { this->SetWorkspace(GemmSoftmaxGemmPermuteTunableOp<T>::GetWorkspaceNumBytes(&this->attn_)); for (auto&& [ts, op] : GetCKGemmSoftmaxGemmPermuteTypeStringAndOps<T, USE_BIAS, USE_MASK>()) { type_strings_.emplace_back(std::move(ts)); ops_.emplace_back(std::move(op)); } } std::vector<std::string> ListOps() const { return type_strings_; } bool SelectOp(const std::string& name) { for (size_t i = 0; i < ops_.size(); i++) { if (type_strings_[i] == name) { selected_op_ = i; Status status = ops_[i].IsSupported(&this->params_); return status.IsOK(); } } ORT_THROW("Cannot find implementation ", name); } void Run() override { ORT_THROW_IF_ERROR(ops_[selected_op_](&this->params_)); } private: using ParamsT = typename IGemmSoftmaxGemmPermuteKernelExplorer<T>::ParamsT; using OpT = Op<ParamsT>; std::vector<OpT> ops_; std::vector<std::string> type_strings_; size_t selected_op_{}; }; #endif // USE_COMPOSABLE_KERNEL // The pipeline composed from rocblas api calls and kernel launches. template <typename T> class GemmSoftmaxGemmPermuteTunable : public IGemmSoftmaxGemmPermuteKernelExplorer<T> { public: GemmSoftmaxGemmPermuteTunable( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) : IGemmSoftmaxGemmPermuteKernelExplorer<T>(batch, seqlen, total_seqlen, max_seqlen, num_heads, head_size, mask_dim, scale, Q, K, V, attn_bias, attn_mask, out) { this->SetWorkspace(::max( GemmSoftmaxGemmPermuteGenericPipeline<T>::GetWorkspaceNumBytes(&this->attn_), GemmSoftmaxGemmPermuteTunableOp<T>::GetWorkspaceNumBytes(&this->attn_))); this->params_.TuningContext()->EnableTunableOp(); } std::vector<std::string> ListOps() const { return {"Tunable"}; } bool SelectOp(const std::string&) { return true; } void Run() override { ORT_THROW_IF_ERROR(GemmSoftmaxGemmPermuteTunableOp<T>{}(&this->params_)); } }; #define REGISTER_COMMON(name, type, ...) \ py::class_<type<__VA_ARGS__>>(m, name) \ .def(py::init<int64_t, int64_t, int64_t, std::optional<int64_t>, int64_t, int64_t, int64_t, \ float, \ DeviceArray&, \ DeviceArray&, \ DeviceArray&, \ std::optional<DeviceArray>&, \ std::optional<DeviceArray>&, \ DeviceArray&>()) \ .def("SetRepeats", &type<__VA_ARGS__>::SetRepeats) \ .def("Run", &type<__VA_ARGS__>::Run) \ .def("Profile", &type<__VA_ARGS__>::Profile) \ .def("ListOps", &type<__VA_ARGS__>::ListOps) \ .def("SelectOp", &type<__VA_ARGS__>::SelectOp); #define REGISTER_GENERIC(dtype) \ REGISTER_COMMON("GemmSoftmaxGemmPermuteGeneric_" #dtype, GemmSoftmaxGemmPermuteGeneric, dtype) #define REGISTER_CK(dtype, biased, masked, mask_bias_suffix) \ REGISTER_COMMON( \ "GemmSoftmaxGemmPermuteCK" mask_bias_suffix "_" #dtype, GemmSoftmaxGemmPermuteCK, dtype, biased, masked) #define REGISTER_TUNABLE(dtype) \ REGISTER_COMMON("GemmSoftmaxGemmPermuteTunable_" #dtype, GemmSoftmaxGemmPermuteTunable, dtype) KE_REGISTER(m) { REGISTER_GENERIC(half); #ifdef USE_COMPOSABLE_KERNEL REGISTER_CK(half, false, false, ""); REGISTER_CK(half, true, false, "Biased"); REGISTER_CK(half, false, true, "Masked"); REGISTER_CK(half, true, true, "BiasedMasked"); #endif REGISTER_TUNABLE(half); } } // namespace onnxruntime
942c95b4ffcd39502cf1774fcb46d1e9ad28a186.cu
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pybind11/stl.h" #include "contrib_ops/rocm/bert/batched_gemm_softmax_gemm_permute_pipelines.cuh" #include "core/providers/rocm/tunable/rocm_tunable.h" #include "python/tools/kernel_explorer/device_array.h" #include "python/tools/kernel_explorer/kernel_explorer_interface.h" #include <vector> namespace py = pybind11; using namespace onnxruntime::contrib::rocm; namespace onnxruntime { template <typename T> class IGemmSoftmaxGemmPermuteKernelExplorer : public IKernelExplorer { public: IGemmSoftmaxGemmPermuteKernelExplorer( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) { ROCBLAS_CALL_THROW(rocblas_create_handle(&rocblas_handle_)); attn_.batch_size = batch; attn_.sequence_length = seqlen; attn_.kv_sequence_length = seqlen; // NOTE: not used attn_.past_sequence_length = 0; attn_.original_past_sequence_length = 0; // NOTE: not used attn_.total_sequence_length = total_seqlen; attn_.max_sequence_length = 0; attn_.hidden_size = num_heads * head_size; attn_.head_size = head_size; attn_.v_hidden_size = attn_.hidden_size; // Q,K,V hidden size must agree now attn_.v_head_size = attn_.head_size; // Q,K,V hidden size must agree now attn_.num_heads = num_heads; attn_.is_unidirectional = false; attn_.past_present_share_buffer = false; attn_.do_rotary = false; attn_.mask_filter_value = -10000.0f; attn_.scale = scale; if (mask_dim == 0) { attn_.mask_type = contrib::MASK_NONE; } else if (mask_dim == 2) { attn_.mask_type = contrib::MASK_2D_KEY_PADDING; } else if (mask_dim == 3) { attn_.mask_type = contrib::MASK_3D_ATTENTION; } else if (mask_dim == 4) { attn_.mask_type = contrib::MASK_4D_MEGATRON; } else { ORT_ENFORCE(false, "mask type not supported"); } device_prop = GetEp()->GetDeviceProp(); params_.tuning_ctx = TuningContext(); params_.stream = Stream(); params_.handle = rocblas_handle_; params_.attention = &attn_; params_.device_prop = &device_prop; params_.scale = scale; params_.q_buffer = reinterpret_cast<T*>(Q.ptr()); params_.k_buffer = reinterpret_cast<T*>(K.ptr()); params_.v_buffer = reinterpret_cast<T*>(V.ptr()); if (attn_bias.has_value()) { params_.bias_buffer = reinterpret_cast<T*>(attn_bias->ptr()); } if (attn_mask.has_value()) { params_.mask_index_buffer = reinterpret_cast<int*>(attn_mask->ptr()); if (mask_dim == 2) { params_.mask_index_dims = {batch, total_seqlen}; } else if (mask_dim == 3) { params_.mask_index_dims = {batch, seqlen, total_seqlen}; } else if (mask_dim == 4) { ORT_ENFORCE(max_seqlen.has_value()); attn_.max_sequence_length = max_seqlen.value(); ORT_ENFORCE(attn_.max_sequence_length >= seqlen); attn_.past_sequence_length = attn_.max_sequence_length - seqlen; params_.mask_index_dims = {batch, 1, attn_.max_sequence_length, attn_.max_sequence_length}; } } params_.out_buffer = reinterpret_cast<T*>(out.ptr()); } ~IGemmSoftmaxGemmPermuteKernelExplorer() { ROCBLAS_CALL_THROW(rocblas_destroy_handle(rocblas_handle_)); } void SetWorkspace(size_t num_bytes) { void* ptr; HIP_CALL_THROW(hipMalloc(&ptr, num_bytes)); workspace_.reset(ptr, [](void* ptr) { HIP_CALL_THROW(hipFree(ptr)); }); params_.workspace_buffer = reinterpret_cast<T*>(workspace_.get()); } protected: using ParamsT = contrib::rocm::GemmSoftmaxGemmPermuteParams<T>; rocblas_handle rocblas_handle_; hipDeviceProp_t device_prop; contrib::AttentionParameters attn_; ParamsT params_; std::shared_ptr<void> workspace_; }; // The pipeline composed from rocblas api calls and kernel launches. template <typename T> class GemmSoftmaxGemmPermuteGeneric : public IGemmSoftmaxGemmPermuteKernelExplorer<T> { public: GemmSoftmaxGemmPermuteGeneric( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) : IGemmSoftmaxGemmPermuteKernelExplorer<T>(batch, seqlen, total_seqlen, max_seqlen, num_heads, head_size, mask_dim, scale, Q, K, V, attn_bias, attn_mask, out) { this->SetWorkspace(GemmSoftmaxGemmPermuteGenericPipeline<T>::GetWorkspaceNumBytes(&this->attn_)); } std::vector<std::string> ListOps() const { return {"Generic"}; } bool SelectOp(const std::string&) { return true; } void Run() override { ORT_THROW_IF_ERROR(GemmSoftmaxGemmPermuteGenericPipeline<T>::Run( &this->params_, /*use_persistent_softmax=*/false)); } }; #ifdef USE_COMPOSABLE_KERNEL template <typename T, bool USE_BIAS, bool USE_MASK> class GemmSoftmaxGemmPermuteCK : public IGemmSoftmaxGemmPermuteKernelExplorer<T> { public: GemmSoftmaxGemmPermuteCK( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) : IGemmSoftmaxGemmPermuteKernelExplorer<T>(batch, seqlen, total_seqlen, max_seqlen, num_heads, head_size, mask_dim, scale, Q, K, V, attn_bias, attn_mask, out) { this->SetWorkspace(GemmSoftmaxGemmPermuteTunableOp<T>::GetWorkspaceNumBytes(&this->attn_)); for (auto&& [ts, op] : GetCKGemmSoftmaxGemmPermuteTypeStringAndOps<T, USE_BIAS, USE_MASK>()) { type_strings_.emplace_back(std::move(ts)); ops_.emplace_back(std::move(op)); } } std::vector<std::string> ListOps() const { return type_strings_; } bool SelectOp(const std::string& name) { for (size_t i = 0; i < ops_.size(); i++) { if (type_strings_[i] == name) { selected_op_ = i; Status status = ops_[i].IsSupported(&this->params_); return status.IsOK(); } } ORT_THROW("Cannot find implementation ", name); } void Run() override { ORT_THROW_IF_ERROR(ops_[selected_op_](&this->params_)); } private: using ParamsT = typename IGemmSoftmaxGemmPermuteKernelExplorer<T>::ParamsT; using OpT = Op<ParamsT>; std::vector<OpT> ops_; std::vector<std::string> type_strings_; size_t selected_op_{}; }; #endif // USE_COMPOSABLE_KERNEL // The pipeline composed from rocblas api calls and kernel launches. template <typename T> class GemmSoftmaxGemmPermuteTunable : public IGemmSoftmaxGemmPermuteKernelExplorer<T> { public: GemmSoftmaxGemmPermuteTunable( int64_t batch, int64_t seqlen, int64_t total_seqlen, std::optional<int64_t> max_seqlen, int64_t num_heads, int64_t head_size, int64_t mask_dim, double scale, DeviceArray& Q, DeviceArray& K, DeviceArray& V, std::optional<DeviceArray>& attn_bias, std::optional<DeviceArray>& attn_mask, DeviceArray& out) : IGemmSoftmaxGemmPermuteKernelExplorer<T>(batch, seqlen, total_seqlen, max_seqlen, num_heads, head_size, mask_dim, scale, Q, K, V, attn_bias, attn_mask, out) { this->SetWorkspace(std::max( GemmSoftmaxGemmPermuteGenericPipeline<T>::GetWorkspaceNumBytes(&this->attn_), GemmSoftmaxGemmPermuteTunableOp<T>::GetWorkspaceNumBytes(&this->attn_))); this->params_.TuningContext()->EnableTunableOp(); } std::vector<std::string> ListOps() const { return {"Tunable"}; } bool SelectOp(const std::string&) { return true; } void Run() override { ORT_THROW_IF_ERROR(GemmSoftmaxGemmPermuteTunableOp<T>{}(&this->params_)); } }; #define REGISTER_COMMON(name, type, ...) \ py::class_<type<__VA_ARGS__>>(m, name) \ .def(py::init<int64_t, int64_t, int64_t, std::optional<int64_t>, int64_t, int64_t, int64_t, \ float, \ DeviceArray&, \ DeviceArray&, \ DeviceArray&, \ std::optional<DeviceArray>&, \ std::optional<DeviceArray>&, \ DeviceArray&>()) \ .def("SetRepeats", &type<__VA_ARGS__>::SetRepeats) \ .def("Run", &type<__VA_ARGS__>::Run) \ .def("Profile", &type<__VA_ARGS__>::Profile) \ .def("ListOps", &type<__VA_ARGS__>::ListOps) \ .def("SelectOp", &type<__VA_ARGS__>::SelectOp); #define REGISTER_GENERIC(dtype) \ REGISTER_COMMON("GemmSoftmaxGemmPermuteGeneric_" #dtype, GemmSoftmaxGemmPermuteGeneric, dtype) #define REGISTER_CK(dtype, biased, masked, mask_bias_suffix) \ REGISTER_COMMON( \ "GemmSoftmaxGemmPermuteCK" mask_bias_suffix "_" #dtype, GemmSoftmaxGemmPermuteCK, dtype, biased, masked) #define REGISTER_TUNABLE(dtype) \ REGISTER_COMMON("GemmSoftmaxGemmPermuteTunable_" #dtype, GemmSoftmaxGemmPermuteTunable, dtype) KE_REGISTER(m) { REGISTER_GENERIC(half); #ifdef USE_COMPOSABLE_KERNEL REGISTER_CK(half, false, false, ""); REGISTER_CK(half, true, false, "Biased"); REGISTER_CK(half, false, true, "Masked"); REGISTER_CK(half, true, true, "BiasedMasked"); #endif REGISTER_TUNABLE(half); } } // namespace onnxruntime
c24beaa21b5437d2907894185a78baaf44085de7.hip
// !!! This is a file automatically generated by hipify!!! // Solve the Laplace equation on a 2D lattice with boundary conditions. // // Includes #include <stdio.h> #include <stdlib.h> #include <omp.h> #include <hip/hip_runtime.h> #include <iostream> #include <fstream> // field variables float* h_new; // pointer to the working space float* h_old; // pointer to the working space float* h_1; // host field vectors (working space) float* h_2; // host field vectors (working space) float* h_C; // sum of diff*diff of each block float* g_new; // device solution back to the host float** d_1; // device field vectors (working space) float** d_2; // device field vectors (working space) float** d_C; // sum of diff*diff of each block int MAX=10000000; // maximum iterations double eps=1.0e-10; // stopping criterion float w = 1.0; __global__ void laplacian(float* phi0_old, float* phiL_old, float* phiR_old, float* phiB_old, float* phiT_old, float* phi0_new, float* C) { float w = 1.0; extern __shared__ float cache[]; float t, l, c, r, b; // top, left, center, right, bottom float diff; int site, skip; int Lx = blockDim.x*gridDim.x; int Ly = blockDim.y*gridDim.y; int x = blockDim.x*blockIdx.x + threadIdx.x; int y = blockDim.y*blockIdx.y + threadIdx.y; int cacheIndex = threadIdx.x + threadIdx.y*blockDim.x; site = x + y*Lx; skip = 0; diff = 0.0; b = 0.0; l = 0.0; r = 0.0; t = 0.0; c = phi0_old[site]; if (x == 0) { if (phiL_old != NULL) { l = phiL_old[(Lx-1)+y*Lx]; r = phi0_old[site+1]; } else skip = 1; } else if (x == Lx-1) { if(phiR_old != NULL) { l = phi0_old[site-1]; r = phiR_old[y*Lx]; } else skip = 1; } else { l = phi0_old[site-1]; // x-1 r = phi0_old[site+1]; // x+1 } if (y == 0) { if (phiB_old != NULL) { b = phiB_old[x+(Ly-1)*Lx]; t = phi0_old[site+Lx]; } else skip = 1; } else if (y == Ly-1) { if (phiT_old != NULL) { b = phi0_old[site-Lx]; t = phiT_old[x]; } else skip = 1; } else { b = phi0_old[site-Lx]; t = phi0_old[site+Lx]; } if (skip == 0) { phi0_new[site] = 0.25*w*(b+l+r+t)+(1-w)*phi0_old[site]; diff = phi0_new[site]-c; } // each thread saves its error estimate to the shared memory cache[cacheIndex]=diff*diff; __syncthreads(); // parallel reduction in each block int ib = blockDim.x*blockDim.y/2; while (ib != 0) { if(cacheIndex < ib) cache[cacheIndex] += cache[cacheIndex + ib]; __syncthreads(); ib /=2; } // save the partial sum of each block to C int blockIndex = blockIdx.x + gridDim.x*blockIdx.y; if(cacheIndex == 0) C[blockIndex] = cache[0]; } int op(int NGPU,int* Dev, int Nx, int Ny, int Lx, int Ly, int NGx, int NGy, int tx, int ty, int CPU , std::ofstream& myfile) { hipEvent_t start, stop; volatile bool flag; // to toggle between two working spaces. int cpu_thread_id=0; int sm; // size of the shared memory in each block. int iter; // counter of iterations. float cputime; float gputime; float gputime_tot; float Intime,Outime; double flops; double error; // error estimate int bx,by; // # of blocks. dim3 threads(tx,ty); if( tx*ty > 1024 ) { printf("!!! The number of threads per block must be less than 1024.\n"); exit(0); } // The total number of threads in the grid is equal to the total number // of lattice sites bx = Nx/tx; if (bx*tx != Nx) { printf("The blocksize in x is incorrect\n"); exit(0); } by = Ny/ty; if (by*ty != Ny) { printf("The blocksize in y is incorrect\n"); exit(0); } if ((bx/NGx > 65535) || (by/NGy > 65535)) { printf("!!! The grid size exceeds the limit.\n"); exit(0); } dim3 blocks(bx/NGx,by/NGy); printf(" The dimension of the grid per GPU is (%d, %d)\n",bx/NGx,by/NGy); error = 10*eps; // any value bigger than eps is OK flag = true; // Allocate field vector h_phi in host memory int N = Nx*Ny; int size = N*sizeof(float); int sb = bx*by*sizeof(float); h_1 = (float*)malloc(size); h_2 = (float*)malloc(size); h_C = (float*)malloc(sb); g_new = (float*)malloc(size); // Initialize the field vector with boundary conditions memset(h_1, 0, size); memset(h_2, 0, size); for (int x=0; x<Nx; x++) { h_1[x]=273; h_2[x]=273; h_1[x+Nx*(Ny-1)]=400; h_2[x+Nx*(Ny-1)]=400; } for (int y=0; y<Ny; y++) { h_1[y]=273; h_2[y]=273; h_1[y+Ny*(Nx-1)]=273; h_2[y+Ny*(Nx-1)]=273; } // Save initial configuration in phi_initial.dat FILE *out1; if ((out1 = fopen("phi_initial.dat","w")) == NULL) { printf("!!! Cannot open file: phi_initial.dat\n"); exit(1); } fprintf(out1, "Inital field configuration:\n"); for(int j=Ny-1;j>-1;j--) { for(int i=0; i<Nx; i++) { fprintf(out1,"%.2e ", h_1[i+j*Nx]); } fprintf(out1,"\n"); } fclose(out1); // Allocate working space for GPUs. printf("\n* Allocate working space for GPUs ....\n"); sm = tx*ty*sizeof(float); // size of the shared memory in each block d_1 = (float **)malloc(NGPU*sizeof(float *)); d_2 = (float **)malloc(NGPU*sizeof(float *)); d_C = (float **)malloc(NGPU*sizeof(float *)); omp_set_num_threads(NGPU); #pragma omp parallel private(cpu_thread_id) { int cpuid_x, cpuid_y; cpu_thread_id = omp_get_thread_num(); cpuid_x = cpu_thread_id % NGx; cpuid_y = cpu_thread_id / NGx; hipSetDevice(Dev[cpu_thread_id]); int cpuid_r = ((cpuid_x+1)%NGx) + cpuid_y*NGx; // GPU on the right hipDeviceEnablePeerAccess(Dev[cpuid_r],0); int cpuid_l = ((cpuid_x+NGx-1)%NGx) + cpuid_y*NGx; // GPU on the left hipDeviceEnablePeerAccess(Dev[cpuid_l],0); int cpuid_t = cpuid_x + ((cpuid_y+1)%NGy)*NGx; // GPU on the top hipDeviceEnablePeerAccess(Dev[cpuid_t],0); int cpuid_b = cpuid_x + ((cpuid_y+NGy-1)%NGy)*NGx; // GPU on the bottom hipDeviceEnablePeerAccess(Dev[cpuid_b],0); // start the timer if (cpu_thread_id == 0) { hipEventCreate(&start); hipEventCreate(&stop); hipEventRecord(start,0); } // Allocate vectors in device memory hipMalloc((void**)&d_1[cpu_thread_id], size/NGPU); hipMalloc((void**)&d_2[cpu_thread_id], size/NGPU); hipMalloc((void**)&d_C[cpu_thread_id], sb/NGPU); // Copy vectors from the host memory to the device memory for (int i=0; i < Ly; i++) { float *h, *d; h = h_1 + cpuid_x*Lx + (cpuid_y*Ly+i)*Nx; d = d_1[cpu_thread_id] + i*Lx; hipMemcpy(d, h, Lx*sizeof(float), hipMemcpyHostToDevice); } for (int i=0; i < Ly; i++) { float *h, *d; h = h_2 + cpuid_x*Lx + (cpuid_y*Ly+i)*Nx; d = d_2[cpu_thread_id] + i*Lx; hipMemcpy(d, h, Lx*sizeof(float), hipMemcpyHostToDevice); } #pragma omp barrier // stop the timer if (cpu_thread_id == 0) { hipEventRecord(stop,0); hipEventSynchronize(stop); hipEventElapsedTime(&Intime, start, stop); printf(" Data input time for GPU: %f (ms) \n",Intime); } } // OpenMP // Compute GPU solution. // start the timer hipEventRecord(start,0); printf("\n* Compute GPU solution ....\n"); fflush(stdout); iter = 0; // counter for iterations while ((error > eps) && (iter < MAX)) { #pragma omp parallel private(cpu_thread_id) { int cpuid_x, cpuid_y; cpu_thread_id = omp_get_thread_num(); cpuid_x = cpu_thread_id % NGx; cpuid_y = cpu_thread_id / NGx; hipSetDevice(Dev[cpu_thread_id]); float **d_old, **d_new; float *dL_old, *dR_old, *dT_old, *dB_old, *d0_old, *d0_new; d_old = (flag == true) ? d_1 : d_2; d_new = (flag == true) ? d_2 : d_1; d0_old = d_old[cpu_thread_id]; d0_new = d_new[cpu_thread_id]; dL_old = (cpuid_x == 0) ? NULL : d_old[cpuid_x-1+cpuid_y*NGx]; dR_old = (cpuid_x == NGx-1) ? NULL : d_old[cpuid_x+1+cpuid_y*NGx]; dB_old = (cpuid_y == 0 ) ? NULL : d_old[cpuid_x+(cpuid_y-1)*NGx]; dT_old = (cpuid_y == NGy-1) ? NULL : d_old[cpuid_x+(cpuid_y+1)*NGx]; hipLaunchKernelGGL(( laplacian), dim3(blocks),dim3(threads),sm, 0, d0_old, dL_old, dR_old, dB_old, dT_old, d0_new, d_C[cpu_thread_id]); hipDeviceSynchronize(); hipMemcpy(h_C+bx*by/NGPU*cpu_thread_id, d_C[cpu_thread_id], sb/NGPU, hipMemcpyDeviceToHost); } // OpenMP error = 0.0; for(int i=0; i<bx*by; i++) error = error + h_C[i]; error = sqrt(error); iter++; flag = !flag; } printf(" error (GPU) = %.15e\n",error); printf(" total iterations (GPU) = %d\n",iter); fflush(stdout); // stop the timer hipEventRecord(stop,0); hipEventSynchronize(stop); hipEventElapsedTime( &gputime, start, stop); flops = 7.0*(Nx-2)*(Ny-2)*iter; printf(" Processing time for GPU: %f (ms) \n",gputime); printf(" GPU Gflops: %f\n",flops/(1000000.0*gputime)); fflush(stdout); // Copy result from device memory to host memory // start the timer hipEventRecord(start,0); printf("\n* Copy result from device memory to host memory ....\n"); fflush(stdout); #pragma omp parallel private(cpu_thread_id) { int cpuid_x, cpuid_y; cpu_thread_id = omp_get_thread_num(); cpuid_x = cpu_thread_id % NGx; cpuid_y = cpu_thread_id / NGx; hipSetDevice(Dev[cpu_thread_id]); float* d_new = (flag == true) ? d_2[cpu_thread_id] : d_1[cpu_thread_id]; for (int i=0; i < Ly; i++) { float *g, *d; g = g_new + cpuid_x*Lx + (cpuid_y*Ly+i)*Nx; d = d_new + i*Lx; hipMemcpy(g, d, Lx*sizeof(float), hipMemcpyDeviceToHost); } hipFree(d_1[cpu_thread_id]); hipFree(d_2[cpu_thread_id]); hipFree(d_C[cpu_thread_id]); } // OpenMP // stop the timer hipEventRecord(stop,0); hipEventSynchronize(stop); hipEventElapsedTime( &Outime, start, stop); gputime_tot = Intime + gputime + Outime; printf(" Data output time for GPU: %f (ms) \n",Outime); printf(" Total time for GPU: %f (ms) \n",gputime_tot); myfile<<Nx<<"x"<<Ny<<","<<tx<<"x"<<ty<<","<<gputime<<","<<gputime_tot<<std::endl; fflush(stdout); // Save GPU solution in phi_GPU.dat FILE *outg; if ((outg = fopen("phi_GPU.dat","w")) == NULL) { printf("!!! Cannot open file: phi_GPU.dat\n"); exit(1); } fprintf(outg, "GPU field configuration:\n"); for(int j=Ny-1;j>-1;j--) { for(int i=0; i<Nx; i++) { fprintf(outg,"%.2e ",g_new[i+j*Nx]); } fprintf(outg,"\n"); } fclose(outg); // Compute the CPU solution. if (CPU==1) { // start the timer hipEventRecord(start,0); printf("\n* Compute CPU solution ....\n"); fflush(stdout); double diff; float t, l, r, b; // top, left, right, bottom int site, ym1, xm1, xp1, yp1; error = 10*eps; // any value bigger than eps iter = 0; // counter for iterations flag = true; while ( (error > eps) && (iter < MAX) ) { h_old = (flag == true) ? h_1 : h_2; h_new = (flag == true) ? h_2 : h_1; error = 0.0; for (int y=0; y<Ny; y++) { for (int x=0; x<Nx; x++) { if(x==0 || x==Nx-1 || y==0 || y==Ny-1) { } else { site = x+y*Nx; xm1 = site - 1; // x-1 xp1 = site + 1; // x+1 ym1 = site - Nx; // y-1 yp1 = site + Nx; // y+1 b = h_old[ym1]; l = h_old[xm1]; r = h_old[xp1]; t = h_old[yp1]; h_new[site] = 0.25*w*(b+l+r+t)+(1-w)*h_old[site]; diff = h_new[site]-h_old[site]; error = error + diff*diff; } }} flag = !flag; iter++; error = sqrt(error); } // exit if error < eps printf(" error (CPU) = %.15e\n",error); printf(" total iterations (CPU) = %d\n",iter); // stop the timer hipEventRecord(stop,0); hipEventSynchronize(stop); hipEventElapsedTime( &cputime, start, stop); printf(" Processing time for CPU: %f (ms) \n",cputime); flops = 7.0*(Nx-2)*(Ny-2)*iter; printf(" CPU Gflops: %lf\n",flops/(1000000.0*cputime)); printf(" Speed up of GPU = %f\n", cputime/(gputime_tot)); fflush(stdout); FILE *outc; // save CPU solution in phi_CPU.dat if ((outc = fopen("phi_CPU.dat","w")) == NULL) { printf("!!! Cannot output file: phi_CPU.dat\n"); exit(1); } fprintf(outc,"CPU field configuration:\n"); for(int j=Ny-1;j>-1;j--) { for(int i=0; i<Nx; i++) fprintf(outc,"%.2e ",h_new[i+j*Nx]); fprintf(outc,"\n"); } fclose(outc); } free(h_new); free(h_old); free(g_new); free(d_1); free(d_2); free(d_C); // destroy the timer hipEventDestroy(start); hipEventDestroy(stop); #pragma omp parallel private(cpu_thread_id) { cpu_thread_id = omp_get_thread_num(); hipSetDevice(Dev[cpu_thread_id]); hipDeviceReset(); } // OpenMP return 0; } int main(void) { int NGPU; int *Dev; // GPU device numbers. int Nx,Ny; // lattice size in x and y directions. int Lx,Ly; // lattice size in each GPU. int NGx,NGy; // The partition of the lattice (NGx*NGy=NGPU). int tx,ty; // block size: tx*ty int CPU; // compute CPU solution or not. std::ofstream myfile; myfile.open("Output.csv"); myfile<<"lattice_sizes"<<","<<"block sizes"<<","<<"gputime"<<","<<"gputime_tot"<<std::endl; // Get the input parameters. printf("\n* Initial parameters:\n"); printf(" Enter the number of GPUs (NGx, NGy): "); scanf("%d %d", &NGx, &NGy); printf("%d %d\n", NGx, NGy); NGPU = NGx * NGy; Dev = (int *)malloc(sizeof(int)*NGPU); for (int i=0; i < NGPU; i++) { printf(" * Enter the GPU ID (0/1/...): "); scanf("%d",&(Dev[i])); printf("%d\n", Dev[i]); } printf(" Solve Laplace equation on 2D lattice with boundary conditions\n"); printf(" Enter the size (Nx, Ny) of the 2D lattice: "); scanf("%d %d",&Nx,&Ny); printf("%d %d\n",Nx,Ny); if (Nx % NGx != 0) { printf("!!! Invalid partition of lattice: Nx %% NGx != 0\n"); exit(1); } if (Ny % NGy != 0) { printf("!!! Invalid partition of lattice: Ny %% NGy != 0\n"); exit(1); } Lx = Nx / NGx; Ly = Ny / NGy; // Set the number of threads (tx,ty) per block printf(" Enter the number of threads (tx,ty) per block: "); scanf("%d %d",&tx, &ty); printf("%d %d\n",tx, ty); if( tx*ty > 1024 ) { printf("!!! The number of threads per block must be less than 1024.\n"); exit(0); } printf(" To compute the solution vector with CPU (1/0) ? "); scanf("%d",&CPU); printf("%d\n",CPU); fflush(stdout); int ln = 4; int ts[ln] = {4, 8, 16, 32}; //int ts[ln] = {8, 32}; for(int i=0; i<ln; i++) { printf("loop: %d\n",i); op(NGPU, Dev, Nx, Ny, Lx, Ly, NGx, NGy, ts[i], ts[i], CPU, myfile); } }
c24beaa21b5437d2907894185a78baaf44085de7.cu
// Solve the Laplace equation on a 2D lattice with boundary conditions. // // Includes #include <stdio.h> #include <stdlib.h> #include <omp.h> #include <cuda_runtime.h> #include <iostream> #include <fstream> // field variables float* h_new; // pointer to the working space float* h_old; // pointer to the working space float* h_1; // host field vectors (working space) float* h_2; // host field vectors (working space) float* h_C; // sum of diff*diff of each block float* g_new; // device solution back to the host float** d_1; // device field vectors (working space) float** d_2; // device field vectors (working space) float** d_C; // sum of diff*diff of each block int MAX=10000000; // maximum iterations double eps=1.0e-10; // stopping criterion float w = 1.0; __global__ void laplacian(float* phi0_old, float* phiL_old, float* phiR_old, float* phiB_old, float* phiT_old, float* phi0_new, float* C) { float w = 1.0; extern __shared__ float cache[]; float t, l, c, r, b; // top, left, center, right, bottom float diff; int site, skip; int Lx = blockDim.x*gridDim.x; int Ly = blockDim.y*gridDim.y; int x = blockDim.x*blockIdx.x + threadIdx.x; int y = blockDim.y*blockIdx.y + threadIdx.y; int cacheIndex = threadIdx.x + threadIdx.y*blockDim.x; site = x + y*Lx; skip = 0; diff = 0.0; b = 0.0; l = 0.0; r = 0.0; t = 0.0; c = phi0_old[site]; if (x == 0) { if (phiL_old != NULL) { l = phiL_old[(Lx-1)+y*Lx]; r = phi0_old[site+1]; } else skip = 1; } else if (x == Lx-1) { if(phiR_old != NULL) { l = phi0_old[site-1]; r = phiR_old[y*Lx]; } else skip = 1; } else { l = phi0_old[site-1]; // x-1 r = phi0_old[site+1]; // x+1 } if (y == 0) { if (phiB_old != NULL) { b = phiB_old[x+(Ly-1)*Lx]; t = phi0_old[site+Lx]; } else skip = 1; } else if (y == Ly-1) { if (phiT_old != NULL) { b = phi0_old[site-Lx]; t = phiT_old[x]; } else skip = 1; } else { b = phi0_old[site-Lx]; t = phi0_old[site+Lx]; } if (skip == 0) { phi0_new[site] = 0.25*w*(b+l+r+t)+(1-w)*phi0_old[site]; diff = phi0_new[site]-c; } // each thread saves its error estimate to the shared memory cache[cacheIndex]=diff*diff; __syncthreads(); // parallel reduction in each block int ib = blockDim.x*blockDim.y/2; while (ib != 0) { if(cacheIndex < ib) cache[cacheIndex] += cache[cacheIndex + ib]; __syncthreads(); ib /=2; } // save the partial sum of each block to C int blockIndex = blockIdx.x + gridDim.x*blockIdx.y; if(cacheIndex == 0) C[blockIndex] = cache[0]; } int op(int NGPU,int* Dev, int Nx, int Ny, int Lx, int Ly, int NGx, int NGy, int tx, int ty, int CPU , std::ofstream& myfile) { cudaEvent_t start, stop; volatile bool flag; // to toggle between two working spaces. int cpu_thread_id=0; int sm; // size of the shared memory in each block. int iter; // counter of iterations. float cputime; float gputime; float gputime_tot; float Intime,Outime; double flops; double error; // error estimate int bx,by; // # of blocks. dim3 threads(tx,ty); if( tx*ty > 1024 ) { printf("!!! The number of threads per block must be less than 1024.\n"); exit(0); } // The total number of threads in the grid is equal to the total number // of lattice sites bx = Nx/tx; if (bx*tx != Nx) { printf("The blocksize in x is incorrect\n"); exit(0); } by = Ny/ty; if (by*ty != Ny) { printf("The blocksize in y is incorrect\n"); exit(0); } if ((bx/NGx > 65535) || (by/NGy > 65535)) { printf("!!! The grid size exceeds the limit.\n"); exit(0); } dim3 blocks(bx/NGx,by/NGy); printf(" The dimension of the grid per GPU is (%d, %d)\n",bx/NGx,by/NGy); error = 10*eps; // any value bigger than eps is OK flag = true; // Allocate field vector h_phi in host memory int N = Nx*Ny; int size = N*sizeof(float); int sb = bx*by*sizeof(float); h_1 = (float*)malloc(size); h_2 = (float*)malloc(size); h_C = (float*)malloc(sb); g_new = (float*)malloc(size); // Initialize the field vector with boundary conditions memset(h_1, 0, size); memset(h_2, 0, size); for (int x=0; x<Nx; x++) { h_1[x]=273; h_2[x]=273; h_1[x+Nx*(Ny-1)]=400; h_2[x+Nx*(Ny-1)]=400; } for (int y=0; y<Ny; y++) { h_1[y]=273; h_2[y]=273; h_1[y+Ny*(Nx-1)]=273; h_2[y+Ny*(Nx-1)]=273; } // Save initial configuration in phi_initial.dat FILE *out1; if ((out1 = fopen("phi_initial.dat","w")) == NULL) { printf("!!! Cannot open file: phi_initial.dat\n"); exit(1); } fprintf(out1, "Inital field configuration:\n"); for(int j=Ny-1;j>-1;j--) { for(int i=0; i<Nx; i++) { fprintf(out1,"%.2e ", h_1[i+j*Nx]); } fprintf(out1,"\n"); } fclose(out1); // Allocate working space for GPUs. printf("\n* Allocate working space for GPUs ....\n"); sm = tx*ty*sizeof(float); // size of the shared memory in each block d_1 = (float **)malloc(NGPU*sizeof(float *)); d_2 = (float **)malloc(NGPU*sizeof(float *)); d_C = (float **)malloc(NGPU*sizeof(float *)); omp_set_num_threads(NGPU); #pragma omp parallel private(cpu_thread_id) { int cpuid_x, cpuid_y; cpu_thread_id = omp_get_thread_num(); cpuid_x = cpu_thread_id % NGx; cpuid_y = cpu_thread_id / NGx; cudaSetDevice(Dev[cpu_thread_id]); int cpuid_r = ((cpuid_x+1)%NGx) + cpuid_y*NGx; // GPU on the right cudaDeviceEnablePeerAccess(Dev[cpuid_r],0); int cpuid_l = ((cpuid_x+NGx-1)%NGx) + cpuid_y*NGx; // GPU on the left cudaDeviceEnablePeerAccess(Dev[cpuid_l],0); int cpuid_t = cpuid_x + ((cpuid_y+1)%NGy)*NGx; // GPU on the top cudaDeviceEnablePeerAccess(Dev[cpuid_t],0); int cpuid_b = cpuid_x + ((cpuid_y+NGy-1)%NGy)*NGx; // GPU on the bottom cudaDeviceEnablePeerAccess(Dev[cpuid_b],0); // start the timer if (cpu_thread_id == 0) { cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start,0); } // Allocate vectors in device memory cudaMalloc((void**)&d_1[cpu_thread_id], size/NGPU); cudaMalloc((void**)&d_2[cpu_thread_id], size/NGPU); cudaMalloc((void**)&d_C[cpu_thread_id], sb/NGPU); // Copy vectors from the host memory to the device memory for (int i=0; i < Ly; i++) { float *h, *d; h = h_1 + cpuid_x*Lx + (cpuid_y*Ly+i)*Nx; d = d_1[cpu_thread_id] + i*Lx; cudaMemcpy(d, h, Lx*sizeof(float), cudaMemcpyHostToDevice); } for (int i=0; i < Ly; i++) { float *h, *d; h = h_2 + cpuid_x*Lx + (cpuid_y*Ly+i)*Nx; d = d_2[cpu_thread_id] + i*Lx; cudaMemcpy(d, h, Lx*sizeof(float), cudaMemcpyHostToDevice); } #pragma omp barrier // stop the timer if (cpu_thread_id == 0) { cudaEventRecord(stop,0); cudaEventSynchronize(stop); cudaEventElapsedTime(&Intime, start, stop); printf(" Data input time for GPU: %f (ms) \n",Intime); } } // OpenMP // Compute GPU solution. // start the timer cudaEventRecord(start,0); printf("\n* Compute GPU solution ....\n"); fflush(stdout); iter = 0; // counter for iterations while ((error > eps) && (iter < MAX)) { #pragma omp parallel private(cpu_thread_id) { int cpuid_x, cpuid_y; cpu_thread_id = omp_get_thread_num(); cpuid_x = cpu_thread_id % NGx; cpuid_y = cpu_thread_id / NGx; cudaSetDevice(Dev[cpu_thread_id]); float **d_old, **d_new; float *dL_old, *dR_old, *dT_old, *dB_old, *d0_old, *d0_new; d_old = (flag == true) ? d_1 : d_2; d_new = (flag == true) ? d_2 : d_1; d0_old = d_old[cpu_thread_id]; d0_new = d_new[cpu_thread_id]; dL_old = (cpuid_x == 0) ? NULL : d_old[cpuid_x-1+cpuid_y*NGx]; dR_old = (cpuid_x == NGx-1) ? NULL : d_old[cpuid_x+1+cpuid_y*NGx]; dB_old = (cpuid_y == 0 ) ? NULL : d_old[cpuid_x+(cpuid_y-1)*NGx]; dT_old = (cpuid_y == NGy-1) ? NULL : d_old[cpuid_x+(cpuid_y+1)*NGx]; laplacian<<<blocks,threads,sm>>>(d0_old, dL_old, dR_old, dB_old, dT_old, d0_new, d_C[cpu_thread_id]); cudaDeviceSynchronize(); cudaMemcpy(h_C+bx*by/NGPU*cpu_thread_id, d_C[cpu_thread_id], sb/NGPU, cudaMemcpyDeviceToHost); } // OpenMP error = 0.0; for(int i=0; i<bx*by; i++) error = error + h_C[i]; error = sqrt(error); iter++; flag = !flag; } printf(" error (GPU) = %.15e\n",error); printf(" total iterations (GPU) = %d\n",iter); fflush(stdout); // stop the timer cudaEventRecord(stop,0); cudaEventSynchronize(stop); cudaEventElapsedTime( &gputime, start, stop); flops = 7.0*(Nx-2)*(Ny-2)*iter; printf(" Processing time for GPU: %f (ms) \n",gputime); printf(" GPU Gflops: %f\n",flops/(1000000.0*gputime)); fflush(stdout); // Copy result from device memory to host memory // start the timer cudaEventRecord(start,0); printf("\n* Copy result from device memory to host memory ....\n"); fflush(stdout); #pragma omp parallel private(cpu_thread_id) { int cpuid_x, cpuid_y; cpu_thread_id = omp_get_thread_num(); cpuid_x = cpu_thread_id % NGx; cpuid_y = cpu_thread_id / NGx; cudaSetDevice(Dev[cpu_thread_id]); float* d_new = (flag == true) ? d_2[cpu_thread_id] : d_1[cpu_thread_id]; for (int i=0; i < Ly; i++) { float *g, *d; g = g_new + cpuid_x*Lx + (cpuid_y*Ly+i)*Nx; d = d_new + i*Lx; cudaMemcpy(g, d, Lx*sizeof(float), cudaMemcpyDeviceToHost); } cudaFree(d_1[cpu_thread_id]); cudaFree(d_2[cpu_thread_id]); cudaFree(d_C[cpu_thread_id]); } // OpenMP // stop the timer cudaEventRecord(stop,0); cudaEventSynchronize(stop); cudaEventElapsedTime( &Outime, start, stop); gputime_tot = Intime + gputime + Outime; printf(" Data output time for GPU: %f (ms) \n",Outime); printf(" Total time for GPU: %f (ms) \n",gputime_tot); myfile<<Nx<<"x"<<Ny<<","<<tx<<"x"<<ty<<","<<gputime<<","<<gputime_tot<<std::endl; fflush(stdout); // Save GPU solution in phi_GPU.dat FILE *outg; if ((outg = fopen("phi_GPU.dat","w")) == NULL) { printf("!!! Cannot open file: phi_GPU.dat\n"); exit(1); } fprintf(outg, "GPU field configuration:\n"); for(int j=Ny-1;j>-1;j--) { for(int i=0; i<Nx; i++) { fprintf(outg,"%.2e ",g_new[i+j*Nx]); } fprintf(outg,"\n"); } fclose(outg); // Compute the CPU solution. if (CPU==1) { // start the timer cudaEventRecord(start,0); printf("\n* Compute CPU solution ....\n"); fflush(stdout); double diff; float t, l, r, b; // top, left, right, bottom int site, ym1, xm1, xp1, yp1; error = 10*eps; // any value bigger than eps iter = 0; // counter for iterations flag = true; while ( (error > eps) && (iter < MAX) ) { h_old = (flag == true) ? h_1 : h_2; h_new = (flag == true) ? h_2 : h_1; error = 0.0; for (int y=0; y<Ny; y++) { for (int x=0; x<Nx; x++) { if(x==0 || x==Nx-1 || y==0 || y==Ny-1) { } else { site = x+y*Nx; xm1 = site - 1; // x-1 xp1 = site + 1; // x+1 ym1 = site - Nx; // y-1 yp1 = site + Nx; // y+1 b = h_old[ym1]; l = h_old[xm1]; r = h_old[xp1]; t = h_old[yp1]; h_new[site] = 0.25*w*(b+l+r+t)+(1-w)*h_old[site]; diff = h_new[site]-h_old[site]; error = error + diff*diff; } }} flag = !flag; iter++; error = sqrt(error); } // exit if error < eps printf(" error (CPU) = %.15e\n",error); printf(" total iterations (CPU) = %d\n",iter); // stop the timer cudaEventRecord(stop,0); cudaEventSynchronize(stop); cudaEventElapsedTime( &cputime, start, stop); printf(" Processing time for CPU: %f (ms) \n",cputime); flops = 7.0*(Nx-2)*(Ny-2)*iter; printf(" CPU Gflops: %lf\n",flops/(1000000.0*cputime)); printf(" Speed up of GPU = %f\n", cputime/(gputime_tot)); fflush(stdout); FILE *outc; // save CPU solution in phi_CPU.dat if ((outc = fopen("phi_CPU.dat","w")) == NULL) { printf("!!! Cannot output file: phi_CPU.dat\n"); exit(1); } fprintf(outc,"CPU field configuration:\n"); for(int j=Ny-1;j>-1;j--) { for(int i=0; i<Nx; i++) fprintf(outc,"%.2e ",h_new[i+j*Nx]); fprintf(outc,"\n"); } fclose(outc); } free(h_new); free(h_old); free(g_new); free(d_1); free(d_2); free(d_C); // destroy the timer cudaEventDestroy(start); cudaEventDestroy(stop); #pragma omp parallel private(cpu_thread_id) { cpu_thread_id = omp_get_thread_num(); cudaSetDevice(Dev[cpu_thread_id]); cudaDeviceReset(); } // OpenMP return 0; } int main(void) { int NGPU; int *Dev; // GPU device numbers. int Nx,Ny; // lattice size in x and y directions. int Lx,Ly; // lattice size in each GPU. int NGx,NGy; // The partition of the lattice (NGx*NGy=NGPU). int tx,ty; // block size: tx*ty int CPU; // compute CPU solution or not. std::ofstream myfile; myfile.open("Output.csv"); myfile<<"lattice_sizes"<<","<<"block sizes"<<","<<"gputime"<<","<<"gputime_tot"<<std::endl; // Get the input parameters. printf("\n* Initial parameters:\n"); printf(" Enter the number of GPUs (NGx, NGy): "); scanf("%d %d", &NGx, &NGy); printf("%d %d\n", NGx, NGy); NGPU = NGx * NGy; Dev = (int *)malloc(sizeof(int)*NGPU); for (int i=0; i < NGPU; i++) { printf(" * Enter the GPU ID (0/1/...): "); scanf("%d",&(Dev[i])); printf("%d\n", Dev[i]); } printf(" Solve Laplace equation on 2D lattice with boundary conditions\n"); printf(" Enter the size (Nx, Ny) of the 2D lattice: "); scanf("%d %d",&Nx,&Ny); printf("%d %d\n",Nx,Ny); if (Nx % NGx != 0) { printf("!!! Invalid partition of lattice: Nx %% NGx != 0\n"); exit(1); } if (Ny % NGy != 0) { printf("!!! Invalid partition of lattice: Ny %% NGy != 0\n"); exit(1); } Lx = Nx / NGx; Ly = Ny / NGy; // Set the number of threads (tx,ty) per block printf(" Enter the number of threads (tx,ty) per block: "); scanf("%d %d",&tx, &ty); printf("%d %d\n",tx, ty); if( tx*ty > 1024 ) { printf("!!! The number of threads per block must be less than 1024.\n"); exit(0); } printf(" To compute the solution vector with CPU (1/0) ? "); scanf("%d",&CPU); printf("%d\n",CPU); fflush(stdout); int ln = 4; int ts[ln] = {4, 8, 16, 32}; //int ts[ln] = {8, 32}; for(int i=0; i<ln; i++) { printf("loop: %d\n",i); op(NGPU, Dev, Nx, Ny, Lx, Ly, NGx, NGy, ts[i], ts[i], CPU, myfile); } }
ae2d79c8be2367c4ae89b8750b063bcf34bd875e.hip
// !!! This is a file automatically generated by hipify!!! #include <math.h> #include <stdio.h> #include <hip/hip_runtime.h> #define WARP_SIZE 32 #define MAX_THREADS_X 1024 #define MAX_THREADS_Y 1024 #define MAX_THREADS_Z 64 #define MAX_BLOCKS_X 2147483647 #define MAX_BLOCKS_Y 65535 #define MAX_BLOCKS_Z 65535 #define THREADS_PER_BLOCK 128 //3.0, 16 blocks, 2048 threads //MIN THREADS_PER_BLOCK = 128 #define SIZE 1024 static inline void _safe_cuda_call(hipError_t err, const char* msg, const char* file_name, const int line_number) { if (err != hipSuccess) { fprintf(stderr, "%s\n\nFile: %s\n\nLine Number: %d\n\nReason: %s\n", msg, file_name, line_number, hipGetErrorString(err)); //cin.get(); exit(EXIT_FAILURE); } } #define SAFE_CALL(call,msg) _safe_cuda_call((call),(msg),__FILE__,__LINE__) __global__ void scan(int *input, int n, int d, int size) { int i = blockIdx.x *blockDim.x + threadIdx.x; //printf("i: %d\n", i); //if(n == 1) // input[i + (int)pow(2.0, (double)(d+1)) - 1] = 0; //else //if (i < n ) { i*=pow(2.0, (double)d+1); if(i + (int)pow(2.0, (double)(d+1)) - 1 < size) { input[i + (int)pow(2.0, (double)(d+1)) - 1] = input[i + (int)pow(2.0,(double)d) - 1] + input[i + (int)pow(2.0,(double)(d + 1)) - 1]; //printf("[%d(%d+%d-1)] = [%d] + [%d], SIZE=%d\n", i + (int)pow(2.0, (double)(d+1)) - 1, i,(int)pow(2.0, (double)(d+1)), i + (int)pow(2.0,(double)d) - 1,i + (int)pow(2.0,(double)(d + 1)) - 1, size); } } } __global__ void down_sweep(int *input, int n, int d, int size) { int i = blockIdx.x *blockDim.x + threadIdx.x; //if (i < n) { i*=pow(2.0, (double)d+1); if(i + (int)pow(2.0, (double)(d+1)) - 1 < size) { int temp = input[i + (int)pow(2.0, (double)d) - 1]; input[i + (int)pow(2.0, (double)d) -1] = input[i + (int)pow(2.0, (double)(d+1)) - 1]; input[i + (int)pow(2.0, (double)(d+1)) - 1] = temp + input[i + (int)pow(2.0, (double)(d+1)) - 1]; } } } __global__ void quickfix(int *input, int size) { input[size-1] = 0; } int main() { int * input = (int *)malloc(SIZE * sizeof(int));// = {1,2,3,4,5,6,7,8,9}; for (int i = 0; i < SIZE; i++) input[i] = i; int *d_input; int d = ceil(log2((float)SIZE)); SAFE_CALL(hipMalloc<int>(&d_input, SIZE*sizeof(int)), "CUDA Malloc Failed"); SAFE_CALL(hipMemcpy(d_input, input, SIZE*sizeof(int), hipMemcpyHostToDevice ), "CUDA Memcpy Host To Device Failed"); for (int i = 0; i < d; i++) { //int numop = ceil(SIZE/2) int numop = ceil(SIZE/pow(2, i+1)); int bloques = ceil((float)numop/THREADS_PER_BLOCK); printf("numop: %d", numop); hipLaunchKernelGGL(( scan), dim3(bloques),dim3(THREADS_PER_BLOCK), 0, 0, d_input, numop, i, SIZE); } hipLaunchKernelGGL(( quickfix), dim3(1),dim3(1), 0, 0, d_input, SIZE); int numop2 = 1; for (int i = d - 1; i >= 0; i--) { int bloques = ceil((float)numop2/THREADS_PER_BLOCK); hipLaunchKernelGGL(( down_sweep), dim3(bloques),dim3(THREADS_PER_BLOCK), 0, 0, d_input, numop2, i, SIZE); numop2*=2; } SAFE_CALL(hipMemcpy(input, d_input, SIZE*sizeof(int), hipMemcpyDeviceToHost), "CUDA Memcpy Device To Host Failed"); SAFE_CALL(hipFree(d_input), "CUDA Free Failed"); for(int i = 0; i < SIZE; i++) { printf("%d\n", input[i]); } free(input); }
ae2d79c8be2367c4ae89b8750b063bcf34bd875e.cu
#include <math.h> #include <stdio.h> #include <cuda_runtime.h> #define WARP_SIZE 32 #define MAX_THREADS_X 1024 #define MAX_THREADS_Y 1024 #define MAX_THREADS_Z 64 #define MAX_BLOCKS_X 2147483647 #define MAX_BLOCKS_Y 65535 #define MAX_BLOCKS_Z 65535 #define THREADS_PER_BLOCK 128 //3.0, 16 blocks, 2048 threads //MIN THREADS_PER_BLOCK = 128 #define SIZE 1024 static inline void _safe_cuda_call(cudaError err, const char* msg, const char* file_name, const int line_number) { if (err != cudaSuccess) { fprintf(stderr, "%s\n\nFile: %s\n\nLine Number: %d\n\nReason: %s\n", msg, file_name, line_number, cudaGetErrorString(err)); //cin.get(); exit(EXIT_FAILURE); } } #define SAFE_CALL(call,msg) _safe_cuda_call((call),(msg),__FILE__,__LINE__) __global__ void scan(int *input, int n, int d, int size) { int i = blockIdx.x *blockDim.x + threadIdx.x; //printf("i: %d\n", i); //if(n == 1) // input[i + (int)pow(2.0, (double)(d+1)) - 1] = 0; //else //if (i < n ) { i*=pow(2.0, (double)d+1); if(i + (int)pow(2.0, (double)(d+1)) - 1 < size) { input[i + (int)pow(2.0, (double)(d+1)) - 1] = input[i + (int)pow(2.0,(double)d) - 1] + input[i + (int)pow(2.0,(double)(d + 1)) - 1]; //printf("[%d(%d+%d-1)] = [%d] + [%d], SIZE=%d\n", i + (int)pow(2.0, (double)(d+1)) - 1, i,(int)pow(2.0, (double)(d+1)), i + (int)pow(2.0,(double)d) - 1,i + (int)pow(2.0,(double)(d + 1)) - 1, size); } } } __global__ void down_sweep(int *input, int n, int d, int size) { int i = blockIdx.x *blockDim.x + threadIdx.x; //if (i < n) { i*=pow(2.0, (double)d+1); if(i + (int)pow(2.0, (double)(d+1)) - 1 < size) { int temp = input[i + (int)pow(2.0, (double)d) - 1]; input[i + (int)pow(2.0, (double)d) -1] = input[i + (int)pow(2.0, (double)(d+1)) - 1]; input[i + (int)pow(2.0, (double)(d+1)) - 1] = temp + input[i + (int)pow(2.0, (double)(d+1)) - 1]; } } } __global__ void quickfix(int *input, int size) { input[size-1] = 0; } int main() { int * input = (int *)malloc(SIZE * sizeof(int));// = {1,2,3,4,5,6,7,8,9}; for (int i = 0; i < SIZE; i++) input[i] = i; int *d_input; int d = ceil(log2((float)SIZE)); SAFE_CALL(cudaMalloc<int>(&d_input, SIZE*sizeof(int)), "CUDA Malloc Failed"); SAFE_CALL(cudaMemcpy(d_input, input, SIZE*sizeof(int), cudaMemcpyHostToDevice ), "CUDA Memcpy Host To Device Failed"); for (int i = 0; i < d; i++) { //int numop = ceil(SIZE/2) int numop = ceil(SIZE/pow(2, i+1)); int bloques = ceil((float)numop/THREADS_PER_BLOCK); printf("numop: %d", numop); scan<<<bloques,THREADS_PER_BLOCK>>>(d_input, numop, i, SIZE); } quickfix<<<1,1>>>(d_input, SIZE); int numop2 = 1; for (int i = d - 1; i >= 0; i--) { int bloques = ceil((float)numop2/THREADS_PER_BLOCK); down_sweep<<<bloques,THREADS_PER_BLOCK>>>(d_input, numop2, i, SIZE); numop2*=2; } SAFE_CALL(cudaMemcpy(input, d_input, SIZE*sizeof(int), cudaMemcpyDeviceToHost), "CUDA Memcpy Device To Host Failed"); SAFE_CALL(cudaFree(d_input), "CUDA Free Failed"); for(int i = 0; i < SIZE; i++) { printf("%d\n", input[i]); } free(input); }
c69a15d72897d3c741b87e27836f37829a4cad80.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <float.h> __device__ float euclid2( float* items, float* centers, int itemId, int centerId, int paramsCount) { float sum = 0.0; for(int j = 0; j < paramsCount; j++) { sum = sum + (items[itemId * paramsCount + j] - centers[centerId * paramsCount + j]) * (items[itemId * paramsCount + j] - centers[centerId * paramsCount + j]); } return sum; } __global__ void clusreringKernel( float* items, float* deviceCenters, int* clustersIds, int itemsCount, int clustersCount, int paramsCount) { extern __shared__ float centers[]; for (int i = threadIdx.x; i < clustersCount; i += blockDim.x) { for (int j = 0; j < paramsCount; j++) { centers[clustersCount * j + i] = deviceCenters[clustersCount * j + i]; } } __syncthreads(); int itemId = blockDim.x * blockIdx.x + threadIdx.x; if(itemId < itemsCount) { float minDistance = FLT_MAX; float distance; int index; for(int i = 0; i < clustersCount; i++) { distance = euclid2(items, centers, itemId, i, paramsCount); if(minDistance >= distance) { minDistance = distance; index = i; } } clustersIds[itemId] = index; } } __global__ void newCentersKernel( float* items, int* clustersIds, float* newCenters, int* itemsPerClusters, int itemsCount, int clustersCount, int paramsCount ) { extern __shared__ float center[]; int clusterId = blockIdx.x; int paramId = threadIdx.x; center[paramId] = 0.0; int count = 0; __syncthreads(); for(int i = 0; i < itemsCount; i++) { if(clusterId == clustersIds[i]) { center[paramId] += items[i * paramsCount + paramId]; count += 1; } } newCenters[clusterId * paramsCount + paramId] = center[paramId]; itemsPerClusters[clusterId] = count; }
c69a15d72897d3c741b87e27836f37829a4cad80.cu
#include <float.h> __device__ float euclid2( float* items, float* centers, int itemId, int centerId, int paramsCount) { float sum = 0.0; for(int j = 0; j < paramsCount; j++) { sum = sum + (items[itemId * paramsCount + j] - centers[centerId * paramsCount + j]) * (items[itemId * paramsCount + j] - centers[centerId * paramsCount + j]); } return sum; } __global__ void clusreringKernel( float* items, float* deviceCenters, int* clustersIds, int itemsCount, int clustersCount, int paramsCount) { extern __shared__ float centers[]; for (int i = threadIdx.x; i < clustersCount; i += blockDim.x) { for (int j = 0; j < paramsCount; j++) { centers[clustersCount * j + i] = deviceCenters[clustersCount * j + i]; } } __syncthreads(); int itemId = blockDim.x * blockIdx.x + threadIdx.x; if(itemId < itemsCount) { float minDistance = FLT_MAX; float distance; int index; for(int i = 0; i < clustersCount; i++) { distance = euclid2(items, centers, itemId, i, paramsCount); if(minDistance >= distance) { minDistance = distance; index = i; } } clustersIds[itemId] = index; } } __global__ void newCentersKernel( float* items, int* clustersIds, float* newCenters, int* itemsPerClusters, int itemsCount, int clustersCount, int paramsCount ) { extern __shared__ float center[]; int clusterId = blockIdx.x; int paramId = threadIdx.x; center[paramId] = 0.0; int count = 0; __syncthreads(); for(int i = 0; i < itemsCount; i++) { if(clusterId == clustersIds[i]) { center[paramId] += items[i * paramsCount + paramId]; count += 1; } } newCenters[clusterId * paramsCount + paramId] = center[paramId]; itemsPerClusters[clusterId] = count; }
7600618f5fba2433260fa3b1551737eb06714cc8.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <stdio.h> #define T 8 // As Threads #define N 16 __global__ void vecMatrixTransposed(int *A, int *B) { int x = blockIdx.x * blockDim.x + threadIdx.x; int y = blockIdx.y * blockDim.y+ threadIdx.y; int width = gridDim.x * T; for( int j = 0; j<T; j+=N ) { B[x*width + (j+y)] = A[(y+j)*width + x]; } } int main (int argc, char *argv[]) { int i,j; int size[N*N]; int A[N][N]; int sizearr = N*N *sizeof(int); int *Adefault,*B; for (i=0; i< N; i++) { for(j = 0 ; j<N ; j++ ) { A[i][j] = ((i*i) +1) * (j+1); printf("%5d ", A[i][j]); } } printf("\n"); hipMalloc( (void**)&Adefault,sizearr); hipMalloc( (void**)&B,sizearr); hipMemcpy( Adefault, A, sizearr, hipMemcpyHostToDevice); dim3 dimBlock(T,T); dim3 dimGrid((N+ dimBlock.x - 1)/ dimBlock.x ,(N + dimBlock.y - 1) / dimBlock.y); hipLaunchKernelGGL(( vecMatrixTransposed), dim3(dimGrid),dim3(dimBlock), 0, 0, Adefault,B); hipMemcpy(size, B, sizearr, hipMemcpyDeviceToHost); hipFree(Adefault); hipFree(B); printf("Result\n"); int newline = 0; for (i=0; i < N * N; i++) { newline++; printf("%3d ",size[i]); if(newline == N) { newline = 0; printf("\n"); } } printf("\n"); return 0; }
7600618f5fba2433260fa3b1551737eb06714cc8.cu
#include <stdio.h> #define T 8 // As Threads #define N 16 __global__ void vecMatrixTransposed(int *A, int *B) { int x = blockIdx.x * blockDim.x + threadIdx.x; int y = blockIdx.y * blockDim.y+ threadIdx.y; int width = gridDim.x * T; for( int j = 0; j<T; j+=N ) { B[x*width + (j+y)] = A[(y+j)*width + x]; } } int main (int argc, char *argv[]) { int i,j; int size[N*N]; int A[N][N]; int sizearr = N*N *sizeof(int); int *Adefault,*B; for (i=0; i< N; i++) { for(j = 0 ; j<N ; j++ ) { A[i][j] = ((i*i) +1) * (j+1); printf("%5d ", A[i][j]); } } printf("\n"); cudaMalloc( (void**)&Adefault,sizearr); cudaMalloc( (void**)&B,sizearr); cudaMemcpy( Adefault, A, sizearr, cudaMemcpyHostToDevice); dim3 dimBlock(T,T); dim3 dimGrid((N+ dimBlock.x - 1)/ dimBlock.x ,(N + dimBlock.y - 1) / dimBlock.y); vecMatrixTransposed<<<dimGrid,dimBlock>>>(Adefault,B); cudaMemcpy(size, B, sizearr, cudaMemcpyDeviceToHost); cudaFree(Adefault); cudaFree(B); printf("Result\n"); int newline = 0; for (i=0; i < N * N; i++) { newline++; printf("%3d ",size[i]); if(newline == N) { newline = 0; printf("\n"); } } printf("\n"); return 0; }
e02db2a4aaa49363e2f8e6bf670f7ac7dd24d12c.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* * Copyright (c) Facebook, Inc. and its affiliates. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ #include <ATen/ATen.h> #include <ATen/hip/HIPContext.h> #include <ATen/hip/impl/HIPGuardImplMasqueradingAsCUDA.h> #include <cmath> #include <vector> template <typename scalar_t> __global__ void SigmoidAlphaBlendForwardKernel( // clang-format off const at::PackedTensorAccessor64<scalar_t, 4, at::RestrictPtrTraits> distances, // (N, H, W, K) const at::PackedTensorAccessor64<int64_t, 4, at::RestrictPtrTraits> pix_to_face, // (N, H, W, K) at::PackedTensorAccessor64<scalar_t, 3, at::RestrictPtrTraits> alphas, // (N, H, W) // clang-format on const scalar_t sigma, const int N, const int H, const int W, const int K) { // Parallelize over each pixel in images of // size H * W, for each image in the batch of size N. const int num_threads = gridDim.x * blockDim.x; const int tid = blockIdx.x * blockDim.x + threadIdx.x; // TODO: revisit performance of this kernel with shared memory usage for (int t_i = tid; t_i < N * H * W; t_i += num_threads) { // Convert linear index to 3D index const int n = t_i / (H * W); // batch index. const int pix_idx = t_i % (H * W); // TODO: fix index calculation for non square images. const int yi = pix_idx / W; const int xi = pix_idx % W; scalar_t alpha = 1.0; // Loop over all the faces for this pixel. for (int k = 0; k < K; k++) { // Index into (N, H, W, K) tensors const int f = pix_to_face[n][yi][xi][k]; if (f < 0) { // Sentinel value is -1 indicating no face overlaps the pixel. continue; } // The distance is negative if a pixel is inside a face and positive // outside the face. Therefore use -1.0 * the distance to get the // correct sign. scalar_t dist = -1.0 * distances[n][yi][xi][k]; // Calculate the sigmoid probability. scalar_t prob = 1. / (1. + exp(-dist / sigma)); // The cumulative product ensures that alpha will be 0.0 if at least 1 // face fully covers the pixel as for that face, prob will be 1.0. // This results in a multiplication by 0.0 because of the (1.0 - prob) // term. Therefore the final result of (1.0 - alpha) will be 1.0. alpha *= (1.0 - prob); } alphas[n][yi][xi] = 1.0 - alpha; } } at::Tensor SigmoidAlphaBlendForwardCuda( const at::Tensor& distances, // (N, H, W, K) const at::Tensor& pix_to_face, // (N, H, W, K) const float sigma) { const int N = distances.size(0); const int H = distances.size(1); const int W = distances.size(2); const int K = distances.size(3); at::Tensor alphas = at::zeros({N, H, W}, distances.options()); const size_t blocks = 1024; const size_t threads = 128; // Check inputs are on the same device at::TensorArg distances_t{distances, "distances", 1}, pix_to_face_t{pix_to_face, "pix_to_face", 2}; at::CheckedFrom c = "SigmoidAlphaBlendForwardCuda"; at::checkAllSameGPU(c, {distances_t, pix_to_face_t}); // Set the device for the kernel launch based on the device of distances at::hip::HIPGuardMasqueradingAsCUDA device_guard(distances.device()); hipStream_t stream = at::hip::getCurrentHIPStreamMasqueradingAsCUDA(); if (distances.numel() == 0) { AT_CUDA_CHECK(hipGetLastError()); return alphas; } AT_DISPATCH_FLOATING_TYPES( distances.scalar_type(), "sigmoid_alpha_blend_kernel", ([&] { // clang-format off hipLaunchKernelGGL(( SigmoidAlphaBlendForwardKernel<scalar_t>), dim3(blocks), dim3(threads), 0, stream, distances.packed_accessor64<scalar_t, 4, at::RestrictPtrTraits>(), pix_to_face.packed_accessor64<int64_t, 4, at::RestrictPtrTraits>(), alphas.packed_accessor64<scalar_t, 3, at::RestrictPtrTraits>(), sigma, N, H, W, K); // clang-format on })); AT_CUDA_CHECK(hipGetLastError()); return alphas; } template <typename scalar_t> __global__ void SigmoidAlphaBlendBackwardKernel( // clang-format off const at::PackedTensorAccessor64<scalar_t, 3, at::RestrictPtrTraits> grad_alphas, // (N, H, W) const at::PackedTensorAccessor64<scalar_t, 3, at::RestrictPtrTraits> alphas, // (N, H, W) const at::PackedTensorAccessor64<scalar_t, 4, at::RestrictPtrTraits> distances, // (N, H, W, K) const at::PackedTensorAccessor64<int64_t, 4, at::RestrictPtrTraits> pix_to_face, // (N, H, W, K) at::PackedTensorAccessor64<scalar_t, 4, at::RestrictPtrTraits> grad_distances, // (N, H, W) // clang-format on const scalar_t sigma, const int N, const int H, const int W, const int K) { // Parallelize over each of the top K faces for each pixel in images of // size H * W * K, for each image in the batch of size N. // Get block and thread index. const int n = blockIdx.x; const int num_pixels = H * W * K; const int num_threads = gridDim.y * blockDim.x; const int tid = blockIdx.y * blockDim.x + threadIdx.x; for (int t_i = tid; t_i < num_pixels; t_i += num_threads) { // Convert linear index to 3D index. int yi = t_i / (W * K); int xi = (t_i % (W * K)) / K; int k = (t_i % (W * K)) % K; const scalar_t alpha = 1.0 - alphas[n][yi][xi]; const scalar_t grad_alpha = grad_alphas[n][yi][xi]; const int f = pix_to_face[n][yi][xi][k]; // Sentinel value is -1 indicating no face overlaps the pixel. if (f >= 0) { // The distance is negative if a pixel is inside a face and positive // outside the face. Therefore use -1.0 * the distance to get the // correct sign. scalar_t dist = -1.0 * distances[n][yi][xi][k]; // Calculate the sigmoid probability. scalar_t prob = 1. / (1. + exp(-dist / sigma)); grad_distances[n][yi][xi][k] = grad_alpha * (-1.0 / sigma) * prob * alpha; } } } at::Tensor SigmoidAlphaBlendBackwardCuda( const at::Tensor& grad_alphas, // (N, H, W) const at::Tensor& alphas, // (N, H, W) const at::Tensor& distances, // (N, H, W, K) const at::Tensor& pix_to_face, // (N, H, W, K) float sigma) { const int N = distances.size(0); const int H = distances.size(1); const int W = distances.size(2); const int K = distances.size(3); at::Tensor grad_distances = at::zeros({N, H, W, K}, distances.options()); const dim3 threads(512); const dim3 blocks(N, 1024 / N + 1); at::TensorArg grad_alphas_t{grad_alphas, "grad_alphas", 1}, alphas_t{alphas, "alphas", 2}, distances_t{distances, "distances", 3}, pix_to_face_t{pix_to_face, "pix_to_face", 4}; at::CheckedFrom c = "SigmoidAlphaBlendBackwardCuda"; at::checkAllSameGPU(c, {grad_alphas_t, alphas_t, distances_t, pix_to_face_t}); // Set the device for the kernel launch based on the device of distances at::hip::HIPGuardMasqueradingAsCUDA device_guard(alphas.device()); hipStream_t stream = at::hip::getCurrentHIPStreamMasqueradingAsCUDA(); if (alphas.numel() == 0) { AT_CUDA_CHECK(hipGetLastError()); return grad_alphas; } AT_DISPATCH_FLOATING_TYPES( distances.scalar_type(), "sigmoid_alpha_blend_backward_kernel", ([&] { hipLaunchKernelGGL(( SigmoidAlphaBlendBackwardKernel< scalar_t>), dim3(blocks), dim3(threads), 0, stream, // clang-format off grad_alphas.packed_accessor64<scalar_t, 3,at::RestrictPtrTraits>(), alphas.packed_accessor64<scalar_t, 3, at::RestrictPtrTraits>(), distances.packed_accessor64<scalar_t, 4, at::RestrictPtrTraits>(), pix_to_face.packed_accessor64<int64_t, 4, at::RestrictPtrTraits>(), grad_distances.packed_accessor64<scalar_t, 4, at::RestrictPtrTraits>(), // clang-format on sigma, N, H, W, K); })); AT_CUDA_CHECK(hipGetLastError()); return grad_distances; }
e02db2a4aaa49363e2f8e6bf670f7ac7dd24d12c.cu
/* * Copyright (c) Facebook, Inc. and its affiliates. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ #include <ATen/ATen.h> #include <ATen/cuda/CUDAContext.h> #include <c10/cuda/CUDAGuard.h> #include <cmath> #include <vector> template <typename scalar_t> __global__ void SigmoidAlphaBlendForwardKernel( // clang-format off const at::PackedTensorAccessor64<scalar_t, 4, at::RestrictPtrTraits> distances, // (N, H, W, K) const at::PackedTensorAccessor64<int64_t, 4, at::RestrictPtrTraits> pix_to_face, // (N, H, W, K) at::PackedTensorAccessor64<scalar_t, 3, at::RestrictPtrTraits> alphas, // (N, H, W) // clang-format on const scalar_t sigma, const int N, const int H, const int W, const int K) { // Parallelize over each pixel in images of // size H * W, for each image in the batch of size N. const int num_threads = gridDim.x * blockDim.x; const int tid = blockIdx.x * blockDim.x + threadIdx.x; // TODO: revisit performance of this kernel with shared memory usage for (int t_i = tid; t_i < N * H * W; t_i += num_threads) { // Convert linear index to 3D index const int n = t_i / (H * W); // batch index. const int pix_idx = t_i % (H * W); // TODO: fix index calculation for non square images. const int yi = pix_idx / W; const int xi = pix_idx % W; scalar_t alpha = 1.0; // Loop over all the faces for this pixel. for (int k = 0; k < K; k++) { // Index into (N, H, W, K) tensors const int f = pix_to_face[n][yi][xi][k]; if (f < 0) { // Sentinel value is -1 indicating no face overlaps the pixel. continue; } // The distance is negative if a pixel is inside a face and positive // outside the face. Therefore use -1.0 * the distance to get the // correct sign. scalar_t dist = -1.0 * distances[n][yi][xi][k]; // Calculate the sigmoid probability. scalar_t prob = 1. / (1. + exp(-dist / sigma)); // The cumulative product ensures that alpha will be 0.0 if at least 1 // face fully covers the pixel as for that face, prob will be 1.0. // This results in a multiplication by 0.0 because of the (1.0 - prob) // term. Therefore the final result of (1.0 - alpha) will be 1.0. alpha *= (1.0 - prob); } alphas[n][yi][xi] = 1.0 - alpha; } } at::Tensor SigmoidAlphaBlendForwardCuda( const at::Tensor& distances, // (N, H, W, K) const at::Tensor& pix_to_face, // (N, H, W, K) const float sigma) { const int N = distances.size(0); const int H = distances.size(1); const int W = distances.size(2); const int K = distances.size(3); at::Tensor alphas = at::zeros({N, H, W}, distances.options()); const size_t blocks = 1024; const size_t threads = 128; // Check inputs are on the same device at::TensorArg distances_t{distances, "distances", 1}, pix_to_face_t{pix_to_face, "pix_to_face", 2}; at::CheckedFrom c = "SigmoidAlphaBlendForwardCuda"; at::checkAllSameGPU(c, {distances_t, pix_to_face_t}); // Set the device for the kernel launch based on the device of distances at::cuda::CUDAGuard device_guard(distances.device()); cudaStream_t stream = at::cuda::getCurrentCUDAStream(); if (distances.numel() == 0) { AT_CUDA_CHECK(cudaGetLastError()); return alphas; } AT_DISPATCH_FLOATING_TYPES( distances.scalar_type(), "sigmoid_alpha_blend_kernel", ([&] { // clang-format off SigmoidAlphaBlendForwardKernel<scalar_t><<<blocks, threads, 0, stream>>>( distances.packed_accessor64<scalar_t, 4, at::RestrictPtrTraits>(), pix_to_face.packed_accessor64<int64_t, 4, at::RestrictPtrTraits>(), alphas.packed_accessor64<scalar_t, 3, at::RestrictPtrTraits>(), sigma, N, H, W, K); // clang-format on })); AT_CUDA_CHECK(cudaGetLastError()); return alphas; } template <typename scalar_t> __global__ void SigmoidAlphaBlendBackwardKernel( // clang-format off const at::PackedTensorAccessor64<scalar_t, 3, at::RestrictPtrTraits> grad_alphas, // (N, H, W) const at::PackedTensorAccessor64<scalar_t, 3, at::RestrictPtrTraits> alphas, // (N, H, W) const at::PackedTensorAccessor64<scalar_t, 4, at::RestrictPtrTraits> distances, // (N, H, W, K) const at::PackedTensorAccessor64<int64_t, 4, at::RestrictPtrTraits> pix_to_face, // (N, H, W, K) at::PackedTensorAccessor64<scalar_t, 4, at::RestrictPtrTraits> grad_distances, // (N, H, W) // clang-format on const scalar_t sigma, const int N, const int H, const int W, const int K) { // Parallelize over each of the top K faces for each pixel in images of // size H * W * K, for each image in the batch of size N. // Get block and thread index. const int n = blockIdx.x; const int num_pixels = H * W * K; const int num_threads = gridDim.y * blockDim.x; const int tid = blockIdx.y * blockDim.x + threadIdx.x; for (int t_i = tid; t_i < num_pixels; t_i += num_threads) { // Convert linear index to 3D index. int yi = t_i / (W * K); int xi = (t_i % (W * K)) / K; int k = (t_i % (W * K)) % K; const scalar_t alpha = 1.0 - alphas[n][yi][xi]; const scalar_t grad_alpha = grad_alphas[n][yi][xi]; const int f = pix_to_face[n][yi][xi][k]; // Sentinel value is -1 indicating no face overlaps the pixel. if (f >= 0) { // The distance is negative if a pixel is inside a face and positive // outside the face. Therefore use -1.0 * the distance to get the // correct sign. scalar_t dist = -1.0 * distances[n][yi][xi][k]; // Calculate the sigmoid probability. scalar_t prob = 1. / (1. + exp(-dist / sigma)); grad_distances[n][yi][xi][k] = grad_alpha * (-1.0 / sigma) * prob * alpha; } } } at::Tensor SigmoidAlphaBlendBackwardCuda( const at::Tensor& grad_alphas, // (N, H, W) const at::Tensor& alphas, // (N, H, W) const at::Tensor& distances, // (N, H, W, K) const at::Tensor& pix_to_face, // (N, H, W, K) float sigma) { const int N = distances.size(0); const int H = distances.size(1); const int W = distances.size(2); const int K = distances.size(3); at::Tensor grad_distances = at::zeros({N, H, W, K}, distances.options()); const dim3 threads(512); const dim3 blocks(N, 1024 / N + 1); at::TensorArg grad_alphas_t{grad_alphas, "grad_alphas", 1}, alphas_t{alphas, "alphas", 2}, distances_t{distances, "distances", 3}, pix_to_face_t{pix_to_face, "pix_to_face", 4}; at::CheckedFrom c = "SigmoidAlphaBlendBackwardCuda"; at::checkAllSameGPU(c, {grad_alphas_t, alphas_t, distances_t, pix_to_face_t}); // Set the device for the kernel launch based on the device of distances at::cuda::CUDAGuard device_guard(alphas.device()); cudaStream_t stream = at::cuda::getCurrentCUDAStream(); if (alphas.numel() == 0) { AT_CUDA_CHECK(cudaGetLastError()); return grad_alphas; } AT_DISPATCH_FLOATING_TYPES( distances.scalar_type(), "sigmoid_alpha_blend_backward_kernel", ([&] { SigmoidAlphaBlendBackwardKernel< scalar_t><<<blocks, threads, 0, stream>>>( // clang-format off grad_alphas.packed_accessor64<scalar_t, 3,at::RestrictPtrTraits>(), alphas.packed_accessor64<scalar_t, 3, at::RestrictPtrTraits>(), distances.packed_accessor64<scalar_t, 4, at::RestrictPtrTraits>(), pix_to_face.packed_accessor64<int64_t, 4, at::RestrictPtrTraits>(), grad_distances.packed_accessor64<scalar_t, 4, at::RestrictPtrTraits>(), // clang-format on sigma, N, H, W, K); })); AT_CUDA_CHECK(cudaGetLastError()); return grad_distances; }
c4b0e9ae684a5fb0fe837f5c6882f0511fd291f4.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include<stdio.h> #include<stdlib.h> #include<sys/time.h> #define NUM 10000000 #define CUDA_ERROR_EXIT(str) do{\ hipError_t err = hipGetLastError();\ if( err != hipSuccess){\ printf("Cuda Error: '%s' for %s\n", hipGetErrorString(err), str);\ exit(-1);\ }\ }while(0); #define TDIFF(start, end) ((end.tv_sec - start.tv_sec) * 1000000UL + (end.tv_usec - start.tv_usec)) struct num_array{ double num1; double num2; double result; }; __device__ void function(struct num_array *a) { double square = a ->num1 * a->num1 + a->num2 * a->num2 + 2 * a->num1 * a->num2; a->result = log(square)/sin(square); return; } __global__ void calculate(char *mem, int num) { int i = (blockDim.x * blockDim.y)* blockIdx.x + blockDim.y * threadIdx.x + threadIdx.y; if(i >= num) return; struct num_array *a = (struct num_array *)(mem + (i * 3 * sizeof(double))); function(a); } int main(int argc, char **argv) { struct timeval start, end, t_start, t_end; int i; struct num_array *pa; char *ptr; char *sptr; char *gpu_mem; unsigned long num = NUM; /*Default value of num from MACRO*/ int blocks; if(argc == 4){ num = atoi(argv[1]); /*Update after checking*/ row = atoi(argv[2]); col = atoi(argv[3]); if(num <= 0) num = NUM; } /* Allocate host (CPU) memory and initialize*/ ptr = (char *)malloc(num * 3 * sizeof(double)); sptr = ptr; for(i=0; i<num; ++i){ pa = (struct num_array *) sptr; pa->num1 = (double) i + (double) i * 0.1; pa->num2 = pa->num1 + 1.0; sptr += 3 * sizeof(double); } gettimeofday(&t_start, NULL); /* Allocate GPU memory and copy from CPU --> GPU*/ hipMalloc(&gpu_mem, num * 3 * sizeof(double)); CUDA_ERROR_EXIT("hipMalloc"); hipMemcpy(gpu_mem, ptr, num * 3 * sizeof(double) , hipMemcpyHostToDevice); CUDA_ERROR_EXIT("hipMemcpy"); gettimeofday(&start, NULL); blocks = num /row*col; if(num % (row*col)) ++blocks; hipLaunchKernelGGL(( calculate), dim3(blocks), dim3(dim3 blockDim[row][col][1]), 0, 0, gpu_mem, num); CUDA_ERROR_EXIT("kernel invocation"); gettimeofday(&end, NULL); /* Copy back result*/ hipMemcpy(ptr, gpu_mem, num * 3 * sizeof(double) , hipMemcpyDeviceToHost); CUDA_ERROR_EXIT("memcpy"); gettimeofday(&t_end, NULL); printf("Total time = %ld microsecs Processsing =%ld microsecs\n", TDIFF(t_start, t_end), TDIFF(start, end)); hipFree(gpu_mem); sptr = ptr; /*Print the last element for sanity check*/ pa = (struct num_array *) (sptr + (num -1)*3*sizeof(double)); printf("num1=%f num2=%f result=%f\n", pa->num1, pa->num2, pa->result); free(ptr); }
c4b0e9ae684a5fb0fe837f5c6882f0511fd291f4.cu
#include<stdio.h> #include<stdlib.h> #include<sys/time.h> #define NUM 10000000 #define CUDA_ERROR_EXIT(str) do{\ cudaError err = cudaGetLastError();\ if( err != cudaSuccess){\ printf("Cuda Error: '%s' for %s\n", cudaGetErrorString(err), str);\ exit(-1);\ }\ }while(0); #define TDIFF(start, end) ((end.tv_sec - start.tv_sec) * 1000000UL + (end.tv_usec - start.tv_usec)) struct num_array{ double num1; double num2; double result; }; __device__ void function(struct num_array *a) { double square = a ->num1 * a->num1 + a->num2 * a->num2 + 2 * a->num1 * a->num2; a->result = log(square)/sin(square); return; } __global__ void calculate(char *mem, int num) { int i = (blockDim.x * blockDim.y)* blockIdx.x + blockDim.y * threadIdx.x + threadIdx.y; if(i >= num) return; struct num_array *a = (struct num_array *)(mem + (i * 3 * sizeof(double))); function(a); } int main(int argc, char **argv) { struct timeval start, end, t_start, t_end; int i; struct num_array *pa; char *ptr; char *sptr; char *gpu_mem; unsigned long num = NUM; /*Default value of num from MACRO*/ int blocks; if(argc == 4){ num = atoi(argv[1]); /*Update after checking*/ row = atoi(argv[2]); col = atoi(argv[3]); if(num <= 0) num = NUM; } /* Allocate host (CPU) memory and initialize*/ ptr = (char *)malloc(num * 3 * sizeof(double)); sptr = ptr; for(i=0; i<num; ++i){ pa = (struct num_array *) sptr; pa->num1 = (double) i + (double) i * 0.1; pa->num2 = pa->num1 + 1.0; sptr += 3 * sizeof(double); } gettimeofday(&t_start, NULL); /* Allocate GPU memory and copy from CPU --> GPU*/ cudaMalloc(&gpu_mem, num * 3 * sizeof(double)); CUDA_ERROR_EXIT("cudaMalloc"); cudaMemcpy(gpu_mem, ptr, num * 3 * sizeof(double) , cudaMemcpyHostToDevice); CUDA_ERROR_EXIT("cudaMemcpy"); gettimeofday(&start, NULL); blocks = num /row*col; if(num % (row*col)) ++blocks; calculate<<<blocks, dim3 blockDim[row][col][1]>>>(gpu_mem, num); CUDA_ERROR_EXIT("kernel invocation"); gettimeofday(&end, NULL); /* Copy back result*/ cudaMemcpy(ptr, gpu_mem, num * 3 * sizeof(double) , cudaMemcpyDeviceToHost); CUDA_ERROR_EXIT("memcpy"); gettimeofday(&t_end, NULL); printf("Total time = %ld microsecs Processsing =%ld microsecs\n", TDIFF(t_start, t_end), TDIFF(start, end)); cudaFree(gpu_mem); sptr = ptr; /*Print the last element for sanity check*/ pa = (struct num_array *) (sptr + (num -1)*3*sizeof(double)); printf("num1=%f num2=%f result=%f\n", pa->num1, pa->num2, pa->result); free(ptr); }
865a16c4b985610c6833f3e4fa1bbf056579b19e.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <vector> #include "caffe/layers/winograd_layer.hpp" #include "caffe/util/winograd.hpp" namespace caffe { template <typename Dtype> __global__ void winograd_input_im2col_gpu_kernel( const int n, const Dtype *data, Dtype *col_buff, int height, int width, int pad_h, int pad_w, int ntiles_h, int ntiles_w, int tile_h_in, int tile_w_in, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_in; const int y = index/tile_w_in%tile_h_in; const int tile_w = index/tile_w_in/tile_h_in%ntiles_w; const int tile_h = index/tile_w_in/tile_h_in/ntiles_w%ntiles_h; const int c = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h/nchannels; int in_y = tile_h*tile_h_out + y - pad_h; int in_x = tile_w*tile_w_out + x - pad_w; if (in_y < 0 || in_x < 0 || in_y >= height || in_x >= width) { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_in + y)*tile_w_in + x] = 0; } else { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_in + y)*tile_w_in + x] = data[((image_idx*nchannels + c)*height + in_y)*width + in_x]; } } } template <typename Dtype> __global__ void winograd_output_col2im_gpu_kernel( const int n, const Dtype *col_buff, Dtype *data, int output_h, int output_w, int ntiles_h, int ntiles_w, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_out; const int y = index/tile_w_out%tile_h_out; const int tile_w = index/tile_w_out/tile_h_out%ntiles_w; const int tile_h = index/tile_w_out/tile_h_out/ntiles_w%ntiles_h; const int c = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h/nchannels; int out_y = tile_h*tile_h_out + y; int out_x = tile_w*tile_w_out + x; if (out_y < output_h && out_x < output_w) { data[((image_idx*nchannels + c)*output_h + out_y)*output_w + out_x] = col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_out + y)*tile_w_out + x]; } } } template <typename Dtype> __global__ void winograd_output_im2col_gpu_kernel( const int n, const Dtype *data, Dtype *col_buff, int output_h, int output_w, int ntiles_h, int ntiles_w, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_out; const int y = index/tile_w_out%tile_h_out; const int tile_w = index/tile_w_out/tile_h_out%ntiles_w; const int tile_h = index/tile_w_out/tile_h_out/ntiles_w%ntiles_h; const int c = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h/nchannels; int out_y = tile_h*tile_h_out + y; int out_x = tile_w*tile_w_out + x; if (out_y < 0 || out_x < 0 || out_y >= output_h || out_x >= output_w) { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_out + y)*tile_w_out + x] = 0; } else { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_out + y)*tile_w_out + x] = data[((image_idx*nchannels + c)*output_h + out_y)*output_w + out_x]; } } } template <typename Dtype> __global__ void winograd_input_col2im_gpu_kernel( const int n, const Dtype *col_buff, Dtype *data, int height, int width, int pad_h, int pad_w, int ntiles_h, int ntiles_w, int tile_h_in, int tile_w_in, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { int m = batch_size*nchannels*height*width; CUDA_KERNEL_LOOP(index, m) { data[index] = 0; } CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_in; const int y = index/tile_w_in%tile_h_in; const int tile_w = index/tile_w_in/tile_h_in%ntiles_w; const int tile_h = index/tile_w_in/tile_h_in/ntiles_w%ntiles_h; const int c = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h/nchannels; int in_y = tile_h*tile_h_out + y - pad_h; int in_x = tile_w*tile_w_out + x - pad_w; if (in_y >= 0 && in_x >= 0 && in_y < height && in_x < width) { data[((image_idx*nchannels + c)*height + in_y)*width + in_x] += col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_in + y)*tile_w_in + x]; } } } template <> void WinogradLayer<double>::Forward_gpu(const vector<Blob<double>*>& bottom, const vector<Blob<double>*>& top) { NOT_IMPLEMENTED; } //#define PROFILE_WINOGRAD template <> void WinogradLayer<float>::Forward_gpu(const vector<Blob<float>*>& bottom, const vector<Blob<float>*>& top) { int kernel_h = this->kernel_shape_.cpu_data()[0], kernel_w = this->kernel_shape_.cpu_data()[1]; WinogradAKronA<float> *AKronA = WinogradAKronA<float>::getInstance(kernel_h); WinogradBKronB<float> *BKronB = WinogradBKronB<float>::getInstance(kernel_h); WinogradGKronG<float> *GKronG = WinogradGKronG<float>::getInstance(kernel_h); const float* weight = this->blobs_[0]->gpu_data(); #ifdef PROFILE_WINOGRAD CPUTimer timer; #endif for (int i = 0; i < bottom.size(); ++i) { const float* bottom_data = bottom[i]->gpu_data(); float* top_data = top[i]->mutable_gpu_data(); int M = this->conv_in_channels_*ntiles_h_*ntiles_w_; int num_kernels = this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_in_*tile_w_in_; int height = this->conv_input_shape_.cpu_data()[1], width = this->conv_input_shape_.cpu_data()[2]; int pad_h = this->pad_.cpu_data()[0], pad_w = this->pad_.cpu_data()[1]; #ifdef PROFILE_WINOGRAD timer.Start(); #endif hipLaunchKernelGGL(( winograd_input_im2col_gpu_kernel<float>), dim3(CAFFE_GET_BLOCKS(num_kernels)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, num_kernels, bottom_data, temp2_.mutable_gpu_data(), height, width, pad_h, pad_w, ntiles_h_, ntiles_w_, tile_h_in_, tile_w_in_, tile_h_out_, tile_w_out_, this->conv_in_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_input_im2col takes " << timer.MicroSeconds()/1e6; #endif // Transform input to Winograd domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasTrans, tile_h_in_*tile_w_in_, this->num_*M, tile_h_in_*tile_w_in_, (float)1, BKronB->get()->gpu_data(), temp2_.mutable_gpu_data(), (float)0, temp1_.mutable_gpu_data()); // temp1_ has (tile_h_in*tile_w_in) x conv_in_channels x num_ x (ntiles_h*ntiles_w) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Transformation of bottom takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif // Convolution in Winograd domain { float alpha = 1, beta = 0; int M = this->conv_out_channels_/this->group_; int N = this->num_*ntiles_h_*ntiles_w_; int K = this->conv_in_channels_/this->group_; if (!weight_ptrs_initialized_) { float **weight_ptrs = (float **)weight_ptrs_->mutable_cpu_data(); for (int j = 0; j < tile_h_in_*tile_w_in_*this->group_; ++j) { weight_ptrs[j] = this->blobs_[0]->mutable_gpu_data() + j*(this->conv_out_channels_/this->group_)*(this->conv_in_channels_/this->group_); } weight_ptrs_initialized_ = true; } CUBLAS_CHECK(hipblasSgemmBatched( Caffe::cublas_handle(), HIPBLAS_OP_N, HIPBLAS_OP_N, N, M, K, &alpha, (const float **)in_activation_ptrs_->gpu_data(), N, (const float **)weight_ptrs_->gpu_data(), K, &beta, (float **)out_activation_ptrs_->mutable_gpu_data(), N, in_activation_ptrs_->count())); } // col_buff has (tile_h_in*tile_w_in) x conv_out_channels x num_ x (ntiles_h*ntiles_w) #ifdef PROFILE_WINOGRAD LOG(INFO) << "Convolution takes " << timer.MicroSeconds()/1e6; #endif // Transform back to time domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasNoTrans, this->conv_out_channels_*this->num_*ntiles_h_*ntiles_w_, tile_h_out_*tile_w_out_, tile_h_in_*tile_w_in_, (float)1, temp2_.gpu_data(), AKronA->get()->gpu_data(), (float)0, temp1_.mutable_gpu_data()); #ifdef PROFILE_WINOGRAD LOG(INFO) << "Inverse transformation of top takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif num_kernels = this->conv_out_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_out_*tile_w_out_; const int output_h = this->output_shape_[0], output_w = this->output_shape_[1]; hipLaunchKernelGGL(( winograd_output_col2im_gpu_kernel<float>), dim3(CAFFE_GET_BLOCKS(num_kernels)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, num_kernels, temp1_.gpu_data(), top_data, output_h, output_w, ntiles_h_, ntiles_w_, tile_h_out_, tile_w_out_, this->conv_out_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_output_col2im takes " << timer.MicroSeconds()/1e6; #endif for (int n = 0; n < this->num_; ++n) { // JSP: this->num_ is batch size if (this->bias_term_) { const float* bias = this->blobs_[1]->gpu_data(); this->forward_gpu_bias(top_data + n * this->top_dim_, bias); } } } } template <> void WinogradLayer<double>::Backward_gpu(const vector<Blob<double>*>& top, const vector<bool>& propagate_down, const vector<Blob<double>*>& bottom) { NOT_IMPLEMENTED; } template <> void WinogradLayer<float>::Backward_gpu(const vector<Blob<float>*>& top, const vector<bool>& propagate_down, const vector<Blob<float>*>& bottom) { int kernel_h = this->kernel_shape_.cpu_data()[0], kernel_w = this->kernel_shape_.cpu_data()[1]; WinogradAKronA<float> *AKronA = WinogradAKronA<float>::getInstance(kernel_h); WinogradBKronB<float> *BKronB = WinogradBKronB<float>::getInstance(kernel_h); WinogradGKronG<float> *GKronG = WinogradGKronG<float>::getInstance(kernel_h); const float* weight = this->blobs_[0]->gpu_data(); float* weight_diff = this->blobs_[0]->mutable_gpu_diff(); /*const float *weight_cpu = this->blobs_[0]->cpu_data(); fprintf(stderr, "weight_winograd\n"); for (int j = 0; j < tile_h_in_*tile_w_in_; ++j) { for (int n = 0; n < this->conv_out_channels_; ++n) { for (int c = 0; c < this->conv_in_channels_; ++c) { fprintf(stderr, "%g ", weight_cpu[(j*this->conv_out_channels_ + n)*this->conv_in_channels_ + c]); } } fprintf(stderr, "\n"); }*/ #ifdef PROFILE_WINOGRAD CPUTimer timer; #endif for (int i = 0; i < top.size(); ++i) { const float* top_diff = top[i]->gpu_diff(); const float* bottom_data = bottom[i]->gpu_data(); float* bottom_diff = bottom[i]->mutable_gpu_diff(); // Bias gradient, if necessary. if (this->bias_term_ && this->param_propagate_down_[1]) { float* bias_diff = this->blobs_[1]->mutable_gpu_diff(); for (int n = 0; n < this->num_; ++n) { this->backward_gpu_bias(bias_diff, top_diff + n * this->top_dim_); } } if (this->param_propagate_down_[0] || propagate_down[i]) { int M = this->conv_out_channels_*ntiles_h_*ntiles_w_; int num_kernels = this->num_*this->conv_out_channels_*ntiles_h_*ntiles_w_*tile_h_out_*tile_w_out_; const int output_h = this->output_shape_[0], output_w = this->output_shape_[1]; const int height = this->conv_input_shape_.cpu_data()[1], width = this->conv_input_shape_.cpu_data()[2]; const int pad_h = this->pad_.cpu_data()[0], pad_w = this->pad_.cpu_data()[1]; #ifdef PROFILE_WINOGRAD timer.Start(); #endif hipLaunchKernelGGL(( winograd_output_im2col_gpu_kernel<float>), dim3(CAFFE_GET_BLOCKS(num_kernels)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, num_kernels, top_diff, temp1_.mutable_gpu_data(), output_h, output_w, ntiles_h_, ntiles_w_, tile_h_out_, tile_w_out_, this->conv_out_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_output_im2col takes " << timer.MicroSeconds()/1e6; #endif // Transform out_diff to Winograd domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasNoTrans, CblasTrans, tile_h_in_*tile_w_in_, this->num_*M, tile_h_out_*tile_w_out_, (float)1, AKronA->get()->gpu_data(), temp1_.mutable_gpu_data(), (float)0, temp2_.mutable_gpu_data()); // temp2_ has (tile_h_in*tile_w_in) x conv_out_channels x num_ x (ntiles_h*ntiles_w) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Transformation of top_diff takes " << timer.MicroSeconds()/1e6; #endif // gradient w.r.t. weight. Note that we will accumulate diffs. if (this->param_propagate_down_[0]) { #ifdef PROFILE_WINOGRAD timer.Start(); #endif int num_kernels = this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_in_*tile_w_in_; hipLaunchKernelGGL(( winograd_input_im2col_gpu_kernel<float>), dim3(CAFFE_GET_BLOCKS(num_kernels)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, num_kernels, bottom_data, this->col_buffer_.mutable_gpu_data(), height, width, pad_h, pad_w, ntiles_h_, ntiles_w_, tile_h_in_, tile_w_in_, tile_h_out_, tile_w_out_, this->conv_in_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_input_im2col takes " << timer.MicroSeconds()/1e6; #endif // Transform input to Winograd domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasTrans, tile_h_in_*tile_w_in_, this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_, tile_h_in_*tile_w_in_, (float)1, BKronB->get()->gpu_data(), this->col_buffer_.mutable_gpu_data(), (float)0, temp1_.mutable_gpu_data()); // temp1_ has (tile_h_in*tile_w_in) x conv_in_channels x num_ x (ntiles_h*ntiles_w) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Transformation of bottom takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif if (!weight_diff_ptrs_initialized_) { float **weight_diff_ptrs = (float **)weight_diff_ptrs_->mutable_cpu_data(); for (int j = 0; j < tile_h_in_*tile_w_in_*this->group_; ++j) { weight_diff_ptrs[j] = this->blobs_[0]->mutable_gpu_diff() + j*(this->conv_out_channels_/this->group_)*(this->conv_in_channels_/this->group_); } weight_diff_ptrs_initialized_ = true; } float alpha = 1, beta = 1; int M = this->conv_out_channels_/this->group_; int N = this->conv_in_channels_/this->group_; int K = this->num_*ntiles_h_*ntiles_w_; CUBLAS_CHECK(hipblasSgemmBatched( Caffe::cublas_handle(), HIPBLAS_OP_T, HIPBLAS_OP_N, N, M, K, &alpha, (const float **)in_activation_ptrs_->gpu_data(), K, (const float **)out_activation_ptrs_->gpu_data(), K, &beta, (float **)weight_diff_ptrs_->mutable_gpu_data(), N, tile_h_in_*tile_w_in_*this->group_)); // weight_diff has (tile_h_in*tile_w_in) x (conv_out_channels) x (conv_in_channels/group) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Convolution for weight gradient takes " << timer.MicroSeconds()/1e6; #endif } // param_propagate_down_[0] // gradient w.r.t. bottom data, if necessary. if (propagate_down[i]) { #ifdef PROFILE_WINOGRAD timer.Start(); #endif // Convolution in Winograd domain float alpha = 1, beta = 0; int M = this->conv_in_channels_/this->group_; int N = this->num_*ntiles_h_*ntiles_w_; int K = this->conv_out_channels_/this->group_; CUBLAS_CHECK(hipblasSgemmBatched( Caffe::cublas_handle(), HIPBLAS_OP_N, HIPBLAS_OP_T, N, M, K, &alpha, (const float **)out_activation_ptrs_->gpu_data(), N, (const float **)weight_ptrs_->gpu_data(), M, &beta, (float **)in_activation_ptrs_->mutable_gpu_data(), N, in_activation_ptrs_->count())); #ifdef PROFILE_WINOGRAD LOG(INFO) << "Convolution for bottom gradient takes " << timer.MicroSeconds()/1e6; #endif // Transform back to time domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasTrans, this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_, tile_h_in_*tile_w_in_, tile_h_in_*tile_w_in_, (float)1, temp1_.mutable_gpu_data(), BKronB->get()->gpu_data(), (float)0, this->col_buffer_.mutable_gpu_data()); #ifdef PROFILE_WINOGRAD LOG(INFO) << "Inverse transformation of bottom_diff takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif num_kernels = this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_in_*tile_w_in_; hipLaunchKernelGGL(( winograd_input_col2im_gpu_kernel<float>), dim3(CAFFE_GET_BLOCKS(num_kernels)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, num_kernels, this->col_buffer_.gpu_data(), bottom_diff, height, width, pad_h, pad_w, ntiles_h_, ntiles_w_, tile_h_in_, tile_w_in_, tile_h_out_, tile_w_out_, this->conv_in_channels_, this->num_); #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_input_col2im takes " << timer.MicroSeconds()/1e6; #endif } // propagate_down_[i] } } } INSTANTIATE_LAYER_GPU_FUNCS(WinogradLayer); } // namespace caffe
865a16c4b985610c6833f3e4fa1bbf056579b19e.cu
#include <vector> #include "caffe/layers/winograd_layer.hpp" #include "caffe/util/winograd.hpp" namespace caffe { template <typename Dtype> __global__ void winograd_input_im2col_gpu_kernel( const int n, const Dtype *data, Dtype *col_buff, int height, int width, int pad_h, int pad_w, int ntiles_h, int ntiles_w, int tile_h_in, int tile_w_in, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_in; const int y = index/tile_w_in%tile_h_in; const int tile_w = index/tile_w_in/tile_h_in%ntiles_w; const int tile_h = index/tile_w_in/tile_h_in/ntiles_w%ntiles_h; const int c = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h/nchannels; int in_y = tile_h*tile_h_out + y - pad_h; int in_x = tile_w*tile_w_out + x - pad_w; if (in_y < 0 || in_x < 0 || in_y >= height || in_x >= width) { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_in + y)*tile_w_in + x] = 0; } else { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_in + y)*tile_w_in + x] = data[((image_idx*nchannels + c)*height + in_y)*width + in_x]; } } } template <typename Dtype> __global__ void winograd_output_col2im_gpu_kernel( const int n, const Dtype *col_buff, Dtype *data, int output_h, int output_w, int ntiles_h, int ntiles_w, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_out; const int y = index/tile_w_out%tile_h_out; const int tile_w = index/tile_w_out/tile_h_out%ntiles_w; const int tile_h = index/tile_w_out/tile_h_out/ntiles_w%ntiles_h; const int c = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h/nchannels; int out_y = tile_h*tile_h_out + y; int out_x = tile_w*tile_w_out + x; if (out_y < output_h && out_x < output_w) { data[((image_idx*nchannels + c)*output_h + out_y)*output_w + out_x] = col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_out + y)*tile_w_out + x]; } } } template <typename Dtype> __global__ void winograd_output_im2col_gpu_kernel( const int n, const Dtype *data, Dtype *col_buff, int output_h, int output_w, int ntiles_h, int ntiles_w, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_out; const int y = index/tile_w_out%tile_h_out; const int tile_w = index/tile_w_out/tile_h_out%ntiles_w; const int tile_h = index/tile_w_out/tile_h_out/ntiles_w%ntiles_h; const int c = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_out/tile_h_out/ntiles_w/ntiles_h/nchannels; int out_y = tile_h*tile_h_out + y; int out_x = tile_w*tile_w_out + x; if (out_y < 0 || out_x < 0 || out_y >= output_h || out_x >= output_w) { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_out + y)*tile_w_out + x] = 0; } else { col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_out + y)*tile_w_out + x] = data[((image_idx*nchannels + c)*output_h + out_y)*output_w + out_x]; } } } template <typename Dtype> __global__ void winograd_input_col2im_gpu_kernel( const int n, const Dtype *col_buff, Dtype *data, int height, int width, int pad_h, int pad_w, int ntiles_h, int ntiles_w, int tile_h_in, int tile_w_in, int tile_h_out, int tile_w_out, int nchannels, int batch_size) { int m = batch_size*nchannels*height*width; CUDA_KERNEL_LOOP(index, m) { data[index] = 0; } CUDA_KERNEL_LOOP(index, n) { const int x = index%tile_w_in; const int y = index/tile_w_in%tile_h_in; const int tile_w = index/tile_w_in/tile_h_in%ntiles_w; const int tile_h = index/tile_w_in/tile_h_in/ntiles_w%ntiles_h; const int c = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h%nchannels; const int image_idx = index/tile_w_in/tile_h_in/ntiles_w/ntiles_h/nchannels; int in_y = tile_h*tile_h_out + y - pad_h; int in_x = tile_w*tile_w_out + x - pad_w; if (in_y >= 0 && in_x >= 0 && in_y < height && in_x < width) { data[((image_idx*nchannels + c)*height + in_y)*width + in_x] += col_buff[((((image_idx + c*batch_size)*ntiles_h + tile_h)*ntiles_w + tile_w)*tile_h_in + y)*tile_w_in + x]; } } } template <> void WinogradLayer<double>::Forward_gpu(const vector<Blob<double>*>& bottom, const vector<Blob<double>*>& top) { NOT_IMPLEMENTED; } //#define PROFILE_WINOGRAD template <> void WinogradLayer<float>::Forward_gpu(const vector<Blob<float>*>& bottom, const vector<Blob<float>*>& top) { int kernel_h = this->kernel_shape_.cpu_data()[0], kernel_w = this->kernel_shape_.cpu_data()[1]; WinogradAKronA<float> *AKronA = WinogradAKronA<float>::getInstance(kernel_h); WinogradBKronB<float> *BKronB = WinogradBKronB<float>::getInstance(kernel_h); WinogradGKronG<float> *GKronG = WinogradGKronG<float>::getInstance(kernel_h); const float* weight = this->blobs_[0]->gpu_data(); #ifdef PROFILE_WINOGRAD CPUTimer timer; #endif for (int i = 0; i < bottom.size(); ++i) { const float* bottom_data = bottom[i]->gpu_data(); float* top_data = top[i]->mutable_gpu_data(); int M = this->conv_in_channels_*ntiles_h_*ntiles_w_; int num_kernels = this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_in_*tile_w_in_; int height = this->conv_input_shape_.cpu_data()[1], width = this->conv_input_shape_.cpu_data()[2]; int pad_h = this->pad_.cpu_data()[0], pad_w = this->pad_.cpu_data()[1]; #ifdef PROFILE_WINOGRAD timer.Start(); #endif winograd_input_im2col_gpu_kernel<float><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>( num_kernels, bottom_data, temp2_.mutable_gpu_data(), height, width, pad_h, pad_w, ntiles_h_, ntiles_w_, tile_h_in_, tile_w_in_, tile_h_out_, tile_w_out_, this->conv_in_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_input_im2col takes " << timer.MicroSeconds()/1e6; #endif // Transform input to Winograd domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasTrans, tile_h_in_*tile_w_in_, this->num_*M, tile_h_in_*tile_w_in_, (float)1, BKronB->get()->gpu_data(), temp2_.mutable_gpu_data(), (float)0, temp1_.mutable_gpu_data()); // temp1_ has (tile_h_in*tile_w_in) x conv_in_channels x num_ x (ntiles_h*ntiles_w) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Transformation of bottom takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif // Convolution in Winograd domain { float alpha = 1, beta = 0; int M = this->conv_out_channels_/this->group_; int N = this->num_*ntiles_h_*ntiles_w_; int K = this->conv_in_channels_/this->group_; if (!weight_ptrs_initialized_) { float **weight_ptrs = (float **)weight_ptrs_->mutable_cpu_data(); for (int j = 0; j < tile_h_in_*tile_w_in_*this->group_; ++j) { weight_ptrs[j] = this->blobs_[0]->mutable_gpu_data() + j*(this->conv_out_channels_/this->group_)*(this->conv_in_channels_/this->group_); } weight_ptrs_initialized_ = true; } CUBLAS_CHECK(cublasSgemmBatched( Caffe::cublas_handle(), CUBLAS_OP_N, CUBLAS_OP_N, N, M, K, &alpha, (const float **)in_activation_ptrs_->gpu_data(), N, (const float **)weight_ptrs_->gpu_data(), K, &beta, (float **)out_activation_ptrs_->mutable_gpu_data(), N, in_activation_ptrs_->count())); } // col_buff has (tile_h_in*tile_w_in) x conv_out_channels x num_ x (ntiles_h*ntiles_w) #ifdef PROFILE_WINOGRAD LOG(INFO) << "Convolution takes " << timer.MicroSeconds()/1e6; #endif // Transform back to time domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasNoTrans, this->conv_out_channels_*this->num_*ntiles_h_*ntiles_w_, tile_h_out_*tile_w_out_, tile_h_in_*tile_w_in_, (float)1, temp2_.gpu_data(), AKronA->get()->gpu_data(), (float)0, temp1_.mutable_gpu_data()); #ifdef PROFILE_WINOGRAD LOG(INFO) << "Inverse transformation of top takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif num_kernels = this->conv_out_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_out_*tile_w_out_; const int output_h = this->output_shape_[0], output_w = this->output_shape_[1]; winograd_output_col2im_gpu_kernel<float><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>( num_kernels, temp1_.gpu_data(), top_data, output_h, output_w, ntiles_h_, ntiles_w_, tile_h_out_, tile_w_out_, this->conv_out_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_output_col2im takes " << timer.MicroSeconds()/1e6; #endif for (int n = 0; n < this->num_; ++n) { // JSP: this->num_ is batch size if (this->bias_term_) { const float* bias = this->blobs_[1]->gpu_data(); this->forward_gpu_bias(top_data + n * this->top_dim_, bias); } } } } template <> void WinogradLayer<double>::Backward_gpu(const vector<Blob<double>*>& top, const vector<bool>& propagate_down, const vector<Blob<double>*>& bottom) { NOT_IMPLEMENTED; } template <> void WinogradLayer<float>::Backward_gpu(const vector<Blob<float>*>& top, const vector<bool>& propagate_down, const vector<Blob<float>*>& bottom) { int kernel_h = this->kernel_shape_.cpu_data()[0], kernel_w = this->kernel_shape_.cpu_data()[1]; WinogradAKronA<float> *AKronA = WinogradAKronA<float>::getInstance(kernel_h); WinogradBKronB<float> *BKronB = WinogradBKronB<float>::getInstance(kernel_h); WinogradGKronG<float> *GKronG = WinogradGKronG<float>::getInstance(kernel_h); const float* weight = this->blobs_[0]->gpu_data(); float* weight_diff = this->blobs_[0]->mutable_gpu_diff(); /*const float *weight_cpu = this->blobs_[0]->cpu_data(); fprintf(stderr, "weight_winograd\n"); for (int j = 0; j < tile_h_in_*tile_w_in_; ++j) { for (int n = 0; n < this->conv_out_channels_; ++n) { for (int c = 0; c < this->conv_in_channels_; ++c) { fprintf(stderr, "%g ", weight_cpu[(j*this->conv_out_channels_ + n)*this->conv_in_channels_ + c]); } } fprintf(stderr, "\n"); }*/ #ifdef PROFILE_WINOGRAD CPUTimer timer; #endif for (int i = 0; i < top.size(); ++i) { const float* top_diff = top[i]->gpu_diff(); const float* bottom_data = bottom[i]->gpu_data(); float* bottom_diff = bottom[i]->mutable_gpu_diff(); // Bias gradient, if necessary. if (this->bias_term_ && this->param_propagate_down_[1]) { float* bias_diff = this->blobs_[1]->mutable_gpu_diff(); for (int n = 0; n < this->num_; ++n) { this->backward_gpu_bias(bias_diff, top_diff + n * this->top_dim_); } } if (this->param_propagate_down_[0] || propagate_down[i]) { int M = this->conv_out_channels_*ntiles_h_*ntiles_w_; int num_kernels = this->num_*this->conv_out_channels_*ntiles_h_*ntiles_w_*tile_h_out_*tile_w_out_; const int output_h = this->output_shape_[0], output_w = this->output_shape_[1]; const int height = this->conv_input_shape_.cpu_data()[1], width = this->conv_input_shape_.cpu_data()[2]; const int pad_h = this->pad_.cpu_data()[0], pad_w = this->pad_.cpu_data()[1]; #ifdef PROFILE_WINOGRAD timer.Start(); #endif winograd_output_im2col_gpu_kernel<float><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>( num_kernels, top_diff, temp1_.mutable_gpu_data(), output_h, output_w, ntiles_h_, ntiles_w_, tile_h_out_, tile_w_out_, this->conv_out_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_output_im2col takes " << timer.MicroSeconds()/1e6; #endif // Transform out_diff to Winograd domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasNoTrans, CblasTrans, tile_h_in_*tile_w_in_, this->num_*M, tile_h_out_*tile_w_out_, (float)1, AKronA->get()->gpu_data(), temp1_.mutable_gpu_data(), (float)0, temp2_.mutable_gpu_data()); // temp2_ has (tile_h_in*tile_w_in) x conv_out_channels x num_ x (ntiles_h*ntiles_w) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Transformation of top_diff takes " << timer.MicroSeconds()/1e6; #endif // gradient w.r.t. weight. Note that we will accumulate diffs. if (this->param_propagate_down_[0]) { #ifdef PROFILE_WINOGRAD timer.Start(); #endif int num_kernels = this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_in_*tile_w_in_; winograd_input_im2col_gpu_kernel<float><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>( num_kernels, bottom_data, this->col_buffer_.mutable_gpu_data(), height, width, pad_h, pad_w, ntiles_h_, ntiles_w_, tile_h_in_, tile_w_in_, tile_h_out_, tile_w_out_, this->conv_in_channels_, this->num_); CUDA_POST_KERNEL_CHECK; #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_input_im2col takes " << timer.MicroSeconds()/1e6; #endif // Transform input to Winograd domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasTrans, tile_h_in_*tile_w_in_, this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_, tile_h_in_*tile_w_in_, (float)1, BKronB->get()->gpu_data(), this->col_buffer_.mutable_gpu_data(), (float)0, temp1_.mutable_gpu_data()); // temp1_ has (tile_h_in*tile_w_in) x conv_in_channels x num_ x (ntiles_h*ntiles_w) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Transformation of bottom takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif if (!weight_diff_ptrs_initialized_) { float **weight_diff_ptrs = (float **)weight_diff_ptrs_->mutable_cpu_data(); for (int j = 0; j < tile_h_in_*tile_w_in_*this->group_; ++j) { weight_diff_ptrs[j] = this->blobs_[0]->mutable_gpu_diff() + j*(this->conv_out_channels_/this->group_)*(this->conv_in_channels_/this->group_); } weight_diff_ptrs_initialized_ = true; } float alpha = 1, beta = 1; int M = this->conv_out_channels_/this->group_; int N = this->conv_in_channels_/this->group_; int K = this->num_*ntiles_h_*ntiles_w_; CUBLAS_CHECK(cublasSgemmBatched( Caffe::cublas_handle(), CUBLAS_OP_T, CUBLAS_OP_N, N, M, K, &alpha, (const float **)in_activation_ptrs_->gpu_data(), K, (const float **)out_activation_ptrs_->gpu_data(), K, &beta, (float **)weight_diff_ptrs_->mutable_gpu_data(), N, tile_h_in_*tile_w_in_*this->group_)); // weight_diff has (tile_h_in*tile_w_in) x (conv_out_channels) x (conv_in_channels/group) dimension #ifdef PROFILE_WINOGRAD LOG(INFO) << "Convolution for weight gradient takes " << timer.MicroSeconds()/1e6; #endif } // param_propagate_down_[0] // gradient w.r.t. bottom data, if necessary. if (propagate_down[i]) { #ifdef PROFILE_WINOGRAD timer.Start(); #endif // Convolution in Winograd domain float alpha = 1, beta = 0; int M = this->conv_in_channels_/this->group_; int N = this->num_*ntiles_h_*ntiles_w_; int K = this->conv_out_channels_/this->group_; CUBLAS_CHECK(cublasSgemmBatched( Caffe::cublas_handle(), CUBLAS_OP_N, CUBLAS_OP_T, N, M, K, &alpha, (const float **)out_activation_ptrs_->gpu_data(), N, (const float **)weight_ptrs_->gpu_data(), M, &beta, (float **)in_activation_ptrs_->mutable_gpu_data(), N, in_activation_ptrs_->count())); #ifdef PROFILE_WINOGRAD LOG(INFO) << "Convolution for bottom gradient takes " << timer.MicroSeconds()/1e6; #endif // Transform back to time domain #ifdef PROFILE_WINOGRAD timer.Start(); #endif caffe_gpu_gemm<float>(CblasTrans, CblasTrans, this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_, tile_h_in_*tile_w_in_, tile_h_in_*tile_w_in_, (float)1, temp1_.mutable_gpu_data(), BKronB->get()->gpu_data(), (float)0, this->col_buffer_.mutable_gpu_data()); #ifdef PROFILE_WINOGRAD LOG(INFO) << "Inverse transformation of bottom_diff takes " << timer.MicroSeconds()/1e6; #endif #ifdef PROFILE_WINOGRAD timer.Start(); #endif num_kernels = this->conv_in_channels_*this->num_*ntiles_h_*ntiles_w_*tile_h_in_*tile_w_in_; winograd_input_col2im_gpu_kernel<float><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>( num_kernels, this->col_buffer_.gpu_data(), bottom_diff, height, width, pad_h, pad_w, ntiles_h_, ntiles_w_, tile_h_in_, tile_w_in_, tile_h_out_, tile_w_out_, this->conv_in_channels_, this->num_); #ifdef PROFILE_WINOGRAD LOG(INFO) << "winograd_input_col2im takes " << timer.MicroSeconds()/1e6; #endif } // propagate_down_[i] } } } INSTANTIATE_LAYER_GPU_FUNCS(WinogradLayer); } // namespace caffe
64a675b9c62e0b26b34680939471143861e258b3.hip
// !!! This is a file automatically generated by hipify!!! #ifdef _WIN32 #include <windows.h> #endif #include <stdio.h> #include <cstring> #include <stdlib.h> #include <string.h> #define GL_GLEXT_PROTOTYPES #include <GL/gl.h> #include <SDL/SDL.h> #include <hip/hip_runtime.h> #include <cuda_gl_interop.h> // Number of iterations per pixel #define ITERA 120 #define THREADS_PER_BLOCK 512 int MAXX; int MAXY; GLuint buffer; GLuint tex; # define CU_SAFE_CALL_NO_SYNC( call ) { \ hipError_t err = call; \ if( hipSuccess != err) { \ fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \ err, __FILE__, __LINE__ ); \ exit(EXIT_FAILURE); \ } } # define CU_SAFE_CALL( call ) CU_SAFE_CALL_NO_SYNC(call); unsigned __constant__ lookup[256]; int readtime() { return SDL_GetTicks(); } int kbhit(int *xx, int *yy) { SDL_PumpEvents(); Uint8 *keystate = SDL_GetKeyState(NULL); if ( keystate[SDLK_ESCAPE] ) return 1; int x,y; Uint8 btn = SDL_GetMouseState (&x, &y); if (btn & SDL_BUTTON(SDL_BUTTON_LEFT)) { *xx = x; *yy = y; return 2; } if (btn & SDL_BUTTON(SDL_BUTTON_RIGHT)) { *xx = x; *yy = y; return 3; } return 0; } void create_buffer(GLuint* buffer) { glGenBuffersARB(1, buffer); glBindBuffer(GL_PIXEL_PACK_BUFFER, *buffer); glBufferData(GL_PIXEL_PACK_BUFFER, MAXX*MAXY*4, NULL, GL_DYNAMIC_DRAW); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); } void destroy_buffer(GLuint* buffer) { glBindBuffer(GL_TEXTURE_2D, 0); glDeleteBuffers(1, buffer); *buffer = 0; } void create_texture(GLuint* tex) { glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, MAXX, MAXY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); } void destroy_texture(GLuint* tex) { glBindTexture(GL_TEXTURE_2D, *tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); glDeleteTextures(1, tex); } typedef union colorTag { struct { unsigned char r; unsigned char g; unsigned char b; unsigned char a; }; unsigned value; } color; void setPalette() { color palette[256]; int i; int ofs=0; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 0; palette[i+ofs].b = 16*abs(i-16); } ofs= 16; for (i = 0; i < 16; i++) { palette[i+ofs].r = 0; palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 0; } ofs= 32; for (i = 0; i < 16; i++) { palette[i+ofs].r = 0; palette[i+ofs].g = 0; palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 48; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 0; } ofs= 64; for (i = 0; i < 16; i++) { palette[i+ofs].r = 0; palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 80; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 0; palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 96; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 112; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(8-abs(i-8)); palette[i+ofs].g = 16*(8-abs(i-8)); palette[i+ofs].b = 16*(8-abs(i-8)); } CU_SAFE_CALL_NO_SYNC( hipMemcpyToSymbol( lookup, palette, 256 * sizeof(unsigned)) ); } __global__ void CoreLoop(int *p, float xld, float yld, float xru, float yru, int MAXX, int MAXY) { float re,im,rez,imz; float t1, t2, o1, o2; int k; unsigned result = 0; unsigned idx = blockIdx.x*blockDim.x + threadIdx.x; int y = idx / MAXX; int x = idx % MAXX; re = (float) xld + (xru-xld)*x/MAXX; im = (float) yld + (yru-yld)*y/MAXY; rez = 0.0f; imz = 0.0f; k = 0; while (k < ITERA) { o1 = rez * rez; o2 = imz * imz; t2 = 2 * rez * imz; t1 = o1 - o2; rez = t1 + re; imz = t2 + im; if (o1 + o2 > 4) { result = k; break; } k++; } p[y*MAXX + x] = lookup[result]; } void mandel(double xld, double yld, double xru, double yru) { int blocks = MAXX*MAXY / THREADS_PER_BLOCK; int *pixels; glBindTexture(GL_TEXTURE_2D, tex); CU_SAFE_CALL(hipGLMapBufferObject__((void**)&pixels, buffer)); hipLaunchKernelGGL(( CoreLoop), dim3(blocks), dim3(THREADS_PER_BLOCK) , 0, 0, pixels, xld, yld, xru, yru, MAXX, MAXY); //CU_SAFE_CALL(hipDeviceSynchronize()); CU_SAFE_CALL(hipGLUnmapBufferObject(buffer)); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, MAXX, MAXY, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); SDL_GL_SwapBuffers(); } int main(int argc, char *argv[]) { int st, en; switch (argc) { case 3: MAXX = atoi(argv[1]); MAXY = atoi(argv[2]); MAXX = 16*(MAXX/16); MAXY = 16*(MAXY/16); break; default: MAXX = 800; MAXY = 600; break; } printf("\nMandelbrot Zoomer by Thanassis (an experiment in CUDA).\n\n"); printf("Stats:\n\t"); printf("(CUDA calculation - use left and right mouse buttons to zoom in/out)\n\t"); if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { printf("Couldn't initialize SDL: %d\n", SDL_GetError()); exit(1); } // Clean up on exit atexit(SDL_Quit); if (!SDL_SetVideoMode( MAXX, MAXY, 0, SDL_OPENGL)) { printf("Couldn't set video mode: %d\n", SDL_GetError()); exit(1); } SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); glViewport(0, 0, MAXX, MAXY); glClearColor(0.3f, 0.3f, 0.3f, 0.5f); // This Will Clear The Background Color To Black glEnable(GL_TEXTURE_2D); glLoadIdentity(); setPalette(); create_buffer(&buffer); create_texture(&tex); CU_SAFE_CALL(hipGLRegisterBufferObject(buffer)); st = readtime(); int x; int y; double xld = -2., yld=-1.1, xru=-2+(MAXX/MAXY)*3., yru=1.1; unsigned i = 0; while(1) { mandel(xld, yld, xru, yru); int result = kbhit(&x, &y); if (result == 1) break; else if (result == 2 || result == 3) { double ratiox = double(x)/MAXX; double ratioy = double(y)/MAXY; double xrange = xru-xld; double yrange = yru-yld; double direction = result==2?1.:-1.; xld += direction*0.01*ratiox*xrange; xru -= direction*0.01*(1.-ratiox)*xrange; yld += direction*0.01*(1.-ratioy)*yrange; yru -= direction*0.01*ratioy*yrange; } i++; } en = readtime(); CU_SAFE_CALL(hipGLUnregisterBufferObject(buffer)); destroy_texture(&tex); destroy_buffer(&buffer); printf("frames/sec:%5.2f\n\n", ((float) i) / ((en - st) / 1000.0f)); return 0; }
64a675b9c62e0b26b34680939471143861e258b3.cu
#ifdef _WIN32 #include <windows.h> #endif #include <stdio.h> #include <cstring> #include <stdlib.h> #include <string.h> #define GL_GLEXT_PROTOTYPES #include <GL/gl.h> #include <SDL/SDL.h> #include <cuda.h> #include <cuda_gl_interop.h> // Number of iterations per pixel #define ITERA 120 #define THREADS_PER_BLOCK 512 int MAXX; int MAXY; GLuint buffer; GLuint tex; # define CU_SAFE_CALL_NO_SYNC( call ) { \ cudaError_t err = call; \ if( cudaSuccess != err) { \ fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \ err, __FILE__, __LINE__ ); \ exit(EXIT_FAILURE); \ } } # define CU_SAFE_CALL( call ) CU_SAFE_CALL_NO_SYNC(call); unsigned __constant__ lookup[256]; int readtime() { return SDL_GetTicks(); } int kbhit(int *xx, int *yy) { SDL_PumpEvents(); Uint8 *keystate = SDL_GetKeyState(NULL); if ( keystate[SDLK_ESCAPE] ) return 1; int x,y; Uint8 btn = SDL_GetMouseState (&x, &y); if (btn & SDL_BUTTON(SDL_BUTTON_LEFT)) { *xx = x; *yy = y; return 2; } if (btn & SDL_BUTTON(SDL_BUTTON_RIGHT)) { *xx = x; *yy = y; return 3; } return 0; } void create_buffer(GLuint* buffer) { glGenBuffersARB(1, buffer); glBindBuffer(GL_PIXEL_PACK_BUFFER, *buffer); glBufferData(GL_PIXEL_PACK_BUFFER, MAXX*MAXY*4, NULL, GL_DYNAMIC_DRAW); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); } void destroy_buffer(GLuint* buffer) { glBindBuffer(GL_TEXTURE_2D, 0); glDeleteBuffers(1, buffer); *buffer = 0; } void create_texture(GLuint* tex) { glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, MAXX, MAXY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); } void destroy_texture(GLuint* tex) { glBindTexture(GL_TEXTURE_2D, *tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); glDeleteTextures(1, tex); } typedef union colorTag { struct { unsigned char r; unsigned char g; unsigned char b; unsigned char a; }; unsigned value; } color; void setPalette() { color palette[256]; int i; int ofs=0; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 0; palette[i+ofs].b = 16*abs(i-16); } ofs= 16; for (i = 0; i < 16; i++) { palette[i+ofs].r = 0; palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 0; } ofs= 32; for (i = 0; i < 16; i++) { palette[i+ofs].r = 0; palette[i+ofs].g = 0; palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 48; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 0; } ofs= 64; for (i = 0; i < 16; i++) { palette[i+ofs].r = 0; palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 80; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 0; palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 96; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(16-abs(i-16)); palette[i+ofs].g = 16*(16-abs(i-16)); palette[i+ofs].b = 16*(16-abs(i-16)); } ofs= 112; for (i = 0; i < 16; i++) { palette[i+ofs].r = 16*(8-abs(i-8)); palette[i+ofs].g = 16*(8-abs(i-8)); palette[i+ofs].b = 16*(8-abs(i-8)); } CU_SAFE_CALL_NO_SYNC( cudaMemcpyToSymbol( lookup, palette, 256 * sizeof(unsigned)) ); } __global__ void CoreLoop(int *p, float xld, float yld, float xru, float yru, int MAXX, int MAXY) { float re,im,rez,imz; float t1, t2, o1, o2; int k; unsigned result = 0; unsigned idx = blockIdx.x*blockDim.x + threadIdx.x; int y = idx / MAXX; int x = idx % MAXX; re = (float) xld + (xru-xld)*x/MAXX; im = (float) yld + (yru-yld)*y/MAXY; rez = 0.0f; imz = 0.0f; k = 0; while (k < ITERA) { o1 = rez * rez; o2 = imz * imz; t2 = 2 * rez * imz; t1 = o1 - o2; rez = t1 + re; imz = t2 + im; if (o1 + o2 > 4) { result = k; break; } k++; } p[y*MAXX + x] = lookup[result]; } void mandel(double xld, double yld, double xru, double yru) { int blocks = MAXX*MAXY / THREADS_PER_BLOCK; int *pixels; glBindTexture(GL_TEXTURE_2D, tex); CU_SAFE_CALL(cudaGLMapBufferObject((void**)&pixels, buffer)); CoreLoop<<< blocks, THREADS_PER_BLOCK >>>(pixels, xld, yld, xru, yru, MAXX, MAXY); //CU_SAFE_CALL(cudaThreadSynchronize()); CU_SAFE_CALL(cudaGLUnmapBufferObject(buffer)); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, MAXX, MAXY, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); SDL_GL_SwapBuffers(); } int main(int argc, char *argv[]) { int st, en; switch (argc) { case 3: MAXX = atoi(argv[1]); MAXY = atoi(argv[2]); MAXX = 16*(MAXX/16); MAXY = 16*(MAXY/16); break; default: MAXX = 800; MAXY = 600; break; } printf("\nMandelbrot Zoomer by Thanassis (an experiment in CUDA).\n\n"); printf("Stats:\n\t"); printf("(CUDA calculation - use left and right mouse buttons to zoom in/out)\n\t"); if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { printf("Couldn't initialize SDL: %d\n", SDL_GetError()); exit(1); } // Clean up on exit atexit(SDL_Quit); if (!SDL_SetVideoMode( MAXX, MAXY, 0, SDL_OPENGL)) { printf("Couldn't set video mode: %d\n", SDL_GetError()); exit(1); } SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); glViewport(0, 0, MAXX, MAXY); glClearColor(0.3f, 0.3f, 0.3f, 0.5f); // This Will Clear The Background Color To Black glEnable(GL_TEXTURE_2D); glLoadIdentity(); setPalette(); create_buffer(&buffer); create_texture(&tex); CU_SAFE_CALL(cudaGLRegisterBufferObject(buffer)); st = readtime(); int x; int y; double xld = -2., yld=-1.1, xru=-2+(MAXX/MAXY)*3., yru=1.1; unsigned i = 0; while(1) { mandel(xld, yld, xru, yru); int result = kbhit(&x, &y); if (result == 1) break; else if (result == 2 || result == 3) { double ratiox = double(x)/MAXX; double ratioy = double(y)/MAXY; double xrange = xru-xld; double yrange = yru-yld; double direction = result==2?1.:-1.; xld += direction*0.01*ratiox*xrange; xru -= direction*0.01*(1.-ratiox)*xrange; yld += direction*0.01*(1.-ratioy)*yrange; yru -= direction*0.01*ratioy*yrange; } i++; } en = readtime(); CU_SAFE_CALL(cudaGLUnregisterBufferObject(buffer)); destroy_texture(&tex); destroy_buffer(&buffer); printf("frames/sec:%5.2f\n\n", ((float) i) / ((en - st) / 1000.0f)); return 0; }
f825cc129de8866a7994f36cbc20bc5e4dce6648.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition (HOOMD-blue) Open Source Software License Copyright 2009-2016 The Regents of the University of Michigan All rights reserved. HOOMD-blue may contain modifications ("Contributions") provided, and to which copyright is held, by various Contributors who have granted The Regents of the University of Michigan the right to modify and/or distribute such Contributions. You may redistribute, use, and create derivate works of HOOMD-blue, in source and binary forms, provided you abide by the following conditions: * Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer both in the code and prominently in any materials provided with the distribution. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution. * All publications and presentations based on HOOMD-blue, including any reports or published results obtained, in whole or in part, with HOOMD-blue, will acknowledge its use according to the terms posted at the time of submission on: http://codeblue.umich.edu/hoomd-blue/citations.html * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website: http://codeblue.umich.edu/hoomd-blue/ * Apart from the above required attributions, neither the name of the copyright holder nor the names of HOOMD-blue's contributors may be used to endorse or promote products derived from this software without specific prior written permission. Disclaimer THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // Maintainer: joaander /*! \file NeighborListGPU.cu \brief Defines GPU kernel code for neighbor list processing on the GPU */ #include "NeighborListGPU_hip.cuh" #include <thrust/scan.h> #include <thrust/device_ptr.h> /*! \param d_result Device pointer to a single uint. Will be set to 1 if an update is needed \param d_last_pos Particle positions at the time the nlist was last updated \param d_pos Current particle positions \param N Number of particles \param box Box dimensions \param d_rcut_max The maximum rcut(i,j) that any particle of type i participates in \param r_buff The buffer size that particles can move in \param ntypes The number of particle types \param lambda_min Minimum contraction of deformation tensor \param lambda Diagonal deformation tensor (for orthorhombic boundaries) \param checkn gpu_nlist_needs_update_check_new_kernel() executes one thread per particle. Every particle's current position is compared to its last position. If the particle has moved a distance more than the buffer width, then *d_result is set to \a checkn. */ __global__ void gpu_nlist_needs_update_check_new_kernel(unsigned int *d_result, const Scalar4 *d_last_pos, const Scalar4 *d_pos, const unsigned int N, const BoxDim box, const Scalar *d_rcut_max, const Scalar r_buff, const unsigned int ntypes, const Scalar lambda_min, const Scalar3 lambda, const unsigned int checkn) { // cache delta max into shared memory // shared data for per type pair parameters extern __shared__ unsigned char s_data[]; // pointer for the r_listsq data Scalar *s_maxshiftsq = (Scalar *)(&s_data[0]); // load in the per type pair r_list for (unsigned int cur_offset = 0; cur_offset < ntypes; cur_offset += blockDim.x) { if (cur_offset + threadIdx.x < ntypes) { const Scalar rmin = d_rcut_max[cur_offset + threadIdx.x]; const Scalar rmax = rmin + r_buff; const Scalar delta_max = (rmax*lambda_min - rmin)/Scalar(2.0); s_maxshiftsq[cur_offset + threadIdx.x] = (delta_max > 0) ? delta_max*delta_max : 0.0f; } } __syncthreads(); // each thread will compare vs it's old position to see if the list needs updating int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < N) { Scalar4 cur_postype = d_pos[idx]; Scalar3 cur_pos = make_scalar3(cur_postype.x, cur_postype.y, cur_postype.z); const unsigned int cur_type = __scalar_as_int(cur_postype.w); Scalar4 last_postype = d_last_pos[idx]; Scalar3 last_pos = make_scalar3(last_postype.x, last_postype.y, last_postype.z); Scalar3 dx = cur_pos - lambda*last_pos; dx = box.minImage(dx); if (dot(dx, dx) >= s_maxshiftsq[cur_type]) atomicMax(d_result, checkn); } } hipError_t gpu_nlist_needs_update_check_new(unsigned int *d_result, const Scalar4 *d_last_pos, const Scalar4 *d_pos, const unsigned int N, const BoxDim& box, const Scalar *d_rcut_max, const Scalar r_buff, const unsigned int ntypes, const Scalar lambda_min, const Scalar3 lambda, const unsigned int checkn) { const unsigned int shared_bytes = sizeof(Scalar) * ntypes; unsigned int block_size = 128; int n_blocks = N/block_size+1; hipLaunchKernelGGL(( gpu_nlist_needs_update_check_new_kernel), dim3(n_blocks), dim3(block_size), shared_bytes, 0, d_result, d_last_pos, d_pos, N, box, d_rcut_max, r_buff, ntypes, lambda_min, lambda, checkn); return hipSuccess; } //! Number of elements of the exclusion list to process in each batch const unsigned int FILTER_BATCH_SIZE = 4; /*! \param d_n_neigh Number of neighbors for each particle (read/write) \param d_nlist Neighbor list for each particle (read/write) \param nli Indexer for indexing into d_nlist \param d_n_ex Number of exclusions for each particle \param d_ex_list List of exclusions for each particle \param exli Indexer for indexing into d_ex_list \param N Number of particles \param ex_start Start filtering the nlist from exclusion number \a ex_start gpu_nlist_filter_kernel() processes the neighbor list \a d_nlist and removes any entries that are excluded. To allow for an arbitrary large number of exclusions, these are processed in batch sizes of FILTER_BATCH_SIZE. The kernel must be called multiple times in order to fully remove all exclusions from the nlist. \note The driver gpu_nlist_filter properly makes as many calls as are necessary, it only needs to be called once. \b Implementation One thread is run for each particle. Exclusions \a ex_start, \a ex_start + 1, ... are loaded in for that particle (or the thread returns if there are no exlusions past that point). The thread then loops over the neighbor list, comparing each entry to the list of exclusions. If the entry is not excluded, it is written back out. \a d_n_neigh is updated to reflect the current number of particles in the list at the end of the kernel call. */ __global__ void gpu_nlist_filter_kernel(unsigned int *d_n_neigh, unsigned int *d_nlist, const unsigned int *d_head_list, const unsigned int *d_n_ex, const unsigned int *d_ex_list, const Index2D exli, const unsigned int N, const unsigned int ex_start) { // compute the particle index this thread operates on const unsigned int idx = blockDim.x * blockIdx.x + threadIdx.x; // quit now if this thread is processing past the end of the particle list if (idx >= N) return; const unsigned int n_neigh = d_n_neigh[idx]; const unsigned int n_ex = d_n_ex[idx]; unsigned int new_n_neigh = 0; // quit now if the ex_start flag is past the end of n_ex if (ex_start >= n_ex) return; // count the number of exclusions to process in this thread const unsigned int n_ex_process = n_ex - ex_start; // load the exclusion list into "local" memory - fully unrolled loops should dump this into registers unsigned int l_ex_list[FILTER_BATCH_SIZE]; #pragma unroll for (unsigned int cur_ex_idx = 0; cur_ex_idx < FILTER_BATCH_SIZE; cur_ex_idx++) { if (cur_ex_idx < n_ex_process) l_ex_list[cur_ex_idx] = d_ex_list[exli(idx, cur_ex_idx + ex_start)]; else l_ex_list[cur_ex_idx] = 0xffffffff; } // loop over the list, regenerating it as we go const unsigned int my_head = d_head_list[idx]; for (unsigned int cur_neigh_idx = 0; cur_neigh_idx < n_neigh; cur_neigh_idx++) { unsigned int cur_neigh = d_nlist[my_head + cur_neigh_idx]; // test if excluded bool excluded = false; #pragma unroll for (unsigned int cur_ex_idx = 0; cur_ex_idx < FILTER_BATCH_SIZE; cur_ex_idx++) { if (cur_neigh == l_ex_list[cur_ex_idx]) excluded = true; } // add it back to the list if it is not excluded if (!excluded) { if (new_n_neigh != cur_neigh_idx) d_nlist[my_head + new_n_neigh] = cur_neigh; new_n_neigh++; } } // update the number of neighbors d_n_neigh[idx] = new_n_neigh; } hipError_t gpu_nlist_filter(unsigned int *d_n_neigh, unsigned int *d_nlist, const unsigned int *d_head_list, const unsigned int *d_n_ex, const unsigned int *d_ex_list, const Index2D& exli, const unsigned int N, const unsigned int block_size) { static unsigned int max_block_size = UINT_MAX; if (max_block_size == UINT_MAX) { hipFuncAttributes attr; hipFuncGetAttributes(&attr, (const void *)gpu_nlist_filter_kernel); max_block_size = attr.maxThreadsPerBlock; } unsigned int run_block_size = min(block_size, max_block_size); // determine parameters for kernel launch int n_blocks = N/run_block_size + 1; // split the processing of the full exclusion list up into a number of batches unsigned int n_batches = (unsigned int)ceil(double(exli.getH())/double(FILTER_BATCH_SIZE)); unsigned int ex_start = 0; for (unsigned int batch = 0; batch < n_batches; batch++) { hipLaunchKernelGGL(( gpu_nlist_filter_kernel), dim3(n_blocks), dim3(run_block_size), 0, 0, d_n_neigh, d_nlist, d_head_list, d_n_ex, d_ex_list, exli, N, ex_start); ex_start += FILTER_BATCH_SIZE; } return hipSuccess; } //! GPU kernel to update the exclusions list __global__ void gpu_update_exclusion_list_kernel(const unsigned int *tags, const unsigned int *rtags, const unsigned int *n_ex_tag, const unsigned int *ex_list_tag, const Index2D ex_list_tag_indexer, unsigned int *n_ex_idx, unsigned int *ex_list_idx, const Index2D ex_list_indexer, const unsigned int N) { unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx >= N) return; unsigned int tag = tags[idx]; unsigned int n = n_ex_tag[tag]; // copy over number of exclusions n_ex_idx[idx] = n; for (unsigned int offset = 0; offset < n; offset++) { unsigned int ex_tag = ex_list_tag[ex_list_tag_indexer(tag, offset)]; unsigned int ex_idx = rtags[ex_tag]; ex_list_idx[ex_list_indexer(idx, offset)] = ex_idx; } } //! GPU function to update the exclusion list on the device /*! \param d_tag Array of particle tags \param d_rtag Array of reverse-lookup tag->idx \param d_n_ex_tag List of number of exclusions per tag \param d_ex_list_tag 2D Exclusion list per tag \param ex_list_tag_indexer Indexer for per-tag exclusion list \param d_n_ex_idx List of number of exclusions per idx \param d_ex_list_idx Exclusion list per idx \param ex_list_indexer Indexer for per-idx exclusion list \param N number of particles */ hipError_t gpu_update_exclusion_list(const unsigned int *d_tag, const unsigned int *d_rtag, const unsigned int *d_n_ex_tag, const unsigned int *d_ex_list_tag, const Index2D& ex_list_tag_indexer, unsigned int *d_n_ex_idx, unsigned int *d_ex_list_idx, const Index2D& ex_list_indexer, const unsigned int N) { unsigned int block_size = 512; hipLaunchKernelGGL(( gpu_update_exclusion_list_kernel), dim3(N/block_size + 1), dim3(block_size), 0, 0, d_tag, d_rtag, d_n_ex_tag, d_ex_list_tag, ex_list_tag_indexer, d_n_ex_idx, d_ex_list_idx, ex_list_indexer, N); return hipSuccess; } //! GPU kernel to do a preliminary sizing on particles /*! * \param d_head_list The head list of indexes to overwrite * \param d_req_size_nlist Flag for the required size of the neighbor list to overwrite * \param d_Nmax The number of neighbors to size per particle type * \param d_pos Particle positions and types * \param N the number of particles on this rank * \param ntypes the number of types in the system * * This kernel initializes the head list with the number of neighbors that each type expects from d_Nmax. A prefix sum * is then performed in gpu_nlist_build_head_list() to accumulate starting indices. */ __global__ void gpu_nlist_init_head_list_kernel(unsigned int *d_head_list, unsigned int *d_req_size_nlist, const unsigned int *d_Nmax, const Scalar4 *d_pos, const unsigned int N, const unsigned int ntypes) { // cache the d_Nmax into shared memory for faster reads extern __shared__ unsigned char sh[]; unsigned int *s_Nmax = (unsigned int *)(&sh[0]); for (unsigned int cur_offset = 0; cur_offset < ntypes; cur_offset += blockDim.x) { if (cur_offset + threadIdx.x < ntypes) { s_Nmax[cur_offset + threadIdx.x] = d_Nmax[cur_offset + threadIdx.x]; } } __syncthreads(); // particle index const unsigned int idx = blockDim.x * blockIdx.x + threadIdx.x; // one thread per particle if (idx >= N) return; const Scalar4 postype_i = d_pos[idx]; const unsigned int type_i = __scalar_as_int(postype_i.w); const unsigned int Nmax_i = d_Nmax[type_i]; d_head_list[idx] = Nmax_i; // last thread presets its number of particles in the memory req as well if (idx == (N-1)) { *d_req_size_nlist = Nmax_i; } } /*! * \param d_req_size_nlist Flag for the total size of the neighbor list * \param d_head_list The complete particle head list * \param N the number of particles on this rank * * A single thread on the device is needed to complete the exclusive scan and find the size of the neighbor list. * Because gpu_nlist_init_head_list_kernel() already set the number of neighbors for the last particle in * d_req_size_nlist, the head index of the last particle is added to this number to get the total size. */ __global__ void gpu_nlist_get_nlist_size_kernel(unsigned int *d_req_size_nlist, const unsigned int *d_head_list, const unsigned int N) { *d_req_size_nlist += d_head_list[N-1]; } /*! * \param d_head_list The head list of indexes to compute for reading the neighbor list * \param d_req_size_nlist Flag for the total size of the neighbor list * \param d_Nmax The number of neighbors to size per particle type * \param d_pos Particle positions and types * \param N the number of particles on this rank * \param ntypes the number of types in the system * \param block_size Number of threads per block for gpu_nlist_init_head_list_kernel() * * \return hipSuccess on completion * * \b Implementation * \a d_head_list is filled with the number of neighbors per particle. An exclusive prefix sum is * performed in place on \a d_head_list using the thrust libraries and a single thread is used to perform compute the total * size of the neighbor list while still on device. */ hipError_t gpu_nlist_build_head_list(unsigned int *d_head_list, unsigned int *d_req_size_nlist, const unsigned int *d_Nmax, const Scalar4 *d_pos, const unsigned int N, const unsigned int ntypes, const unsigned int block_size) { static unsigned int max_block_size = UINT_MAX; if (max_block_size == UINT_MAX) { hipFuncAttributes attr; hipFuncGetAttributes(&attr, (const void *)gpu_nlist_init_head_list_kernel); max_block_size = attr.maxThreadsPerBlock; } unsigned int run_block_size = min(block_size, max_block_size); unsigned int shared_bytes = ntypes*sizeof(unsigned int); // initialize each particle with its number of neighbors hipLaunchKernelGGL(( gpu_nlist_init_head_list_kernel), dim3(N/run_block_size + 1), dim3(run_block_size), shared_bytes, 0, d_head_list, d_req_size_nlist, d_Nmax, d_pos, N, ntypes); thrust::device_ptr<unsigned int> t_head_list = thrust::device_pointer_cast(d_head_list); thrust::exclusive_scan(t_head_list, t_head_list+N, t_head_list); hipLaunchKernelGGL(( gpu_nlist_get_nlist_size_kernel), dim3(1),dim3(1), 0, 0, d_req_size_nlist, d_head_list, N); return hipSuccess; }
f825cc129de8866a7994f36cbc20bc5e4dce6648.cu
/* Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition (HOOMD-blue) Open Source Software License Copyright 2009-2016 The Regents of the University of Michigan All rights reserved. HOOMD-blue may contain modifications ("Contributions") provided, and to which copyright is held, by various Contributors who have granted The Regents of the University of Michigan the right to modify and/or distribute such Contributions. You may redistribute, use, and create derivate works of HOOMD-blue, in source and binary forms, provided you abide by the following conditions: * Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer both in the code and prominently in any materials provided with the distribution. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution. * All publications and presentations based on HOOMD-blue, including any reports or published results obtained, in whole or in part, with HOOMD-blue, will acknowledge its use according to the terms posted at the time of submission on: http://codeblue.umich.edu/hoomd-blue/citations.html * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website: http://codeblue.umich.edu/hoomd-blue/ * Apart from the above required attributions, neither the name of the copyright holder nor the names of HOOMD-blue's contributors may be used to endorse or promote products derived from this software without specific prior written permission. Disclaimer THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // Maintainer: joaander /*! \file NeighborListGPU.cu \brief Defines GPU kernel code for neighbor list processing on the GPU */ #include "NeighborListGPU.cuh" #include <thrust/scan.h> #include <thrust/device_ptr.h> /*! \param d_result Device pointer to a single uint. Will be set to 1 if an update is needed \param d_last_pos Particle positions at the time the nlist was last updated \param d_pos Current particle positions \param N Number of particles \param box Box dimensions \param d_rcut_max The maximum rcut(i,j) that any particle of type i participates in \param r_buff The buffer size that particles can move in \param ntypes The number of particle types \param lambda_min Minimum contraction of deformation tensor \param lambda Diagonal deformation tensor (for orthorhombic boundaries) \param checkn gpu_nlist_needs_update_check_new_kernel() executes one thread per particle. Every particle's current position is compared to its last position. If the particle has moved a distance more than the buffer width, then *d_result is set to \a checkn. */ __global__ void gpu_nlist_needs_update_check_new_kernel(unsigned int *d_result, const Scalar4 *d_last_pos, const Scalar4 *d_pos, const unsigned int N, const BoxDim box, const Scalar *d_rcut_max, const Scalar r_buff, const unsigned int ntypes, const Scalar lambda_min, const Scalar3 lambda, const unsigned int checkn) { // cache delta max into shared memory // shared data for per type pair parameters extern __shared__ unsigned char s_data[]; // pointer for the r_listsq data Scalar *s_maxshiftsq = (Scalar *)(&s_data[0]); // load in the per type pair r_list for (unsigned int cur_offset = 0; cur_offset < ntypes; cur_offset += blockDim.x) { if (cur_offset + threadIdx.x < ntypes) { const Scalar rmin = d_rcut_max[cur_offset + threadIdx.x]; const Scalar rmax = rmin + r_buff; const Scalar delta_max = (rmax*lambda_min - rmin)/Scalar(2.0); s_maxshiftsq[cur_offset + threadIdx.x] = (delta_max > 0) ? delta_max*delta_max : 0.0f; } } __syncthreads(); // each thread will compare vs it's old position to see if the list needs updating int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < N) { Scalar4 cur_postype = d_pos[idx]; Scalar3 cur_pos = make_scalar3(cur_postype.x, cur_postype.y, cur_postype.z); const unsigned int cur_type = __scalar_as_int(cur_postype.w); Scalar4 last_postype = d_last_pos[idx]; Scalar3 last_pos = make_scalar3(last_postype.x, last_postype.y, last_postype.z); Scalar3 dx = cur_pos - lambda*last_pos; dx = box.minImage(dx); if (dot(dx, dx) >= s_maxshiftsq[cur_type]) atomicMax(d_result, checkn); } } cudaError_t gpu_nlist_needs_update_check_new(unsigned int *d_result, const Scalar4 *d_last_pos, const Scalar4 *d_pos, const unsigned int N, const BoxDim& box, const Scalar *d_rcut_max, const Scalar r_buff, const unsigned int ntypes, const Scalar lambda_min, const Scalar3 lambda, const unsigned int checkn) { const unsigned int shared_bytes = sizeof(Scalar) * ntypes; unsigned int block_size = 128; int n_blocks = N/block_size+1; gpu_nlist_needs_update_check_new_kernel<<<n_blocks, block_size, shared_bytes>>>(d_result, d_last_pos, d_pos, N, box, d_rcut_max, r_buff, ntypes, lambda_min, lambda, checkn); return cudaSuccess; } //! Number of elements of the exclusion list to process in each batch const unsigned int FILTER_BATCH_SIZE = 4; /*! \param d_n_neigh Number of neighbors for each particle (read/write) \param d_nlist Neighbor list for each particle (read/write) \param nli Indexer for indexing into d_nlist \param d_n_ex Number of exclusions for each particle \param d_ex_list List of exclusions for each particle \param exli Indexer for indexing into d_ex_list \param N Number of particles \param ex_start Start filtering the nlist from exclusion number \a ex_start gpu_nlist_filter_kernel() processes the neighbor list \a d_nlist and removes any entries that are excluded. To allow for an arbitrary large number of exclusions, these are processed in batch sizes of FILTER_BATCH_SIZE. The kernel must be called multiple times in order to fully remove all exclusions from the nlist. \note The driver gpu_nlist_filter properly makes as many calls as are necessary, it only needs to be called once. \b Implementation One thread is run for each particle. Exclusions \a ex_start, \a ex_start + 1, ... are loaded in for that particle (or the thread returns if there are no exlusions past that point). The thread then loops over the neighbor list, comparing each entry to the list of exclusions. If the entry is not excluded, it is written back out. \a d_n_neigh is updated to reflect the current number of particles in the list at the end of the kernel call. */ __global__ void gpu_nlist_filter_kernel(unsigned int *d_n_neigh, unsigned int *d_nlist, const unsigned int *d_head_list, const unsigned int *d_n_ex, const unsigned int *d_ex_list, const Index2D exli, const unsigned int N, const unsigned int ex_start) { // compute the particle index this thread operates on const unsigned int idx = blockDim.x * blockIdx.x + threadIdx.x; // quit now if this thread is processing past the end of the particle list if (idx >= N) return; const unsigned int n_neigh = d_n_neigh[idx]; const unsigned int n_ex = d_n_ex[idx]; unsigned int new_n_neigh = 0; // quit now if the ex_start flag is past the end of n_ex if (ex_start >= n_ex) return; // count the number of exclusions to process in this thread const unsigned int n_ex_process = n_ex - ex_start; // load the exclusion list into "local" memory - fully unrolled loops should dump this into registers unsigned int l_ex_list[FILTER_BATCH_SIZE]; #pragma unroll for (unsigned int cur_ex_idx = 0; cur_ex_idx < FILTER_BATCH_SIZE; cur_ex_idx++) { if (cur_ex_idx < n_ex_process) l_ex_list[cur_ex_idx] = d_ex_list[exli(idx, cur_ex_idx + ex_start)]; else l_ex_list[cur_ex_idx] = 0xffffffff; } // loop over the list, regenerating it as we go const unsigned int my_head = d_head_list[idx]; for (unsigned int cur_neigh_idx = 0; cur_neigh_idx < n_neigh; cur_neigh_idx++) { unsigned int cur_neigh = d_nlist[my_head + cur_neigh_idx]; // test if excluded bool excluded = false; #pragma unroll for (unsigned int cur_ex_idx = 0; cur_ex_idx < FILTER_BATCH_SIZE; cur_ex_idx++) { if (cur_neigh == l_ex_list[cur_ex_idx]) excluded = true; } // add it back to the list if it is not excluded if (!excluded) { if (new_n_neigh != cur_neigh_idx) d_nlist[my_head + new_n_neigh] = cur_neigh; new_n_neigh++; } } // update the number of neighbors d_n_neigh[idx] = new_n_neigh; } cudaError_t gpu_nlist_filter(unsigned int *d_n_neigh, unsigned int *d_nlist, const unsigned int *d_head_list, const unsigned int *d_n_ex, const unsigned int *d_ex_list, const Index2D& exli, const unsigned int N, const unsigned int block_size) { static unsigned int max_block_size = UINT_MAX; if (max_block_size == UINT_MAX) { cudaFuncAttributes attr; cudaFuncGetAttributes(&attr, (const void *)gpu_nlist_filter_kernel); max_block_size = attr.maxThreadsPerBlock; } unsigned int run_block_size = min(block_size, max_block_size); // determine parameters for kernel launch int n_blocks = N/run_block_size + 1; // split the processing of the full exclusion list up into a number of batches unsigned int n_batches = (unsigned int)ceil(double(exli.getH())/double(FILTER_BATCH_SIZE)); unsigned int ex_start = 0; for (unsigned int batch = 0; batch < n_batches; batch++) { gpu_nlist_filter_kernel<<<n_blocks, run_block_size>>>(d_n_neigh, d_nlist, d_head_list, d_n_ex, d_ex_list, exli, N, ex_start); ex_start += FILTER_BATCH_SIZE; } return cudaSuccess; } //! GPU kernel to update the exclusions list __global__ void gpu_update_exclusion_list_kernel(const unsigned int *tags, const unsigned int *rtags, const unsigned int *n_ex_tag, const unsigned int *ex_list_tag, const Index2D ex_list_tag_indexer, unsigned int *n_ex_idx, unsigned int *ex_list_idx, const Index2D ex_list_indexer, const unsigned int N) { unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx >= N) return; unsigned int tag = tags[idx]; unsigned int n = n_ex_tag[tag]; // copy over number of exclusions n_ex_idx[idx] = n; for (unsigned int offset = 0; offset < n; offset++) { unsigned int ex_tag = ex_list_tag[ex_list_tag_indexer(tag, offset)]; unsigned int ex_idx = rtags[ex_tag]; ex_list_idx[ex_list_indexer(idx, offset)] = ex_idx; } } //! GPU function to update the exclusion list on the device /*! \param d_tag Array of particle tags \param d_rtag Array of reverse-lookup tag->idx \param d_n_ex_tag List of number of exclusions per tag \param d_ex_list_tag 2D Exclusion list per tag \param ex_list_tag_indexer Indexer for per-tag exclusion list \param d_n_ex_idx List of number of exclusions per idx \param d_ex_list_idx Exclusion list per idx \param ex_list_indexer Indexer for per-idx exclusion list \param N number of particles */ cudaError_t gpu_update_exclusion_list(const unsigned int *d_tag, const unsigned int *d_rtag, const unsigned int *d_n_ex_tag, const unsigned int *d_ex_list_tag, const Index2D& ex_list_tag_indexer, unsigned int *d_n_ex_idx, unsigned int *d_ex_list_idx, const Index2D& ex_list_indexer, const unsigned int N) { unsigned int block_size = 512; gpu_update_exclusion_list_kernel<<<N/block_size + 1, block_size>>>(d_tag, d_rtag, d_n_ex_tag, d_ex_list_tag, ex_list_tag_indexer, d_n_ex_idx, d_ex_list_idx, ex_list_indexer, N); return cudaSuccess; } //! GPU kernel to do a preliminary sizing on particles /*! * \param d_head_list The head list of indexes to overwrite * \param d_req_size_nlist Flag for the required size of the neighbor list to overwrite * \param d_Nmax The number of neighbors to size per particle type * \param d_pos Particle positions and types * \param N the number of particles on this rank * \param ntypes the number of types in the system * * This kernel initializes the head list with the number of neighbors that each type expects from d_Nmax. A prefix sum * is then performed in gpu_nlist_build_head_list() to accumulate starting indices. */ __global__ void gpu_nlist_init_head_list_kernel(unsigned int *d_head_list, unsigned int *d_req_size_nlist, const unsigned int *d_Nmax, const Scalar4 *d_pos, const unsigned int N, const unsigned int ntypes) { // cache the d_Nmax into shared memory for faster reads extern __shared__ unsigned char sh[]; unsigned int *s_Nmax = (unsigned int *)(&sh[0]); for (unsigned int cur_offset = 0; cur_offset < ntypes; cur_offset += blockDim.x) { if (cur_offset + threadIdx.x < ntypes) { s_Nmax[cur_offset + threadIdx.x] = d_Nmax[cur_offset + threadIdx.x]; } } __syncthreads(); // particle index const unsigned int idx = blockDim.x * blockIdx.x + threadIdx.x; // one thread per particle if (idx >= N) return; const Scalar4 postype_i = d_pos[idx]; const unsigned int type_i = __scalar_as_int(postype_i.w); const unsigned int Nmax_i = d_Nmax[type_i]; d_head_list[idx] = Nmax_i; // last thread presets its number of particles in the memory req as well if (idx == (N-1)) { *d_req_size_nlist = Nmax_i; } } /*! * \param d_req_size_nlist Flag for the total size of the neighbor list * \param d_head_list The complete particle head list * \param N the number of particles on this rank * * A single thread on the device is needed to complete the exclusive scan and find the size of the neighbor list. * Because gpu_nlist_init_head_list_kernel() already set the number of neighbors for the last particle in * d_req_size_nlist, the head index of the last particle is added to this number to get the total size. */ __global__ void gpu_nlist_get_nlist_size_kernel(unsigned int *d_req_size_nlist, const unsigned int *d_head_list, const unsigned int N) { *d_req_size_nlist += d_head_list[N-1]; } /*! * \param d_head_list The head list of indexes to compute for reading the neighbor list * \param d_req_size_nlist Flag for the total size of the neighbor list * \param d_Nmax The number of neighbors to size per particle type * \param d_pos Particle positions and types * \param N the number of particles on this rank * \param ntypes the number of types in the system * \param block_size Number of threads per block for gpu_nlist_init_head_list_kernel() * * \return cudaSuccess on completion * * \b Implementation * \a d_head_list is filled with the number of neighbors per particle. An exclusive prefix sum is * performed in place on \a d_head_list using the thrust libraries and a single thread is used to perform compute the total * size of the neighbor list while still on device. */ cudaError_t gpu_nlist_build_head_list(unsigned int *d_head_list, unsigned int *d_req_size_nlist, const unsigned int *d_Nmax, const Scalar4 *d_pos, const unsigned int N, const unsigned int ntypes, const unsigned int block_size) { static unsigned int max_block_size = UINT_MAX; if (max_block_size == UINT_MAX) { cudaFuncAttributes attr; cudaFuncGetAttributes(&attr, (const void *)gpu_nlist_init_head_list_kernel); max_block_size = attr.maxThreadsPerBlock; } unsigned int run_block_size = min(block_size, max_block_size); unsigned int shared_bytes = ntypes*sizeof(unsigned int); // initialize each particle with its number of neighbors gpu_nlist_init_head_list_kernel<<<N/run_block_size + 1, run_block_size, shared_bytes>>>(d_head_list, d_req_size_nlist, d_Nmax, d_pos, N, ntypes); thrust::device_ptr<unsigned int> t_head_list = thrust::device_pointer_cast(d_head_list); thrust::exclusive_scan(t_head_list, t_head_list+N, t_head_list); gpu_nlist_get_nlist_size_kernel<<<1,1>>>(d_req_size_nlist, d_head_list, N); return cudaSuccess; }
c051ab1484207ab8db94d81cb9e68be1e1800994.hip
// !!! This is a file automatically generated by hipify!!! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <chrono> #include <hip/hip_runtime.h> // scan over N elements #define N 512 template<typename T> void verify(const T* cpu_out, const T* gpu_out, int n) { int error = memcmp(cpu_out, gpu_out, n * sizeof(T)); printf("%s\n", error ? "FAIL" : "PASS"); } // bank conflict aware optimization #define LOG_MEM_BANKS 5 #define OFFSET(n) ((n) >> LOG_MEM_BANKS) template<typename T> __global__ void prescan_bcao ( T *__restrict__ g_odata, const T *__restrict__ g_idata, const int n) { __shared__ T temp[2*N]; int thid = threadIdx.x; int a = thid; int b = a + (n/2); int oa = OFFSET(a); int ob = OFFSET(b); temp[a + oa] = g_idata[a]; temp[b + ob] = g_idata[b]; int offset = 1; for (int d = n >> 1; d > 0; d >>= 1) { __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; ai += OFFSET(ai); bi += OFFSET(bi); temp[bi] += temp[ai]; } offset *= 2; } if (thid == 0) temp[n-1+OFFSET(n-1)] = 0; // clear the last elem for (int d = 1; d < n; d *= 2) // traverse down { offset >>= 1; __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; ai += OFFSET(ai); bi += OFFSET(bi); T t = temp[ai]; temp[ai] = temp[bi]; temp[bi] += t; } } __syncthreads(); // required g_odata[a] = temp[a + oa]; g_odata[b] = temp[b + ob]; } template<typename T> __global__ void prescan( T *__restrict__ g_odata, const T *__restrict__ g_idata, const int n) { __shared__ T temp[N]; int thid = threadIdx.x; int offset = 1; temp[2*thid] = g_idata[2*thid]; temp[2*thid+1] = g_idata[2*thid+1]; for (int d = n >> 1; d > 0; d >>= 1) { __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; temp[bi] += temp[ai]; } offset *= 2; } if (thid == 0) temp[n-1] = 0; // clear the last elem for (int d = 1; d < n; d *= 2) // traverse down { offset >>= 1; __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; float t = temp[ai]; temp[ai] = temp[bi]; temp[bi] += t; } } g_odata[2*thid] = temp[2*thid]; g_odata[2*thid+1] = temp[2*thid+1]; } template <typename T> void runTest (const int repeat, bool timing = false) { T in[N]; T cpu_out[N]; T gpu_out[N]; int n = N; for (int i = 0; i < n; i++) in[i] = (i % 5)+1; cpu_out[0] = 0; for (int i = 1; i < n; i++) cpu_out[i] = cpu_out[i-1] + in[i-1]; T *d_in, *d_out; hipMalloc((void**)&d_in, n*sizeof(T)); hipMemcpy(d_in, in, n*sizeof(T), hipMemcpyHostToDevice); hipMalloc((void**)&d_out, n*sizeof(T)); dim3 grids (1); dim3 blocks (n/2); hipDeviceSynchronize(); auto start = std::chrono::steady_clock::now(); for (int i = 0; i < repeat; i++) { hipLaunchKernelGGL(( prescan), dim3(grids), dim3(blocks), 0, 0, d_out, d_in, n); } hipDeviceSynchronize(); auto end = std::chrono::steady_clock::now(); auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); if (timing) { printf("Element size in bytes is %zu. Average execution time of block scan (w/ bank conflicts): %f (us)\n", sizeof(T), (time * 1e-3f) / repeat); } hipMemcpy(gpu_out, d_out, n*sizeof(T), hipMemcpyDeviceToHost); if (!timing) verify(cpu_out, gpu_out, n); // bcao start = std::chrono::steady_clock::now(); for (int i = 0; i < repeat; i++) { hipLaunchKernelGGL(( prescan_bcao), dim3(grids), dim3(blocks), 0, 0, d_out, d_in, n); } hipDeviceSynchronize(); end = std::chrono::steady_clock::now(); auto bcao_time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); if (timing) { printf("Element size in bytes is %zu. Average execution time of block scan (w/o bank conflicts): %f (us). ", sizeof(T), (bcao_time * 1e-3f) / repeat); printf("Reduce the time by %.1f%%\n", (time - bcao_time) * 1.0 / time * 100); } hipMemcpy(gpu_out, d_out, n*sizeof(T), hipMemcpyDeviceToHost); if (!timing) verify(cpu_out, gpu_out, n); hipFree(d_in); hipFree(d_out); } int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: %s <repeat>\n", argv[0]); return 1; } const int repeat = atoi(argv[1]); for (int i = 0; i < 2; i++) { bool timing = i > 0; runTest<char>(repeat, timing); runTest<short>(repeat, timing); runTest<int>(repeat, timing); runTest<long>(repeat, timing); } return 0; }
c051ab1484207ab8db94d81cb9e68be1e1800994.cu
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <chrono> #include <cuda.h> // scan over N elements #define N 512 template<typename T> void verify(const T* cpu_out, const T* gpu_out, int n) { int error = memcmp(cpu_out, gpu_out, n * sizeof(T)); printf("%s\n", error ? "FAIL" : "PASS"); } // bank conflict aware optimization #define LOG_MEM_BANKS 5 #define OFFSET(n) ((n) >> LOG_MEM_BANKS) template<typename T> __global__ void prescan_bcao ( T *__restrict__ g_odata, const T *__restrict__ g_idata, const int n) { __shared__ T temp[2*N]; int thid = threadIdx.x; int a = thid; int b = a + (n/2); int oa = OFFSET(a); int ob = OFFSET(b); temp[a + oa] = g_idata[a]; temp[b + ob] = g_idata[b]; int offset = 1; for (int d = n >> 1; d > 0; d >>= 1) { __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; ai += OFFSET(ai); bi += OFFSET(bi); temp[bi] += temp[ai]; } offset *= 2; } if (thid == 0) temp[n-1+OFFSET(n-1)] = 0; // clear the last elem for (int d = 1; d < n; d *= 2) // traverse down { offset >>= 1; __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; ai += OFFSET(ai); bi += OFFSET(bi); T t = temp[ai]; temp[ai] = temp[bi]; temp[bi] += t; } } __syncthreads(); // required g_odata[a] = temp[a + oa]; g_odata[b] = temp[b + ob]; } template<typename T> __global__ void prescan( T *__restrict__ g_odata, const T *__restrict__ g_idata, const int n) { __shared__ T temp[N]; int thid = threadIdx.x; int offset = 1; temp[2*thid] = g_idata[2*thid]; temp[2*thid+1] = g_idata[2*thid+1]; for (int d = n >> 1; d > 0; d >>= 1) { __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; temp[bi] += temp[ai]; } offset *= 2; } if (thid == 0) temp[n-1] = 0; // clear the last elem for (int d = 1; d < n; d *= 2) // traverse down { offset >>= 1; __syncthreads(); if (thid < d) { int ai = offset*(2*thid+1)-1; int bi = offset*(2*thid+2)-1; float t = temp[ai]; temp[ai] = temp[bi]; temp[bi] += t; } } g_odata[2*thid] = temp[2*thid]; g_odata[2*thid+1] = temp[2*thid+1]; } template <typename T> void runTest (const int repeat, bool timing = false) { T in[N]; T cpu_out[N]; T gpu_out[N]; int n = N; for (int i = 0; i < n; i++) in[i] = (i % 5)+1; cpu_out[0] = 0; for (int i = 1; i < n; i++) cpu_out[i] = cpu_out[i-1] + in[i-1]; T *d_in, *d_out; cudaMalloc((void**)&d_in, n*sizeof(T)); cudaMemcpy(d_in, in, n*sizeof(T), cudaMemcpyHostToDevice); cudaMalloc((void**)&d_out, n*sizeof(T)); dim3 grids (1); dim3 blocks (n/2); cudaDeviceSynchronize(); auto start = std::chrono::steady_clock::now(); for (int i = 0; i < repeat; i++) { prescan<<<grids, blocks>>>(d_out, d_in, n); } cudaDeviceSynchronize(); auto end = std::chrono::steady_clock::now(); auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); if (timing) { printf("Element size in bytes is %zu. Average execution time of block scan (w/ bank conflicts): %f (us)\n", sizeof(T), (time * 1e-3f) / repeat); } cudaMemcpy(gpu_out, d_out, n*sizeof(T), cudaMemcpyDeviceToHost); if (!timing) verify(cpu_out, gpu_out, n); // bcao start = std::chrono::steady_clock::now(); for (int i = 0; i < repeat; i++) { prescan_bcao<<<grids, blocks>>>(d_out, d_in, n); } cudaDeviceSynchronize(); end = std::chrono::steady_clock::now(); auto bcao_time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); if (timing) { printf("Element size in bytes is %zu. Average execution time of block scan (w/o bank conflicts): %f (us). ", sizeof(T), (bcao_time * 1e-3f) / repeat); printf("Reduce the time by %.1f%%\n", (time - bcao_time) * 1.0 / time * 100); } cudaMemcpy(gpu_out, d_out, n*sizeof(T), cudaMemcpyDeviceToHost); if (!timing) verify(cpu_out, gpu_out, n); cudaFree(d_in); cudaFree(d_out); } int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: %s <repeat>\n", argv[0]); return 1; } const int repeat = atoi(argv[1]); for (int i = 0; i < 2; i++) { bool timing = i > 0; runTest<char>(repeat, timing); runTest<short>(repeat, timing); runTest<int>(repeat, timing); runTest<long>(repeat, timing); } return 0; }
5d6046e218929d324b6f7f81b36181cadbc18ec7.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // dot-perfect is our best version of a dot product of vectors // fat threads: more than one vector elemnent per thread // tree-based: reduction of scalar product // summation of c[i] on the host const int N = 2048 * 2048; const int threadsPerBlock = 1024; __global__ void dot( int *a, int *b, int *c ) { __shared__ int temp[threadsPerBlock]; int index = threadIdx.x + blockIdx.x * blockDim.x; int tempindex = threadIdx.x; int tempthread = 0; while (index < N) { tempthread += a[index] * b[index]; index += blockDim.x * gridDim.x; } // set the temp value temp[tempindex] = tempthread; __syncthreads(); // now reduction, need threadsPerBlock to be power of 2 int i = blockDim.x/2; while (i != 0) { if(tempindex < i) temp[tempindex] += temp[tempindex + i]; __syncthreads(); i /= 2; } if (tempindex == 0) c[blockIdx.x] = temp[0]; } #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <iostream> /* #include <book.h> */ #define imin(a,b) (a<b?a:b) int main( void ) { int *a, *b, c, *partial_c ; // host copies of a,b,c,partial_c int *dev_a, *dev_b, *dev_partial_c; // device copies of a, b, partial_c int size = N *sizeof( int); // we need space for N integers hipDeviceProp_t prop; hipGetDeviceProperties( &prop, 0 ); int blocks = prop.multiProcessorCount; const int blocksPerGrid = imin( blocks, (N+threadsPerBlock-1) / threadsPerBlock ); // allocate device copies of a, b, c hipMalloc( (void**)&dev_a, size ); hipMalloc( (void**)&dev_b, size ); hipMalloc( (void**)&dev_partial_c, blocksPerGrid*sizeof(int) ); a = (int*)malloc( size ); b = (int*)malloc( size ); partial_c = (int*)malloc( blocksPerGrid*sizeof(int) ); for (int i=0; i<N; i++) { a[i] = 1; }; for (int i=0; i<N; i++) { b[i] = 1; }; // copy inputs to device hipMemcpy( dev_a, a, size, hipMemcpyHostToDevice); hipMemcpy( dev_b, b, size, hipMemcpyHostToDevice); // launch dot() kernel with N parallel blockshipLaunchKernelGGL(( dot), dim3(blocksPerGrid), dim3(threadsPerBlock) , 0, 0, dev_a, dev_b, dev_partial_c); // copy device result back to host copy of d hipMemcpy( partial_c, dev_partial_c, blocksPerGrid*sizeof(int) , hipMemcpyDeviceToHost); // finish on the CPU side c = 0; for (int i=0; i<blocksPerGrid; i++) { c += partial_c[i]; } printf("a %i b %i ; d %i; \n ",a[0],b[0],c); free( a ); free( b ); free (partial_c ); hipFree( dev_a); hipFree( dev_b); hipFree( dev_partial_c); return 0; }
5d6046e218929d324b6f7f81b36181cadbc18ec7.cu
// dot-perfect is our best version of a dot product of vectors // fat threads: more than one vector elemnent per thread // tree-based: reduction of scalar product // summation of c[i] on the host const int N = 2048 * 2048; const int threadsPerBlock = 1024; __global__ void dot( int *a, int *b, int *c ) { __shared__ int temp[threadsPerBlock]; int index = threadIdx.x + blockIdx.x * blockDim.x; int tempindex = threadIdx.x; int tempthread = 0; while (index < N) { tempthread += a[index] * b[index]; index += blockDim.x * gridDim.x; } // set the temp value temp[tempindex] = tempthread; __syncthreads(); // now reduction, need threadsPerBlock to be power of 2 int i = blockDim.x/2; while (i != 0) { if(tempindex < i) temp[tempindex] += temp[tempindex + i]; __syncthreads(); i /= 2; } if (tempindex == 0) c[blockIdx.x] = temp[0]; } #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <iostream> /* #include <book.h> */ #define imin(a,b) (a<b?a:b) int main( void ) { int *a, *b, c, *partial_c ; // host copies of a,b,c,partial_c int *dev_a, *dev_b, *dev_partial_c; // device copies of a, b, partial_c int size = N *sizeof( int); // we need space for N integers cudaDeviceProp prop; cudaGetDeviceProperties( &prop, 0 ); int blocks = prop.multiProcessorCount; const int blocksPerGrid = imin( blocks, (N+threadsPerBlock-1) / threadsPerBlock ); // allocate device copies of a, b, c cudaMalloc( (void**)&dev_a, size ); cudaMalloc( (void**)&dev_b, size ); cudaMalloc( (void**)&dev_partial_c, blocksPerGrid*sizeof(int) ); a = (int*)malloc( size ); b = (int*)malloc( size ); partial_c = (int*)malloc( blocksPerGrid*sizeof(int) ); for (int i=0; i<N; i++) { a[i] = 1; }; for (int i=0; i<N; i++) { b[i] = 1; }; // copy inputs to device cudaMemcpy( dev_a, a, size, cudaMemcpyHostToDevice); cudaMemcpy( dev_b, b, size, cudaMemcpyHostToDevice); // launch dot() kernel with N parallel blocks dot<<< blocksPerGrid, threadsPerBlock >>>( dev_a, dev_b, dev_partial_c); // copy device result back to host copy of d cudaMemcpy( partial_c, dev_partial_c, blocksPerGrid*sizeof(int) , cudaMemcpyDeviceToHost); // finish on the CPU side c = 0; for (int i=0; i<blocksPerGrid; i++) { c += partial_c[i]; } printf("a %i b %i ; d %i; \n ",a[0],b[0],c); free( a ); free( b ); free (partial_c ); cudaFree( dev_a); cudaFree( dev_b); cudaFree( dev_partial_c); return 0; }
deff56a9e59b12dbc1808ea63bf4c64d8603fdc7.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "divergence.h" /*3D divergence in Fourier space----------------------------------*/ __global__ void solve_divergence(cudaComplex *div_hat_d, cudaComplex *ux_hat_d, cudaComplex *uy_hat_d, cudaComplex *uz_hat_d, real* kx_nabla_d, real* ky_nabla_d, real* kz_nabla_d, int Nx, int Ny, int Nz, real dt) { unsigned int t1, xIndex, yIndex, zIndex, index_in, index_out, gridIndex ; // step 1: compute gridIndex in 1-D and 1-D data index "index_in" unsigned int sizeOfData=(unsigned int) Nx*Ny*Nz; gridIndex = blockIdx.y * gridDim.x + blockIdx.x ; index_in = ( gridIndex * block_size_y + threadIdx.y )*block_size_x + threadIdx.x ; // step 2: extract 3-D data index via // index_in = row-map(i,j,k) = (i-1)*n2*n3 + (j-1)*n3 + (k-1) // where xIndex = i-1, yIndex = j-1, zIndex = k-1 if ( index_in < sizeOfData ){ t1 = index_in/ Nz; zIndex = index_in - Nz*t1 ; xIndex = t1/ Ny; yIndex = t1 - Ny * xIndex ; unsigned int j=xIndex, k=yIndex, l=zIndex; if((j<Nx)&&(k<Ny)&&(l<Nz)){ div_hat_d[IN(j,k,l)].x=(-ux_hat_d[IN(j,k,l)].y*kx_nabla_d[j]-uy_hat_d[IN(j,k,l)].y*ky_nabla_d[k]-uz_hat_d[IN(j,k,l)].y*kz_nabla_d[l])/dt; div_hat_d[IN(j,k,l)].y=(ux_hat_d[IN(j,k,l)].x*kx_nabla_d[j]+uy_hat_d[IN(j,k,l)].x*ky_nabla_d[k]+uz_hat_d[IN(j,k,l)].x*kz_nabla_d[l])/dt; } } } void divergence_device(dim3 dimGrid_C, dim3 dimBlock_C, int Nx, int Ny, int Nz, cudaComplex *div_hat_d, cudaComplex *ux_hat_d, cudaComplex *uy_hat_d, cudaComplex *uz_hat_d, real* kx_nabla_d, real* ky_nabla_d, real* kz_nabla_d, real dt, real Re){ hipLaunchKernelGGL(( solve_divergence), dim3(dimGrid_C), dim3(dimBlock_C), 0, 0, div_hat_d, ux_hat_d, uy_hat_d, uz_hat_d, kx_nabla_d, ky_nabla_d, kz_nabla_d, Nx, Ny, Nz, dt); } void devergence_to_double(dim3 dimGrid, dim3 dimBlock, int Nx, int Ny, int Nz, cudaComplex *div_hat_d, real* div_d){ iFFTN_Device(dimGrid, dimBlock, div_hat_d, div_d, Nx, Ny, Nz); }
deff56a9e59b12dbc1808ea63bf4c64d8603fdc7.cu
#include "divergence.h" /*3D divergence in Fourier space----------------------------------*/ __global__ void solve_divergence(cudaComplex *div_hat_d, cudaComplex *ux_hat_d, cudaComplex *uy_hat_d, cudaComplex *uz_hat_d, real* kx_nabla_d, real* ky_nabla_d, real* kz_nabla_d, int Nx, int Ny, int Nz, real dt) { unsigned int t1, xIndex, yIndex, zIndex, index_in, index_out, gridIndex ; // step 1: compute gridIndex in 1-D and 1-D data index "index_in" unsigned int sizeOfData=(unsigned int) Nx*Ny*Nz; gridIndex = blockIdx.y * gridDim.x + blockIdx.x ; index_in = ( gridIndex * block_size_y + threadIdx.y )*block_size_x + threadIdx.x ; // step 2: extract 3-D data index via // index_in = row-map(i,j,k) = (i-1)*n2*n3 + (j-1)*n3 + (k-1) // where xIndex = i-1, yIndex = j-1, zIndex = k-1 if ( index_in < sizeOfData ){ t1 = index_in/ Nz; zIndex = index_in - Nz*t1 ; xIndex = t1/ Ny; yIndex = t1 - Ny * xIndex ; unsigned int j=xIndex, k=yIndex, l=zIndex; if((j<Nx)&&(k<Ny)&&(l<Nz)){ div_hat_d[IN(j,k,l)].x=(-ux_hat_d[IN(j,k,l)].y*kx_nabla_d[j]-uy_hat_d[IN(j,k,l)].y*ky_nabla_d[k]-uz_hat_d[IN(j,k,l)].y*kz_nabla_d[l])/dt; div_hat_d[IN(j,k,l)].y=(ux_hat_d[IN(j,k,l)].x*kx_nabla_d[j]+uy_hat_d[IN(j,k,l)].x*ky_nabla_d[k]+uz_hat_d[IN(j,k,l)].x*kz_nabla_d[l])/dt; } } } void divergence_device(dim3 dimGrid_C, dim3 dimBlock_C, int Nx, int Ny, int Nz, cudaComplex *div_hat_d, cudaComplex *ux_hat_d, cudaComplex *uy_hat_d, cudaComplex *uz_hat_d, real* kx_nabla_d, real* ky_nabla_d, real* kz_nabla_d, real dt, real Re){ solve_divergence<<<dimGrid_C, dimBlock_C>>>(div_hat_d, ux_hat_d, uy_hat_d, uz_hat_d, kx_nabla_d, ky_nabla_d, kz_nabla_d, Nx, Ny, Nz, dt); } void devergence_to_double(dim3 dimGrid, dim3 dimBlock, int Nx, int Ny, int Nz, cudaComplex *div_hat_d, real* div_d){ iFFTN_Device(dimGrid, dimBlock, div_hat_d, div_d, Nx, Ny, Nz); }
2f6a67dc260e05a026b12a01b1f28c0a1361da06.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <iostream> #include <stdio.h> __global__ void print(){ printf("KYU NHI CHAL RHAA\n"); } int main(){ int n = 3; int x[n]; x[0] = 0; x[1] = 1; x[2] = 2; for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(i != j){ std::cout<<i<<" "<<j<<std::endl; hipSetDevice(j); int* d_v; hipMalloc((void **)&d_v, sizeof(int)); hipMemcpy(d_v, &x[i], sizeof(int), hipMemcpyHostToDevice); hipLaunchKernelGGL(( print), dim3(1),dim3(1), 0, 0, ); hipDeviceSynchronize(); } //std::cout<<i<<" "<<j<<std::endl; /*hipSetDevice(j); int* d_v; hipMalloc((void **)&d_v, sizeof(int)); hipMemcpy(d_v, &x[i], sizeof(int), hipMemcpyHostToDevice);*/ } } for(int i=0;i<n;i++){ hipSetDevice(i); for(int j=0;j<1000;j++){ //std::cout<<j<<std::endl; hipLaunchKernelGGL(( print), dim3(1),dim3(1), 0, 0, ); hipDeviceSynchronize(); } //print<<<1,1>>>(); //hipDeviceSynchronize(); } for(int i=0;i<n;i++){ hipSetDevice(i); for(int j=0;j<1000;j++){ //std::cout<<j<<std::endl; hipLaunchKernelGGL(( print), dim3(1),dim3(1), 0, 0, ); hipDeviceSynchronize(); } //print<<<1,1>>>(); //hipDeviceSynchronize(); } }
2f6a67dc260e05a026b12a01b1f28c0a1361da06.cu
#include <iostream> #include <stdio.h> __global__ void print(){ printf("KYU NHI CHAL RHAA\n"); } int main(){ int n = 3; int x[n]; x[0] = 0; x[1] = 1; x[2] = 2; for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(i != j){ std::cout<<i<<" "<<j<<std::endl; cudaSetDevice(j); int* d_v; cudaMalloc((void **)&d_v, sizeof(int)); cudaMemcpy(d_v, &x[i], sizeof(int), cudaMemcpyHostToDevice); print<<<1,1>>>(); cudaDeviceSynchronize(); } //std::cout<<i<<" "<<j<<std::endl; /*cudaSetDevice(j); int* d_v; cudaMalloc((void **)&d_v, sizeof(int)); cudaMemcpy(d_v, &x[i], sizeof(int), cudaMemcpyHostToDevice);*/ } } for(int i=0;i<n;i++){ cudaSetDevice(i); for(int j=0;j<1000;j++){ //std::cout<<j<<std::endl; print<<<1,1>>>(); cudaDeviceSynchronize(); } //print<<<1,1>>>(); //cudaDeviceSynchronize(); } for(int i=0;i<n;i++){ cudaSetDevice(i); for(int j=0;j<1000;j++){ //std::cout<<j<<std::endl; print<<<1,1>>>(); cudaDeviceSynchronize(); } //print<<<1,1>>>(); //cudaDeviceSynchronize(); } }
814339b2a18a8094a6abfc20279774cd5b7553ff.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <algorithm> #include <vector> #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" namespace caffe { //const float kBNLL_THRESHOLD = 50.; #define kBNLL_THRESHOLD 50.0 template <typename Dtype> __global__ void BNLLForward(const int n, const Dtype* in, Dtype* out) { CUDA_KERNEL_LOOP(index, n) { out[index] = in[index] > 0 ? in[index] + log(1. + exp(-in[index])) : log(1. + exp(in[index])); } } template <typename Dtype> void BNLLLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = top[0]->mutable_gpu_data(); const int count = bottom[0]->count(); // NOLINT_NEXT_LINE(whitespace/operators) hipLaunchKernelGGL(( BNLLForward<Dtype>), dim3(CAFFE_GET_BLOCKS(count)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, count, bottom_data, top_data); CUDA_POST_KERNEL_CHECK; } template <typename Dtype> __global__ void BNLLBackward(const int n, const Dtype* in_diff, const Dtype* in_data, Dtype* out_diff) { CUDA_KERNEL_LOOP(index, n) { Dtype expval = exp(min(in_data[index], Dtype(kBNLL_THRESHOLD))); out_diff[index] = in_diff[index] * expval / (expval + 1.); } } template <typename Dtype> void BNLLLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { if (propagate_down[0]) { const Dtype* bottom_data = bottom[0]->gpu_data(); const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); const int count = bottom[0]->count(); // NOLINT_NEXT_LINE(whitespace/operators) hipLaunchKernelGGL(( BNLLBackward<Dtype>), dim3(CAFFE_GET_BLOCKS(count)), dim3(CAFFE_CUDA_NUM_THREADS), 0, 0, count, top_diff, bottom_data, bottom_diff); CUDA_POST_KERNEL_CHECK; } } INSTANTIATE_LAYER_GPU_FUNCS(BNLLLayer); } // namespace caffe
814339b2a18a8094a6abfc20279774cd5b7553ff.cu
#include <algorithm> #include <vector> #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" namespace caffe { //const float kBNLL_THRESHOLD = 50.; #define kBNLL_THRESHOLD 50.0 template <typename Dtype> __global__ void BNLLForward(const int n, const Dtype* in, Dtype* out) { CUDA_KERNEL_LOOP(index, n) { out[index] = in[index] > 0 ? in[index] + log(1. + exp(-in[index])) : log(1. + exp(in[index])); } } template <typename Dtype> void BNLLLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = top[0]->mutable_gpu_data(); const int count = bottom[0]->count(); // NOLINT_NEXT_LINE(whitespace/operators) BNLLForward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, bottom_data, top_data); CUDA_POST_KERNEL_CHECK; } template <typename Dtype> __global__ void BNLLBackward(const int n, const Dtype* in_diff, const Dtype* in_data, Dtype* out_diff) { CUDA_KERNEL_LOOP(index, n) { Dtype expval = exp(min(in_data[index], Dtype(kBNLL_THRESHOLD))); out_diff[index] = in_diff[index] * expval / (expval + 1.); } } template <typename Dtype> void BNLLLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { if (propagate_down[0]) { const Dtype* bottom_data = bottom[0]->gpu_data(); const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); const int count = bottom[0]->count(); // NOLINT_NEXT_LINE(whitespace/operators) BNLLBackward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( count, top_diff, bottom_data, bottom_diff); CUDA_POST_KERNEL_CHECK; } } INSTANTIATE_LAYER_GPU_FUNCS(BNLLLayer); } // namespace caffe
38656c8885956678eb9eb1b0d525228dd8fe0af2.hip
// !!! This is a file automatically generated by hipify!!! /* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <vector> #include <rocblas.h> #include <hip/hip_runtime.h> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) { hipblasHandle_t cublasH = NULL; hipStream_t stream = NULL; const int m = 2; const int n = 2; const int lda = m; /* * A = | 1.0 2.0 | * | 3.0 4.0 | * x = | 5.0 6.0 | */ const std::vector<data_type> AP = {1.0, 3.0, 2.0, 4.0}; std::vector<data_type> x = {5.0, 6.0}; const int incx = 1; data_type *d_AP = nullptr; data_type *d_x = nullptr; hipblasFillMode_t uplo = HIPBLAS_FILL_MODE_UPPER; hipblasOperation_t transa = HIPBLAS_OP_N; hipblasDiagType_t diag = HIPBLAS_DIAG_NON_UNIT; printf("AP\n"); print_matrix(m, n, AP.data(), lda); printf("=====\n"); printf("x\n"); print_vector(x.size(), x.data()); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ CUBLAS_CHECK(hipblasCreate(&cublasH)); CUDA_CHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking)); CUBLAS_CHECK(hipblasSetStream(cublasH, stream)); /* step 2: copy data to device */ CUDA_CHECK(hipMalloc(reinterpret_cast<void **>(&d_AP), sizeof(data_type) * AP.size())); CUDA_CHECK(hipMalloc(reinterpret_cast<void **>(&d_x), sizeof(data_type) * x.size())); CUDA_CHECK(hipMemcpyAsync(d_AP, AP.data(), sizeof(data_type) * AP.size(), hipMemcpyHostToDevice, stream)); CUDA_CHECK(hipMemcpyAsync(d_x, x.data(), sizeof(data_type) * x.size(), hipMemcpyHostToDevice, stream)); /* step 3: compute */ CUBLAS_CHECK(hipblasDtpsv(cublasH, uplo, transa, diag, n, d_AP, d_x, incx)); /* step 4: copy data to host */ CUDA_CHECK(hipMemcpyAsync(x.data(), d_x, sizeof(data_type) * x.size(), hipMemcpyDeviceToHost, stream)); CUDA_CHECK(hipStreamSynchronize(stream)); /* * x = | -4.00 3.00 | */ printf("x\n"); print_vector(x.size(), x.data()); printf("=====\n"); /* free resources */ CUDA_CHECK(hipFree(d_AP)); CUDA_CHECK(hipFree(d_x)); CUBLAS_CHECK(hipblasDestroy(cublasH)); CUDA_CHECK(hipStreamDestroy(stream)); CUDA_CHECK(hipDeviceReset()); return EXIT_SUCCESS; }
38656c8885956678eb9eb1b0d525228dd8fe0af2.cu
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <vector> #include <cublas_v2.h> #include <cuda_runtime.h> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) { cublasHandle_t cublasH = NULL; cudaStream_t stream = NULL; const int m = 2; const int n = 2; const int lda = m; /* * A = | 1.0 2.0 | * | 3.0 4.0 | * x = | 5.0 6.0 | */ const std::vector<data_type> AP = {1.0, 3.0, 2.0, 4.0}; std::vector<data_type> x = {5.0, 6.0}; const int incx = 1; data_type *d_AP = nullptr; data_type *d_x = nullptr; cublasFillMode_t uplo = CUBLAS_FILL_MODE_UPPER; cublasOperation_t transa = CUBLAS_OP_N; cublasDiagType_t diag = CUBLAS_DIAG_NON_UNIT; printf("AP\n"); print_matrix(m, n, AP.data(), lda); printf("=====\n"); printf("x\n"); print_vector(x.size(), x.data()); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ CUBLAS_CHECK(cublasCreate(&cublasH)); CUDA_CHECK(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking)); CUBLAS_CHECK(cublasSetStream(cublasH, stream)); /* step 2: copy data to device */ CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_AP), sizeof(data_type) * AP.size())); CUDA_CHECK(cudaMalloc(reinterpret_cast<void **>(&d_x), sizeof(data_type) * x.size())); CUDA_CHECK(cudaMemcpyAsync(d_AP, AP.data(), sizeof(data_type) * AP.size(), cudaMemcpyHostToDevice, stream)); CUDA_CHECK(cudaMemcpyAsync(d_x, x.data(), sizeof(data_type) * x.size(), cudaMemcpyHostToDevice, stream)); /* step 3: compute */ CUBLAS_CHECK(cublasDtpsv(cublasH, uplo, transa, diag, n, d_AP, d_x, incx)); /* step 4: copy data to host */ CUDA_CHECK(cudaMemcpyAsync(x.data(), d_x, sizeof(data_type) * x.size(), cudaMemcpyDeviceToHost, stream)); CUDA_CHECK(cudaStreamSynchronize(stream)); /* * x = | -4.00 3.00 | */ printf("x\n"); print_vector(x.size(), x.data()); printf("=====\n"); /* free resources */ CUDA_CHECK(cudaFree(d_AP)); CUDA_CHECK(cudaFree(d_x)); CUBLAS_CHECK(cublasDestroy(cublasH)); CUDA_CHECK(cudaStreamDestroy(stream)); CUDA_CHECK(cudaDeviceReset()); return EXIT_SUCCESS; }
706d557d4a08a38e3bdc690283225e0343ddcaf2.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <stdio.h> #define NUM_BLOCKS 3 //Number of blocks #define BLOCK_WIDTH 192 //number of threads in in the thread block __global__ void hello() { printf("Hello world! I'm a thread in block %d and my thread id is %d\n", blockIdx.x,threadIdx.x); } int main(int argc,char **argv) { // launch the kernel hipLaunchKernelGGL(( hello), dim3(NUM_BLOCKS), dim3(BLOCK_WIDTH), 0, 0, ); // force the printf()s to flush hipDeviceSynchronize(); printf("That's all!\n"); return 0; }
706d557d4a08a38e3bdc690283225e0343ddcaf2.cu
#include <stdio.h> #define NUM_BLOCKS 3 //Number of blocks #define BLOCK_WIDTH 192 //number of threads in in the thread block __global__ void hello() { printf("Hello world! I'm a thread in block %d and my thread id is %d\n", blockIdx.x,threadIdx.x); } int main(int argc,char **argv) { // launch the kernel hello<<<NUM_BLOCKS, BLOCK_WIDTH>>>(); // force the printf()s to flush cudaDeviceSynchronize(); printf("That's all!\n"); return 0; }
2c9a6a7281497c8ba853fd547f4ccb678d730014.hip
// !!! This is a file automatically generated by hipify!!! /******************|********************|*******************|******************| * * * ROUTER ALGORITHM * * * * PREPARED BY: Bennett D. Clarke * * University of Illinois ( Chicago ) * * Chicago, Il. 60610 * * Date: April 17, 2013 * * * ******************************************************************************* * * * OBJECTIVE: To use a data set of routing nodes and to use a parallel * * processor to run a single source shortest path algorithm * * to find the first hop node from the chosen source to any * * of the nodes on the routing topology. The code is in * * Cuda. * * * * METHODOLOGY: Use Dykstra's algorithm with uniform cost of 1 per router * * hop ( 1 per edge ). * ******************************************************************************** * * * DESCRIPTION OF VARIABLES * * * * edgeArrayParallel[ mVertex + nEdges ] Large Array with All vertices * * and their outbound neighbors * * EdgeAPCount Index of edgeArrayParallel for both * * terminus of outbound edges and * * flags for end of linked list * * EdgeTable[ mVertices ] Array of pointers; for each vertex * * the array entry points to a * * linked list of outbound edges * * each of which is a neighbor of * * the indexed vertex * * edgeWeightParallel [ iEdge ] Weight set to 1000000 until reduced * * leavingEdgeNo Count of number of outbound edges * * from a Vertex * * processingNode[ ] * * vertexArrayParallel[ iVertex ] Each index represents corresponding * * vertex and it points to the * * index in the edgeArrayParallel * * where its first neighbor is * * represented * * *******************************************************************************/ #include <iostream> #include <string> #include <fstream> #include <vector> #include <map> #include <cstdlib> //#include <limits> #include <cstdio> #include <new> // CUDA INCLUDES #include <hip/hip_runtime.h> // CUDA BY EXAMPLE CODE #include "../common/book.h" // DECLARATIONS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //#define N 10 // DEFINE FOR THREAD BLOCK NUMBER using std::cout; using std::endl; typedef unsigned int vertex; //CUDA KERNELS __global__ void ssss_1 ( int N, bool * processingNode, float * costToNode , float * updatedCost, int * vertexArrayParallel, int * edgeArrayParallel, float * edgeWeightParallel, int * accessedNode, int * parentArray ) { int tid = blockIdx.x; int nid; if ( tid < N ) { if( processingNode[ tid ] ) { processingNode[ tid ] = false; // Remove node from the exploring group // TEST IF ORIGINATING VERTEX HAS NEIGHBORS OTHER THAN ITS INBOUND // ROUTES FROM THE SOURCE -- -1 MEANS NO OUTBOUND EDGES if ( vertexArrayParallel [ tid ] != -1 ){ // OUTBOUND EDGE TERMINUS IS NID; VAP[ TID ] GIVES THE INDEX IN EAP // WHERE THE OUTBOUND EDGE TERMINUS VERTEX ID CAN BE FOUND int eAPIndex = vertexArrayParallel[ tid ]; nid = edgeArrayParallel [ eAPIndex ]; // THE ARRAY ENTRIES THAT CORRESPOND TO A LINKED LIST OF EDGES // EMANATING FROM THE EXPANDING NODE WILL TERMINATE IN AN ARRAY ENTRY // EQUAL TO -1 -- EACH SEPARATE VALID NID REPRESENTS THE TERMINUS // OF A NEIGHBOR NODE while ( -1 != nid ){ ++accessedNode[ nid ]; if ( updatedCost[ nid ] > costToNode[ tid ] + edgeWeightParallel[ eAPIndex ] ) { updatedCost[ nid ] = costToNode [ tid ] + edgeWeightParallel[ eAPIndex ]; parentArray[ nid ] = tid; } // INCREMENT THE INDEX TO THE EAP BY 1 TO GET NEXT OUTBOUND EDGE // TERMINUS - OTHERWISE KNOWN AS NEIGHBOR //++(vertexArrayParallel[ tid ]); ++eAPIndex; nid = edgeArrayParallel [ eAPIndex ]; } // END WHILE // FINISHED PROCESSING ALL OUTBOUND EDGES TO NEIGHBORS // RETURN DATA TO CPU is coded in cpu section } // END if != NULL } // END IF PROCEESINGNODE } // END IF TID < N } // END KERNEL 1 __global__ void ssss_2 ( int N, bool * processingNode, float * costToNode , float * updatedCost, int * vertexArrayParallel, int * edgeArrayParallel, float * edgeWeightParallel ) { int tid = blockIdx.x; if ( tid < N ) { if( costToNode[ tid ] > updatedCost[ tid ] ) { costToNode[ tid ] = updatedCost[ tid ]; processingNode[ tid ] = true; } // END UPDATE PATH TO NODE COST else { updatedCost[ tid ] = costToNode[ tid ]; } } // END IF tid is valid } // END KERNEL 2 // VARIABLES bool undirectedGraphFlag ( false ); // A LINE OF DATA FROM INPUT FILE CONSISTING OF 2 VERTICES AND THE COST OF A TRAVERSAL // FROM VERTEX A TO VERTEX B struct DataLineIn{ public: vertex vertexANo; vertex vertexBNo; float weight; DataLineIn( vertex initVertexANo, vertex initVertexBNo, float initWeight ) : vertexANo( initVertexANo ), vertexBNo( initVertexBNo ), weight( initWeight ) {} }; // NODE CONSISTING OF A LINE OF DATA ( VERTEX A, VERTEX B, WEIGHT ) AND PTR struct Node{ DataLineIn data; Node * next; Node( DataLineIn initData ) : data ( initData ) { next = NULL; } Node( DataLineIn initData, Node * initNext ) : data ( initData ), next ( initNext ) {} Node( vertex initVertexANo, vertex initVertexBNo, float initWeight ) : data( initVertexANo, initVertexBNo, initWeight), next ( NULL ) { std::cout << "Detailed constructor" << std::endl; } }; struct EdgeQueue{ Node * head; Node * tail; EdgeQueue( ) { head = NULL; tail = NULL; } }; struct TreeNode{ vertex vertexNo; float distance; std::map < vertex, TreeNode * > childMapPtr; TreeNode * parent; TreeNode( vertex initVertexNo, float initDistance ) : vertexNo ( initVertexNo ), distance ( initDistance ) { parent = NULL; } }; struct Queue{ TreeNode * head; TreeNode * tail; Queue( ) { head = NULL; tail = NULL; } }; // VECTOR CONSISTING OF DATA IN ENTRIES ( VERTEX A, VERTEX B, WEIGHT ) std::vector < DataLineIn * > DataIn; // FUNCTIONS // PROVIDES USAGE static void show_usage(std::string fileName) { std::cerr << "Running RouterAlgorithmArt.cu\n" "Usage: " << fileName << " <option(s)> DATA INPUT FILE " << "Options:\n" << "\t-h,--help\t\tShow this help message\n" << "\t-d,--data_file_path \tSpecify the data inputs path" << "\t-u,--undirected graph input option" << std::endl; } int main ( int argc, char ** argv ){ if ( argc < 3 ) { show_usage( argv[ 0 ] ); return 1; } std::string dataIn_Name = argv[ 1 ]; for ( int i = 1; i < argc; ++i ) { std::string arg = argv[ i ]; if ( ( arg == "-h" ) || ( arg == "--help" ) ) { show_usage( argv[ 0 ] ); return 0; } else if ( arg == "-u" ){ undirectedGraphFlag = true; } else if ( ( arg == "-d") || ( arg == "--data_file_path" ) ) { if ( i + 1 < argc ) { dataIn_Name = argv[ ++i ]; } else { std::cerr << "--data_file_path option requires one argument." << std::endl; return 1; } } } // END FOR LOOP std::cout << "datafilename is: " << dataIn_Name << std::endl; unsigned int mV ( 0 ); // TO INPUT NUMBER OF VERTICES unsigned int nE ( 0 ); // TO INPUT NUMBER OF EDGES int a, b; // INTEGER REPRESENTATION OF VERTICES double c; // DOUBLE REPRESENTATION OF WEIGHT std::string line; std::ifstream dataIn; // STREAM TO INPUT DATAFILE // PROCESS INPUT FILE INTO VECTOR OF ENTRIES (VERTEXA, VERTEXB, WEIGHT) dataIn.open ( dataIn_Name.c_str() ); if ( dataIn.is_open() ) { // READ FIRST LINE OF DATA WITH SIZE OF GRAPH AND PRINT dataIn >> mV >> nE; // UNDIRECTED GRAPH OPTION if ( undirectedGraphFlag == true ){ nE *= 2; } std::cout << "mVertices: " << mV << ", nEdges: " << nE << std::endl; // PROCESS BODY OF DATA unsigned int dataCount ( 0 ); while ( dataCount < nE ) { // READ A LINE OF DATA FROM FILE AND PRINT IT dataIn >> a >> b >> c; std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl; DataLineIn *ptr = new DataLineIn( a, b, c ); // Enqueue new data element ptr to vector DataIn.push_back( ptr ); // UNDIRECTED GRAPH OPTION if ( undirectedGraphFlag == true ) { cout << "a: " << b << ", b: " << a << ", c: " << c << endl; DataLineIn *ptr = new DataLineIn( b, a, c ); // ENQUEUE DataIn.push_back( ptr ); ++dataCount; } // INCREMENT FOR NEXT DATA LINE INPUT ++dataCount; } dataIn.close(); std::cout << "DataIn contains " << DataIn.size() << " records." << std::endl; } // IF FILE FAILS TO OPEN else std::cout << "Unable to open file"; // CREATE CONST UNSIGNED INT TO USE FOR DECLARATION OF ARRAYS // DOES NOT WORK HERE const unsigned int mVertices ( mV ); // NUMBER OF VERTICES IN GRAPH const unsigned int nEdges ( nE ); // NUMBER OF EDGES IN GRAPH // INITIALIZE 2D DYNAMIC ADJACENCY TABLE TO AN INFINITE ( 1000000 ) VALUE // std::cout << "Create adjacency table " << std::endl; // float ** adjacency = new float* [ mVertices ]; // for ( unsigned int i = 0; i < mVertices; ++i ){ // adjacency[ mVertices ] = new float[ mVertices ]; // } // // std::cout << "Initialize Adjacency Table " << std::endl; // for ( unsigned int iCount = 0; iCount < mVertices; iCount++ ){ // for ( unsigned int jCount = 0; jCount < mVertices; jCount++ ){ // std::cout << "at iter i,j, " << iCount << "," << jCount << "," << std::flush; // //std::cout << adjacency[ iCount ][ jCount ] << std::endl; // // adjacency[ iCount ][ jCount ] = 1000000.0; // } // } // CREATE EDGE TABLE AND INITIALIZE std::cout << "Create and initialize Edge Table " << " Size of node* " << sizeof( Node ** ) << std::endl; Node ** EdgeTable = new Node * [ mVertices ]; // std::cout << "Error in creating EdgeTable array! " << std::endl; // INITIALIZE ARRAY OF NODE POINTERS TO NULL // Node * EdgeTable [ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ EdgeTable [ iCount ] = NULL; } // LOAD GRAPH DATA INTO ADJACENCY MATRIX std::cout << "Loading data into adjacency table " << std::cout; unsigned int kCount = 0; // while( kCount < DataIn.size() ){ // adjacency[ DataIn[ kCount ]->vertexANo ][ DataIn[ kCount ]->vertexBNo ] = // DataIn[ kCount ]->weight; // ++kCount; // } // END WHILE // PRINT ADJACENCY TABLE // std::cout << "\nAdjacency Matrix:" << std::endl; // std::cout << " "; // for( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ // std::cout << " v" << iCount; // } // std::cout << "Not used with CUDA" << std::endl; // for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ // std::cout << "v" << iCount; // for( unsigned int jCount = 0; jCount < mVertices; ++jCount ){ // if( adjacency[ iCount ][ jCount ] > 999999.9 ) // std::cout << " " << "-" ; // else // std::cout << " " << adjacency[ iCount ][ jCount ]; // } // std::cout << std::endl; // } kCount = 0; while ( kCount < DataIn.size() ){ // EDGE TABLE -- EACH DATA LINE OF THE INPUT ( AN EDGE ) IS PLACED IN A NODE // THE NODE IS THEN INSERTED IN A LINKED LIST THAT IS UNIQUE FOR EACH VERTEX // DATA RECORDS NUMBER IS KCOUNT; EDGETABLE ARRAY 1 FOR EACH VERTEX // NOTE AUTHORS USE A SINGLE EDGE ARRAY WHERE THE LINKED LIST ELEMENTS ARE // HELD AS CONTIGUOUS ARRAY MEMBERS DataLineIn dat = *DataIn[ kCount ]; Node * nPtr = new Node( dat ); Node * traveller; // IF THE EDGE TABLE ENTRY HAS NO LINKED LIST - ADD AS FIRST NODE if ( !EdgeTable[ DataIn[ kCount ]->vertexANo ] ){ EdgeTable[ DataIn[ kCount ]->vertexANo ] = nPtr; } else // ELSE ADD AT END OF LIST { traveller = EdgeTable[ DataIn[ kCount ]->vertexANo ]; while ( traveller->next ){ traveller = traveller->next; } traveller->next = nPtr; } // END IF-ELSE BLOCKS ++kCount; } // END WHILE // PRINT EDGE TABLE std::cout << "Edge Table:" << std::endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ std::cout << iCount << ": " ; Node * traveller; traveller = EdgeTable[ iCount ]; while ( traveller ){ std::cout << iCount << "->" << traveller->data.vertexBNo << " " ; traveller = traveller->next; } std::cout << std::endl; } //////////////////// CREATE EDGE ADJACENCY ARRAY FOR CUDA ///////////////////// // SIZE OF EXTENDED ADJACENCY TABLE IS SAME AS NUMBER OF DATA RECORDS int * vertexArrayParallel = new int [ mVertices ]; // Each index represents its // respectively numbered vertex // the value is the EdgeArray index // of the terminus vertex of // the first edge in the linked list // Represents edge: index->Edarray[value] // INITIALIZE THE VERTEXARRAYPARALLEL ENTRIES TO -1 WHICH WILL REPRESENT // A VERTEX WITHOUT OUTBOUND EDGES for ( unsigned int iVertex = 0; iVertex < mVertices; ++iVertex ){ vertexArrayParallel [ iVertex ] = -1; } // PRINT OUT ARRAY VALUES // for ( unsigned int iVertex = 0; iVertex < mVertices; ++iVertex ){ // std::cout << iVertex << ": " << vertexArrayParallel[ iVertex ] << std::endl; // } // CREATE AN ARRAY THAT WILL PROVIDE ONE ENTRY FOR EVERY OUT EDGE AND EVERY VERTEX // INITIALIZE TO -1 -- -1 WILL BE A FLAG TO REPRESENT THE END OF A LINKED LIST // OF OUTBOUND EDGES int * edgeArrayP = new int [ nEdges + mVertices ]; float * edgeWeightParallel = new float [ nEdges + mVertices ]; // INITIALIZE LARGE PARALLEL ARRAY ( edgeArrayP ) WITH -1 unsigned int iEdge ( 0 ); for ( iEdge = 0; iEdge < (nEdges + mVertices); ++iEdge ){ edgeArrayP[ iEdge ] = -1; } unsigned int i ( 0 ); cout << "After initialization the edgeArrayP:" << endl; for( i = 0; i < (nEdges + mVertices); ++i ){ std::cout << "edgeArrayP[ " << i << " ] : " << edgeArrayP[ i ] << ", " << std::endl; } // INITIALIZE ARRAY OF WEIGHTS TO INFINITE = 1000000 // NOTE THAT INDICES MUST MATCH THOSE OF THE EDGEARRAYP SO THAT THERE ARE // EDGE + VERTICES ENTRIES AND SOME REMAIN 1000000 AS FLAGS for ( iEdge = 0; iEdge < ( nEdges + mVertices ); ++iEdge ){ edgeWeightParallel [ iEdge ] = 1000000; } cout << "edgeWeightParallel[ each edge ] array as initialized " << endl; for( i = 0; i < ( nEdges + mVertices ); ++i ){ std::cout << "edgeWeightParallel[ " << i << " ] : " << edgeWeightParallel[ i ] << ", " << std::endl; } // LOAD DATA int EdgeAPCount ( 0 ); // Index in EdgeArrayParallel std::cout << "Loading data to parallel arrays" << std::endl; for ( unsigned int iVertex = 0; iVertex < mVertices; ++iVertex ){ Node * traveller; traveller = EdgeTable[ iVertex ]; // Each table entry is head of linked list int leavingEdgeNo ( 0 ); // Initialize number of outbound edges from V // FOR EACH OUTBOUND EDGE IN A LINKED LIST - TRAVELLER POINTS TO THE EDGE // THAT EMANATES FROM VERTEX NUMBERED IVERTEX while ( traveller ){ // LEADINGEDGENO == 0 INDICATES THAT THIS IS THE FIRST OUTBOUND EDGE // FROM A SPECIFIC EMANATING VERTEX if ( ! leavingEdgeNo ) { // USE POINTER FOR FIRST EDGE IN EACH VERTICE'S LINKED LIST std::cout << "iVertex: " << iVertex << std::endl; // SET POINTER AND ITERATE FOR FIRST ELEMENT IN LINKED LIST std::cout << "Head of a list: " << std::flush; //vertexArrayParallel [ iVertex ] = &edgeArrayParallel[ EdgeAPCount ]; vertexArrayParallel [ iVertex ] = EdgeAPCount; std::cout << "vertexA<index> to edgeTable<index>: " << "Vertex no: " << iVertex << " maps to " << vertexArrayParallel [ iVertex ] << " in the large array" << std::endl; ++leavingEdgeNo; } // RECORD B (A->B) VERTEX IN EDGEARRAY FOR ALL NODES edgeArrayP[ EdgeAPCount ] = traveller->data.vertexBNo; edgeWeightParallel[ EdgeAPCount ] = traveller->data.weight; std::cout << "General node data points to vertex: " << "edgeArrayP[ " << EdgeAPCount << " ]: " << edgeArrayP[ EdgeAPCount ] << " weight is " << edgeWeightParallel[ EdgeAPCount ] << std::endl; ++EdgeAPCount; // ITERATE ON TRAVELLER IN LINKED LIST traveller = traveller->next; // for( unsigned int i = 0; i < EdgeAPCount; ++i ){ // std::cout << "index : " << i << " is " << edgeArrayP[ i ] // << ", " << std::endl; // } } // END WHILE LOOP // for loop will increment to next vertex // PROVIDE A FLAG ( -1 ) TO SEPARATE ELEMENTS BY THEIR INITIATION VERTEX // A OF ( A->B ) cout << "\t\t\tSentinel value: " << "edgeArrayP[ " << EdgeAPCount << " ]: " << edgeArrayP[ EdgeAPCount ] << std::endl; ++EdgeAPCount; } // END FOR LOOP // CHECK ARRAYS for( int i = 0; i < EdgeAPCount; ++i ){ std::cout << "edgeArrayP[ " << i << " ] : " << edgeArrayP[ i ] << ", " << std::endl; } for( i = 0; i < ( EdgeAPCount ); ++i ){ std::cout << "index : " << i << " is " << edgeWeightParallel[ i ] << ", " << std::endl; } std::cout << "Declaring device data structures" << std::endl; int * dev_vertexArrayParallel; int * dev_edgeArrayP; float * dev_edgeWeightParallel; hipMalloc( ( void** ) &dev_vertexArrayParallel, mVertices * sizeof( int * ) ); hipMalloc( ( void** ) &dev_edgeArrayP, (nEdges + mVertices) * sizeof( int ) ); hipMalloc( ( void** ) &dev_edgeWeightParallel, ( nEdges + mVertices ) * sizeof( float ) ); //////////////////////////////////////////////////////////////////////////////// // TRANSFORM TO ARRAY OF VERTICES AT HEAD OF LINKED LISTS int singleSourceId ( -1 ); std::cout << "Enter integer identifier of single source node: " << std::flush; std::cin >> singleSourceId; cout << "Received data: " << singleSourceId << endl; // CREATE A SINGLE SOURCE DATALINE NODE - THIS POINTS TO ITSELF WITH WEIGHT 0 float sourceWeight ( 0 ); //DataLineIn * initDataLine = new DataLineIn( singleSourceId, singleSourceId, // sourceWeight ); Node * initNode = new Node( singleSourceId, singleSourceId, sourceWeight ); std::cout << "SS.weight: " << initNode->data.weight << std::endl; ///////////////// DATA STRUCTURES FOR PARALLEL PROCESSING ///////////////////////// // CREATE A BOOL ARRAY TO HOLD FLAG FOR NODES BEING PROCESSED bool processingNode[ mVertices ]; // CREATE A COST ARRAY AS THE COST OF REACHING EACH NODE FROM THE SOURCE float costToNode[ mVertices ]; // CREATE A UPDATE COST ARRAY AS THE TEMPORARILY CALCULATED LEAST COST TO // EACH NODE FROM THE SOURCE float updatedCost[ mVertices ]; // INITIALIZE THE ARRAYS // FALSE FOR NOT CURRENTLY EXAMINED // 1000000.00 TO APPROXIMATE INFINITE COST OF AN UNCONNECTED VERTEX for ( unsigned int ii = 0; ii < mVertices; ++ii ){ processingNode [ ii ] = false; updatedCost[ ii ] = costToNode[ ii ] = 1000000.00; } for ( unsigned int ii = 0; ii < mVertices; ++ii ){ cout << "costToNode[ " << ii << " ] : " << costToNode[ ii ] << endl; } // CREATE PARENT ARRAY TO INDICATE PARENT OF ATTACHED NODE// INITIALIZE TO -1 int parentArray[ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ parentArray[ iCount ] = -1; } // CREATE CORRESPONDING ARRAYS FOR GPU AND CUDA bool *dev_processingNode; float *dev_costToNode; float *dev_updatedCost; int *dev_parentArray; // // ALLOCATE MEMORY ON GPU hipMalloc( ( void** ) &dev_processingNode, mVertices * sizeof( bool ) ); hipMalloc( ( void** ) &dev_costToNode, mVertices * sizeof( float ) ); hipMalloc( ( void** ) &dev_updatedCost, mVertices * sizeof( float ) ); hipMalloc( ( void** ) &dev_parentArray, mVertices * sizeof( int ) ); std::cout << "parallel processing arrays initialized" << std::endl; ////////////////////////// END COST ARRAYS ////////////////////////////////////// // CREATE A ROOT NODE FOR USE IN THE EXPLORED QUEUE ( element is the source // parent is null ) // TreeNode has elements: vertexNo, distance to source, ptr to map of children, // ptr to parent TreeNode * root = new TreeNode( singleSourceId, 0.0 ); root->parent = NULL; // CREATE A MAP WITH VERTEX NO = KEY; TREE NODE * = VALUE std::map< vertex, TreeNode* > * mapPtr = new std::map<vertex, TreeNode* >(); root->childMapPtr = *mapPtr; // SET EXPLORED NODE QUEUE // ANALOG TO GPU SETTING BOOL TO FALSE Queue *exploredPtr = new Queue(); // INITIALIZE ARRAY TO HOLD DISTANCE FROM SOURCE TO EACH NODE ID // CPU ANALOG OF COSTTONODE float distanceToS [ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ distanceToS[ iCount ] = -1.0; } // ENTER 0 DISTANCE FOR THE SOURCE NODE distanceToS[ singleSourceId ] = 0.0; // SET ARRAY "PARENT NODE ARRAY' TO HOLD PTR TO PARENT'S TREENODE TreeNode * parentNodeArray[ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ parentNodeArray[ iCount ] = NULL; } // SET PARENTARRAY FOR GPU CALCULATIONS parentArray[ singleSourceId ] = singleSourceId; //INITIALIZE THE EXPLORING QUEUE WITH THE SINGLE SOURCE EdgeQueue *exploringPtr = new EdgeQueue(); exploringPtr->head = initNode; std::cout << "adding to exploring queue: " << exploringPtr->head->data.vertexANo << std::endl; //////////////////////////// DIAGNOSTIC STRUCTURES ///////////////////////////////// int accessedNode[ mVertices ]; int accessedNodeReply[ mVertices ]; int *dev_accessedNode; for( int iCount = 0; iCount < mVertices; ++iCount ) { accessedNode[ iCount ] = accessedNodeReply[ iCount ] = 0; } hipMalloc( ( void** ) &dev_accessedNode, mVertices * sizeof( int ) ); //////////////////////// GPU INITIALIZATION WITH SINGLE SOURCE ///////////////////// processingNode[ singleSourceId ] = true; costToNode[ singleSourceId ] = 0.0; updatedCost[ singleSourceId ] = 0.0; //////////////////////// START GPU PROCESSING LOOP /////////////////////////////////// hipMemcpy( dev_vertexArrayParallel, vertexArrayParallel, mVertices * sizeof( int ), hipMemcpyHostToDevice ); hipMemcpy( dev_edgeArrayP, edgeArrayP, ( mVertices + nEdges ) * sizeof( int ), hipMemcpyHostToDevice ); hipMemcpy( dev_edgeWeightParallel, edgeWeightParallel, ( mVertices + nEdges ) * sizeof( float ), hipMemcpyHostToDevice ); bool flagContinue ( true ); while ( flagContinue ){ // COPY INITIAL DATA TO GPU hipMemcpy( dev_processingNode, processingNode, mVertices * sizeof( bool ), hipMemcpyHostToDevice ); hipMemcpy( dev_costToNode, costToNode, mVertices * sizeof( float ), hipMemcpyHostToDevice ); hipMemcpy( dev_updatedCost, updatedCost, mVertices * sizeof( float ), hipMemcpyHostToDevice ); hipMemcpy( dev_parentArray, parentArray, mVertices * sizeof( int ), hipMemcpyHostToDevice ); // DIAGNOSTIC FUNCTION hipMemcpy( dev_accessedNode, accessedNode, mVertices * sizeof( int ), hipMemcpyHostToDevice ); cout << "Copied data to gpu " << endl; // EXECUTE KERNEL ON GPU hipLaunchKernelGGL(( ssss_1), dim3(mVertices), dim3(1), 0, 0, mVertices, dev_processingNode, dev_costToNode, dev_updatedCost, dev_vertexArrayParallel, dev_edgeArrayP, dev_edgeWeightParallel, dev_accessedNode, dev_parentArray ); cout << "Processed kernel1 " << endl; // RETURN PROCESSED DATA FROM GPU HANDLE_ERROR( hipMemcpy( updatedCost, dev_updatedCost, mVertices * sizeof( float ), hipMemcpyDeviceToHost ) ); cout << "copied updatedCost " << endl; // exit ( 0 ); hipMemcpy( processingNode, dev_processingNode, mVertices * sizeof( bool ), hipMemcpyDeviceToHost ); hipMemcpy( parentArray, dev_parentArray, mVertices * sizeof( int ), hipMemcpyDeviceToHost ); // DIAGNOSTIC STRUCTURE hipMemcpy( accessedNodeReply, dev_accessedNode, mVertices* sizeof( int ), hipMemcpyDeviceToHost ); ///////////////////// END KERNEL 1 /////////////////////////////////////////// cout << "End kernel 1" << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "processing [ " << iCount << " ] : " << processingNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "updatedCost [ " << iCount << " ] : " << updatedCost[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "costToNode [ " << iCount << " ] : " << costToNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "accessedNodeReply [ " << iCount << " ] : " << accessedNodeReply[ iCount ] << endl; } // exit ( 0 ); ///////////////////// KERNEL 2 //////////////////////////////////////////////////// // COPY INITIAL DATA TO GPU hipMemcpy( dev_processingNode, processingNode, mVertices * sizeof( bool ), hipMemcpyHostToDevice ); hipMemcpy( dev_costToNode, costToNode, mVertices * sizeof( float ), hipMemcpyHostToDevice ); hipMemcpy( dev_updatedCost, updatedCost, mVertices * sizeof( float ), hipMemcpyHostToDevice ); // EXECUTE KERNEL ON GPU hipLaunchKernelGGL(( ssss_2), dim3(mVertices), dim3(1), 0, 0, mVertices, dev_processingNode, dev_costToNode, dev_updatedCost, dev_vertexArrayParallel, dev_edgeArrayP, dev_edgeWeightParallel ); // RETURN PROCESSED DATA FROM GPU hipMemcpy( costToNode, dev_costToNode, mVertices * sizeof( float ), hipMemcpyDeviceToHost ); hipMemcpy( updatedCost, dev_updatedCost, mVertices * sizeof( float ), hipMemcpyDeviceToHost ); hipMemcpy( processingNode, dev_processingNode, mVertices * sizeof( bool ), hipMemcpyDeviceToHost ); // hipMemcpy( parentArray, dev_parentArray, mVertices * sizeof( int ), // hipMemcpyDeviceToHost ); cout << "End kernel2" << endl; ////////////////////////// END KERNEL 2 //////////////////////////////////////////// cout << "processingNode after kernel2: " << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "processing [ " << iCount << " ] : " << processingNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "updatedCost [ " << iCount << " ] : " << updatedCost[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "costToNode [ " << iCount << " ] : " << costToNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < ( mVertices + nEdges ); ++iCount ){ cout << "edgeWeightParallel [ " << iCount << " ] : " << edgeWeightParallel[ iCount ] << endl; } for ( unsigned int i = 0; i < mVertices; ++i ){ if( processingNode[ i ] ) { flagContinue = true; goto repeatLoopTrue; } } flagContinue = false; repeatLoopTrue: continue; } // END GPU PROCESSING LOOP // PRINT RESULTS OF GPU PROCESSING cout << "Parent array: " << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "parentArray[ " << iCount << " ] : " << parentArray[ iCount ] << endl; } cout << "Destination Node\tDistance" << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "\t " << iCount << ":\t\t\t" << costToNode[ iCount ] << endl; } cout << "Finished distance" << endl; int firstHopArray[ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ firstHopArray[ iCount ] = -1; } cout << "initialized firstHop" << endl; int parent ( -1 ); int intervenor ( -1 ); for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "FirstHop: " << iCount << endl; parent = -1; intervenor = -1; // FOR SOURCE NODE if( parentArray[ iCount ] == iCount ){ firstHopArray[ iCount ] = iCount; continue; } // FOR ISOLATED NODE if( parentArray[ iCount ] == -1 ){ cout << iCount << " is isolated from the source node" << endl; continue; } // FOR ALL NON-SOURCE VERTICES - BACK TRACK UNTIL SINGLESOURCEID IS REACHED cout << "Not source: " << iCount << endl; parent = parentArray[ iCount ]; intervenor = iCount; while ( parent != singleSourceId ){ cout << iCount << " has parent: " << parent << endl; intervenor = parent; parent = parentArray[ parent ]; } firstHopArray[ iCount ] = intervenor; } // END FOR LOOP cout << "Destination Node\tFirst Hop Router" << endl; for( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ if( firstHopArray [ iCount ] == -1 ) { continue; } cout << "\t " << iCount << ":\t\t\t" << firstHopArray[ iCount ] << endl; } exit ( 0 ); std::cout << exploringPtr->head << std::endl; std::cout << exploringPtr->head->data.vertexANo << std::endl; std::cout << exploringPtr->head->data.weight << std::endl; int xCount = 0; // EXPLORINGPTR->HEAD IS THE NODE THAT IS BEING SEARCHED FOR THE LEAST COST NEXT HOP while( exploringPtr->head ){ // FIND CLOSEST NODE Node * trav; trav = exploringPtr->head; Node * followNode; Node * preMinNode; followNode = exploringPtr->head; // PREVIOUS NODE ALLOWS LINK BACK // APPROXIMATE INFINITE DISTANCE ( COST ) WEIGHT AS 1000000.0 // FOR EACH NEW SEARCH FOR NEXT SHORTEST THROUGH LINK TO SOURCE float minDist ( 1000000.0 ); Node * minNode; while ( trav ){ std::cout << "Checking for min with edge: " << trav->data.vertexANo << "->" << trav->data.vertexBNo << std::endl; if( distanceToS[ trav->data.vertexANo ] < 0.0 ){ // ERROR CHECK std::cout << "ERROR - Negative distance" << std::endl; } else { // CHECK SOURCE-TO-TRAV + TRAV-TO-TEST EDGE FOR MIN TOTAL COST if ( trav->data.weight + distanceToS[trav->data.vertexANo ] < minDist ) { minDist = trav->data.weight + distanceToS[ trav->data.vertexANo ]; std::cout << "New min is Edge : " << trav->data.vertexANo << "->" << trav->data.vertexBNo << std::endl; std::cout << "weight: " << trav->data.weight << std::endl; minNode = trav; preMinNode = followNode; } } // INCREMENT ITERATORS SO THAT FOLLOWNODE IS IMMEDIATELY BEHIND trav // TRAV ITERATES THROUGH ALL NODES THAT ARE IN EXPLORING QUEUE // EXPLORING QUEUE = // NODE THAT ARE BEING CHECKED AS LINKS BUT CHECKS ARE NOT COMPLETE if ( trav != exploringPtr->head ) followNode = trav; trav = trav->next; std::cout << "end while loop: " << trav << std::endl; } // END WHILE LOOP TO SEARCH FOR THE NEXT SHORTEST THROUGH LINK TO THE SOURCE std::cout << "closest uncatalouged route source to node: " << std::flush; std::cout << minNode->data.vertexANo << "->" << std::flush; std::cout << minNode->data.vertexBNo << ", distance: " << std::flush; std::cout << minDist << "!" << std::endl; // REMOVE CLOSEST NODE FROM EXPLORING QUEUE if ( minNode == exploringPtr->head ){ exploringPtr->head = exploringPtr->head->next; } else { preMinNode->next = minNode->next; } if ( minNode == exploringPtr->tail ){ exploringPtr->tail = preMinNode; } std::cout << "Deleted from exploring queue edge: " << minNode->data.vertexANo << "->" << minNode->data.vertexBNo << std::endl; // PURGE ALL INSTANCES OF REMOVED NODE (THEY ARE GREATER DISTANCE ) trav = exploringPtr->head; followNode = exploringPtr->head; while( trav ){ if ( trav->data.vertexBNo == minNode->data.vertexBNo ){ if( trav == exploringPtr->head ){ exploringPtr->head = exploringPtr->head->next; } else { followNode->next = trav->next; } if ( trav == exploringPtr->tail ){ exploringPtr->tail = followNode; } } // INCREMENT ITERATORS SO THAT FOLLOWNODE IS IMMEDIATELY BEHIND trav if ( trav != exploringPtr->head ) followNode = trav; trav = trav->next; } // ADD REMOVED NODE TO THE EXPLORED QUEUE // IF EXPLORED NODE IS EMPTY MAKE THE FIRST NODE (SINGLE SOURCE ) ROOT if ( !exploredPtr->head ){ exploredPtr->head = root; exploredPtr->tail = root; root->childMapPtr[ singleSourceId ] = root; std::cout << "Adding to explored queue root: " << exploredPtr->head->vertexNo << std::endl; // SET ( LEAVE ) PARENTNODE QUEUE TO NULL parentNodeArray[ exploredPtr->head->vertexNo ] = root; } // IF EXPLORED NODE IS NOT EMPTY ADD A NEW NODE // ADD NEWEST NODE TO TREE else { TreeNode * nwNodePtr = new TreeNode( minNode->data.vertexBNo, minNode->data.weight ); // UPDATE PARENTNODEARRAY FOR NEW EXPLORED NODE parentNodeArray[ nwNodePtr->vertexNo ] = nwNodePtr; // FIND NWNODEPTR'S PARENT NODE FROM PARENTNODEARRAY AND // ADD PARENT PTR TO NEW NODE, THEN ADD CHILD PTR TO PARENT // -node of parent nwNodePtr->parent = parentNodeArray[ minNode->data.vertexANo ]; std::cout << "Parent Node is: " << nwNodePtr->parent->vertexNo << std::endl; // Parent adds ptr to child (new addition) nwNodePtr->parent->childMapPtr[ nwNodePtr->vertexNo ] = nwNodePtr; std::cout << "parent: " << nwNodePtr->parent->vertexNo << " has child in node: " << nwNodePtr->parent->childMapPtr[ nwNodePtr->vertexNo ]->vertexNo << std::endl; TreeNode * tnTrav = nwNodePtr->parent; while( tnTrav->parent ){ tnTrav->parent->childMapPtr[ nwNodePtr->vertexNo ] = tnTrav; std::cout << "parent: " << tnTrav->parent->vertexNo << "has child on route to " << nwNodePtr->vertexNo << " in node: " << tnTrav->vertexNo << std::endl; tnTrav = tnTrav->parent; } // minNode->data.vertexANo as treenode.vertexNo } // END IF ELSE BLOCK // PRINT TREE std::cout << "root: " << root->vertexNo << std::endl; //int iCount = 0; // SET DISTANCE OF NODE ADDED TO EXPLORED QUEUE distanceToS[ minNode->data.vertexBNo ] = minDist; std::cout << "Setting total distance for node: " << minNode->data.vertexBNo << " at " << minDist << std::endl; // PRINT EXPLORING QUEUE std::cout << "Exploring Queue after purge before expansion: "; trav = exploringPtr->head; if ( trav ){ while ( trav ){ std::cout << trav->data.vertexBNo << ", " ; trav = trav->next; } } else { std::cout << "empty queue"; } std::cout << std::endl; // EXPAND CHOSEN NODE IN EXPLORING QUEUE AND ADD TO EXPLORING NODE QUEUE // ADD ALL NODES REACHABLE FROM MINNODE (BUT NOT IN EXPLORED QUEUE ) // TO THE EXPLORING QUEUE // minNode->data trav = EdgeTable[ minNode->data.vertexBNo ]; while( trav ){ if ( distanceToS[ trav->data.vertexBNo ] < 0.0 ){ std::cout << "have entry(ies) to add" << std::endl; if ( !exploringPtr->head ){ exploringPtr->head = trav; exploringPtr->tail = trav; std::cout << "Adding to queue" << std::endl; } else { exploringPtr->tail->next = trav; exploringPtr->tail = trav; } std::cout << "edge: " << trav->data.vertexANo << "->" << trav->data.vertexBNo << std::endl; } trav = trav->next; } // PRINT EXPLORING QUEUE std::cout << "Exploring Queue: "; trav = exploringPtr->head; while ( trav ){ std::cout << trav->data.vertexANo << "->" << trav->data.vertexBNo << ", " ; trav = trav->next; } std::cout << std::endl; // PRINT DISTANCE ARRAY std::cout << "\nNode\tDistance From Source" << std::endl; for( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ std::cout << iCount << ": \t\t" << distanceToS[ iCount ] << std::endl; } // PURGE ALL INSTANCES OF REMOVED NODE (THEY ARE GREATER DISTANCE ) trav = exploringPtr->head; followNode = exploringPtr->head; while( trav ){ if ( distanceToS[ trav->data.vertexBNo ] >= 0.0 ){ if( trav == exploringPtr->head ){ exploringPtr->head = exploringPtr->head->next; } else { followNode->next = trav->next; } if ( trav == exploringPtr->tail ){ exploringPtr->tail = followNode; } } // INCREMENT ITERATORS SO THAT FOLLOWNODE IS IMMEDIATELY BEHIND trav if ( trav != exploringPtr->head ) followNode = trav; trav = trav->next; } ++xCount; } // END WHILE std::cout << "First hop router list: \n" << "Destination Node First Hop Node" << std::endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ std::cout << " " << iCount << " " << root->childMapPtr[ iCount ]->vertexNo << std::endl; } // DELETE POINTERS TO DYNAMIC OBJECTS hipFree ( dev_vertexArrayParallel ); hipFree ( dev_edgeArrayP ); hipFree ( dev_edgeWeightParallel ); hipFree ( dev_processingNode ); hipFree ( dev_costToNode ); hipFree ( dev_updatedCost ); hipFree ( dev_parentArray ); hipFree ( dev_accessedNode ); delete exploringPtr; delete exploredPtr; delete mapPtr; delete root; delete initNode; delete [] edgeWeightParallel; delete [] edgeArrayP; delete [] vertexArrayParallel; delete [] EdgeTable; // for ( unsigned int i = 0; i < mVertices; ++i ){ // delete [] adjacency[ i ]; // } // delete [] adjacency; return 0; }
2c9a6a7281497c8ba853fd547f4ccb678d730014.cu
/******************|********************|*******************|******************| * * * ROUTER ALGORITHM * * * * PREPARED BY: Bennett D. Clarke * * University of Illinois ( Chicago ) * * Chicago, Il. 60610 * * Date: April 17, 2013 * * * ******************************************************************************* * * * OBJECTIVE: To use a data set of routing nodes and to use a parallel * * processor to run a single source shortest path algorithm * * to find the first hop node from the chosen source to any * * of the nodes on the routing topology. The code is in * * Cuda. * * * * METHODOLOGY: Use Dykstra's algorithm with uniform cost of 1 per router * * hop ( 1 per edge ). * ******************************************************************************** * * * DESCRIPTION OF VARIABLES * * * * edgeArrayParallel[ mVertex + nEdges ] Large Array with All vertices * * and their outbound neighbors * * EdgeAPCount Index of edgeArrayParallel for both * * terminus of outbound edges and * * flags for end of linked list * * EdgeTable[ mVertices ] Array of pointers; for each vertex * * the array entry points to a * * linked list of outbound edges * * each of which is a neighbor of * * the indexed vertex * * edgeWeightParallel [ iEdge ] Weight set to 1000000 until reduced * * leavingEdgeNo Count of number of outbound edges * * from a Vertex * * processingNode[ ] * * vertexArrayParallel[ iVertex ] Each index represents corresponding * * vertex and it points to the * * index in the edgeArrayParallel * * where its first neighbor is * * represented * * *******************************************************************************/ #include <iostream> #include <string> #include <fstream> #include <vector> #include <map> #include <cstdlib> //#include <limits> #include <cstdio> #include <new> // CUDA INCLUDES #include <cuda.h> // CUDA BY EXAMPLE CODE #include "../common/book.h" // DECLARATIONS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //#define N 10 // DEFINE FOR THREAD BLOCK NUMBER using std::cout; using std::endl; typedef unsigned int vertex; //CUDA KERNELS __global__ void ssss_1 ( int N, bool * processingNode, float * costToNode , float * updatedCost, int * vertexArrayParallel, int * edgeArrayParallel, float * edgeWeightParallel, int * accessedNode, int * parentArray ) { int tid = blockIdx.x; int nid; if ( tid < N ) { if( processingNode[ tid ] ) { processingNode[ tid ] = false; // Remove node from the exploring group // TEST IF ORIGINATING VERTEX HAS NEIGHBORS OTHER THAN ITS INBOUND // ROUTES FROM THE SOURCE -- -1 MEANS NO OUTBOUND EDGES if ( vertexArrayParallel [ tid ] != -1 ){ // OUTBOUND EDGE TERMINUS IS NID; VAP[ TID ] GIVES THE INDEX IN EAP // WHERE THE OUTBOUND EDGE TERMINUS VERTEX ID CAN BE FOUND int eAPIndex = vertexArrayParallel[ tid ]; nid = edgeArrayParallel [ eAPIndex ]; // THE ARRAY ENTRIES THAT CORRESPOND TO A LINKED LIST OF EDGES // EMANATING FROM THE EXPANDING NODE WILL TERMINATE IN AN ARRAY ENTRY // EQUAL TO -1 -- EACH SEPARATE VALID NID REPRESENTS THE TERMINUS // OF A NEIGHBOR NODE while ( -1 != nid ){ ++accessedNode[ nid ]; if ( updatedCost[ nid ] > costToNode[ tid ] + edgeWeightParallel[ eAPIndex ] ) { updatedCost[ nid ] = costToNode [ tid ] + edgeWeightParallel[ eAPIndex ]; parentArray[ nid ] = tid; } // INCREMENT THE INDEX TO THE EAP BY 1 TO GET NEXT OUTBOUND EDGE // TERMINUS - OTHERWISE KNOWN AS NEIGHBOR //++(vertexArrayParallel[ tid ]); ++eAPIndex; nid = edgeArrayParallel [ eAPIndex ]; } // END WHILE // FINISHED PROCESSING ALL OUTBOUND EDGES TO NEIGHBORS // RETURN DATA TO CPU is coded in cpu section } // END if != NULL } // END IF PROCEESINGNODE } // END IF TID < N } // END KERNEL 1 __global__ void ssss_2 ( int N, bool * processingNode, float * costToNode , float * updatedCost, int * vertexArrayParallel, int * edgeArrayParallel, float * edgeWeightParallel ) { int tid = blockIdx.x; if ( tid < N ) { if( costToNode[ tid ] > updatedCost[ tid ] ) { costToNode[ tid ] = updatedCost[ tid ]; processingNode[ tid ] = true; } // END UPDATE PATH TO NODE COST else { updatedCost[ tid ] = costToNode[ tid ]; } } // END IF tid is valid } // END KERNEL 2 // VARIABLES bool undirectedGraphFlag ( false ); // A LINE OF DATA FROM INPUT FILE CONSISTING OF 2 VERTICES AND THE COST OF A TRAVERSAL // FROM VERTEX A TO VERTEX B struct DataLineIn{ public: vertex vertexANo; vertex vertexBNo; float weight; DataLineIn( vertex initVertexANo, vertex initVertexBNo, float initWeight ) : vertexANo( initVertexANo ), vertexBNo( initVertexBNo ), weight( initWeight ) {} }; // NODE CONSISTING OF A LINE OF DATA ( VERTEX A, VERTEX B, WEIGHT ) AND PTR struct Node{ DataLineIn data; Node * next; Node( DataLineIn initData ) : data ( initData ) { next = NULL; } Node( DataLineIn initData, Node * initNext ) : data ( initData ), next ( initNext ) {} Node( vertex initVertexANo, vertex initVertexBNo, float initWeight ) : data( initVertexANo, initVertexBNo, initWeight), next ( NULL ) { std::cout << "Detailed constructor" << std::endl; } }; struct EdgeQueue{ Node * head; Node * tail; EdgeQueue( ) { head = NULL; tail = NULL; } }; struct TreeNode{ vertex vertexNo; float distance; std::map < vertex, TreeNode * > childMapPtr; TreeNode * parent; TreeNode( vertex initVertexNo, float initDistance ) : vertexNo ( initVertexNo ), distance ( initDistance ) { parent = NULL; } }; struct Queue{ TreeNode * head; TreeNode * tail; Queue( ) { head = NULL; tail = NULL; } }; // VECTOR CONSISTING OF DATA IN ENTRIES ( VERTEX A, VERTEX B, WEIGHT ) std::vector < DataLineIn * > DataIn; // FUNCTIONS // PROVIDES USAGE static void show_usage(std::string fileName) { std::cerr << "Running RouterAlgorithmArt.cu\n" "Usage: " << fileName << " <option(s)> DATA INPUT FILE " << "Options:\n" << "\t-h,--help\t\tShow this help message\n" << "\t-d,--data_file_path \tSpecify the data inputs path" << "\t-u,--undirected graph input option" << std::endl; } int main ( int argc, char ** argv ){ if ( argc < 3 ) { show_usage( argv[ 0 ] ); return 1; } std::string dataIn_Name = argv[ 1 ]; for ( int i = 1; i < argc; ++i ) { std::string arg = argv[ i ]; if ( ( arg == "-h" ) || ( arg == "--help" ) ) { show_usage( argv[ 0 ] ); return 0; } else if ( arg == "-u" ){ undirectedGraphFlag = true; } else if ( ( arg == "-d") || ( arg == "--data_file_path" ) ) { if ( i + 1 < argc ) { dataIn_Name = argv[ ++i ]; } else { std::cerr << "--data_file_path option requires one argument." << std::endl; return 1; } } } // END FOR LOOP std::cout << "datafilename is: " << dataIn_Name << std::endl; unsigned int mV ( 0 ); // TO INPUT NUMBER OF VERTICES unsigned int nE ( 0 ); // TO INPUT NUMBER OF EDGES int a, b; // INTEGER REPRESENTATION OF VERTICES double c; // DOUBLE REPRESENTATION OF WEIGHT std::string line; std::ifstream dataIn; // STREAM TO INPUT DATAFILE // PROCESS INPUT FILE INTO VECTOR OF ENTRIES (VERTEXA, VERTEXB, WEIGHT) dataIn.open ( dataIn_Name.c_str() ); if ( dataIn.is_open() ) { // READ FIRST LINE OF DATA WITH SIZE OF GRAPH AND PRINT dataIn >> mV >> nE; // UNDIRECTED GRAPH OPTION if ( undirectedGraphFlag == true ){ nE *= 2; } std::cout << "mVertices: " << mV << ", nEdges: " << nE << std::endl; // PROCESS BODY OF DATA unsigned int dataCount ( 0 ); while ( dataCount < nE ) { // READ A LINE OF DATA FROM FILE AND PRINT IT dataIn >> a >> b >> c; std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl; DataLineIn *ptr = new DataLineIn( a, b, c ); // Enqueue new data element ptr to vector DataIn.push_back( ptr ); // UNDIRECTED GRAPH OPTION if ( undirectedGraphFlag == true ) { cout << "a: " << b << ", b: " << a << ", c: " << c << endl; DataLineIn *ptr = new DataLineIn( b, a, c ); // ENQUEUE DataIn.push_back( ptr ); ++dataCount; } // INCREMENT FOR NEXT DATA LINE INPUT ++dataCount; } dataIn.close(); std::cout << "DataIn contains " << DataIn.size() << " records." << std::endl; } // IF FILE FAILS TO OPEN else std::cout << "Unable to open file"; // CREATE CONST UNSIGNED INT TO USE FOR DECLARATION OF ARRAYS // DOES NOT WORK HERE const unsigned int mVertices ( mV ); // NUMBER OF VERTICES IN GRAPH const unsigned int nEdges ( nE ); // NUMBER OF EDGES IN GRAPH // INITIALIZE 2D DYNAMIC ADJACENCY TABLE TO AN INFINITE ( 1000000 ) VALUE // std::cout << "Create adjacency table " << std::endl; // float ** adjacency = new float* [ mVertices ]; // for ( unsigned int i = 0; i < mVertices; ++i ){ // adjacency[ mVertices ] = new float[ mVertices ]; // } // // std::cout << "Initialize Adjacency Table " << std::endl; // for ( unsigned int iCount = 0; iCount < mVertices; iCount++ ){ // for ( unsigned int jCount = 0; jCount < mVertices; jCount++ ){ // std::cout << "at iter i,j, " << iCount << "," << jCount << "," << std::flush; // //std::cout << adjacency[ iCount ][ jCount ] << std::endl; // // adjacency[ iCount ][ jCount ] = 1000000.0; // } // } // CREATE EDGE TABLE AND INITIALIZE std::cout << "Create and initialize Edge Table " << " Size of node* " << sizeof( Node ** ) << std::endl; Node ** EdgeTable = new Node * [ mVertices ]; // std::cout << "Error in creating EdgeTable array! " << std::endl; // INITIALIZE ARRAY OF NODE POINTERS TO NULL // Node * EdgeTable [ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ EdgeTable [ iCount ] = NULL; } // LOAD GRAPH DATA INTO ADJACENCY MATRIX std::cout << "Loading data into adjacency table " << std::cout; unsigned int kCount = 0; // while( kCount < DataIn.size() ){ // adjacency[ DataIn[ kCount ]->vertexANo ][ DataIn[ kCount ]->vertexBNo ] = // DataIn[ kCount ]->weight; // ++kCount; // } // END WHILE // PRINT ADJACENCY TABLE // std::cout << "\nAdjacency Matrix:" << std::endl; // std::cout << " "; // for( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ // std::cout << " v" << iCount; // } // std::cout << "Not used with CUDA" << std::endl; // for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ // std::cout << "v" << iCount; // for( unsigned int jCount = 0; jCount < mVertices; ++jCount ){ // if( adjacency[ iCount ][ jCount ] > 999999.9 ) // std::cout << " " << "-" ; // else // std::cout << " " << adjacency[ iCount ][ jCount ]; // } // std::cout << std::endl; // } kCount = 0; while ( kCount < DataIn.size() ){ // EDGE TABLE -- EACH DATA LINE OF THE INPUT ( AN EDGE ) IS PLACED IN A NODE // THE NODE IS THEN INSERTED IN A LINKED LIST THAT IS UNIQUE FOR EACH VERTEX // DATA RECORDS NUMBER IS KCOUNT; EDGETABLE ARRAY 1 FOR EACH VERTEX // NOTE AUTHORS USE A SINGLE EDGE ARRAY WHERE THE LINKED LIST ELEMENTS ARE // HELD AS CONTIGUOUS ARRAY MEMBERS DataLineIn dat = *DataIn[ kCount ]; Node * nPtr = new Node( dat ); Node * traveller; // IF THE EDGE TABLE ENTRY HAS NO LINKED LIST - ADD AS FIRST NODE if ( !EdgeTable[ DataIn[ kCount ]->vertexANo ] ){ EdgeTable[ DataIn[ kCount ]->vertexANo ] = nPtr; } else // ELSE ADD AT END OF LIST { traveller = EdgeTable[ DataIn[ kCount ]->vertexANo ]; while ( traveller->next ){ traveller = traveller->next; } traveller->next = nPtr; } // END IF-ELSE BLOCKS ++kCount; } // END WHILE // PRINT EDGE TABLE std::cout << "Edge Table:" << std::endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ std::cout << iCount << ": " ; Node * traveller; traveller = EdgeTable[ iCount ]; while ( traveller ){ std::cout << iCount << "->" << traveller->data.vertexBNo << " " ; traveller = traveller->next; } std::cout << std::endl; } //////////////////// CREATE EDGE ADJACENCY ARRAY FOR CUDA ///////////////////// // SIZE OF EXTENDED ADJACENCY TABLE IS SAME AS NUMBER OF DATA RECORDS int * vertexArrayParallel = new int [ mVertices ]; // Each index represents its // respectively numbered vertex // the value is the EdgeArray index // of the terminus vertex of // the first edge in the linked list // Represents edge: index->Edarray[value] // INITIALIZE THE VERTEXARRAYPARALLEL ENTRIES TO -1 WHICH WILL REPRESENT // A VERTEX WITHOUT OUTBOUND EDGES for ( unsigned int iVertex = 0; iVertex < mVertices; ++iVertex ){ vertexArrayParallel [ iVertex ] = -1; } // PRINT OUT ARRAY VALUES // for ( unsigned int iVertex = 0; iVertex < mVertices; ++iVertex ){ // std::cout << iVertex << ": " << vertexArrayParallel[ iVertex ] << std::endl; // } // CREATE AN ARRAY THAT WILL PROVIDE ONE ENTRY FOR EVERY OUT EDGE AND EVERY VERTEX // INITIALIZE TO -1 -- -1 WILL BE A FLAG TO REPRESENT THE END OF A LINKED LIST // OF OUTBOUND EDGES int * edgeArrayP = new int [ nEdges + mVertices ]; float * edgeWeightParallel = new float [ nEdges + mVertices ]; // INITIALIZE LARGE PARALLEL ARRAY ( edgeArrayP ) WITH -1 unsigned int iEdge ( 0 ); for ( iEdge = 0; iEdge < (nEdges + mVertices); ++iEdge ){ edgeArrayP[ iEdge ] = -1; } unsigned int i ( 0 ); cout << "After initialization the edgeArrayP:" << endl; for( i = 0; i < (nEdges + mVertices); ++i ){ std::cout << "edgeArrayP[ " << i << " ] : " << edgeArrayP[ i ] << ", " << std::endl; } // INITIALIZE ARRAY OF WEIGHTS TO INFINITE = 1000000 // NOTE THAT INDICES MUST MATCH THOSE OF THE EDGEARRAYP SO THAT THERE ARE // EDGE + VERTICES ENTRIES AND SOME REMAIN 1000000 AS FLAGS for ( iEdge = 0; iEdge < ( nEdges + mVertices ); ++iEdge ){ edgeWeightParallel [ iEdge ] = 1000000; } cout << "edgeWeightParallel[ each edge ] array as initialized " << endl; for( i = 0; i < ( nEdges + mVertices ); ++i ){ std::cout << "edgeWeightParallel[ " << i << " ] : " << edgeWeightParallel[ i ] << ", " << std::endl; } // LOAD DATA int EdgeAPCount ( 0 ); // Index in EdgeArrayParallel std::cout << "Loading data to parallel arrays" << std::endl; for ( unsigned int iVertex = 0; iVertex < mVertices; ++iVertex ){ Node * traveller; traveller = EdgeTable[ iVertex ]; // Each table entry is head of linked list int leavingEdgeNo ( 0 ); // Initialize number of outbound edges from V // FOR EACH OUTBOUND EDGE IN A LINKED LIST - TRAVELLER POINTS TO THE EDGE // THAT EMANATES FROM VERTEX NUMBERED IVERTEX while ( traveller ){ // LEADINGEDGENO == 0 INDICATES THAT THIS IS THE FIRST OUTBOUND EDGE // FROM A SPECIFIC EMANATING VERTEX if ( ! leavingEdgeNo ) { // USE POINTER FOR FIRST EDGE IN EACH VERTICE'S LINKED LIST std::cout << "iVertex: " << iVertex << std::endl; // SET POINTER AND ITERATE FOR FIRST ELEMENT IN LINKED LIST std::cout << "Head of a list: " << std::flush; //vertexArrayParallel [ iVertex ] = &edgeArrayParallel[ EdgeAPCount ]; vertexArrayParallel [ iVertex ] = EdgeAPCount; std::cout << "vertexA<index> to edgeTable<index>: " << "Vertex no: " << iVertex << " maps to " << vertexArrayParallel [ iVertex ] << " in the large array" << std::endl; ++leavingEdgeNo; } // RECORD B (A->B) VERTEX IN EDGEARRAY FOR ALL NODES edgeArrayP[ EdgeAPCount ] = traveller->data.vertexBNo; edgeWeightParallel[ EdgeAPCount ] = traveller->data.weight; std::cout << "General node data points to vertex: " << "edgeArrayP[ " << EdgeAPCount << " ]: " << edgeArrayP[ EdgeAPCount ] << " weight is " << edgeWeightParallel[ EdgeAPCount ] << std::endl; ++EdgeAPCount; // ITERATE ON TRAVELLER IN LINKED LIST traveller = traveller->next; // for( unsigned int i = 0; i < EdgeAPCount; ++i ){ // std::cout << "index : " << i << " is " << edgeArrayP[ i ] // << ", " << std::endl; // } } // END WHILE LOOP // for loop will increment to next vertex // PROVIDE A FLAG ( -1 ) TO SEPARATE ELEMENTS BY THEIR INITIATION VERTEX // A OF ( A->B ) cout << "\t\t\tSentinel value: " << "edgeArrayP[ " << EdgeAPCount << " ]: " << edgeArrayP[ EdgeAPCount ] << std::endl; ++EdgeAPCount; } // END FOR LOOP // CHECK ARRAYS for( int i = 0; i < EdgeAPCount; ++i ){ std::cout << "edgeArrayP[ " << i << " ] : " << edgeArrayP[ i ] << ", " << std::endl; } for( i = 0; i < ( EdgeAPCount ); ++i ){ std::cout << "index : " << i << " is " << edgeWeightParallel[ i ] << ", " << std::endl; } std::cout << "Declaring device data structures" << std::endl; int * dev_vertexArrayParallel; int * dev_edgeArrayP; float * dev_edgeWeightParallel; cudaMalloc( ( void** ) &dev_vertexArrayParallel, mVertices * sizeof( int * ) ); cudaMalloc( ( void** ) &dev_edgeArrayP, (nEdges + mVertices) * sizeof( int ) ); cudaMalloc( ( void** ) &dev_edgeWeightParallel, ( nEdges + mVertices ) * sizeof( float ) ); //////////////////////////////////////////////////////////////////////////////// // TRANSFORM TO ARRAY OF VERTICES AT HEAD OF LINKED LISTS int singleSourceId ( -1 ); std::cout << "Enter integer identifier of single source node: " << std::flush; std::cin >> singleSourceId; cout << "Received data: " << singleSourceId << endl; // CREATE A SINGLE SOURCE DATALINE NODE - THIS POINTS TO ITSELF WITH WEIGHT 0 float sourceWeight ( 0 ); //DataLineIn * initDataLine = new DataLineIn( singleSourceId, singleSourceId, // sourceWeight ); Node * initNode = new Node( singleSourceId, singleSourceId, sourceWeight ); std::cout << "SS.weight: " << initNode->data.weight << std::endl; ///////////////// DATA STRUCTURES FOR PARALLEL PROCESSING ///////////////////////// // CREATE A BOOL ARRAY TO HOLD FLAG FOR NODES BEING PROCESSED bool processingNode[ mVertices ]; // CREATE A COST ARRAY AS THE COST OF REACHING EACH NODE FROM THE SOURCE float costToNode[ mVertices ]; // CREATE A UPDATE COST ARRAY AS THE TEMPORARILY CALCULATED LEAST COST TO // EACH NODE FROM THE SOURCE float updatedCost[ mVertices ]; // INITIALIZE THE ARRAYS // FALSE FOR NOT CURRENTLY EXAMINED // 1000000.00 TO APPROXIMATE INFINITE COST OF AN UNCONNECTED VERTEX for ( unsigned int ii = 0; ii < mVertices; ++ii ){ processingNode [ ii ] = false; updatedCost[ ii ] = costToNode[ ii ] = 1000000.00; } for ( unsigned int ii = 0; ii < mVertices; ++ii ){ cout << "costToNode[ " << ii << " ] : " << costToNode[ ii ] << endl; } // CREATE PARENT ARRAY TO INDICATE PARENT OF ATTACHED NODE// INITIALIZE TO -1 int parentArray[ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ parentArray[ iCount ] = -1; } // CREATE CORRESPONDING ARRAYS FOR GPU AND CUDA bool *dev_processingNode; float *dev_costToNode; float *dev_updatedCost; int *dev_parentArray; // // ALLOCATE MEMORY ON GPU cudaMalloc( ( void** ) &dev_processingNode, mVertices * sizeof( bool ) ); cudaMalloc( ( void** ) &dev_costToNode, mVertices * sizeof( float ) ); cudaMalloc( ( void** ) &dev_updatedCost, mVertices * sizeof( float ) ); cudaMalloc( ( void** ) &dev_parentArray, mVertices * sizeof( int ) ); std::cout << "parallel processing arrays initialized" << std::endl; ////////////////////////// END COST ARRAYS ////////////////////////////////////// // CREATE A ROOT NODE FOR USE IN THE EXPLORED QUEUE ( element is the source // parent is null ) // TreeNode has elements: vertexNo, distance to source, ptr to map of children, // ptr to parent TreeNode * root = new TreeNode( singleSourceId, 0.0 ); root->parent = NULL; // CREATE A MAP WITH VERTEX NO = KEY; TREE NODE * = VALUE std::map< vertex, TreeNode* > * mapPtr = new std::map<vertex, TreeNode* >(); root->childMapPtr = *mapPtr; // SET EXPLORED NODE QUEUE // ANALOG TO GPU SETTING BOOL TO FALSE Queue *exploredPtr = new Queue(); // INITIALIZE ARRAY TO HOLD DISTANCE FROM SOURCE TO EACH NODE ID // CPU ANALOG OF COSTTONODE float distanceToS [ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ distanceToS[ iCount ] = -1.0; } // ENTER 0 DISTANCE FOR THE SOURCE NODE distanceToS[ singleSourceId ] = 0.0; // SET ARRAY "PARENT NODE ARRAY' TO HOLD PTR TO PARENT'S TREENODE TreeNode * parentNodeArray[ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ parentNodeArray[ iCount ] = NULL; } // SET PARENTARRAY FOR GPU CALCULATIONS parentArray[ singleSourceId ] = singleSourceId; //INITIALIZE THE EXPLORING QUEUE WITH THE SINGLE SOURCE EdgeQueue *exploringPtr = new EdgeQueue(); exploringPtr->head = initNode; std::cout << "adding to exploring queue: " << exploringPtr->head->data.vertexANo << std::endl; //////////////////////////// DIAGNOSTIC STRUCTURES ///////////////////////////////// int accessedNode[ mVertices ]; int accessedNodeReply[ mVertices ]; int *dev_accessedNode; for( int iCount = 0; iCount < mVertices; ++iCount ) { accessedNode[ iCount ] = accessedNodeReply[ iCount ] = 0; } cudaMalloc( ( void** ) &dev_accessedNode, mVertices * sizeof( int ) ); //////////////////////// GPU INITIALIZATION WITH SINGLE SOURCE ///////////////////// processingNode[ singleSourceId ] = true; costToNode[ singleSourceId ] = 0.0; updatedCost[ singleSourceId ] = 0.0; //////////////////////// START GPU PROCESSING LOOP /////////////////////////////////// cudaMemcpy( dev_vertexArrayParallel, vertexArrayParallel, mVertices * sizeof( int ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_edgeArrayP, edgeArrayP, ( mVertices + nEdges ) * sizeof( int ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_edgeWeightParallel, edgeWeightParallel, ( mVertices + nEdges ) * sizeof( float ), cudaMemcpyHostToDevice ); bool flagContinue ( true ); while ( flagContinue ){ // COPY INITIAL DATA TO GPU cudaMemcpy( dev_processingNode, processingNode, mVertices * sizeof( bool ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_costToNode, costToNode, mVertices * sizeof( float ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_updatedCost, updatedCost, mVertices * sizeof( float ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_parentArray, parentArray, mVertices * sizeof( int ), cudaMemcpyHostToDevice ); // DIAGNOSTIC FUNCTION cudaMemcpy( dev_accessedNode, accessedNode, mVertices * sizeof( int ), cudaMemcpyHostToDevice ); cout << "Copied data to gpu " << endl; // EXECUTE KERNEL ON GPU ssss_1<<< mVertices, 1>>> ( mVertices, dev_processingNode, dev_costToNode, dev_updatedCost, dev_vertexArrayParallel, dev_edgeArrayP, dev_edgeWeightParallel, dev_accessedNode, dev_parentArray ); cout << "Processed kernel1 " << endl; // RETURN PROCESSED DATA FROM GPU HANDLE_ERROR( cudaMemcpy( updatedCost, dev_updatedCost, mVertices * sizeof( float ), cudaMemcpyDeviceToHost ) ); cout << "copied updatedCost " << endl; // exit ( 0 ); cudaMemcpy( processingNode, dev_processingNode, mVertices * sizeof( bool ), cudaMemcpyDeviceToHost ); cudaMemcpy( parentArray, dev_parentArray, mVertices * sizeof( int ), cudaMemcpyDeviceToHost ); // DIAGNOSTIC STRUCTURE cudaMemcpy( accessedNodeReply, dev_accessedNode, mVertices* sizeof( int ), cudaMemcpyDeviceToHost ); ///////////////////// END KERNEL 1 /////////////////////////////////////////// cout << "End kernel 1" << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "processing [ " << iCount << " ] : " << processingNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "updatedCost [ " << iCount << " ] : " << updatedCost[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "costToNode [ " << iCount << " ] : " << costToNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "accessedNodeReply [ " << iCount << " ] : " << accessedNodeReply[ iCount ] << endl; } // exit ( 0 ); ///////////////////// KERNEL 2 //////////////////////////////////////////////////// // COPY INITIAL DATA TO GPU cudaMemcpy( dev_processingNode, processingNode, mVertices * sizeof( bool ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_costToNode, costToNode, mVertices * sizeof( float ), cudaMemcpyHostToDevice ); cudaMemcpy( dev_updatedCost, updatedCost, mVertices * sizeof( float ), cudaMemcpyHostToDevice ); // EXECUTE KERNEL ON GPU ssss_2<<< mVertices, 1>>> ( mVertices, dev_processingNode, dev_costToNode, dev_updatedCost, dev_vertexArrayParallel, dev_edgeArrayP, dev_edgeWeightParallel ); // RETURN PROCESSED DATA FROM GPU cudaMemcpy( costToNode, dev_costToNode, mVertices * sizeof( float ), cudaMemcpyDeviceToHost ); cudaMemcpy( updatedCost, dev_updatedCost, mVertices * sizeof( float ), cudaMemcpyDeviceToHost ); cudaMemcpy( processingNode, dev_processingNode, mVertices * sizeof( bool ), cudaMemcpyDeviceToHost ); // cudaMemcpy( parentArray, dev_parentArray, mVertices * sizeof( int ), // cudaMemcpyDeviceToHost ); cout << "End kernel2" << endl; ////////////////////////// END KERNEL 2 //////////////////////////////////////////// cout << "processingNode after kernel2: " << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "processing [ " << iCount << " ] : " << processingNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "updatedCost [ " << iCount << " ] : " << updatedCost[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "costToNode [ " << iCount << " ] : " << costToNode[ iCount ] << endl; } for ( unsigned int iCount = 0; iCount < ( mVertices + nEdges ); ++iCount ){ cout << "edgeWeightParallel [ " << iCount << " ] : " << edgeWeightParallel[ iCount ] << endl; } for ( unsigned int i = 0; i < mVertices; ++i ){ if( processingNode[ i ] ) { flagContinue = true; goto repeatLoopTrue; } } flagContinue = false; repeatLoopTrue: continue; } // END GPU PROCESSING LOOP // PRINT RESULTS OF GPU PROCESSING cout << "Parent array: " << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "parentArray[ " << iCount << " ] : " << parentArray[ iCount ] << endl; } cout << "Destination Node\tDistance" << endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "\t " << iCount << ":\t\t\t" << costToNode[ iCount ] << endl; } cout << "Finished distance" << endl; int firstHopArray[ mVertices ]; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ firstHopArray[ iCount ] = -1; } cout << "initialized firstHop" << endl; int parent ( -1 ); int intervenor ( -1 ); for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ cout << "FirstHop: " << iCount << endl; parent = -1; intervenor = -1; // FOR SOURCE NODE if( parentArray[ iCount ] == iCount ){ firstHopArray[ iCount ] = iCount; continue; } // FOR ISOLATED NODE if( parentArray[ iCount ] == -1 ){ cout << iCount << " is isolated from the source node" << endl; continue; } // FOR ALL NON-SOURCE VERTICES - BACK TRACK UNTIL SINGLESOURCEID IS REACHED cout << "Not source: " << iCount << endl; parent = parentArray[ iCount ]; intervenor = iCount; while ( parent != singleSourceId ){ cout << iCount << " has parent: " << parent << endl; intervenor = parent; parent = parentArray[ parent ]; } firstHopArray[ iCount ] = intervenor; } // END FOR LOOP cout << "Destination Node\tFirst Hop Router" << endl; for( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ if( firstHopArray [ iCount ] == -1 ) { continue; } cout << "\t " << iCount << ":\t\t\t" << firstHopArray[ iCount ] << endl; } exit ( 0 ); std::cout << exploringPtr->head << std::endl; std::cout << exploringPtr->head->data.vertexANo << std::endl; std::cout << exploringPtr->head->data.weight << std::endl; int xCount = 0; // EXPLORINGPTR->HEAD IS THE NODE THAT IS BEING SEARCHED FOR THE LEAST COST NEXT HOP while( exploringPtr->head ){ // FIND CLOSEST NODE Node * trav; trav = exploringPtr->head; Node * followNode; Node * preMinNode; followNode = exploringPtr->head; // PREVIOUS NODE ALLOWS LINK BACK // APPROXIMATE INFINITE DISTANCE ( COST ) WEIGHT AS 1000000.0 // FOR EACH NEW SEARCH FOR NEXT SHORTEST THROUGH LINK TO SOURCE float minDist ( 1000000.0 ); Node * minNode; while ( trav ){ std::cout << "Checking for min with edge: " << trav->data.vertexANo << "->" << trav->data.vertexBNo << std::endl; if( distanceToS[ trav->data.vertexANo ] < 0.0 ){ // ERROR CHECK std::cout << "ERROR - Negative distance" << std::endl; } else { // CHECK SOURCE-TO-TRAV + TRAV-TO-TEST EDGE FOR MIN TOTAL COST if ( trav->data.weight + distanceToS[trav->data.vertexANo ] < minDist ) { minDist = trav->data.weight + distanceToS[ trav->data.vertexANo ]; std::cout << "New min is Edge : " << trav->data.vertexANo << "->" << trav->data.vertexBNo << std::endl; std::cout << "weight: " << trav->data.weight << std::endl; minNode = trav; preMinNode = followNode; } } // INCREMENT ITERATORS SO THAT FOLLOWNODE IS IMMEDIATELY BEHIND trav // TRAV ITERATES THROUGH ALL NODES THAT ARE IN EXPLORING QUEUE // EXPLORING QUEUE = // NODE THAT ARE BEING CHECKED AS LINKS BUT CHECKS ARE NOT COMPLETE if ( trav != exploringPtr->head ) followNode = trav; trav = trav->next; std::cout << "end while loop: " << trav << std::endl; } // END WHILE LOOP TO SEARCH FOR THE NEXT SHORTEST THROUGH LINK TO THE SOURCE std::cout << "closest uncatalouged route source to node: " << std::flush; std::cout << minNode->data.vertexANo << "->" << std::flush; std::cout << minNode->data.vertexBNo << ", distance: " << std::flush; std::cout << minDist << "!" << std::endl; // REMOVE CLOSEST NODE FROM EXPLORING QUEUE if ( minNode == exploringPtr->head ){ exploringPtr->head = exploringPtr->head->next; } else { preMinNode->next = minNode->next; } if ( minNode == exploringPtr->tail ){ exploringPtr->tail = preMinNode; } std::cout << "Deleted from exploring queue edge: " << minNode->data.vertexANo << "->" << minNode->data.vertexBNo << std::endl; // PURGE ALL INSTANCES OF REMOVED NODE (THEY ARE GREATER DISTANCE ) trav = exploringPtr->head; followNode = exploringPtr->head; while( trav ){ if ( trav->data.vertexBNo == minNode->data.vertexBNo ){ if( trav == exploringPtr->head ){ exploringPtr->head = exploringPtr->head->next; } else { followNode->next = trav->next; } if ( trav == exploringPtr->tail ){ exploringPtr->tail = followNode; } } // INCREMENT ITERATORS SO THAT FOLLOWNODE IS IMMEDIATELY BEHIND trav if ( trav != exploringPtr->head ) followNode = trav; trav = trav->next; } // ADD REMOVED NODE TO THE EXPLORED QUEUE // IF EXPLORED NODE IS EMPTY MAKE THE FIRST NODE (SINGLE SOURCE ) ROOT if ( !exploredPtr->head ){ exploredPtr->head = root; exploredPtr->tail = root; root->childMapPtr[ singleSourceId ] = root; std::cout << "Adding to explored queue root: " << exploredPtr->head->vertexNo << std::endl; // SET ( LEAVE ) PARENTNODE QUEUE TO NULL parentNodeArray[ exploredPtr->head->vertexNo ] = root; } // IF EXPLORED NODE IS NOT EMPTY ADD A NEW NODE // ADD NEWEST NODE TO TREE else { TreeNode * nwNodePtr = new TreeNode( minNode->data.vertexBNo, minNode->data.weight ); // UPDATE PARENTNODEARRAY FOR NEW EXPLORED NODE parentNodeArray[ nwNodePtr->vertexNo ] = nwNodePtr; // FIND NWNODEPTR'S PARENT NODE FROM PARENTNODEARRAY AND // ADD PARENT PTR TO NEW NODE, THEN ADD CHILD PTR TO PARENT // -node of parent nwNodePtr->parent = parentNodeArray[ minNode->data.vertexANo ]; std::cout << "Parent Node is: " << nwNodePtr->parent->vertexNo << std::endl; // Parent adds ptr to child (new addition) nwNodePtr->parent->childMapPtr[ nwNodePtr->vertexNo ] = nwNodePtr; std::cout << "parent: " << nwNodePtr->parent->vertexNo << " has child in node: " << nwNodePtr->parent->childMapPtr[ nwNodePtr->vertexNo ]->vertexNo << std::endl; TreeNode * tnTrav = nwNodePtr->parent; while( tnTrav->parent ){ tnTrav->parent->childMapPtr[ nwNodePtr->vertexNo ] = tnTrav; std::cout << "parent: " << tnTrav->parent->vertexNo << "has child on route to " << nwNodePtr->vertexNo << " in node: " << tnTrav->vertexNo << std::endl; tnTrav = tnTrav->parent; } // minNode->data.vertexANo as treenode.vertexNo } // END IF ELSE BLOCK // PRINT TREE std::cout << "root: " << root->vertexNo << std::endl; //int iCount = 0; // SET DISTANCE OF NODE ADDED TO EXPLORED QUEUE distanceToS[ minNode->data.vertexBNo ] = minDist; std::cout << "Setting total distance for node: " << minNode->data.vertexBNo << " at " << minDist << std::endl; // PRINT EXPLORING QUEUE std::cout << "Exploring Queue after purge before expansion: "; trav = exploringPtr->head; if ( trav ){ while ( trav ){ std::cout << trav->data.vertexBNo << ", " ; trav = trav->next; } } else { std::cout << "empty queue"; } std::cout << std::endl; // EXPAND CHOSEN NODE IN EXPLORING QUEUE AND ADD TO EXPLORING NODE QUEUE // ADD ALL NODES REACHABLE FROM MINNODE (BUT NOT IN EXPLORED QUEUE ) // TO THE EXPLORING QUEUE // minNode->data trav = EdgeTable[ minNode->data.vertexBNo ]; while( trav ){ if ( distanceToS[ trav->data.vertexBNo ] < 0.0 ){ std::cout << "have entry(ies) to add" << std::endl; if ( !exploringPtr->head ){ exploringPtr->head = trav; exploringPtr->tail = trav; std::cout << "Adding to queue" << std::endl; } else { exploringPtr->tail->next = trav; exploringPtr->tail = trav; } std::cout << "edge: " << trav->data.vertexANo << "->" << trav->data.vertexBNo << std::endl; } trav = trav->next; } // PRINT EXPLORING QUEUE std::cout << "Exploring Queue: "; trav = exploringPtr->head; while ( trav ){ std::cout << trav->data.vertexANo << "->" << trav->data.vertexBNo << ", " ; trav = trav->next; } std::cout << std::endl; // PRINT DISTANCE ARRAY std::cout << "\nNode\tDistance From Source" << std::endl; for( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ std::cout << iCount << ": \t\t" << distanceToS[ iCount ] << std::endl; } // PURGE ALL INSTANCES OF REMOVED NODE (THEY ARE GREATER DISTANCE ) trav = exploringPtr->head; followNode = exploringPtr->head; while( trav ){ if ( distanceToS[ trav->data.vertexBNo ] >= 0.0 ){ if( trav == exploringPtr->head ){ exploringPtr->head = exploringPtr->head->next; } else { followNode->next = trav->next; } if ( trav == exploringPtr->tail ){ exploringPtr->tail = followNode; } } // INCREMENT ITERATORS SO THAT FOLLOWNODE IS IMMEDIATELY BEHIND trav if ( trav != exploringPtr->head ) followNode = trav; trav = trav->next; } ++xCount; } // END WHILE std::cout << "First hop router list: \n" << "Destination Node First Hop Node" << std::endl; for ( unsigned int iCount = 0; iCount < mVertices; ++iCount ){ std::cout << " " << iCount << " " << root->childMapPtr[ iCount ]->vertexNo << std::endl; } // DELETE POINTERS TO DYNAMIC OBJECTS cudaFree ( dev_vertexArrayParallel ); cudaFree ( dev_edgeArrayP ); cudaFree ( dev_edgeWeightParallel ); cudaFree ( dev_processingNode ); cudaFree ( dev_costToNode ); cudaFree ( dev_updatedCost ); cudaFree ( dev_parentArray ); cudaFree ( dev_accessedNode ); delete exploringPtr; delete exploredPtr; delete mapPtr; delete root; delete initNode; delete [] edgeWeightParallel; delete [] edgeArrayP; delete [] vertexArrayParallel; delete [] EdgeTable; // for ( unsigned int i = 0; i < mVertices; ++i ){ // delete [] adjacency[ i ]; // } // delete [] adjacency; return 0; }
981e6fb3340d6555aa7e3f8da49543578d209f08.hip
// !!! This is a file automatically generated by hipify!!! /* ** The MIT License (MIT) ** ** Copyright (c) 2014, Erick Lavoie, Faiz Khan, Sujay Kathrotia, Vincent ** Foley-Bourgon, Laurie Hendren ** ** Permission is hereby granted, free of charge, to any person obtaining a copy **of this software and associated documentation files (the "Software"), to deal ** in the Software without restriction, including without limitation the rights ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ** copies of the Software, and to permit persons to whom the Software is ** furnished to do so, subject to the following conditions: ** ** The above copyright notice and this permission notice shall be included in all ** copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ** SOFTWARE. ** **/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include <getopt.h> #include <chrono> #include <hip/hip_runtime.h> #define D_FACTOR (0.85) #ifndef BLOCK_SIZE #define BLOCK_SIZE 256 #endif // default values const int max_iter = 1000; const float threshold= 0.00001f; __global__ void map( int *pages, float *page_ranks, float *maps, unsigned int *noutlinks, int n) { int i = threadIdx.x + blockIdx.x * blockDim.x; int j; if(i < n){ float outbound_rank = page_ranks[i]/(float)noutlinks[i]; for(j=0; j<n; ++j){ maps[i*n+j] = pages[i*n+j]*outbound_rank; } } } __global__ void reduce( float *page_ranks, float *maps, int n, float *dif) { int j = threadIdx.x + blockIdx.x * blockDim.x; int i; float new_rank; float old_rank; if(j<n){ old_rank = page_ranks[j]; new_rank = 0.0f; for(i=0; i< n; ++i){ new_rank += maps[i*n + j]; } new_rank = ((1-D_FACTOR)/n)+(D_FACTOR*new_rank); dif[j] = fabs(new_rank - old_rank) > dif[j] ? fabs(new_rank - old_rank) : dif[j]; page_ranks[j] = new_rank; } } // generates an array of random pages and their links int *random_pages(int n, unsigned int *noutlinks, int divisor){ int i, j, k; int *pages = (int*) malloc(sizeof(int)*n*n); // matrix 1 means link from j->i if (divisor <= 0) { fprintf(stderr, "ERROR: Invalid divisor '%d' for random initialization, divisor should be greater or equal to 1\n", divisor); exit(1); } for(i=0; i<n; ++i){ noutlinks[i] = 0; for(j=0; j<n; ++j){ if(i!=j && (abs(rand())%divisor == 0)){ pages[i*n+j] = 1; noutlinks[i] += 1; } } // the case with no outlinks is avoided if(noutlinks[i] == 0){ do { k = abs(rand()) % n; } while ( k == i); pages[i*n + k] = 1; noutlinks[i] = 1; } } return pages; } void init_array(float *a, int n, float val){ int i; for(i=0; i<n; ++i){ a[i] = val; } } void usage(char *argv[]){ fprintf(stderr, "Usage: %s [-n number of pages] [-i max iterations]" " [-t threshold] [-q divsor for zero density]\n", argv[0]); } static struct option size_opts[] = { /* name, has_tag, flag, val*/ {"number of pages", 1, NULL, 'n'}, {"max number of iterations", 1, NULL, 'i'}, {"minimum threshold", 1, NULL, 't'}, {"divisor for zero density", 1, NULL, 'q'}, { 0, 0, 0} }; float maximum_dif(float *difs, int n){ int i; float max = 0.0f; for(i=0; i<n; ++i){ max = difs[i] > max ? difs[i] : max; } return max; } int main(int argc, char *argv[]) { int *pages; float *maps; float *page_ranks; unsigned int *noutlinks; int t; float max_diff; int i = 0; int j; int n = 1000; int iter = max_iter; float thresh = threshold; int divisor = 2; int nb_links = 0; int opt, opt_index = 0; while((opt = getopt_long(argc, argv, "::n:i:t:q:", size_opts, &opt_index)) != -1){ switch(opt){ case 'n': n = atoi(optarg); break; case 'i': iter = atoi(optarg); break; case 't': thresh = atof(optarg); break; case 'q': divisor = atoi(optarg); break; default: usage(argv); exit(EXIT_FAILURE); } } page_ranks = (float*)malloc(sizeof(float)*n); maps = (float*)malloc(sizeof(float)*n*n); noutlinks = (unsigned int*)malloc(sizeof(unsigned int)*n); max_diff=99.0f; for (i=0; i<n; ++i) { noutlinks[i] = 0; } pages = random_pages(n,noutlinks,divisor); init_array(page_ranks, n, 1.0f / (float) n); nb_links = 0; for (i=0; i<n; ++i) { for (j=0; j<n; ++j) { nb_links += pages[i*n+j]; } } float *diffs, *nzeros; diffs = (float*) malloc(sizeof(float)*n); nzeros = (float*) malloc(sizeof(float)*n); for(i = 0; i < n; ++i){ diffs[i] = 0.0f; nzeros[i] = 0.0f; } auto start = std::chrono::high_resolution_clock::now(); int *d_pages; float *d_maps; float *d_page_ranks; float *d_diffs; unsigned int *d_noutlinks; hipMalloc((void**)&d_pages, sizeof(*pages)*n*n); hipMalloc((void**)&d_page_ranks, sizeof(*page_ranks)*n); hipMalloc((void**)&d_maps, sizeof(*maps)*n*n); hipMalloc((void**)&d_noutlinks, sizeof(*noutlinks)*n); hipMalloc((void**)&d_diffs, sizeof(*diffs)*n); hipMemcpy(d_pages, pages, sizeof(int)*n*n, hipMemcpyHostToDevice); hipMemcpy(d_page_ranks, page_ranks, sizeof(float)*n, hipMemcpyHostToDevice); hipMemcpy(d_noutlinks, noutlinks, sizeof(unsigned int)*n, hipMemcpyHostToDevice); size_t block_size = n < BLOCK_SIZE ? n : BLOCK_SIZE; size_t num_blocks = (n+block_size-1) / block_size; for (t=1; t<=iter && max_diff>=thresh; ++t) { hipLaunchKernelGGL(( map) , dim3(dim3(num_blocks)), dim3(dim3(block_size)) , 0, 0, d_pages, d_page_ranks, d_maps, d_noutlinks, n); hipLaunchKernelGGL(( reduce), dim3(dim3(num_blocks)), dim3(dim3(block_size)) , 0, 0, d_page_ranks, d_maps, n, d_diffs); hipMemcpy(diffs, d_diffs, sizeof(float)*n, hipMemcpyDeviceToHost); max_diff = maximum_dif(diffs, n); hipMemcpy(d_diffs, nzeros, sizeof(*nzeros)*n, hipMemcpyHostToDevice); } hipMemcpy(maps, d_maps, sizeof(float)*n*n, hipMemcpyDeviceToHost); hipMemcpy(page_ranks, d_page_ranks, sizeof(float)*n, hipMemcpyDeviceToHost); hipFree(d_pages); hipFree(d_maps); hipFree(d_page_ranks); hipFree(d_noutlinks); hipFree(d_diffs); auto end = std::chrono::high_resolution_clock::now(); double seconds = std::chrono::duration_cast<std::chrono::duration<double> >(end - start).count(); hipDeviceProp_t props; hipGetDeviceProperties(&props, 0); printf("Device name: %s\n", props.name); fprintf(stderr, "max dif %f is reached at iteration %d\n", max_diff, t); printf("{ \"status\": %d, \"options\": \"-n %d -i %d -t %f\", \"time\": %f }\n", 1, n, iter, thresh, seconds); free(pages); free(maps); free(page_ranks); free(noutlinks); free(nzeros); free(diffs); return 0; }
981e6fb3340d6555aa7e3f8da49543578d209f08.cu
/* ** The MIT License (MIT) ** ** Copyright (c) 2014, Erick Lavoie, Faiz Khan, Sujay Kathrotia, Vincent ** Foley-Bourgon, Laurie Hendren ** ** Permission is hereby granted, free of charge, to any person obtaining a copy **of this software and associated documentation files (the "Software"), to deal ** in the Software without restriction, including without limitation the rights ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ** copies of the Software, and to permit persons to whom the Software is ** furnished to do so, subject to the following conditions: ** ** The above copyright notice and this permission notice shall be included in all ** copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ** SOFTWARE. ** **/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include <getopt.h> #include <chrono> #include <cuda.h> #define D_FACTOR (0.85) #ifndef BLOCK_SIZE #define BLOCK_SIZE 256 #endif // default values const int max_iter = 1000; const float threshold= 0.00001f; __global__ void map( int *pages, float *page_ranks, float *maps, unsigned int *noutlinks, int n) { int i = threadIdx.x + blockIdx.x * blockDim.x; int j; if(i < n){ float outbound_rank = page_ranks[i]/(float)noutlinks[i]; for(j=0; j<n; ++j){ maps[i*n+j] = pages[i*n+j]*outbound_rank; } } } __global__ void reduce( float *page_ranks, float *maps, int n, float *dif) { int j = threadIdx.x + blockIdx.x * blockDim.x; int i; float new_rank; float old_rank; if(j<n){ old_rank = page_ranks[j]; new_rank = 0.0f; for(i=0; i< n; ++i){ new_rank += maps[i*n + j]; } new_rank = ((1-D_FACTOR)/n)+(D_FACTOR*new_rank); dif[j] = fabs(new_rank - old_rank) > dif[j] ? fabs(new_rank - old_rank) : dif[j]; page_ranks[j] = new_rank; } } // generates an array of random pages and their links int *random_pages(int n, unsigned int *noutlinks, int divisor){ int i, j, k; int *pages = (int*) malloc(sizeof(int)*n*n); // matrix 1 means link from j->i if (divisor <= 0) { fprintf(stderr, "ERROR: Invalid divisor '%d' for random initialization, divisor should be greater or equal to 1\n", divisor); exit(1); } for(i=0; i<n; ++i){ noutlinks[i] = 0; for(j=0; j<n; ++j){ if(i!=j && (abs(rand())%divisor == 0)){ pages[i*n+j] = 1; noutlinks[i] += 1; } } // the case with no outlinks is avoided if(noutlinks[i] == 0){ do { k = abs(rand()) % n; } while ( k == i); pages[i*n + k] = 1; noutlinks[i] = 1; } } return pages; } void init_array(float *a, int n, float val){ int i; for(i=0; i<n; ++i){ a[i] = val; } } void usage(char *argv[]){ fprintf(stderr, "Usage: %s [-n number of pages] [-i max iterations]" " [-t threshold] [-q divsor for zero density]\n", argv[0]); } static struct option size_opts[] = { /* name, has_tag, flag, val*/ {"number of pages", 1, NULL, 'n'}, {"max number of iterations", 1, NULL, 'i'}, {"minimum threshold", 1, NULL, 't'}, {"divisor for zero density", 1, NULL, 'q'}, { 0, 0, 0} }; float maximum_dif(float *difs, int n){ int i; float max = 0.0f; for(i=0; i<n; ++i){ max = difs[i] > max ? difs[i] : max; } return max; } int main(int argc, char *argv[]) { int *pages; float *maps; float *page_ranks; unsigned int *noutlinks; int t; float max_diff; int i = 0; int j; int n = 1000; int iter = max_iter; float thresh = threshold; int divisor = 2; int nb_links = 0; int opt, opt_index = 0; while((opt = getopt_long(argc, argv, "::n:i:t:q:", size_opts, &opt_index)) != -1){ switch(opt){ case 'n': n = atoi(optarg); break; case 'i': iter = atoi(optarg); break; case 't': thresh = atof(optarg); break; case 'q': divisor = atoi(optarg); break; default: usage(argv); exit(EXIT_FAILURE); } } page_ranks = (float*)malloc(sizeof(float)*n); maps = (float*)malloc(sizeof(float)*n*n); noutlinks = (unsigned int*)malloc(sizeof(unsigned int)*n); max_diff=99.0f; for (i=0; i<n; ++i) { noutlinks[i] = 0; } pages = random_pages(n,noutlinks,divisor); init_array(page_ranks, n, 1.0f / (float) n); nb_links = 0; for (i=0; i<n; ++i) { for (j=0; j<n; ++j) { nb_links += pages[i*n+j]; } } float *diffs, *nzeros; diffs = (float*) malloc(sizeof(float)*n); nzeros = (float*) malloc(sizeof(float)*n); for(i = 0; i < n; ++i){ diffs[i] = 0.0f; nzeros[i] = 0.0f; } auto start = std::chrono::high_resolution_clock::now(); int *d_pages; float *d_maps; float *d_page_ranks; float *d_diffs; unsigned int *d_noutlinks; cudaMalloc((void**)&d_pages, sizeof(*pages)*n*n); cudaMalloc((void**)&d_page_ranks, sizeof(*page_ranks)*n); cudaMalloc((void**)&d_maps, sizeof(*maps)*n*n); cudaMalloc((void**)&d_noutlinks, sizeof(*noutlinks)*n); cudaMalloc((void**)&d_diffs, sizeof(*diffs)*n); cudaMemcpy(d_pages, pages, sizeof(int)*n*n, cudaMemcpyHostToDevice); cudaMemcpy(d_page_ranks, page_ranks, sizeof(float)*n, cudaMemcpyHostToDevice); cudaMemcpy(d_noutlinks, noutlinks, sizeof(unsigned int)*n, cudaMemcpyHostToDevice); size_t block_size = n < BLOCK_SIZE ? n : BLOCK_SIZE; size_t num_blocks = (n+block_size-1) / block_size; for (t=1; t<=iter && max_diff>=thresh; ++t) { map <<< dim3(num_blocks), dim3(block_size) >>> ( d_pages, d_page_ranks, d_maps, d_noutlinks, n); reduce<<< dim3(num_blocks), dim3(block_size) >>>(d_page_ranks, d_maps, n, d_diffs); cudaMemcpy(diffs, d_diffs, sizeof(float)*n, cudaMemcpyDeviceToHost); max_diff = maximum_dif(diffs, n); cudaMemcpy(d_diffs, nzeros, sizeof(*nzeros)*n, cudaMemcpyHostToDevice); } cudaMemcpy(maps, d_maps, sizeof(float)*n*n, cudaMemcpyDeviceToHost); cudaMemcpy(page_ranks, d_page_ranks, sizeof(float)*n, cudaMemcpyDeviceToHost); cudaFree(d_pages); cudaFree(d_maps); cudaFree(d_page_ranks); cudaFree(d_noutlinks); cudaFree(d_diffs); auto end = std::chrono::high_resolution_clock::now(); double seconds = std::chrono::duration_cast<std::chrono::duration<double> >(end - start).count(); cudaDeviceProp props; cudaGetDeviceProperties(&props, 0); printf("Device name: %s\n", props.name); fprintf(stderr, "max dif %f is reached at iteration %d\n", max_diff, t); printf("{ \"status\": %d, \"options\": \"-n %d -i %d -t %f\", \"time\": %f }\n", 1, n, iter, thresh, seconds); free(pages); free(maps); free(page_ranks); free(noutlinks); free(nzeros); free(diffs); return 0; }
858ae85bfac6788abf415df00c7f6720aae6e4f7.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <stdio.h> #include "gvm_fs.ch" #include "gpu_file.ch" #include "rpc_queue.ch" #include "types.ch" #include "util.ch" #include <unistd.h> __global__ void fill_queue(void) { if (blockIdx.x == 0 && threadIdx.x == 0) { //if ((blockIdx.x == 0 && threadIdx.x == 0) || (blockIdx.x == 1 && threadIdx.x == 0)) { //if (threadIdx.x == 0) { //gpu_fd fd = gpu_file_open("/home/noah/School/gvm_fs/files/sm_test_file.txt", RW__); //char * file_buf = NULL; //size_t bytes_read = gpu_file_read(fd, 16, &file_buf); //printf("Bytes read: %d\n", bytes_read); //printf("File: %s\n", file_buf); //file_buf[blockIdx.x] = '*'; //printf("File: %s\n", file_buf); ////gpu_file_grow(fd, 32); //gpu_file_close(fd); int gpu_fd = gvm_fs_open((char *) "/home/noah/School/Thesis/gvm_fs/files/sm_test_file.txt"); char * file_buf = NULL; gvm_fs_read(gpu_fd, 16, &file_buf); file_buf[5] = '\0'; printf("Filebuf: %s\n", file_buf); } } int main() { printf("MAIN\n"); init_gvm_fs(); printf("DONE init_rpc_queue\n"); //fill_queue<<<rpc_queue_SIZE, 32>>>(); hipLaunchKernelGGL(( fill_queue), dim3(10), dim3(32), 0, 0, ); CUDA_CALL(hipDeviceSynchronize()); //sleep(10); printf("Kernel finished\n"); cleanup_gvm_fs(); }
858ae85bfac6788abf415df00c7f6720aae6e4f7.cu
#include <stdio.h> #include "gvm_fs.ch" #include "gpu_file.ch" #include "rpc_queue.ch" #include "types.ch" #include "util.ch" #include <unistd.h> __global__ void fill_queue(void) { if (blockIdx.x == 0 && threadIdx.x == 0) { //if ((blockIdx.x == 0 && threadIdx.x == 0) || (blockIdx.x == 1 && threadIdx.x == 0)) { //if (threadIdx.x == 0) { //gpu_fd fd = gpu_file_open("/home/noah/School/gvm_fs/files/sm_test_file.txt", RW__); //char * file_buf = NULL; //size_t bytes_read = gpu_file_read(fd, 16, &file_buf); //printf("Bytes read: %d\n", bytes_read); //printf("File: %s\n", file_buf); //file_buf[blockIdx.x] = '*'; //printf("File: %s\n", file_buf); ////gpu_file_grow(fd, 32); //gpu_file_close(fd); int gpu_fd = gvm_fs_open((char *) "/home/noah/School/Thesis/gvm_fs/files/sm_test_file.txt"); char * file_buf = NULL; gvm_fs_read(gpu_fd, 16, &file_buf); file_buf[5] = '\0'; printf("Filebuf: %s\n", file_buf); } } int main() { printf("MAIN\n"); init_gvm_fs(); printf("DONE init_rpc_queue\n"); //fill_queue<<<rpc_queue_SIZE, 32>>>(); fill_queue<<<10, 32>>>(); CUDA_CALL(cudaDeviceSynchronize()); //sleep(10); printf("Kernel finished\n"); cleanup_gvm_fs(); }
87ed9cf09d44e562bb766ae56cf7f6e387275e97.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /*! * Copyright 2017-2020 by Contributors */ #include <thrust/copy.h> #include <thrust/device_ptr.h> #include <thrust/device_vector.h> #include <thrust/fill.h> #include <GPUTreeShap/gpu_treeshap.h> #include <memory> #include "xgboost/data.h" #include "xgboost/predictor.h" #include "xgboost/tree_model.h" #include "xgboost/tree_updater.h" #include "xgboost/host_device_vector.h" #include "../gbm/gbtree_model.h" #include "../data/ellpack_page.cuh" #include "../data/device_adapter.cuh" #include "../common/common.h" #include "../common/bitfield.h" #include "../common/categorical.h" #include "../common/device_helpers.cuh" namespace xgboost { namespace predictor { DMLC_REGISTRY_FILE_TAG(gpu_predictor); struct SparsePageView { common::Span<const Entry> d_data; common::Span<const bst_row_t> d_row_ptr; bst_feature_t num_features; SparsePageView() = default; XGBOOST_DEVICE SparsePageView(common::Span<const Entry> data, common::Span<const bst_row_t> row_ptr, bst_feature_t num_features) : d_data{data}, d_row_ptr{row_ptr}, num_features(num_features) {} __device__ float GetElement(size_t ridx, size_t fidx) const { // Binary search auto begin_ptr = d_data.begin() + d_row_ptr[ridx]; auto end_ptr = d_data.begin() + d_row_ptr[ridx + 1]; if (end_ptr - begin_ptr == this->NumCols()) { // Bypass span check for dense data return d_data.data()[d_row_ptr[ridx] + fidx].fvalue; } common::Span<const Entry>::iterator previous_middle; while (end_ptr != begin_ptr) { auto middle = begin_ptr + (end_ptr - begin_ptr) / 2; if (middle == previous_middle) { break; } else { previous_middle = middle; } if (middle->index == fidx) { return middle->fvalue; } else if (middle->index < fidx) { begin_ptr = middle; } else { end_ptr = middle; } } // Value is missing return nanf(""); } XGBOOST_DEVICE size_t NumRows() const { return d_row_ptr.size() - 1; } XGBOOST_DEVICE size_t NumCols() const { return num_features; } }; struct SparsePageLoader { bool use_shared; SparsePageView data; float* smem; size_t entry_start; __device__ SparsePageLoader(SparsePageView data, bool use_shared, bst_feature_t num_features, bst_row_t num_rows, size_t entry_start) : use_shared(use_shared), data(data), entry_start(entry_start) { extern __shared__ float _smem[]; smem = _smem; // Copy instances if (use_shared) { bst_uint global_idx = blockDim.x * blockIdx.x + threadIdx.x; int shared_elements = blockDim.x * data.num_features; dh::BlockFill(smem, shared_elements, nanf("")); __syncthreads(); if (global_idx < num_rows) { bst_uint elem_begin = data.d_row_ptr[global_idx]; bst_uint elem_end = data.d_row_ptr[global_idx + 1]; for (bst_uint elem_idx = elem_begin; elem_idx < elem_end; elem_idx++) { Entry elem = data.d_data[elem_idx - entry_start]; smem[threadIdx.x * data.num_features + elem.index] = elem.fvalue; } } __syncthreads(); } } __device__ float GetElement(size_t ridx, size_t fidx) const { if (use_shared) { return smem[threadIdx.x * data.num_features + fidx]; } else { return data.GetElement(ridx, fidx); } } }; struct EllpackLoader { EllpackDeviceAccessor const& matrix; XGBOOST_DEVICE EllpackLoader(EllpackDeviceAccessor const& m, bool, bst_feature_t, bst_row_t, size_t) : matrix{m} {} __device__ __forceinline__ float GetElement(size_t ridx, size_t fidx) const { auto gidx = matrix.GetBinIndex(ridx, fidx); if (gidx == -1) { return nan(""); } // The gradient index needs to be shifted by one as min values are not included in the // cuts. if (gidx == matrix.feature_segments[fidx]) { return matrix.min_fvalue[fidx]; } return matrix.gidx_fvalue_map[gidx - 1]; } }; template <typename Batch> struct DeviceAdapterLoader { Batch batch; bst_feature_t columns; float* smem; bool use_shared; using BatchT = Batch; XGBOOST_DEV_INLINE DeviceAdapterLoader(Batch const batch, bool use_shared, bst_feature_t num_features, bst_row_t num_rows, size_t entry_start) : batch{batch}, columns{num_features}, use_shared{use_shared} { extern __shared__ float _smem[]; smem = _smem; if (use_shared) { uint32_t global_idx = blockDim.x * blockIdx.x + threadIdx.x; size_t shared_elements = blockDim.x * num_features; dh::BlockFill(smem, shared_elements, nanf("")); __syncthreads(); if (global_idx < num_rows) { auto beg = global_idx * columns; auto end = (global_idx + 1) * columns; for (size_t i = beg; i < end; ++i) { smem[threadIdx.x * num_features + (i - beg)] = batch.GetElement(i).value; } } } __syncthreads(); } XGBOOST_DEV_INLINE float GetElement(size_t ridx, size_t fidx) const { if (use_shared) { return smem[threadIdx.x * columns + fidx]; } return batch.GetElement(ridx * columns + fidx).value; } }; template <typename Loader> __device__ float GetLeafWeight(bst_uint ridx, const RegTree::Node* tree, common::Span<FeatureType const> split_types, common::Span<RegTree::Segment const> d_cat_ptrs, common::Span<uint32_t const> d_categories, Loader* loader) { bst_node_t nidx = 0; RegTree::Node n = tree[nidx]; while (!n.IsLeaf()) { float fvalue = loader->GetElement(ridx, n.SplitIndex()); // Missing value if (common::CheckNAN(fvalue)) { nidx = n.DefaultChild(); } else { bool go_left = true; if (common::IsCat(split_types, nidx)) { auto categories = d_categories.subspan(d_cat_ptrs[nidx].beg, d_cat_ptrs[nidx].size); go_left = Decision(categories, common::AsCat(fvalue)); } else { go_left = fvalue < n.SplitCond(); } if (go_left) { nidx = n.LeftChild(); } else { nidx = n.RightChild(); } } n = tree[nidx]; } return tree[nidx].LeafValue(); } template <typename Loader, typename Data> __global__ void PredictKernel(Data data, common::Span<const RegTree::Node> d_nodes, common::Span<float> d_out_predictions, common::Span<size_t const> d_tree_segments, common::Span<int const> d_tree_group, common::Span<FeatureType const> d_tree_split_types, common::Span<uint32_t const> d_cat_tree_segments, common::Span<RegTree::Segment const> d_cat_node_segments, common::Span<uint32_t const> d_categories, size_t tree_begin, size_t tree_end, size_t num_features, size_t num_rows, size_t entry_start, bool use_shared, int num_group) { bst_uint global_idx = blockDim.x * blockIdx.x + threadIdx.x; Loader loader(data, use_shared, num_features, num_rows, entry_start); if (global_idx >= num_rows) return; if (num_group == 1) { float sum = 0; for (int tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { const RegTree::Node* d_tree = &d_nodes[d_tree_segments[tree_idx - tree_begin]]; auto tree_cat_ptrs = d_cat_node_segments.subspan( d_tree_segments[tree_idx - tree_begin], d_tree_segments[tree_idx - tree_begin + 1] - d_tree_segments[tree_idx - tree_begin]); auto tree_categories = d_categories.subspan(d_cat_tree_segments[tree_idx - tree_begin], d_cat_tree_segments[tree_idx - tree_begin + 1] - d_cat_tree_segments[tree_idx - tree_begin]); float leaf = GetLeafWeight(global_idx, d_tree, d_tree_split_types, tree_cat_ptrs, tree_categories, &loader); sum += leaf; } d_out_predictions[global_idx] += sum; } else { for (int tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { int tree_group = d_tree_group[tree_idx]; const RegTree::Node* d_tree = &d_nodes[d_tree_segments[tree_idx - tree_begin]]; bst_uint out_prediction_idx = global_idx * num_group + tree_group; auto tree_cat_ptrs = d_cat_node_segments.subspan( d_tree_segments[tree_idx - tree_begin], d_tree_segments[tree_idx - tree_begin + 1] - d_tree_segments[tree_idx - tree_begin]); auto tree_categories = d_categories.subspan(d_cat_tree_segments[tree_idx - tree_begin], d_cat_tree_segments[tree_idx - tree_begin + 1] - d_cat_tree_segments[tree_idx - tree_begin]); d_out_predictions[out_prediction_idx] += GetLeafWeight(global_idx, d_tree, d_tree_split_types, tree_cat_ptrs, tree_categories, &loader); } } } class DeviceModel { public: // Need to lazily construct the vectors because GPU id is only known at runtime HostDeviceVector<RTreeNodeStat> stats; HostDeviceVector<size_t> tree_segments; HostDeviceVector<RegTree::Node> nodes; HostDeviceVector<int> tree_group; HostDeviceVector<FeatureType> split_types; // Pointer to each tree, segmenting the node array. HostDeviceVector<uint32_t> categories_tree_segments; // Pointer to each node, segmenting categories array. HostDeviceVector<RegTree::Segment> categories_node_segments; HostDeviceVector<uint32_t> categories; size_t tree_beg_; // NOLINT size_t tree_end_; // NOLINT int num_group; void Init(const gbm::GBTreeModel& model, size_t tree_begin, size_t tree_end, int32_t gpu_id) { dh::safe_cuda(hipSetDevice(gpu_id)); CHECK_EQ(model.param.size_leaf_vector, 0); // Copy decision trees to device tree_segments = std::move(HostDeviceVector<size_t>({}, gpu_id)); auto& h_tree_segments = tree_segments.HostVector(); h_tree_segments.reserve((tree_end - tree_begin) + 1); size_t sum = 0; h_tree_segments.push_back(sum); for (auto tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { sum += model.trees.at(tree_idx)->GetNodes().size(); h_tree_segments.push_back(sum); } nodes = std::move(HostDeviceVector<RegTree::Node>(h_tree_segments.back(), RegTree::Node(), gpu_id)); stats = std::move(HostDeviceVector<RTreeNodeStat>(h_tree_segments.back(), RTreeNodeStat(), gpu_id)); auto d_nodes = nodes.DevicePointer(); auto d_stats = stats.DevicePointer(); for (auto tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { auto& src_nodes = model.trees.at(tree_idx)->GetNodes(); auto& src_stats = model.trees.at(tree_idx)->GetStats(); dh::safe_cuda(hipMemcpyAsync( d_nodes + h_tree_segments[tree_idx - tree_begin], src_nodes.data(), sizeof(RegTree::Node) * src_nodes.size(), hipMemcpyDefault)); dh::safe_cuda(hipMemcpyAsync( d_stats + h_tree_segments[tree_idx - tree_begin], src_stats.data(), sizeof(RTreeNodeStat) * src_stats.size(), hipMemcpyDefault)); } tree_group = std::move(HostDeviceVector<int>(model.tree_info.size(), 0, gpu_id)); auto& h_tree_group = tree_group.HostVector(); std::memcpy(h_tree_group.data(), model.tree_info.data(), sizeof(int) * model.tree_info.size()); // Initialize categorical splits. split_types.SetDevice(gpu_id); std::vector<FeatureType>& h_split_types = split_types.HostVector(); h_split_types.resize(h_tree_segments.back()); for (auto tree_idx = tree_begin; tree_idx < tree_end; ++tree_idx) { auto const& src_st = model.trees.at(tree_idx)->GetSplitTypes(); std::copy(src_st.cbegin(), src_st.cend(), h_split_types.begin() + h_tree_segments[tree_idx - tree_begin]); } categories = HostDeviceVector<uint32_t>({}, gpu_id); categories_tree_segments = HostDeviceVector<uint32_t>(1, 0, gpu_id); std::vector<uint32_t> &h_categories = categories.HostVector(); std::vector<uint32_t> &h_split_cat_segments = categories_tree_segments.HostVector(); for (auto tree_idx = tree_begin; tree_idx < tree_end; ++tree_idx) { auto const& src_cats = model.trees.at(tree_idx)->GetSplitCategories(); size_t orig_size = h_categories.size(); h_categories.resize(orig_size + src_cats.size()); std::copy(src_cats.cbegin(), src_cats.cend(), h_categories.begin() + orig_size); h_split_cat_segments.push_back(h_categories.size()); } categories_node_segments = HostDeviceVector<RegTree::Segment>(h_tree_segments.back(), {}, gpu_id); std::vector<RegTree::Segment> &h_categories_node_segments = categories_node_segments.HostVector(); for (auto tree_idx = tree_begin; tree_idx < tree_end; ++tree_idx) { auto const &src_cats_ptr = model.trees.at(tree_idx)->GetSplitCategoriesPtr(); std::copy(src_cats_ptr.cbegin(), src_cats_ptr.cend(), h_categories_node_segments.begin() + h_tree_segments[tree_idx - tree_begin]); } this->tree_beg_ = tree_begin; this->tree_end_ = tree_end; this->num_group = model.learner_model_param->num_output_group; } }; struct PathInfo { int64_t leaf_position; // -1 not a leaf size_t length; size_t tree_idx; }; // Transform model into path element form for GPUTreeShap void ExtractPaths(dh::device_vector<gpu_treeshap::PathElement>* paths, const gbm::GBTreeModel& model, size_t tree_limit, int gpu_id) { DeviceModel device_model; device_model.Init(model, 0, tree_limit, gpu_id); dh::caching_device_vector<PathInfo> info(device_model.nodes.Size()); dh::XGBCachingDeviceAllocator<PathInfo> alloc; auto d_nodes = device_model.nodes.ConstDeviceSpan(); auto d_tree_segments = device_model.tree_segments.ConstDeviceSpan(); auto nodes_transform = dh::MakeTransformIterator<PathInfo>( thrust::make_counting_iterator(0ull), [=] __device__(size_t idx) { auto n = d_nodes[idx]; if (!n.IsLeaf() || n.IsDeleted()) { return PathInfo{-1, 0, 0}; } size_t tree_idx = dh::SegmentId(d_tree_segments.begin(), d_tree_segments.end(), idx); size_t tree_offset = d_tree_segments[tree_idx]; size_t path_length = 1; while (!n.IsRoot()) { n = d_nodes[n.Parent() + tree_offset]; path_length++; } return PathInfo{int64_t(idx), path_length, tree_idx}; }); auto end = thrust::copy_if( thrust::hip::par(alloc), nodes_transform, nodes_transform + d_nodes.size(), info.begin(), [=] __device__(const PathInfo& e) { return e.leaf_position != -1; }); info.resize(end - info.begin()); auto length_iterator = dh::MakeTransformIterator<size_t>( info.begin(), [=] __device__(const PathInfo& info) { return info.length; }); dh::caching_device_vector<size_t> path_segments(info.size() + 1); thrust::exclusive_scan(thrust::hip::par(alloc), length_iterator, length_iterator + info.size() + 1, path_segments.begin()); paths->resize(path_segments.back()); auto d_paths = paths->data().get(); auto d_info = info.data().get(); auto d_stats = device_model.stats.ConstDeviceSpan(); auto d_tree_group = device_model.tree_group.ConstDeviceSpan(); auto d_path_segments = path_segments.data().get(); dh::LaunchN(gpu_id, info.size(), [=] __device__(size_t idx) { auto path_info = d_info[idx]; size_t tree_offset = d_tree_segments[path_info.tree_idx]; int group = d_tree_group[path_info.tree_idx]; size_t child_idx = path_info.leaf_position; auto child = d_nodes[child_idx]; float v = child.LeafValue(); const float inf = std::numeric_limits<float>::infinity(); size_t output_position = d_path_segments[idx + 1] - 1; while (!child.IsRoot()) { size_t parent_idx = tree_offset + child.Parent(); double child_cover = d_stats[child_idx].sum_hess; double parent_cover = d_stats[parent_idx].sum_hess; double zero_fraction = child_cover / parent_cover; auto parent = d_nodes[parent_idx]; bool is_left_path = (tree_offset + parent.LeftChild()) == child_idx; bool is_missing_path = (!parent.DefaultLeft() && !is_left_path) || (parent.DefaultLeft() && is_left_path); float lower_bound = is_left_path ? -inf : parent.SplitCond(); float upper_bound = is_left_path ? parent.SplitCond() : inf; d_paths[output_position--] = { idx, parent.SplitIndex(), group, lower_bound, upper_bound, is_missing_path, zero_fraction, v}; child_idx = parent_idx; child = parent; } // Root node has feature -1 d_paths[output_position] = {idx, -1, group, -inf, inf, false, 1.0, v}; }); } class GPUPredictor : public xgboost::Predictor { private: void PredictInternal(const SparsePage& batch, size_t num_features, HostDeviceVector<bst_float>* predictions, size_t batch_offset) { batch.offset.SetDevice(generic_param_->gpu_id); batch.data.SetDevice(generic_param_->gpu_id); const uint32_t BLOCK_THREADS = 128; size_t num_rows = batch.Size(); auto GRID_SIZE = static_cast<uint32_t>(common::DivRoundUp(num_rows, BLOCK_THREADS)); auto shared_memory_bytes = static_cast<size_t>(sizeof(float) * num_features * BLOCK_THREADS); bool use_shared = true; if (shared_memory_bytes > max_shared_memory_bytes_) { shared_memory_bytes = 0; use_shared = false; } size_t entry_start = 0; SparsePageView data(batch.data.DeviceSpan(), batch.offset.DeviceSpan(), num_features); dh::LaunchKernel {GRID_SIZE, BLOCK_THREADS, shared_memory_bytes} ( PredictKernel<SparsePageLoader, SparsePageView>, data, model_.nodes.ConstDeviceSpan(), predictions->DeviceSpan().subspan(batch_offset), model_.tree_segments.ConstDeviceSpan(), model_.tree_group.ConstDeviceSpan(), model_.split_types.ConstDeviceSpan(), model_.categories_tree_segments.ConstDeviceSpan(), model_.categories_node_segments.ConstDeviceSpan(), model_.categories.ConstDeviceSpan(), model_.tree_beg_, model_.tree_end_, num_features, num_rows, entry_start, use_shared, model_.num_group); } void PredictInternal(EllpackDeviceAccessor const& batch, HostDeviceVector<bst_float>* out_preds, size_t batch_offset) { const uint32_t BLOCK_THREADS = 256; size_t num_rows = batch.n_rows; auto GRID_SIZE = static_cast<uint32_t>(common::DivRoundUp(num_rows, BLOCK_THREADS)); bool use_shared = false; size_t entry_start = 0; dh::LaunchKernel {GRID_SIZE, BLOCK_THREADS} ( PredictKernel<EllpackLoader, EllpackDeviceAccessor>, batch, model_.nodes.ConstDeviceSpan(), out_preds->DeviceSpan().subspan(batch_offset), model_.tree_segments.ConstDeviceSpan(), model_.tree_group.ConstDeviceSpan(), model_.split_types.ConstDeviceSpan(), model_.categories_tree_segments.ConstDeviceSpan(), model_.categories_node_segments.ConstDeviceSpan(), model_.categories.ConstDeviceSpan(), model_.tree_beg_, model_.tree_end_, batch.NumFeatures(), num_rows, entry_start, use_shared, model_.num_group); } void DevicePredictInternal(DMatrix* dmat, HostDeviceVector<float>* out_preds, const gbm::GBTreeModel& model, size_t tree_begin, size_t tree_end) { dh::safe_cuda(hipSetDevice(generic_param_->gpu_id)); if (tree_end - tree_begin == 0) { return; } model_.Init(model, tree_begin, tree_end, generic_param_->gpu_id); out_preds->SetDevice(generic_param_->gpu_id); auto const& info = dmat->Info(); if (dmat->PageExists<SparsePage>()) { size_t batch_offset = 0; for (auto &batch : dmat->GetBatches<SparsePage>()) { this->PredictInternal(batch, model.learner_model_param->num_feature, out_preds, batch_offset); batch_offset += batch.Size() * model.learner_model_param->num_output_group; } } else { size_t batch_offset = 0; for (auto const& page : dmat->GetBatches<EllpackPage>()) { this->PredictInternal( page.Impl()->GetDeviceAccessor(generic_param_->gpu_id), out_preds, batch_offset); batch_offset += page.Impl()->n_rows; } } } public: explicit GPUPredictor(GenericParameter const* generic_param) : Predictor::Predictor{generic_param} {} ~GPUPredictor() override { if (generic_param_->gpu_id >= 0) { dh::safe_cuda(hipSetDevice(generic_param_->gpu_id)); } } void PredictBatch(DMatrix* dmat, PredictionCacheEntry* predts, const gbm::GBTreeModel& model, int tree_begin, unsigned ntree_limit = 0) override { // This function is duplicated with CPU predictor PredictBatch, see comments in there. // FIXME(trivialfis): Remove the duplication. std::lock_guard<std::mutex> const guard(lock_); int device = generic_param_->gpu_id; CHECK_GE(device, 0) << "Set `gpu_id' to positive value for processing GPU data."; ConfigureDevice(device); CHECK_EQ(tree_begin, 0); auto* out_preds = &predts->predictions; CHECK_GE(predts->version, tree_begin); if (out_preds->Size() == 0 && dmat->Info().num_row_ != 0) { CHECK_EQ(predts->version, 0); } if (predts->version == 0) { this->InitOutPredictions(dmat->Info(), out_preds, model); } uint32_t const output_groups = model.learner_model_param->num_output_group; CHECK_NE(output_groups, 0); uint32_t real_ntree_limit = ntree_limit * output_groups; if (real_ntree_limit == 0 || real_ntree_limit > model.trees.size()) { real_ntree_limit = static_cast<uint32_t>(model.trees.size()); } uint32_t const end_version = (tree_begin + real_ntree_limit) / output_groups; if (predts->version > end_version) { CHECK_NE(ntree_limit, 0); this->InitOutPredictions(dmat->Info(), out_preds, model); predts->version = 0; } uint32_t const beg_version = predts->version; CHECK_LE(beg_version, end_version); if (beg_version < end_version) { this->DevicePredictInternal(dmat, out_preds, model, beg_version * output_groups, end_version * output_groups); } uint32_t delta = end_version - beg_version; CHECK_LE(delta, model.trees.size()); predts->Update(delta); CHECK(out_preds->Size() == output_groups * dmat->Info().num_row_ || out_preds->Size() == dmat->Info().num_row_); } template <typename Adapter, typename Loader> void DispatchedInplacePredict(dmlc::any const &x, const gbm::GBTreeModel &model, float, PredictionCacheEntry *out_preds, uint32_t tree_begin, uint32_t tree_end) const { auto max_shared_memory_bytes = dh::MaxSharedMemory(this->generic_param_->gpu_id); uint32_t const output_groups = model.learner_model_param->num_output_group; DeviceModel d_model; d_model.Init(model, tree_begin, tree_end, this->generic_param_->gpu_id); auto m = dmlc::get<std::shared_ptr<Adapter>>(x); CHECK_EQ(m->NumColumns(), model.learner_model_param->num_feature) << "Number of columns in data must equal to trained model."; CHECK_EQ(this->generic_param_->gpu_id, m->DeviceIdx()) << "XGBoost is running on device: " << this->generic_param_->gpu_id << ", " << "but data is on: " << m->DeviceIdx(); MetaInfo info; info.num_col_ = m->NumColumns(); info.num_row_ = m->NumRows(); this->InitOutPredictions(info, &(out_preds->predictions), model); const uint32_t BLOCK_THREADS = 128; auto GRID_SIZE = static_cast<uint32_t>(common::DivRoundUp(info.num_row_, BLOCK_THREADS)); auto shared_memory_bytes = static_cast<size_t>(sizeof(float) * m->NumColumns() * BLOCK_THREADS); bool use_shared = true; if (shared_memory_bytes > max_shared_memory_bytes) { shared_memory_bytes = 0; use_shared = false; } size_t entry_start = 0; dh::LaunchKernel {GRID_SIZE, BLOCK_THREADS, shared_memory_bytes} ( PredictKernel<Loader, typename Loader::BatchT>, m->Value(), d_model.nodes.ConstDeviceSpan(), out_preds->predictions.DeviceSpan(), d_model.tree_segments.ConstDeviceSpan(), d_model.tree_group.ConstDeviceSpan(), d_model.split_types.ConstDeviceSpan(), d_model.categories_tree_segments.ConstDeviceSpan(), d_model.categories_node_segments.ConstDeviceSpan(), d_model.categories.ConstDeviceSpan(), tree_begin, tree_end, m->NumColumns(), info.num_row_, entry_start, use_shared, output_groups); } void InplacePredict(dmlc::any const &x, const gbm::GBTreeModel &model, float missing, PredictionCacheEntry *out_preds, uint32_t tree_begin, unsigned tree_end) const override { if (x.type() == typeid(std::shared_ptr<data::CupyAdapter>)) { this->DispatchedInplacePredict< data::CupyAdapter, DeviceAdapterLoader<data::CupyAdapterBatch>>( x, model, missing, out_preds, tree_begin, tree_end); } else if (x.type() == typeid(std::shared_ptr<data::CudfAdapter>)) { this->DispatchedInplacePredict< data::CudfAdapter, DeviceAdapterLoader<data::CudfAdapterBatch>>( x, model, missing, out_preds, tree_begin, tree_end); } else { LOG(FATAL) << "Only CuPy and CuDF are supported by GPU Predictor."; } } void PredictContribution(DMatrix* p_fmat, HostDeviceVector<bst_float>* out_contribs, const gbm::GBTreeModel& model, unsigned ntree_limit, std::vector<bst_float>*, bool approximate, int, unsigned) override { if (approximate) { LOG(FATAL) << "Approximated contribution is not implemented in GPU Predictor."; } dh::safe_cuda(hipSetDevice(generic_param_->gpu_id)); out_contribs->SetDevice(generic_param_->gpu_id); uint32_t real_ntree_limit = ntree_limit * model.learner_model_param->num_output_group; if (real_ntree_limit == 0 || real_ntree_limit > model.trees.size()) { real_ntree_limit = static_cast<uint32_t>(model.trees.size()); } const int ngroup = model.learner_model_param->num_output_group; CHECK_NE(ngroup, 0); // allocate space for (number of features + bias) times the number of rows size_t contributions_columns = model.learner_model_param->num_feature + 1; // +1 for bias out_contribs->Resize(p_fmat->Info().num_row_ * contributions_columns * model.learner_model_param->num_output_group); out_contribs->Fill(0.0f); auto phis = out_contribs->DeviceSpan(); p_fmat->Info().base_margin_.SetDevice(generic_param_->gpu_id); const auto margin = p_fmat->Info().base_margin_.ConstDeviceSpan(); float base_score = model.learner_model_param->base_score; // Add the base margin term to last column dh::LaunchN( generic_param_->gpu_id, p_fmat->Info().num_row_ * model.learner_model_param->num_output_group, [=] __device__(size_t idx) { phis[(idx + 1) * contributions_columns - 1] = margin.empty() ? base_score : margin[idx]; }); dh::device_vector<gpu_treeshap::PathElement> device_paths; ExtractPaths(&device_paths, model, real_ntree_limit, generic_param_->gpu_id); for (auto& batch : p_fmat->GetBatches<SparsePage>()) { batch.data.SetDevice(generic_param_->gpu_id); batch.offset.SetDevice(generic_param_->gpu_id); SparsePageView X(batch.data.DeviceSpan(), batch.offset.DeviceSpan(), model.learner_model_param->num_feature); gpu_treeshap::GPUTreeShap( X, device_paths.begin(), device_paths.end(), ngroup, phis.data() + batch.base_rowid * contributions_columns, phis.size()); } } void PredictInteractionContributions(DMatrix* p_fmat, HostDeviceVector<bst_float>* out_contribs, const gbm::GBTreeModel& model, unsigned ntree_limit, std::vector<bst_float>*, bool approximate) override { if (approximate) { LOG(FATAL) << "[Internal error]: " << __func__ << " approximate is not implemented in GPU Predictor."; } dh::safe_cuda(hipSetDevice(generic_param_->gpu_id)); out_contribs->SetDevice(generic_param_->gpu_id); uint32_t real_ntree_limit = ntree_limit * model.learner_model_param->num_output_group; if (real_ntree_limit == 0 || real_ntree_limit > model.trees.size()) { real_ntree_limit = static_cast<uint32_t>(model.trees.size()); } const int ngroup = model.learner_model_param->num_output_group; CHECK_NE(ngroup, 0); // allocate space for (number of features + bias) times the number of rows size_t contributions_columns = model.learner_model_param->num_feature + 1; // +1 for bias out_contribs->Resize(p_fmat->Info().num_row_ * contributions_columns * contributions_columns * model.learner_model_param->num_output_group); out_contribs->Fill(0.0f); auto phis = out_contribs->DeviceSpan(); p_fmat->Info().base_margin_.SetDevice(generic_param_->gpu_id); const auto margin = p_fmat->Info().base_margin_.ConstDeviceSpan(); float base_score = model.learner_model_param->base_score; // Add the base margin term to last column size_t n_features = model.learner_model_param->num_feature; dh::LaunchN( generic_param_->gpu_id, p_fmat->Info().num_row_ * model.learner_model_param->num_output_group, [=] __device__(size_t idx) { size_t group = idx % ngroup; size_t row_idx = idx / ngroup; phis[gpu_treeshap::IndexPhiInteractions( row_idx, ngroup, group, n_features, n_features, n_features)] = margin.empty() ? base_score : margin[idx]; }); dh::device_vector<gpu_treeshap::PathElement> device_paths; ExtractPaths(&device_paths, model, real_ntree_limit, generic_param_->gpu_id); for (auto& batch : p_fmat->GetBatches<SparsePage>()) { batch.data.SetDevice(generic_param_->gpu_id); batch.offset.SetDevice(generic_param_->gpu_id); SparsePageView X(batch.data.DeviceSpan(), batch.offset.DeviceSpan(), model.learner_model_param->num_feature); gpu_treeshap::GPUTreeShapInteractions( X, device_paths.begin(), device_paths.end(), ngroup, phis.data() + batch.base_rowid * contributions_columns, phis.size()); } } protected: void InitOutPredictions(const MetaInfo& info, HostDeviceVector<bst_float>* out_preds, const gbm::GBTreeModel& model) const { size_t n_classes = model.learner_model_param->num_output_group; size_t n = n_classes * info.num_row_; const HostDeviceVector<bst_float>& base_margin = info.base_margin_; out_preds->SetDevice(generic_param_->gpu_id); out_preds->Resize(n); if (base_margin.Size() != 0) { CHECK_EQ(base_margin.Size(), n); out_preds->Copy(base_margin); } else { out_preds->Fill(model.learner_model_param->base_score); } } void PredictInstance(const SparsePage::Inst&, std::vector<bst_float>*, const gbm::GBTreeModel&, unsigned) override { LOG(FATAL) << "[Internal error]: " << __func__ << " is not implemented in GPU Predictor."; } void PredictLeaf(DMatrix*, std::vector<bst_float>*, const gbm::GBTreeModel&, unsigned) override { LOG(FATAL) << "[Internal error]: " << __func__ << " is not implemented in GPU Predictor."; } void Configure(const std::vector<std::pair<std::string, std::string>>& cfg) override { Predictor::Configure(cfg); } private: /*! \brief Reconfigure the device when GPU is changed. */ void ConfigureDevice(int device) { if (device >= 0) { max_shared_memory_bytes_ = dh::MaxSharedMemory(device); } } std::mutex lock_; DeviceModel model_; size_t max_shared_memory_bytes_; }; XGBOOST_REGISTER_PREDICTOR(GPUPredictor, "gpu_predictor") .describe("Make predictions using GPU.") .set_body([](GenericParameter const* generic_param) { return new GPUPredictor(generic_param); }); } // namespace predictor } // namespace xgboost
87ed9cf09d44e562bb766ae56cf7f6e387275e97.cu
/*! * Copyright 2017-2020 by Contributors */ #include <thrust/copy.h> #include <thrust/device_ptr.h> #include <thrust/device_vector.h> #include <thrust/fill.h> #include <GPUTreeShap/gpu_treeshap.h> #include <memory> #include "xgboost/data.h" #include "xgboost/predictor.h" #include "xgboost/tree_model.h" #include "xgboost/tree_updater.h" #include "xgboost/host_device_vector.h" #include "../gbm/gbtree_model.h" #include "../data/ellpack_page.cuh" #include "../data/device_adapter.cuh" #include "../common/common.h" #include "../common/bitfield.h" #include "../common/categorical.h" #include "../common/device_helpers.cuh" namespace xgboost { namespace predictor { DMLC_REGISTRY_FILE_TAG(gpu_predictor); struct SparsePageView { common::Span<const Entry> d_data; common::Span<const bst_row_t> d_row_ptr; bst_feature_t num_features; SparsePageView() = default; XGBOOST_DEVICE SparsePageView(common::Span<const Entry> data, common::Span<const bst_row_t> row_ptr, bst_feature_t num_features) : d_data{data}, d_row_ptr{row_ptr}, num_features(num_features) {} __device__ float GetElement(size_t ridx, size_t fidx) const { // Binary search auto begin_ptr = d_data.begin() + d_row_ptr[ridx]; auto end_ptr = d_data.begin() + d_row_ptr[ridx + 1]; if (end_ptr - begin_ptr == this->NumCols()) { // Bypass span check for dense data return d_data.data()[d_row_ptr[ridx] + fidx].fvalue; } common::Span<const Entry>::iterator previous_middle; while (end_ptr != begin_ptr) { auto middle = begin_ptr + (end_ptr - begin_ptr) / 2; if (middle == previous_middle) { break; } else { previous_middle = middle; } if (middle->index == fidx) { return middle->fvalue; } else if (middle->index < fidx) { begin_ptr = middle; } else { end_ptr = middle; } } // Value is missing return nanf(""); } XGBOOST_DEVICE size_t NumRows() const { return d_row_ptr.size() - 1; } XGBOOST_DEVICE size_t NumCols() const { return num_features; } }; struct SparsePageLoader { bool use_shared; SparsePageView data; float* smem; size_t entry_start; __device__ SparsePageLoader(SparsePageView data, bool use_shared, bst_feature_t num_features, bst_row_t num_rows, size_t entry_start) : use_shared(use_shared), data(data), entry_start(entry_start) { extern __shared__ float _smem[]; smem = _smem; // Copy instances if (use_shared) { bst_uint global_idx = blockDim.x * blockIdx.x + threadIdx.x; int shared_elements = blockDim.x * data.num_features; dh::BlockFill(smem, shared_elements, nanf("")); __syncthreads(); if (global_idx < num_rows) { bst_uint elem_begin = data.d_row_ptr[global_idx]; bst_uint elem_end = data.d_row_ptr[global_idx + 1]; for (bst_uint elem_idx = elem_begin; elem_idx < elem_end; elem_idx++) { Entry elem = data.d_data[elem_idx - entry_start]; smem[threadIdx.x * data.num_features + elem.index] = elem.fvalue; } } __syncthreads(); } } __device__ float GetElement(size_t ridx, size_t fidx) const { if (use_shared) { return smem[threadIdx.x * data.num_features + fidx]; } else { return data.GetElement(ridx, fidx); } } }; struct EllpackLoader { EllpackDeviceAccessor const& matrix; XGBOOST_DEVICE EllpackLoader(EllpackDeviceAccessor const& m, bool, bst_feature_t, bst_row_t, size_t) : matrix{m} {} __device__ __forceinline__ float GetElement(size_t ridx, size_t fidx) const { auto gidx = matrix.GetBinIndex(ridx, fidx); if (gidx == -1) { return nan(""); } // The gradient index needs to be shifted by one as min values are not included in the // cuts. if (gidx == matrix.feature_segments[fidx]) { return matrix.min_fvalue[fidx]; } return matrix.gidx_fvalue_map[gidx - 1]; } }; template <typename Batch> struct DeviceAdapterLoader { Batch batch; bst_feature_t columns; float* smem; bool use_shared; using BatchT = Batch; XGBOOST_DEV_INLINE DeviceAdapterLoader(Batch const batch, bool use_shared, bst_feature_t num_features, bst_row_t num_rows, size_t entry_start) : batch{batch}, columns{num_features}, use_shared{use_shared} { extern __shared__ float _smem[]; smem = _smem; if (use_shared) { uint32_t global_idx = blockDim.x * blockIdx.x + threadIdx.x; size_t shared_elements = blockDim.x * num_features; dh::BlockFill(smem, shared_elements, nanf("")); __syncthreads(); if (global_idx < num_rows) { auto beg = global_idx * columns; auto end = (global_idx + 1) * columns; for (size_t i = beg; i < end; ++i) { smem[threadIdx.x * num_features + (i - beg)] = batch.GetElement(i).value; } } } __syncthreads(); } XGBOOST_DEV_INLINE float GetElement(size_t ridx, size_t fidx) const { if (use_shared) { return smem[threadIdx.x * columns + fidx]; } return batch.GetElement(ridx * columns + fidx).value; } }; template <typename Loader> __device__ float GetLeafWeight(bst_uint ridx, const RegTree::Node* tree, common::Span<FeatureType const> split_types, common::Span<RegTree::Segment const> d_cat_ptrs, common::Span<uint32_t const> d_categories, Loader* loader) { bst_node_t nidx = 0; RegTree::Node n = tree[nidx]; while (!n.IsLeaf()) { float fvalue = loader->GetElement(ridx, n.SplitIndex()); // Missing value if (common::CheckNAN(fvalue)) { nidx = n.DefaultChild(); } else { bool go_left = true; if (common::IsCat(split_types, nidx)) { auto categories = d_categories.subspan(d_cat_ptrs[nidx].beg, d_cat_ptrs[nidx].size); go_left = Decision(categories, common::AsCat(fvalue)); } else { go_left = fvalue < n.SplitCond(); } if (go_left) { nidx = n.LeftChild(); } else { nidx = n.RightChild(); } } n = tree[nidx]; } return tree[nidx].LeafValue(); } template <typename Loader, typename Data> __global__ void PredictKernel(Data data, common::Span<const RegTree::Node> d_nodes, common::Span<float> d_out_predictions, common::Span<size_t const> d_tree_segments, common::Span<int const> d_tree_group, common::Span<FeatureType const> d_tree_split_types, common::Span<uint32_t const> d_cat_tree_segments, common::Span<RegTree::Segment const> d_cat_node_segments, common::Span<uint32_t const> d_categories, size_t tree_begin, size_t tree_end, size_t num_features, size_t num_rows, size_t entry_start, bool use_shared, int num_group) { bst_uint global_idx = blockDim.x * blockIdx.x + threadIdx.x; Loader loader(data, use_shared, num_features, num_rows, entry_start); if (global_idx >= num_rows) return; if (num_group == 1) { float sum = 0; for (int tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { const RegTree::Node* d_tree = &d_nodes[d_tree_segments[tree_idx - tree_begin]]; auto tree_cat_ptrs = d_cat_node_segments.subspan( d_tree_segments[tree_idx - tree_begin], d_tree_segments[tree_idx - tree_begin + 1] - d_tree_segments[tree_idx - tree_begin]); auto tree_categories = d_categories.subspan(d_cat_tree_segments[tree_idx - tree_begin], d_cat_tree_segments[tree_idx - tree_begin + 1] - d_cat_tree_segments[tree_idx - tree_begin]); float leaf = GetLeafWeight(global_idx, d_tree, d_tree_split_types, tree_cat_ptrs, tree_categories, &loader); sum += leaf; } d_out_predictions[global_idx] += sum; } else { for (int tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { int tree_group = d_tree_group[tree_idx]; const RegTree::Node* d_tree = &d_nodes[d_tree_segments[tree_idx - tree_begin]]; bst_uint out_prediction_idx = global_idx * num_group + tree_group; auto tree_cat_ptrs = d_cat_node_segments.subspan( d_tree_segments[tree_idx - tree_begin], d_tree_segments[tree_idx - tree_begin + 1] - d_tree_segments[tree_idx - tree_begin]); auto tree_categories = d_categories.subspan(d_cat_tree_segments[tree_idx - tree_begin], d_cat_tree_segments[tree_idx - tree_begin + 1] - d_cat_tree_segments[tree_idx - tree_begin]); d_out_predictions[out_prediction_idx] += GetLeafWeight(global_idx, d_tree, d_tree_split_types, tree_cat_ptrs, tree_categories, &loader); } } } class DeviceModel { public: // Need to lazily construct the vectors because GPU id is only known at runtime HostDeviceVector<RTreeNodeStat> stats; HostDeviceVector<size_t> tree_segments; HostDeviceVector<RegTree::Node> nodes; HostDeviceVector<int> tree_group; HostDeviceVector<FeatureType> split_types; // Pointer to each tree, segmenting the node array. HostDeviceVector<uint32_t> categories_tree_segments; // Pointer to each node, segmenting categories array. HostDeviceVector<RegTree::Segment> categories_node_segments; HostDeviceVector<uint32_t> categories; size_t tree_beg_; // NOLINT size_t tree_end_; // NOLINT int num_group; void Init(const gbm::GBTreeModel& model, size_t tree_begin, size_t tree_end, int32_t gpu_id) { dh::safe_cuda(cudaSetDevice(gpu_id)); CHECK_EQ(model.param.size_leaf_vector, 0); // Copy decision trees to device tree_segments = std::move(HostDeviceVector<size_t>({}, gpu_id)); auto& h_tree_segments = tree_segments.HostVector(); h_tree_segments.reserve((tree_end - tree_begin) + 1); size_t sum = 0; h_tree_segments.push_back(sum); for (auto tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { sum += model.trees.at(tree_idx)->GetNodes().size(); h_tree_segments.push_back(sum); } nodes = std::move(HostDeviceVector<RegTree::Node>(h_tree_segments.back(), RegTree::Node(), gpu_id)); stats = std::move(HostDeviceVector<RTreeNodeStat>(h_tree_segments.back(), RTreeNodeStat(), gpu_id)); auto d_nodes = nodes.DevicePointer(); auto d_stats = stats.DevicePointer(); for (auto tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { auto& src_nodes = model.trees.at(tree_idx)->GetNodes(); auto& src_stats = model.trees.at(tree_idx)->GetStats(); dh::safe_cuda(cudaMemcpyAsync( d_nodes + h_tree_segments[tree_idx - tree_begin], src_nodes.data(), sizeof(RegTree::Node) * src_nodes.size(), cudaMemcpyDefault)); dh::safe_cuda(cudaMemcpyAsync( d_stats + h_tree_segments[tree_idx - tree_begin], src_stats.data(), sizeof(RTreeNodeStat) * src_stats.size(), cudaMemcpyDefault)); } tree_group = std::move(HostDeviceVector<int>(model.tree_info.size(), 0, gpu_id)); auto& h_tree_group = tree_group.HostVector(); std::memcpy(h_tree_group.data(), model.tree_info.data(), sizeof(int) * model.tree_info.size()); // Initialize categorical splits. split_types.SetDevice(gpu_id); std::vector<FeatureType>& h_split_types = split_types.HostVector(); h_split_types.resize(h_tree_segments.back()); for (auto tree_idx = tree_begin; tree_idx < tree_end; ++tree_idx) { auto const& src_st = model.trees.at(tree_idx)->GetSplitTypes(); std::copy(src_st.cbegin(), src_st.cend(), h_split_types.begin() + h_tree_segments[tree_idx - tree_begin]); } categories = HostDeviceVector<uint32_t>({}, gpu_id); categories_tree_segments = HostDeviceVector<uint32_t>(1, 0, gpu_id); std::vector<uint32_t> &h_categories = categories.HostVector(); std::vector<uint32_t> &h_split_cat_segments = categories_tree_segments.HostVector(); for (auto tree_idx = tree_begin; tree_idx < tree_end; ++tree_idx) { auto const& src_cats = model.trees.at(tree_idx)->GetSplitCategories(); size_t orig_size = h_categories.size(); h_categories.resize(orig_size + src_cats.size()); std::copy(src_cats.cbegin(), src_cats.cend(), h_categories.begin() + orig_size); h_split_cat_segments.push_back(h_categories.size()); } categories_node_segments = HostDeviceVector<RegTree::Segment>(h_tree_segments.back(), {}, gpu_id); std::vector<RegTree::Segment> &h_categories_node_segments = categories_node_segments.HostVector(); for (auto tree_idx = tree_begin; tree_idx < tree_end; ++tree_idx) { auto const &src_cats_ptr = model.trees.at(tree_idx)->GetSplitCategoriesPtr(); std::copy(src_cats_ptr.cbegin(), src_cats_ptr.cend(), h_categories_node_segments.begin() + h_tree_segments[tree_idx - tree_begin]); } this->tree_beg_ = tree_begin; this->tree_end_ = tree_end; this->num_group = model.learner_model_param->num_output_group; } }; struct PathInfo { int64_t leaf_position; // -1 not a leaf size_t length; size_t tree_idx; }; // Transform model into path element form for GPUTreeShap void ExtractPaths(dh::device_vector<gpu_treeshap::PathElement>* paths, const gbm::GBTreeModel& model, size_t tree_limit, int gpu_id) { DeviceModel device_model; device_model.Init(model, 0, tree_limit, gpu_id); dh::caching_device_vector<PathInfo> info(device_model.nodes.Size()); dh::XGBCachingDeviceAllocator<PathInfo> alloc; auto d_nodes = device_model.nodes.ConstDeviceSpan(); auto d_tree_segments = device_model.tree_segments.ConstDeviceSpan(); auto nodes_transform = dh::MakeTransformIterator<PathInfo>( thrust::make_counting_iterator(0ull), [=] __device__(size_t idx) { auto n = d_nodes[idx]; if (!n.IsLeaf() || n.IsDeleted()) { return PathInfo{-1, 0, 0}; } size_t tree_idx = dh::SegmentId(d_tree_segments.begin(), d_tree_segments.end(), idx); size_t tree_offset = d_tree_segments[tree_idx]; size_t path_length = 1; while (!n.IsRoot()) { n = d_nodes[n.Parent() + tree_offset]; path_length++; } return PathInfo{int64_t(idx), path_length, tree_idx}; }); auto end = thrust::copy_if( thrust::cuda::par(alloc), nodes_transform, nodes_transform + d_nodes.size(), info.begin(), [=] __device__(const PathInfo& e) { return e.leaf_position != -1; }); info.resize(end - info.begin()); auto length_iterator = dh::MakeTransformIterator<size_t>( info.begin(), [=] __device__(const PathInfo& info) { return info.length; }); dh::caching_device_vector<size_t> path_segments(info.size() + 1); thrust::exclusive_scan(thrust::cuda::par(alloc), length_iterator, length_iterator + info.size() + 1, path_segments.begin()); paths->resize(path_segments.back()); auto d_paths = paths->data().get(); auto d_info = info.data().get(); auto d_stats = device_model.stats.ConstDeviceSpan(); auto d_tree_group = device_model.tree_group.ConstDeviceSpan(); auto d_path_segments = path_segments.data().get(); dh::LaunchN(gpu_id, info.size(), [=] __device__(size_t idx) { auto path_info = d_info[idx]; size_t tree_offset = d_tree_segments[path_info.tree_idx]; int group = d_tree_group[path_info.tree_idx]; size_t child_idx = path_info.leaf_position; auto child = d_nodes[child_idx]; float v = child.LeafValue(); const float inf = std::numeric_limits<float>::infinity(); size_t output_position = d_path_segments[idx + 1] - 1; while (!child.IsRoot()) { size_t parent_idx = tree_offset + child.Parent(); double child_cover = d_stats[child_idx].sum_hess; double parent_cover = d_stats[parent_idx].sum_hess; double zero_fraction = child_cover / parent_cover; auto parent = d_nodes[parent_idx]; bool is_left_path = (tree_offset + parent.LeftChild()) == child_idx; bool is_missing_path = (!parent.DefaultLeft() && !is_left_path) || (parent.DefaultLeft() && is_left_path); float lower_bound = is_left_path ? -inf : parent.SplitCond(); float upper_bound = is_left_path ? parent.SplitCond() : inf; d_paths[output_position--] = { idx, parent.SplitIndex(), group, lower_bound, upper_bound, is_missing_path, zero_fraction, v}; child_idx = parent_idx; child = parent; } // Root node has feature -1 d_paths[output_position] = {idx, -1, group, -inf, inf, false, 1.0, v}; }); } class GPUPredictor : public xgboost::Predictor { private: void PredictInternal(const SparsePage& batch, size_t num_features, HostDeviceVector<bst_float>* predictions, size_t batch_offset) { batch.offset.SetDevice(generic_param_->gpu_id); batch.data.SetDevice(generic_param_->gpu_id); const uint32_t BLOCK_THREADS = 128; size_t num_rows = batch.Size(); auto GRID_SIZE = static_cast<uint32_t>(common::DivRoundUp(num_rows, BLOCK_THREADS)); auto shared_memory_bytes = static_cast<size_t>(sizeof(float) * num_features * BLOCK_THREADS); bool use_shared = true; if (shared_memory_bytes > max_shared_memory_bytes_) { shared_memory_bytes = 0; use_shared = false; } size_t entry_start = 0; SparsePageView data(batch.data.DeviceSpan(), batch.offset.DeviceSpan(), num_features); dh::LaunchKernel {GRID_SIZE, BLOCK_THREADS, shared_memory_bytes} ( PredictKernel<SparsePageLoader, SparsePageView>, data, model_.nodes.ConstDeviceSpan(), predictions->DeviceSpan().subspan(batch_offset), model_.tree_segments.ConstDeviceSpan(), model_.tree_group.ConstDeviceSpan(), model_.split_types.ConstDeviceSpan(), model_.categories_tree_segments.ConstDeviceSpan(), model_.categories_node_segments.ConstDeviceSpan(), model_.categories.ConstDeviceSpan(), model_.tree_beg_, model_.tree_end_, num_features, num_rows, entry_start, use_shared, model_.num_group); } void PredictInternal(EllpackDeviceAccessor const& batch, HostDeviceVector<bst_float>* out_preds, size_t batch_offset) { const uint32_t BLOCK_THREADS = 256; size_t num_rows = batch.n_rows; auto GRID_SIZE = static_cast<uint32_t>(common::DivRoundUp(num_rows, BLOCK_THREADS)); bool use_shared = false; size_t entry_start = 0; dh::LaunchKernel {GRID_SIZE, BLOCK_THREADS} ( PredictKernel<EllpackLoader, EllpackDeviceAccessor>, batch, model_.nodes.ConstDeviceSpan(), out_preds->DeviceSpan().subspan(batch_offset), model_.tree_segments.ConstDeviceSpan(), model_.tree_group.ConstDeviceSpan(), model_.split_types.ConstDeviceSpan(), model_.categories_tree_segments.ConstDeviceSpan(), model_.categories_node_segments.ConstDeviceSpan(), model_.categories.ConstDeviceSpan(), model_.tree_beg_, model_.tree_end_, batch.NumFeatures(), num_rows, entry_start, use_shared, model_.num_group); } void DevicePredictInternal(DMatrix* dmat, HostDeviceVector<float>* out_preds, const gbm::GBTreeModel& model, size_t tree_begin, size_t tree_end) { dh::safe_cuda(cudaSetDevice(generic_param_->gpu_id)); if (tree_end - tree_begin == 0) { return; } model_.Init(model, tree_begin, tree_end, generic_param_->gpu_id); out_preds->SetDevice(generic_param_->gpu_id); auto const& info = dmat->Info(); if (dmat->PageExists<SparsePage>()) { size_t batch_offset = 0; for (auto &batch : dmat->GetBatches<SparsePage>()) { this->PredictInternal(batch, model.learner_model_param->num_feature, out_preds, batch_offset); batch_offset += batch.Size() * model.learner_model_param->num_output_group; } } else { size_t batch_offset = 0; for (auto const& page : dmat->GetBatches<EllpackPage>()) { this->PredictInternal( page.Impl()->GetDeviceAccessor(generic_param_->gpu_id), out_preds, batch_offset); batch_offset += page.Impl()->n_rows; } } } public: explicit GPUPredictor(GenericParameter const* generic_param) : Predictor::Predictor{generic_param} {} ~GPUPredictor() override { if (generic_param_->gpu_id >= 0) { dh::safe_cuda(cudaSetDevice(generic_param_->gpu_id)); } } void PredictBatch(DMatrix* dmat, PredictionCacheEntry* predts, const gbm::GBTreeModel& model, int tree_begin, unsigned ntree_limit = 0) override { // This function is duplicated with CPU predictor PredictBatch, see comments in there. // FIXME(trivialfis): Remove the duplication. std::lock_guard<std::mutex> const guard(lock_); int device = generic_param_->gpu_id; CHECK_GE(device, 0) << "Set `gpu_id' to positive value for processing GPU data."; ConfigureDevice(device); CHECK_EQ(tree_begin, 0); auto* out_preds = &predts->predictions; CHECK_GE(predts->version, tree_begin); if (out_preds->Size() == 0 && dmat->Info().num_row_ != 0) { CHECK_EQ(predts->version, 0); } if (predts->version == 0) { this->InitOutPredictions(dmat->Info(), out_preds, model); } uint32_t const output_groups = model.learner_model_param->num_output_group; CHECK_NE(output_groups, 0); uint32_t real_ntree_limit = ntree_limit * output_groups; if (real_ntree_limit == 0 || real_ntree_limit > model.trees.size()) { real_ntree_limit = static_cast<uint32_t>(model.trees.size()); } uint32_t const end_version = (tree_begin + real_ntree_limit) / output_groups; if (predts->version > end_version) { CHECK_NE(ntree_limit, 0); this->InitOutPredictions(dmat->Info(), out_preds, model); predts->version = 0; } uint32_t const beg_version = predts->version; CHECK_LE(beg_version, end_version); if (beg_version < end_version) { this->DevicePredictInternal(dmat, out_preds, model, beg_version * output_groups, end_version * output_groups); } uint32_t delta = end_version - beg_version; CHECK_LE(delta, model.trees.size()); predts->Update(delta); CHECK(out_preds->Size() == output_groups * dmat->Info().num_row_ || out_preds->Size() == dmat->Info().num_row_); } template <typename Adapter, typename Loader> void DispatchedInplacePredict(dmlc::any const &x, const gbm::GBTreeModel &model, float, PredictionCacheEntry *out_preds, uint32_t tree_begin, uint32_t tree_end) const { auto max_shared_memory_bytes = dh::MaxSharedMemory(this->generic_param_->gpu_id); uint32_t const output_groups = model.learner_model_param->num_output_group; DeviceModel d_model; d_model.Init(model, tree_begin, tree_end, this->generic_param_->gpu_id); auto m = dmlc::get<std::shared_ptr<Adapter>>(x); CHECK_EQ(m->NumColumns(), model.learner_model_param->num_feature) << "Number of columns in data must equal to trained model."; CHECK_EQ(this->generic_param_->gpu_id, m->DeviceIdx()) << "XGBoost is running on device: " << this->generic_param_->gpu_id << ", " << "but data is on: " << m->DeviceIdx(); MetaInfo info; info.num_col_ = m->NumColumns(); info.num_row_ = m->NumRows(); this->InitOutPredictions(info, &(out_preds->predictions), model); const uint32_t BLOCK_THREADS = 128; auto GRID_SIZE = static_cast<uint32_t>(common::DivRoundUp(info.num_row_, BLOCK_THREADS)); auto shared_memory_bytes = static_cast<size_t>(sizeof(float) * m->NumColumns() * BLOCK_THREADS); bool use_shared = true; if (shared_memory_bytes > max_shared_memory_bytes) { shared_memory_bytes = 0; use_shared = false; } size_t entry_start = 0; dh::LaunchKernel {GRID_SIZE, BLOCK_THREADS, shared_memory_bytes} ( PredictKernel<Loader, typename Loader::BatchT>, m->Value(), d_model.nodes.ConstDeviceSpan(), out_preds->predictions.DeviceSpan(), d_model.tree_segments.ConstDeviceSpan(), d_model.tree_group.ConstDeviceSpan(), d_model.split_types.ConstDeviceSpan(), d_model.categories_tree_segments.ConstDeviceSpan(), d_model.categories_node_segments.ConstDeviceSpan(), d_model.categories.ConstDeviceSpan(), tree_begin, tree_end, m->NumColumns(), info.num_row_, entry_start, use_shared, output_groups); } void InplacePredict(dmlc::any const &x, const gbm::GBTreeModel &model, float missing, PredictionCacheEntry *out_preds, uint32_t tree_begin, unsigned tree_end) const override { if (x.type() == typeid(std::shared_ptr<data::CupyAdapter>)) { this->DispatchedInplacePredict< data::CupyAdapter, DeviceAdapterLoader<data::CupyAdapterBatch>>( x, model, missing, out_preds, tree_begin, tree_end); } else if (x.type() == typeid(std::shared_ptr<data::CudfAdapter>)) { this->DispatchedInplacePredict< data::CudfAdapter, DeviceAdapterLoader<data::CudfAdapterBatch>>( x, model, missing, out_preds, tree_begin, tree_end); } else { LOG(FATAL) << "Only CuPy and CuDF are supported by GPU Predictor."; } } void PredictContribution(DMatrix* p_fmat, HostDeviceVector<bst_float>* out_contribs, const gbm::GBTreeModel& model, unsigned ntree_limit, std::vector<bst_float>*, bool approximate, int, unsigned) override { if (approximate) { LOG(FATAL) << "Approximated contribution is not implemented in GPU Predictor."; } dh::safe_cuda(cudaSetDevice(generic_param_->gpu_id)); out_contribs->SetDevice(generic_param_->gpu_id); uint32_t real_ntree_limit = ntree_limit * model.learner_model_param->num_output_group; if (real_ntree_limit == 0 || real_ntree_limit > model.trees.size()) { real_ntree_limit = static_cast<uint32_t>(model.trees.size()); } const int ngroup = model.learner_model_param->num_output_group; CHECK_NE(ngroup, 0); // allocate space for (number of features + bias) times the number of rows size_t contributions_columns = model.learner_model_param->num_feature + 1; // +1 for bias out_contribs->Resize(p_fmat->Info().num_row_ * contributions_columns * model.learner_model_param->num_output_group); out_contribs->Fill(0.0f); auto phis = out_contribs->DeviceSpan(); p_fmat->Info().base_margin_.SetDevice(generic_param_->gpu_id); const auto margin = p_fmat->Info().base_margin_.ConstDeviceSpan(); float base_score = model.learner_model_param->base_score; // Add the base margin term to last column dh::LaunchN( generic_param_->gpu_id, p_fmat->Info().num_row_ * model.learner_model_param->num_output_group, [=] __device__(size_t idx) { phis[(idx + 1) * contributions_columns - 1] = margin.empty() ? base_score : margin[idx]; }); dh::device_vector<gpu_treeshap::PathElement> device_paths; ExtractPaths(&device_paths, model, real_ntree_limit, generic_param_->gpu_id); for (auto& batch : p_fmat->GetBatches<SparsePage>()) { batch.data.SetDevice(generic_param_->gpu_id); batch.offset.SetDevice(generic_param_->gpu_id); SparsePageView X(batch.data.DeviceSpan(), batch.offset.DeviceSpan(), model.learner_model_param->num_feature); gpu_treeshap::GPUTreeShap( X, device_paths.begin(), device_paths.end(), ngroup, phis.data() + batch.base_rowid * contributions_columns, phis.size()); } } void PredictInteractionContributions(DMatrix* p_fmat, HostDeviceVector<bst_float>* out_contribs, const gbm::GBTreeModel& model, unsigned ntree_limit, std::vector<bst_float>*, bool approximate) override { if (approximate) { LOG(FATAL) << "[Internal error]: " << __func__ << " approximate is not implemented in GPU Predictor."; } dh::safe_cuda(cudaSetDevice(generic_param_->gpu_id)); out_contribs->SetDevice(generic_param_->gpu_id); uint32_t real_ntree_limit = ntree_limit * model.learner_model_param->num_output_group; if (real_ntree_limit == 0 || real_ntree_limit > model.trees.size()) { real_ntree_limit = static_cast<uint32_t>(model.trees.size()); } const int ngroup = model.learner_model_param->num_output_group; CHECK_NE(ngroup, 0); // allocate space for (number of features + bias) times the number of rows size_t contributions_columns = model.learner_model_param->num_feature + 1; // +1 for bias out_contribs->Resize(p_fmat->Info().num_row_ * contributions_columns * contributions_columns * model.learner_model_param->num_output_group); out_contribs->Fill(0.0f); auto phis = out_contribs->DeviceSpan(); p_fmat->Info().base_margin_.SetDevice(generic_param_->gpu_id); const auto margin = p_fmat->Info().base_margin_.ConstDeviceSpan(); float base_score = model.learner_model_param->base_score; // Add the base margin term to last column size_t n_features = model.learner_model_param->num_feature; dh::LaunchN( generic_param_->gpu_id, p_fmat->Info().num_row_ * model.learner_model_param->num_output_group, [=] __device__(size_t idx) { size_t group = idx % ngroup; size_t row_idx = idx / ngroup; phis[gpu_treeshap::IndexPhiInteractions( row_idx, ngroup, group, n_features, n_features, n_features)] = margin.empty() ? base_score : margin[idx]; }); dh::device_vector<gpu_treeshap::PathElement> device_paths; ExtractPaths(&device_paths, model, real_ntree_limit, generic_param_->gpu_id); for (auto& batch : p_fmat->GetBatches<SparsePage>()) { batch.data.SetDevice(generic_param_->gpu_id); batch.offset.SetDevice(generic_param_->gpu_id); SparsePageView X(batch.data.DeviceSpan(), batch.offset.DeviceSpan(), model.learner_model_param->num_feature); gpu_treeshap::GPUTreeShapInteractions( X, device_paths.begin(), device_paths.end(), ngroup, phis.data() + batch.base_rowid * contributions_columns, phis.size()); } } protected: void InitOutPredictions(const MetaInfo& info, HostDeviceVector<bst_float>* out_preds, const gbm::GBTreeModel& model) const { size_t n_classes = model.learner_model_param->num_output_group; size_t n = n_classes * info.num_row_; const HostDeviceVector<bst_float>& base_margin = info.base_margin_; out_preds->SetDevice(generic_param_->gpu_id); out_preds->Resize(n); if (base_margin.Size() != 0) { CHECK_EQ(base_margin.Size(), n); out_preds->Copy(base_margin); } else { out_preds->Fill(model.learner_model_param->base_score); } } void PredictInstance(const SparsePage::Inst&, std::vector<bst_float>*, const gbm::GBTreeModel&, unsigned) override { LOG(FATAL) << "[Internal error]: " << __func__ << " is not implemented in GPU Predictor."; } void PredictLeaf(DMatrix*, std::vector<bst_float>*, const gbm::GBTreeModel&, unsigned) override { LOG(FATAL) << "[Internal error]: " << __func__ << " is not implemented in GPU Predictor."; } void Configure(const std::vector<std::pair<std::string, std::string>>& cfg) override { Predictor::Configure(cfg); } private: /*! \brief Reconfigure the device when GPU is changed. */ void ConfigureDevice(int device) { if (device >= 0) { max_shared_memory_bytes_ = dh::MaxSharedMemory(device); } } std::mutex lock_; DeviceModel model_; size_t max_shared_memory_bytes_; }; XGBOOST_REGISTER_PREDICTOR(GPUPredictor, "gpu_predictor") .describe("Make predictions using GPU.") .set_body([](GenericParameter const* generic_param) { return new GPUPredictor(generic_param); }); } // namespace predictor } // namespace xgboost
3cddd91d10bf76445f3f0d2ce74ae0f6a7eb18bd.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "includes.h" /* * JCudaVec - Vector operations for JCuda * http://www.jcuda.org * * Copyright (c) 2013-2015 Marco Hutter - http://www.jcuda.org */ extern "C" //=== Vector arithmetic ====================================================== extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector-and-scalar arithmetic =========================================== extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector comparison ====================================================== extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector-and-scalar comparison =========================================== extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector math (one argument) ============================================= // Calculate the arc cosine of the input argument. extern "C" // Calculate the nonnegative arc hyperbolic cosine of the input argument. extern "C" // Calculate the arc sine of the input argument. extern "C" // Calculate the arc hyperbolic sine of the input argument. extern "C" // Calculate the arc tangent of the input argument. extern "C" // Calculate the arc hyperbolic tangent of the input argument. extern "C" // Calculate the cube root of the input argument. extern "C" // Calculate ceiling of the input argument. extern "C" // Calculate the cosine of the input argument. extern "C" // Calculate the hyperbolic cosine of the input argument. extern "C" // Calculate the cosine of the input argument p . extern "C" // Calculate the complementary error function of the input argument. extern "C" // Calculate the inverse complementary error function of the input argument. extern "C" // Calculate the scaled complementary error function of the input argument. extern "C" // Calculate the error function of the input argument. extern "C" // Calculate the inverse error function of the input argument. extern "C" // Calculate the base 10 exponential of the input argument. extern "C" // Calculate the base 2 exponential of the input argument. extern "C" // Calculate the base e exponential of the input argument. extern "C" // Calculate the base e exponential of the input argument, minus 1. extern "C" // Calculate the absolute value of its argument. extern "C" // Calculate the largest integer less than or equal to x. extern "C" // Calculate the value of the Bessel function of the first kind of order 0 for the input argument. extern "C" // Calculate the value of the Bessel function of the first kind of order 1 for the input argument. extern "C" // Calculate the natural logarithm of the absolute value of the gamma function of the input argument. extern "C" // Calculate the base 10 logarithm of the input argument. extern "C" // Calculate the value of l o g e ( 1 + x ) . extern "C" // Calculate the base 2 logarithm of the input argument. extern "C" // Calculate the floating point representation of the exponent of the input argument. extern "C" // Calculate the natural logarithm of the input argument. extern "C" // Calculate the standard normal cumulative distribution function. extern "C" // Calculate the inverse of the standard normal cumulative distribution function. extern "C" // Calculate reciprocal cube root function. extern "C" // Round input to nearest integer value in floating-point. extern "C" // Round to nearest integer value in floating-point. extern "C" // Calculate the reciprocal of the square root of the input argument. extern "C" // Calculate the sine of the input argument. extern "C" // Calculate the hyperbolic sine of the input argument. extern "C" // Calculate the sine of the input argument p . extern "C" // Calculate the square root of the input argument. extern "C" // Calculate the tangent of the input argument. extern "C" // Calculate the hyperbolic tangent of the input argument. extern "C" // Calculate the gamma function of the input argument. extern "C" // Truncate input argument to the integral part. extern "C" // Calculate the value of the Bessel function of the second kind of order 0 for the input argument. extern "C" // Calculate the value of the Bessel function of the second kind of order 1 for the input argument. extern "C" //=== Vector math (two arguments) ============================================ // Create value with given magnitude, copying sign of second value. extern "C" // Compute the positive difference between x and y. extern "C" // Divide two floating point values. extern "C" // Determine the maximum numeric value of the arguments. extern "C" // Determine the minimum numeric value of the arguments. extern "C" // Calculate the floating-point remainder of x / y. extern "C" // Calculate the square root of the sum of squares of two arguments. extern "C" // Return next representable single-precision floating-point value afer argument. extern "C" // Calculate the value of first argument to the power of second argument. extern "C" // Compute single-precision floating-point remainder. extern "C" __global__ void vec_copysignf ( size_t n, float *result, float *x, float *y) { int id = threadIdx.x + blockIdx.x * blockDim.x; if (id < n) { result[id] = copysignf(x[id], y[id]); } }
3cddd91d10bf76445f3f0d2ce74ae0f6a7eb18bd.cu
#include "includes.h" /* * JCudaVec - Vector operations for JCuda * http://www.jcuda.org * * Copyright (c) 2013-2015 Marco Hutter - http://www.jcuda.org */ extern "C" //=== Vector arithmetic ====================================================== extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector-and-scalar arithmetic =========================================== extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector comparison ====================================================== extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector-and-scalar comparison =========================================== extern "C" extern "C" extern "C" extern "C" extern "C" extern "C" //=== Vector math (one argument) ============================================= // Calculate the arc cosine of the input argument. extern "C" // Calculate the nonnegative arc hyperbolic cosine of the input argument. extern "C" // Calculate the arc sine of the input argument. extern "C" // Calculate the arc hyperbolic sine of the input argument. extern "C" // Calculate the arc tangent of the input argument. extern "C" // Calculate the arc hyperbolic tangent of the input argument. extern "C" // Calculate the cube root of the input argument. extern "C" // Calculate ceiling of the input argument. extern "C" // Calculate the cosine of the input argument. extern "C" // Calculate the hyperbolic cosine of the input argument. extern "C" // Calculate the cosine of the input argument × p . extern "C" // Calculate the complementary error function of the input argument. extern "C" // Calculate the inverse complementary error function of the input argument. extern "C" // Calculate the scaled complementary error function of the input argument. extern "C" // Calculate the error function of the input argument. extern "C" // Calculate the inverse error function of the input argument. extern "C" // Calculate the base 10 exponential of the input argument. extern "C" // Calculate the base 2 exponential of the input argument. extern "C" // Calculate the base e exponential of the input argument. extern "C" // Calculate the base e exponential of the input argument, minus 1. extern "C" // Calculate the absolute value of its argument. extern "C" // Calculate the largest integer less than or equal to x. extern "C" // Calculate the value of the Bessel function of the first kind of order 0 for the input argument. extern "C" // Calculate the value of the Bessel function of the first kind of order 1 for the input argument. extern "C" // Calculate the natural logarithm of the absolute value of the gamma function of the input argument. extern "C" // Calculate the base 10 logarithm of the input argument. extern "C" // Calculate the value of l o g e ( 1 + x ) . extern "C" // Calculate the base 2 logarithm of the input argument. extern "C" // Calculate the floating point representation of the exponent of the input argument. extern "C" // Calculate the natural logarithm of the input argument. extern "C" // Calculate the standard normal cumulative distribution function. extern "C" // Calculate the inverse of the standard normal cumulative distribution function. extern "C" // Calculate reciprocal cube root function. extern "C" // Round input to nearest integer value in floating-point. extern "C" // Round to nearest integer value in floating-point. extern "C" // Calculate the reciprocal of the square root of the input argument. extern "C" // Calculate the sine of the input argument. extern "C" // Calculate the hyperbolic sine of the input argument. extern "C" // Calculate the sine of the input argument × p . extern "C" // Calculate the square root of the input argument. extern "C" // Calculate the tangent of the input argument. extern "C" // Calculate the hyperbolic tangent of the input argument. extern "C" // Calculate the gamma function of the input argument. extern "C" // Truncate input argument to the integral part. extern "C" // Calculate the value of the Bessel function of the second kind of order 0 for the input argument. extern "C" // Calculate the value of the Bessel function of the second kind of order 1 for the input argument. extern "C" //=== Vector math (two arguments) ============================================ // Create value with given magnitude, copying sign of second value. extern "C" // Compute the positive difference between x and y. extern "C" // Divide two floating point values. extern "C" // Determine the maximum numeric value of the arguments. extern "C" // Determine the minimum numeric value of the arguments. extern "C" // Calculate the floating-point remainder of x / y. extern "C" // Calculate the square root of the sum of squares of two arguments. extern "C" // Return next representable single-precision floating-point value afer argument. extern "C" // Calculate the value of first argument to the power of second argument. extern "C" // Compute single-precision floating-point remainder. extern "C" __global__ void vec_copysignf ( size_t n, float *result, float *x, float *y) { int id = threadIdx.x + blockIdx.x * blockDim.x; if (id < n) { result[id] = copysignf(x[id], y[id]); } }
90002ac0bdbdca5128c26e4cb0fe7a42ae9167cc.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* -- MAGMA (version 1.3.0) -- Univ. of Tennessee, Knoxville Univ. of California, Berkeley Univ. of Colorado, Denver November 2012 @generated c Wed Nov 14 22:53:45 2012 */ #include "common_magma.h" #define PRECISION_c /*The version for tesla can be found in chemv_tesla.cu */ #if (GPUSHMEM >= 200) #define magmablas_chemv_200 magmablas_chemv #define magmablas_chemv2_200 magmablas_chemv2 #define NB_64 /* turning on NB_64, it will call routine blocksize = 64 otherwise it will can blocksize = 32 which is 10% faster in z,c precision */ #ifdef NB_64// using block size 64 #define chemv_bs 64 #define thread_x 64 #define thread_y 4 #define bank_shift 33 #define quarter_thread_x 16 #define half_thread_x 32 #else // using block size 32 #define chemv_bs 32 #define thread_x 32 #define thread_y 8 #define bank_shift 33 #define SWITCH 1400 #endif /******************************************************************************* * Functions for each specific cases - Lower case */ #ifdef NB_64 __global__ void magmablas_chemv_200_L_special( magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO; cuFloatComplex res_ = MAGMA_C_ZERO; cuFloatComplex res1 = MAGMA_C_ZERO; __shared__ cuFloatComplex la [quarter_thread_x][thread_x+2]; __shared__ cuFloatComplex buff [thread_x]; __shared__ cuFloatComplex buff2 [thread_x]; cuFloatComplex tr[4]; cuFloatComplex b[4]; magma_int_t break_d = thread_x * blkc; const magma_int_t td = (thread_x * ty ) + tx; magma_int_t tx_ = td % half_thread_x; magma_int_t ty_ = td / half_thread_x; WC += break_d + tx; x += (break_d + tx ) * incx; A += break_d * (lda+1); A += ty_* lda + tx_ ; if( ty == 0 ){ buff[tx] = x[0]; } // obtain the vector x store in buff; tx = tx_ ; ty = ty_ ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j +=8) la[0][ bank_shift * (ty_+j) + tx_] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(ty_ * 4 + 4) ; i++){ if ( i < tx_ ) { la[0][bank_shift * tx_ + i] = cuConjf( la[0][ i * bank_shift + tx_] ) ; } else la[0][bank_shift * tx_ + i] = la[0][ bank_shift * tx_ + i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res+= cuConjf( la[0][bank_shift * tx_ + j + ty_ * 4] ) * buff[j + ty_ * 4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_== 0 ) res1 = la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res1,0); } __syncthreads(); MAGMA_C_SET2REAL(res, 0) ; A+= half_thread_x + half_thread_x *lda ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8) la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(4+ty_*4) ; i++){ if ( i < tx_ ) { la[0][bank_shift*tx_+i] = cuConjf( la[0][bank_shift*i+tx_] ) ; } else la[0][bank_shift*tx_+i] = la[0][bank_shift*tx_+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res+= cuConjf( la[0][bank_shift*tx_+j+ty_*4] ) * buff[half_thread_x + j + 4 * ty_]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); cuFloatComplex res2; MAGMA_C_SET2REAL(res2,0); if( ty_== 1 ) res2 = la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); MAGMA_C_SET2REAL(res,0); A-=half_thread_x *lda ; MAGMA_C_SET2REAL(res_,0); #pragma unroll for(magma_int_t j=0; j<half_thread_x; j+=8) tr[j/8] = A[ j * lda]; #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res += tr[j] * buff[ j*8 + ty_]; la[0][bank_shift*(ty_+j*8)+tx_] = tr[j]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+= cuConjf(la[0][bank_shift*tx_+j+ty_*4]) * buff[half_thread_x +j+ty_*4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_ == 1 ) res2 = res2 + la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); la[0][bank_shift*tx_+ty_]= res_ ; __syncthreads(); if( ty_ == 0 ) { res1 = res1 + la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; } else { MAGMA_C_SET2REAL(res1,0); } A-=half_thread_x; __syncthreads(); tx = threadIdx.x ; ty = threadIdx.y ; if( ty_ == 0 && ty == 0 ) res = res1 ; else if( ty_ == 1 && ty == 0 ) res = res2 ; else { MAGMA_C_SET2REAL(res,0); } A-=ty_* lda ; A-=tx_; A= A - lda * blkc * thread_x; x= x - blkc * thread_x *incx ; A+=4 * ty* lda ; A+=tx; magma_int_t wc_c = 0 ; magma_int_t count = 0 ; tx_ = td % quarter_thread_x ; ty_ = td / quarter_thread_x ; WC-=tx ; WC+=tx_; if( blkc * thread_x >=thread_x) #pragma unroll for(magma_int_t i=0; i<thread_x; i += thread_x ) { MAGMA_C_SET2REAL(res_,0); count++; if( ty== 0 ) { buff2[tx] = x[i*incx]; } __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++) { #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda]; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += tr[j] * buff2[ quarter_thread_x * k + ty * 4 + j]; la[( j + ty * 4)][tx] = cuConjf(tr[j]) * buff[tx]; } __syncthreads(); MAGMA_C_SET2REAL(res_,0); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res_+=la[tx_][ty_*4+j] ; } b[k] = res_ ; __syncthreads(); A += lda * quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } for(magma_int_t i=thread_x; i< (blkc * thread_x); i += thread_x ) { MAGMA_C_SET2REAL(res_,0); count++; if( ty== 0 ) { buff2[tx] = x[i*incx]; } __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++) { #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda] ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += tr[j] * buff2[quarter_thread_x*k + ty*4+(j)]; la[( j + ty * 4)][tx] = cuConjf( tr[j] )* buff[tx]; } __syncthreads(); MAGMA_C_SET2REAL(res_,0); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+=la[tx_][ty_*4+j] ; b[k] = res_ ; __syncthreads(); A += lda * quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } WC+=tx ; WC-=tx_; la[ty][tx]= res ; __syncthreads(); if( ty == 0 ) { res = la[0][tx]+ la[1][tx] + la[2][tx]+ la[3][tx]; WC[0+lda*(blkc) ] = res; } } /************************************************************** * Lower case for generic sizes */ __global__ void magmablas_chemv_200_L_generic(magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t m_mod_thread_x) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO; cuFloatComplex res_ = MAGMA_C_ZERO; cuFloatComplex res1 = MAGMA_C_ZERO; __shared__ cuFloatComplex la [quarter_thread_x][thread_x+2]; __shared__ cuFloatComplex buff [thread_x]; __shared__ cuFloatComplex buff2[thread_x]; cuFloatComplex tr[4]; cuFloatComplex b[8]; magma_int_t break_d = thread_x * blkc; const magma_int_t td = (thread_x * ty ) + tx; magma_int_t tx_ = td % half_thread_x; magma_int_t ty_ = td / half_thread_x; WC+= break_d + tx; x += (break_d + tx ) * incx; A += break_d * (lda+1); A += lda * ty_; magma_int_t trackA ; if( blkc == ( gridDim.x - 1 ) ) { if( ty == 0 ){ if( tx > m_mod_thread_x ) { MAGMA_C_SET2REAL(buff[tx],0); } else buff[tx] = x[0]; } if ( tx_ > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx_; A += trackA ; } else { if( ty == 0 ){ buff[tx] = x[0]; } trackA = tx_; A += trackA ; } // Somehow merging these two if - else creates problem // It could be a potential bug -- from synchronization or from cuda or compiler if( blkc == ( gridDim.x - 1 ) ) { #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ if( ( ty_ + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[0][bank_shift*(ty_+j)+tx_], 9999); } else la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } A-=trackA; } else { #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } } tx = tx_ ; ty = ty_ ; __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(ty_*4+4) ; i++){ if ( i < tx_ ) { la[0][bank_shift*tx_+i] = cuConjf(la[0][i*bank_shift+tx_]) ; } else la[0][bank_shift*tx_+i] = la[0][bank_shift*tx_+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[0][bank_shift*tx_+j+ty_*4])* buff[j+ty_*4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_== 0 ) res1 = la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res1,0); } __syncthreads(); MAGMA_C_SET2REAL(res,0); if( blkc == ( gridDim.x - 1 ) ) { if ( (tx_+half_thread_x) > m_mod_thread_x ) trackA = m_mod_thread_x; else trackA = tx_ + half_thread_x; A+= trackA+half_thread_x*lda ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ if( ( ty_ + j+half_thread_x ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[0][bank_shift*(ty_+j)+tx_], 99999); } else la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } A-= trackA+half_thread_x*lda ; A+=tx_ ; A+= half_thread_x + half_thread_x *lda ; } else { A+= half_thread_x + half_thread_x *lda ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } } __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(4+ty_*4) ; i++){ if ( i < tx_ ) { la[0][bank_shift*tx_+i] = cuConjf(la[0][bank_shift*i+tx_]) ; } else la[0][bank_shift*tx_+i] = la[0][bank_shift*tx_+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res+= cuConjf(la[0][bank_shift*tx_+j+ty_*4]) * buff[half_thread_x + j + 4 * ty_]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); cuFloatComplex res2; MAGMA_C_SET2REAL(res2,0); if( ty_== 1 ) res2 = la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); MAGMA_C_SET2REAL(res,0); MAGMA_C_SET2REAL(res_,0); A-=half_thread_x *lda ; if( blkc == ( gridDim.x - 1 ) ) { A-=tx_; if ( tx_ > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx_; A+= trackA ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8) if( ( ty_ + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(tr[j/8], 99999); } else tr[j/8] = A[ j * lda]; A-=trackA; A+=tx_; } else { #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8) tr[j/8] = A[ j * lda]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res+= tr[j] * buff[ j*8 + ty_]; la[0][bank_shift*(ty_+j*8)+tx_] = tr[j]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+= cuConjf(la[0][bank_shift*tx_+j+ty_*4]) * buff[half_thread_x +j+ty_*4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_ == 1 ) res2 = res2 + la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); la[0][bank_shift*tx_+ty_]= res_ ; __syncthreads(); if( ty_ == 0 ) { res1 = res1 + la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; } else { MAGMA_C_SET2REAL(res1,0); } A-=half_thread_x; __syncthreads(); tx = threadIdx.x ; ty = threadIdx.y ; if( ty_ == 0 && ty == 0 ) res = res1 ; else if( ty_ == 1 && ty == 0 ) res = res2 ; else { MAGMA_C_SET2REAL(res,0); } A-=ty_* lda ; A-=tx_; A= A - lda*break_d; x= x - break_d *incx ; A+=4 * ty* lda ; if( blkc == ( gridDim.x - 1 ) ) { if(tx <= m_mod_thread_x ) A+=tx; else A+=m_mod_thread_x; } else{ A+=tx; } magma_int_t wc_c = 0 ; magma_int_t count = 0 ; tx_ = td % quarter_thread_x ; ty_ = td / quarter_thread_x ; WC-=tx ; WC+=tx_; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) b[j] = buff[ty_*4+j]; if( break_d > 0) #pragma unroll for(magma_int_t i=0; i< thread_x; i += thread_x ){ MAGMA_C_SET2REAL(res_,0); count++; if( ty== 0 ) { buff2[tx] = x[i*incx]; } __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++){ #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda] ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res+=tr[j]*buff2[quarter_thread_x*k + ty*4+(j)]; la[( (j)+ty*4)][tx] = cuConjf(tr[j]); } __syncthreads(); MAGMA_C_SET2REAL(res_, 0) ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+=la[tx_][ty_*4+j]* b[j] ; b[4+k] = res_ ; __syncthreads(); A+=lda* quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[4+k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } for(magma_int_t i=thread_x; i<break_d; i += thread_x ){ MAGMA_C_SET2REAL(res_, 0) ; count++; if(ty == 0 ) buff2[tx] = x[i*incx]; __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++){ #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda] ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res+=tr[j]*buff2[quarter_thread_x*k + ty*4+(j)]; la[( (j)+ty*4)][tx] = cuConjf(tr[j]); } __syncthreads(); MAGMA_C_SET2REAL(res_, 0) ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+=la[tx_][ty_*4+j]* b[j] ; b[4+k] = res_ ; __syncthreads(); A+=lda* quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[4+k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } WC+=tx ; WC-=tx_; la[ty][tx]= res ; __syncthreads(); if( ty == 0 ) { res=la[0][tx]+ la[1][tx]+ la[2][tx]+ la[3][tx] ; WC[0+lda*(blkc)] = res; } } __global__ void magmablas_chemv_200_L_update(magma_int_t n, cuFloatComplex alpha, const cuFloatComplex* A, magma_int_t lda, const cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC ) { magma_int_t i; magma_int_t tx = threadIdx.x ; magma_int_t ind = blockIdx.x * thread_x + tx ; cuFloatComplex Ca; MAGMA_C_SET2REAL(Ca, 0) ; WC+= ind + lda * blockIdx.x; for(i = blockIdx.x*thread_x; i<n; i+=thread_x){ Ca += WC[0] ; WC += thread_x; } if( ind < n ) y[ind * incy] = beta * y[ind * incy] + alpha * Ca ; } extern "C" void magmablas_chemv_200_L(magma_int_t m, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy, cuFloatComplex *dC_work) { magma_int_t blocks; if (m % chemv_bs==0) blocks = m / chemv_bs; else blocks = m / chemv_bs + 1; dim3 grid(blocks, 1, 1); dim3 threads(thread_x, thread_y, 1); dim3 threads_u(chemv_bs, 1, 1); /* * If matrix size is multiple of chemv_bs, we use a specific code. * otherwise, we call the generic case. */ if(m % chemv_bs == 0 ) { hipLaunchKernelGGL(( magmablas_chemv_200_L_special) , dim3(grid), dim3(threads), 0, magma_stream , m, alpha, A, lda, X, incx, beta, Y, incy, dC_work); } else{ magma_int_t m_mod_thread_x = m%chemv_bs - 1; hipLaunchKernelGGL(( magmablas_chemv_200_L_generic) , dim3(grid), dim3(threads), 0, magma_stream , m, alpha, A, lda, X, incx ,beta, Y, incy, dC_work, m_mod_thread_x); } hipLaunchKernelGGL(( magmablas_chemv_200_L_update), dim3(grid), dim3(threads_u), 0, magma_stream , m, alpha, A, lda, X, incx, beta, Y, incy, dC_work); } #else /******************************************************************************* * Functions for each specific cases - Lower case nb = 32 */ __global__ void magmablas_chemv_200_L_special_32_s( magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb) { if(blockIdx.y > blockIdx.x) return; magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; cuFloatComplex res = MAGMA_C_ZERO;// used in scan the row cuFloatComplex res_ = MAGMA_C_ZERO;// used in scan the column __shared__ cuFloatComplex la [1056]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blockIdx.x; A += break_d ; A += lda * ty + tx; A += lda * (blockIdx.y ) * chemv_bs; // x += tx; if ( blockIdx.x == blockIdx.y ) // diagonal { x += (blockIdx.y * chemv_bs) * incx; if( ty == 0 ) { buff[tx] = x[0]; } // obtain the vector x store in buff; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[ bank_shift * (ty+j) + tx] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty * 4 + 4) ; i++) { if ( i < tx ) { la[bank_shift * tx + i] = cuConjf(la[ i * bank_shift + tx]) ; } } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; __syncthreads(); } else // non diagonal { x += (blockIdx.x * chemv_bs) * incx; if( ty == 0 ) { buff[tx] = x[0]; } // obtain the vector x and store in buff; buff store its corresponding upper elements instead of buff2; x -= (blockIdx.x * chemv_bs ) * incx; x += (blockIdx.y * chemv_bs ) * incx; if( ty == 0 ) { buff2[tx] = x[0]; } // obtain the vector x store in buff2; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) { la[ bank_shift * (ty+j) + tx] = A[ j * lda]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; // } __syncthreads(); la[bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res_ = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.y * chemv_bs + lda * blockIdx.x ] = res_; // write to its corresponding upper side position } __syncthreads(); } // end if else la[bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.x * chemv_bs + lda * blockIdx.y] = res; } } __global__ void magmablas_chemv_200_L_special_32( magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO;// used in scan the row cuFloatComplex res_ = MAGMA_C_ZERO;// used in scan the column cuFloatComplex res1 = MAGMA_C_ZERO;// tem for res cuFloatComplex res2 = MAGMA_C_ZERO;// tem for res_ __shared__ cuFloatComplex la [16][64+2]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blkc; x += (break_d + tx ) * incx; A += break_d ; A += ty * lda + tx ; if( ty == 0 ) { buff[tx] = x[0]; } // obtain the vector x store in buff; { A += lda * (blkc) * chemv_bs; // change #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[0][ bank_shift * (ty+j) + tx] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty * 4 + 4) ; i++){ if ( i < tx ) { la[0][bank_shift * tx + i] = cuConjf( la[0][ i * bank_shift + tx] ) ; } } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf( la[0][bank_shift * tx + j + ty * 4] ) * buff[j + ty * 4]; __syncthreads(); A -= lda * (blkc) * chemv_bs; } x -= blkc * chemv_bs *incx ; x= x- tx*incx; magma_int_t wc_c = 0 ; magma_int_t count = 0 ; WC += break_d + tx; if( blkc > 0) for(magma_int_t s=0; s< (blkc * chemv_bs); s += chemv_bs ) { MAGMA_C_SET2REAL(res_,0); count++; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[0][ bank_shift * (ty+j) + tx] = A[ j * lda]; if( ty == 0 ) { buff2[tx] = x[tx]; } // obtain the vector x store in buff; __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[0][bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf( la[0][bank_shift * tx + j + ty * 4] ) * buff[j + ty * 4]; //iterate colum } __syncthreads(); la[0][bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res2 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[wc_c*lda ] = res2; } __syncthreads(); wc_c += 1; x += chemv_bs; A += lda * chemv_bs ; } la[0][bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res1 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[0+lda*(blkc)] = res1; } } /************************************************************** * Lower case for generic sizes */ __global__ void magmablas_chemv_200_L_generic_32_s( magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t m_mod_thread_x, magma_int_t nb) { if(blockIdx.y > blockIdx.x) return; magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; cuFloatComplex res = MAGMA_C_ZERO;// used in scan the row cuFloatComplex res_ = MAGMA_C_ZERO;// used in scan the column __shared__ cuFloatComplex la [1056]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blockIdx.x; A += break_d ; A += lda * ty; A += lda * (blockIdx.y ) * chemv_bs; // x += tx; x += (blockIdx.x * chemv_bs) * incx; magma_int_t trackA ; if( blockIdx.x == ( gridDim.x - 1 ) ) { if( ty == 0 ){ if( tx > m_mod_thread_x ) { MAGMA_C_SET2REAL(buff[tx],0); } else buff[tx] = x[0]; } if ( tx > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx; A += trackA ; } else { if( ty == 0 ){ buff[tx] = x[0]; } trackA = tx; A += trackA ; } __syncthreads(); if ( blockIdx.x == blockIdx.y) // diagonal { if( blockIdx.x == ( gridDim.x - 1 ) ) { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ if( ( ty + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[bank_shift*(ty+j)+tx], 9999); } else la[bank_shift*(ty+j)+tx] = A[ j * lda]; } } else { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ la[bank_shift*(ty+j)+tx] = A[ j * lda]; } } __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty * 4 + 4) ; i++) { if ( i < tx ) { la[bank_shift * tx + i] = cuConjf(la[ i * bank_shift + tx]) ; } } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; __syncthreads(); } else // non diagonal { // obtain the vector x and store in buff; buff store its corresponding upper elements instead of buff2; x -= (blockIdx.x * chemv_bs ) * incx; x += (blockIdx.y * chemv_bs ) * incx; if( ty == 0 ) { buff2[tx] = x[0]; } // obtain the vector x store in buff2; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) { la[ bank_shift * (ty+j) + tx] = A[ j * lda]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; // } __syncthreads(); la[bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res_ = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.y * chemv_bs + lda * blockIdx.x ] = res_; // write to its corresponding upper side position } __syncthreads(); } // end if else la[bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.x * chemv_bs + lda * blockIdx.y] = res; } } __global__ void magmablas_chemv_200_L_generic_32(magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t m_mod_thread_x, magma_int_t nb) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO; cuFloatComplex res_ = MAGMA_C_ZERO; cuFloatComplex res1 = MAGMA_C_ZERO; cuFloatComplex res2 = MAGMA_C_ZERO; __shared__ cuFloatComplex la [16][64+2]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blkc; x += (break_d + tx ) * incx; A += break_d ; A += lda * ty; magma_int_t trackA ; if( blkc == ( gridDim.x - 1 ) ) { if( ty == 0 ){ if( tx > m_mod_thread_x ) { MAGMA_C_SET2REAL(buff[tx],0); } else buff[tx] = x[0]; } if ( tx > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx; A += trackA ; } else { if( ty == 0 ){ buff[tx] = x[0]; } trackA = tx; A += trackA ; } { A += lda * (blkc) * chemv_bs; // change // Somehow merging these two if - else creates problem // It could be a potential bug -- from synchronization or from cuda or compiler if( blkc == ( gridDim.x - 1 ) ) { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ if( ( ty + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[0][bank_shift*(ty+j)+tx], 9999); } else la[0][bank_shift*(ty+j)+tx] = A[ j * lda]; } } else { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ la[0][bank_shift*(ty+j)+tx] = A[ j * lda]; } } __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty*4+4) ; i++){ if ( i < tx ) { la[0][bank_shift*tx+i] = cuConjf(la[0][i*bank_shift+tx]) ; } else la[0][bank_shift*tx+i] = la[0][bank_shift*tx+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[0][bank_shift*tx+j+ty*4])* buff[j+ty*4]; __syncthreads(); A -= lda * (blkc) * chemv_bs; } __syncthreads(); x= x - break_d *incx ; x= x - tx * incx ; magma_int_t wc_c = 0 ; magma_int_t count = 0 ; WC += break_d + tx; if( blkc > 0) for(magma_int_t s=0; s< (blkc * chemv_bs); s += chemv_bs ) { MAGMA_C_SET2REAL(res_,0); count++; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[0][ bank_shift * (ty+j) + tx] = A[ j * lda]; __syncthreads(); if( ty == 0 ) { buff2[tx] = x[tx]; } // obtain the vector x store in buff2; __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[0][bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf( la[0][bank_shift * tx + j + ty * 4] ) * buff[j + ty * 4]; //iterate colum } __syncthreads(); la[0][bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res2 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[wc_c*lda ] = res2; } __syncthreads(); wc_c += 1; x += chemv_bs; A += lda * chemv_bs ; } la[0][bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res1 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[0+lda*(blkc)] = res1; } } __global__ void magmablas_chemv_200_L_update_32_s(magma_int_t n, cuFloatComplex alpha, cuFloatComplex* A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb ) { magma_int_t i; magma_int_t tx = threadIdx.x ; magma_int_t ind = blockIdx.x * chemv_bs + tx ; cuFloatComplex Ca; MAGMA_C_SET2REAL(Ca, 0) ; WC+= ind; for(i =0; i<n; i+=chemv_bs){ Ca += WC[i/chemv_bs * lda] ; } if( ind < n ) y[ind * incy] = beta * y[ind * incy] + alpha * Ca ; } __global__ void magmablas_chemv_200_L_update_32(magma_int_t n, cuFloatComplex alpha, cuFloatComplex* A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb ) { magma_int_t i; magma_int_t tx = threadIdx.x ; magma_int_t ind = blockIdx.x * chemv_bs + tx ; cuFloatComplex Ca; MAGMA_C_SET2REAL(Ca, 0) ; WC+= ind + lda * blockIdx.x; for(i = blockIdx.x*chemv_bs; i<n; i+=chemv_bs){ Ca += WC[0] ; WC += chemv_bs; } if( ind < n ) y[ind * incy] = beta * y[ind * incy] + alpha * Ca ; } extern "C" void magmablas_chemv_200_L_32(magma_int_t m, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy, cuFloatComplex *dC_work, magma_int_t nb) { magma_int_t blocks; if (m % chemv_bs==0) blocks = m / chemv_bs; else blocks = m / chemv_bs + 1; dim3 grid(blocks, 1, 1); dim3 grid_s(blocks, blocks, 1); dim3 threads(thread_x, thread_y, 1); dim3 threads_u(chemv_bs, 1, 1); /* * If matrix size is multiple of chemv_bs, we use a specific code. * otherwise, we call the generic case. */ if(m % chemv_bs == 0 ) { if(m < SWITCH) hipLaunchKernelGGL(( magmablas_chemv_200_L_special_32_s) , dim3(grid_s), dim3(threads), 0, magma_stream , m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); else hipLaunchKernelGGL(( magmablas_chemv_200_L_special_32) , dim3(grid), dim3(threads), 0, magma_stream , m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); } else{ magma_int_t m_mod_thread_x = m%chemv_bs - 1; if(m < SWITCH) hipLaunchKernelGGL(( magmablas_chemv_200_L_generic_32_s) , dim3(grid_s), dim3(threads), 0, magma_stream , m, alpha, A, lda, X, incx ,beta, Y, incy, dC_work, m_mod_thread_x, nb); else hipLaunchKernelGGL(( magmablas_chemv_200_L_generic_32) , dim3(grid), dim3(threads), 0, magma_stream , m, alpha, A, lda, X, incx ,beta, Y, incy, dC_work, m_mod_thread_x, nb); } if(m < SWITCH) hipLaunchKernelGGL(( magmablas_chemv_200_L_update_32_s), dim3(grid), dim3(threads_u), 0, magma_stream , m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); else hipLaunchKernelGGL(( magmablas_chemv_200_L_update_32), dim3(grid), dim3(threads_u), 0, magma_stream , m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); } #endif /************************************************************************* Purpose ======= magmablas_chemv performs the matrix-vector operation on fermi: y := alpha*A*x + beta*y, where alpha and beta are scalars, x and y are n element vectors and A is an n by n hermitian matrix. Arguments ========== UPLO - CHARACTER*1. On entry, UPLO specifies whether the upper or lower triangular part of the array A is to be referenced as follows: UPLO = 'U' or 'u' Only the upper triangular part of A is to be referenced. UPLO = 'L' or 'l' Only the lower triangular part of A is to be referenced. Unchanged on exit. N - INTEGER. On entry, N specifies the order of the matrix A. N must be at least zero. Unchanged on exit. ALPHA - COMPLEX*16 . On entry, ALPHA specifies the scalar alpha. Unchanged on exit. A - COMPLEX*16 array of DIMENSION ( LDA, n ). Before entry with UPLO = 'U' or 'u', the leading n by n upper triangular part of the array A must contain the upper triangular part of the hermitian matrix and the strictly lower triangular part of A is not referenced. Before entry with UPLO = 'L' or 'l', the leading n by n lower triangular part of the array A must contain the lower triangular part of the hermitian matrix and the strictly upper triangular part of A is not referenced. Note that the imaginary parts of the diagonal elements need not be set and are assumed to be zero. Unchanged on exit. LDA - INTEGER. On entry, LDA specifies the first dimension of A as declared in the calling (sub) program. LDA must be at least max( 1, n ). Unchanged on exit. It is recommended that lda is multiple of 16. Otherwise performance would be deteriorated as the memory accesses would not be fully coalescent. X - COMPLEX*16 array of dimension at least ( 1 + ( n - 1 )*abs( INCX ) ). Before entry, the incremented array X must contain the n element vector x. Unchanged on exit. INCX - INTEGER. On entry, INCX specifies the increment for the elements of X. INCX must not be zero. Unchanged on exit. BETA - COMPLEX*16 . On entry, BETA specifies the scalar beta. When BETA is supplied as zero then Y need not be set on input. Unchanged on exit. Y - COMPLEX*16 array of dimension at least ( 1 + ( n - 1 )*abs( INCY ) ). Before entry, the incremented array Y must contain the n element vector y. On exit, Y is overwritten by the updated vector y. INCY - INTEGER. On entry, INCY specifies the increment for the elements of Y. INCY must not be zero. Unchanged on exit. */ extern "C" magma_int_t magmablas_chemv_200( char uplo, magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy) { char uplo_[2] = {uplo, 0}; int upper = lapackf77_lsame(uplo_, "U"); /* * Test the input parameters. */ if ((! upper) && (! lapackf77_lsame(uplo_, "L"))) { return -1; } else if ( n < 0 ) { return -2; } else if ( lda < max(1,n) ) { return -5; } else if ( incx == 0 ) { return -7; } else if ( incy == 0 ) { return -10; } /* * Quick return if possible. */ if ( (n == 0) || ( MAGMA_C_EQUAL(alpha, MAGMA_C_ZERO) && MAGMA_C_EQUAL(beta, MAGMA_C_ONE) ) ) return MAGMA_SUCCESS; /* TODO: Upper case is not implemented in MAGMA */ if ( upper ) hipblasChemv(uplo, n, alpha, A, lda, X, incx, beta, Y, incy); else { cuFloatComplex *dC_work; magma_int_t blocks = n / chemv_bs + (n % chemv_bs != 0); magma_int_t workspace = lda * (blocks + 1); /* TODO: need to add a MAGMA context to handle workspaces */ hipblasAlloc( workspace, sizeof(cuFloatComplex), (void**)&dC_work ) ; hipblasGetError( ) ; #ifdef NB_64 magmablas_chemv_200_L(n, alpha, A, lda, X, incx, beta, Y, incy, dC_work); #else magmablas_chemv_200_L_32(n, alpha, A, lda, X, incx, beta, Y, incy, dC_work, chemv_bs); #endif hipblasFree(dC_work); hipblasGetError( ) ; } return MAGMA_SUCCESS; } extern "C" magma_int_t magmablas_chemv2_200( char uplo, magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy, cuFloatComplex *work, int lwork) { char uplo_[2] = {uplo, 0}; int upper = lapackf77_lsame(uplo_, "U"); /* * Test the input parameters. */ if ((! upper) && (! lapackf77_lsame(uplo_, "L"))) { return -1; } else if ( n < 0 ) { return -2; } else if ( lda < max(1,n) ) { return -5; } else if ( incx == 0 ) { return -7; } else if ( incy == 0 ) { return -10; } /* * Quick return if possible. */ if ( (n == 0) || ( MAGMA_C_EQUAL(alpha, MAGMA_C_ZERO) && MAGMA_C_EQUAL(beta, MAGMA_C_ONE) ) ) return MAGMA_SUCCESS; /* TODO: Upper case is not implemented in MAGMA */ if ( upper ) hipblasChemv(uplo, n, alpha, A, lda, X, incx, beta, Y, incy); else { magma_int_t blocks = n / chemv_bs + (n % chemv_bs != 0); magma_int_t workspace = lda * (blocks + 1); if (lwork < workspace){ printf("Not enough work space in magmablas_chemv: passed %d, required %d\n", lwork, workspace); exit(1); } //printf("You are using chemv_bs=%d\n", chemv_bs); #ifdef NB_64 if( n < 1622) hipblasChemv(uplo, n, alpha, A, lda, X, incx, beta, Y, incy); else magmablas_chemv_200_L(n, alpha, A, lda, X, incx, beta, Y, incy, work); #else magmablas_chemv_200_L_32(n, alpha, A, lda, X, incx, beta, Y, incy, work, chemv_bs); #endif } return MAGMA_SUCCESS; } #endif /* (GPUSHMEM >= 200) */
90002ac0bdbdca5128c26e4cb0fe7a42ae9167cc.cu
/* -- MAGMA (version 1.3.0) -- Univ. of Tennessee, Knoxville Univ. of California, Berkeley Univ. of Colorado, Denver November 2012 @generated c Wed Nov 14 22:53:45 2012 */ #include "common_magma.h" #define PRECISION_c /*The version for tesla can be found in chemv_tesla.cu */ #if (GPUSHMEM >= 200) #define magmablas_chemv_200 magmablas_chemv #define magmablas_chemv2_200 magmablas_chemv2 #define NB_64 /* turning on NB_64, it will call routine blocksize = 64 otherwise it will can blocksize = 32 which is 10% faster in z,c precision */ #ifdef NB_64// using block size 64 #define chemv_bs 64 #define thread_x 64 #define thread_y 4 #define bank_shift 33 #define quarter_thread_x 16 #define half_thread_x 32 #else // using block size 32 #define chemv_bs 32 #define thread_x 32 #define thread_y 8 #define bank_shift 33 #define SWITCH 1400 #endif /******************************************************************************* * Functions for each specific cases - Lower case */ #ifdef NB_64 __global__ void magmablas_chemv_200_L_special( magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO; cuFloatComplex res_ = MAGMA_C_ZERO; cuFloatComplex res1 = MAGMA_C_ZERO; __shared__ cuFloatComplex la [quarter_thread_x][thread_x+2]; __shared__ cuFloatComplex buff [thread_x]; __shared__ cuFloatComplex buff2 [thread_x]; cuFloatComplex tr[4]; cuFloatComplex b[4]; magma_int_t break_d = thread_x * blkc; const magma_int_t td = (thread_x * ty ) + tx; magma_int_t tx_ = td % half_thread_x; magma_int_t ty_ = td / half_thread_x; WC += break_d + tx; x += (break_d + tx ) * incx; A += break_d * (lda+1); A += ty_* lda + tx_ ; if( ty == 0 ){ buff[tx] = x[0]; } // obtain the vector x store in buff; tx = tx_ ; ty = ty_ ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j +=8) la[0][ bank_shift * (ty_+j) + tx_] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(ty_ * 4 + 4) ; i++){ if ( i < tx_ ) { la[0][bank_shift * tx_ + i] = cuConjf( la[0][ i * bank_shift + tx_] ) ; } else la[0][bank_shift * tx_ + i] = la[0][ bank_shift * tx_ + i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res+= cuConjf( la[0][bank_shift * tx_ + j + ty_ * 4] ) * buff[j + ty_ * 4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_== 0 ) res1 = la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res1,0); } __syncthreads(); MAGMA_C_SET2REAL(res, 0) ; A+= half_thread_x + half_thread_x *lda ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8) la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(4+ty_*4) ; i++){ if ( i < tx_ ) { la[0][bank_shift*tx_+i] = cuConjf( la[0][bank_shift*i+tx_] ) ; } else la[0][bank_shift*tx_+i] = la[0][bank_shift*tx_+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res+= cuConjf( la[0][bank_shift*tx_+j+ty_*4] ) * buff[half_thread_x + j + 4 * ty_]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); cuFloatComplex res2; MAGMA_C_SET2REAL(res2,0); if( ty_== 1 ) res2 = la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); MAGMA_C_SET2REAL(res,0); A-=half_thread_x *lda ; MAGMA_C_SET2REAL(res_,0); #pragma unroll for(magma_int_t j=0; j<half_thread_x; j+=8) tr[j/8] = A[ j * lda]; #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res += tr[j] * buff[ j*8 + ty_]; la[0][bank_shift*(ty_+j*8)+tx_] = tr[j]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+= cuConjf(la[0][bank_shift*tx_+j+ty_*4]) * buff[half_thread_x +j+ty_*4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_ == 1 ) res2 = res2 + la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); la[0][bank_shift*tx_+ty_]= res_ ; __syncthreads(); if( ty_ == 0 ) { res1 = res1 + la[0][tx_*bank_shift+0]+la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2]+la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4]+la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6]+la[0][tx_*bank_shift+7]; } else { MAGMA_C_SET2REAL(res1,0); } A-=half_thread_x; __syncthreads(); tx = threadIdx.x ; ty = threadIdx.y ; if( ty_ == 0 && ty == 0 ) res = res1 ; else if( ty_ == 1 && ty == 0 ) res = res2 ; else { MAGMA_C_SET2REAL(res,0); } A-=ty_* lda ; A-=tx_; A= A - lda * blkc * thread_x; x= x - blkc * thread_x *incx ; A+=4 * ty* lda ; A+=tx; magma_int_t wc_c = 0 ; magma_int_t count = 0 ; tx_ = td % quarter_thread_x ; ty_ = td / quarter_thread_x ; WC-=tx ; WC+=tx_; if( blkc * thread_x >=thread_x) #pragma unroll for(magma_int_t i=0; i<thread_x; i += thread_x ) { MAGMA_C_SET2REAL(res_,0); count++; if( ty== 0 ) { buff2[tx] = x[i*incx]; } __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++) { #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda]; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += tr[j] * buff2[ quarter_thread_x * k + ty * 4 + j]; la[( j + ty * 4)][tx] = cuConjf(tr[j]) * buff[tx]; } __syncthreads(); MAGMA_C_SET2REAL(res_,0); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res_+=la[tx_][ty_*4+j] ; } b[k] = res_ ; __syncthreads(); A += lda * quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } for(magma_int_t i=thread_x; i< (blkc * thread_x); i += thread_x ) { MAGMA_C_SET2REAL(res_,0); count++; if( ty== 0 ) { buff2[tx] = x[i*incx]; } __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++) { #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda] ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += tr[j] * buff2[quarter_thread_x*k + ty*4+(j)]; la[( j + ty * 4)][tx] = cuConjf( tr[j] )* buff[tx]; } __syncthreads(); MAGMA_C_SET2REAL(res_,0); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+=la[tx_][ty_*4+j] ; b[k] = res_ ; __syncthreads(); A += lda * quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } WC+=tx ; WC-=tx_; la[ty][tx]= res ; __syncthreads(); if( ty == 0 ) { res = la[0][tx]+ la[1][tx] + la[2][tx]+ la[3][tx]; WC[0+lda*(blkc) ] = res; } } /************************************************************** * Lower case for generic sizes */ __global__ void magmablas_chemv_200_L_generic(magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t m_mod_thread_x) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO; cuFloatComplex res_ = MAGMA_C_ZERO; cuFloatComplex res1 = MAGMA_C_ZERO; __shared__ cuFloatComplex la [quarter_thread_x][thread_x+2]; __shared__ cuFloatComplex buff [thread_x]; __shared__ cuFloatComplex buff2[thread_x]; cuFloatComplex tr[4]; cuFloatComplex b[8]; magma_int_t break_d = thread_x * blkc; const magma_int_t td = (thread_x * ty ) + tx; magma_int_t tx_ = td % half_thread_x; magma_int_t ty_ = td / half_thread_x; WC+= break_d + tx; x += (break_d + tx ) * incx; A += break_d * (lda+1); A += lda * ty_; magma_int_t trackA ; if( blkc == ( gridDim.x - 1 ) ) { if( ty == 0 ){ if( tx > m_mod_thread_x ) { MAGMA_C_SET2REAL(buff[tx],0); } else buff[tx] = x[0]; } if ( tx_ > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx_; A += trackA ; } else { if( ty == 0 ){ buff[tx] = x[0]; } trackA = tx_; A += trackA ; } // Somehow merging these two if - else creates problem // It could be a potential bug -- from synchronization or from cuda or compiler if( blkc == ( gridDim.x - 1 ) ) { #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ if( ( ty_ + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[0][bank_shift*(ty_+j)+tx_], 9999); } else la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } A-=trackA; } else { #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } } tx = tx_ ; ty = ty_ ; __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(ty_*4+4) ; i++){ if ( i < tx_ ) { la[0][bank_shift*tx_+i] = cuConjf(la[0][i*bank_shift+tx_]) ; } else la[0][bank_shift*tx_+i] = la[0][bank_shift*tx_+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[0][bank_shift*tx_+j+ty_*4])* buff[j+ty_*4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_== 0 ) res1 = la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res1,0); } __syncthreads(); MAGMA_C_SET2REAL(res,0); if( blkc == ( gridDim.x - 1 ) ) { if ( (tx_+half_thread_x) > m_mod_thread_x ) trackA = m_mod_thread_x; else trackA = tx_ + half_thread_x; A+= trackA+half_thread_x*lda ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ if( ( ty_ + j+half_thread_x ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[0][bank_shift*(ty_+j)+tx_], 99999); } else la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } A-= trackA+half_thread_x*lda ; A+=tx_ ; A+= half_thread_x + half_thread_x *lda ; } else { A+= half_thread_x + half_thread_x *lda ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8){ la[0][bank_shift*(ty_+j)+tx_] = A[ j * lda]; } } __syncthreads(); #pragma unroll for(magma_int_t i=ty_*4; i<(4+ty_*4) ; i++){ if ( i < tx_ ) { la[0][bank_shift*tx_+i] = cuConjf(la[0][bank_shift*i+tx_]) ; } else la[0][bank_shift*tx_+i] = la[0][bank_shift*tx_+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res+= cuConjf(la[0][bank_shift*tx_+j+ty_*4]) * buff[half_thread_x + j + 4 * ty_]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); cuFloatComplex res2; MAGMA_C_SET2REAL(res2,0); if( ty_== 1 ) res2 = la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); MAGMA_C_SET2REAL(res,0); MAGMA_C_SET2REAL(res_,0); A-=half_thread_x *lda ; if( blkc == ( gridDim.x - 1 ) ) { A-=tx_; if ( tx_ > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx_; A+= trackA ; #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8) if( ( ty_ + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(tr[j/8], 99999); } else tr[j/8] = A[ j * lda]; A-=trackA; A+=tx_; } else { #pragma unroll for(magma_int_t j =0; j<half_thread_x; j+=8) tr[j/8] = A[ j * lda]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res+= tr[j] * buff[ j*8 + ty_]; la[0][bank_shift*(ty_+j*8)+tx_] = tr[j]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+= cuConjf(la[0][bank_shift*tx_+j+ty_*4]) * buff[half_thread_x +j+ty_*4]; __syncthreads(); la[0][bank_shift*tx_+ty_]= res ; __syncthreads(); if( ty_ == 1 ) res2 = res2 + la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; else { MAGMA_C_SET2REAL(res2,0); } __syncthreads(); la[0][bank_shift*tx_+ty_]= res_ ; __syncthreads(); if( ty_ == 0 ) { res1 = res1 + la[0][tx_*bank_shift+0] + la[0][tx_*bank_shift+1] + la[0][tx_*bank_shift+2] + la[0][tx_*bank_shift+3] + la[0][tx_*bank_shift+4] + la[0][tx_*bank_shift+5] + la[0][tx_*bank_shift+6] + la[0][tx_*bank_shift+7]; } else { MAGMA_C_SET2REAL(res1,0); } A-=half_thread_x; __syncthreads(); tx = threadIdx.x ; ty = threadIdx.y ; if( ty_ == 0 && ty == 0 ) res = res1 ; else if( ty_ == 1 && ty == 0 ) res = res2 ; else { MAGMA_C_SET2REAL(res,0); } A-=ty_* lda ; A-=tx_; A= A - lda*break_d; x= x - break_d *incx ; A+=4 * ty* lda ; if( blkc == ( gridDim.x - 1 ) ) { if(tx <= m_mod_thread_x ) A+=tx; else A+=m_mod_thread_x; } else{ A+=tx; } magma_int_t wc_c = 0 ; magma_int_t count = 0 ; tx_ = td % quarter_thread_x ; ty_ = td / quarter_thread_x ; WC-=tx ; WC+=tx_; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) b[j] = buff[ty_*4+j]; if( break_d > 0) #pragma unroll for(magma_int_t i=0; i< thread_x; i += thread_x ){ MAGMA_C_SET2REAL(res_,0); count++; if( ty== 0 ) { buff2[tx] = x[i*incx]; } __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++){ #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda] ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res+=tr[j]*buff2[quarter_thread_x*k + ty*4+(j)]; la[( (j)+ty*4)][tx] = cuConjf(tr[j]); } __syncthreads(); MAGMA_C_SET2REAL(res_, 0) ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+=la[tx_][ty_*4+j]* b[j] ; b[4+k] = res_ ; __syncthreads(); A+=lda* quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[4+k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } for(magma_int_t i=thread_x; i<break_d; i += thread_x ){ MAGMA_C_SET2REAL(res_, 0) ; count++; if(ty == 0 ) buff2[tx] = x[i*incx]; __syncthreads(); #pragma unroll for( magma_int_t k=0;k<4;k++){ #pragma unroll for(magma_int_t j=0; j < 4 ; j++) tr[j] = A[j*lda] ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++){ res+=tr[j]*buff2[quarter_thread_x*k + ty*4+(j)]; la[( (j)+ty*4)][tx] = cuConjf(tr[j]); } __syncthreads(); MAGMA_C_SET2REAL(res_, 0) ; #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res_+=la[tx_][ty_*4+j]* b[j] ; b[4+k] = res_ ; __syncthreads(); A+=lda* quarter_thread_x ; } #pragma unroll for(magma_int_t k=0; k < 4 ; k++){ la[tx_][ty_+quarter_thread_x*k]= b[4+k] ; } __syncthreads(); if( ty_ < 4 ) { magma_int_t k = ty_*quarter_thread_x; res_ = la[tx_][0+k] + la[tx_][1+k] + la[tx_][2+k] + la[tx_][3+k] + la[tx_][4+k] + la[tx_][5+k] + la[tx_][6+k] + la[tx_][7+k] + la[tx_][8+k] + la[tx_][9+k] + la[tx_][10+k]+ la[tx_][11+k] + la[tx_][12+k]+ la[tx_][13+k] + la[tx_][14+k]+ la[tx_][15+k]; WC[k + wc_c*lda ] = res_; } wc_c++; __syncthreads(); } WC+=tx ; WC-=tx_; la[ty][tx]= res ; __syncthreads(); if( ty == 0 ) { res=la[0][tx]+ la[1][tx]+ la[2][tx]+ la[3][tx] ; WC[0+lda*(blkc)] = res; } } __global__ void magmablas_chemv_200_L_update(magma_int_t n, cuFloatComplex alpha, const cuFloatComplex* A, magma_int_t lda, const cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC ) { magma_int_t i; magma_int_t tx = threadIdx.x ; magma_int_t ind = blockIdx.x * thread_x + tx ; cuFloatComplex Ca; MAGMA_C_SET2REAL(Ca, 0) ; WC+= ind + lda * blockIdx.x; for(i = blockIdx.x*thread_x; i<n; i+=thread_x){ Ca += WC[0] ; WC += thread_x; } if( ind < n ) y[ind * incy] = beta * y[ind * incy] + alpha * Ca ; } extern "C" void magmablas_chemv_200_L(magma_int_t m, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy, cuFloatComplex *dC_work) { magma_int_t blocks; if (m % chemv_bs==0) blocks = m / chemv_bs; else blocks = m / chemv_bs + 1; dim3 grid(blocks, 1, 1); dim3 threads(thread_x, thread_y, 1); dim3 threads_u(chemv_bs, 1, 1); /* * If matrix size is multiple of chemv_bs, we use a specific code. * otherwise, we call the generic case. */ if(m % chemv_bs == 0 ) { magmablas_chemv_200_L_special <<< grid, threads, 0, magma_stream >>>( m, alpha, A, lda, X, incx, beta, Y, incy, dC_work); } else{ magma_int_t m_mod_thread_x = m%chemv_bs - 1; magmablas_chemv_200_L_generic <<< grid, threads, 0, magma_stream >>> ( m, alpha, A, lda, X, incx ,beta, Y, incy, dC_work, m_mod_thread_x); } magmablas_chemv_200_L_update<<< grid, threads_u, 0, magma_stream >>>( m, alpha, A, lda, X, incx, beta, Y, incy, dC_work); } #else /******************************************************************************* * Functions for each specific cases - Lower case nb = 32 */ __global__ void magmablas_chemv_200_L_special_32_s( magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb) { if(blockIdx.y > blockIdx.x) return; magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; cuFloatComplex res = MAGMA_C_ZERO;// used in scan the row cuFloatComplex res_ = MAGMA_C_ZERO;// used in scan the column __shared__ cuFloatComplex la [1056]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blockIdx.x; A += break_d ; A += lda * ty + tx; A += lda * (blockIdx.y ) * chemv_bs; // x += tx; if ( blockIdx.x == blockIdx.y ) // diagonal { x += (blockIdx.y * chemv_bs) * incx; if( ty == 0 ) { buff[tx] = x[0]; } // obtain the vector x store in buff; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[ bank_shift * (ty+j) + tx] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty * 4 + 4) ; i++) { if ( i < tx ) { la[bank_shift * tx + i] = cuConjf(la[ i * bank_shift + tx]) ; } } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; __syncthreads(); } else // non diagonal { x += (blockIdx.x * chemv_bs) * incx; if( ty == 0 ) { buff[tx] = x[0]; } // obtain the vector x and store in buff; buff store its corresponding upper elements instead of buff2; x -= (blockIdx.x * chemv_bs ) * incx; x += (blockIdx.y * chemv_bs ) * incx; if( ty == 0 ) { buff2[tx] = x[0]; } // obtain the vector x store in buff2; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) { la[ bank_shift * (ty+j) + tx] = A[ j * lda]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; // } __syncthreads(); la[bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res_ = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.y * chemv_bs + lda * blockIdx.x ] = res_; // write to its corresponding upper side position } __syncthreads(); } // end if else la[bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.x * chemv_bs + lda * blockIdx.y] = res; } } __global__ void magmablas_chemv_200_L_special_32( magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO;// used in scan the row cuFloatComplex res_ = MAGMA_C_ZERO;// used in scan the column cuFloatComplex res1 = MAGMA_C_ZERO;// tem for res cuFloatComplex res2 = MAGMA_C_ZERO;// tem for res_ __shared__ cuFloatComplex la [16][64+2]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blkc; x += (break_d + tx ) * incx; A += break_d ; A += ty * lda + tx ; if( ty == 0 ) { buff[tx] = x[0]; } // obtain the vector x store in buff; { A += lda * (blkc) * chemv_bs; // change #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[0][ bank_shift * (ty+j) + tx] = A[ j * lda]; __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty * 4 + 4) ; i++){ if ( i < tx ) { la[0][bank_shift * tx + i] = cuConjf( la[0][ i * bank_shift + tx] ) ; } } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf( la[0][bank_shift * tx + j + ty * 4] ) * buff[j + ty * 4]; __syncthreads(); A -= lda * (blkc) * chemv_bs; } x -= blkc * chemv_bs *incx ; x= x- tx*incx; magma_int_t wc_c = 0 ; magma_int_t count = 0 ; WC += break_d + tx; if( blkc > 0) for(magma_int_t s=0; s< (blkc * chemv_bs); s += chemv_bs ) { MAGMA_C_SET2REAL(res_,0); count++; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[0][ bank_shift * (ty+j) + tx] = A[ j * lda]; if( ty == 0 ) { buff2[tx] = x[tx]; } // obtain the vector x store in buff; __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[0][bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf( la[0][bank_shift * tx + j + ty * 4] ) * buff[j + ty * 4]; //iterate colum } __syncthreads(); la[0][bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res2 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[wc_c*lda ] = res2; } __syncthreads(); wc_c += 1; x += chemv_bs; A += lda * chemv_bs ; } la[0][bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res1 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[0+lda*(blkc)] = res1; } } /************************************************************** * Lower case for generic sizes */ __global__ void magmablas_chemv_200_L_generic_32_s( magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t m_mod_thread_x, magma_int_t nb) { if(blockIdx.y > blockIdx.x) return; magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; cuFloatComplex res = MAGMA_C_ZERO;// used in scan the row cuFloatComplex res_ = MAGMA_C_ZERO;// used in scan the column __shared__ cuFloatComplex la [1056]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blockIdx.x; A += break_d ; A += lda * ty; A += lda * (blockIdx.y ) * chemv_bs; // x += tx; x += (blockIdx.x * chemv_bs) * incx; magma_int_t trackA ; if( blockIdx.x == ( gridDim.x - 1 ) ) { if( ty == 0 ){ if( tx > m_mod_thread_x ) { MAGMA_C_SET2REAL(buff[tx],0); } else buff[tx] = x[0]; } if ( tx > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx; A += trackA ; } else { if( ty == 0 ){ buff[tx] = x[0]; } trackA = tx; A += trackA ; } __syncthreads(); if ( blockIdx.x == blockIdx.y) // diagonal { if( blockIdx.x == ( gridDim.x - 1 ) ) { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ if( ( ty + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[bank_shift*(ty+j)+tx], 9999); } else la[bank_shift*(ty+j)+tx] = A[ j * lda]; } } else { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ la[bank_shift*(ty+j)+tx] = A[ j * lda]; } } __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty * 4 + 4) ; i++) { if ( i < tx ) { la[bank_shift * tx + i] = cuConjf(la[ i * bank_shift + tx]) ; } } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; __syncthreads(); } else // non diagonal { // obtain the vector x and store in buff; buff store its corresponding upper elements instead of buff2; x -= (blockIdx.x * chemv_bs ) * incx; x += (blockIdx.y * chemv_bs ) * incx; if( ty == 0 ) { buff2[tx] = x[0]; } // obtain the vector x store in buff2; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) { la[ bank_shift * (ty+j) + tx] = A[ j * lda]; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf(la[bank_shift * tx + j + ty * 4]) * buff[j + ty * 4]; // } __syncthreads(); la[bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res_ = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.y * chemv_bs + lda * blockIdx.x ] = res_; // write to its corresponding upper side position } __syncthreads(); } // end if else la[bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res = la[tx*bank_shift+0]+la[tx*bank_shift+1] + la[tx*bank_shift+2]+la[tx*bank_shift+3] + la[tx*bank_shift+4]+la[tx*bank_shift+5] + la[tx*bank_shift+6]+la[tx*bank_shift+7]; WC[ tx + blockIdx.x * chemv_bs + lda * blockIdx.y] = res; } } __global__ void magmablas_chemv_200_L_generic_32(magma_int_t n, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t m_mod_thread_x, magma_int_t nb) { magma_int_t tx = threadIdx.x ; magma_int_t ty = threadIdx.y ; magma_int_t blkc = blockIdx.x ; cuFloatComplex res = MAGMA_C_ZERO; cuFloatComplex res_ = MAGMA_C_ZERO; cuFloatComplex res1 = MAGMA_C_ZERO; cuFloatComplex res2 = MAGMA_C_ZERO; __shared__ cuFloatComplex la [16][64+2]; __shared__ cuFloatComplex buff [chemv_bs]; __shared__ cuFloatComplex buff2 [chemv_bs]; magma_int_t break_d = chemv_bs * blkc; x += (break_d + tx ) * incx; A += break_d ; A += lda * ty; magma_int_t trackA ; if( blkc == ( gridDim.x - 1 ) ) { if( ty == 0 ){ if( tx > m_mod_thread_x ) { MAGMA_C_SET2REAL(buff[tx],0); } else buff[tx] = x[0]; } if ( tx > m_mod_thread_x ) trackA=m_mod_thread_x; else trackA=tx; A += trackA ; } else { if( ty == 0 ){ buff[tx] = x[0]; } trackA = tx; A += trackA ; } { A += lda * (blkc) * chemv_bs; // change // Somehow merging these two if - else creates problem // It could be a potential bug -- from synchronization or from cuda or compiler if( blkc == ( gridDim.x - 1 ) ) { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ if( ( ty + j ) > m_mod_thread_x ) { MAGMA_C_SET2REAL(la[0][bank_shift*(ty+j)+tx], 9999); } else la[0][bank_shift*(ty+j)+tx] = A[ j * lda]; } } else { #pragma unroll for(magma_int_t j =0; j<chemv_bs; j+=8){ la[0][bank_shift*(ty+j)+tx] = A[ j * lda]; } } __syncthreads(); #pragma unroll for(magma_int_t i=ty*4; i<(ty*4+4) ; i++){ if ( i < tx ) { la[0][bank_shift*tx+i] = cuConjf(la[0][i*bank_shift+tx]) ; } else la[0][bank_shift*tx+i] = la[0][bank_shift*tx+i] ; } __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) res += cuConjf(la[0][bank_shift*tx+j+ty*4])* buff[j+ty*4]; __syncthreads(); A -= lda * (blkc) * chemv_bs; } __syncthreads(); x= x - break_d *incx ; x= x - tx * incx ; magma_int_t wc_c = 0 ; magma_int_t count = 0 ; WC += break_d + tx; if( blkc > 0) for(magma_int_t s=0; s< (blkc * chemv_bs); s += chemv_bs ) { MAGMA_C_SET2REAL(res_,0); count++; #pragma unroll for(magma_int_t j =0; j<chemv_bs; j +=8) la[0][ bank_shift * (ty+j) + tx] = A[ j * lda]; __syncthreads(); if( ty == 0 ) { buff2[tx] = x[tx]; } // obtain the vector x store in buff2; __syncthreads(); #pragma unroll for(magma_int_t j=0; j < 4 ; j++) { res += (la[0][bank_shift * (ty + j * 8) + tx] )* buff2[ ty + j * 8]; res_ += cuConjf( la[0][bank_shift * tx + j + ty * 4] ) * buff[j + ty * 4]; //iterate colum } __syncthreads(); la[0][bank_shift*tx+ty]= res_ ; __syncthreads(); if( ty== 0 ) { res2 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[wc_c*lda ] = res2; } __syncthreads(); wc_c += 1; x += chemv_bs; A += lda * chemv_bs ; } la[0][bank_shift*tx+ty]= res ; __syncthreads(); if( ty== 0 ) { res1 = la[0][tx*bank_shift+0]+la[0][tx*bank_shift+1] + la[0][tx*bank_shift+2]+la[0][tx*bank_shift+3] + la[0][tx*bank_shift+4]+la[0][tx*bank_shift+5] + la[0][tx*bank_shift+6]+la[0][tx*bank_shift+7]; WC[0+lda*(blkc)] = res1; } } __global__ void magmablas_chemv_200_L_update_32_s(magma_int_t n, cuFloatComplex alpha, cuFloatComplex* A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb ) { magma_int_t i; magma_int_t tx = threadIdx.x ; magma_int_t ind = blockIdx.x * chemv_bs + tx ; cuFloatComplex Ca; MAGMA_C_SET2REAL(Ca, 0) ; WC+= ind; for(i =0; i<n; i+=chemv_bs){ Ca += WC[i/chemv_bs * lda] ; } if( ind < n ) y[ind * incy] = beta * y[ind * incy] + alpha * Ca ; } __global__ void magmablas_chemv_200_L_update_32(magma_int_t n, cuFloatComplex alpha, cuFloatComplex* A, magma_int_t lda, cuFloatComplex *x, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *y, magma_int_t incy, cuFloatComplex *WC, magma_int_t nb ) { magma_int_t i; magma_int_t tx = threadIdx.x ; magma_int_t ind = blockIdx.x * chemv_bs + tx ; cuFloatComplex Ca; MAGMA_C_SET2REAL(Ca, 0) ; WC+= ind + lda * blockIdx.x; for(i = blockIdx.x*chemv_bs; i<n; i+=chemv_bs){ Ca += WC[0] ; WC += chemv_bs; } if( ind < n ) y[ind * incy] = beta * y[ind * incy] + alpha * Ca ; } extern "C" void magmablas_chemv_200_L_32(magma_int_t m, cuFloatComplex alpha, cuFloatComplex *A, magma_int_t lda, cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy, cuFloatComplex *dC_work, magma_int_t nb) { magma_int_t blocks; if (m % chemv_bs==0) blocks = m / chemv_bs; else blocks = m / chemv_bs + 1; dim3 grid(blocks, 1, 1); dim3 grid_s(blocks, blocks, 1); dim3 threads(thread_x, thread_y, 1); dim3 threads_u(chemv_bs, 1, 1); /* * If matrix size is multiple of chemv_bs, we use a specific code. * otherwise, we call the generic case. */ if(m % chemv_bs == 0 ) { if(m < SWITCH) magmablas_chemv_200_L_special_32_s <<< grid_s, threads, 0, magma_stream >>>( m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); else magmablas_chemv_200_L_special_32 <<< grid, threads, 0, magma_stream >>>( m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); } else{ magma_int_t m_mod_thread_x = m%chemv_bs - 1; if(m < SWITCH) magmablas_chemv_200_L_generic_32_s <<< grid_s, threads, 0, magma_stream >>> ( m, alpha, A, lda, X, incx ,beta, Y, incy, dC_work, m_mod_thread_x, nb); else magmablas_chemv_200_L_generic_32 <<< grid, threads, 0, magma_stream >>> ( m, alpha, A, lda, X, incx ,beta, Y, incy, dC_work, m_mod_thread_x, nb); } if(m < SWITCH) magmablas_chemv_200_L_update_32_s<<< grid, threads_u, 0, magma_stream >>>( m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); else magmablas_chemv_200_L_update_32<<< grid, threads_u, 0, magma_stream >>>( m, alpha, A, lda, X, incx, beta, Y, incy, dC_work, nb); } #endif /************************************************************************* Purpose ======= magmablas_chemv performs the matrix-vector operation on fermi: y := alpha*A*x + beta*y, where alpha and beta are scalars, x and y are n element vectors and A is an n by n hermitian matrix. Arguments ========== UPLO - CHARACTER*1. On entry, UPLO specifies whether the upper or lower triangular part of the array A is to be referenced as follows: UPLO = 'U' or 'u' Only the upper triangular part of A is to be referenced. UPLO = 'L' or 'l' Only the lower triangular part of A is to be referenced. Unchanged on exit. N - INTEGER. On entry, N specifies the order of the matrix A. N must be at least zero. Unchanged on exit. ALPHA - COMPLEX*16 . On entry, ALPHA specifies the scalar alpha. Unchanged on exit. A - COMPLEX*16 array of DIMENSION ( LDA, n ). Before entry with UPLO = 'U' or 'u', the leading n by n upper triangular part of the array A must contain the upper triangular part of the hermitian matrix and the strictly lower triangular part of A is not referenced. Before entry with UPLO = 'L' or 'l', the leading n by n lower triangular part of the array A must contain the lower triangular part of the hermitian matrix and the strictly upper triangular part of A is not referenced. Note that the imaginary parts of the diagonal elements need not be set and are assumed to be zero. Unchanged on exit. LDA - INTEGER. On entry, LDA specifies the first dimension of A as declared in the calling (sub) program. LDA must be at least max( 1, n ). Unchanged on exit. It is recommended that lda is multiple of 16. Otherwise performance would be deteriorated as the memory accesses would not be fully coalescent. X - COMPLEX*16 array of dimension at least ( 1 + ( n - 1 )*abs( INCX ) ). Before entry, the incremented array X must contain the n element vector x. Unchanged on exit. INCX - INTEGER. On entry, INCX specifies the increment for the elements of X. INCX must not be zero. Unchanged on exit. BETA - COMPLEX*16 . On entry, BETA specifies the scalar beta. When BETA is supplied as zero then Y need not be set on input. Unchanged on exit. Y - COMPLEX*16 array of dimension at least ( 1 + ( n - 1 )*abs( INCY ) ). Before entry, the incremented array Y must contain the n element vector y. On exit, Y is overwritten by the updated vector y. INCY - INTEGER. On entry, INCY specifies the increment for the elements of Y. INCY must not be zero. Unchanged on exit. */ extern "C" magma_int_t magmablas_chemv_200( char uplo, magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy) { char uplo_[2] = {uplo, 0}; int upper = lapackf77_lsame(uplo_, "U"); /* * Test the input parameters. */ if ((! upper) && (! lapackf77_lsame(uplo_, "L"))) { return -1; } else if ( n < 0 ) { return -2; } else if ( lda < max(1,n) ) { return -5; } else if ( incx == 0 ) { return -7; } else if ( incy == 0 ) { return -10; } /* * Quick return if possible. */ if ( (n == 0) || ( MAGMA_C_EQUAL(alpha, MAGMA_C_ZERO) && MAGMA_C_EQUAL(beta, MAGMA_C_ONE) ) ) return MAGMA_SUCCESS; /* TODO: Upper case is not implemented in MAGMA */ if ( upper ) cublasChemv(uplo, n, alpha, A, lda, X, incx, beta, Y, incy); else { cuFloatComplex *dC_work; magma_int_t blocks = n / chemv_bs + (n % chemv_bs != 0); magma_int_t workspace = lda * (blocks + 1); /* TODO: need to add a MAGMA context to handle workspaces */ cublasAlloc( workspace, sizeof(cuFloatComplex), (void**)&dC_work ) ; cublasGetError( ) ; #ifdef NB_64 magmablas_chemv_200_L(n, alpha, A, lda, X, incx, beta, Y, incy, dC_work); #else magmablas_chemv_200_L_32(n, alpha, A, lda, X, incx, beta, Y, incy, dC_work, chemv_bs); #endif cublasFree(dC_work); cublasGetError( ) ; } return MAGMA_SUCCESS; } extern "C" magma_int_t magmablas_chemv2_200( char uplo, magma_int_t n, cuFloatComplex alpha, const cuFloatComplex *A, magma_int_t lda, const cuFloatComplex *X, magma_int_t incx, cuFloatComplex beta, cuFloatComplex *Y, magma_int_t incy, cuFloatComplex *work, int lwork) { char uplo_[2] = {uplo, 0}; int upper = lapackf77_lsame(uplo_, "U"); /* * Test the input parameters. */ if ((! upper) && (! lapackf77_lsame(uplo_, "L"))) { return -1; } else if ( n < 0 ) { return -2; } else if ( lda < max(1,n) ) { return -5; } else if ( incx == 0 ) { return -7; } else if ( incy == 0 ) { return -10; } /* * Quick return if possible. */ if ( (n == 0) || ( MAGMA_C_EQUAL(alpha, MAGMA_C_ZERO) && MAGMA_C_EQUAL(beta, MAGMA_C_ONE) ) ) return MAGMA_SUCCESS; /* TODO: Upper case is not implemented in MAGMA */ if ( upper ) cublasChemv(uplo, n, alpha, A, lda, X, incx, beta, Y, incy); else { magma_int_t blocks = n / chemv_bs + (n % chemv_bs != 0); magma_int_t workspace = lda * (blocks + 1); if (lwork < workspace){ printf("Not enough work space in magmablas_chemv: passed %d, required %d\n", lwork, workspace); exit(1); } //printf("You are using chemv_bs=%d\n", chemv_bs); #ifdef NB_64 if( n < 1622) cublasChemv(uplo, n, alpha, A, lda, X, incx, beta, Y, incy); else magmablas_chemv_200_L(n, alpha, A, lda, X, incx, beta, Y, incy, work); #else magmablas_chemv_200_L_32(n, alpha, A, lda, X, incx, beta, Y, incy, work, chemv_bs); #endif } return MAGMA_SUCCESS; } #endif /* (GPUSHMEM >= 200) */
84100902f500df721bbd17f981960ae1685e65a5.hip
// !!! This is a file automatically generated by hipify!!! /* This program is writen by qp09. * usually just for fun. * Mon December 14 2015 */ #include "../../third_party/cuda/helper_cuda.h" #include "GDecideNeurons.h" int cudaAllocDecide(void *pCpu, void *pGpu, int num) { GDecideNeurons *pGpuNeurons = (GDecideNeurons*)pGpu; GDecideNeurons *p = (GDecideNeurons*)pCpu; checkCudaErrors(hipMalloc((void**)&(pGpuNeurons->p_tmp_rate), sizeof(real)*num)); checkCudaErrors(hipMemcpy(pGpuNeurons->p_tmp_rate, p->p_tmp_rate, sizeof(real)*num, hipMemcpyHostToDevice)); checkCudaErrors(hipMalloc((void**)&(pGpuNeurons->p_fire_rate), sizeof(real)*num)); checkCudaErrors(hipMemcpy(pGpuNeurons->p_fire_rate, p->p_fire_rate, sizeof(real)*num, hipMemcpyHostToDevice)); checkCudaErrors(hipMalloc((void**)&(pGpuNeurons->p_fire_count), sizeof(int)*num)); checkCudaErrors(hipMemcpy(pGpuNeurons->p_fire_count, p->p_fire_count, sizeof(int)*num, hipMemcpyHostToDevice)); return 0; } int cudaFreeDecide(void *pGpu) { GDecideNeurons *pGpuNeurons = (GDecideNeurons*)pGpu; checkCudaErrors(hipFree(pGpuNeurons->p_tmp_rate)); checkCudaErrors(hipFree(pGpuNeurons->p_fire_rate)); checkCudaErrors(hipFree(pGpuNeurons->p_fire_count)); return 0; }
84100902f500df721bbd17f981960ae1685e65a5.cu
/* This program is writen by qp09. * usually just for fun. * Mon December 14 2015 */ #include "../../third_party/cuda/helper_cuda.h" #include "GDecideNeurons.h" int cudaAllocDecide(void *pCpu, void *pGpu, int num) { GDecideNeurons *pGpuNeurons = (GDecideNeurons*)pGpu; GDecideNeurons *p = (GDecideNeurons*)pCpu; checkCudaErrors(cudaMalloc((void**)&(pGpuNeurons->p_tmp_rate), sizeof(real)*num)); checkCudaErrors(cudaMemcpy(pGpuNeurons->p_tmp_rate, p->p_tmp_rate, sizeof(real)*num, cudaMemcpyHostToDevice)); checkCudaErrors(cudaMalloc((void**)&(pGpuNeurons->p_fire_rate), sizeof(real)*num)); checkCudaErrors(cudaMemcpy(pGpuNeurons->p_fire_rate, p->p_fire_rate, sizeof(real)*num, cudaMemcpyHostToDevice)); checkCudaErrors(cudaMalloc((void**)&(pGpuNeurons->p_fire_count), sizeof(int)*num)); checkCudaErrors(cudaMemcpy(pGpuNeurons->p_fire_count, p->p_fire_count, sizeof(int)*num, cudaMemcpyHostToDevice)); return 0; } int cudaFreeDecide(void *pGpu) { GDecideNeurons *pGpuNeurons = (GDecideNeurons*)pGpu; checkCudaErrors(cudaFree(pGpuNeurons->p_tmp_rate)); checkCudaErrors(cudaFree(pGpuNeurons->p_fire_rate)); checkCudaErrors(cudaFree(pGpuNeurons->p_fire_count)); return 0; }
336008976e8655ab091d2ff07785c6ccf3b01a96.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" __global__ void matmul(float* A, float* B, float* C, int ARows, int ACols, int BRows, int BCols, int CRows, int CCols) { float CValue = 0; int Row = blockIdx.y*16 + threadIdx.y; int Col = blockIdx.x*16 + threadIdx.x; for (int k = 0; k < (16 + ACols - 1)/16; k++) { for (int n = 0; n < 16; ++n) if ((k*16 + n < ACols && Row < ARows) && (k*16 + n < BRows && Col < BCols)) CValue += A[Row*ACols + k*16 + n] * B[(k*16 + n)*BCols + Col]; } if (Row < CRows && Col < CCols) C[((blockIdx.y * blockDim.y + threadIdx.y)*CCols)+(blockIdx.x*blockDim.x)+threadIdx.x]=CValue; }
336008976e8655ab091d2ff07785c6ccf3b01a96.cu
__global__ void matmul(float* A, float* B, float* C, int ARows, int ACols, int BRows, int BCols, int CRows, int CCols) { float CValue = 0; int Row = blockIdx.y*16 + threadIdx.y; int Col = blockIdx.x*16 + threadIdx.x; for (int k = 0; k < (16 + ACols - 1)/16; k++) { for (int n = 0; n < 16; ++n) if ((k*16 + n < ACols && Row < ARows) && (k*16 + n < BRows && Col < BCols)) CValue += A[Row*ACols + k*16 + n] * B[(k*16 + n)*BCols + Col]; } if (Row < CRows && Col < CCols) C[((blockIdx.y * blockDim.y + threadIdx.y)*CCols)+(blockIdx.x*blockDim.x)+threadIdx.x]=CValue; }
c7cd13d543085d2ec98d96239bf4159fbf5a248a.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" __global__ void orcu_kernel4037(const int nrows, const int ndiags, int sbdiag, int ndofs, int* offsets, double* A, double* x, double* y) { const int tid=blockIdx.x*blockDim.x+threadIdx.x; const int gsize=gridDim.x*blockDim.x; double ysum; int j, k, col, row; for (int i=tid; i<=nrows-1; i+=gsize) { { ysum=0.0; for (j=0; j<=ndiags-1; j++ ) { row=i+j*sbdiag; col=(floor((float)i/ndofs)+offsets[j])*ndofs; if (col>=0&&col<nrows) for (k=0; k<=ndofs-1; k++ ) ysum=ysum+A[row+k*nrows]*x[col+k]; } y[i]=ysum; } } } void MatMult_SeqDIA(double* A, double* x, double* y, int M, int N, int P, int NOS, int DOF) { register int i,j,k; int col,row; double ysum; /*@ begin PerfTuning ( def performance_params { param TC[] = range(32,1025,32); param BC[] = range(14,105,14); param PL[] = [16,32,48]; } def input_params { param M[] = [64]; param N[] = [64]; param P[] = [64]; param NOS = 7; param DOF[] = range(1,17); constraint c1 = (M==N); constraint c2 = (N==P); } def input_vars { decl dynamic double A[M*N*P*DOF*DOF*NOS] = random; decl dynamic double x[M*N*P*DOF] = random; decl dynamic double y[M*N*P*DOF] = 0; decl static int offsets[NOS] = {-M*N*DOF,-M*DOF,-DOF,0,DOF,M*DOF,M*N*DOF}; } ) @*/ /**-- (Generated by Orio) Best performance cost: [0.47420800000000002, 0.45235199999999998, 0.44425599999999998, 0.45331199999999999, 0.44502399999999998] Tuned for specific problem sizes: DOF = 2 M = 64 N = 64 NOS = 7 P = 64 Best performance parameters: BC = 42 PL = 16 TC = 640 --**/ int nrows=M*N*P*DOF; int ndiags=NOS; int ndofs=DOF; int sbdiag=M*N*P*DOF*DOF; /*@ begin Loop(transform CUDA(threadCount=TC, blockCount=BC, preferL1Size=PL) for(i=0; i<=nrows-1; i++){ ysum = 0.0; for(j=0; j<=ndiags-1; j++){ row = i+j*sbdiag; col = (floor((float)i/ndofs)+offsets[j])*ndofs; if(col>=0&&col<nrows) for(k=0; k<=ndofs-1; k++) ysum += A[row+k*nrows] * x[col+k]; } y[i] = ysum; } ) @*/ { hipDeviceSynchronize(); /*declare variables*/ double *dev_A, *dev_x, *dev_y; int *dev_offsets; int nthreads=640; /*calculate device dimensions*/ dim3 dimGrid, dimBlock; dimBlock.x=nthreads; dimGrid.x=42; /*allocate device memory*/ hipMalloc(&dev_A,M *N *P *DOF *DOF *NOS*sizeof(double)); hipMalloc(&dev_x,M *N *P *DOF*sizeof(double)); hipMalloc(&dev_y,M *N *P *DOF*sizeof(double)); hipMalloc(&dev_offsets,NOS*sizeof(int)); hipDeviceSetCacheConfig(hipFuncCachePreferShared); /*copy data from host to device*/ hipEventRecord(tstart,0); hipMemcpy(dev_A,A,M *N *P *DOF *DOF *NOS*sizeof(double),hipMemcpyHostToDevice); hipMemcpy(dev_x,x,M *N *P *DOF*sizeof(double),hipMemcpyHostToDevice); hipMemcpy(dev_offsets,offsets,NOS*sizeof(int),hipMemcpyHostToDevice); hipEventRecord(tstop,0); hipEventSynchronize(tstop); hipEventElapsedTime(&orcu_transfer,tstart,tstop); hipEventRecord(start,0); /*invoke device kernel*/ hipLaunchKernelGGL(( orcu_kernel4037), dim3(dimGrid),dim3(dimBlock), 0, 0, nrows,ndiags,sbdiag,ndofs,dev_offsets,dev_A,dev_x,dev_y); hipEventRecord(stop,0); hipEventSynchronize(stop); hipEventElapsedTime(&orcu_elapsed,start,stop); /*copy data from device to host*/ hipMemcpy(y,dev_y,M *N *P *DOF*sizeof(double),hipMemcpyDeviceToHost); hipDeviceSetCacheConfig(hipFuncCachePreferNone); /*free allocated memory*/ hipFree(dev_A); hipFree(dev_x); hipFree(dev_y); hipFree(dev_offsets); hipError_t err=hipGetLastError(); if (hipSuccess!=err) printf("CUDA runtime error: %s@",hipGetErrorString(err)); } /*@ end @*/ /*@ end @*/ }
c7cd13d543085d2ec98d96239bf4159fbf5a248a.cu
__global__ void orcu_kernel4037(const int nrows, const int ndiags, int sbdiag, int ndofs, int* offsets, double* A, double* x, double* y) { const int tid=blockIdx.x*blockDim.x+threadIdx.x; const int gsize=gridDim.x*blockDim.x; double ysum; int j, k, col, row; for (int i=tid; i<=nrows-1; i+=gsize) { { ysum=0.0; for (j=0; j<=ndiags-1; j++ ) { row=i+j*sbdiag; col=(floor((float)i/ndofs)+offsets[j])*ndofs; if (col>=0&&col<nrows) for (k=0; k<=ndofs-1; k++ ) ysum=ysum+A[row+k*nrows]*x[col+k]; } y[i]=ysum; } } } void MatMult_SeqDIA(double* A, double* x, double* y, int M, int N, int P, int NOS, int DOF) { register int i,j,k; int col,row; double ysum; /*@ begin PerfTuning ( def performance_params { param TC[] = range(32,1025,32); param BC[] = range(14,105,14); param PL[] = [16,32,48]; } def input_params { param M[] = [64]; param N[] = [64]; param P[] = [64]; param NOS = 7; param DOF[] = range(1,17); constraint c1 = (M==N); constraint c2 = (N==P); } def input_vars { decl dynamic double A[M*N*P*DOF*DOF*NOS] = random; decl dynamic double x[M*N*P*DOF] = random; decl dynamic double y[M*N*P*DOF] = 0; decl static int offsets[NOS] = {-M*N*DOF,-M*DOF,-DOF,0,DOF,M*DOF,M*N*DOF}; } ) @*/ /**-- (Generated by Orio) Best performance cost: [0.47420800000000002, 0.45235199999999998, 0.44425599999999998, 0.45331199999999999, 0.44502399999999998] Tuned for specific problem sizes: DOF = 2 M = 64 N = 64 NOS = 7 P = 64 Best performance parameters: BC = 42 PL = 16 TC = 640 --**/ int nrows=M*N*P*DOF; int ndiags=NOS; int ndofs=DOF; int sbdiag=M*N*P*DOF*DOF; /*@ begin Loop(transform CUDA(threadCount=TC, blockCount=BC, preferL1Size=PL) for(i=0; i<=nrows-1; i++){ ysum = 0.0; for(j=0; j<=ndiags-1; j++){ row = i+j*sbdiag; col = (floor((float)i/ndofs)+offsets[j])*ndofs; if(col>=0&&col<nrows) for(k=0; k<=ndofs-1; k++) ysum += A[row+k*nrows] * x[col+k]; } y[i] = ysum; } ) @*/ { cudaDeviceSynchronize(); /*declare variables*/ double *dev_A, *dev_x, *dev_y; int *dev_offsets; int nthreads=640; /*calculate device dimensions*/ dim3 dimGrid, dimBlock; dimBlock.x=nthreads; dimGrid.x=42; /*allocate device memory*/ cudaMalloc(&dev_A,M *N *P *DOF *DOF *NOS*sizeof(double)); cudaMalloc(&dev_x,M *N *P *DOF*sizeof(double)); cudaMalloc(&dev_y,M *N *P *DOF*sizeof(double)); cudaMalloc(&dev_offsets,NOS*sizeof(int)); cudaDeviceSetCacheConfig(cudaFuncCachePreferShared); /*copy data from host to device*/ cudaEventRecord(tstart,0); cudaMemcpy(dev_A,A,M *N *P *DOF *DOF *NOS*sizeof(double),cudaMemcpyHostToDevice); cudaMemcpy(dev_x,x,M *N *P *DOF*sizeof(double),cudaMemcpyHostToDevice); cudaMemcpy(dev_offsets,offsets,NOS*sizeof(int),cudaMemcpyHostToDevice); cudaEventRecord(tstop,0); cudaEventSynchronize(tstop); cudaEventElapsedTime(&orcu_transfer,tstart,tstop); cudaEventRecord(start,0); /*invoke device kernel*/ orcu_kernel4037<<<dimGrid,dimBlock>>>(nrows,ndiags,sbdiag,ndofs,dev_offsets,dev_A,dev_x,dev_y); cudaEventRecord(stop,0); cudaEventSynchronize(stop); cudaEventElapsedTime(&orcu_elapsed,start,stop); /*copy data from device to host*/ cudaMemcpy(y,dev_y,M *N *P *DOF*sizeof(double),cudaMemcpyDeviceToHost); cudaDeviceSetCacheConfig(cudaFuncCachePreferNone); /*free allocated memory*/ cudaFree(dev_A); cudaFree(dev_x); cudaFree(dev_y); cudaFree(dev_offsets); cudaError_t err=cudaGetLastError(); if (cudaSuccess!=err) printf("CUDA runtime error: %s@",cudaGetErrorString(err)); } /*@ end @*/ /*@ end @*/ }
e45a0d7c0561ec72901f5474b9f32edd625a148e.hip
// !!! This is a file automatically generated by hipify!!! #include <stdio.h> // code from mixbench #define CUDA_SAFE_CALL( call) { \ hipError_t err = call; \ if( hipSuccess != err) { \ fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \ __FILE__, __LINE__, hipGetErrorString( err) ); \ exit(EXIT_FAILURE); \ } } // SM version to 32 FP cores static inline int _ConvertSMVer2Cores(int major, int minor) { switch(major){ case 1: return 8; case 2: switch(minor){ case 1: return 48; default: return 32; } case 3: return 192; case 6: switch(minor){ case 0: return 64; default: return 128; } case 7: switch(minor){ case 0: return 64; default: return 128; } default: return 128; } } static inline void GetDevicePeakInfo(double *aGIPS, double *aGBPS, hipDeviceProp_t *aDeviceProp = NULL){ hipDeviceProp_t deviceProp; int current_device; if( aDeviceProp ) deviceProp = *aDeviceProp; else{ CUDA_SAFE_CALL( hipGetDevice(&current_device) ); CUDA_SAFE_CALL( hipGetDeviceProperties(&deviceProp, current_device) ); } const int TotalSPs = _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor)*deviceProp.multiProcessorCount; *aGIPS = 1000.0 * deviceProp.clockRate * TotalSPs / (1000.0 * 1000.0 * 1000.0); // Giga instructions/sec // *aGIPS64 = 1000.0 * deviceProp.clockRate * TotalSPs / (1000.0 * 1000.0 * 1000.0); // Giga instructions/sec *aGBPS = 2.0 * (double)deviceProp.memoryClockRate * 1000.0 * (double)deviceProp.memoryBusWidth / 8.0; } #if 0 static inline hipDeviceProp_t GetDeviceProperties(void){ hipDeviceProp_t deviceProp; int current_device; CUDA_SAFE_CALL( hipGetDevice(&current_device) ); CUDA_SAFE_CALL( hipGetDeviceProperties(&deviceProp, current_device) ); return deviceProp; } #endif // Print basic device information static void StoreDeviceInfo(FILE *fout){ hipDeviceProp_t deviceProp; int current_device, driver_version; CUDA_SAFE_CALL( hipGetDevice(&current_device) ); CUDA_SAFE_CALL( hipGetDeviceProperties(&deviceProp, current_device) ); CUDA_SAFE_CALL( hipDriverGetVersion(&driver_version) ); fprintf(fout, "------------------------ Device specifications ------------------------\n"); fprintf(fout, "Device: %s\n", deviceProp.name); fprintf(fout, "CUDA driver version: %d.%d\n", driver_version/1000, driver_version%1000); fprintf(fout, "GPU clock rate: %d MHz\n", deviceProp.clockRate/1000); fprintf(fout, "Memory clock rate: %d MHz\n", deviceProp.memoryClockRate/1000/2); // TODO: why divide by 2 here?? fprintf(fout, "Memory bus width: %d bits\n", deviceProp.memoryBusWidth); fprintf(fout, "WarpSize: %d\n", deviceProp.warpSize); fprintf(fout, "L2 cache size: %d KB\n", deviceProp.l2CacheSize/1024); fprintf(fout, "Total global mem: %d MB\n", (int)(deviceProp.totalGlobalMem/1024/1024)); fprintf(fout, "ECC enabled: %s\n", deviceProp.ECCEnabled?"Yes":"No"); fprintf(fout, "Compute Capability: %d.%d\n", deviceProp.major, deviceProp.minor); const int TotalSPs = _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor)*deviceProp.multiProcessorCount; fprintf(fout, "Total SPs: %d (%d MPs x %d SPs/MP)\n", TotalSPs, deviceProp.multiProcessorCount, _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor)); double InstrThroughput, MemBandwidth; GetDevicePeakInfo(&InstrThroughput, &MemBandwidth, &deviceProp); fprintf(fout, "Compute throughput: %.2f GFlops (theoretical single precision FMAs)\n", 2.0*InstrThroughput); fprintf(fout, "Memory bandwidth: %.2f GB/sec\n", MemBandwidth/(1000.0*1000.0*1000.0)); fprintf(fout, "-----------------------------------------------------------------------\n"); } int main() { int count; hipGetDeviceCount (&count); printf ("Total GPU device count =%d\n", count); #if 1 for (int i =0; i< 1; i++) { hipDeviceProp_t prop; hipGetDeviceProperties (&prop, i); printf ("Name: %s\n", prop.name); printf ("Global Mem (GB): %zu\n", prop.totalGlobalMem/1024/1024/1024); printf ("Shared Mem per Block: %zd\n", prop.sharedMemPerBlock); printf ("regs per block: %d\n", prop.regsPerBlock); printf ("warpSize: %d\n", prop.warpSize); //5 printf ("memPitch: %zd\n", prop.memPitch); printf ("maxThreadsPerBlock: %d\n", prop.maxThreadsPerBlock); printf ("maxThreadsDim[0]: %d\n", prop.maxThreadsDim[0]); printf ("maxThreadsDim[1]: %d\n", prop.maxThreadsDim[1]); printf ("maxThreadsDim[2]: %d\n", prop.maxThreadsDim[2]); printf ("maxGridSize[0]: %d\n", prop.maxGridSize[0]); printf ("maxGridSize[1]: %d\n", prop.maxGridSize[1]); printf ("maxGridSize[2]: %d\n", prop.maxGridSize[2]); printf ("clockRate: %d\n", prop.clockRate); //10 printf ("totalConstMem: %zd\n", prop.totalConstMem); printf ("major: %d\n", prop.major); printf ("minor: %d\n", prop.minor); printf ("textureAlignment: %zd\n", prop.textureAlignment); printf ("texturePitchAlignment: %zd\n", prop.texturePitchAlignment); //15 printf ("deviceOverlap: %d\n", prop.deviceOverlap); printf ("multiProcessorCount: %d\n", prop.multiProcessorCount); printf ("kernelExecTimeoutEnabled: %d\n", prop.kernelExecTimeoutEnabled); printf ("integrated: %d\n", prop.integrated); printf ("canMapHostMemory: %d\n", prop.canMapHostMemory); // 20 printf ("computeMode: %d\n", prop.computeMode); printf ("maxTexture1D: %d\n", prop.maxTexture1D); printf ("maxTexture1DMipmap: %d\n", prop.maxTexture1DMipmap); printf ("maxTexture1DLinear: %d\n", prop.maxTexture1DLinear); printf ("maxTexture2D[0]: %d\n", prop.maxTexture2D[0]); printf ("maxTexture2D[1]: %d\n", prop.maxTexture2D[1]); //25 printf ("maxTexture3D[0]: %d\n", prop.maxTexture3D[0]); printf ("maxTexture3D[1]: %d\n", prop.maxTexture3D[1]); printf ("maxTexture3D[2]: %d\n", prop.maxTexture3D[2]); printf ("maxTexture1DLayered[0]: %d\n", prop.maxTexture1DLayered[0]); printf ("maxTexture1DLayered[1]: %d\n", prop.maxTexture1DLayered[1]); printf ("maxTexture2DLayered[0]: %d\n", prop.maxTexture2DLayered[0]); printf ("maxTexture2DLayered[1]: %d\n", prop.maxTexture2DLayered[1]); printf ("maxTexture2DLayered[2]: %d\n", prop.maxTexture2DLayered[2]); printf ("maxSurfaceCubemap: %d\n", prop.maxSurfaceCubemap); //40 printf ("maxSurfaceCubemapLayered[0]: %d\n", prop.maxSurfaceCubemapLayered[0]); printf ("maxSurfaceCubemapLayered[1]: %d\n", prop.maxSurfaceCubemapLayered[1]); printf ("surfaceAlignment: %zd\n", prop.surfaceAlignment); printf ("concurrentKernels: %d\n", prop.concurrentKernels); printf ("ECCEnabled: %d\n", prop.ECCEnabled); printf ("pciBusID: %d\n", prop.pciBusID); //45 printf ("pciDeviceID: %d\n", prop.pciDeviceID); printf ("pciDomainID: %d\n", prop.pciDomainID); printf ("tccDriver: %d\n", prop.tccDriver); printf ("asyncEngineCount: %d\n", prop.asyncEngineCount); printf ("unifiedAddressing: %d\n", prop.unifiedAddressing); // 50 printf ("memoryClockRate: %d\n", prop.memoryClockRate); printf ("memoryBusWidth: %d\n", prop.memoryBusWidth); printf ("l2CacheSize: %d\n", prop.l2CacheSize); printf ("maxThreadsPerMultiProcessor: %d\n", prop.maxThreadsPerMultiProcessor); printf ("streamPrioritiesSupported: %d\n", prop.streamPrioritiesSupported); // 55 printf ("globalL1CacheSupported: %d\n", prop.globalL1CacheSupported); printf ("localL1CacheSupported: %d\n", prop.localL1CacheSupported); printf ("sharedMemPerMultiprocessor: %zd\n", prop.sharedMemPerMultiprocessor); printf ("regsPerMultiprocessor: %d\n", prop.regsPerMultiprocessor); // printf ("managedMemSupported: %d\n", prop.managedMemSupported); //60 printf ("isMultiGpuBoard: %d\n", prop.isMultiGpuBoard); printf ("multiGpuBoardGroupID: %d\n", prop.multiGpuBoardGroupID); printf ("singleToDoublePrecisionPerfRatio: %d\n", prop.singleToDoublePrecisionPerfRatio); printf ("pageableMemoryAccess: %d\n", prop.pageableMemoryAccess); printf ("concurrentManagedAccess: %d\n", prop.concurrentManagedAccess); //65 printf ("computePreemptionSupported: %d\n", prop.computePreemptionSupported); printf ("canUseHostPointerForRegisteredMem: %d\n", prop.canUseHostPointerForRegisteredMem); printf ("cooperativeLaunch: %d\n", prop.cooperativeLaunch); printf ("cooperativeMultiDeviceLaunch: %d\n", prop.cooperativeMultiDeviceLaunch); } #endif hipSetDevice(0); StoreDeviceInfo(stdout); return 0; }
e45a0d7c0561ec72901f5474b9f32edd625a148e.cu
#include <stdio.h> // code from mixbench #define CUDA_SAFE_CALL( call) { \ cudaError err = call; \ if( cudaSuccess != err) { \ fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \ __FILE__, __LINE__, cudaGetErrorString( err) ); \ exit(EXIT_FAILURE); \ } } // SM version to 32 FP cores static inline int _ConvertSMVer2Cores(int major, int minor) { switch(major){ case 1: return 8; case 2: switch(minor){ case 1: return 48; default: return 32; } case 3: return 192; case 6: switch(minor){ case 0: return 64; default: return 128; } case 7: switch(minor){ case 0: return 64; default: return 128; } default: return 128; } } static inline void GetDevicePeakInfo(double *aGIPS, double *aGBPS, cudaDeviceProp *aDeviceProp = NULL){ cudaDeviceProp deviceProp; int current_device; if( aDeviceProp ) deviceProp = *aDeviceProp; else{ CUDA_SAFE_CALL( cudaGetDevice(&current_device) ); CUDA_SAFE_CALL( cudaGetDeviceProperties(&deviceProp, current_device) ); } const int TotalSPs = _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor)*deviceProp.multiProcessorCount; *aGIPS = 1000.0 * deviceProp.clockRate * TotalSPs / (1000.0 * 1000.0 * 1000.0); // Giga instructions/sec // *aGIPS64 = 1000.0 * deviceProp.clockRate * TotalSPs / (1000.0 * 1000.0 * 1000.0); // Giga instructions/sec *aGBPS = 2.0 * (double)deviceProp.memoryClockRate * 1000.0 * (double)deviceProp.memoryBusWidth / 8.0; } #if 0 static inline cudaDeviceProp GetDeviceProperties(void){ cudaDeviceProp deviceProp; int current_device; CUDA_SAFE_CALL( cudaGetDevice(&current_device) ); CUDA_SAFE_CALL( cudaGetDeviceProperties(&deviceProp, current_device) ); return deviceProp; } #endif // Print basic device information static void StoreDeviceInfo(FILE *fout){ cudaDeviceProp deviceProp; int current_device, driver_version; CUDA_SAFE_CALL( cudaGetDevice(&current_device) ); CUDA_SAFE_CALL( cudaGetDeviceProperties(&deviceProp, current_device) ); CUDA_SAFE_CALL( cudaDriverGetVersion(&driver_version) ); fprintf(fout, "------------------------ Device specifications ------------------------\n"); fprintf(fout, "Device: %s\n", deviceProp.name); fprintf(fout, "CUDA driver version: %d.%d\n", driver_version/1000, driver_version%1000); fprintf(fout, "GPU clock rate: %d MHz\n", deviceProp.clockRate/1000); fprintf(fout, "Memory clock rate: %d MHz\n", deviceProp.memoryClockRate/1000/2); // TODO: why divide by 2 here?? fprintf(fout, "Memory bus width: %d bits\n", deviceProp.memoryBusWidth); fprintf(fout, "WarpSize: %d\n", deviceProp.warpSize); fprintf(fout, "L2 cache size: %d KB\n", deviceProp.l2CacheSize/1024); fprintf(fout, "Total global mem: %d MB\n", (int)(deviceProp.totalGlobalMem/1024/1024)); fprintf(fout, "ECC enabled: %s\n", deviceProp.ECCEnabled?"Yes":"No"); fprintf(fout, "Compute Capability: %d.%d\n", deviceProp.major, deviceProp.minor); const int TotalSPs = _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor)*deviceProp.multiProcessorCount; fprintf(fout, "Total SPs: %d (%d MPs x %d SPs/MP)\n", TotalSPs, deviceProp.multiProcessorCount, _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor)); double InstrThroughput, MemBandwidth; GetDevicePeakInfo(&InstrThroughput, &MemBandwidth, &deviceProp); fprintf(fout, "Compute throughput: %.2f GFlops (theoretical single precision FMAs)\n", 2.0*InstrThroughput); fprintf(fout, "Memory bandwidth: %.2f GB/sec\n", MemBandwidth/(1000.0*1000.0*1000.0)); fprintf(fout, "-----------------------------------------------------------------------\n"); } int main() { int count; cudaGetDeviceCount (&count); printf ("Total GPU device count =%d\n", count); #if 1 for (int i =0; i< 1; i++) { cudaDeviceProp prop; cudaGetDeviceProperties (&prop, i); printf ("Name: %s\n", prop.name); printf ("Global Mem (GB): %zu\n", prop.totalGlobalMem/1024/1024/1024); printf ("Shared Mem per Block: %zd\n", prop.sharedMemPerBlock); printf ("regs per block: %d\n", prop.regsPerBlock); printf ("warpSize: %d\n", prop.warpSize); //5 printf ("memPitch: %zd\n", prop.memPitch); printf ("maxThreadsPerBlock: %d\n", prop.maxThreadsPerBlock); printf ("maxThreadsDim[0]: %d\n", prop.maxThreadsDim[0]); printf ("maxThreadsDim[1]: %d\n", prop.maxThreadsDim[1]); printf ("maxThreadsDim[2]: %d\n", prop.maxThreadsDim[2]); printf ("maxGridSize[0]: %d\n", prop.maxGridSize[0]); printf ("maxGridSize[1]: %d\n", prop.maxGridSize[1]); printf ("maxGridSize[2]: %d\n", prop.maxGridSize[2]); printf ("clockRate: %d\n", prop.clockRate); //10 printf ("totalConstMem: %zd\n", prop.totalConstMem); printf ("major: %d\n", prop.major); printf ("minor: %d\n", prop.minor); printf ("textureAlignment: %zd\n", prop.textureAlignment); printf ("texturePitchAlignment: %zd\n", prop.texturePitchAlignment); //15 printf ("deviceOverlap: %d\n", prop.deviceOverlap); printf ("multiProcessorCount: %d\n", prop.multiProcessorCount); printf ("kernelExecTimeoutEnabled: %d\n", prop.kernelExecTimeoutEnabled); printf ("integrated: %d\n", prop.integrated); printf ("canMapHostMemory: %d\n", prop.canMapHostMemory); // 20 printf ("computeMode: %d\n", prop.computeMode); printf ("maxTexture1D: %d\n", prop.maxTexture1D); printf ("maxTexture1DMipmap: %d\n", prop.maxTexture1DMipmap); printf ("maxTexture1DLinear: %d\n", prop.maxTexture1DLinear); printf ("maxTexture2D[0]: %d\n", prop.maxTexture2D[0]); printf ("maxTexture2D[1]: %d\n", prop.maxTexture2D[1]); //25 printf ("maxTexture3D[0]: %d\n", prop.maxTexture3D[0]); printf ("maxTexture3D[1]: %d\n", prop.maxTexture3D[1]); printf ("maxTexture3D[2]: %d\n", prop.maxTexture3D[2]); printf ("maxTexture1DLayered[0]: %d\n", prop.maxTexture1DLayered[0]); printf ("maxTexture1DLayered[1]: %d\n", prop.maxTexture1DLayered[1]); printf ("maxTexture2DLayered[0]: %d\n", prop.maxTexture2DLayered[0]); printf ("maxTexture2DLayered[1]: %d\n", prop.maxTexture2DLayered[1]); printf ("maxTexture2DLayered[2]: %d\n", prop.maxTexture2DLayered[2]); printf ("maxSurfaceCubemap: %d\n", prop.maxSurfaceCubemap); //40 printf ("maxSurfaceCubemapLayered[0]: %d\n", prop.maxSurfaceCubemapLayered[0]); printf ("maxSurfaceCubemapLayered[1]: %d\n", prop.maxSurfaceCubemapLayered[1]); printf ("surfaceAlignment: %zd\n", prop.surfaceAlignment); printf ("concurrentKernels: %d\n", prop.concurrentKernels); printf ("ECCEnabled: %d\n", prop.ECCEnabled); printf ("pciBusID: %d\n", prop.pciBusID); //45 printf ("pciDeviceID: %d\n", prop.pciDeviceID); printf ("pciDomainID: %d\n", prop.pciDomainID); printf ("tccDriver: %d\n", prop.tccDriver); printf ("asyncEngineCount: %d\n", prop.asyncEngineCount); printf ("unifiedAddressing: %d\n", prop.unifiedAddressing); // 50 printf ("memoryClockRate: %d\n", prop.memoryClockRate); printf ("memoryBusWidth: %d\n", prop.memoryBusWidth); printf ("l2CacheSize: %d\n", prop.l2CacheSize); printf ("maxThreadsPerMultiProcessor: %d\n", prop.maxThreadsPerMultiProcessor); printf ("streamPrioritiesSupported: %d\n", prop.streamPrioritiesSupported); // 55 printf ("globalL1CacheSupported: %d\n", prop.globalL1CacheSupported); printf ("localL1CacheSupported: %d\n", prop.localL1CacheSupported); printf ("sharedMemPerMultiprocessor: %zd\n", prop.sharedMemPerMultiprocessor); printf ("regsPerMultiprocessor: %d\n", prop.regsPerMultiprocessor); // printf ("managedMemSupported: %d\n", prop.managedMemSupported); //60 printf ("isMultiGpuBoard: %d\n", prop.isMultiGpuBoard); printf ("multiGpuBoardGroupID: %d\n", prop.multiGpuBoardGroupID); printf ("singleToDoublePrecisionPerfRatio: %d\n", prop.singleToDoublePrecisionPerfRatio); printf ("pageableMemoryAccess: %d\n", prop.pageableMemoryAccess); printf ("concurrentManagedAccess: %d\n", prop.concurrentManagedAccess); //65 printf ("computePreemptionSupported: %d\n", prop.computePreemptionSupported); printf ("canUseHostPointerForRegisteredMem: %d\n", prop.canUseHostPointerForRegisteredMem); printf ("cooperativeLaunch: %d\n", prop.cooperativeLaunch); printf ("cooperativeMultiDeviceLaunch: %d\n", prop.cooperativeMultiDeviceLaunch); } #endif cudaSetDevice(0); StoreDeviceInfo(stdout); return 0; }
743119803d0b07080254216fd48eea5743d18d23.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <vector> #include "caffe/layer.hpp" #include "caffe/util/io.hpp" #include "caffe/util/math_functions.hpp" #include "caffe/layers/imp_map_layer.hpp" #include "thrust/device_vector.h" #include "thrust/copy.h" #include "thrust/sort.h" //#include "thrust/host_vector.h" namespace caffe { template <typename Dtype> __global__ void imp_copy_kernel(const int nthreads, const Dtype* const src_data, Dtype* const dst_data) { CUDA_KERNEL_LOOP(index, nthreads) { dst_data[index] = src_data[index]; } } template <typename Dtype> __global__ void imp_forward_kernel(const int nthreads, const Dtype* const bottom_data, Dtype* const top_data,const int* const hash,int * const imp,const int num,const int channel_out, const int base_space, const int mod,const int max_channel) { CUDA_KERNEL_LOOP(index, nthreads) { int n = index / base_space; int pxy = index % base_space; int out_space = channel_out*base_space; int ch = hash[int(bottom_data[index] * mod+0.00001)]; //if (bottom_data[index] * mod < 0.01) // ch = 0; //ch = ch < max_channel ? ch : max_channel; imp[index] = ch; for (int i = 0; i < ch; i++) { top_data[ n*out_space+ i*base_space+ pxy]=1.0; } } } template <typename Dtype> void ImpMapLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { int cnt = bottom[0]->count(); caffe_gpu_set<Dtype>(top[0]->count(), 0, top[0]->mutable_gpu_data()); const Dtype * bottom_data = bottom[0]->gpu_data(); /* if (sort_map_method_){ thrust::device_vector<Dtype> dt_a(cnt); Dtype * raw_ptr = thrust::raw_pointer_cast(dt_a.data()); imp_copy_kernel<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_data, raw_ptr); CUDA_POST_KERNEL_CHECK; thrust::sort(dt_a.begin(), dt_a.end()); const Dtype * per = per_.cpu_data(); // Dtype * bound = boundary_.mutable_cpu_data(); Dtype pt = 0; for (int i = 0; i < ngroup_; i++) { pt = pt + per[i]; int idx = int(pt*cnt)-1; if (idx < 0) idx = 0; bound[i] = dt_a[idx]; } LOG(INFO) << "3L"; create_hash_table(); }*/ int mod = ngroup_; //const int nthreads, const Dtype* const bottom_data, // Dtype* const top_data, const Dtype* const hash, const int num, const int channel_out, // const int base_space, const int mod imp_forward_kernel<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_data,top[0]->mutable_gpu_data(),hash_.gpu_data(),imp_map_.mutable_gpu_data(),num_,channel_out_,width_*height_, mod,max_channel_); CUDA_POST_KERNEL_CHECK; Dtype sum_ratio; caffe_gpu_asum(top[0]->count(), top[0]->gpu_data(), &sum_ratio); this->blobs_[0]->mutable_cpu_data()[0] = sum_ratio / (num_*width_*height_*channel_out_); } template <typename Dtype> __global__ void imp_backward_kernel_global(const int nthreads, Dtype* const bottom_diff, const Dtype* const top_diff, const int channel_out,const int base_space) { CUDA_KERNEL_LOOP(index, nthreads) { int n = index / base_space; int pxy = index % base_space; int out_space = channel_out*base_space; bottom_diff[index] = 0; for (int i = 0; i < channel_out; i++) { bottom_diff[index]+=top_diff[n*out_space + i*base_space + pxy]; } //bottom_diff[index] = bottom_diff[index] * groups; } } template <typename Dtype> __global__ void imp_backward_kernel_local(const int nthreads, Dtype* const bottom_diff, const Dtype* const top_diff, const int * const imp, const int lbits, const int groups, const int channel_out, const int base_space) { CUDA_KERNEL_LOOP(index, nthreads) { int n = index / base_space; int pxy = index % base_space; int out_space = channel_out*base_space; int start_idx = imp[index] - lbits >= 0 ? imp[index] - lbits : 0; int end_idx = imp[index] + lbits <= channel_out ? imp[index] + lbits : channel_out; bottom_diff[index] = 0; for (int i = start_idx; i < end_idx; i++) { bottom_diff[index] += top_diff[n*out_space + i*base_space + pxy]; } bottom_diff[index] = bottom_diff[index] * groups; } } template <typename Dtype> void ImpMapLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); const Dtype * bottom_data = bottom[0]->gpu_data(); const int cnt = bottom[0]->count(); caffe_gpu_set(bottom[0]->count(), Dtype(0), bottom_diff); if (global_gradient_) { imp_backward_kernel_global<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_diff, top[0]->gpu_diff(), channel_out_, width_*height_); } else{ imp_backward_kernel_local<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_diff, top[0]->gpu_diff(), imp_map_.gpu_data(), channels_per_group_, ngroup_, channel_out_, width_*height_); } //imp_backward_kernel<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > // (cnt, bottom_diff, top[0]->gpu_diff(), imp_map_.gpu_data(), channels_per_group_, ngroup_, channel_out_, width_*height_); //LOG(INFO) << "1 L: " << channels_per_group_<<" "<<ngroup_; CUDA_POST_KERNEL_CHECK; /* if (sort_map_method_){ Dtype scale; caffe_gpu_asum(bottom[0]->count(), bottom[0]->gpu_diff(), &scale); scale = scale / bottom[0]->count(); caffe_gpu_add_scalar(bottom[0]->count(), -scale, bottom[0]->mutable_gpu_diff()); } else{}*/ Dtype sum_ratio; //LOG(INFO) << weight_; caffe_gpu_asum(top[0]->count(), top[0]->gpu_data(), &sum_ratio); //LOG(INFO) << sum_ratio << " " << ratio_*num_*width_*height_*channel_out_; if (sum_ratio>ratio_*num_*width_*height_*channel_out_) { //LOG(INFO) << "CONS"; caffe_gpu_add_scalar(bottom[0]->count(), weight_, bottom[0]->mutable_gpu_diff()); } } INSTANTIATE_LAYER_GPU_FUNCS(ImpMapLayer); } // namespace caffe
743119803d0b07080254216fd48eea5743d18d23.cu
#include <vector> #include "caffe/layer.hpp" #include "caffe/util/io.hpp" #include "caffe/util/math_functions.hpp" #include "caffe/layers/imp_map_layer.hpp" #include "thrust/device_vector.h" #include "thrust/copy.h" #include "thrust/sort.h" //#include "thrust/host_vector.h" namespace caffe { template <typename Dtype> __global__ void imp_copy_kernel(const int nthreads, const Dtype* const src_data, Dtype* const dst_data) { CUDA_KERNEL_LOOP(index, nthreads) { dst_data[index] = src_data[index]; } } template <typename Dtype> __global__ void imp_forward_kernel(const int nthreads, const Dtype* const bottom_data, Dtype* const top_data,const int* const hash,int * const imp,const int num,const int channel_out, const int base_space, const int mod,const int max_channel) { CUDA_KERNEL_LOOP(index, nthreads) { int n = index / base_space; int pxy = index % base_space; int out_space = channel_out*base_space; int ch = hash[int(bottom_data[index] * mod+0.00001)]; //if (bottom_data[index] * mod < 0.01) // ch = 0; //ch = ch < max_channel ? ch : max_channel; imp[index] = ch; for (int i = 0; i < ch; i++) { top_data[ n*out_space+ i*base_space+ pxy]=1.0; } } } template <typename Dtype> void ImpMapLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { int cnt = bottom[0]->count(); caffe_gpu_set<Dtype>(top[0]->count(), 0, top[0]->mutable_gpu_data()); const Dtype * bottom_data = bottom[0]->gpu_data(); /* if (sort_map_method_){ thrust::device_vector<Dtype> dt_a(cnt); Dtype * raw_ptr = thrust::raw_pointer_cast(dt_a.data()); imp_copy_kernel<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_data, raw_ptr); CUDA_POST_KERNEL_CHECK; thrust::sort(dt_a.begin(), dt_a.end()); const Dtype * per = per_.cpu_data(); // Dtype * bound = boundary_.mutable_cpu_data(); Dtype pt = 0; for (int i = 0; i < ngroup_; i++) { pt = pt + per[i]; int idx = int(pt*cnt)-1; if (idx < 0) idx = 0; bound[i] = dt_a[idx]; } LOG(INFO) << "3L"; create_hash_table(); }*/ int mod = ngroup_; //const int nthreads, const Dtype* const bottom_data, // Dtype* const top_data, const Dtype* const hash, const int num, const int channel_out, // const int base_space, const int mod imp_forward_kernel<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_data,top[0]->mutable_gpu_data(),hash_.gpu_data(),imp_map_.mutable_gpu_data(),num_,channel_out_,width_*height_, mod,max_channel_); CUDA_POST_KERNEL_CHECK; Dtype sum_ratio; caffe_gpu_asum(top[0]->count(), top[0]->gpu_data(), &sum_ratio); this->blobs_[0]->mutable_cpu_data()[0] = sum_ratio / (num_*width_*height_*channel_out_); } template <typename Dtype> __global__ void imp_backward_kernel_global(const int nthreads, Dtype* const bottom_diff, const Dtype* const top_diff, const int channel_out,const int base_space) { CUDA_KERNEL_LOOP(index, nthreads) { int n = index / base_space; int pxy = index % base_space; int out_space = channel_out*base_space; bottom_diff[index] = 0; for (int i = 0; i < channel_out; i++) { bottom_diff[index]+=top_diff[n*out_space + i*base_space + pxy]; } //bottom_diff[index] = bottom_diff[index] * groups; } } template <typename Dtype> __global__ void imp_backward_kernel_local(const int nthreads, Dtype* const bottom_diff, const Dtype* const top_diff, const int * const imp, const int lbits, const int groups, const int channel_out, const int base_space) { CUDA_KERNEL_LOOP(index, nthreads) { int n = index / base_space; int pxy = index % base_space; int out_space = channel_out*base_space; int start_idx = imp[index] - lbits >= 0 ? imp[index] - lbits : 0; int end_idx = imp[index] + lbits <= channel_out ? imp[index] + lbits : channel_out; bottom_diff[index] = 0; for (int i = start_idx; i < end_idx; i++) { bottom_diff[index] += top_diff[n*out_space + i*base_space + pxy]; } bottom_diff[index] = bottom_diff[index] * groups; } } template <typename Dtype> void ImpMapLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); const Dtype * bottom_data = bottom[0]->gpu_data(); const int cnt = bottom[0]->count(); caffe_gpu_set(bottom[0]->count(), Dtype(0), bottom_diff); if (global_gradient_) { imp_backward_kernel_global<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_diff, top[0]->gpu_diff(), channel_out_, width_*height_); } else{ imp_backward_kernel_local<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > (cnt, bottom_diff, top[0]->gpu_diff(), imp_map_.gpu_data(), channels_per_group_, ngroup_, channel_out_, width_*height_); } //imp_backward_kernel<Dtype> << <CAFFE_GET_BLOCKS(cnt), CAFFE_CUDA_NUM_THREADS >> > // (cnt, bottom_diff, top[0]->gpu_diff(), imp_map_.gpu_data(), channels_per_group_, ngroup_, channel_out_, width_*height_); //LOG(INFO) << "1 L: " << channels_per_group_<<" "<<ngroup_; CUDA_POST_KERNEL_CHECK; /* if (sort_map_method_){ Dtype scale; caffe_gpu_asum(bottom[0]->count(), bottom[0]->gpu_diff(), &scale); scale = scale / bottom[0]->count(); caffe_gpu_add_scalar(bottom[0]->count(), -scale, bottom[0]->mutable_gpu_diff()); } else{}*/ Dtype sum_ratio; //LOG(INFO) << weight_; caffe_gpu_asum(top[0]->count(), top[0]->gpu_data(), &sum_ratio); //LOG(INFO) << sum_ratio << " " << ratio_*num_*width_*height_*channel_out_; if (sum_ratio>ratio_*num_*width_*height_*channel_out_) { //LOG(INFO) << "CONS"; caffe_gpu_add_scalar(bottom[0]->count(), weight_, bottom[0]->mutable_gpu_diff()); } } INSTANTIATE_LAYER_GPU_FUNCS(ImpMapLayer); } // namespace caffe
5beb8786421ce18a6d671c38a2d31725b0be98b3.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and its licensors retain all intellectual property and * proprietary rights in and to this software and related documentation. * Any use, reproduction, disclosure, or distribution of this software * and related documentation without an express license agreement from * NVIDIA Corporation is strictly prohibited. * * Please refer to the applicable NVIDIA end user license agreement (EULA) * associated with this source code for terms and conditions that govern * your use of this NVIDIA software. * */ #include "../common/book.h" #include "../common/cpu_bitmap.h" #define DIM 1000 struct hipComplex { float r; float i; hipComplex( float a, float b ) : r(a), i(b) {} __device__ float magnitude2( void ) { return r * r + i * i; } __device__ hipComplex operator*(const hipComplex& a) { return hipComplex(r*a.r - i*a.i, i*a.r + r*a.i); } __device__ hipComplex operator+(const hipComplex& a) { return hipComplex(r+a.r, i+a.i); } }; __device__ int julia( int x, int y ) { const float scale = 1.5; float jx = scale * (float)(DIM/2 - x)/(DIM/2); float jy = scale * (float)(DIM/2 - y)/(DIM/2); hipComplex c(-0.8, 0.156); hipComplex a(jx, jy); int i = 0; for (i=0; i<200; i++) { a = a * a + c; if (a.magnitude2() > 1000) return 0; } return 1; } __global__ void kernel( unsigned char *ptr ) { // map from blockIdx to pixel position int x = blockIdx.x; int y = blockIdx.y; int offset = x + y * gridDim.x; // now calculate the value at that position int juliaValue = julia( x, y ); ptr[offset*4 + 0] = 255 * juliaValue; ptr[offset*4 + 1] = 0; ptr[offset*4 + 2] = 0; ptr[offset*4 + 3] = 255; } // globals needed by the update routine struct DataBlock { unsigned char *dev_bitmap; }; int main( void ) { DataBlock data; CPUBitmap bitmap( DIM, DIM, &data ); unsigned char *dev_bitmap; HANDLE_ERROR( hipMalloc( (void**)&dev_bitmap, bitmap.image_size() ) ); data.dev_bitmap = dev_bitmap; dim3 grid(DIM,DIM); hipLaunchKernelGGL(( kernel), dim3(grid),dim3(1), 0, 0, dev_bitmap ); HANDLE_ERROR( hipMemcpy( bitmap.get_ptr(), dev_bitmap, bitmap.image_size(), hipMemcpyDeviceToHost ) ); HANDLE_ERROR( hipFree( dev_bitmap ) ); bitmap.display_and_exit(); }
5beb8786421ce18a6d671c38a2d31725b0be98b3.cu
/* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and its licensors retain all intellectual property and * proprietary rights in and to this software and related documentation. * Any use, reproduction, disclosure, or distribution of this software * and related documentation without an express license agreement from * NVIDIA Corporation is strictly prohibited. * * Please refer to the applicable NVIDIA end user license agreement (EULA) * associated with this source code for terms and conditions that govern * your use of this NVIDIA software. * */ #include "../common/book.h" #include "../common/cpu_bitmap.h" #define DIM 1000 struct cuComplex { float r; float i; cuComplex( float a, float b ) : r(a), i(b) {} __device__ float magnitude2( void ) { return r * r + i * i; } __device__ cuComplex operator*(const cuComplex& a) { return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i); } __device__ cuComplex operator+(const cuComplex& a) { return cuComplex(r+a.r, i+a.i); } }; __device__ int julia( int x, int y ) { const float scale = 1.5; float jx = scale * (float)(DIM/2 - x)/(DIM/2); float jy = scale * (float)(DIM/2 - y)/(DIM/2); cuComplex c(-0.8, 0.156); cuComplex a(jx, jy); int i = 0; for (i=0; i<200; i++) { a = a * a + c; if (a.magnitude2() > 1000) return 0; } return 1; } __global__ void kernel( unsigned char *ptr ) { // map from blockIdx to pixel position int x = blockIdx.x; int y = blockIdx.y; int offset = x + y * gridDim.x; // now calculate the value at that position int juliaValue = julia( x, y ); ptr[offset*4 + 0] = 255 * juliaValue; ptr[offset*4 + 1] = 0; ptr[offset*4 + 2] = 0; ptr[offset*4 + 3] = 255; } // globals needed by the update routine struct DataBlock { unsigned char *dev_bitmap; }; int main( void ) { DataBlock data; CPUBitmap bitmap( DIM, DIM, &data ); unsigned char *dev_bitmap; HANDLE_ERROR( cudaMalloc( (void**)&dev_bitmap, bitmap.image_size() ) ); data.dev_bitmap = dev_bitmap; dim3 grid(DIM,DIM); kernel<<<grid,1>>>( dev_bitmap ); HANDLE_ERROR( cudaMemcpy( bitmap.get_ptr(), dev_bitmap, bitmap.image_size(), cudaMemcpyDeviceToHost ) ); HANDLE_ERROR( cudaFree( dev_bitmap ) ); bitmap.display_and_exit(); }
1089736bd1b93664389e88bb2a95a7df75e07c63.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "kernels_hip.cuh" #include <float.h> #include "device_launch_parameters.h" #include "texturegpu.cuh" #include "hitable_list.cuh" #include "camera.cuh" #include "math.cuh" #include "ray.cuh" #include "vec3.cuh" #include "cuda_utils.cuh" #include "managed_ptr.cuh" #include "world.cuh" __device__ Vec3 color_11(const Ray& r, Hitable** hitable_objects, hiprandState_t* local_rand_state) { Ray cur_ray = r; Vec3 cur_attenuation = Vec3(1.0f, 1.0f, 1.0f); for (int i = 0; i < 50; ++i) { HitRecord rec; if ((*hitable_objects)->hit(cur_ray, 0.001f, 10.0f, rec)) { Ray scattered; Vec3 attenuation; if (rec.mat_ptr->scatter(cur_ray, rec, attenuation, scattered, local_rand_state)) { cur_attenuation *= attenuation; cur_ray = scattered; } else { return Vec3(0.0f, 0.0f, 0.0f); } } else { Vec3 unit_direction = unit_vector(cur_ray.direction()); float t = 0.5f * (unit_direction.y() + 1.0f); Vec3 c = (1.0f - t) * Vec3(1.0f, 1.0f, 1.0f) + t * Vec3(0.5f, 0.7f, 1.0f); return cur_attenuation * c; } } return Vec3(0.0f, 0.0f, 0.0f); } __global__ void chapter_11_kernel(TextureGPU* tex, Camera camera, Hitable** hitable_objects, hiprandState_t* rand_state) { int x = threadIdx.x + blockIdx.x * blockDim.x; int y = threadIdx.y + blockIdx.y * blockDim.y; size_t w = tex->get_width(); size_t h = tex->get_height(); if ((x >= w || (y >= h))) return; int pixel_index = y * w + x; hiprandState_t* local_rand_state = &rand_state[pixel_index]; Vec3 col(0.0f, 0.0f, 0.0f); for (int s = 0; s < camera.get_ns(); ++s) { float u = float(x + hiprand_uniform(local_rand_state)) / float(w); float v = float(h - y + hiprand_uniform(local_rand_state)) / float(h); Ray ray = camera.get_ray(u, v, local_rand_state); col += color_11(ray, hitable_objects, local_rand_state); } rand_state[pixel_index] = *local_rand_state; col /= float(camera.get_ns()); Uint8 r = sqrt(col.r()) * 255.99f; Uint8 g = sqrt(col.g()) * 255.99f; Uint8 b = sqrt(col.b()) * 255.99f; tex->set_rgb(x, y, r, g, b); }
1089736bd1b93664389e88bb2a95a7df75e07c63.cu
#include "kernels.cuh" #include <float.h> #include "device_launch_parameters.h" #include "texturegpu.cuh" #include "hitable_list.cuh" #include "camera.cuh" #include "math.cuh" #include "ray.cuh" #include "vec3.cuh" #include "cuda_utils.cuh" #include "managed_ptr.cuh" #include "world.cuh" __device__ Vec3 color_11(const Ray& r, Hitable** hitable_objects, curandState* local_rand_state) { Ray cur_ray = r; Vec3 cur_attenuation = Vec3(1.0f, 1.0f, 1.0f); for (int i = 0; i < 50; ++i) { HitRecord rec; if ((*hitable_objects)->hit(cur_ray, 0.001f, 10.0f, rec)) { Ray scattered; Vec3 attenuation; if (rec.mat_ptr->scatter(cur_ray, rec, attenuation, scattered, local_rand_state)) { cur_attenuation *= attenuation; cur_ray = scattered; } else { return Vec3(0.0f, 0.0f, 0.0f); } } else { Vec3 unit_direction = unit_vector(cur_ray.direction()); float t = 0.5f * (unit_direction.y() + 1.0f); Vec3 c = (1.0f - t) * Vec3(1.0f, 1.0f, 1.0f) + t * Vec3(0.5f, 0.7f, 1.0f); return cur_attenuation * c; } } return Vec3(0.0f, 0.0f, 0.0f); } __global__ void chapter_11_kernel(TextureGPU* tex, Camera camera, Hitable** hitable_objects, curandState* rand_state) { int x = threadIdx.x + blockIdx.x * blockDim.x; int y = threadIdx.y + blockIdx.y * blockDim.y; size_t w = tex->get_width(); size_t h = tex->get_height(); if ((x >= w || (y >= h))) return; int pixel_index = y * w + x; curandState* local_rand_state = &rand_state[pixel_index]; Vec3 col(0.0f, 0.0f, 0.0f); for (int s = 0; s < camera.get_ns(); ++s) { float u = float(x + curand_uniform(local_rand_state)) / float(w); float v = float(h - y + curand_uniform(local_rand_state)) / float(h); Ray ray = camera.get_ray(u, v, local_rand_state); col += color_11(ray, hitable_objects, local_rand_state); } rand_state[pixel_index] = *local_rand_state; col /= float(camera.get_ns()); Uint8 r = sqrt(col.r()) * 255.99f; Uint8 g = sqrt(col.g()) * 255.99f; Uint8 b = sqrt(col.b()) * 255.99f; tex->set_rgb(x, y, r, g, b); }
07f1a7915572c1d9f5de5b8e3aa82d85fb8227c1.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // // Created by root on 23/03/2020. // #include "../Matrix.cuh" #include <iostream> __global__ void matrixMultiply(double *a, double *b, double *c, int cr, int cc, int ac, int bc){ long x = blockIdx.x * blockDim.x + threadIdx.x; // col long y = blockIdx.y * blockDim.y + threadIdx.y; // row double sum = 0; if(x < cc && y < cr){ for(int k = 0; k<ac; k++){ sum+= a[y*ac+k] * b[k*bc+x]; } c[y * cc + x] = sum; } } Matrix Matrix::multiply(Matrix m){ if(this->Columns != m.Rows){ std::cout << "Cannot multiply matrix. Invalid size"; exit(-1); } static double* c; c = (double*) calloc(this->Rows*m.Columns,sizeof(double)); //Define os endereoes da memria de vdeo double *d_a, *d_b, *d_c; //Define o tamanho de cada matriz na memria int aSize = this->Rows*this->Columns*sizeof(double); int bSize = m.Rows*m.Columns*sizeof(double); int cSize = this->Rows*m.Columns*sizeof(double); //Aloca espao na memria de vdeo hipMalloc((void**)&d_a, aSize); hipMalloc((void**)&d_b, bSize); hipMalloc((void**)&d_c, cSize); //Move as 2 matrizes para a memria de vdeo alocada hipMemcpy(d_a, this->Value, aSize, hipMemcpyHostToDevice); hipMemcpy(d_b, m.Value, bSize, hipMemcpyHostToDevice); //Define as dimenses dim3 dimBlock(32,32); // 32x32 -> 1024 Threads dim3 dimGrid(this->Rows,m.Columns); //Efetua a multiplicao hipLaunchKernelGGL(( matrixMultiply), dim3(dimGrid), dim3(dimBlock), 0, 0, d_a, d_b, d_c, this->Rows, m.Columns, this->Columns, m.Columns); //Copia o resultado de volta hipMemcpy(c, d_c, cSize, hipMemcpyDeviceToHost); //Limpa a memria de vdeo hipFree(d_a); hipFree(d_b); hipFree(d_c); //Salva return {m.Columns, this->Rows, c}; }
07f1a7915572c1d9f5de5b8e3aa82d85fb8227c1.cu
// // Created by root on 23/03/2020. // #include "../Matrix.cuh" #include <iostream> __global__ void matrixMultiply(double *a, double *b, double *c, int cr, int cc, int ac, int bc){ long x = blockIdx.x * blockDim.x + threadIdx.x; // col long y = blockIdx.y * blockDim.y + threadIdx.y; // row double sum = 0; if(x < cc && y < cr){ for(int k = 0; k<ac; k++){ sum+= a[y*ac+k] * b[k*bc+x]; } c[y * cc + x] = sum; } } Matrix Matrix::multiply(Matrix m){ if(this->Columns != m.Rows){ std::cout << "Cannot multiply matrix. Invalid size"; exit(-1); } static double* c; c = (double*) calloc(this->Rows*m.Columns,sizeof(double)); //Define os endereçoes da memória de vídeo double *d_a, *d_b, *d_c; //Define o tamanho de cada matriz na memória int aSize = this->Rows*this->Columns*sizeof(double); int bSize = m.Rows*m.Columns*sizeof(double); int cSize = this->Rows*m.Columns*sizeof(double); //Aloca espaço na memória de vídeo cudaMalloc((void**)&d_a, aSize); cudaMalloc((void**)&d_b, bSize); cudaMalloc((void**)&d_c, cSize); //Move as 2 matrizes para a memória de vídeo alocada cudaMemcpy(d_a, this->Value, aSize, cudaMemcpyHostToDevice); cudaMemcpy(d_b, m.Value, bSize, cudaMemcpyHostToDevice); //Define as dimensões dim3 dimBlock(32,32); // 32x32 -> 1024 Threads dim3 dimGrid(this->Rows,m.Columns); //Efetua a multiplicação matrixMultiply<<<dimGrid, dimBlock>>>(d_a, d_b, d_c, this->Rows, m.Columns, this->Columns, m.Columns); //Copia o resultado de volta cudaMemcpy(c, d_c, cSize, cudaMemcpyDeviceToHost); //Limpa a memória de vídeo cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); //Salva return {m.Columns, this->Rows, c}; }
550746605648b0f906f666116a18e3ecb7d92169.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <iostream> #include <math.h> // Kernel function to add the elements of two arrays __global__ void add(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] + y[i]; } __global__ void subtract(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] - y[i]; } __global__ void multiply(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] * y[i]; } __global__ void divide(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] / y[i]; } void readData( char * filePath, int maxNumbers, float* buffer ){ FILE * pFile; int i = 0; pFile = fopen(filePath, "r"); while ( i < maxNumbers ) { int number; if (fscanf(pFile, "%d", &number) != 1) break; // file finished or there was an error buffer[i] = (float)number; i++; } fclose(pFile); } void writeData( char *filePath, int maxNumbers, float* buffer ){ FILE * pFile; int i = 0; pFile = fopen(filePath, "w"); while ( i < maxNumbers ) { fprintf(pFile, "%d ", buffer[i]); i++; } fclose(pFile); } int main(void) { float * a; hipMallocManaged(&a, 10000*sizeof(float)); float * b; hipMallocManaged(&b, 10000*sizeof(float)); for ( int i = 0; i < 10000; i++ ) b[i] = 10000.0f; for ( int i = 0; i < 10000; i++ ) a[i] = 5.0f; readData( "valuesA.txt", 10000, a ); writeData( "valuesC.txt", 10000, a ); { int blockSize = 256; int numBlocks = ( 10000 + blockSize - 1) / blockSize;hipLaunchKernelGGL(( add), dim3(numBlocks), dim3(blockSize), 0, 0, 10000, a, b ); } hipDeviceSynchronize(); for ( int i = 0; i < 10000; i++ ) printf("%d ", a); hipFree(a); hipFree(b); return 0; }
550746605648b0f906f666116a18e3ecb7d92169.cu
#include <iostream> #include <math.h> // Kernel function to add the elements of two arrays __global__ void add(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] + y[i]; } __global__ void subtract(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] - y[i]; } __global__ void multiply(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] * y[i]; } __global__ void divide(int n, float *x, float *y) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) y[i] = x[i] / y[i]; } void readData( char * filePath, int maxNumbers, float* buffer ){ FILE * pFile; int i = 0; pFile = fopen(filePath, "r"); while ( i < maxNumbers ) { int number; if (fscanf(pFile, "%d", &number) != 1) break; // file finished or there was an error buffer[i] = (float)number; i++; } fclose(pFile); } void writeData( char *filePath, int maxNumbers, float* buffer ){ FILE * pFile; int i = 0; pFile = fopen(filePath, "w"); while ( i < maxNumbers ) { fprintf(pFile, "%d ", buffer[i]); i++; } fclose(pFile); } int main(void) { float * a; cudaMallocManaged(&a, 10000*sizeof(float)); float * b; cudaMallocManaged(&b, 10000*sizeof(float)); for ( int i = 0; i < 10000; i++ ) b[i] = 10000.0f; for ( int i = 0; i < 10000; i++ ) a[i] = 5.0f; readData( "valuesA.txt", 10000, a ); writeData( "valuesC.txt", 10000, a ); { int blockSize = 256; int numBlocks = ( 10000 + blockSize - 1) / blockSize; add<<<numBlocks, blockSize>>>( 10000, a, b ); } cudaDeviceSynchronize(); for ( int i = 0; i < 10000; i++ ) printf("%d ", a); cudaFree(a); cudaFree(b); return 0; }
0a7d73e97926e607c97e72571f07416835ec0ab6.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "layer_params.h" void ConvLayerParams::initializeValues( cudnnHandle_t cudnn_handle, ConvDescriptor *user_params, cudnnDataType_t data_type, int batch_size, cudnnTensorFormat_t tensor_format, size_t data_type_size, LayerDimension &output_size, UpdateRule update_rule) { // create tensor, filter, conv descriptor checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&output_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&bias_desc)); checkCUDNN(cudnnCreateFilterDescriptor(&filter_desc)); checkCUDNN(cudnnCreateConvolutionDescriptor(&conv_desc)); C_in = user_params->input_channels; C_out = user_params->output_channels; filter_h = user_params->kernel_h; filter_w = user_params->kernel_w; kernel_size = C_out * C_in * filter_h * filter_w; this->data_type = data_type; this->activation_mode = user_params->activation_mode; checkCUDNN(cudnnSetTensor4dDescriptor( input_tensor, tensor_format, data_type, batch_size, user_params->input_channels, user_params->input_h, user_params->input_w)); checkCUDNN(cudnnSetFilter4dDescriptor( filter_desc, data_type, tensor_format, user_params->output_channels, user_params->input_channels, user_params->kernel_h, user_params->kernel_w)); int dilation_h = 1, dilation_w = 1; checkCUDNN(cudnnSetConvolution2dDescriptor( conv_desc, user_params->pad_h, user_params->pad_w, user_params->stride_y, user_params->stride_x, dilation_h, dilation_w, CUDNN_CROSS_CORRELATION, data_type)); int output_batch_size, output_channels, output_h, output_w; checkCUDNN(cudnnGetConvolution2dForwardOutputDim( conv_desc, input_tensor, filter_desc, &output_batch_size, &output_channels, &output_h, &output_w)); checkCUDNN(cudnnSetTensor4dDescriptor(output_tensor, tensor_format, data_type, output_batch_size, output_channels, output_h, output_w)); checkCUDNN(cudnnSetTensor4dDescriptor(bias_desc, tensor_format, data_type, 1, output_channels, 1, 1)); fwd_req_count = 10; fwd_perf = (cudnnConvolutionFwdAlgoPerf_t *)malloc( fwd_req_count * sizeof(cudnnConvolutionFwdAlgoPerf_t)); checkCUDNN(cudnnFindConvolutionForwardAlgorithm( cudnn_handle, input_tensor, filter_desc, conv_desc, output_tensor, fwd_req_count, &fwd_ret_count, fwd_perf)); // std::cout << "Printing forward conv algo perf\n"; // std::cout << "CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM: " << // CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM << std::endl; // for (int i = 0; i < fwd_ret_count; i++) { // std::cout << i << std::endl; // std::cout << "algo: " << fwd_perf[i].algo << std::endl; // std::cout << "status: " << cudnnGetErrorString(fwd_perf[i].status) << // std::endl; // std::cout << "time(ms): " << fwd_perf[i].time << std::endl; // std::cout << "memory(MB): " << fwd_perf[i].memory * 1.0 / 1024 / 1024 << // std::endl; // std::cout << "mathType: " << fwd_perf[i].mathType << std::endl; // std::cout << std::endl; // } bwd_filter_req_count = 10; bwd_filter_perf = (cudnnConvolutionBwdFilterAlgoPerf_t *)malloc( bwd_filter_req_count * sizeof(cudnnConvolutionBwdFilterAlgoPerf_t)); checkCUDNN(cudnnFindConvolutionBackwardFilterAlgorithm( cudnn_handle, input_tensor, output_tensor, conv_desc, filter_desc, bwd_filter_req_count, &bwd_filter_ret_count, bwd_filter_perf)); // std::cout << "Printing bwdfilter conv algo perf\n"; // std::cout << "CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1 " << // CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1 << std::endl; // for (int i = 0; i < bwd_filter_ret_count; i++) { // std::cout << i << std::endl; // std::cout << "algo: " << bwd_filter_perf[i].algo << std::endl; // std::cout << "status: " << // cudnnGetErrorString(bwd_filter_perf[i].status) << std::endl; // std::cout << "time(ms): " << bwd_filter_perf[i].time << std::endl; // std::cout << "memory(MB): " << bwd_filter_perf[i].memory * 1.0 / 1024 / // 1024 << std::endl; // std::cout << "mathType: " << bwd_filter_perf[i].mathType << std::endl; // std::cout << std::endl; // } bwd_data_req_count = 10; bwd_data_perf = (cudnnConvolutionBwdDataAlgoPerf_t *)malloc( bwd_data_req_count * sizeof(cudnnConvolutionBwdDataAlgoPerf_t)); checkCUDNN(cudnnFindConvolutionBackwardDataAlgorithm( cudnn_handle, filter_desc, output_tensor, conv_desc, input_tensor, bwd_data_req_count, &bwd_data_ret_count, bwd_data_perf)); // std::cout << "Printing bwddata conv algo perf\n"; // for (int i = 0; i < bwd_data_ret_count; i++) { // std::cout << i << std::endl; // std::cout << "algo: " << bwd_data_perf[i].algo << std::endl; // std::cout << "status: " << cudnnGetErrorString(bwd_data_perf[i].status) // << std::endl; // std::cout << "time(ms): " << bwd_data_perf[i].time << std::endl; // std::cout << "memory(MB): " << bwd_data_perf[i].memory * 1.0 / 1024 / // 1024 << std::endl; // std::cout << "mathType: " << bwd_data_perf[i].mathType << std::endl; // std::cout << std::endl; // } this->update_rule = update_rule; cudnnActivationMode_t mode; if (activation_mode == SIGMOID) mode = CUDNN_ACTIVATION_SIGMOID; else if (activation_mode == RELU) mode = CUDNN_ACTIVATION_RELU; else if (activation_mode == TANH) mode = CUDNN_ACTIVATION_TANH; else if (activation_mode == CLIPPED_RELU) mode = CUDNN_ACTIVATION_CLIPPED_RELU; else if (activation_mode == ELU) mode = CUDNN_ACTIVATION_ELU; if (activation_mode != ACTIVATION_NONE) { checkCUDNN(cudnnCreateActivationDescriptor(&actv_desc)); checkCUDNN(cudnnSetActivationDescriptor( actv_desc, mode, CUDNN_PROPAGATE_NAN, user_params->actv_coef)); } output_size.N = output_batch_size, output_size.C = output_channels, output_size.H = output_h, output_size.W = output_w; } void ConvLayerParams::allocateSpace(hiprandGenerator_t curand_gen, cudnnDataType_t data_type, size_t data_type_size, float std_dev, size_t &free_bytes, bool alloc_derivative) { if (kernel_size % 2 != 0) kernel_size += 1; checkCudaErrors(hipMalloc(&W, kernel_size * data_type_size)); checkCudaErrors(hipMalloc(&b, C_out * data_type_size)); if (alloc_derivative) { checkCudaErrors(hipMalloc(&dW, kernel_size * data_type_size)); checkCudaErrors(hipMalloc(&db, C_out * data_type_size)); } if (data_type == CUDNN_DATA_FLOAT) { checkCURAND( hiprandGenerateNormal(curand_gen, (float *)W, kernel_size, 0, std_dev)); hipLaunchKernelGGL(( fillValue<float>), dim3(ceil(1.0 * C_out / BW)), dim3(BW), 0, 0, (float *)b, C_out, 0); } else { checkCURAND(hiprandGenerateNormalDouble(curand_gen, (double *)W, kernel_size, 0, std_dev)); hipLaunchKernelGGL(( fillValue<double>), dim3(ceil(1.0 * C_out / BW)), dim3(BW), 0, 0, (double *)b, C_out, 0); } free_bytes = free_bytes - 2 * (kernel_size + C_out) * data_type_size; } void ConvLayerParams::cnmemAllocDerivatives(size_t data_type_size, hipStream_t stream) { checkCNMEM(cnmemMalloc(&dW, kernel_size * data_type_size, stream)); checkCNMEM(cnmemMalloc(&db, C_out * data_type_size, stream)); } bool ConvLayerParams::cnmemAllocDerivativesCheck(size_t data_type_size, hipStream_t stream, size_t &max_consume, size_t free_bytes, bool &out_of_memory) { checkCNMEMSim(cnmemMalloc(&dW, kernel_size * data_type_size, stream), kernel_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); checkCNMEMSim(cnmemMalloc(&db, C_out * data_type_size, stream), C_out * data_type_size, max_consume, free_bytes, return false, out_of_memory); return true; } void ConvLayerParams::stepParams(hipblasHandle_t cublas_handle, double learning_rate) { float Salpha = -learning_rate; double Dalpha = -learning_rate; if (update_rule == SGD) { if (data_type == CUDNN_DATA_FLOAT) { checkCUBLAS(hipblasSaxpy(cublas_handle, kernel_size, &Salpha, (float *)dW, 1, (float *)W, 1)); checkCUBLAS(hipblasSaxpy(cublas_handle, C_out, &Salpha, (float *)db, 1, (float *)b, 1)); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCUBLAS(hipblasDaxpy(cublas_handle, kernel_size, &Dalpha, (double *)dW, 1, (double *)W, 1)); checkCUBLAS(hipblasDaxpy(cublas_handle, C_out, &Dalpha, (double *)db, 1, (double *)b, 1)); } } } void ConvLayerParams::cnmemFreeDerivatives(hipStream_t stream) { checkCNMEM(cnmemFree(dW, stream)); checkCNMEM(cnmemFree(db, stream)); } size_t ConvLayerParams::getWorkspaceSize( size_t &free_bytes, ConvLayerParams::ConvDirection conv_direction, vDNNConvAlgo vdnn_conv_algo) { if (vdnn_conv_algo == vDNN_PERFORMANCE_OPTIMAL) { if (conv_direction == FWD) { if (fwd_perf[0].memory > free_bytes) outOfMemory(); fwd_algo = fwd_perf[0].algo; return fwd_perf[0].memory; } else if (conv_direction == BWD_FILTER) { if (bwd_filter_perf[0].memory > free_bytes) outOfMemory(); bwd_filter_algo = bwd_filter_perf[0].algo; return bwd_filter_perf[0].memory; } else if (conv_direction == BWD_DATA) { if (bwd_data_perf[0].memory > free_bytes) outOfMemory(); bwd_data_algo = bwd_data_perf[0].algo; return bwd_data_perf[0].memory; } } else if (vdnn_conv_algo == vDNN_MEMORY_OPTIMAL) { if (conv_direction == FWD) { for (int i = 0; i < fwd_ret_count; i++) { if (fwd_perf[i].algo == CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM && fwd_perf[i].status == CUDNN_STATUS_SUCCESS && fwd_perf[i].memory < free_bytes) { fwd_algo = fwd_perf[i].algo; return fwd_perf[i].memory; } } } else if (conv_direction == BWD_FILTER) { for (int i = 0; i < bwd_filter_ret_count; i++) { if (bwd_filter_perf[i].algo == CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1 && bwd_filter_perf[i].status == CUDNN_STATUS_SUCCESS && bwd_filter_perf[i].memory < free_bytes) { bwd_filter_algo = bwd_filter_perf[i].algo; // std::cout << "Free bytes " << free_bytes << std::endl; // std::cout << "bwd_filter_perf[i].memory " << // bwd_filter_perf[i].memory << std::endl; return bwd_filter_perf[i].memory; } } } else if (conv_direction == BWD_DATA) { for (int i = 0; i < bwd_data_ret_count; i++) { if (bwd_data_perf[i].algo == CUDNN_CONVOLUTION_BWD_DATA_ALGO_1 && bwd_data_perf[i].status == CUDNN_STATUS_SUCCESS && bwd_data_perf[i].memory < free_bytes) { bwd_data_algo = bwd_data_perf[i].algo; return bwd_data_perf[i].memory; } } } std::cout << "Error in getWorkspaceSize" << std::endl; exit(0); } return 0; } workspaceStatus_t ConvLayerParams::getWorkspaceSize( size_t &free_bytes, ConvLayerParams::ConvDirection conv_direction, vDNNConvAlgoPref algo_pref, bool hard_pref, size_t &workspace_size) { if (hard_pref) { if (algo_pref == PREFER_PERFORMANCE_OPTIMAL) { if (conv_direction == FWD) { if (fwd_perf[0].memory > free_bytes && fwd_perf[0].status == CUDNN_STATUS_SUCCESS) return WORKSPACE_STATUS_OUT_OF_MEMORY; fwd_algo = fwd_perf[0].algo; fwd_workspace_size = fwd_perf[0].memory; workspace_size = fwd_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else if (conv_direction == BWD_FILTER) { if (bwd_filter_perf[0].memory > free_bytes && bwd_filter_perf[0].status == CUDNN_STATUS_SUCCESS) return WORKSPACE_STATUS_OUT_OF_MEMORY; bwd_filter_algo = bwd_filter_perf[0].algo; bwd_filter_workspace_size = bwd_filter_perf[0].memory; workspace_size = bwd_filter_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else if (conv_direction == BWD_DATA) { if (bwd_data_perf[0].memory > free_bytes && bwd_data_perf[0].status == CUDNN_STATUS_SUCCESS) return WORKSPACE_STATUS_OUT_OF_MEMORY; bwd_data_algo = bwd_data_perf[0].algo; bwd_data_workspace_size = bwd_data_perf[0].memory; workspace_size = bwd_data_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } else if (algo_pref == PREFER_MEMORY_OPTIMAL) { if (conv_direction == FWD) { for (int i = 0; i < fwd_ret_count; i++) { if (fwd_perf[i].algo == CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM) if (fwd_perf[i].memory < free_bytes && fwd_perf[i].status == CUDNN_STATUS_SUCCESS) { fwd_algo = fwd_perf[i].algo; fwd_workspace_size = fwd_perf[i].memory; workspace_size = fwd_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else return WORKSPACE_STATUS_OUT_OF_MEMORY; } } else if (conv_direction == BWD_FILTER) { for (int i = 0; i < bwd_filter_ret_count; i++) { if (bwd_filter_perf[i].algo == CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1) if (bwd_filter_perf[i].memory < free_bytes && bwd_filter_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_filter_algo = bwd_filter_perf[i].algo; // std::cout << "Free bytes " << free_bytes << std::endl; // std::cout << "bwd_filter_perf[i].memory " << // bwd_filter_perf[i].memory << std::endl; bwd_filter_workspace_size = bwd_filter_perf[i].memory; workspace_size = bwd_filter_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else return WORKSPACE_STATUS_OUT_OF_MEMORY; } } else if (conv_direction == BWD_DATA) { for (int i = 0; i < bwd_data_ret_count; i++) { if (bwd_data_perf[i].algo == CUDNN_CONVOLUTION_BWD_DATA_ALGO_1) if (bwd_data_perf[i].memory < free_bytes && bwd_data_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_data_algo = bwd_data_perf[i].algo; bwd_data_workspace_size = bwd_data_perf[i].memory; workspace_size = bwd_data_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else return WORKSPACE_STATUS_OUT_OF_MEMORY; } } } } else { // only performance optimal is possible if (algo_pref == PREFER_PERFORMANCE_OPTIMAL) { if (conv_direction == FWD) { for (int i = 0; i < fwd_ret_count; i++) { if (fwd_perf[i].memory < free_bytes && fwd_perf[i].status == CUDNN_STATUS_SUCCESS) { fwd_algo = fwd_perf[i].algo; fwd_workspace_size = fwd_perf[i].memory; workspace_size = fwd_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } } else if (conv_direction == BWD_FILTER) { for (int i = 0; i < bwd_filter_ret_count; i++) { if (bwd_filter_perf[i].memory < free_bytes && bwd_filter_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_filter_algo = bwd_filter_perf[i].algo; // std::cout << "Free bytes " << free_bytes << std::endl; // std::cout << "bwd_filter_perf[i].memory " << // bwd_filter_perf[i].memory << std::endl; bwd_filter_workspace_size = bwd_filter_perf[i].memory; workspace_size = bwd_filter_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } } else if (conv_direction == BWD_DATA) { for (int i = 0; i < bwd_data_ret_count; i++) { if (bwd_data_perf[i].memory < free_bytes && bwd_data_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_data_algo = bwd_data_perf[i].algo; bwd_data_workspace_size = bwd_data_perf[i].memory; workspace_size = bwd_data_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } } } } return WORKSPACE_STATUS_OUT_OF_MEMORY; } void FCLayerParams::initializeValues(FCDescriptor *user_params, int batch_size, cudnnTensorFormat_t tensor_format, cudnnDataType_t data_type, LayerDimension &output_size, UpdateRule update_rule) { C_in = user_params->input_channels; C_out = user_params->output_channels; weight_matrix_size = C_in * C_out; this->data_type = data_type; this->activation_mode = user_params->activation_mode; this->update_rule = update_rule; cudnnActivationMode_t mode; if (activation_mode == SIGMOID) mode = CUDNN_ACTIVATION_SIGMOID; else if (activation_mode == RELU) mode = CUDNN_ACTIVATION_RELU; else if (activation_mode == TANH) mode = CUDNN_ACTIVATION_TANH; else if (activation_mode == CLIPPED_RELU) mode = CUDNN_ACTIVATION_CLIPPED_RELU; else if (activation_mode == ELU) mode = CUDNN_ACTIVATION_ELU; if (activation_mode != ACTIVATION_NONE) { checkCUDNN(cudnnCreateActivationDescriptor(&actv_desc)); checkCUDNN(cudnnSetActivationDescriptor( actv_desc, mode, CUDNN_PROPAGATE_NAN, user_params->actv_coef)); checkCUDNN(cudnnCreateTensorDescriptor(&output_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(output_tensor, tensor_format, data_type, batch_size, user_params->output_channels, 1, 1)); } output_size.N = batch_size, output_size.C = C_out, output_size.H = output_size.W = 1; } void FCLayerParams::allocateSpace(hiprandGenerator_t curand_gen, cudnnDataType_t data_type, size_t data_type_size, float std_dev, size_t &free_bytes, bool alloc_derivative) { int wt_alloc_size = weight_matrix_size; if (wt_alloc_size % 2 != 0) wt_alloc_size += 1; checkCudaErrors(hipMalloc(&W, wt_alloc_size * data_type_size)); checkCudaErrors(hipMalloc(&b, C_out * data_type_size)); if (alloc_derivative) { checkCudaErrors(hipMalloc(&dW, wt_alloc_size * data_type_size)); checkCudaErrors(hipMalloc(&db, C_out * data_type_size)); } if (data_type == CUDNN_DATA_FLOAT) { checkCURAND(hiprandGenerateNormal(curand_gen, (float *)W, wt_alloc_size, 0, std_dev)); hipLaunchKernelGGL(( fillValue<float>), dim3(ceil(1.0 * C_out / BW)), dim3(BW), 0, 0, (float *)b, C_out, 0); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCURAND(hiprandGenerateNormalDouble(curand_gen, (double *)W, wt_alloc_size, 0, std_dev)); hipLaunchKernelGGL(( fillValue<double>), dim3(ceil(1.0 * C_out / BW)), dim3(BW), 0, 0, (double *)b, C_out, 0); } free_bytes = free_bytes - 2 * (C_in * C_out + C_out) * data_type_size; } void FCLayerParams::cnmemAllocDerivatives(size_t data_type_size, hipStream_t stream) { checkCNMEM(cnmemMalloc(&dW, weight_matrix_size * data_type_size, stream)); checkCNMEM(cnmemMalloc(&db, C_out * data_type_size, stream)); } bool FCLayerParams::cnmemAllocDerivativesCheck(size_t data_type_size, hipStream_t stream, size_t &max_consume, size_t free_bytes, bool &out_of_memory) { checkCNMEMSim(cnmemMalloc(&dW, weight_matrix_size * data_type_size, stream), weight_matrix_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); checkCNMEMSim(cnmemMalloc(&db, C_out * data_type_size, stream), C_out * data_type_size, max_consume, free_bytes, return false, out_of_memory); return true; } void FCLayerParams::stepParams(hipblasHandle_t cublas_handle, double learning_rate) { float Salpha = -learning_rate; double Dalpha = -learning_rate; // { // float *db_h = (float *)malloc(C_out * sizeof(float)); // checkCudaErrors(hipMemcpy(db_h, db, C_out * sizeof(float), // hipMemcpyDeviceToHost)); // for (int i = 0; i < C_out; i++) { // std::cout << db_h[i] << ' '; // } // std::cout << "\n"; // int n; // std::cin >> n; // } if (update_rule == SGD) { if (data_type == CUDNN_DATA_FLOAT) { checkCUBLAS(hipblasSaxpy(cublas_handle, weight_matrix_size, &Salpha, (float *)dW, 1, (float *)W, 1)); checkCUBLAS(hipblasSaxpy(cublas_handle, C_out, &Salpha, (float *)db, 1, (float *)b, 1)); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCUBLAS(hipblasDaxpy(cublas_handle, weight_matrix_size, &Dalpha, (double *)dW, 1, (double *)W, 1)); checkCUBLAS(hipblasDaxpy(cublas_handle, C_out, &Dalpha, (double *)db, 1, (double *)b, 1)); } } // { // float *db_h = (float *)malloc(C_out * sizeof(float)); // checkCudaErrors(hipMemcpy(db_h, b, C_out * sizeof(float), // hipMemcpyDeviceToHost)); // for (int i = 0; i < C_out; i++) { // std::cout << db_h[i] << ' '; // } // std::cout << "\n"; // int n; // std::cin >> n; // } } void FCLayerParams::cnmemFreeDerivatives(hipStream_t stream) { checkCNMEM(cnmemFree(dW, stream)); checkCNMEM(cnmemFree(db, stream)); } void DropoutLayerParams::initializeValues(cudnnHandle_t cudnn_handle, DropoutDescriptor *user_params, cudnnDataType_t data_type, int batch_size, cudnnTensorFormat_t tensor_format, LayerDimension &output_size) { checkCUDNN(cudnnCreateDropoutDescriptor(&dropout_desc)); checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); checkCUDNN(cudnnDropoutGetStatesSize(cudnn_handle, &state_size)); checkCUDNN( cudnnDropoutGetReserveSpaceSize(input_tensor, &reserved_space_size)); output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void DropoutLayerParams::allocateSpace(size_t &free_bytes, cudnnHandle_t cudnn_handle, DropoutDescriptor *user_params, long long seed) { checkCudaErrors(hipMalloc(&state, state_size)); checkCudaErrors(hipMalloc(&reserved_space, reserved_space_size)); checkCUDNN(cudnnSetDropoutDescriptor(dropout_desc, cudnn_handle, user_params->dropout_value, state, state_size, seed)); free_bytes = free_bytes - (state_size + reserved_space_size); } void BatchNormLayerParams::initializeValues(BatchNormDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size, UpdateRule update_rule) { checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&sbmv_desc)); c = user_params->channels, h = user_params->h, w = user_params->w; if (user_params->mode == BATCHNORM_PER_ACTIVATION) { mode = CUDNN_BATCHNORM_PER_ACTIVATION; checkCUDNN(cudnnSetTensor4dDescriptor(sbmv_desc, tensor_format, data_type, 1, user_params->channels, user_params->h, user_params->w)); sbmv_size = c * h * w; } else if (user_params->mode == BATCHNORM_SPATIAL) { mode = CUDNN_BATCHNORM_SPATIAL; checkCUDNN(cudnnSetTensor4dDescriptor(sbmv_desc, tensor_format, data_type, 1, user_params->channels, 1, 1)); sbmv_size = c; } checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); factor = user_params->factor; epsilon = user_params->epsilon; this->update_rule = update_rule; this->data_type = data_type; if (mode == CUDNN_BATCHNORM_PER_ACTIVATION) allocation_size = c * h * w; else allocation_size = c; output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void BatchNormLayerParams::allocateSpace(cudnnDataType_t data_type, size_t data_type_size, size_t &free_bytes, bool alloc_derivative) { size_t allocation_size_bytes = allocation_size * data_type_size; checkCudaErrors(hipMalloc(&scale, allocation_size_bytes)); checkCudaErrors(hipMalloc(&bias, allocation_size_bytes)); if (alloc_derivative) { checkCudaErrors(hipMalloc(&dscale, allocation_size_bytes)); checkCudaErrors(hipMalloc(&dbias, allocation_size_bytes)); } checkCudaErrors(hipMalloc(&running_mean, allocation_size_bytes)); checkCudaErrors(hipMalloc(&running_variance, allocation_size_bytes)); checkCudaErrors(hipMalloc(&result_save_mean, allocation_size_bytes)); checkCudaErrors(hipMalloc(&result_save_inv_var, allocation_size_bytes)); if (data_type == CUDNN_DATA_FLOAT) { hipLaunchKernelGGL(( fillValue<float>), dim3(ceil(1.0 * allocation_size / BW)), dim3(BW), 0, 0, (float *)scale, allocation_size, 1); hipLaunchKernelGGL(( fillValue<float>), dim3(ceil(1.0 * allocation_size / BW)), dim3(BW), 0, 0, (float *)bias, allocation_size, 1); } else if (data_type == CUDNN_DATA_DOUBLE) { hipLaunchKernelGGL(( fillValue<double>), dim3(ceil(1.0 * allocation_size / BW)), dim3(BW), 0, 0, (double *)scale, allocation_size, 1); hipLaunchKernelGGL(( fillValue<double>), dim3(ceil(1.0 * allocation_size / BW)), dim3(BW), 0, 0, (double *)bias, allocation_size, 1); } free_bytes = free_bytes - 6 * allocation_size_bytes; } void BatchNormLayerParams::cnmemAllocDerivatives(size_t data_type_size, hipStream_t stream) { checkCNMEM(cnmemMalloc(&dscale, allocation_size * data_type_size, stream)); checkCNMEM(cnmemMalloc(&dbias, allocation_size * data_type_size, stream)); } bool BatchNormLayerParams::cnmemAllocDerivativesCheck(size_t data_type_size, hipStream_t stream, size_t &max_consume, size_t free_bytes, bool &out_of_memory) { checkCNMEMSim(cnmemMalloc(&dscale, allocation_size * data_type_size, stream), allocation_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); checkCNMEMSim(cnmemMalloc(&dbias, allocation_size * data_type_size, stream), allocation_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); return true; } void BatchNormLayerParams::stepParams(hipblasHandle_t cublas_handle, double learning_rate) { float Salpha = -learning_rate; double Dalpha = -learning_rate; if (update_rule == SGD) { if (data_type == CUDNN_DATA_FLOAT) { checkCUBLAS(hipblasSaxpy(cublas_handle, sbmv_size, &Salpha, (float *)dscale, 1, (float *)scale, 1)); checkCUBLAS(hipblasSaxpy(cublas_handle, sbmv_size, &Salpha, (float *)dbias, 1, (float *)bias, 1)); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCUBLAS(hipblasDaxpy(cublas_handle, sbmv_size, &Dalpha, (double *)dscale, 1, (double *)scale, 1)); checkCUBLAS(hipblasDaxpy(cublas_handle, sbmv_size, &Dalpha, (double *)dbias, 1, (double *)bias, 1)); } } } void BatchNormLayerParams::cnmemFreeDerivatives(hipStream_t stream) { checkCNMEM(cnmemFree(dscale, stream)); checkCNMEM(cnmemFree(dbias, stream)); } void PoolingLayerParams::initializeValues(PoolingDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size) { checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&output_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor( input_tensor, tensor_format, data_type, batch_size, user_params->input_channels, user_params->input_h, user_params->input_w)); checkCUDNN(cudnnCreatePoolingDescriptor(&pool_desc)); cudnnPoolingMode_t mode; if (user_params->mode == POOLING_MAX) mode = CUDNN_POOLING_MAX; else if (user_params->mode == POOLING_AVERAGE_COUNT_INCLUDE_PADDING) mode = CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING; else if (user_params->mode == POOLING_AVERAGE_COUNT_EXCLUDE_PADDING) mode = CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING; checkCUDNN(cudnnSetPooling2dDescriptor( pool_desc, mode, CUDNN_PROPAGATE_NAN, user_params->kernel_h, user_params->kernel_w, user_params->pad_h, user_params->pad_w, user_params->stride_y, user_params->stride_x)); int output_batch_size, output_channels, output_h, output_w; checkCUDNN(cudnnGetPooling2dForwardOutputDim( pool_desc, input_tensor, &output_batch_size, &output_channels, &output_h, &output_w)); checkCUDNN(cudnnSetTensor4dDescriptor(output_tensor, tensor_format, data_type, output_batch_size, output_channels, output_h, output_w)); output_size.N = output_batch_size, output_size.C = output_channels, output_size.H = output_h, output_size.W = output_w; } void PoolingLayerParams::allocateSpace(size_t &free_bytes) {} void ActivationLayerParams::initializeValues(ActivationDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size) { checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); cudnnActivationMode_t mode; if (user_params->mode == SIGMOID) mode = CUDNN_ACTIVATION_SIGMOID; else if (user_params->mode == RELU) mode = CUDNN_ACTIVATION_RELU; else if (user_params->mode == TANH) mode = CUDNN_ACTIVATION_TANH; else if (user_params->mode == CLIPPED_RELU) mode = CUDNN_ACTIVATION_CLIPPED_RELU; else if (user_params->mode == ELU) mode = CUDNN_ACTIVATION_ELU; checkCUDNN(cudnnCreateActivationDescriptor(&actv_desc)); checkCUDNN(cudnnSetActivationDescriptor(actv_desc, mode, CUDNN_PROPAGATE_NAN, user_params->coef)); output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void ActivationLayerParams::allocateSpace(size_t &free_bytes) {} void SoftmaxLayerParams::initializeValues(SoftmaxDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size) { if (user_params->algo == SOFTMAX_FAST) algo = CUDNN_SOFTMAX_FAST; else if (user_params->algo == SOFTMAX_ACCURATE) algo = CUDNN_SOFTMAX_ACCURATE; if (user_params->mode == SOFTMAX_MODE_INSTANCE) mode = CUDNN_SOFTMAX_MODE_INSTANCE; else if (user_params->mode == SOFTMAX_MODE_CHANNEL) { mode = CUDNN_SOFTMAX_MODE_CHANNEL; } checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void SoftmaxLayerParams::allocateSpace(size_t &free_bytes) {}
0a7d73e97926e607c97e72571f07416835ec0ab6.cu
#include "layer_params.h" void ConvLayerParams::initializeValues( cudnnHandle_t cudnn_handle, ConvDescriptor *user_params, cudnnDataType_t data_type, int batch_size, cudnnTensorFormat_t tensor_format, size_t data_type_size, LayerDimension &output_size, UpdateRule update_rule) { // create tensor, filter, conv descriptor checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&output_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&bias_desc)); checkCUDNN(cudnnCreateFilterDescriptor(&filter_desc)); checkCUDNN(cudnnCreateConvolutionDescriptor(&conv_desc)); C_in = user_params->input_channels; C_out = user_params->output_channels; filter_h = user_params->kernel_h; filter_w = user_params->kernel_w; kernel_size = C_out * C_in * filter_h * filter_w; this->data_type = data_type; this->activation_mode = user_params->activation_mode; checkCUDNN(cudnnSetTensor4dDescriptor( input_tensor, tensor_format, data_type, batch_size, user_params->input_channels, user_params->input_h, user_params->input_w)); checkCUDNN(cudnnSetFilter4dDescriptor( filter_desc, data_type, tensor_format, user_params->output_channels, user_params->input_channels, user_params->kernel_h, user_params->kernel_w)); int dilation_h = 1, dilation_w = 1; checkCUDNN(cudnnSetConvolution2dDescriptor( conv_desc, user_params->pad_h, user_params->pad_w, user_params->stride_y, user_params->stride_x, dilation_h, dilation_w, CUDNN_CROSS_CORRELATION, data_type)); int output_batch_size, output_channels, output_h, output_w; checkCUDNN(cudnnGetConvolution2dForwardOutputDim( conv_desc, input_tensor, filter_desc, &output_batch_size, &output_channels, &output_h, &output_w)); checkCUDNN(cudnnSetTensor4dDescriptor(output_tensor, tensor_format, data_type, output_batch_size, output_channels, output_h, output_w)); checkCUDNN(cudnnSetTensor4dDescriptor(bias_desc, tensor_format, data_type, 1, output_channels, 1, 1)); fwd_req_count = 10; fwd_perf = (cudnnConvolutionFwdAlgoPerf_t *)malloc( fwd_req_count * sizeof(cudnnConvolutionFwdAlgoPerf_t)); checkCUDNN(cudnnFindConvolutionForwardAlgorithm( cudnn_handle, input_tensor, filter_desc, conv_desc, output_tensor, fwd_req_count, &fwd_ret_count, fwd_perf)); // std::cout << "Printing forward conv algo perf\n"; // std::cout << "CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM: " << // CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM << std::endl; // for (int i = 0; i < fwd_ret_count; i++) { // std::cout << i << std::endl; // std::cout << "algo: " << fwd_perf[i].algo << std::endl; // std::cout << "status: " << cudnnGetErrorString(fwd_perf[i].status) << // std::endl; // std::cout << "time(ms): " << fwd_perf[i].time << std::endl; // std::cout << "memory(MB): " << fwd_perf[i].memory * 1.0 / 1024 / 1024 << // std::endl; // std::cout << "mathType: " << fwd_perf[i].mathType << std::endl; // std::cout << std::endl; // } bwd_filter_req_count = 10; bwd_filter_perf = (cudnnConvolutionBwdFilterAlgoPerf_t *)malloc( bwd_filter_req_count * sizeof(cudnnConvolutionBwdFilterAlgoPerf_t)); checkCUDNN(cudnnFindConvolutionBackwardFilterAlgorithm( cudnn_handle, input_tensor, output_tensor, conv_desc, filter_desc, bwd_filter_req_count, &bwd_filter_ret_count, bwd_filter_perf)); // std::cout << "Printing bwdfilter conv algo perf\n"; // std::cout << "CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1 " << // CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1 << std::endl; // for (int i = 0; i < bwd_filter_ret_count; i++) { // std::cout << i << std::endl; // std::cout << "algo: " << bwd_filter_perf[i].algo << std::endl; // std::cout << "status: " << // cudnnGetErrorString(bwd_filter_perf[i].status) << std::endl; // std::cout << "time(ms): " << bwd_filter_perf[i].time << std::endl; // std::cout << "memory(MB): " << bwd_filter_perf[i].memory * 1.0 / 1024 / // 1024 << std::endl; // std::cout << "mathType: " << bwd_filter_perf[i].mathType << std::endl; // std::cout << std::endl; // } bwd_data_req_count = 10; bwd_data_perf = (cudnnConvolutionBwdDataAlgoPerf_t *)malloc( bwd_data_req_count * sizeof(cudnnConvolutionBwdDataAlgoPerf_t)); checkCUDNN(cudnnFindConvolutionBackwardDataAlgorithm( cudnn_handle, filter_desc, output_tensor, conv_desc, input_tensor, bwd_data_req_count, &bwd_data_ret_count, bwd_data_perf)); // std::cout << "Printing bwddata conv algo perf\n"; // for (int i = 0; i < bwd_data_ret_count; i++) { // std::cout << i << std::endl; // std::cout << "algo: " << bwd_data_perf[i].algo << std::endl; // std::cout << "status: " << cudnnGetErrorString(bwd_data_perf[i].status) // << std::endl; // std::cout << "time(ms): " << bwd_data_perf[i].time << std::endl; // std::cout << "memory(MB): " << bwd_data_perf[i].memory * 1.0 / 1024 / // 1024 << std::endl; // std::cout << "mathType: " << bwd_data_perf[i].mathType << std::endl; // std::cout << std::endl; // } this->update_rule = update_rule; cudnnActivationMode_t mode; if (activation_mode == SIGMOID) mode = CUDNN_ACTIVATION_SIGMOID; else if (activation_mode == RELU) mode = CUDNN_ACTIVATION_RELU; else if (activation_mode == TANH) mode = CUDNN_ACTIVATION_TANH; else if (activation_mode == CLIPPED_RELU) mode = CUDNN_ACTIVATION_CLIPPED_RELU; else if (activation_mode == ELU) mode = CUDNN_ACTIVATION_ELU; if (activation_mode != ACTIVATION_NONE) { checkCUDNN(cudnnCreateActivationDescriptor(&actv_desc)); checkCUDNN(cudnnSetActivationDescriptor( actv_desc, mode, CUDNN_PROPAGATE_NAN, user_params->actv_coef)); } output_size.N = output_batch_size, output_size.C = output_channels, output_size.H = output_h, output_size.W = output_w; } void ConvLayerParams::allocateSpace(curandGenerator_t curand_gen, cudnnDataType_t data_type, size_t data_type_size, float std_dev, size_t &free_bytes, bool alloc_derivative) { if (kernel_size % 2 != 0) kernel_size += 1; checkCudaErrors(cudaMalloc(&W, kernel_size * data_type_size)); checkCudaErrors(cudaMalloc(&b, C_out * data_type_size)); if (alloc_derivative) { checkCudaErrors(cudaMalloc(&dW, kernel_size * data_type_size)); checkCudaErrors(cudaMalloc(&db, C_out * data_type_size)); } if (data_type == CUDNN_DATA_FLOAT) { checkCURAND( curandGenerateNormal(curand_gen, (float *)W, kernel_size, 0, std_dev)); fillValue<float><<<ceil(1.0 * C_out / BW), BW>>>((float *)b, C_out, 0); } else { checkCURAND(curandGenerateNormalDouble(curand_gen, (double *)W, kernel_size, 0, std_dev)); fillValue<double><<<ceil(1.0 * C_out / BW), BW>>>((double *)b, C_out, 0); } free_bytes = free_bytes - 2 * (kernel_size + C_out) * data_type_size; } void ConvLayerParams::cnmemAllocDerivatives(size_t data_type_size, cudaStream_t stream) { checkCNMEM(cnmemMalloc(&dW, kernel_size * data_type_size, stream)); checkCNMEM(cnmemMalloc(&db, C_out * data_type_size, stream)); } bool ConvLayerParams::cnmemAllocDerivativesCheck(size_t data_type_size, cudaStream_t stream, size_t &max_consume, size_t free_bytes, bool &out_of_memory) { checkCNMEMSim(cnmemMalloc(&dW, kernel_size * data_type_size, stream), kernel_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); checkCNMEMSim(cnmemMalloc(&db, C_out * data_type_size, stream), C_out * data_type_size, max_consume, free_bytes, return false, out_of_memory); return true; } void ConvLayerParams::stepParams(cublasHandle_t cublas_handle, double learning_rate) { float Salpha = -learning_rate; double Dalpha = -learning_rate; if (update_rule == SGD) { if (data_type == CUDNN_DATA_FLOAT) { checkCUBLAS(cublasSaxpy(cublas_handle, kernel_size, &Salpha, (float *)dW, 1, (float *)W, 1)); checkCUBLAS(cublasSaxpy(cublas_handle, C_out, &Salpha, (float *)db, 1, (float *)b, 1)); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCUBLAS(cublasDaxpy(cublas_handle, kernel_size, &Dalpha, (double *)dW, 1, (double *)W, 1)); checkCUBLAS(cublasDaxpy(cublas_handle, C_out, &Dalpha, (double *)db, 1, (double *)b, 1)); } } } void ConvLayerParams::cnmemFreeDerivatives(cudaStream_t stream) { checkCNMEM(cnmemFree(dW, stream)); checkCNMEM(cnmemFree(db, stream)); } size_t ConvLayerParams::getWorkspaceSize( size_t &free_bytes, ConvLayerParams::ConvDirection conv_direction, vDNNConvAlgo vdnn_conv_algo) { if (vdnn_conv_algo == vDNN_PERFORMANCE_OPTIMAL) { if (conv_direction == FWD) { if (fwd_perf[0].memory > free_bytes) outOfMemory(); fwd_algo = fwd_perf[0].algo; return fwd_perf[0].memory; } else if (conv_direction == BWD_FILTER) { if (bwd_filter_perf[0].memory > free_bytes) outOfMemory(); bwd_filter_algo = bwd_filter_perf[0].algo; return bwd_filter_perf[0].memory; } else if (conv_direction == BWD_DATA) { if (bwd_data_perf[0].memory > free_bytes) outOfMemory(); bwd_data_algo = bwd_data_perf[0].algo; return bwd_data_perf[0].memory; } } else if (vdnn_conv_algo == vDNN_MEMORY_OPTIMAL) { if (conv_direction == FWD) { for (int i = 0; i < fwd_ret_count; i++) { if (fwd_perf[i].algo == CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM && fwd_perf[i].status == CUDNN_STATUS_SUCCESS && fwd_perf[i].memory < free_bytes) { fwd_algo = fwd_perf[i].algo; return fwd_perf[i].memory; } } } else if (conv_direction == BWD_FILTER) { for (int i = 0; i < bwd_filter_ret_count; i++) { if (bwd_filter_perf[i].algo == CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1 && bwd_filter_perf[i].status == CUDNN_STATUS_SUCCESS && bwd_filter_perf[i].memory < free_bytes) { bwd_filter_algo = bwd_filter_perf[i].algo; // std::cout << "Free bytes " << free_bytes << std::endl; // std::cout << "bwd_filter_perf[i].memory " << // bwd_filter_perf[i].memory << std::endl; return bwd_filter_perf[i].memory; } } } else if (conv_direction == BWD_DATA) { for (int i = 0; i < bwd_data_ret_count; i++) { if (bwd_data_perf[i].algo == CUDNN_CONVOLUTION_BWD_DATA_ALGO_1 && bwd_data_perf[i].status == CUDNN_STATUS_SUCCESS && bwd_data_perf[i].memory < free_bytes) { bwd_data_algo = bwd_data_perf[i].algo; return bwd_data_perf[i].memory; } } } std::cout << "Error in getWorkspaceSize" << std::endl; exit(0); } return 0; } workspaceStatus_t ConvLayerParams::getWorkspaceSize( size_t &free_bytes, ConvLayerParams::ConvDirection conv_direction, vDNNConvAlgoPref algo_pref, bool hard_pref, size_t &workspace_size) { if (hard_pref) { if (algo_pref == PREFER_PERFORMANCE_OPTIMAL) { if (conv_direction == FWD) { if (fwd_perf[0].memory > free_bytes && fwd_perf[0].status == CUDNN_STATUS_SUCCESS) return WORKSPACE_STATUS_OUT_OF_MEMORY; fwd_algo = fwd_perf[0].algo; fwd_workspace_size = fwd_perf[0].memory; workspace_size = fwd_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else if (conv_direction == BWD_FILTER) { if (bwd_filter_perf[0].memory > free_bytes && bwd_filter_perf[0].status == CUDNN_STATUS_SUCCESS) return WORKSPACE_STATUS_OUT_OF_MEMORY; bwd_filter_algo = bwd_filter_perf[0].algo; bwd_filter_workspace_size = bwd_filter_perf[0].memory; workspace_size = bwd_filter_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else if (conv_direction == BWD_DATA) { if (bwd_data_perf[0].memory > free_bytes && bwd_data_perf[0].status == CUDNN_STATUS_SUCCESS) return WORKSPACE_STATUS_OUT_OF_MEMORY; bwd_data_algo = bwd_data_perf[0].algo; bwd_data_workspace_size = bwd_data_perf[0].memory; workspace_size = bwd_data_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } else if (algo_pref == PREFER_MEMORY_OPTIMAL) { if (conv_direction == FWD) { for (int i = 0; i < fwd_ret_count; i++) { if (fwd_perf[i].algo == CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM) if (fwd_perf[i].memory < free_bytes && fwd_perf[i].status == CUDNN_STATUS_SUCCESS) { fwd_algo = fwd_perf[i].algo; fwd_workspace_size = fwd_perf[i].memory; workspace_size = fwd_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else return WORKSPACE_STATUS_OUT_OF_MEMORY; } } else if (conv_direction == BWD_FILTER) { for (int i = 0; i < bwd_filter_ret_count; i++) { if (bwd_filter_perf[i].algo == CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1) if (bwd_filter_perf[i].memory < free_bytes && bwd_filter_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_filter_algo = bwd_filter_perf[i].algo; // std::cout << "Free bytes " << free_bytes << std::endl; // std::cout << "bwd_filter_perf[i].memory " << // bwd_filter_perf[i].memory << std::endl; bwd_filter_workspace_size = bwd_filter_perf[i].memory; workspace_size = bwd_filter_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else return WORKSPACE_STATUS_OUT_OF_MEMORY; } } else if (conv_direction == BWD_DATA) { for (int i = 0; i < bwd_data_ret_count; i++) { if (bwd_data_perf[i].algo == CUDNN_CONVOLUTION_BWD_DATA_ALGO_1) if (bwd_data_perf[i].memory < free_bytes && bwd_data_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_data_algo = bwd_data_perf[i].algo; bwd_data_workspace_size = bwd_data_perf[i].memory; workspace_size = bwd_data_workspace_size; return WORKSPACE_STATUS_SUCCESS; } else return WORKSPACE_STATUS_OUT_OF_MEMORY; } } } } else { // only performance optimal is possible if (algo_pref == PREFER_PERFORMANCE_OPTIMAL) { if (conv_direction == FWD) { for (int i = 0; i < fwd_ret_count; i++) { if (fwd_perf[i].memory < free_bytes && fwd_perf[i].status == CUDNN_STATUS_SUCCESS) { fwd_algo = fwd_perf[i].algo; fwd_workspace_size = fwd_perf[i].memory; workspace_size = fwd_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } } else if (conv_direction == BWD_FILTER) { for (int i = 0; i < bwd_filter_ret_count; i++) { if (bwd_filter_perf[i].memory < free_bytes && bwd_filter_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_filter_algo = bwd_filter_perf[i].algo; // std::cout << "Free bytes " << free_bytes << std::endl; // std::cout << "bwd_filter_perf[i].memory " << // bwd_filter_perf[i].memory << std::endl; bwd_filter_workspace_size = bwd_filter_perf[i].memory; workspace_size = bwd_filter_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } } else if (conv_direction == BWD_DATA) { for (int i = 0; i < bwd_data_ret_count; i++) { if (bwd_data_perf[i].memory < free_bytes && bwd_data_perf[i].status == CUDNN_STATUS_SUCCESS) { bwd_data_algo = bwd_data_perf[i].algo; bwd_data_workspace_size = bwd_data_perf[i].memory; workspace_size = bwd_data_workspace_size; return WORKSPACE_STATUS_SUCCESS; } } } } } return WORKSPACE_STATUS_OUT_OF_MEMORY; } void FCLayerParams::initializeValues(FCDescriptor *user_params, int batch_size, cudnnTensorFormat_t tensor_format, cudnnDataType_t data_type, LayerDimension &output_size, UpdateRule update_rule) { C_in = user_params->input_channels; C_out = user_params->output_channels; weight_matrix_size = C_in * C_out; this->data_type = data_type; this->activation_mode = user_params->activation_mode; this->update_rule = update_rule; cudnnActivationMode_t mode; if (activation_mode == SIGMOID) mode = CUDNN_ACTIVATION_SIGMOID; else if (activation_mode == RELU) mode = CUDNN_ACTIVATION_RELU; else if (activation_mode == TANH) mode = CUDNN_ACTIVATION_TANH; else if (activation_mode == CLIPPED_RELU) mode = CUDNN_ACTIVATION_CLIPPED_RELU; else if (activation_mode == ELU) mode = CUDNN_ACTIVATION_ELU; if (activation_mode != ACTIVATION_NONE) { checkCUDNN(cudnnCreateActivationDescriptor(&actv_desc)); checkCUDNN(cudnnSetActivationDescriptor( actv_desc, mode, CUDNN_PROPAGATE_NAN, user_params->actv_coef)); checkCUDNN(cudnnCreateTensorDescriptor(&output_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(output_tensor, tensor_format, data_type, batch_size, user_params->output_channels, 1, 1)); } output_size.N = batch_size, output_size.C = C_out, output_size.H = output_size.W = 1; } void FCLayerParams::allocateSpace(curandGenerator_t curand_gen, cudnnDataType_t data_type, size_t data_type_size, float std_dev, size_t &free_bytes, bool alloc_derivative) { int wt_alloc_size = weight_matrix_size; if (wt_alloc_size % 2 != 0) wt_alloc_size += 1; checkCudaErrors(cudaMalloc(&W, wt_alloc_size * data_type_size)); checkCudaErrors(cudaMalloc(&b, C_out * data_type_size)); if (alloc_derivative) { checkCudaErrors(cudaMalloc(&dW, wt_alloc_size * data_type_size)); checkCudaErrors(cudaMalloc(&db, C_out * data_type_size)); } if (data_type == CUDNN_DATA_FLOAT) { checkCURAND(curandGenerateNormal(curand_gen, (float *)W, wt_alloc_size, 0, std_dev)); fillValue<float><<<ceil(1.0 * C_out / BW), BW>>>((float *)b, C_out, 0); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCURAND(curandGenerateNormalDouble(curand_gen, (double *)W, wt_alloc_size, 0, std_dev)); fillValue<double><<<ceil(1.0 * C_out / BW), BW>>>((double *)b, C_out, 0); } free_bytes = free_bytes - 2 * (C_in * C_out + C_out) * data_type_size; } void FCLayerParams::cnmemAllocDerivatives(size_t data_type_size, cudaStream_t stream) { checkCNMEM(cnmemMalloc(&dW, weight_matrix_size * data_type_size, stream)); checkCNMEM(cnmemMalloc(&db, C_out * data_type_size, stream)); } bool FCLayerParams::cnmemAllocDerivativesCheck(size_t data_type_size, cudaStream_t stream, size_t &max_consume, size_t free_bytes, bool &out_of_memory) { checkCNMEMSim(cnmemMalloc(&dW, weight_matrix_size * data_type_size, stream), weight_matrix_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); checkCNMEMSim(cnmemMalloc(&db, C_out * data_type_size, stream), C_out * data_type_size, max_consume, free_bytes, return false, out_of_memory); return true; } void FCLayerParams::stepParams(cublasHandle_t cublas_handle, double learning_rate) { float Salpha = -learning_rate; double Dalpha = -learning_rate; // { // float *db_h = (float *)malloc(C_out * sizeof(float)); // checkCudaErrors(cudaMemcpy(db_h, db, C_out * sizeof(float), // cudaMemcpyDeviceToHost)); // for (int i = 0; i < C_out; i++) { // std::cout << db_h[i] << ' '; // } // std::cout << "\n"; // int n; // std::cin >> n; // } if (update_rule == SGD) { if (data_type == CUDNN_DATA_FLOAT) { checkCUBLAS(cublasSaxpy(cublas_handle, weight_matrix_size, &Salpha, (float *)dW, 1, (float *)W, 1)); checkCUBLAS(cublasSaxpy(cublas_handle, C_out, &Salpha, (float *)db, 1, (float *)b, 1)); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCUBLAS(cublasDaxpy(cublas_handle, weight_matrix_size, &Dalpha, (double *)dW, 1, (double *)W, 1)); checkCUBLAS(cublasDaxpy(cublas_handle, C_out, &Dalpha, (double *)db, 1, (double *)b, 1)); } } // { // float *db_h = (float *)malloc(C_out * sizeof(float)); // checkCudaErrors(cudaMemcpy(db_h, b, C_out * sizeof(float), // cudaMemcpyDeviceToHost)); // for (int i = 0; i < C_out; i++) { // std::cout << db_h[i] << ' '; // } // std::cout << "\n"; // int n; // std::cin >> n; // } } void FCLayerParams::cnmemFreeDerivatives(cudaStream_t stream) { checkCNMEM(cnmemFree(dW, stream)); checkCNMEM(cnmemFree(db, stream)); } void DropoutLayerParams::initializeValues(cudnnHandle_t cudnn_handle, DropoutDescriptor *user_params, cudnnDataType_t data_type, int batch_size, cudnnTensorFormat_t tensor_format, LayerDimension &output_size) { checkCUDNN(cudnnCreateDropoutDescriptor(&dropout_desc)); checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); checkCUDNN(cudnnDropoutGetStatesSize(cudnn_handle, &state_size)); checkCUDNN( cudnnDropoutGetReserveSpaceSize(input_tensor, &reserved_space_size)); output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void DropoutLayerParams::allocateSpace(size_t &free_bytes, cudnnHandle_t cudnn_handle, DropoutDescriptor *user_params, long long seed) { checkCudaErrors(cudaMalloc(&state, state_size)); checkCudaErrors(cudaMalloc(&reserved_space, reserved_space_size)); checkCUDNN(cudnnSetDropoutDescriptor(dropout_desc, cudnn_handle, user_params->dropout_value, state, state_size, seed)); free_bytes = free_bytes - (state_size + reserved_space_size); } void BatchNormLayerParams::initializeValues(BatchNormDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size, UpdateRule update_rule) { checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&sbmv_desc)); c = user_params->channels, h = user_params->h, w = user_params->w; if (user_params->mode == BATCHNORM_PER_ACTIVATION) { mode = CUDNN_BATCHNORM_PER_ACTIVATION; checkCUDNN(cudnnSetTensor4dDescriptor(sbmv_desc, tensor_format, data_type, 1, user_params->channels, user_params->h, user_params->w)); sbmv_size = c * h * w; } else if (user_params->mode == BATCHNORM_SPATIAL) { mode = CUDNN_BATCHNORM_SPATIAL; checkCUDNN(cudnnSetTensor4dDescriptor(sbmv_desc, tensor_format, data_type, 1, user_params->channels, 1, 1)); sbmv_size = c; } checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); factor = user_params->factor; epsilon = user_params->epsilon; this->update_rule = update_rule; this->data_type = data_type; if (mode == CUDNN_BATCHNORM_PER_ACTIVATION) allocation_size = c * h * w; else allocation_size = c; output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void BatchNormLayerParams::allocateSpace(cudnnDataType_t data_type, size_t data_type_size, size_t &free_bytes, bool alloc_derivative) { size_t allocation_size_bytes = allocation_size * data_type_size; checkCudaErrors(cudaMalloc(&scale, allocation_size_bytes)); checkCudaErrors(cudaMalloc(&bias, allocation_size_bytes)); if (alloc_derivative) { checkCudaErrors(cudaMalloc(&dscale, allocation_size_bytes)); checkCudaErrors(cudaMalloc(&dbias, allocation_size_bytes)); } checkCudaErrors(cudaMalloc(&running_mean, allocation_size_bytes)); checkCudaErrors(cudaMalloc(&running_variance, allocation_size_bytes)); checkCudaErrors(cudaMalloc(&result_save_mean, allocation_size_bytes)); checkCudaErrors(cudaMalloc(&result_save_inv_var, allocation_size_bytes)); if (data_type == CUDNN_DATA_FLOAT) { fillValue<float><<<ceil(1.0 * allocation_size / BW), BW>>>( (float *)scale, allocation_size, 1); fillValue<float><<<ceil(1.0 * allocation_size / BW), BW>>>( (float *)bias, allocation_size, 1); } else if (data_type == CUDNN_DATA_DOUBLE) { fillValue<double><<<ceil(1.0 * allocation_size / BW), BW>>>( (double *)scale, allocation_size, 1); fillValue<double><<<ceil(1.0 * allocation_size / BW), BW>>>( (double *)bias, allocation_size, 1); } free_bytes = free_bytes - 6 * allocation_size_bytes; } void BatchNormLayerParams::cnmemAllocDerivatives(size_t data_type_size, cudaStream_t stream) { checkCNMEM(cnmemMalloc(&dscale, allocation_size * data_type_size, stream)); checkCNMEM(cnmemMalloc(&dbias, allocation_size * data_type_size, stream)); } bool BatchNormLayerParams::cnmemAllocDerivativesCheck(size_t data_type_size, cudaStream_t stream, size_t &max_consume, size_t free_bytes, bool &out_of_memory) { checkCNMEMSim(cnmemMalloc(&dscale, allocation_size * data_type_size, stream), allocation_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); checkCNMEMSim(cnmemMalloc(&dbias, allocation_size * data_type_size, stream), allocation_size * data_type_size, max_consume, free_bytes, return false, out_of_memory); return true; } void BatchNormLayerParams::stepParams(cublasHandle_t cublas_handle, double learning_rate) { float Salpha = -learning_rate; double Dalpha = -learning_rate; if (update_rule == SGD) { if (data_type == CUDNN_DATA_FLOAT) { checkCUBLAS(cublasSaxpy(cublas_handle, sbmv_size, &Salpha, (float *)dscale, 1, (float *)scale, 1)); checkCUBLAS(cublasSaxpy(cublas_handle, sbmv_size, &Salpha, (float *)dbias, 1, (float *)bias, 1)); } else if (data_type == CUDNN_DATA_DOUBLE) { checkCUBLAS(cublasDaxpy(cublas_handle, sbmv_size, &Dalpha, (double *)dscale, 1, (double *)scale, 1)); checkCUBLAS(cublasDaxpy(cublas_handle, sbmv_size, &Dalpha, (double *)dbias, 1, (double *)bias, 1)); } } } void BatchNormLayerParams::cnmemFreeDerivatives(cudaStream_t stream) { checkCNMEM(cnmemFree(dscale, stream)); checkCNMEM(cnmemFree(dbias, stream)); } void PoolingLayerParams::initializeValues(PoolingDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size) { checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnCreateTensorDescriptor(&output_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor( input_tensor, tensor_format, data_type, batch_size, user_params->input_channels, user_params->input_h, user_params->input_w)); checkCUDNN(cudnnCreatePoolingDescriptor(&pool_desc)); cudnnPoolingMode_t mode; if (user_params->mode == POOLING_MAX) mode = CUDNN_POOLING_MAX; else if (user_params->mode == POOLING_AVERAGE_COUNT_INCLUDE_PADDING) mode = CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING; else if (user_params->mode == POOLING_AVERAGE_COUNT_EXCLUDE_PADDING) mode = CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING; checkCUDNN(cudnnSetPooling2dDescriptor( pool_desc, mode, CUDNN_PROPAGATE_NAN, user_params->kernel_h, user_params->kernel_w, user_params->pad_h, user_params->pad_w, user_params->stride_y, user_params->stride_x)); int output_batch_size, output_channels, output_h, output_w; checkCUDNN(cudnnGetPooling2dForwardOutputDim( pool_desc, input_tensor, &output_batch_size, &output_channels, &output_h, &output_w)); checkCUDNN(cudnnSetTensor4dDescriptor(output_tensor, tensor_format, data_type, output_batch_size, output_channels, output_h, output_w)); output_size.N = output_batch_size, output_size.C = output_channels, output_size.H = output_h, output_size.W = output_w; } void PoolingLayerParams::allocateSpace(size_t &free_bytes) {} void ActivationLayerParams::initializeValues(ActivationDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size) { checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); cudnnActivationMode_t mode; if (user_params->mode == SIGMOID) mode = CUDNN_ACTIVATION_SIGMOID; else if (user_params->mode == RELU) mode = CUDNN_ACTIVATION_RELU; else if (user_params->mode == TANH) mode = CUDNN_ACTIVATION_TANH; else if (user_params->mode == CLIPPED_RELU) mode = CUDNN_ACTIVATION_CLIPPED_RELU; else if (user_params->mode == ELU) mode = CUDNN_ACTIVATION_ELU; checkCUDNN(cudnnCreateActivationDescriptor(&actv_desc)); checkCUDNN(cudnnSetActivationDescriptor(actv_desc, mode, CUDNN_PROPAGATE_NAN, user_params->coef)); output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void ActivationLayerParams::allocateSpace(size_t &free_bytes) {} void SoftmaxLayerParams::initializeValues(SoftmaxDescriptor *user_params, cudnnDataType_t data_type, cudnnTensorFormat_t tensor_format, int batch_size, LayerDimension &output_size) { if (user_params->algo == SOFTMAX_FAST) algo = CUDNN_SOFTMAX_FAST; else if (user_params->algo == SOFTMAX_ACCURATE) algo = CUDNN_SOFTMAX_ACCURATE; if (user_params->mode == SOFTMAX_MODE_INSTANCE) mode = CUDNN_SOFTMAX_MODE_INSTANCE; else if (user_params->mode == SOFTMAX_MODE_CHANNEL) { mode = CUDNN_SOFTMAX_MODE_CHANNEL; } checkCUDNN(cudnnCreateTensorDescriptor(&input_tensor)); checkCUDNN(cudnnSetTensor4dDescriptor(input_tensor, tensor_format, data_type, batch_size, user_params->channels, user_params->h, user_params->w)); output_size.N = batch_size, output_size.C = user_params->channels, output_size.H = user_params->h, output_size.W = user_params->w; } void SoftmaxLayerParams::allocateSpace(size_t &free_bytes) {}
17363643b6d7bef8e42a2c58ecfd254483e29e1d.hip
// !!! This is a file automatically generated by hipify!!! #include <stdbool.h> #include <stdio.h> #include <string.h> #include <getopt.h> #include <hiprand/hiprand_kernel.h> #include <stdlib.h> #include <hip/hip_runtime.h> #include <sys/time.h> #include "ShortestPath2.cu" #include<chrono> #include<iostream> using namespace std; using namespace std::chrono; int blocks_[20][2] = {{8,8},{16,16},{24,24},{32,32},{1,64},{1,128},{1,192},{1,256},{1,320},{1,384},{1,448},{1,512},{1,576},{1,640},{1,704},{1,768},{1,832},{1,896},{1,960},{1,1024}}; int matrices_[7][2] = {{240,240},{496,496},{784,784},{1016,1016},{1232,1232},{1680,1680},{2024,2024}}; int main(int argc, char **argv) { hipSetDevice(0); char* p;int matrix_len=strtol(argv[1], &p, 10); for(int matrix_looper=0;matrix_looper<matrix_len;matrix_looper++){ for(int block_looper=0;block_looper<20;block_looper++){ int XSIZE=matrices_[matrix_looper][0],YSIZE=matrices_[matrix_looper][1],BLOCKX=blocks_[block_looper][0],BLOCKY=blocks_[block_looper][1]; float *Arr1 = NULL; hipMalloc(&Arr1, XSIZE*YSIZE); float *Arr2 = NULL; hipMalloc(&Arr2, XSIZE*YSIZE); int N = XSIZE*YSIZE; int iXSIZE= XSIZE; int iYSIZE= YSIZE; while(iXSIZE%BLOCKX!=0) { iXSIZE++; } while(iYSIZE%BLOCKY!=0) { iYSIZE++; } dim3 gridBlock(iXSIZE/BLOCKX, iYSIZE/BLOCKY); dim3 threadBlock(BLOCKX, BLOCKY); hipFree(0);hipLaunchKernelGGL(( ShortestPath2), dim3(gridBlock),dim3(threadBlock), 0, 0, Arr1,Arr2,N); hipDeviceSynchronize(); for (int loop_counter = 0; loop_counter < 10; ++loop_counter) {hipLaunchKernelGGL(( ShortestPath2), dim3(gridBlock),dim3(threadBlock), 0, 0, Arr1,Arr2,N); } auto start = steady_clock::now(); for (int loop_counter = 0; loop_counter < 1000; loop_counter++) {hipLaunchKernelGGL(( ShortestPath2), dim3(gridBlock),dim3(threadBlock), 0, 0, Arr1,Arr2,N); } auto end = steady_clock::now(); auto usecs = duration_cast<duration<float, microseconds::period> >(end - start); cout <<'['<<usecs.count()<<','<<'('<<BLOCKX<<','<<BLOCKY<<')' << ','<<'('<<XSIZE<<','<<YSIZE<<')'<<']' << endl; } }}
17363643b6d7bef8e42a2c58ecfd254483e29e1d.cu
#include <stdbool.h> #include <stdio.h> #include <string.h> #include <getopt.h> #include <curand_kernel.h> #include <stdlib.h> #include <cuda.h> #include <sys/time.h> #include "ShortestPath2.cu" #include<chrono> #include<iostream> using namespace std; using namespace std::chrono; int blocks_[20][2] = {{8,8},{16,16},{24,24},{32,32},{1,64},{1,128},{1,192},{1,256},{1,320},{1,384},{1,448},{1,512},{1,576},{1,640},{1,704},{1,768},{1,832},{1,896},{1,960},{1,1024}}; int matrices_[7][2] = {{240,240},{496,496},{784,784},{1016,1016},{1232,1232},{1680,1680},{2024,2024}}; int main(int argc, char **argv) { cudaSetDevice(0); char* p;int matrix_len=strtol(argv[1], &p, 10); for(int matrix_looper=0;matrix_looper<matrix_len;matrix_looper++){ for(int block_looper=0;block_looper<20;block_looper++){ int XSIZE=matrices_[matrix_looper][0],YSIZE=matrices_[matrix_looper][1],BLOCKX=blocks_[block_looper][0],BLOCKY=blocks_[block_looper][1]; float *Arr1 = NULL; cudaMalloc(&Arr1, XSIZE*YSIZE); float *Arr2 = NULL; cudaMalloc(&Arr2, XSIZE*YSIZE); int N = XSIZE*YSIZE; int iXSIZE= XSIZE; int iYSIZE= YSIZE; while(iXSIZE%BLOCKX!=0) { iXSIZE++; } while(iYSIZE%BLOCKY!=0) { iYSIZE++; } dim3 gridBlock(iXSIZE/BLOCKX, iYSIZE/BLOCKY); dim3 threadBlock(BLOCKX, BLOCKY); cudaFree(0); ShortestPath2<<<gridBlock,threadBlock>>>(Arr1,Arr2,N); cudaDeviceSynchronize(); for (int loop_counter = 0; loop_counter < 10; ++loop_counter) { ShortestPath2<<<gridBlock,threadBlock>>>(Arr1,Arr2,N); } auto start = steady_clock::now(); for (int loop_counter = 0; loop_counter < 1000; loop_counter++) { ShortestPath2<<<gridBlock,threadBlock>>>(Arr1,Arr2,N); } auto end = steady_clock::now(); auto usecs = duration_cast<duration<float, microseconds::period> >(end - start); cout <<'['<<usecs.count()<<','<<'('<<BLOCKX<<','<<BLOCKY<<')' << ','<<'('<<XSIZE<<','<<YSIZE<<')'<<']' << endl; } }}
f8b7ff120e5d20d651907fe59315a7cda837492e.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include<math.h> #include"Boys_gpu.h" #define PI 3.1415926535897932 #define P25 17.4934183276248620 #define NTHREAD 64 texture<int2,1,hipReadModeElementType> tex_P; texture<int2,1,hipReadModeElementType> tex_Zta; texture<int2,1,hipReadModeElementType> tex_pp; texture<float,1,hipReadModeElementType> tex_K2_p; texture<int2,1,hipReadModeElementType> tex_PA; texture<int2,1,hipReadModeElementType> tex_PB; texture<unsigned int,1,hipReadModeElementType> tex_id_bra; texture<int2,1,hipReadModeElementType> tex_Q; texture<int2,1,hipReadModeElementType> tex_Eta; texture<int2,1,hipReadModeElementType> tex_pq; texture<float,1,hipReadModeElementType> tex_K2_q; texture<int2,1,hipReadModeElementType> tex_QC; texture<int2,1,hipReadModeElementType> tex_QD; texture<unsigned int,1,hipReadModeElementType> tex_id_ket; void MD_texture_binding_bra_sd(double * P_d,double * PA_d,double * PB_d,\ double * alphaP_d,double * pp_d,float * K2_p_d,unsigned int * id_bra_d,\ unsigned int primit_len){ hipBindTexture(0, tex_P, P_d, sizeof(double)*primit_len*3); hipBindTexture(0, tex_Zta, alphaP_d, sizeof(double)*primit_len); hipBindTexture(0, tex_pp, pp_d, sizeof(double)*primit_len); hipBindTexture(0, tex_K2_p, K2_p_d, sizeof(float)*primit_len); hipBindTexture(0, tex_PA, PA_d, sizeof(double)*primit_len*3); hipBindTexture(0, tex_PB, PB_d, sizeof(double)*primit_len*3); hipBindTexture(0, tex_id_bra, id_bra_d, sizeof(unsigned int)*primit_len); } void MD_texture_unbind_bra_sd(){ hipUnbindTexture(tex_P); hipUnbindTexture(tex_Zta); hipUnbindTexture(tex_pp); hipUnbindTexture(tex_K2_p); hipUnbindTexture(tex_PA); hipUnbindTexture(tex_PB); hipUnbindTexture(tex_id_bra); } void MD_texture_binding_ket_sd(double * Q_d,double * QC_d,double * QD_d,\ double * alphaQ_d,double * pq_d,float * K2_q_d,unsigned int * id_ket_d,\ unsigned int primit_len){ hipBindTexture(0, tex_Q, Q_d, sizeof(double)*primit_len*3); hipBindTexture(0, tex_Eta, alphaQ_d, sizeof(double)*primit_len); hipBindTexture(0, tex_pq, pq_d, sizeof(double)*primit_len); hipBindTexture(0, tex_K2_q, K2_q_d, sizeof(float)*primit_len); hipBindTexture(0, tex_QC, QC_d, sizeof(double)*primit_len*3); hipBindTexture(0, tex_QD, QD_d, sizeof(double)*primit_len*3); hipBindTexture(0, tex_id_ket, id_ket_d, sizeof(unsigned int)*primit_len); } void MD_texture_unbind_ket_sd(){ hipUnbindTexture(tex_Q); hipUnbindTexture(tex_Eta); hipUnbindTexture(tex_pq); hipUnbindTexture(tex_K2_q); hipUnbindTexture(tex_QC); hipUnbindTexture(tex_QD); hipUnbindTexture(tex_id_ket); } __global__ void MD_Kp_sdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*1]; for(int i=0;i<1;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; if(i_contrc_bra>j_contrc_ket){ if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=0.0; } } continue; } for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=2*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[5]; Ft_fs_4(4,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[4]; double R_200[3]; double R_300[2]; double R_400[1]; double R_010[4]; double R_110[3]; double R_210[2]; double R_310[1]; double R_020[3]; double R_120[2]; double R_220[1]; double R_030[2]; double R_130[1]; double R_040[1]; double R_001[4]; double R_101[3]; double R_201[2]; double R_301[1]; double R_011[3]; double R_111[2]; double R_211[1]; double R_021[2]; double R_121[1]; double R_031[1]; double R_002[3]; double R_102[2]; double R_202[1]; double R_012[2]; double R_112[1]; double R_022[1]; double R_003[2]; double R_103[1]; double R_013[1]; double R_004[1]; for(int i=0;i<4;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<4;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<4;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<3;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<3;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<3;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<3;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<2;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<2;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<2;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<2;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<2;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<2;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<2;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<2;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<2;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<2;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<1;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<1;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<1;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<1;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<1;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<1;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<1;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<1;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<1;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<1;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<1;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<1;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<1;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } double P_002000000=Pd_002[0]; double P_102000000=Pd_102[0]; double P_202000000=Pd_202[0]; double P_001001000=Pd_001[0]*Pd_001[1]; double P_001101000=Pd_001[0]*Pd_101[1]; double P_101001000=Pd_101[0]*Pd_001[1]; double P_101101000=Pd_101[0]*Pd_101[1]; double P_000002000=Pd_002[1]; double P_000102000=Pd_102[1]; double P_000202000=Pd_202[1]; double P_001000001=Pd_001[0]*Pd_001[2]; double P_001000101=Pd_001[0]*Pd_101[2]; double P_101000001=Pd_101[0]*Pd_001[2]; double P_101000101=Pd_101[0]*Pd_101[2]; double P_000001001=Pd_001[1]*Pd_001[2]; double P_000001101=Pd_001[1]*Pd_101[2]; double P_000101001=Pd_101[1]*Pd_001[2]; double P_000101101=Pd_101[1]*Pd_101[2]; double P_000000002=Pd_002[2]; double P_000000102=Pd_102[2]; double P_000000202=Pd_202[2]; double PR_002000000000=P_002000000*R_000[0]+-1*P_102000000*R_100[0]+P_202000000*R_200[0]; double PR_001001000000=P_001001000*R_000[0]+-1*P_001101000*R_010[0]+-1*P_101001000*R_100[0]+P_101101000*R_110[0]; double PR_000002000000=P_000002000*R_000[0]+-1*P_000102000*R_010[0]+P_000202000*R_020[0]; double PR_001000001000=P_001000001*R_000[0]+-1*P_001000101*R_001[0]+-1*P_101000001*R_100[0]+P_101000101*R_101[0]; double PR_000001001000=P_000001001*R_000[0]+-1*P_000001101*R_001[0]+-1*P_000101001*R_010[0]+P_000101101*R_011[0]; double PR_000000002000=P_000000002*R_000[0]+-1*P_000000102*R_001[0]+P_000000202*R_002[0]; double PR_002000000001=P_002000000*R_001[0]+-1*P_102000000*R_101[0]+P_202000000*R_201[0]; double PR_001001000001=P_001001000*R_001[0]+-1*P_001101000*R_011[0]+-1*P_101001000*R_101[0]+P_101101000*R_111[0]; double PR_000002000001=P_000002000*R_001[0]+-1*P_000102000*R_011[0]+P_000202000*R_021[0]; double PR_001000001001=P_001000001*R_001[0]+-1*P_001000101*R_002[0]+-1*P_101000001*R_101[0]+P_101000101*R_102[0]; double PR_000001001001=P_000001001*R_001[0]+-1*P_000001101*R_002[0]+-1*P_000101001*R_011[0]+P_000101101*R_012[0]; double PR_000000002001=P_000000002*R_001[0]+-1*P_000000102*R_002[0]+P_000000202*R_003[0]; double PR_002000000010=P_002000000*R_010[0]+-1*P_102000000*R_110[0]+P_202000000*R_210[0]; double PR_001001000010=P_001001000*R_010[0]+-1*P_001101000*R_020[0]+-1*P_101001000*R_110[0]+P_101101000*R_120[0]; double PR_000002000010=P_000002000*R_010[0]+-1*P_000102000*R_020[0]+P_000202000*R_030[0]; double PR_001000001010=P_001000001*R_010[0]+-1*P_001000101*R_011[0]+-1*P_101000001*R_110[0]+P_101000101*R_111[0]; double PR_000001001010=P_000001001*R_010[0]+-1*P_000001101*R_011[0]+-1*P_000101001*R_020[0]+P_000101101*R_021[0]; double PR_000000002010=P_000000002*R_010[0]+-1*P_000000102*R_011[0]+P_000000202*R_012[0]; double PR_002000000100=P_002000000*R_100[0]+-1*P_102000000*R_200[0]+P_202000000*R_300[0]; double PR_001001000100=P_001001000*R_100[0]+-1*P_001101000*R_110[0]+-1*P_101001000*R_200[0]+P_101101000*R_210[0]; double PR_000002000100=P_000002000*R_100[0]+-1*P_000102000*R_110[0]+P_000202000*R_120[0]; double PR_001000001100=P_001000001*R_100[0]+-1*P_001000101*R_101[0]+-1*P_101000001*R_200[0]+P_101000101*R_201[0]; double PR_000001001100=P_000001001*R_100[0]+-1*P_000001101*R_101[0]+-1*P_000101001*R_110[0]+P_000101101*R_111[0]; double PR_000000002100=P_000000002*R_100[0]+-1*P_000000102*R_101[0]+P_000000202*R_102[0]; double PR_002000000002=P_002000000*R_002[0]+-1*P_102000000*R_102[0]+P_202000000*R_202[0]; double PR_001001000002=P_001001000*R_002[0]+-1*P_001101000*R_012[0]+-1*P_101001000*R_102[0]+P_101101000*R_112[0]; double PR_000002000002=P_000002000*R_002[0]+-1*P_000102000*R_012[0]+P_000202000*R_022[0]; double PR_001000001002=P_001000001*R_002[0]+-1*P_001000101*R_003[0]+-1*P_101000001*R_102[0]+P_101000101*R_103[0]; double PR_000001001002=P_000001001*R_002[0]+-1*P_000001101*R_003[0]+-1*P_000101001*R_012[0]+P_000101101*R_013[0]; double PR_000000002002=P_000000002*R_002[0]+-1*P_000000102*R_003[0]+P_000000202*R_004[0]; double PR_002000000011=P_002000000*R_011[0]+-1*P_102000000*R_111[0]+P_202000000*R_211[0]; double PR_001001000011=P_001001000*R_011[0]+-1*P_001101000*R_021[0]+-1*P_101001000*R_111[0]+P_101101000*R_121[0]; double PR_000002000011=P_000002000*R_011[0]+-1*P_000102000*R_021[0]+P_000202000*R_031[0]; double PR_001000001011=P_001000001*R_011[0]+-1*P_001000101*R_012[0]+-1*P_101000001*R_111[0]+P_101000101*R_112[0]; double PR_000001001011=P_000001001*R_011[0]+-1*P_000001101*R_012[0]+-1*P_000101001*R_021[0]+P_000101101*R_022[0]; double PR_000000002011=P_000000002*R_011[0]+-1*P_000000102*R_012[0]+P_000000202*R_013[0]; double PR_002000000020=P_002000000*R_020[0]+-1*P_102000000*R_120[0]+P_202000000*R_220[0]; double PR_001001000020=P_001001000*R_020[0]+-1*P_001101000*R_030[0]+-1*P_101001000*R_120[0]+P_101101000*R_130[0]; double PR_000002000020=P_000002000*R_020[0]+-1*P_000102000*R_030[0]+P_000202000*R_040[0]; double PR_001000001020=P_001000001*R_020[0]+-1*P_001000101*R_021[0]+-1*P_101000001*R_120[0]+P_101000101*R_121[0]; double PR_000001001020=P_000001001*R_020[0]+-1*P_000001101*R_021[0]+-1*P_000101001*R_030[0]+P_000101101*R_031[0]; double PR_000000002020=P_000000002*R_020[0]+-1*P_000000102*R_021[0]+P_000000202*R_022[0]; double PR_002000000101=P_002000000*R_101[0]+-1*P_102000000*R_201[0]+P_202000000*R_301[0]; double PR_001001000101=P_001001000*R_101[0]+-1*P_001101000*R_111[0]+-1*P_101001000*R_201[0]+P_101101000*R_211[0]; double PR_000002000101=P_000002000*R_101[0]+-1*P_000102000*R_111[0]+P_000202000*R_121[0]; double PR_001000001101=P_001000001*R_101[0]+-1*P_001000101*R_102[0]+-1*P_101000001*R_201[0]+P_101000101*R_202[0]; double PR_000001001101=P_000001001*R_101[0]+-1*P_000001101*R_102[0]+-1*P_000101001*R_111[0]+P_000101101*R_112[0]; double PR_000000002101=P_000000002*R_101[0]+-1*P_000000102*R_102[0]+P_000000202*R_103[0]; double PR_002000000110=P_002000000*R_110[0]+-1*P_102000000*R_210[0]+P_202000000*R_310[0]; double PR_001001000110=P_001001000*R_110[0]+-1*P_001101000*R_120[0]+-1*P_101001000*R_210[0]+P_101101000*R_220[0]; double PR_000002000110=P_000002000*R_110[0]+-1*P_000102000*R_120[0]+P_000202000*R_130[0]; double PR_001000001110=P_001000001*R_110[0]+-1*P_001000101*R_111[0]+-1*P_101000001*R_210[0]+P_101000101*R_211[0]; double PR_000001001110=P_000001001*R_110[0]+-1*P_000001101*R_111[0]+-1*P_000101001*R_120[0]+P_000101101*R_121[0]; double PR_000000002110=P_000000002*R_110[0]+-1*P_000000102*R_111[0]+P_000000202*R_112[0]; double PR_002000000200=P_002000000*R_200[0]+-1*P_102000000*R_300[0]+P_202000000*R_400[0]; double PR_001001000200=P_001001000*R_200[0]+-1*P_001101000*R_210[0]+-1*P_101001000*R_300[0]+P_101101000*R_310[0]; double PR_000002000200=P_000002000*R_200[0]+-1*P_000102000*R_210[0]+P_000202000*R_220[0]; double PR_001000001200=P_001000001*R_200[0]+-1*P_001000101*R_201[0]+-1*P_101000001*R_300[0]+P_101000101*R_301[0]; double PR_000001001200=P_000001001*R_200[0]+-1*P_000001101*R_201[0]+-1*P_000101001*R_210[0]+P_000101101*R_211[0]; double PR_000000002200=P_000000002*R_200[0]+-1*P_000000102*R_201[0]+P_000000202*R_202[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*1+0]+=Pmtrx[0]*(Q_002000000*PR_002000000000+Q_102000000*PR_002000000100+Q_202000000*PR_002000000200); ans_temp[ans_id*1+0]+=Pmtrx[1]*(Q_001001000*PR_002000000000+Q_001101000*PR_002000000010+Q_101001000*PR_002000000100+Q_101101000*PR_002000000110); ans_temp[ans_id*1+0]+=Pmtrx[2]*(Q_000002000*PR_002000000000+Q_000102000*PR_002000000010+Q_000202000*PR_002000000020); ans_temp[ans_id*1+0]+=Pmtrx[3]*(Q_001000001*PR_002000000000+Q_001000101*PR_002000000001+Q_101000001*PR_002000000100+Q_101000101*PR_002000000101); ans_temp[ans_id*1+0]+=Pmtrx[4]*(Q_000001001*PR_002000000000+Q_000001101*PR_002000000001+Q_000101001*PR_002000000010+Q_000101101*PR_002000000011); ans_temp[ans_id*1+0]+=Pmtrx[5]*(Q_000000002*PR_002000000000+Q_000000102*PR_002000000001+Q_000000202*PR_002000000002); ans_temp[ans_id*1+0]+=Pmtrx[6]*(Q_002000000*PR_001001000000+Q_102000000*PR_001001000100+Q_202000000*PR_001001000200); ans_temp[ans_id*1+0]+=Pmtrx[7]*(Q_001001000*PR_001001000000+Q_001101000*PR_001001000010+Q_101001000*PR_001001000100+Q_101101000*PR_001001000110); ans_temp[ans_id*1+0]+=Pmtrx[8]*(Q_000002000*PR_001001000000+Q_000102000*PR_001001000010+Q_000202000*PR_001001000020); ans_temp[ans_id*1+0]+=Pmtrx[9]*(Q_001000001*PR_001001000000+Q_001000101*PR_001001000001+Q_101000001*PR_001001000100+Q_101000101*PR_001001000101); ans_temp[ans_id*1+0]+=Pmtrx[10]*(Q_000001001*PR_001001000000+Q_000001101*PR_001001000001+Q_000101001*PR_001001000010+Q_000101101*PR_001001000011); ans_temp[ans_id*1+0]+=Pmtrx[11]*(Q_000000002*PR_001001000000+Q_000000102*PR_001001000001+Q_000000202*PR_001001000002); ans_temp[ans_id*1+0]+=Pmtrx[12]*(Q_002000000*PR_000002000000+Q_102000000*PR_000002000100+Q_202000000*PR_000002000200); ans_temp[ans_id*1+0]+=Pmtrx[13]*(Q_001001000*PR_000002000000+Q_001101000*PR_000002000010+Q_101001000*PR_000002000100+Q_101101000*PR_000002000110); ans_temp[ans_id*1+0]+=Pmtrx[14]*(Q_000002000*PR_000002000000+Q_000102000*PR_000002000010+Q_000202000*PR_000002000020); ans_temp[ans_id*1+0]+=Pmtrx[15]*(Q_001000001*PR_000002000000+Q_001000101*PR_000002000001+Q_101000001*PR_000002000100+Q_101000101*PR_000002000101); ans_temp[ans_id*1+0]+=Pmtrx[16]*(Q_000001001*PR_000002000000+Q_000001101*PR_000002000001+Q_000101001*PR_000002000010+Q_000101101*PR_000002000011); ans_temp[ans_id*1+0]+=Pmtrx[17]*(Q_000000002*PR_000002000000+Q_000000102*PR_000002000001+Q_000000202*PR_000002000002); ans_temp[ans_id*1+0]+=Pmtrx[18]*(Q_002000000*PR_001000001000+Q_102000000*PR_001000001100+Q_202000000*PR_001000001200); ans_temp[ans_id*1+0]+=Pmtrx[19]*(Q_001001000*PR_001000001000+Q_001101000*PR_001000001010+Q_101001000*PR_001000001100+Q_101101000*PR_001000001110); ans_temp[ans_id*1+0]+=Pmtrx[20]*(Q_000002000*PR_001000001000+Q_000102000*PR_001000001010+Q_000202000*PR_001000001020); ans_temp[ans_id*1+0]+=Pmtrx[21]*(Q_001000001*PR_001000001000+Q_001000101*PR_001000001001+Q_101000001*PR_001000001100+Q_101000101*PR_001000001101); ans_temp[ans_id*1+0]+=Pmtrx[22]*(Q_000001001*PR_001000001000+Q_000001101*PR_001000001001+Q_000101001*PR_001000001010+Q_000101101*PR_001000001011); ans_temp[ans_id*1+0]+=Pmtrx[23]*(Q_000000002*PR_001000001000+Q_000000102*PR_001000001001+Q_000000202*PR_001000001002); ans_temp[ans_id*1+0]+=Pmtrx[24]*(Q_002000000*PR_000001001000+Q_102000000*PR_000001001100+Q_202000000*PR_000001001200); ans_temp[ans_id*1+0]+=Pmtrx[25]*(Q_001001000*PR_000001001000+Q_001101000*PR_000001001010+Q_101001000*PR_000001001100+Q_101101000*PR_000001001110); ans_temp[ans_id*1+0]+=Pmtrx[26]*(Q_000002000*PR_000001001000+Q_000102000*PR_000001001010+Q_000202000*PR_000001001020); ans_temp[ans_id*1+0]+=Pmtrx[27]*(Q_001000001*PR_000001001000+Q_001000101*PR_000001001001+Q_101000001*PR_000001001100+Q_101000101*PR_000001001101); ans_temp[ans_id*1+0]+=Pmtrx[28]*(Q_000001001*PR_000001001000+Q_000001101*PR_000001001001+Q_000101001*PR_000001001010+Q_000101101*PR_000001001011); ans_temp[ans_id*1+0]+=Pmtrx[29]*(Q_000000002*PR_000001001000+Q_000000102*PR_000001001001+Q_000000202*PR_000001001002); ans_temp[ans_id*1+0]+=Pmtrx[30]*(Q_002000000*PR_000000002000+Q_102000000*PR_000000002100+Q_202000000*PR_000000002200); ans_temp[ans_id*1+0]+=Pmtrx[31]*(Q_001001000*PR_000000002000+Q_001101000*PR_000000002010+Q_101001000*PR_000000002100+Q_101101000*PR_000000002110); ans_temp[ans_id*1+0]+=Pmtrx[32]*(Q_000002000*PR_000000002000+Q_000102000*PR_000000002010+Q_000202000*PR_000000002020); ans_temp[ans_id*1+0]+=Pmtrx[33]*(Q_001000001*PR_000000002000+Q_001000101*PR_000000002001+Q_101000001*PR_000000002100+Q_101000101*PR_000000002101); ans_temp[ans_id*1+0]+=Pmtrx[34]*(Q_000001001*PR_000000002000+Q_000001101*PR_000000002001+Q_000101001*PR_000000002010+Q_000101101*PR_000000002011); ans_temp[ans_id*1+0]+=Pmtrx[35]*(Q_000000002*PR_000000002000+Q_000000102*PR_000000002001+Q_000000202*PR_000000002002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<1;ians++){ ans_temp[tId_x*1+ians]+=ans_temp[(tId_x+num_thread)*1+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=ans_temp[(tId_x)*1+ians]; } } } } } __global__ void MD_Kq_sdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*1]; for(int i=0;i<1;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; if(i_contrc_bra>j_contrc_ket){ if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=0.0; } } continue; } for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=2*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[5]; Ft_fs_4(4,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[4]; double R_200[3]; double R_300[2]; double R_400[1]; double R_010[4]; double R_110[3]; double R_210[2]; double R_310[1]; double R_020[3]; double R_120[2]; double R_220[1]; double R_030[2]; double R_130[1]; double R_040[1]; double R_001[4]; double R_101[3]; double R_201[2]; double R_301[1]; double R_011[3]; double R_111[2]; double R_211[1]; double R_021[2]; double R_121[1]; double R_031[1]; double R_002[3]; double R_102[2]; double R_202[1]; double R_012[2]; double R_112[1]; double R_022[1]; double R_003[2]; double R_103[1]; double R_013[1]; double R_004[1]; for(int i=0;i<4;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<4;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<4;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<3;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<3;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<3;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<3;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<2;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<2;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<2;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<2;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<2;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<2;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<2;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<2;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<2;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<2;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<1;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<1;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<1;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<1;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<1;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<1;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<1;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<1;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<1;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<1;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<1;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<1;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<1;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } double P_002000000=Pd_002[0]; double P_102000000=Pd_102[0]; double P_202000000=Pd_202[0]; double P_001001000=Pd_001[0]*Pd_001[1]; double P_001101000=Pd_001[0]*Pd_101[1]; double P_101001000=Pd_101[0]*Pd_001[1]; double P_101101000=Pd_101[0]*Pd_101[1]; double P_000002000=Pd_002[1]; double P_000102000=Pd_102[1]; double P_000202000=Pd_202[1]; double P_001000001=Pd_001[0]*Pd_001[2]; double P_001000101=Pd_001[0]*Pd_101[2]; double P_101000001=Pd_101[0]*Pd_001[2]; double P_101000101=Pd_101[0]*Pd_101[2]; double P_000001001=Pd_001[1]*Pd_001[2]; double P_000001101=Pd_001[1]*Pd_101[2]; double P_000101001=Pd_101[1]*Pd_001[2]; double P_000101101=Pd_101[1]*Pd_101[2]; double P_000000002=Pd_002[2]; double P_000000102=Pd_102[2]; double P_000000202=Pd_202[2]; ans_temp[ans_id*1+0]+=Pmtrx[0]*(P_002000000*QR_002000000000+P_102000000*QR_002000000100+P_202000000*QR_002000000200); ans_temp[ans_id*1+0]+=Pmtrx[1]*(P_002000000*QR_001001000000+P_102000000*QR_001001000100+P_202000000*QR_001001000200); ans_temp[ans_id*1+0]+=Pmtrx[2]*(P_002000000*QR_000002000000+P_102000000*QR_000002000100+P_202000000*QR_000002000200); ans_temp[ans_id*1+0]+=Pmtrx[3]*(P_002000000*QR_001000001000+P_102000000*QR_001000001100+P_202000000*QR_001000001200); ans_temp[ans_id*1+0]+=Pmtrx[4]*(P_002000000*QR_000001001000+P_102000000*QR_000001001100+P_202000000*QR_000001001200); ans_temp[ans_id*1+0]+=Pmtrx[5]*(P_002000000*QR_000000002000+P_102000000*QR_000000002100+P_202000000*QR_000000002200); ans_temp[ans_id*1+0]+=Pmtrx[6]*(P_001001000*QR_002000000000+P_001101000*QR_002000000010+P_101001000*QR_002000000100+P_101101000*QR_002000000110); ans_temp[ans_id*1+0]+=Pmtrx[7]*(P_001001000*QR_001001000000+P_001101000*QR_001001000010+P_101001000*QR_001001000100+P_101101000*QR_001001000110); ans_temp[ans_id*1+0]+=Pmtrx[8]*(P_001001000*QR_000002000000+P_001101000*QR_000002000010+P_101001000*QR_000002000100+P_101101000*QR_000002000110); ans_temp[ans_id*1+0]+=Pmtrx[9]*(P_001001000*QR_001000001000+P_001101000*QR_001000001010+P_101001000*QR_001000001100+P_101101000*QR_001000001110); ans_temp[ans_id*1+0]+=Pmtrx[10]*(P_001001000*QR_000001001000+P_001101000*QR_000001001010+P_101001000*QR_000001001100+P_101101000*QR_000001001110); ans_temp[ans_id*1+0]+=Pmtrx[11]*(P_001001000*QR_000000002000+P_001101000*QR_000000002010+P_101001000*QR_000000002100+P_101101000*QR_000000002110); ans_temp[ans_id*1+0]+=Pmtrx[12]*(P_000002000*QR_002000000000+P_000102000*QR_002000000010+P_000202000*QR_002000000020); ans_temp[ans_id*1+0]+=Pmtrx[13]*(P_000002000*QR_001001000000+P_000102000*QR_001001000010+P_000202000*QR_001001000020); ans_temp[ans_id*1+0]+=Pmtrx[14]*(P_000002000*QR_000002000000+P_000102000*QR_000002000010+P_000202000*QR_000002000020); ans_temp[ans_id*1+0]+=Pmtrx[15]*(P_000002000*QR_001000001000+P_000102000*QR_001000001010+P_000202000*QR_001000001020); ans_temp[ans_id*1+0]+=Pmtrx[16]*(P_000002000*QR_000001001000+P_000102000*QR_000001001010+P_000202000*QR_000001001020); ans_temp[ans_id*1+0]+=Pmtrx[17]*(P_000002000*QR_000000002000+P_000102000*QR_000000002010+P_000202000*QR_000000002020); ans_temp[ans_id*1+0]+=Pmtrx[18]*(P_001000001*QR_002000000000+P_001000101*QR_002000000001+P_101000001*QR_002000000100+P_101000101*QR_002000000101); ans_temp[ans_id*1+0]+=Pmtrx[19]*(P_001000001*QR_001001000000+P_001000101*QR_001001000001+P_101000001*QR_001001000100+P_101000101*QR_001001000101); ans_temp[ans_id*1+0]+=Pmtrx[20]*(P_001000001*QR_000002000000+P_001000101*QR_000002000001+P_101000001*QR_000002000100+P_101000101*QR_000002000101); ans_temp[ans_id*1+0]+=Pmtrx[21]*(P_001000001*QR_001000001000+P_001000101*QR_001000001001+P_101000001*QR_001000001100+P_101000101*QR_001000001101); ans_temp[ans_id*1+0]+=Pmtrx[22]*(P_001000001*QR_000001001000+P_001000101*QR_000001001001+P_101000001*QR_000001001100+P_101000101*QR_000001001101); ans_temp[ans_id*1+0]+=Pmtrx[23]*(P_001000001*QR_000000002000+P_001000101*QR_000000002001+P_101000001*QR_000000002100+P_101000101*QR_000000002101); ans_temp[ans_id*1+0]+=Pmtrx[24]*(P_000001001*QR_002000000000+P_000001101*QR_002000000001+P_000101001*QR_002000000010+P_000101101*QR_002000000011); ans_temp[ans_id*1+0]+=Pmtrx[25]*(P_000001001*QR_001001000000+P_000001101*QR_001001000001+P_000101001*QR_001001000010+P_000101101*QR_001001000011); ans_temp[ans_id*1+0]+=Pmtrx[26]*(P_000001001*QR_000002000000+P_000001101*QR_000002000001+P_000101001*QR_000002000010+P_000101101*QR_000002000011); ans_temp[ans_id*1+0]+=Pmtrx[27]*(P_000001001*QR_001000001000+P_000001101*QR_001000001001+P_000101001*QR_001000001010+P_000101101*QR_001000001011); ans_temp[ans_id*1+0]+=Pmtrx[28]*(P_000001001*QR_000001001000+P_000001101*QR_000001001001+P_000101001*QR_000001001010+P_000101101*QR_000001001011); ans_temp[ans_id*1+0]+=Pmtrx[29]*(P_000001001*QR_000000002000+P_000001101*QR_000000002001+P_000101001*QR_000000002010+P_000101101*QR_000000002011); ans_temp[ans_id*1+0]+=Pmtrx[30]*(P_000000002*QR_002000000000+P_000000102*QR_002000000001+P_000000202*QR_002000000002); ans_temp[ans_id*1+0]+=Pmtrx[31]*(P_000000002*QR_001001000000+P_000000102*QR_001001000001+P_000000202*QR_001001000002); ans_temp[ans_id*1+0]+=Pmtrx[32]*(P_000000002*QR_000002000000+P_000000102*QR_000002000001+P_000000202*QR_000002000002); ans_temp[ans_id*1+0]+=Pmtrx[33]*(P_000000002*QR_001000001000+P_000000102*QR_001000001001+P_000000202*QR_001000001002); ans_temp[ans_id*1+0]+=Pmtrx[34]*(P_000000002*QR_000001001000+P_000000102*QR_000001001001+P_000000202*QR_000001001002); ans_temp[ans_id*1+0]+=Pmtrx[35]*(P_000000002*QR_000000002000+P_000000102*QR_000000002001+P_000000202*QR_000000002002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<1;ians++){ ans_temp[tId_x*1+ians]+=ans_temp[(tId_x+num_thread)*1+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=ans_temp[(tId_x)*1+ians]; } } } } } __global__ void MD_Kp_pdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*3]; for(int i=0;i<3;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_010[3]; temp_int2=tex1Dfetch(tex_PA,jj*3+0); Pd_010[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+1); Pd_010[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+2); Pd_010[2]=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } double P_012000000=Pd_012[0]; double P_112000000=Pd_112[0]; double P_212000000=Pd_212[0]; double P_312000000=Pd_312[0]; double P_011001000=Pd_011[0]*Pd_001[1]; double P_011101000=Pd_011[0]*Pd_101[1]; double P_111001000=Pd_111[0]*Pd_001[1]; double P_111101000=Pd_111[0]*Pd_101[1]; double P_211001000=Pd_211[0]*Pd_001[1]; double P_211101000=Pd_211[0]*Pd_101[1]; double P_010002000=Pd_010[0]*Pd_002[1]; double P_010102000=Pd_010[0]*Pd_102[1]; double P_010202000=Pd_010[0]*Pd_202[1]; double P_110002000=Pd_110[0]*Pd_002[1]; double P_110102000=Pd_110[0]*Pd_102[1]; double P_110202000=Pd_110[0]*Pd_202[1]; double P_011000001=Pd_011[0]*Pd_001[2]; double P_011000101=Pd_011[0]*Pd_101[2]; double P_111000001=Pd_111[0]*Pd_001[2]; double P_111000101=Pd_111[0]*Pd_101[2]; double P_211000001=Pd_211[0]*Pd_001[2]; double P_211000101=Pd_211[0]*Pd_101[2]; double P_010001001=Pd_010[0]*Pd_001[1]*Pd_001[2]; double P_010001101=Pd_010[0]*Pd_001[1]*Pd_101[2]; double P_010101001=Pd_010[0]*Pd_101[1]*Pd_001[2]; double P_010101101=Pd_010[0]*Pd_101[1]*Pd_101[2]; double P_110001001=Pd_110[0]*Pd_001[1]*Pd_001[2]; double P_110001101=Pd_110[0]*Pd_001[1]*Pd_101[2]; double P_110101001=Pd_110[0]*Pd_101[1]*Pd_001[2]; double P_110101101=Pd_110[0]*Pd_101[1]*Pd_101[2]; double P_010000002=Pd_010[0]*Pd_002[2]; double P_010000102=Pd_010[0]*Pd_102[2]; double P_010000202=Pd_010[0]*Pd_202[2]; double P_110000002=Pd_110[0]*Pd_002[2]; double P_110000102=Pd_110[0]*Pd_102[2]; double P_110000202=Pd_110[0]*Pd_202[2]; double P_002010000=Pd_002[0]*Pd_010[1]; double P_002110000=Pd_002[0]*Pd_110[1]; double P_102010000=Pd_102[0]*Pd_010[1]; double P_102110000=Pd_102[0]*Pd_110[1]; double P_202010000=Pd_202[0]*Pd_010[1]; double P_202110000=Pd_202[0]*Pd_110[1]; double P_001011000=Pd_001[0]*Pd_011[1]; double P_001111000=Pd_001[0]*Pd_111[1]; double P_001211000=Pd_001[0]*Pd_211[1]; double P_101011000=Pd_101[0]*Pd_011[1]; double P_101111000=Pd_101[0]*Pd_111[1]; double P_101211000=Pd_101[0]*Pd_211[1]; double P_000012000=Pd_012[1]; double P_000112000=Pd_112[1]; double P_000212000=Pd_212[1]; double P_000312000=Pd_312[1]; double P_001010001=Pd_001[0]*Pd_010[1]*Pd_001[2]; double P_001010101=Pd_001[0]*Pd_010[1]*Pd_101[2]; double P_001110001=Pd_001[0]*Pd_110[1]*Pd_001[2]; double P_001110101=Pd_001[0]*Pd_110[1]*Pd_101[2]; double P_101010001=Pd_101[0]*Pd_010[1]*Pd_001[2]; double P_101010101=Pd_101[0]*Pd_010[1]*Pd_101[2]; double P_101110001=Pd_101[0]*Pd_110[1]*Pd_001[2]; double P_101110101=Pd_101[0]*Pd_110[1]*Pd_101[2]; double P_000011001=Pd_011[1]*Pd_001[2]; double P_000011101=Pd_011[1]*Pd_101[2]; double P_000111001=Pd_111[1]*Pd_001[2]; double P_000111101=Pd_111[1]*Pd_101[2]; double P_000211001=Pd_211[1]*Pd_001[2]; double P_000211101=Pd_211[1]*Pd_101[2]; double P_000010002=Pd_010[1]*Pd_002[2]; double P_000010102=Pd_010[1]*Pd_102[2]; double P_000010202=Pd_010[1]*Pd_202[2]; double P_000110002=Pd_110[1]*Pd_002[2]; double P_000110102=Pd_110[1]*Pd_102[2]; double P_000110202=Pd_110[1]*Pd_202[2]; double P_002000010=Pd_002[0]*Pd_010[2]; double P_002000110=Pd_002[0]*Pd_110[2]; double P_102000010=Pd_102[0]*Pd_010[2]; double P_102000110=Pd_102[0]*Pd_110[2]; double P_202000010=Pd_202[0]*Pd_010[2]; double P_202000110=Pd_202[0]*Pd_110[2]; double P_001001010=Pd_001[0]*Pd_001[1]*Pd_010[2]; double P_001001110=Pd_001[0]*Pd_001[1]*Pd_110[2]; double P_001101010=Pd_001[0]*Pd_101[1]*Pd_010[2]; double P_001101110=Pd_001[0]*Pd_101[1]*Pd_110[2]; double P_101001010=Pd_101[0]*Pd_001[1]*Pd_010[2]; double P_101001110=Pd_101[0]*Pd_001[1]*Pd_110[2]; double P_101101010=Pd_101[0]*Pd_101[1]*Pd_010[2]; double P_101101110=Pd_101[0]*Pd_101[1]*Pd_110[2]; double P_000002010=Pd_002[1]*Pd_010[2]; double P_000002110=Pd_002[1]*Pd_110[2]; double P_000102010=Pd_102[1]*Pd_010[2]; double P_000102110=Pd_102[1]*Pd_110[2]; double P_000202010=Pd_202[1]*Pd_010[2]; double P_000202110=Pd_202[1]*Pd_110[2]; double P_001000011=Pd_001[0]*Pd_011[2]; double P_001000111=Pd_001[0]*Pd_111[2]; double P_001000211=Pd_001[0]*Pd_211[2]; double P_101000011=Pd_101[0]*Pd_011[2]; double P_101000111=Pd_101[0]*Pd_111[2]; double P_101000211=Pd_101[0]*Pd_211[2]; double P_000001011=Pd_001[1]*Pd_011[2]; double P_000001111=Pd_001[1]*Pd_111[2]; double P_000001211=Pd_001[1]*Pd_211[2]; double P_000101011=Pd_101[1]*Pd_011[2]; double P_000101111=Pd_101[1]*Pd_111[2]; double P_000101211=Pd_101[1]*Pd_211[2]; double P_000000012=Pd_012[2]; double P_000000112=Pd_112[2]; double P_000000212=Pd_212[2]; double P_000000312=Pd_312[2]; double PR_012000000000=P_012000000*R_000[0]+-1*P_112000000*R_100[0]+P_212000000*R_200[0]+-1*P_312000000*R_300[0]; double PR_011001000000=P_011001000*R_000[0]+-1*P_011101000*R_010[0]+-1*P_111001000*R_100[0]+P_111101000*R_110[0]+P_211001000*R_200[0]+-1*P_211101000*R_210[0]; double PR_010002000000=P_010002000*R_000[0]+-1*P_010102000*R_010[0]+P_010202000*R_020[0]+-1*P_110002000*R_100[0]+P_110102000*R_110[0]+-1*P_110202000*R_120[0]; double PR_011000001000=P_011000001*R_000[0]+-1*P_011000101*R_001[0]+-1*P_111000001*R_100[0]+P_111000101*R_101[0]+P_211000001*R_200[0]+-1*P_211000101*R_201[0]; double PR_010001001000=P_010001001*R_000[0]+-1*P_010001101*R_001[0]+-1*P_010101001*R_010[0]+P_010101101*R_011[0]+-1*P_110001001*R_100[0]+P_110001101*R_101[0]+P_110101001*R_110[0]+-1*P_110101101*R_111[0]; double PR_010000002000=P_010000002*R_000[0]+-1*P_010000102*R_001[0]+P_010000202*R_002[0]+-1*P_110000002*R_100[0]+P_110000102*R_101[0]+-1*P_110000202*R_102[0]; double PR_002010000000=P_002010000*R_000[0]+-1*P_002110000*R_010[0]+-1*P_102010000*R_100[0]+P_102110000*R_110[0]+P_202010000*R_200[0]+-1*P_202110000*R_210[0]; double PR_001011000000=P_001011000*R_000[0]+-1*P_001111000*R_010[0]+P_001211000*R_020[0]+-1*P_101011000*R_100[0]+P_101111000*R_110[0]+-1*P_101211000*R_120[0]; double PR_000012000000=P_000012000*R_000[0]+-1*P_000112000*R_010[0]+P_000212000*R_020[0]+-1*P_000312000*R_030[0]; double PR_001010001000=P_001010001*R_000[0]+-1*P_001010101*R_001[0]+-1*P_001110001*R_010[0]+P_001110101*R_011[0]+-1*P_101010001*R_100[0]+P_101010101*R_101[0]+P_101110001*R_110[0]+-1*P_101110101*R_111[0]; double PR_000011001000=P_000011001*R_000[0]+-1*P_000011101*R_001[0]+-1*P_000111001*R_010[0]+P_000111101*R_011[0]+P_000211001*R_020[0]+-1*P_000211101*R_021[0]; double PR_000010002000=P_000010002*R_000[0]+-1*P_000010102*R_001[0]+P_000010202*R_002[0]+-1*P_000110002*R_010[0]+P_000110102*R_011[0]+-1*P_000110202*R_012[0]; double PR_002000010000=P_002000010*R_000[0]+-1*P_002000110*R_001[0]+-1*P_102000010*R_100[0]+P_102000110*R_101[0]+P_202000010*R_200[0]+-1*P_202000110*R_201[0]; double PR_001001010000=P_001001010*R_000[0]+-1*P_001001110*R_001[0]+-1*P_001101010*R_010[0]+P_001101110*R_011[0]+-1*P_101001010*R_100[0]+P_101001110*R_101[0]+P_101101010*R_110[0]+-1*P_101101110*R_111[0]; double PR_000002010000=P_000002010*R_000[0]+-1*P_000002110*R_001[0]+-1*P_000102010*R_010[0]+P_000102110*R_011[0]+P_000202010*R_020[0]+-1*P_000202110*R_021[0]; double PR_001000011000=P_001000011*R_000[0]+-1*P_001000111*R_001[0]+P_001000211*R_002[0]+-1*P_101000011*R_100[0]+P_101000111*R_101[0]+-1*P_101000211*R_102[0]; double PR_000001011000=P_000001011*R_000[0]+-1*P_000001111*R_001[0]+P_000001211*R_002[0]+-1*P_000101011*R_010[0]+P_000101111*R_011[0]+-1*P_000101211*R_012[0]; double PR_000000012000=P_000000012*R_000[0]+-1*P_000000112*R_001[0]+P_000000212*R_002[0]+-1*P_000000312*R_003[0]; double PR_012000000001=P_012000000*R_001[0]+-1*P_112000000*R_101[0]+P_212000000*R_201[0]+-1*P_312000000*R_301[0]; double PR_011001000001=P_011001000*R_001[0]+-1*P_011101000*R_011[0]+-1*P_111001000*R_101[0]+P_111101000*R_111[0]+P_211001000*R_201[0]+-1*P_211101000*R_211[0]; double PR_010002000001=P_010002000*R_001[0]+-1*P_010102000*R_011[0]+P_010202000*R_021[0]+-1*P_110002000*R_101[0]+P_110102000*R_111[0]+-1*P_110202000*R_121[0]; double PR_011000001001=P_011000001*R_001[0]+-1*P_011000101*R_002[0]+-1*P_111000001*R_101[0]+P_111000101*R_102[0]+P_211000001*R_201[0]+-1*P_211000101*R_202[0]; double PR_010001001001=P_010001001*R_001[0]+-1*P_010001101*R_002[0]+-1*P_010101001*R_011[0]+P_010101101*R_012[0]+-1*P_110001001*R_101[0]+P_110001101*R_102[0]+P_110101001*R_111[0]+-1*P_110101101*R_112[0]; double PR_010000002001=P_010000002*R_001[0]+-1*P_010000102*R_002[0]+P_010000202*R_003[0]+-1*P_110000002*R_101[0]+P_110000102*R_102[0]+-1*P_110000202*R_103[0]; double PR_002010000001=P_002010000*R_001[0]+-1*P_002110000*R_011[0]+-1*P_102010000*R_101[0]+P_102110000*R_111[0]+P_202010000*R_201[0]+-1*P_202110000*R_211[0]; double PR_001011000001=P_001011000*R_001[0]+-1*P_001111000*R_011[0]+P_001211000*R_021[0]+-1*P_101011000*R_101[0]+P_101111000*R_111[0]+-1*P_101211000*R_121[0]; double PR_000012000001=P_000012000*R_001[0]+-1*P_000112000*R_011[0]+P_000212000*R_021[0]+-1*P_000312000*R_031[0]; double PR_001010001001=P_001010001*R_001[0]+-1*P_001010101*R_002[0]+-1*P_001110001*R_011[0]+P_001110101*R_012[0]+-1*P_101010001*R_101[0]+P_101010101*R_102[0]+P_101110001*R_111[0]+-1*P_101110101*R_112[0]; double PR_000011001001=P_000011001*R_001[0]+-1*P_000011101*R_002[0]+-1*P_000111001*R_011[0]+P_000111101*R_012[0]+P_000211001*R_021[0]+-1*P_000211101*R_022[0]; double PR_000010002001=P_000010002*R_001[0]+-1*P_000010102*R_002[0]+P_000010202*R_003[0]+-1*P_000110002*R_011[0]+P_000110102*R_012[0]+-1*P_000110202*R_013[0]; double PR_002000010001=P_002000010*R_001[0]+-1*P_002000110*R_002[0]+-1*P_102000010*R_101[0]+P_102000110*R_102[0]+P_202000010*R_201[0]+-1*P_202000110*R_202[0]; double PR_001001010001=P_001001010*R_001[0]+-1*P_001001110*R_002[0]+-1*P_001101010*R_011[0]+P_001101110*R_012[0]+-1*P_101001010*R_101[0]+P_101001110*R_102[0]+P_101101010*R_111[0]+-1*P_101101110*R_112[0]; double PR_000002010001=P_000002010*R_001[0]+-1*P_000002110*R_002[0]+-1*P_000102010*R_011[0]+P_000102110*R_012[0]+P_000202010*R_021[0]+-1*P_000202110*R_022[0]; double PR_001000011001=P_001000011*R_001[0]+-1*P_001000111*R_002[0]+P_001000211*R_003[0]+-1*P_101000011*R_101[0]+P_101000111*R_102[0]+-1*P_101000211*R_103[0]; double PR_000001011001=P_000001011*R_001[0]+-1*P_000001111*R_002[0]+P_000001211*R_003[0]+-1*P_000101011*R_011[0]+P_000101111*R_012[0]+-1*P_000101211*R_013[0]; double PR_000000012001=P_000000012*R_001[0]+-1*P_000000112*R_002[0]+P_000000212*R_003[0]+-1*P_000000312*R_004[0]; double PR_012000000010=P_012000000*R_010[0]+-1*P_112000000*R_110[0]+P_212000000*R_210[0]+-1*P_312000000*R_310[0]; double PR_011001000010=P_011001000*R_010[0]+-1*P_011101000*R_020[0]+-1*P_111001000*R_110[0]+P_111101000*R_120[0]+P_211001000*R_210[0]+-1*P_211101000*R_220[0]; double PR_010002000010=P_010002000*R_010[0]+-1*P_010102000*R_020[0]+P_010202000*R_030[0]+-1*P_110002000*R_110[0]+P_110102000*R_120[0]+-1*P_110202000*R_130[0]; double PR_011000001010=P_011000001*R_010[0]+-1*P_011000101*R_011[0]+-1*P_111000001*R_110[0]+P_111000101*R_111[0]+P_211000001*R_210[0]+-1*P_211000101*R_211[0]; double PR_010001001010=P_010001001*R_010[0]+-1*P_010001101*R_011[0]+-1*P_010101001*R_020[0]+P_010101101*R_021[0]+-1*P_110001001*R_110[0]+P_110001101*R_111[0]+P_110101001*R_120[0]+-1*P_110101101*R_121[0]; double PR_010000002010=P_010000002*R_010[0]+-1*P_010000102*R_011[0]+P_010000202*R_012[0]+-1*P_110000002*R_110[0]+P_110000102*R_111[0]+-1*P_110000202*R_112[0]; double PR_002010000010=P_002010000*R_010[0]+-1*P_002110000*R_020[0]+-1*P_102010000*R_110[0]+P_102110000*R_120[0]+P_202010000*R_210[0]+-1*P_202110000*R_220[0]; double PR_001011000010=P_001011000*R_010[0]+-1*P_001111000*R_020[0]+P_001211000*R_030[0]+-1*P_101011000*R_110[0]+P_101111000*R_120[0]+-1*P_101211000*R_130[0]; double PR_000012000010=P_000012000*R_010[0]+-1*P_000112000*R_020[0]+P_000212000*R_030[0]+-1*P_000312000*R_040[0]; double PR_001010001010=P_001010001*R_010[0]+-1*P_001010101*R_011[0]+-1*P_001110001*R_020[0]+P_001110101*R_021[0]+-1*P_101010001*R_110[0]+P_101010101*R_111[0]+P_101110001*R_120[0]+-1*P_101110101*R_121[0]; double PR_000011001010=P_000011001*R_010[0]+-1*P_000011101*R_011[0]+-1*P_000111001*R_020[0]+P_000111101*R_021[0]+P_000211001*R_030[0]+-1*P_000211101*R_031[0]; double PR_000010002010=P_000010002*R_010[0]+-1*P_000010102*R_011[0]+P_000010202*R_012[0]+-1*P_000110002*R_020[0]+P_000110102*R_021[0]+-1*P_000110202*R_022[0]; double PR_002000010010=P_002000010*R_010[0]+-1*P_002000110*R_011[0]+-1*P_102000010*R_110[0]+P_102000110*R_111[0]+P_202000010*R_210[0]+-1*P_202000110*R_211[0]; double PR_001001010010=P_001001010*R_010[0]+-1*P_001001110*R_011[0]+-1*P_001101010*R_020[0]+P_001101110*R_021[0]+-1*P_101001010*R_110[0]+P_101001110*R_111[0]+P_101101010*R_120[0]+-1*P_101101110*R_121[0]; double PR_000002010010=P_000002010*R_010[0]+-1*P_000002110*R_011[0]+-1*P_000102010*R_020[0]+P_000102110*R_021[0]+P_000202010*R_030[0]+-1*P_000202110*R_031[0]; double PR_001000011010=P_001000011*R_010[0]+-1*P_001000111*R_011[0]+P_001000211*R_012[0]+-1*P_101000011*R_110[0]+P_101000111*R_111[0]+-1*P_101000211*R_112[0]; double PR_000001011010=P_000001011*R_010[0]+-1*P_000001111*R_011[0]+P_000001211*R_012[0]+-1*P_000101011*R_020[0]+P_000101111*R_021[0]+-1*P_000101211*R_022[0]; double PR_000000012010=P_000000012*R_010[0]+-1*P_000000112*R_011[0]+P_000000212*R_012[0]+-1*P_000000312*R_013[0]; double PR_012000000100=P_012000000*R_100[0]+-1*P_112000000*R_200[0]+P_212000000*R_300[0]+-1*P_312000000*R_400[0]; double PR_011001000100=P_011001000*R_100[0]+-1*P_011101000*R_110[0]+-1*P_111001000*R_200[0]+P_111101000*R_210[0]+P_211001000*R_300[0]+-1*P_211101000*R_310[0]; double PR_010002000100=P_010002000*R_100[0]+-1*P_010102000*R_110[0]+P_010202000*R_120[0]+-1*P_110002000*R_200[0]+P_110102000*R_210[0]+-1*P_110202000*R_220[0]; double PR_011000001100=P_011000001*R_100[0]+-1*P_011000101*R_101[0]+-1*P_111000001*R_200[0]+P_111000101*R_201[0]+P_211000001*R_300[0]+-1*P_211000101*R_301[0]; double PR_010001001100=P_010001001*R_100[0]+-1*P_010001101*R_101[0]+-1*P_010101001*R_110[0]+P_010101101*R_111[0]+-1*P_110001001*R_200[0]+P_110001101*R_201[0]+P_110101001*R_210[0]+-1*P_110101101*R_211[0]; double PR_010000002100=P_010000002*R_100[0]+-1*P_010000102*R_101[0]+P_010000202*R_102[0]+-1*P_110000002*R_200[0]+P_110000102*R_201[0]+-1*P_110000202*R_202[0]; double PR_002010000100=P_002010000*R_100[0]+-1*P_002110000*R_110[0]+-1*P_102010000*R_200[0]+P_102110000*R_210[0]+P_202010000*R_300[0]+-1*P_202110000*R_310[0]; double PR_001011000100=P_001011000*R_100[0]+-1*P_001111000*R_110[0]+P_001211000*R_120[0]+-1*P_101011000*R_200[0]+P_101111000*R_210[0]+-1*P_101211000*R_220[0]; double PR_000012000100=P_000012000*R_100[0]+-1*P_000112000*R_110[0]+P_000212000*R_120[0]+-1*P_000312000*R_130[0]; double PR_001010001100=P_001010001*R_100[0]+-1*P_001010101*R_101[0]+-1*P_001110001*R_110[0]+P_001110101*R_111[0]+-1*P_101010001*R_200[0]+P_101010101*R_201[0]+P_101110001*R_210[0]+-1*P_101110101*R_211[0]; double PR_000011001100=P_000011001*R_100[0]+-1*P_000011101*R_101[0]+-1*P_000111001*R_110[0]+P_000111101*R_111[0]+P_000211001*R_120[0]+-1*P_000211101*R_121[0]; double PR_000010002100=P_000010002*R_100[0]+-1*P_000010102*R_101[0]+P_000010202*R_102[0]+-1*P_000110002*R_110[0]+P_000110102*R_111[0]+-1*P_000110202*R_112[0]; double PR_002000010100=P_002000010*R_100[0]+-1*P_002000110*R_101[0]+-1*P_102000010*R_200[0]+P_102000110*R_201[0]+P_202000010*R_300[0]+-1*P_202000110*R_301[0]; double PR_001001010100=P_001001010*R_100[0]+-1*P_001001110*R_101[0]+-1*P_001101010*R_110[0]+P_001101110*R_111[0]+-1*P_101001010*R_200[0]+P_101001110*R_201[0]+P_101101010*R_210[0]+-1*P_101101110*R_211[0]; double PR_000002010100=P_000002010*R_100[0]+-1*P_000002110*R_101[0]+-1*P_000102010*R_110[0]+P_000102110*R_111[0]+P_000202010*R_120[0]+-1*P_000202110*R_121[0]; double PR_001000011100=P_001000011*R_100[0]+-1*P_001000111*R_101[0]+P_001000211*R_102[0]+-1*P_101000011*R_200[0]+P_101000111*R_201[0]+-1*P_101000211*R_202[0]; double PR_000001011100=P_000001011*R_100[0]+-1*P_000001111*R_101[0]+P_000001211*R_102[0]+-1*P_000101011*R_110[0]+P_000101111*R_111[0]+-1*P_000101211*R_112[0]; double PR_000000012100=P_000000012*R_100[0]+-1*P_000000112*R_101[0]+P_000000212*R_102[0]+-1*P_000000312*R_103[0]; double PR_012000000002=P_012000000*R_002[0]+-1*P_112000000*R_102[0]+P_212000000*R_202[0]+-1*P_312000000*R_302[0]; double PR_011001000002=P_011001000*R_002[0]+-1*P_011101000*R_012[0]+-1*P_111001000*R_102[0]+P_111101000*R_112[0]+P_211001000*R_202[0]+-1*P_211101000*R_212[0]; double PR_010002000002=P_010002000*R_002[0]+-1*P_010102000*R_012[0]+P_010202000*R_022[0]+-1*P_110002000*R_102[0]+P_110102000*R_112[0]+-1*P_110202000*R_122[0]; double PR_011000001002=P_011000001*R_002[0]+-1*P_011000101*R_003[0]+-1*P_111000001*R_102[0]+P_111000101*R_103[0]+P_211000001*R_202[0]+-1*P_211000101*R_203[0]; double PR_010001001002=P_010001001*R_002[0]+-1*P_010001101*R_003[0]+-1*P_010101001*R_012[0]+P_010101101*R_013[0]+-1*P_110001001*R_102[0]+P_110001101*R_103[0]+P_110101001*R_112[0]+-1*P_110101101*R_113[0]; double PR_010000002002=P_010000002*R_002[0]+-1*P_010000102*R_003[0]+P_010000202*R_004[0]+-1*P_110000002*R_102[0]+P_110000102*R_103[0]+-1*P_110000202*R_104[0]; double PR_002010000002=P_002010000*R_002[0]+-1*P_002110000*R_012[0]+-1*P_102010000*R_102[0]+P_102110000*R_112[0]+P_202010000*R_202[0]+-1*P_202110000*R_212[0]; double PR_001011000002=P_001011000*R_002[0]+-1*P_001111000*R_012[0]+P_001211000*R_022[0]+-1*P_101011000*R_102[0]+P_101111000*R_112[0]+-1*P_101211000*R_122[0]; double PR_000012000002=P_000012000*R_002[0]+-1*P_000112000*R_012[0]+P_000212000*R_022[0]+-1*P_000312000*R_032[0]; double PR_001010001002=P_001010001*R_002[0]+-1*P_001010101*R_003[0]+-1*P_001110001*R_012[0]+P_001110101*R_013[0]+-1*P_101010001*R_102[0]+P_101010101*R_103[0]+P_101110001*R_112[0]+-1*P_101110101*R_113[0]; double PR_000011001002=P_000011001*R_002[0]+-1*P_000011101*R_003[0]+-1*P_000111001*R_012[0]+P_000111101*R_013[0]+P_000211001*R_022[0]+-1*P_000211101*R_023[0]; double PR_000010002002=P_000010002*R_002[0]+-1*P_000010102*R_003[0]+P_000010202*R_004[0]+-1*P_000110002*R_012[0]+P_000110102*R_013[0]+-1*P_000110202*R_014[0]; double PR_002000010002=P_002000010*R_002[0]+-1*P_002000110*R_003[0]+-1*P_102000010*R_102[0]+P_102000110*R_103[0]+P_202000010*R_202[0]+-1*P_202000110*R_203[0]; double PR_001001010002=P_001001010*R_002[0]+-1*P_001001110*R_003[0]+-1*P_001101010*R_012[0]+P_001101110*R_013[0]+-1*P_101001010*R_102[0]+P_101001110*R_103[0]+P_101101010*R_112[0]+-1*P_101101110*R_113[0]; double PR_000002010002=P_000002010*R_002[0]+-1*P_000002110*R_003[0]+-1*P_000102010*R_012[0]+P_000102110*R_013[0]+P_000202010*R_022[0]+-1*P_000202110*R_023[0]; double PR_001000011002=P_001000011*R_002[0]+-1*P_001000111*R_003[0]+P_001000211*R_004[0]+-1*P_101000011*R_102[0]+P_101000111*R_103[0]+-1*P_101000211*R_104[0]; double PR_000001011002=P_000001011*R_002[0]+-1*P_000001111*R_003[0]+P_000001211*R_004[0]+-1*P_000101011*R_012[0]+P_000101111*R_013[0]+-1*P_000101211*R_014[0]; double PR_000000012002=P_000000012*R_002[0]+-1*P_000000112*R_003[0]+P_000000212*R_004[0]+-1*P_000000312*R_005[0]; double PR_012000000011=P_012000000*R_011[0]+-1*P_112000000*R_111[0]+P_212000000*R_211[0]+-1*P_312000000*R_311[0]; double PR_011001000011=P_011001000*R_011[0]+-1*P_011101000*R_021[0]+-1*P_111001000*R_111[0]+P_111101000*R_121[0]+P_211001000*R_211[0]+-1*P_211101000*R_221[0]; double PR_010002000011=P_010002000*R_011[0]+-1*P_010102000*R_021[0]+P_010202000*R_031[0]+-1*P_110002000*R_111[0]+P_110102000*R_121[0]+-1*P_110202000*R_131[0]; double PR_011000001011=P_011000001*R_011[0]+-1*P_011000101*R_012[0]+-1*P_111000001*R_111[0]+P_111000101*R_112[0]+P_211000001*R_211[0]+-1*P_211000101*R_212[0]; double PR_010001001011=P_010001001*R_011[0]+-1*P_010001101*R_012[0]+-1*P_010101001*R_021[0]+P_010101101*R_022[0]+-1*P_110001001*R_111[0]+P_110001101*R_112[0]+P_110101001*R_121[0]+-1*P_110101101*R_122[0]; double PR_010000002011=P_010000002*R_011[0]+-1*P_010000102*R_012[0]+P_010000202*R_013[0]+-1*P_110000002*R_111[0]+P_110000102*R_112[0]+-1*P_110000202*R_113[0]; double PR_002010000011=P_002010000*R_011[0]+-1*P_002110000*R_021[0]+-1*P_102010000*R_111[0]+P_102110000*R_121[0]+P_202010000*R_211[0]+-1*P_202110000*R_221[0]; double PR_001011000011=P_001011000*R_011[0]+-1*P_001111000*R_021[0]+P_001211000*R_031[0]+-1*P_101011000*R_111[0]+P_101111000*R_121[0]+-1*P_101211000*R_131[0]; double PR_000012000011=P_000012000*R_011[0]+-1*P_000112000*R_021[0]+P_000212000*R_031[0]+-1*P_000312000*R_041[0]; double PR_001010001011=P_001010001*R_011[0]+-1*P_001010101*R_012[0]+-1*P_001110001*R_021[0]+P_001110101*R_022[0]+-1*P_101010001*R_111[0]+P_101010101*R_112[0]+P_101110001*R_121[0]+-1*P_101110101*R_122[0]; double PR_000011001011=P_000011001*R_011[0]+-1*P_000011101*R_012[0]+-1*P_000111001*R_021[0]+P_000111101*R_022[0]+P_000211001*R_031[0]+-1*P_000211101*R_032[0]; double PR_000010002011=P_000010002*R_011[0]+-1*P_000010102*R_012[0]+P_000010202*R_013[0]+-1*P_000110002*R_021[0]+P_000110102*R_022[0]+-1*P_000110202*R_023[0]; double PR_002000010011=P_002000010*R_011[0]+-1*P_002000110*R_012[0]+-1*P_102000010*R_111[0]+P_102000110*R_112[0]+P_202000010*R_211[0]+-1*P_202000110*R_212[0]; double PR_001001010011=P_001001010*R_011[0]+-1*P_001001110*R_012[0]+-1*P_001101010*R_021[0]+P_001101110*R_022[0]+-1*P_101001010*R_111[0]+P_101001110*R_112[0]+P_101101010*R_121[0]+-1*P_101101110*R_122[0]; double PR_000002010011=P_000002010*R_011[0]+-1*P_000002110*R_012[0]+-1*P_000102010*R_021[0]+P_000102110*R_022[0]+P_000202010*R_031[0]+-1*P_000202110*R_032[0]; double PR_001000011011=P_001000011*R_011[0]+-1*P_001000111*R_012[0]+P_001000211*R_013[0]+-1*P_101000011*R_111[0]+P_101000111*R_112[0]+-1*P_101000211*R_113[0]; double PR_000001011011=P_000001011*R_011[0]+-1*P_000001111*R_012[0]+P_000001211*R_013[0]+-1*P_000101011*R_021[0]+P_000101111*R_022[0]+-1*P_000101211*R_023[0]; double PR_000000012011=P_000000012*R_011[0]+-1*P_000000112*R_012[0]+P_000000212*R_013[0]+-1*P_000000312*R_014[0]; double PR_012000000020=P_012000000*R_020[0]+-1*P_112000000*R_120[0]+P_212000000*R_220[0]+-1*P_312000000*R_320[0]; double PR_011001000020=P_011001000*R_020[0]+-1*P_011101000*R_030[0]+-1*P_111001000*R_120[0]+P_111101000*R_130[0]+P_211001000*R_220[0]+-1*P_211101000*R_230[0]; double PR_010002000020=P_010002000*R_020[0]+-1*P_010102000*R_030[0]+P_010202000*R_040[0]+-1*P_110002000*R_120[0]+P_110102000*R_130[0]+-1*P_110202000*R_140[0]; double PR_011000001020=P_011000001*R_020[0]+-1*P_011000101*R_021[0]+-1*P_111000001*R_120[0]+P_111000101*R_121[0]+P_211000001*R_220[0]+-1*P_211000101*R_221[0]; double PR_010001001020=P_010001001*R_020[0]+-1*P_010001101*R_021[0]+-1*P_010101001*R_030[0]+P_010101101*R_031[0]+-1*P_110001001*R_120[0]+P_110001101*R_121[0]+P_110101001*R_130[0]+-1*P_110101101*R_131[0]; double PR_010000002020=P_010000002*R_020[0]+-1*P_010000102*R_021[0]+P_010000202*R_022[0]+-1*P_110000002*R_120[0]+P_110000102*R_121[0]+-1*P_110000202*R_122[0]; double PR_002010000020=P_002010000*R_020[0]+-1*P_002110000*R_030[0]+-1*P_102010000*R_120[0]+P_102110000*R_130[0]+P_202010000*R_220[0]+-1*P_202110000*R_230[0]; double PR_001011000020=P_001011000*R_020[0]+-1*P_001111000*R_030[0]+P_001211000*R_040[0]+-1*P_101011000*R_120[0]+P_101111000*R_130[0]+-1*P_101211000*R_140[0]; double PR_000012000020=P_000012000*R_020[0]+-1*P_000112000*R_030[0]+P_000212000*R_040[0]+-1*P_000312000*R_050[0]; double PR_001010001020=P_001010001*R_020[0]+-1*P_001010101*R_021[0]+-1*P_001110001*R_030[0]+P_001110101*R_031[0]+-1*P_101010001*R_120[0]+P_101010101*R_121[0]+P_101110001*R_130[0]+-1*P_101110101*R_131[0]; double PR_000011001020=P_000011001*R_020[0]+-1*P_000011101*R_021[0]+-1*P_000111001*R_030[0]+P_000111101*R_031[0]+P_000211001*R_040[0]+-1*P_000211101*R_041[0]; double PR_000010002020=P_000010002*R_020[0]+-1*P_000010102*R_021[0]+P_000010202*R_022[0]+-1*P_000110002*R_030[0]+P_000110102*R_031[0]+-1*P_000110202*R_032[0]; double PR_002000010020=P_002000010*R_020[0]+-1*P_002000110*R_021[0]+-1*P_102000010*R_120[0]+P_102000110*R_121[0]+P_202000010*R_220[0]+-1*P_202000110*R_221[0]; double PR_001001010020=P_001001010*R_020[0]+-1*P_001001110*R_021[0]+-1*P_001101010*R_030[0]+P_001101110*R_031[0]+-1*P_101001010*R_120[0]+P_101001110*R_121[0]+P_101101010*R_130[0]+-1*P_101101110*R_131[0]; double PR_000002010020=P_000002010*R_020[0]+-1*P_000002110*R_021[0]+-1*P_000102010*R_030[0]+P_000102110*R_031[0]+P_000202010*R_040[0]+-1*P_000202110*R_041[0]; double PR_001000011020=P_001000011*R_020[0]+-1*P_001000111*R_021[0]+P_001000211*R_022[0]+-1*P_101000011*R_120[0]+P_101000111*R_121[0]+-1*P_101000211*R_122[0]; double PR_000001011020=P_000001011*R_020[0]+-1*P_000001111*R_021[0]+P_000001211*R_022[0]+-1*P_000101011*R_030[0]+P_000101111*R_031[0]+-1*P_000101211*R_032[0]; double PR_000000012020=P_000000012*R_020[0]+-1*P_000000112*R_021[0]+P_000000212*R_022[0]+-1*P_000000312*R_023[0]; double PR_012000000101=P_012000000*R_101[0]+-1*P_112000000*R_201[0]+P_212000000*R_301[0]+-1*P_312000000*R_401[0]; double PR_011001000101=P_011001000*R_101[0]+-1*P_011101000*R_111[0]+-1*P_111001000*R_201[0]+P_111101000*R_211[0]+P_211001000*R_301[0]+-1*P_211101000*R_311[0]; double PR_010002000101=P_010002000*R_101[0]+-1*P_010102000*R_111[0]+P_010202000*R_121[0]+-1*P_110002000*R_201[0]+P_110102000*R_211[0]+-1*P_110202000*R_221[0]; double PR_011000001101=P_011000001*R_101[0]+-1*P_011000101*R_102[0]+-1*P_111000001*R_201[0]+P_111000101*R_202[0]+P_211000001*R_301[0]+-1*P_211000101*R_302[0]; double PR_010001001101=P_010001001*R_101[0]+-1*P_010001101*R_102[0]+-1*P_010101001*R_111[0]+P_010101101*R_112[0]+-1*P_110001001*R_201[0]+P_110001101*R_202[0]+P_110101001*R_211[0]+-1*P_110101101*R_212[0]; double PR_010000002101=P_010000002*R_101[0]+-1*P_010000102*R_102[0]+P_010000202*R_103[0]+-1*P_110000002*R_201[0]+P_110000102*R_202[0]+-1*P_110000202*R_203[0]; double PR_002010000101=P_002010000*R_101[0]+-1*P_002110000*R_111[0]+-1*P_102010000*R_201[0]+P_102110000*R_211[0]+P_202010000*R_301[0]+-1*P_202110000*R_311[0]; double PR_001011000101=P_001011000*R_101[0]+-1*P_001111000*R_111[0]+P_001211000*R_121[0]+-1*P_101011000*R_201[0]+P_101111000*R_211[0]+-1*P_101211000*R_221[0]; double PR_000012000101=P_000012000*R_101[0]+-1*P_000112000*R_111[0]+P_000212000*R_121[0]+-1*P_000312000*R_131[0]; double PR_001010001101=P_001010001*R_101[0]+-1*P_001010101*R_102[0]+-1*P_001110001*R_111[0]+P_001110101*R_112[0]+-1*P_101010001*R_201[0]+P_101010101*R_202[0]+P_101110001*R_211[0]+-1*P_101110101*R_212[0]; double PR_000011001101=P_000011001*R_101[0]+-1*P_000011101*R_102[0]+-1*P_000111001*R_111[0]+P_000111101*R_112[0]+P_000211001*R_121[0]+-1*P_000211101*R_122[0]; double PR_000010002101=P_000010002*R_101[0]+-1*P_000010102*R_102[0]+P_000010202*R_103[0]+-1*P_000110002*R_111[0]+P_000110102*R_112[0]+-1*P_000110202*R_113[0]; double PR_002000010101=P_002000010*R_101[0]+-1*P_002000110*R_102[0]+-1*P_102000010*R_201[0]+P_102000110*R_202[0]+P_202000010*R_301[0]+-1*P_202000110*R_302[0]; double PR_001001010101=P_001001010*R_101[0]+-1*P_001001110*R_102[0]+-1*P_001101010*R_111[0]+P_001101110*R_112[0]+-1*P_101001010*R_201[0]+P_101001110*R_202[0]+P_101101010*R_211[0]+-1*P_101101110*R_212[0]; double PR_000002010101=P_000002010*R_101[0]+-1*P_000002110*R_102[0]+-1*P_000102010*R_111[0]+P_000102110*R_112[0]+P_000202010*R_121[0]+-1*P_000202110*R_122[0]; double PR_001000011101=P_001000011*R_101[0]+-1*P_001000111*R_102[0]+P_001000211*R_103[0]+-1*P_101000011*R_201[0]+P_101000111*R_202[0]+-1*P_101000211*R_203[0]; double PR_000001011101=P_000001011*R_101[0]+-1*P_000001111*R_102[0]+P_000001211*R_103[0]+-1*P_000101011*R_111[0]+P_000101111*R_112[0]+-1*P_000101211*R_113[0]; double PR_000000012101=P_000000012*R_101[0]+-1*P_000000112*R_102[0]+P_000000212*R_103[0]+-1*P_000000312*R_104[0]; double PR_012000000110=P_012000000*R_110[0]+-1*P_112000000*R_210[0]+P_212000000*R_310[0]+-1*P_312000000*R_410[0]; double PR_011001000110=P_011001000*R_110[0]+-1*P_011101000*R_120[0]+-1*P_111001000*R_210[0]+P_111101000*R_220[0]+P_211001000*R_310[0]+-1*P_211101000*R_320[0]; double PR_010002000110=P_010002000*R_110[0]+-1*P_010102000*R_120[0]+P_010202000*R_130[0]+-1*P_110002000*R_210[0]+P_110102000*R_220[0]+-1*P_110202000*R_230[0]; double PR_011000001110=P_011000001*R_110[0]+-1*P_011000101*R_111[0]+-1*P_111000001*R_210[0]+P_111000101*R_211[0]+P_211000001*R_310[0]+-1*P_211000101*R_311[0]; double PR_010001001110=P_010001001*R_110[0]+-1*P_010001101*R_111[0]+-1*P_010101001*R_120[0]+P_010101101*R_121[0]+-1*P_110001001*R_210[0]+P_110001101*R_211[0]+P_110101001*R_220[0]+-1*P_110101101*R_221[0]; double PR_010000002110=P_010000002*R_110[0]+-1*P_010000102*R_111[0]+P_010000202*R_112[0]+-1*P_110000002*R_210[0]+P_110000102*R_211[0]+-1*P_110000202*R_212[0]; double PR_002010000110=P_002010000*R_110[0]+-1*P_002110000*R_120[0]+-1*P_102010000*R_210[0]+P_102110000*R_220[0]+P_202010000*R_310[0]+-1*P_202110000*R_320[0]; double PR_001011000110=P_001011000*R_110[0]+-1*P_001111000*R_120[0]+P_001211000*R_130[0]+-1*P_101011000*R_210[0]+P_101111000*R_220[0]+-1*P_101211000*R_230[0]; double PR_000012000110=P_000012000*R_110[0]+-1*P_000112000*R_120[0]+P_000212000*R_130[0]+-1*P_000312000*R_140[0]; double PR_001010001110=P_001010001*R_110[0]+-1*P_001010101*R_111[0]+-1*P_001110001*R_120[0]+P_001110101*R_121[0]+-1*P_101010001*R_210[0]+P_101010101*R_211[0]+P_101110001*R_220[0]+-1*P_101110101*R_221[0]; double PR_000011001110=P_000011001*R_110[0]+-1*P_000011101*R_111[0]+-1*P_000111001*R_120[0]+P_000111101*R_121[0]+P_000211001*R_130[0]+-1*P_000211101*R_131[0]; double PR_000010002110=P_000010002*R_110[0]+-1*P_000010102*R_111[0]+P_000010202*R_112[0]+-1*P_000110002*R_120[0]+P_000110102*R_121[0]+-1*P_000110202*R_122[0]; double PR_002000010110=P_002000010*R_110[0]+-1*P_002000110*R_111[0]+-1*P_102000010*R_210[0]+P_102000110*R_211[0]+P_202000010*R_310[0]+-1*P_202000110*R_311[0]; double PR_001001010110=P_001001010*R_110[0]+-1*P_001001110*R_111[0]+-1*P_001101010*R_120[0]+P_001101110*R_121[0]+-1*P_101001010*R_210[0]+P_101001110*R_211[0]+P_101101010*R_220[0]+-1*P_101101110*R_221[0]; double PR_000002010110=P_000002010*R_110[0]+-1*P_000002110*R_111[0]+-1*P_000102010*R_120[0]+P_000102110*R_121[0]+P_000202010*R_130[0]+-1*P_000202110*R_131[0]; double PR_001000011110=P_001000011*R_110[0]+-1*P_001000111*R_111[0]+P_001000211*R_112[0]+-1*P_101000011*R_210[0]+P_101000111*R_211[0]+-1*P_101000211*R_212[0]; double PR_000001011110=P_000001011*R_110[0]+-1*P_000001111*R_111[0]+P_000001211*R_112[0]+-1*P_000101011*R_120[0]+P_000101111*R_121[0]+-1*P_000101211*R_122[0]; double PR_000000012110=P_000000012*R_110[0]+-1*P_000000112*R_111[0]+P_000000212*R_112[0]+-1*P_000000312*R_113[0]; double PR_012000000200=P_012000000*R_200[0]+-1*P_112000000*R_300[0]+P_212000000*R_400[0]+-1*P_312000000*R_500[0]; double PR_011001000200=P_011001000*R_200[0]+-1*P_011101000*R_210[0]+-1*P_111001000*R_300[0]+P_111101000*R_310[0]+P_211001000*R_400[0]+-1*P_211101000*R_410[0]; double PR_010002000200=P_010002000*R_200[0]+-1*P_010102000*R_210[0]+P_010202000*R_220[0]+-1*P_110002000*R_300[0]+P_110102000*R_310[0]+-1*P_110202000*R_320[0]; double PR_011000001200=P_011000001*R_200[0]+-1*P_011000101*R_201[0]+-1*P_111000001*R_300[0]+P_111000101*R_301[0]+P_211000001*R_400[0]+-1*P_211000101*R_401[0]; double PR_010001001200=P_010001001*R_200[0]+-1*P_010001101*R_201[0]+-1*P_010101001*R_210[0]+P_010101101*R_211[0]+-1*P_110001001*R_300[0]+P_110001101*R_301[0]+P_110101001*R_310[0]+-1*P_110101101*R_311[0]; double PR_010000002200=P_010000002*R_200[0]+-1*P_010000102*R_201[0]+P_010000202*R_202[0]+-1*P_110000002*R_300[0]+P_110000102*R_301[0]+-1*P_110000202*R_302[0]; double PR_002010000200=P_002010000*R_200[0]+-1*P_002110000*R_210[0]+-1*P_102010000*R_300[0]+P_102110000*R_310[0]+P_202010000*R_400[0]+-1*P_202110000*R_410[0]; double PR_001011000200=P_001011000*R_200[0]+-1*P_001111000*R_210[0]+P_001211000*R_220[0]+-1*P_101011000*R_300[0]+P_101111000*R_310[0]+-1*P_101211000*R_320[0]; double PR_000012000200=P_000012000*R_200[0]+-1*P_000112000*R_210[0]+P_000212000*R_220[0]+-1*P_000312000*R_230[0]; double PR_001010001200=P_001010001*R_200[0]+-1*P_001010101*R_201[0]+-1*P_001110001*R_210[0]+P_001110101*R_211[0]+-1*P_101010001*R_300[0]+P_101010101*R_301[0]+P_101110001*R_310[0]+-1*P_101110101*R_311[0]; double PR_000011001200=P_000011001*R_200[0]+-1*P_000011101*R_201[0]+-1*P_000111001*R_210[0]+P_000111101*R_211[0]+P_000211001*R_220[0]+-1*P_000211101*R_221[0]; double PR_000010002200=P_000010002*R_200[0]+-1*P_000010102*R_201[0]+P_000010202*R_202[0]+-1*P_000110002*R_210[0]+P_000110102*R_211[0]+-1*P_000110202*R_212[0]; double PR_002000010200=P_002000010*R_200[0]+-1*P_002000110*R_201[0]+-1*P_102000010*R_300[0]+P_102000110*R_301[0]+P_202000010*R_400[0]+-1*P_202000110*R_401[0]; double PR_001001010200=P_001001010*R_200[0]+-1*P_001001110*R_201[0]+-1*P_001101010*R_210[0]+P_001101110*R_211[0]+-1*P_101001010*R_300[0]+P_101001110*R_301[0]+P_101101010*R_310[0]+-1*P_101101110*R_311[0]; double PR_000002010200=P_000002010*R_200[0]+-1*P_000002110*R_201[0]+-1*P_000102010*R_210[0]+P_000102110*R_211[0]+P_000202010*R_220[0]+-1*P_000202110*R_221[0]; double PR_001000011200=P_001000011*R_200[0]+-1*P_001000111*R_201[0]+P_001000211*R_202[0]+-1*P_101000011*R_300[0]+P_101000111*R_301[0]+-1*P_101000211*R_302[0]; double PR_000001011200=P_000001011*R_200[0]+-1*P_000001111*R_201[0]+P_000001211*R_202[0]+-1*P_000101011*R_210[0]+P_000101111*R_211[0]+-1*P_000101211*R_212[0]; double PR_000000012200=P_000000012*R_200[0]+-1*P_000000112*R_201[0]+P_000000212*R_202[0]+-1*P_000000312*R_203[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*3+0]+=Pmtrx[0]*(Q_002000000*PR_012000000000+Q_102000000*PR_012000000100+Q_202000000*PR_012000000200); ans_temp[ans_id*3+0]+=Pmtrx[1]*(Q_001001000*PR_012000000000+Q_001101000*PR_012000000010+Q_101001000*PR_012000000100+Q_101101000*PR_012000000110); ans_temp[ans_id*3+0]+=Pmtrx[2]*(Q_000002000*PR_012000000000+Q_000102000*PR_012000000010+Q_000202000*PR_012000000020); ans_temp[ans_id*3+0]+=Pmtrx[3]*(Q_001000001*PR_012000000000+Q_001000101*PR_012000000001+Q_101000001*PR_012000000100+Q_101000101*PR_012000000101); ans_temp[ans_id*3+0]+=Pmtrx[4]*(Q_000001001*PR_012000000000+Q_000001101*PR_012000000001+Q_000101001*PR_012000000010+Q_000101101*PR_012000000011); ans_temp[ans_id*3+0]+=Pmtrx[5]*(Q_000000002*PR_012000000000+Q_000000102*PR_012000000001+Q_000000202*PR_012000000002); ans_temp[ans_id*3+0]+=Pmtrx[6]*(Q_002000000*PR_011001000000+Q_102000000*PR_011001000100+Q_202000000*PR_011001000200); ans_temp[ans_id*3+0]+=Pmtrx[7]*(Q_001001000*PR_011001000000+Q_001101000*PR_011001000010+Q_101001000*PR_011001000100+Q_101101000*PR_011001000110); ans_temp[ans_id*3+0]+=Pmtrx[8]*(Q_000002000*PR_011001000000+Q_000102000*PR_011001000010+Q_000202000*PR_011001000020); ans_temp[ans_id*3+0]+=Pmtrx[9]*(Q_001000001*PR_011001000000+Q_001000101*PR_011001000001+Q_101000001*PR_011001000100+Q_101000101*PR_011001000101); ans_temp[ans_id*3+0]+=Pmtrx[10]*(Q_000001001*PR_011001000000+Q_000001101*PR_011001000001+Q_000101001*PR_011001000010+Q_000101101*PR_011001000011); ans_temp[ans_id*3+0]+=Pmtrx[11]*(Q_000000002*PR_011001000000+Q_000000102*PR_011001000001+Q_000000202*PR_011001000002); ans_temp[ans_id*3+0]+=Pmtrx[12]*(Q_002000000*PR_010002000000+Q_102000000*PR_010002000100+Q_202000000*PR_010002000200); ans_temp[ans_id*3+0]+=Pmtrx[13]*(Q_001001000*PR_010002000000+Q_001101000*PR_010002000010+Q_101001000*PR_010002000100+Q_101101000*PR_010002000110); ans_temp[ans_id*3+0]+=Pmtrx[14]*(Q_000002000*PR_010002000000+Q_000102000*PR_010002000010+Q_000202000*PR_010002000020); ans_temp[ans_id*3+0]+=Pmtrx[15]*(Q_001000001*PR_010002000000+Q_001000101*PR_010002000001+Q_101000001*PR_010002000100+Q_101000101*PR_010002000101); ans_temp[ans_id*3+0]+=Pmtrx[16]*(Q_000001001*PR_010002000000+Q_000001101*PR_010002000001+Q_000101001*PR_010002000010+Q_000101101*PR_010002000011); ans_temp[ans_id*3+0]+=Pmtrx[17]*(Q_000000002*PR_010002000000+Q_000000102*PR_010002000001+Q_000000202*PR_010002000002); ans_temp[ans_id*3+0]+=Pmtrx[18]*(Q_002000000*PR_011000001000+Q_102000000*PR_011000001100+Q_202000000*PR_011000001200); ans_temp[ans_id*3+0]+=Pmtrx[19]*(Q_001001000*PR_011000001000+Q_001101000*PR_011000001010+Q_101001000*PR_011000001100+Q_101101000*PR_011000001110); ans_temp[ans_id*3+0]+=Pmtrx[20]*(Q_000002000*PR_011000001000+Q_000102000*PR_011000001010+Q_000202000*PR_011000001020); ans_temp[ans_id*3+0]+=Pmtrx[21]*(Q_001000001*PR_011000001000+Q_001000101*PR_011000001001+Q_101000001*PR_011000001100+Q_101000101*PR_011000001101); ans_temp[ans_id*3+0]+=Pmtrx[22]*(Q_000001001*PR_011000001000+Q_000001101*PR_011000001001+Q_000101001*PR_011000001010+Q_000101101*PR_011000001011); ans_temp[ans_id*3+0]+=Pmtrx[23]*(Q_000000002*PR_011000001000+Q_000000102*PR_011000001001+Q_000000202*PR_011000001002); ans_temp[ans_id*3+0]+=Pmtrx[24]*(Q_002000000*PR_010001001000+Q_102000000*PR_010001001100+Q_202000000*PR_010001001200); ans_temp[ans_id*3+0]+=Pmtrx[25]*(Q_001001000*PR_010001001000+Q_001101000*PR_010001001010+Q_101001000*PR_010001001100+Q_101101000*PR_010001001110); ans_temp[ans_id*3+0]+=Pmtrx[26]*(Q_000002000*PR_010001001000+Q_000102000*PR_010001001010+Q_000202000*PR_010001001020); ans_temp[ans_id*3+0]+=Pmtrx[27]*(Q_001000001*PR_010001001000+Q_001000101*PR_010001001001+Q_101000001*PR_010001001100+Q_101000101*PR_010001001101); ans_temp[ans_id*3+0]+=Pmtrx[28]*(Q_000001001*PR_010001001000+Q_000001101*PR_010001001001+Q_000101001*PR_010001001010+Q_000101101*PR_010001001011); ans_temp[ans_id*3+0]+=Pmtrx[29]*(Q_000000002*PR_010001001000+Q_000000102*PR_010001001001+Q_000000202*PR_010001001002); ans_temp[ans_id*3+0]+=Pmtrx[30]*(Q_002000000*PR_010000002000+Q_102000000*PR_010000002100+Q_202000000*PR_010000002200); ans_temp[ans_id*3+0]+=Pmtrx[31]*(Q_001001000*PR_010000002000+Q_001101000*PR_010000002010+Q_101001000*PR_010000002100+Q_101101000*PR_010000002110); ans_temp[ans_id*3+0]+=Pmtrx[32]*(Q_000002000*PR_010000002000+Q_000102000*PR_010000002010+Q_000202000*PR_010000002020); ans_temp[ans_id*3+0]+=Pmtrx[33]*(Q_001000001*PR_010000002000+Q_001000101*PR_010000002001+Q_101000001*PR_010000002100+Q_101000101*PR_010000002101); ans_temp[ans_id*3+0]+=Pmtrx[34]*(Q_000001001*PR_010000002000+Q_000001101*PR_010000002001+Q_000101001*PR_010000002010+Q_000101101*PR_010000002011); ans_temp[ans_id*3+0]+=Pmtrx[35]*(Q_000000002*PR_010000002000+Q_000000102*PR_010000002001+Q_000000202*PR_010000002002); ans_temp[ans_id*3+1]+=Pmtrx[0]*(Q_002000000*PR_002010000000+Q_102000000*PR_002010000100+Q_202000000*PR_002010000200); ans_temp[ans_id*3+1]+=Pmtrx[1]*(Q_001001000*PR_002010000000+Q_001101000*PR_002010000010+Q_101001000*PR_002010000100+Q_101101000*PR_002010000110); ans_temp[ans_id*3+1]+=Pmtrx[2]*(Q_000002000*PR_002010000000+Q_000102000*PR_002010000010+Q_000202000*PR_002010000020); ans_temp[ans_id*3+1]+=Pmtrx[3]*(Q_001000001*PR_002010000000+Q_001000101*PR_002010000001+Q_101000001*PR_002010000100+Q_101000101*PR_002010000101); ans_temp[ans_id*3+1]+=Pmtrx[4]*(Q_000001001*PR_002010000000+Q_000001101*PR_002010000001+Q_000101001*PR_002010000010+Q_000101101*PR_002010000011); ans_temp[ans_id*3+1]+=Pmtrx[5]*(Q_000000002*PR_002010000000+Q_000000102*PR_002010000001+Q_000000202*PR_002010000002); ans_temp[ans_id*3+1]+=Pmtrx[6]*(Q_002000000*PR_001011000000+Q_102000000*PR_001011000100+Q_202000000*PR_001011000200); ans_temp[ans_id*3+1]+=Pmtrx[7]*(Q_001001000*PR_001011000000+Q_001101000*PR_001011000010+Q_101001000*PR_001011000100+Q_101101000*PR_001011000110); ans_temp[ans_id*3+1]+=Pmtrx[8]*(Q_000002000*PR_001011000000+Q_000102000*PR_001011000010+Q_000202000*PR_001011000020); ans_temp[ans_id*3+1]+=Pmtrx[9]*(Q_001000001*PR_001011000000+Q_001000101*PR_001011000001+Q_101000001*PR_001011000100+Q_101000101*PR_001011000101); ans_temp[ans_id*3+1]+=Pmtrx[10]*(Q_000001001*PR_001011000000+Q_000001101*PR_001011000001+Q_000101001*PR_001011000010+Q_000101101*PR_001011000011); ans_temp[ans_id*3+1]+=Pmtrx[11]*(Q_000000002*PR_001011000000+Q_000000102*PR_001011000001+Q_000000202*PR_001011000002); ans_temp[ans_id*3+1]+=Pmtrx[12]*(Q_002000000*PR_000012000000+Q_102000000*PR_000012000100+Q_202000000*PR_000012000200); ans_temp[ans_id*3+1]+=Pmtrx[13]*(Q_001001000*PR_000012000000+Q_001101000*PR_000012000010+Q_101001000*PR_000012000100+Q_101101000*PR_000012000110); ans_temp[ans_id*3+1]+=Pmtrx[14]*(Q_000002000*PR_000012000000+Q_000102000*PR_000012000010+Q_000202000*PR_000012000020); ans_temp[ans_id*3+1]+=Pmtrx[15]*(Q_001000001*PR_000012000000+Q_001000101*PR_000012000001+Q_101000001*PR_000012000100+Q_101000101*PR_000012000101); ans_temp[ans_id*3+1]+=Pmtrx[16]*(Q_000001001*PR_000012000000+Q_000001101*PR_000012000001+Q_000101001*PR_000012000010+Q_000101101*PR_000012000011); ans_temp[ans_id*3+1]+=Pmtrx[17]*(Q_000000002*PR_000012000000+Q_000000102*PR_000012000001+Q_000000202*PR_000012000002); ans_temp[ans_id*3+1]+=Pmtrx[18]*(Q_002000000*PR_001010001000+Q_102000000*PR_001010001100+Q_202000000*PR_001010001200); ans_temp[ans_id*3+1]+=Pmtrx[19]*(Q_001001000*PR_001010001000+Q_001101000*PR_001010001010+Q_101001000*PR_001010001100+Q_101101000*PR_001010001110); ans_temp[ans_id*3+1]+=Pmtrx[20]*(Q_000002000*PR_001010001000+Q_000102000*PR_001010001010+Q_000202000*PR_001010001020); ans_temp[ans_id*3+1]+=Pmtrx[21]*(Q_001000001*PR_001010001000+Q_001000101*PR_001010001001+Q_101000001*PR_001010001100+Q_101000101*PR_001010001101); ans_temp[ans_id*3+1]+=Pmtrx[22]*(Q_000001001*PR_001010001000+Q_000001101*PR_001010001001+Q_000101001*PR_001010001010+Q_000101101*PR_001010001011); ans_temp[ans_id*3+1]+=Pmtrx[23]*(Q_000000002*PR_001010001000+Q_000000102*PR_001010001001+Q_000000202*PR_001010001002); ans_temp[ans_id*3+1]+=Pmtrx[24]*(Q_002000000*PR_000011001000+Q_102000000*PR_000011001100+Q_202000000*PR_000011001200); ans_temp[ans_id*3+1]+=Pmtrx[25]*(Q_001001000*PR_000011001000+Q_001101000*PR_000011001010+Q_101001000*PR_000011001100+Q_101101000*PR_000011001110); ans_temp[ans_id*3+1]+=Pmtrx[26]*(Q_000002000*PR_000011001000+Q_000102000*PR_000011001010+Q_000202000*PR_000011001020); ans_temp[ans_id*3+1]+=Pmtrx[27]*(Q_001000001*PR_000011001000+Q_001000101*PR_000011001001+Q_101000001*PR_000011001100+Q_101000101*PR_000011001101); ans_temp[ans_id*3+1]+=Pmtrx[28]*(Q_000001001*PR_000011001000+Q_000001101*PR_000011001001+Q_000101001*PR_000011001010+Q_000101101*PR_000011001011); ans_temp[ans_id*3+1]+=Pmtrx[29]*(Q_000000002*PR_000011001000+Q_000000102*PR_000011001001+Q_000000202*PR_000011001002); ans_temp[ans_id*3+1]+=Pmtrx[30]*(Q_002000000*PR_000010002000+Q_102000000*PR_000010002100+Q_202000000*PR_000010002200); ans_temp[ans_id*3+1]+=Pmtrx[31]*(Q_001001000*PR_000010002000+Q_001101000*PR_000010002010+Q_101001000*PR_000010002100+Q_101101000*PR_000010002110); ans_temp[ans_id*3+1]+=Pmtrx[32]*(Q_000002000*PR_000010002000+Q_000102000*PR_000010002010+Q_000202000*PR_000010002020); ans_temp[ans_id*3+1]+=Pmtrx[33]*(Q_001000001*PR_000010002000+Q_001000101*PR_000010002001+Q_101000001*PR_000010002100+Q_101000101*PR_000010002101); ans_temp[ans_id*3+1]+=Pmtrx[34]*(Q_000001001*PR_000010002000+Q_000001101*PR_000010002001+Q_000101001*PR_000010002010+Q_000101101*PR_000010002011); ans_temp[ans_id*3+1]+=Pmtrx[35]*(Q_000000002*PR_000010002000+Q_000000102*PR_000010002001+Q_000000202*PR_000010002002); ans_temp[ans_id*3+2]+=Pmtrx[0]*(Q_002000000*PR_002000010000+Q_102000000*PR_002000010100+Q_202000000*PR_002000010200); ans_temp[ans_id*3+2]+=Pmtrx[1]*(Q_001001000*PR_002000010000+Q_001101000*PR_002000010010+Q_101001000*PR_002000010100+Q_101101000*PR_002000010110); ans_temp[ans_id*3+2]+=Pmtrx[2]*(Q_000002000*PR_002000010000+Q_000102000*PR_002000010010+Q_000202000*PR_002000010020); ans_temp[ans_id*3+2]+=Pmtrx[3]*(Q_001000001*PR_002000010000+Q_001000101*PR_002000010001+Q_101000001*PR_002000010100+Q_101000101*PR_002000010101); ans_temp[ans_id*3+2]+=Pmtrx[4]*(Q_000001001*PR_002000010000+Q_000001101*PR_002000010001+Q_000101001*PR_002000010010+Q_000101101*PR_002000010011); ans_temp[ans_id*3+2]+=Pmtrx[5]*(Q_000000002*PR_002000010000+Q_000000102*PR_002000010001+Q_000000202*PR_002000010002); ans_temp[ans_id*3+2]+=Pmtrx[6]*(Q_002000000*PR_001001010000+Q_102000000*PR_001001010100+Q_202000000*PR_001001010200); ans_temp[ans_id*3+2]+=Pmtrx[7]*(Q_001001000*PR_001001010000+Q_001101000*PR_001001010010+Q_101001000*PR_001001010100+Q_101101000*PR_001001010110); ans_temp[ans_id*3+2]+=Pmtrx[8]*(Q_000002000*PR_001001010000+Q_000102000*PR_001001010010+Q_000202000*PR_001001010020); ans_temp[ans_id*3+2]+=Pmtrx[9]*(Q_001000001*PR_001001010000+Q_001000101*PR_001001010001+Q_101000001*PR_001001010100+Q_101000101*PR_001001010101); ans_temp[ans_id*3+2]+=Pmtrx[10]*(Q_000001001*PR_001001010000+Q_000001101*PR_001001010001+Q_000101001*PR_001001010010+Q_000101101*PR_001001010011); ans_temp[ans_id*3+2]+=Pmtrx[11]*(Q_000000002*PR_001001010000+Q_000000102*PR_001001010001+Q_000000202*PR_001001010002); ans_temp[ans_id*3+2]+=Pmtrx[12]*(Q_002000000*PR_000002010000+Q_102000000*PR_000002010100+Q_202000000*PR_000002010200); ans_temp[ans_id*3+2]+=Pmtrx[13]*(Q_001001000*PR_000002010000+Q_001101000*PR_000002010010+Q_101001000*PR_000002010100+Q_101101000*PR_000002010110); ans_temp[ans_id*3+2]+=Pmtrx[14]*(Q_000002000*PR_000002010000+Q_000102000*PR_000002010010+Q_000202000*PR_000002010020); ans_temp[ans_id*3+2]+=Pmtrx[15]*(Q_001000001*PR_000002010000+Q_001000101*PR_000002010001+Q_101000001*PR_000002010100+Q_101000101*PR_000002010101); ans_temp[ans_id*3+2]+=Pmtrx[16]*(Q_000001001*PR_000002010000+Q_000001101*PR_000002010001+Q_000101001*PR_000002010010+Q_000101101*PR_000002010011); ans_temp[ans_id*3+2]+=Pmtrx[17]*(Q_000000002*PR_000002010000+Q_000000102*PR_000002010001+Q_000000202*PR_000002010002); ans_temp[ans_id*3+2]+=Pmtrx[18]*(Q_002000000*PR_001000011000+Q_102000000*PR_001000011100+Q_202000000*PR_001000011200); ans_temp[ans_id*3+2]+=Pmtrx[19]*(Q_001001000*PR_001000011000+Q_001101000*PR_001000011010+Q_101001000*PR_001000011100+Q_101101000*PR_001000011110); ans_temp[ans_id*3+2]+=Pmtrx[20]*(Q_000002000*PR_001000011000+Q_000102000*PR_001000011010+Q_000202000*PR_001000011020); ans_temp[ans_id*3+2]+=Pmtrx[21]*(Q_001000001*PR_001000011000+Q_001000101*PR_001000011001+Q_101000001*PR_001000011100+Q_101000101*PR_001000011101); ans_temp[ans_id*3+2]+=Pmtrx[22]*(Q_000001001*PR_001000011000+Q_000001101*PR_001000011001+Q_000101001*PR_001000011010+Q_000101101*PR_001000011011); ans_temp[ans_id*3+2]+=Pmtrx[23]*(Q_000000002*PR_001000011000+Q_000000102*PR_001000011001+Q_000000202*PR_001000011002); ans_temp[ans_id*3+2]+=Pmtrx[24]*(Q_002000000*PR_000001011000+Q_102000000*PR_000001011100+Q_202000000*PR_000001011200); ans_temp[ans_id*3+2]+=Pmtrx[25]*(Q_001001000*PR_000001011000+Q_001101000*PR_000001011010+Q_101001000*PR_000001011100+Q_101101000*PR_000001011110); ans_temp[ans_id*3+2]+=Pmtrx[26]*(Q_000002000*PR_000001011000+Q_000102000*PR_000001011010+Q_000202000*PR_000001011020); ans_temp[ans_id*3+2]+=Pmtrx[27]*(Q_001000001*PR_000001011000+Q_001000101*PR_000001011001+Q_101000001*PR_000001011100+Q_101000101*PR_000001011101); ans_temp[ans_id*3+2]+=Pmtrx[28]*(Q_000001001*PR_000001011000+Q_000001101*PR_000001011001+Q_000101001*PR_000001011010+Q_000101101*PR_000001011011); ans_temp[ans_id*3+2]+=Pmtrx[29]*(Q_000000002*PR_000001011000+Q_000000102*PR_000001011001+Q_000000202*PR_000001011002); ans_temp[ans_id*3+2]+=Pmtrx[30]*(Q_002000000*PR_000000012000+Q_102000000*PR_000000012100+Q_202000000*PR_000000012200); ans_temp[ans_id*3+2]+=Pmtrx[31]*(Q_001001000*PR_000000012000+Q_001101000*PR_000000012010+Q_101001000*PR_000000012100+Q_101101000*PR_000000012110); ans_temp[ans_id*3+2]+=Pmtrx[32]*(Q_000002000*PR_000000012000+Q_000102000*PR_000000012010+Q_000202000*PR_000000012020); ans_temp[ans_id*3+2]+=Pmtrx[33]*(Q_001000001*PR_000000012000+Q_001000101*PR_000000012001+Q_101000001*PR_000000012100+Q_101000101*PR_000000012101); ans_temp[ans_id*3+2]+=Pmtrx[34]*(Q_000001001*PR_000000012000+Q_000001101*PR_000000012001+Q_000101001*PR_000000012010+Q_000101101*PR_000000012011); ans_temp[ans_id*3+2]+=Pmtrx[35]*(Q_000000002*PR_000000012000+Q_000000102*PR_000000012001+Q_000000202*PR_000000012002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<3;ians++){ ans_temp[tId_x*3+ians]+=ans_temp[(tId_x+num_thread)*3+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<3;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*3+ians]=ans_temp[(tId_x)*3+ians]; } } } } } __global__ void MD_Kq_pdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*3]; for(int i=0;i<3;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_010[3]; Pd_010[0]=PA[ii*3+0]; Pd_010[1]=PA[ii*3+1]; Pd_010[2]=PA[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double QR_002000000003=Q_002000000*R_003[0]+-1*Q_102000000*R_103[0]+Q_202000000*R_203[0]; double QR_001001000003=Q_001001000*R_003[0]+-1*Q_001101000*R_013[0]+-1*Q_101001000*R_103[0]+Q_101101000*R_113[0]; double QR_000002000003=Q_000002000*R_003[0]+-1*Q_000102000*R_013[0]+Q_000202000*R_023[0]; double QR_001000001003=Q_001000001*R_003[0]+-1*Q_001000101*R_004[0]+-1*Q_101000001*R_103[0]+Q_101000101*R_104[0]; double QR_000001001003=Q_000001001*R_003[0]+-1*Q_000001101*R_004[0]+-1*Q_000101001*R_013[0]+Q_000101101*R_014[0]; double QR_000000002003=Q_000000002*R_003[0]+-1*Q_000000102*R_004[0]+Q_000000202*R_005[0]; double QR_002000000012=Q_002000000*R_012[0]+-1*Q_102000000*R_112[0]+Q_202000000*R_212[0]; double QR_001001000012=Q_001001000*R_012[0]+-1*Q_001101000*R_022[0]+-1*Q_101001000*R_112[0]+Q_101101000*R_122[0]; double QR_000002000012=Q_000002000*R_012[0]+-1*Q_000102000*R_022[0]+Q_000202000*R_032[0]; double QR_001000001012=Q_001000001*R_012[0]+-1*Q_001000101*R_013[0]+-1*Q_101000001*R_112[0]+Q_101000101*R_113[0]; double QR_000001001012=Q_000001001*R_012[0]+-1*Q_000001101*R_013[0]+-1*Q_000101001*R_022[0]+Q_000101101*R_023[0]; double QR_000000002012=Q_000000002*R_012[0]+-1*Q_000000102*R_013[0]+Q_000000202*R_014[0]; double QR_002000000021=Q_002000000*R_021[0]+-1*Q_102000000*R_121[0]+Q_202000000*R_221[0]; double QR_001001000021=Q_001001000*R_021[0]+-1*Q_001101000*R_031[0]+-1*Q_101001000*R_121[0]+Q_101101000*R_131[0]; double QR_000002000021=Q_000002000*R_021[0]+-1*Q_000102000*R_031[0]+Q_000202000*R_041[0]; double QR_001000001021=Q_001000001*R_021[0]+-1*Q_001000101*R_022[0]+-1*Q_101000001*R_121[0]+Q_101000101*R_122[0]; double QR_000001001021=Q_000001001*R_021[0]+-1*Q_000001101*R_022[0]+-1*Q_000101001*R_031[0]+Q_000101101*R_032[0]; double QR_000000002021=Q_000000002*R_021[0]+-1*Q_000000102*R_022[0]+Q_000000202*R_023[0]; double QR_002000000030=Q_002000000*R_030[0]+-1*Q_102000000*R_130[0]+Q_202000000*R_230[0]; double QR_001001000030=Q_001001000*R_030[0]+-1*Q_001101000*R_040[0]+-1*Q_101001000*R_130[0]+Q_101101000*R_140[0]; double QR_000002000030=Q_000002000*R_030[0]+-1*Q_000102000*R_040[0]+Q_000202000*R_050[0]; double QR_001000001030=Q_001000001*R_030[0]+-1*Q_001000101*R_031[0]+-1*Q_101000001*R_130[0]+Q_101000101*R_131[0]; double QR_000001001030=Q_000001001*R_030[0]+-1*Q_000001101*R_031[0]+-1*Q_000101001*R_040[0]+Q_000101101*R_041[0]; double QR_000000002030=Q_000000002*R_030[0]+-1*Q_000000102*R_031[0]+Q_000000202*R_032[0]; double QR_002000000102=Q_002000000*R_102[0]+-1*Q_102000000*R_202[0]+Q_202000000*R_302[0]; double QR_001001000102=Q_001001000*R_102[0]+-1*Q_001101000*R_112[0]+-1*Q_101001000*R_202[0]+Q_101101000*R_212[0]; double QR_000002000102=Q_000002000*R_102[0]+-1*Q_000102000*R_112[0]+Q_000202000*R_122[0]; double QR_001000001102=Q_001000001*R_102[0]+-1*Q_001000101*R_103[0]+-1*Q_101000001*R_202[0]+Q_101000101*R_203[0]; double QR_000001001102=Q_000001001*R_102[0]+-1*Q_000001101*R_103[0]+-1*Q_000101001*R_112[0]+Q_000101101*R_113[0]; double QR_000000002102=Q_000000002*R_102[0]+-1*Q_000000102*R_103[0]+Q_000000202*R_104[0]; double QR_002000000111=Q_002000000*R_111[0]+-1*Q_102000000*R_211[0]+Q_202000000*R_311[0]; double QR_001001000111=Q_001001000*R_111[0]+-1*Q_001101000*R_121[0]+-1*Q_101001000*R_211[0]+Q_101101000*R_221[0]; double QR_000002000111=Q_000002000*R_111[0]+-1*Q_000102000*R_121[0]+Q_000202000*R_131[0]; double QR_001000001111=Q_001000001*R_111[0]+-1*Q_001000101*R_112[0]+-1*Q_101000001*R_211[0]+Q_101000101*R_212[0]; double QR_000001001111=Q_000001001*R_111[0]+-1*Q_000001101*R_112[0]+-1*Q_000101001*R_121[0]+Q_000101101*R_122[0]; double QR_000000002111=Q_000000002*R_111[0]+-1*Q_000000102*R_112[0]+Q_000000202*R_113[0]; double QR_002000000120=Q_002000000*R_120[0]+-1*Q_102000000*R_220[0]+Q_202000000*R_320[0]; double QR_001001000120=Q_001001000*R_120[0]+-1*Q_001101000*R_130[0]+-1*Q_101001000*R_220[0]+Q_101101000*R_230[0]; double QR_000002000120=Q_000002000*R_120[0]+-1*Q_000102000*R_130[0]+Q_000202000*R_140[0]; double QR_001000001120=Q_001000001*R_120[0]+-1*Q_001000101*R_121[0]+-1*Q_101000001*R_220[0]+Q_101000101*R_221[0]; double QR_000001001120=Q_000001001*R_120[0]+-1*Q_000001101*R_121[0]+-1*Q_000101001*R_130[0]+Q_000101101*R_131[0]; double QR_000000002120=Q_000000002*R_120[0]+-1*Q_000000102*R_121[0]+Q_000000202*R_122[0]; double QR_002000000201=Q_002000000*R_201[0]+-1*Q_102000000*R_301[0]+Q_202000000*R_401[0]; double QR_001001000201=Q_001001000*R_201[0]+-1*Q_001101000*R_211[0]+-1*Q_101001000*R_301[0]+Q_101101000*R_311[0]; double QR_000002000201=Q_000002000*R_201[0]+-1*Q_000102000*R_211[0]+Q_000202000*R_221[0]; double QR_001000001201=Q_001000001*R_201[0]+-1*Q_001000101*R_202[0]+-1*Q_101000001*R_301[0]+Q_101000101*R_302[0]; double QR_000001001201=Q_000001001*R_201[0]+-1*Q_000001101*R_202[0]+-1*Q_000101001*R_211[0]+Q_000101101*R_212[0]; double QR_000000002201=Q_000000002*R_201[0]+-1*Q_000000102*R_202[0]+Q_000000202*R_203[0]; double QR_002000000210=Q_002000000*R_210[0]+-1*Q_102000000*R_310[0]+Q_202000000*R_410[0]; double QR_001001000210=Q_001001000*R_210[0]+-1*Q_001101000*R_220[0]+-1*Q_101001000*R_310[0]+Q_101101000*R_320[0]; double QR_000002000210=Q_000002000*R_210[0]+-1*Q_000102000*R_220[0]+Q_000202000*R_230[0]; double QR_001000001210=Q_001000001*R_210[0]+-1*Q_001000101*R_211[0]+-1*Q_101000001*R_310[0]+Q_101000101*R_311[0]; double QR_000001001210=Q_000001001*R_210[0]+-1*Q_000001101*R_211[0]+-1*Q_000101001*R_220[0]+Q_000101101*R_221[0]; double QR_000000002210=Q_000000002*R_210[0]+-1*Q_000000102*R_211[0]+Q_000000202*R_212[0]; double QR_002000000300=Q_002000000*R_300[0]+-1*Q_102000000*R_400[0]+Q_202000000*R_500[0]; double QR_001001000300=Q_001001000*R_300[0]+-1*Q_001101000*R_310[0]+-1*Q_101001000*R_400[0]+Q_101101000*R_410[0]; double QR_000002000300=Q_000002000*R_300[0]+-1*Q_000102000*R_310[0]+Q_000202000*R_320[0]; double QR_001000001300=Q_001000001*R_300[0]+-1*Q_001000101*R_301[0]+-1*Q_101000001*R_400[0]+Q_101000101*R_401[0]; double QR_000001001300=Q_000001001*R_300[0]+-1*Q_000001101*R_301[0]+-1*Q_000101001*R_310[0]+Q_000101101*R_311[0]; double QR_000000002300=Q_000000002*R_300[0]+-1*Q_000000102*R_301[0]+Q_000000202*R_302[0]; double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } double P_012000000=Pd_012[0]; double P_112000000=Pd_112[0]; double P_212000000=Pd_212[0]; double P_312000000=Pd_312[0]; double P_011001000=Pd_011[0]*Pd_001[1]; double P_011101000=Pd_011[0]*Pd_101[1]; double P_111001000=Pd_111[0]*Pd_001[1]; double P_111101000=Pd_111[0]*Pd_101[1]; double P_211001000=Pd_211[0]*Pd_001[1]; double P_211101000=Pd_211[0]*Pd_101[1]; double P_010002000=Pd_010[0]*Pd_002[1]; double P_010102000=Pd_010[0]*Pd_102[1]; double P_010202000=Pd_010[0]*Pd_202[1]; double P_110002000=Pd_110[0]*Pd_002[1]; double P_110102000=Pd_110[0]*Pd_102[1]; double P_110202000=Pd_110[0]*Pd_202[1]; double P_011000001=Pd_011[0]*Pd_001[2]; double P_011000101=Pd_011[0]*Pd_101[2]; double P_111000001=Pd_111[0]*Pd_001[2]; double P_111000101=Pd_111[0]*Pd_101[2]; double P_211000001=Pd_211[0]*Pd_001[2]; double P_211000101=Pd_211[0]*Pd_101[2]; double P_010001001=Pd_010[0]*Pd_001[1]*Pd_001[2]; double P_010001101=Pd_010[0]*Pd_001[1]*Pd_101[2]; double P_010101001=Pd_010[0]*Pd_101[1]*Pd_001[2]; double P_010101101=Pd_010[0]*Pd_101[1]*Pd_101[2]; double P_110001001=Pd_110[0]*Pd_001[1]*Pd_001[2]; double P_110001101=Pd_110[0]*Pd_001[1]*Pd_101[2]; double P_110101001=Pd_110[0]*Pd_101[1]*Pd_001[2]; double P_110101101=Pd_110[0]*Pd_101[1]*Pd_101[2]; double P_010000002=Pd_010[0]*Pd_002[2]; double P_010000102=Pd_010[0]*Pd_102[2]; double P_010000202=Pd_010[0]*Pd_202[2]; double P_110000002=Pd_110[0]*Pd_002[2]; double P_110000102=Pd_110[0]*Pd_102[2]; double P_110000202=Pd_110[0]*Pd_202[2]; double P_002010000=Pd_002[0]*Pd_010[1]; double P_002110000=Pd_002[0]*Pd_110[1]; double P_102010000=Pd_102[0]*Pd_010[1]; double P_102110000=Pd_102[0]*Pd_110[1]; double P_202010000=Pd_202[0]*Pd_010[1]; double P_202110000=Pd_202[0]*Pd_110[1]; double P_001011000=Pd_001[0]*Pd_011[1]; double P_001111000=Pd_001[0]*Pd_111[1]; double P_001211000=Pd_001[0]*Pd_211[1]; double P_101011000=Pd_101[0]*Pd_011[1]; double P_101111000=Pd_101[0]*Pd_111[1]; double P_101211000=Pd_101[0]*Pd_211[1]; double P_000012000=Pd_012[1]; double P_000112000=Pd_112[1]; double P_000212000=Pd_212[1]; double P_000312000=Pd_312[1]; double P_001010001=Pd_001[0]*Pd_010[1]*Pd_001[2]; double P_001010101=Pd_001[0]*Pd_010[1]*Pd_101[2]; double P_001110001=Pd_001[0]*Pd_110[1]*Pd_001[2]; double P_001110101=Pd_001[0]*Pd_110[1]*Pd_101[2]; double P_101010001=Pd_101[0]*Pd_010[1]*Pd_001[2]; double P_101010101=Pd_101[0]*Pd_010[1]*Pd_101[2]; double P_101110001=Pd_101[0]*Pd_110[1]*Pd_001[2]; double P_101110101=Pd_101[0]*Pd_110[1]*Pd_101[2]; double P_000011001=Pd_011[1]*Pd_001[2]; double P_000011101=Pd_011[1]*Pd_101[2]; double P_000111001=Pd_111[1]*Pd_001[2]; double P_000111101=Pd_111[1]*Pd_101[2]; double P_000211001=Pd_211[1]*Pd_001[2]; double P_000211101=Pd_211[1]*Pd_101[2]; double P_000010002=Pd_010[1]*Pd_002[2]; double P_000010102=Pd_010[1]*Pd_102[2]; double P_000010202=Pd_010[1]*Pd_202[2]; double P_000110002=Pd_110[1]*Pd_002[2]; double P_000110102=Pd_110[1]*Pd_102[2]; double P_000110202=Pd_110[1]*Pd_202[2]; double P_002000010=Pd_002[0]*Pd_010[2]; double P_002000110=Pd_002[0]*Pd_110[2]; double P_102000010=Pd_102[0]*Pd_010[2]; double P_102000110=Pd_102[0]*Pd_110[2]; double P_202000010=Pd_202[0]*Pd_010[2]; double P_202000110=Pd_202[0]*Pd_110[2]; double P_001001010=Pd_001[0]*Pd_001[1]*Pd_010[2]; double P_001001110=Pd_001[0]*Pd_001[1]*Pd_110[2]; double P_001101010=Pd_001[0]*Pd_101[1]*Pd_010[2]; double P_001101110=Pd_001[0]*Pd_101[1]*Pd_110[2]; double P_101001010=Pd_101[0]*Pd_001[1]*Pd_010[2]; double P_101001110=Pd_101[0]*Pd_001[1]*Pd_110[2]; double P_101101010=Pd_101[0]*Pd_101[1]*Pd_010[2]; double P_101101110=Pd_101[0]*Pd_101[1]*Pd_110[2]; double P_000002010=Pd_002[1]*Pd_010[2]; double P_000002110=Pd_002[1]*Pd_110[2]; double P_000102010=Pd_102[1]*Pd_010[2]; double P_000102110=Pd_102[1]*Pd_110[2]; double P_000202010=Pd_202[1]*Pd_010[2]; double P_000202110=Pd_202[1]*Pd_110[2]; double P_001000011=Pd_001[0]*Pd_011[2]; double P_001000111=Pd_001[0]*Pd_111[2]; double P_001000211=Pd_001[0]*Pd_211[2]; double P_101000011=Pd_101[0]*Pd_011[2]; double P_101000111=Pd_101[0]*Pd_111[2]; double P_101000211=Pd_101[0]*Pd_211[2]; double P_000001011=Pd_001[1]*Pd_011[2]; double P_000001111=Pd_001[1]*Pd_111[2]; double P_000001211=Pd_001[1]*Pd_211[2]; double P_000101011=Pd_101[1]*Pd_011[2]; double P_000101111=Pd_101[1]*Pd_111[2]; double P_000101211=Pd_101[1]*Pd_211[2]; double P_000000012=Pd_012[2]; double P_000000112=Pd_112[2]; double P_000000212=Pd_212[2]; double P_000000312=Pd_312[2]; ans_temp[ans_id*3+0]+=Pmtrx[0]*(P_012000000*QR_002000000000+P_112000000*QR_002000000100+P_212000000*QR_002000000200+P_312000000*QR_002000000300); ans_temp[ans_id*3+0]+=Pmtrx[1]*(P_012000000*QR_001001000000+P_112000000*QR_001001000100+P_212000000*QR_001001000200+P_312000000*QR_001001000300); ans_temp[ans_id*3+0]+=Pmtrx[2]*(P_012000000*QR_000002000000+P_112000000*QR_000002000100+P_212000000*QR_000002000200+P_312000000*QR_000002000300); ans_temp[ans_id*3+0]+=Pmtrx[3]*(P_012000000*QR_001000001000+P_112000000*QR_001000001100+P_212000000*QR_001000001200+P_312000000*QR_001000001300); ans_temp[ans_id*3+0]+=Pmtrx[4]*(P_012000000*QR_000001001000+P_112000000*QR_000001001100+P_212000000*QR_000001001200+P_312000000*QR_000001001300); ans_temp[ans_id*3+0]+=Pmtrx[5]*(P_012000000*QR_000000002000+P_112000000*QR_000000002100+P_212000000*QR_000000002200+P_312000000*QR_000000002300); ans_temp[ans_id*3+0]+=Pmtrx[6]*(P_011001000*QR_002000000000+P_011101000*QR_002000000010+P_111001000*QR_002000000100+P_111101000*QR_002000000110+P_211001000*QR_002000000200+P_211101000*QR_002000000210); ans_temp[ans_id*3+0]+=Pmtrx[7]*(P_011001000*QR_001001000000+P_011101000*QR_001001000010+P_111001000*QR_001001000100+P_111101000*QR_001001000110+P_211001000*QR_001001000200+P_211101000*QR_001001000210); ans_temp[ans_id*3+0]+=Pmtrx[8]*(P_011001000*QR_000002000000+P_011101000*QR_000002000010+P_111001000*QR_000002000100+P_111101000*QR_000002000110+P_211001000*QR_000002000200+P_211101000*QR_000002000210); ans_temp[ans_id*3+0]+=Pmtrx[9]*(P_011001000*QR_001000001000+P_011101000*QR_001000001010+P_111001000*QR_001000001100+P_111101000*QR_001000001110+P_211001000*QR_001000001200+P_211101000*QR_001000001210); ans_temp[ans_id*3+0]+=Pmtrx[10]*(P_011001000*QR_000001001000+P_011101000*QR_000001001010+P_111001000*QR_000001001100+P_111101000*QR_000001001110+P_211001000*QR_000001001200+P_211101000*QR_000001001210); ans_temp[ans_id*3+0]+=Pmtrx[11]*(P_011001000*QR_000000002000+P_011101000*QR_000000002010+P_111001000*QR_000000002100+P_111101000*QR_000000002110+P_211001000*QR_000000002200+P_211101000*QR_000000002210); ans_temp[ans_id*3+0]+=Pmtrx[12]*(P_010002000*QR_002000000000+P_010102000*QR_002000000010+P_010202000*QR_002000000020+P_110002000*QR_002000000100+P_110102000*QR_002000000110+P_110202000*QR_002000000120); ans_temp[ans_id*3+0]+=Pmtrx[13]*(P_010002000*QR_001001000000+P_010102000*QR_001001000010+P_010202000*QR_001001000020+P_110002000*QR_001001000100+P_110102000*QR_001001000110+P_110202000*QR_001001000120); ans_temp[ans_id*3+0]+=Pmtrx[14]*(P_010002000*QR_000002000000+P_010102000*QR_000002000010+P_010202000*QR_000002000020+P_110002000*QR_000002000100+P_110102000*QR_000002000110+P_110202000*QR_000002000120); ans_temp[ans_id*3+0]+=Pmtrx[15]*(P_010002000*QR_001000001000+P_010102000*QR_001000001010+P_010202000*QR_001000001020+P_110002000*QR_001000001100+P_110102000*QR_001000001110+P_110202000*QR_001000001120); ans_temp[ans_id*3+0]+=Pmtrx[16]*(P_010002000*QR_000001001000+P_010102000*QR_000001001010+P_010202000*QR_000001001020+P_110002000*QR_000001001100+P_110102000*QR_000001001110+P_110202000*QR_000001001120); ans_temp[ans_id*3+0]+=Pmtrx[17]*(P_010002000*QR_000000002000+P_010102000*QR_000000002010+P_010202000*QR_000000002020+P_110002000*QR_000000002100+P_110102000*QR_000000002110+P_110202000*QR_000000002120); ans_temp[ans_id*3+0]+=Pmtrx[18]*(P_011000001*QR_002000000000+P_011000101*QR_002000000001+P_111000001*QR_002000000100+P_111000101*QR_002000000101+P_211000001*QR_002000000200+P_211000101*QR_002000000201); ans_temp[ans_id*3+0]+=Pmtrx[19]*(P_011000001*QR_001001000000+P_011000101*QR_001001000001+P_111000001*QR_001001000100+P_111000101*QR_001001000101+P_211000001*QR_001001000200+P_211000101*QR_001001000201); ans_temp[ans_id*3+0]+=Pmtrx[20]*(P_011000001*QR_000002000000+P_011000101*QR_000002000001+P_111000001*QR_000002000100+P_111000101*QR_000002000101+P_211000001*QR_000002000200+P_211000101*QR_000002000201); ans_temp[ans_id*3+0]+=Pmtrx[21]*(P_011000001*QR_001000001000+P_011000101*QR_001000001001+P_111000001*QR_001000001100+P_111000101*QR_001000001101+P_211000001*QR_001000001200+P_211000101*QR_001000001201); ans_temp[ans_id*3+0]+=Pmtrx[22]*(P_011000001*QR_000001001000+P_011000101*QR_000001001001+P_111000001*QR_000001001100+P_111000101*QR_000001001101+P_211000001*QR_000001001200+P_211000101*QR_000001001201); ans_temp[ans_id*3+0]+=Pmtrx[23]*(P_011000001*QR_000000002000+P_011000101*QR_000000002001+P_111000001*QR_000000002100+P_111000101*QR_000000002101+P_211000001*QR_000000002200+P_211000101*QR_000000002201); ans_temp[ans_id*3+0]+=Pmtrx[24]*(P_010001001*QR_002000000000+P_010001101*QR_002000000001+P_010101001*QR_002000000010+P_010101101*QR_002000000011+P_110001001*QR_002000000100+P_110001101*QR_002000000101+P_110101001*QR_002000000110+P_110101101*QR_002000000111); ans_temp[ans_id*3+0]+=Pmtrx[25]*(P_010001001*QR_001001000000+P_010001101*QR_001001000001+P_010101001*QR_001001000010+P_010101101*QR_001001000011+P_110001001*QR_001001000100+P_110001101*QR_001001000101+P_110101001*QR_001001000110+P_110101101*QR_001001000111); ans_temp[ans_id*3+0]+=Pmtrx[26]*(P_010001001*QR_000002000000+P_010001101*QR_000002000001+P_010101001*QR_000002000010+P_010101101*QR_000002000011+P_110001001*QR_000002000100+P_110001101*QR_000002000101+P_110101001*QR_000002000110+P_110101101*QR_000002000111); ans_temp[ans_id*3+0]+=Pmtrx[27]*(P_010001001*QR_001000001000+P_010001101*QR_001000001001+P_010101001*QR_001000001010+P_010101101*QR_001000001011+P_110001001*QR_001000001100+P_110001101*QR_001000001101+P_110101001*QR_001000001110+P_110101101*QR_001000001111); ans_temp[ans_id*3+0]+=Pmtrx[28]*(P_010001001*QR_000001001000+P_010001101*QR_000001001001+P_010101001*QR_000001001010+P_010101101*QR_000001001011+P_110001001*QR_000001001100+P_110001101*QR_000001001101+P_110101001*QR_000001001110+P_110101101*QR_000001001111); ans_temp[ans_id*3+0]+=Pmtrx[29]*(P_010001001*QR_000000002000+P_010001101*QR_000000002001+P_010101001*QR_000000002010+P_010101101*QR_000000002011+P_110001001*QR_000000002100+P_110001101*QR_000000002101+P_110101001*QR_000000002110+P_110101101*QR_000000002111); ans_temp[ans_id*3+0]+=Pmtrx[30]*(P_010000002*QR_002000000000+P_010000102*QR_002000000001+P_010000202*QR_002000000002+P_110000002*QR_002000000100+P_110000102*QR_002000000101+P_110000202*QR_002000000102); ans_temp[ans_id*3+0]+=Pmtrx[31]*(P_010000002*QR_001001000000+P_010000102*QR_001001000001+P_010000202*QR_001001000002+P_110000002*QR_001001000100+P_110000102*QR_001001000101+P_110000202*QR_001001000102); ans_temp[ans_id*3+0]+=Pmtrx[32]*(P_010000002*QR_000002000000+P_010000102*QR_000002000001+P_010000202*QR_000002000002+P_110000002*QR_000002000100+P_110000102*QR_000002000101+P_110000202*QR_000002000102); ans_temp[ans_id*3+0]+=Pmtrx[33]*(P_010000002*QR_001000001000+P_010000102*QR_001000001001+P_010000202*QR_001000001002+P_110000002*QR_001000001100+P_110000102*QR_001000001101+P_110000202*QR_001000001102); ans_temp[ans_id*3+0]+=Pmtrx[34]*(P_010000002*QR_000001001000+P_010000102*QR_000001001001+P_010000202*QR_000001001002+P_110000002*QR_000001001100+P_110000102*QR_000001001101+P_110000202*QR_000001001102); ans_temp[ans_id*3+0]+=Pmtrx[35]*(P_010000002*QR_000000002000+P_010000102*QR_000000002001+P_010000202*QR_000000002002+P_110000002*QR_000000002100+P_110000102*QR_000000002101+P_110000202*QR_000000002102); ans_temp[ans_id*3+1]+=Pmtrx[0]*(P_002010000*QR_002000000000+P_002110000*QR_002000000010+P_102010000*QR_002000000100+P_102110000*QR_002000000110+P_202010000*QR_002000000200+P_202110000*QR_002000000210); ans_temp[ans_id*3+1]+=Pmtrx[1]*(P_002010000*QR_001001000000+P_002110000*QR_001001000010+P_102010000*QR_001001000100+P_102110000*QR_001001000110+P_202010000*QR_001001000200+P_202110000*QR_001001000210); ans_temp[ans_id*3+1]+=Pmtrx[2]*(P_002010000*QR_000002000000+P_002110000*QR_000002000010+P_102010000*QR_000002000100+P_102110000*QR_000002000110+P_202010000*QR_000002000200+P_202110000*QR_000002000210); ans_temp[ans_id*3+1]+=Pmtrx[3]*(P_002010000*QR_001000001000+P_002110000*QR_001000001010+P_102010000*QR_001000001100+P_102110000*QR_001000001110+P_202010000*QR_001000001200+P_202110000*QR_001000001210); ans_temp[ans_id*3+1]+=Pmtrx[4]*(P_002010000*QR_000001001000+P_002110000*QR_000001001010+P_102010000*QR_000001001100+P_102110000*QR_000001001110+P_202010000*QR_000001001200+P_202110000*QR_000001001210); ans_temp[ans_id*3+1]+=Pmtrx[5]*(P_002010000*QR_000000002000+P_002110000*QR_000000002010+P_102010000*QR_000000002100+P_102110000*QR_000000002110+P_202010000*QR_000000002200+P_202110000*QR_000000002210); ans_temp[ans_id*3+1]+=Pmtrx[6]*(P_001011000*QR_002000000000+P_001111000*QR_002000000010+P_001211000*QR_002000000020+P_101011000*QR_002000000100+P_101111000*QR_002000000110+P_101211000*QR_002000000120); ans_temp[ans_id*3+1]+=Pmtrx[7]*(P_001011000*QR_001001000000+P_001111000*QR_001001000010+P_001211000*QR_001001000020+P_101011000*QR_001001000100+P_101111000*QR_001001000110+P_101211000*QR_001001000120); ans_temp[ans_id*3+1]+=Pmtrx[8]*(P_001011000*QR_000002000000+P_001111000*QR_000002000010+P_001211000*QR_000002000020+P_101011000*QR_000002000100+P_101111000*QR_000002000110+P_101211000*QR_000002000120); ans_temp[ans_id*3+1]+=Pmtrx[9]*(P_001011000*QR_001000001000+P_001111000*QR_001000001010+P_001211000*QR_001000001020+P_101011000*QR_001000001100+P_101111000*QR_001000001110+P_101211000*QR_001000001120); ans_temp[ans_id*3+1]+=Pmtrx[10]*(P_001011000*QR_000001001000+P_001111000*QR_000001001010+P_001211000*QR_000001001020+P_101011000*QR_000001001100+P_101111000*QR_000001001110+P_101211000*QR_000001001120); ans_temp[ans_id*3+1]+=Pmtrx[11]*(P_001011000*QR_000000002000+P_001111000*QR_000000002010+P_001211000*QR_000000002020+P_101011000*QR_000000002100+P_101111000*QR_000000002110+P_101211000*QR_000000002120); ans_temp[ans_id*3+1]+=Pmtrx[12]*(P_000012000*QR_002000000000+P_000112000*QR_002000000010+P_000212000*QR_002000000020+P_000312000*QR_002000000030); ans_temp[ans_id*3+1]+=Pmtrx[13]*(P_000012000*QR_001001000000+P_000112000*QR_001001000010+P_000212000*QR_001001000020+P_000312000*QR_001001000030); ans_temp[ans_id*3+1]+=Pmtrx[14]*(P_000012000*QR_000002000000+P_000112000*QR_000002000010+P_000212000*QR_000002000020+P_000312000*QR_000002000030); ans_temp[ans_id*3+1]+=Pmtrx[15]*(P_000012000*QR_001000001000+P_000112000*QR_001000001010+P_000212000*QR_001000001020+P_000312000*QR_001000001030); ans_temp[ans_id*3+1]+=Pmtrx[16]*(P_000012000*QR_000001001000+P_000112000*QR_000001001010+P_000212000*QR_000001001020+P_000312000*QR_000001001030); ans_temp[ans_id*3+1]+=Pmtrx[17]*(P_000012000*QR_000000002000+P_000112000*QR_000000002010+P_000212000*QR_000000002020+P_000312000*QR_000000002030); ans_temp[ans_id*3+1]+=Pmtrx[18]*(P_001010001*QR_002000000000+P_001010101*QR_002000000001+P_001110001*QR_002000000010+P_001110101*QR_002000000011+P_101010001*QR_002000000100+P_101010101*QR_002000000101+P_101110001*QR_002000000110+P_101110101*QR_002000000111); ans_temp[ans_id*3+1]+=Pmtrx[19]*(P_001010001*QR_001001000000+P_001010101*QR_001001000001+P_001110001*QR_001001000010+P_001110101*QR_001001000011+P_101010001*QR_001001000100+P_101010101*QR_001001000101+P_101110001*QR_001001000110+P_101110101*QR_001001000111); ans_temp[ans_id*3+1]+=Pmtrx[20]*(P_001010001*QR_000002000000+P_001010101*QR_000002000001+P_001110001*QR_000002000010+P_001110101*QR_000002000011+P_101010001*QR_000002000100+P_101010101*QR_000002000101+P_101110001*QR_000002000110+P_101110101*QR_000002000111); ans_temp[ans_id*3+1]+=Pmtrx[21]*(P_001010001*QR_001000001000+P_001010101*QR_001000001001+P_001110001*QR_001000001010+P_001110101*QR_001000001011+P_101010001*QR_001000001100+P_101010101*QR_001000001101+P_101110001*QR_001000001110+P_101110101*QR_001000001111); ans_temp[ans_id*3+1]+=Pmtrx[22]*(P_001010001*QR_000001001000+P_001010101*QR_000001001001+P_001110001*QR_000001001010+P_001110101*QR_000001001011+P_101010001*QR_000001001100+P_101010101*QR_000001001101+P_101110001*QR_000001001110+P_101110101*QR_000001001111); ans_temp[ans_id*3+1]+=Pmtrx[23]*(P_001010001*QR_000000002000+P_001010101*QR_000000002001+P_001110001*QR_000000002010+P_001110101*QR_000000002011+P_101010001*QR_000000002100+P_101010101*QR_000000002101+P_101110001*QR_000000002110+P_101110101*QR_000000002111); ans_temp[ans_id*3+1]+=Pmtrx[24]*(P_000011001*QR_002000000000+P_000011101*QR_002000000001+P_000111001*QR_002000000010+P_000111101*QR_002000000011+P_000211001*QR_002000000020+P_000211101*QR_002000000021); ans_temp[ans_id*3+1]+=Pmtrx[25]*(P_000011001*QR_001001000000+P_000011101*QR_001001000001+P_000111001*QR_001001000010+P_000111101*QR_001001000011+P_000211001*QR_001001000020+P_000211101*QR_001001000021); ans_temp[ans_id*3+1]+=Pmtrx[26]*(P_000011001*QR_000002000000+P_000011101*QR_000002000001+P_000111001*QR_000002000010+P_000111101*QR_000002000011+P_000211001*QR_000002000020+P_000211101*QR_000002000021); ans_temp[ans_id*3+1]+=Pmtrx[27]*(P_000011001*QR_001000001000+P_000011101*QR_001000001001+P_000111001*QR_001000001010+P_000111101*QR_001000001011+P_000211001*QR_001000001020+P_000211101*QR_001000001021); ans_temp[ans_id*3+1]+=Pmtrx[28]*(P_000011001*QR_000001001000+P_000011101*QR_000001001001+P_000111001*QR_000001001010+P_000111101*QR_000001001011+P_000211001*QR_000001001020+P_000211101*QR_000001001021); ans_temp[ans_id*3+1]+=Pmtrx[29]*(P_000011001*QR_000000002000+P_000011101*QR_000000002001+P_000111001*QR_000000002010+P_000111101*QR_000000002011+P_000211001*QR_000000002020+P_000211101*QR_000000002021); ans_temp[ans_id*3+1]+=Pmtrx[30]*(P_000010002*QR_002000000000+P_000010102*QR_002000000001+P_000010202*QR_002000000002+P_000110002*QR_002000000010+P_000110102*QR_002000000011+P_000110202*QR_002000000012); ans_temp[ans_id*3+1]+=Pmtrx[31]*(P_000010002*QR_001001000000+P_000010102*QR_001001000001+P_000010202*QR_001001000002+P_000110002*QR_001001000010+P_000110102*QR_001001000011+P_000110202*QR_001001000012); ans_temp[ans_id*3+1]+=Pmtrx[32]*(P_000010002*QR_000002000000+P_000010102*QR_000002000001+P_000010202*QR_000002000002+P_000110002*QR_000002000010+P_000110102*QR_000002000011+P_000110202*QR_000002000012); ans_temp[ans_id*3+1]+=Pmtrx[33]*(P_000010002*QR_001000001000+P_000010102*QR_001000001001+P_000010202*QR_001000001002+P_000110002*QR_001000001010+P_000110102*QR_001000001011+P_000110202*QR_001000001012); ans_temp[ans_id*3+1]+=Pmtrx[34]*(P_000010002*QR_000001001000+P_000010102*QR_000001001001+P_000010202*QR_000001001002+P_000110002*QR_000001001010+P_000110102*QR_000001001011+P_000110202*QR_000001001012); ans_temp[ans_id*3+1]+=Pmtrx[35]*(P_000010002*QR_000000002000+P_000010102*QR_000000002001+P_000010202*QR_000000002002+P_000110002*QR_000000002010+P_000110102*QR_000000002011+P_000110202*QR_000000002012); ans_temp[ans_id*3+2]+=Pmtrx[0]*(P_002000010*QR_002000000000+P_002000110*QR_002000000001+P_102000010*QR_002000000100+P_102000110*QR_002000000101+P_202000010*QR_002000000200+P_202000110*QR_002000000201); ans_temp[ans_id*3+2]+=Pmtrx[1]*(P_002000010*QR_001001000000+P_002000110*QR_001001000001+P_102000010*QR_001001000100+P_102000110*QR_001001000101+P_202000010*QR_001001000200+P_202000110*QR_001001000201); ans_temp[ans_id*3+2]+=Pmtrx[2]*(P_002000010*QR_000002000000+P_002000110*QR_000002000001+P_102000010*QR_000002000100+P_102000110*QR_000002000101+P_202000010*QR_000002000200+P_202000110*QR_000002000201); ans_temp[ans_id*3+2]+=Pmtrx[3]*(P_002000010*QR_001000001000+P_002000110*QR_001000001001+P_102000010*QR_001000001100+P_102000110*QR_001000001101+P_202000010*QR_001000001200+P_202000110*QR_001000001201); ans_temp[ans_id*3+2]+=Pmtrx[4]*(P_002000010*QR_000001001000+P_002000110*QR_000001001001+P_102000010*QR_000001001100+P_102000110*QR_000001001101+P_202000010*QR_000001001200+P_202000110*QR_000001001201); ans_temp[ans_id*3+2]+=Pmtrx[5]*(P_002000010*QR_000000002000+P_002000110*QR_000000002001+P_102000010*QR_000000002100+P_102000110*QR_000000002101+P_202000010*QR_000000002200+P_202000110*QR_000000002201); ans_temp[ans_id*3+2]+=Pmtrx[6]*(P_001001010*QR_002000000000+P_001001110*QR_002000000001+P_001101010*QR_002000000010+P_001101110*QR_002000000011+P_101001010*QR_002000000100+P_101001110*QR_002000000101+P_101101010*QR_002000000110+P_101101110*QR_002000000111); ans_temp[ans_id*3+2]+=Pmtrx[7]*(P_001001010*QR_001001000000+P_001001110*QR_001001000001+P_001101010*QR_001001000010+P_001101110*QR_001001000011+P_101001010*QR_001001000100+P_101001110*QR_001001000101+P_101101010*QR_001001000110+P_101101110*QR_001001000111); ans_temp[ans_id*3+2]+=Pmtrx[8]*(P_001001010*QR_000002000000+P_001001110*QR_000002000001+P_001101010*QR_000002000010+P_001101110*QR_000002000011+P_101001010*QR_000002000100+P_101001110*QR_000002000101+P_101101010*QR_000002000110+P_101101110*QR_000002000111); ans_temp[ans_id*3+2]+=Pmtrx[9]*(P_001001010*QR_001000001000+P_001001110*QR_001000001001+P_001101010*QR_001000001010+P_001101110*QR_001000001011+P_101001010*QR_001000001100+P_101001110*QR_001000001101+P_101101010*QR_001000001110+P_101101110*QR_001000001111); ans_temp[ans_id*3+2]+=Pmtrx[10]*(P_001001010*QR_000001001000+P_001001110*QR_000001001001+P_001101010*QR_000001001010+P_001101110*QR_000001001011+P_101001010*QR_000001001100+P_101001110*QR_000001001101+P_101101010*QR_000001001110+P_101101110*QR_000001001111); ans_temp[ans_id*3+2]+=Pmtrx[11]*(P_001001010*QR_000000002000+P_001001110*QR_000000002001+P_001101010*QR_000000002010+P_001101110*QR_000000002011+P_101001010*QR_000000002100+P_101001110*QR_000000002101+P_101101010*QR_000000002110+P_101101110*QR_000000002111); ans_temp[ans_id*3+2]+=Pmtrx[12]*(P_000002010*QR_002000000000+P_000002110*QR_002000000001+P_000102010*QR_002000000010+P_000102110*QR_002000000011+P_000202010*QR_002000000020+P_000202110*QR_002000000021); ans_temp[ans_id*3+2]+=Pmtrx[13]*(P_000002010*QR_001001000000+P_000002110*QR_001001000001+P_000102010*QR_001001000010+P_000102110*QR_001001000011+P_000202010*QR_001001000020+P_000202110*QR_001001000021); ans_temp[ans_id*3+2]+=Pmtrx[14]*(P_000002010*QR_000002000000+P_000002110*QR_000002000001+P_000102010*QR_000002000010+P_000102110*QR_000002000011+P_000202010*QR_000002000020+P_000202110*QR_000002000021); ans_temp[ans_id*3+2]+=Pmtrx[15]*(P_000002010*QR_001000001000+P_000002110*QR_001000001001+P_000102010*QR_001000001010+P_000102110*QR_001000001011+P_000202010*QR_001000001020+P_000202110*QR_001000001021); ans_temp[ans_id*3+2]+=Pmtrx[16]*(P_000002010*QR_000001001000+P_000002110*QR_000001001001+P_000102010*QR_000001001010+P_000102110*QR_000001001011+P_000202010*QR_000001001020+P_000202110*QR_000001001021); ans_temp[ans_id*3+2]+=Pmtrx[17]*(P_000002010*QR_000000002000+P_000002110*QR_000000002001+P_000102010*QR_000000002010+P_000102110*QR_000000002011+P_000202010*QR_000000002020+P_000202110*QR_000000002021); ans_temp[ans_id*3+2]+=Pmtrx[18]*(P_001000011*QR_002000000000+P_001000111*QR_002000000001+P_001000211*QR_002000000002+P_101000011*QR_002000000100+P_101000111*QR_002000000101+P_101000211*QR_002000000102); ans_temp[ans_id*3+2]+=Pmtrx[19]*(P_001000011*QR_001001000000+P_001000111*QR_001001000001+P_001000211*QR_001001000002+P_101000011*QR_001001000100+P_101000111*QR_001001000101+P_101000211*QR_001001000102); ans_temp[ans_id*3+2]+=Pmtrx[20]*(P_001000011*QR_000002000000+P_001000111*QR_000002000001+P_001000211*QR_000002000002+P_101000011*QR_000002000100+P_101000111*QR_000002000101+P_101000211*QR_000002000102); ans_temp[ans_id*3+2]+=Pmtrx[21]*(P_001000011*QR_001000001000+P_001000111*QR_001000001001+P_001000211*QR_001000001002+P_101000011*QR_001000001100+P_101000111*QR_001000001101+P_101000211*QR_001000001102); ans_temp[ans_id*3+2]+=Pmtrx[22]*(P_001000011*QR_000001001000+P_001000111*QR_000001001001+P_001000211*QR_000001001002+P_101000011*QR_000001001100+P_101000111*QR_000001001101+P_101000211*QR_000001001102); ans_temp[ans_id*3+2]+=Pmtrx[23]*(P_001000011*QR_000000002000+P_001000111*QR_000000002001+P_001000211*QR_000000002002+P_101000011*QR_000000002100+P_101000111*QR_000000002101+P_101000211*QR_000000002102); ans_temp[ans_id*3+2]+=Pmtrx[24]*(P_000001011*QR_002000000000+P_000001111*QR_002000000001+P_000001211*QR_002000000002+P_000101011*QR_002000000010+P_000101111*QR_002000000011+P_000101211*QR_002000000012); ans_temp[ans_id*3+2]+=Pmtrx[25]*(P_000001011*QR_001001000000+P_000001111*QR_001001000001+P_000001211*QR_001001000002+P_000101011*QR_001001000010+P_000101111*QR_001001000011+P_000101211*QR_001001000012); ans_temp[ans_id*3+2]+=Pmtrx[26]*(P_000001011*QR_000002000000+P_000001111*QR_000002000001+P_000001211*QR_000002000002+P_000101011*QR_000002000010+P_000101111*QR_000002000011+P_000101211*QR_000002000012); ans_temp[ans_id*3+2]+=Pmtrx[27]*(P_000001011*QR_001000001000+P_000001111*QR_001000001001+P_000001211*QR_001000001002+P_000101011*QR_001000001010+P_000101111*QR_001000001011+P_000101211*QR_001000001012); ans_temp[ans_id*3+2]+=Pmtrx[28]*(P_000001011*QR_000001001000+P_000001111*QR_000001001001+P_000001211*QR_000001001002+P_000101011*QR_000001001010+P_000101111*QR_000001001011+P_000101211*QR_000001001012); ans_temp[ans_id*3+2]+=Pmtrx[29]*(P_000001011*QR_000000002000+P_000001111*QR_000000002001+P_000001211*QR_000000002002+P_000101011*QR_000000002010+P_000101111*QR_000000002011+P_000101211*QR_000000002012); ans_temp[ans_id*3+2]+=Pmtrx[30]*(P_000000012*QR_002000000000+P_000000112*QR_002000000001+P_000000212*QR_002000000002+P_000000312*QR_002000000003); ans_temp[ans_id*3+2]+=Pmtrx[31]*(P_000000012*QR_001001000000+P_000000112*QR_001001000001+P_000000212*QR_001001000002+P_000000312*QR_001001000003); ans_temp[ans_id*3+2]+=Pmtrx[32]*(P_000000012*QR_000002000000+P_000000112*QR_000002000001+P_000000212*QR_000002000002+P_000000312*QR_000002000003); ans_temp[ans_id*3+2]+=Pmtrx[33]*(P_000000012*QR_001000001000+P_000000112*QR_001000001001+P_000000212*QR_001000001002+P_000000312*QR_001000001003); ans_temp[ans_id*3+2]+=Pmtrx[34]*(P_000000012*QR_000001001000+P_000000112*QR_000001001001+P_000000212*QR_000001001002+P_000000312*QR_000001001003); ans_temp[ans_id*3+2]+=Pmtrx[35]*(P_000000012*QR_000000002000+P_000000112*QR_000000002001+P_000000212*QR_000000002002+P_000000312*QR_000000002003); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<3;ians++){ ans_temp[tId_x*3+ians]+=ans_temp[(tId_x+num_thread)*3+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<3;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*3+ians]=ans_temp[(tId_x)*3+ians]; } } } } } __global__ void MD_Kp_dpsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[18]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<3;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_010[3]; temp_int2=tex1Dfetch(tex_PA,jj*3+0); Pd_010[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+1); Pd_010[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+2); Pd_010[2]=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Pd_101[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } double P_021000000=Pd_021[0]; double P_121000000=Pd_121[0]; double P_221000000=Pd_221[0]; double P_321000000=Pd_321[0]; double P_020001000=Pd_020[0]*Pd_001[1]; double P_020101000=Pd_020[0]*Pd_101[1]; double P_120001000=Pd_120[0]*Pd_001[1]; double P_120101000=Pd_120[0]*Pd_101[1]; double P_220001000=Pd_220[0]*Pd_001[1]; double P_220101000=Pd_220[0]*Pd_101[1]; double P_020000001=Pd_020[0]*Pd_001[2]; double P_020000101=Pd_020[0]*Pd_101[2]; double P_120000001=Pd_120[0]*Pd_001[2]; double P_120000101=Pd_120[0]*Pd_101[2]; double P_220000001=Pd_220[0]*Pd_001[2]; double P_220000101=Pd_220[0]*Pd_101[2]; double P_011010000=Pd_011[0]*Pd_010[1]; double P_011110000=Pd_011[0]*Pd_110[1]; double P_111010000=Pd_111[0]*Pd_010[1]; double P_111110000=Pd_111[0]*Pd_110[1]; double P_211010000=Pd_211[0]*Pd_010[1]; double P_211110000=Pd_211[0]*Pd_110[1]; double P_010011000=Pd_010[0]*Pd_011[1]; double P_010111000=Pd_010[0]*Pd_111[1]; double P_010211000=Pd_010[0]*Pd_211[1]; double P_110011000=Pd_110[0]*Pd_011[1]; double P_110111000=Pd_110[0]*Pd_111[1]; double P_110211000=Pd_110[0]*Pd_211[1]; double P_010010001=Pd_010[0]*Pd_010[1]*Pd_001[2]; double P_010010101=Pd_010[0]*Pd_010[1]*Pd_101[2]; double P_010110001=Pd_010[0]*Pd_110[1]*Pd_001[2]; double P_010110101=Pd_010[0]*Pd_110[1]*Pd_101[2]; double P_110010001=Pd_110[0]*Pd_010[1]*Pd_001[2]; double P_110010101=Pd_110[0]*Pd_010[1]*Pd_101[2]; double P_110110001=Pd_110[0]*Pd_110[1]*Pd_001[2]; double P_110110101=Pd_110[0]*Pd_110[1]*Pd_101[2]; double P_001020000=Pd_001[0]*Pd_020[1]; double P_001120000=Pd_001[0]*Pd_120[1]; double P_001220000=Pd_001[0]*Pd_220[1]; double P_101020000=Pd_101[0]*Pd_020[1]; double P_101120000=Pd_101[0]*Pd_120[1]; double P_101220000=Pd_101[0]*Pd_220[1]; double P_000021000=Pd_021[1]; double P_000121000=Pd_121[1]; double P_000221000=Pd_221[1]; double P_000321000=Pd_321[1]; double P_000020001=Pd_020[1]*Pd_001[2]; double P_000020101=Pd_020[1]*Pd_101[2]; double P_000120001=Pd_120[1]*Pd_001[2]; double P_000120101=Pd_120[1]*Pd_101[2]; double P_000220001=Pd_220[1]*Pd_001[2]; double P_000220101=Pd_220[1]*Pd_101[2]; double P_011000010=Pd_011[0]*Pd_010[2]; double P_011000110=Pd_011[0]*Pd_110[2]; double P_111000010=Pd_111[0]*Pd_010[2]; double P_111000110=Pd_111[0]*Pd_110[2]; double P_211000010=Pd_211[0]*Pd_010[2]; double P_211000110=Pd_211[0]*Pd_110[2]; double P_010001010=Pd_010[0]*Pd_001[1]*Pd_010[2]; double P_010001110=Pd_010[0]*Pd_001[1]*Pd_110[2]; double P_010101010=Pd_010[0]*Pd_101[1]*Pd_010[2]; double P_010101110=Pd_010[0]*Pd_101[1]*Pd_110[2]; double P_110001010=Pd_110[0]*Pd_001[1]*Pd_010[2]; double P_110001110=Pd_110[0]*Pd_001[1]*Pd_110[2]; double P_110101010=Pd_110[0]*Pd_101[1]*Pd_010[2]; double P_110101110=Pd_110[0]*Pd_101[1]*Pd_110[2]; double P_010000011=Pd_010[0]*Pd_011[2]; double P_010000111=Pd_010[0]*Pd_111[2]; double P_010000211=Pd_010[0]*Pd_211[2]; double P_110000011=Pd_110[0]*Pd_011[2]; double P_110000111=Pd_110[0]*Pd_111[2]; double P_110000211=Pd_110[0]*Pd_211[2]; double P_001010010=Pd_001[0]*Pd_010[1]*Pd_010[2]; double P_001010110=Pd_001[0]*Pd_010[1]*Pd_110[2]; double P_001110010=Pd_001[0]*Pd_110[1]*Pd_010[2]; double P_001110110=Pd_001[0]*Pd_110[1]*Pd_110[2]; double P_101010010=Pd_101[0]*Pd_010[1]*Pd_010[2]; double P_101010110=Pd_101[0]*Pd_010[1]*Pd_110[2]; double P_101110010=Pd_101[0]*Pd_110[1]*Pd_010[2]; double P_101110110=Pd_101[0]*Pd_110[1]*Pd_110[2]; double P_000011010=Pd_011[1]*Pd_010[2]; double P_000011110=Pd_011[1]*Pd_110[2]; double P_000111010=Pd_111[1]*Pd_010[2]; double P_000111110=Pd_111[1]*Pd_110[2]; double P_000211010=Pd_211[1]*Pd_010[2]; double P_000211110=Pd_211[1]*Pd_110[2]; double P_000010011=Pd_010[1]*Pd_011[2]; double P_000010111=Pd_010[1]*Pd_111[2]; double P_000010211=Pd_010[1]*Pd_211[2]; double P_000110011=Pd_110[1]*Pd_011[2]; double P_000110111=Pd_110[1]*Pd_111[2]; double P_000110211=Pd_110[1]*Pd_211[2]; double P_001000020=Pd_001[0]*Pd_020[2]; double P_001000120=Pd_001[0]*Pd_120[2]; double P_001000220=Pd_001[0]*Pd_220[2]; double P_101000020=Pd_101[0]*Pd_020[2]; double P_101000120=Pd_101[0]*Pd_120[2]; double P_101000220=Pd_101[0]*Pd_220[2]; double P_000001020=Pd_001[1]*Pd_020[2]; double P_000001120=Pd_001[1]*Pd_120[2]; double P_000001220=Pd_001[1]*Pd_220[2]; double P_000101020=Pd_101[1]*Pd_020[2]; double P_000101120=Pd_101[1]*Pd_120[2]; double P_000101220=Pd_101[1]*Pd_220[2]; double P_000000021=Pd_021[2]; double P_000000121=Pd_121[2]; double P_000000221=Pd_221[2]; double P_000000321=Pd_321[2]; double PR_021000000000=P_021000000*R_000[0]+-1*P_121000000*R_100[0]+P_221000000*R_200[0]+-1*P_321000000*R_300[0]; double PR_020001000000=P_020001000*R_000[0]+-1*P_020101000*R_010[0]+-1*P_120001000*R_100[0]+P_120101000*R_110[0]+P_220001000*R_200[0]+-1*P_220101000*R_210[0]; double PR_020000001000=P_020000001*R_000[0]+-1*P_020000101*R_001[0]+-1*P_120000001*R_100[0]+P_120000101*R_101[0]+P_220000001*R_200[0]+-1*P_220000101*R_201[0]; double PR_011010000000=P_011010000*R_000[0]+-1*P_011110000*R_010[0]+-1*P_111010000*R_100[0]+P_111110000*R_110[0]+P_211010000*R_200[0]+-1*P_211110000*R_210[0]; double PR_010011000000=P_010011000*R_000[0]+-1*P_010111000*R_010[0]+P_010211000*R_020[0]+-1*P_110011000*R_100[0]+P_110111000*R_110[0]+-1*P_110211000*R_120[0]; double PR_010010001000=P_010010001*R_000[0]+-1*P_010010101*R_001[0]+-1*P_010110001*R_010[0]+P_010110101*R_011[0]+-1*P_110010001*R_100[0]+P_110010101*R_101[0]+P_110110001*R_110[0]+-1*P_110110101*R_111[0]; double PR_001020000000=P_001020000*R_000[0]+-1*P_001120000*R_010[0]+P_001220000*R_020[0]+-1*P_101020000*R_100[0]+P_101120000*R_110[0]+-1*P_101220000*R_120[0]; double PR_000021000000=P_000021000*R_000[0]+-1*P_000121000*R_010[0]+P_000221000*R_020[0]+-1*P_000321000*R_030[0]; double PR_000020001000=P_000020001*R_000[0]+-1*P_000020101*R_001[0]+-1*P_000120001*R_010[0]+P_000120101*R_011[0]+P_000220001*R_020[0]+-1*P_000220101*R_021[0]; double PR_011000010000=P_011000010*R_000[0]+-1*P_011000110*R_001[0]+-1*P_111000010*R_100[0]+P_111000110*R_101[0]+P_211000010*R_200[0]+-1*P_211000110*R_201[0]; double PR_010001010000=P_010001010*R_000[0]+-1*P_010001110*R_001[0]+-1*P_010101010*R_010[0]+P_010101110*R_011[0]+-1*P_110001010*R_100[0]+P_110001110*R_101[0]+P_110101010*R_110[0]+-1*P_110101110*R_111[0]; double PR_010000011000=P_010000011*R_000[0]+-1*P_010000111*R_001[0]+P_010000211*R_002[0]+-1*P_110000011*R_100[0]+P_110000111*R_101[0]+-1*P_110000211*R_102[0]; double PR_001010010000=P_001010010*R_000[0]+-1*P_001010110*R_001[0]+-1*P_001110010*R_010[0]+P_001110110*R_011[0]+-1*P_101010010*R_100[0]+P_101010110*R_101[0]+P_101110010*R_110[0]+-1*P_101110110*R_111[0]; double PR_000011010000=P_000011010*R_000[0]+-1*P_000011110*R_001[0]+-1*P_000111010*R_010[0]+P_000111110*R_011[0]+P_000211010*R_020[0]+-1*P_000211110*R_021[0]; double PR_000010011000=P_000010011*R_000[0]+-1*P_000010111*R_001[0]+P_000010211*R_002[0]+-1*P_000110011*R_010[0]+P_000110111*R_011[0]+-1*P_000110211*R_012[0]; double PR_001000020000=P_001000020*R_000[0]+-1*P_001000120*R_001[0]+P_001000220*R_002[0]+-1*P_101000020*R_100[0]+P_101000120*R_101[0]+-1*P_101000220*R_102[0]; double PR_000001020000=P_000001020*R_000[0]+-1*P_000001120*R_001[0]+P_000001220*R_002[0]+-1*P_000101020*R_010[0]+P_000101120*R_011[0]+-1*P_000101220*R_012[0]; double PR_000000021000=P_000000021*R_000[0]+-1*P_000000121*R_001[0]+P_000000221*R_002[0]+-1*P_000000321*R_003[0]; double PR_021000000001=P_021000000*R_001[0]+-1*P_121000000*R_101[0]+P_221000000*R_201[0]+-1*P_321000000*R_301[0]; double PR_020001000001=P_020001000*R_001[0]+-1*P_020101000*R_011[0]+-1*P_120001000*R_101[0]+P_120101000*R_111[0]+P_220001000*R_201[0]+-1*P_220101000*R_211[0]; double PR_020000001001=P_020000001*R_001[0]+-1*P_020000101*R_002[0]+-1*P_120000001*R_101[0]+P_120000101*R_102[0]+P_220000001*R_201[0]+-1*P_220000101*R_202[0]; double PR_011010000001=P_011010000*R_001[0]+-1*P_011110000*R_011[0]+-1*P_111010000*R_101[0]+P_111110000*R_111[0]+P_211010000*R_201[0]+-1*P_211110000*R_211[0]; double PR_010011000001=P_010011000*R_001[0]+-1*P_010111000*R_011[0]+P_010211000*R_021[0]+-1*P_110011000*R_101[0]+P_110111000*R_111[0]+-1*P_110211000*R_121[0]; double PR_010010001001=P_010010001*R_001[0]+-1*P_010010101*R_002[0]+-1*P_010110001*R_011[0]+P_010110101*R_012[0]+-1*P_110010001*R_101[0]+P_110010101*R_102[0]+P_110110001*R_111[0]+-1*P_110110101*R_112[0]; double PR_001020000001=P_001020000*R_001[0]+-1*P_001120000*R_011[0]+P_001220000*R_021[0]+-1*P_101020000*R_101[0]+P_101120000*R_111[0]+-1*P_101220000*R_121[0]; double PR_000021000001=P_000021000*R_001[0]+-1*P_000121000*R_011[0]+P_000221000*R_021[0]+-1*P_000321000*R_031[0]; double PR_000020001001=P_000020001*R_001[0]+-1*P_000020101*R_002[0]+-1*P_000120001*R_011[0]+P_000120101*R_012[0]+P_000220001*R_021[0]+-1*P_000220101*R_022[0]; double PR_011000010001=P_011000010*R_001[0]+-1*P_011000110*R_002[0]+-1*P_111000010*R_101[0]+P_111000110*R_102[0]+P_211000010*R_201[0]+-1*P_211000110*R_202[0]; double PR_010001010001=P_010001010*R_001[0]+-1*P_010001110*R_002[0]+-1*P_010101010*R_011[0]+P_010101110*R_012[0]+-1*P_110001010*R_101[0]+P_110001110*R_102[0]+P_110101010*R_111[0]+-1*P_110101110*R_112[0]; double PR_010000011001=P_010000011*R_001[0]+-1*P_010000111*R_002[0]+P_010000211*R_003[0]+-1*P_110000011*R_101[0]+P_110000111*R_102[0]+-1*P_110000211*R_103[0]; double PR_001010010001=P_001010010*R_001[0]+-1*P_001010110*R_002[0]+-1*P_001110010*R_011[0]+P_001110110*R_012[0]+-1*P_101010010*R_101[0]+P_101010110*R_102[0]+P_101110010*R_111[0]+-1*P_101110110*R_112[0]; double PR_000011010001=P_000011010*R_001[0]+-1*P_000011110*R_002[0]+-1*P_000111010*R_011[0]+P_000111110*R_012[0]+P_000211010*R_021[0]+-1*P_000211110*R_022[0]; double PR_000010011001=P_000010011*R_001[0]+-1*P_000010111*R_002[0]+P_000010211*R_003[0]+-1*P_000110011*R_011[0]+P_000110111*R_012[0]+-1*P_000110211*R_013[0]; double PR_001000020001=P_001000020*R_001[0]+-1*P_001000120*R_002[0]+P_001000220*R_003[0]+-1*P_101000020*R_101[0]+P_101000120*R_102[0]+-1*P_101000220*R_103[0]; double PR_000001020001=P_000001020*R_001[0]+-1*P_000001120*R_002[0]+P_000001220*R_003[0]+-1*P_000101020*R_011[0]+P_000101120*R_012[0]+-1*P_000101220*R_013[0]; double PR_000000021001=P_000000021*R_001[0]+-1*P_000000121*R_002[0]+P_000000221*R_003[0]+-1*P_000000321*R_004[0]; double PR_021000000010=P_021000000*R_010[0]+-1*P_121000000*R_110[0]+P_221000000*R_210[0]+-1*P_321000000*R_310[0]; double PR_020001000010=P_020001000*R_010[0]+-1*P_020101000*R_020[0]+-1*P_120001000*R_110[0]+P_120101000*R_120[0]+P_220001000*R_210[0]+-1*P_220101000*R_220[0]; double PR_020000001010=P_020000001*R_010[0]+-1*P_020000101*R_011[0]+-1*P_120000001*R_110[0]+P_120000101*R_111[0]+P_220000001*R_210[0]+-1*P_220000101*R_211[0]; double PR_011010000010=P_011010000*R_010[0]+-1*P_011110000*R_020[0]+-1*P_111010000*R_110[0]+P_111110000*R_120[0]+P_211010000*R_210[0]+-1*P_211110000*R_220[0]; double PR_010011000010=P_010011000*R_010[0]+-1*P_010111000*R_020[0]+P_010211000*R_030[0]+-1*P_110011000*R_110[0]+P_110111000*R_120[0]+-1*P_110211000*R_130[0]; double PR_010010001010=P_010010001*R_010[0]+-1*P_010010101*R_011[0]+-1*P_010110001*R_020[0]+P_010110101*R_021[0]+-1*P_110010001*R_110[0]+P_110010101*R_111[0]+P_110110001*R_120[0]+-1*P_110110101*R_121[0]; double PR_001020000010=P_001020000*R_010[0]+-1*P_001120000*R_020[0]+P_001220000*R_030[0]+-1*P_101020000*R_110[0]+P_101120000*R_120[0]+-1*P_101220000*R_130[0]; double PR_000021000010=P_000021000*R_010[0]+-1*P_000121000*R_020[0]+P_000221000*R_030[0]+-1*P_000321000*R_040[0]; double PR_000020001010=P_000020001*R_010[0]+-1*P_000020101*R_011[0]+-1*P_000120001*R_020[0]+P_000120101*R_021[0]+P_000220001*R_030[0]+-1*P_000220101*R_031[0]; double PR_011000010010=P_011000010*R_010[0]+-1*P_011000110*R_011[0]+-1*P_111000010*R_110[0]+P_111000110*R_111[0]+P_211000010*R_210[0]+-1*P_211000110*R_211[0]; double PR_010001010010=P_010001010*R_010[0]+-1*P_010001110*R_011[0]+-1*P_010101010*R_020[0]+P_010101110*R_021[0]+-1*P_110001010*R_110[0]+P_110001110*R_111[0]+P_110101010*R_120[0]+-1*P_110101110*R_121[0]; double PR_010000011010=P_010000011*R_010[0]+-1*P_010000111*R_011[0]+P_010000211*R_012[0]+-1*P_110000011*R_110[0]+P_110000111*R_111[0]+-1*P_110000211*R_112[0]; double PR_001010010010=P_001010010*R_010[0]+-1*P_001010110*R_011[0]+-1*P_001110010*R_020[0]+P_001110110*R_021[0]+-1*P_101010010*R_110[0]+P_101010110*R_111[0]+P_101110010*R_120[0]+-1*P_101110110*R_121[0]; double PR_000011010010=P_000011010*R_010[0]+-1*P_000011110*R_011[0]+-1*P_000111010*R_020[0]+P_000111110*R_021[0]+P_000211010*R_030[0]+-1*P_000211110*R_031[0]; double PR_000010011010=P_000010011*R_010[0]+-1*P_000010111*R_011[0]+P_000010211*R_012[0]+-1*P_000110011*R_020[0]+P_000110111*R_021[0]+-1*P_000110211*R_022[0]; double PR_001000020010=P_001000020*R_010[0]+-1*P_001000120*R_011[0]+P_001000220*R_012[0]+-1*P_101000020*R_110[0]+P_101000120*R_111[0]+-1*P_101000220*R_112[0]; double PR_000001020010=P_000001020*R_010[0]+-1*P_000001120*R_011[0]+P_000001220*R_012[0]+-1*P_000101020*R_020[0]+P_000101120*R_021[0]+-1*P_000101220*R_022[0]; double PR_000000021010=P_000000021*R_010[0]+-1*P_000000121*R_011[0]+P_000000221*R_012[0]+-1*P_000000321*R_013[0]; double PR_021000000100=P_021000000*R_100[0]+-1*P_121000000*R_200[0]+P_221000000*R_300[0]+-1*P_321000000*R_400[0]; double PR_020001000100=P_020001000*R_100[0]+-1*P_020101000*R_110[0]+-1*P_120001000*R_200[0]+P_120101000*R_210[0]+P_220001000*R_300[0]+-1*P_220101000*R_310[0]; double PR_020000001100=P_020000001*R_100[0]+-1*P_020000101*R_101[0]+-1*P_120000001*R_200[0]+P_120000101*R_201[0]+P_220000001*R_300[0]+-1*P_220000101*R_301[0]; double PR_011010000100=P_011010000*R_100[0]+-1*P_011110000*R_110[0]+-1*P_111010000*R_200[0]+P_111110000*R_210[0]+P_211010000*R_300[0]+-1*P_211110000*R_310[0]; double PR_010011000100=P_010011000*R_100[0]+-1*P_010111000*R_110[0]+P_010211000*R_120[0]+-1*P_110011000*R_200[0]+P_110111000*R_210[0]+-1*P_110211000*R_220[0]; double PR_010010001100=P_010010001*R_100[0]+-1*P_010010101*R_101[0]+-1*P_010110001*R_110[0]+P_010110101*R_111[0]+-1*P_110010001*R_200[0]+P_110010101*R_201[0]+P_110110001*R_210[0]+-1*P_110110101*R_211[0]; double PR_001020000100=P_001020000*R_100[0]+-1*P_001120000*R_110[0]+P_001220000*R_120[0]+-1*P_101020000*R_200[0]+P_101120000*R_210[0]+-1*P_101220000*R_220[0]; double PR_000021000100=P_000021000*R_100[0]+-1*P_000121000*R_110[0]+P_000221000*R_120[0]+-1*P_000321000*R_130[0]; double PR_000020001100=P_000020001*R_100[0]+-1*P_000020101*R_101[0]+-1*P_000120001*R_110[0]+P_000120101*R_111[0]+P_000220001*R_120[0]+-1*P_000220101*R_121[0]; double PR_011000010100=P_011000010*R_100[0]+-1*P_011000110*R_101[0]+-1*P_111000010*R_200[0]+P_111000110*R_201[0]+P_211000010*R_300[0]+-1*P_211000110*R_301[0]; double PR_010001010100=P_010001010*R_100[0]+-1*P_010001110*R_101[0]+-1*P_010101010*R_110[0]+P_010101110*R_111[0]+-1*P_110001010*R_200[0]+P_110001110*R_201[0]+P_110101010*R_210[0]+-1*P_110101110*R_211[0]; double PR_010000011100=P_010000011*R_100[0]+-1*P_010000111*R_101[0]+P_010000211*R_102[0]+-1*P_110000011*R_200[0]+P_110000111*R_201[0]+-1*P_110000211*R_202[0]; double PR_001010010100=P_001010010*R_100[0]+-1*P_001010110*R_101[0]+-1*P_001110010*R_110[0]+P_001110110*R_111[0]+-1*P_101010010*R_200[0]+P_101010110*R_201[0]+P_101110010*R_210[0]+-1*P_101110110*R_211[0]; double PR_000011010100=P_000011010*R_100[0]+-1*P_000011110*R_101[0]+-1*P_000111010*R_110[0]+P_000111110*R_111[0]+P_000211010*R_120[0]+-1*P_000211110*R_121[0]; double PR_000010011100=P_000010011*R_100[0]+-1*P_000010111*R_101[0]+P_000010211*R_102[0]+-1*P_000110011*R_110[0]+P_000110111*R_111[0]+-1*P_000110211*R_112[0]; double PR_001000020100=P_001000020*R_100[0]+-1*P_001000120*R_101[0]+P_001000220*R_102[0]+-1*P_101000020*R_200[0]+P_101000120*R_201[0]+-1*P_101000220*R_202[0]; double PR_000001020100=P_000001020*R_100[0]+-1*P_000001120*R_101[0]+P_000001220*R_102[0]+-1*P_000101020*R_110[0]+P_000101120*R_111[0]+-1*P_000101220*R_112[0]; double PR_000000021100=P_000000021*R_100[0]+-1*P_000000121*R_101[0]+P_000000221*R_102[0]+-1*P_000000321*R_103[0]; double PR_021000000002=P_021000000*R_002[0]+-1*P_121000000*R_102[0]+P_221000000*R_202[0]+-1*P_321000000*R_302[0]; double PR_020001000002=P_020001000*R_002[0]+-1*P_020101000*R_012[0]+-1*P_120001000*R_102[0]+P_120101000*R_112[0]+P_220001000*R_202[0]+-1*P_220101000*R_212[0]; double PR_020000001002=P_020000001*R_002[0]+-1*P_020000101*R_003[0]+-1*P_120000001*R_102[0]+P_120000101*R_103[0]+P_220000001*R_202[0]+-1*P_220000101*R_203[0]; double PR_011010000002=P_011010000*R_002[0]+-1*P_011110000*R_012[0]+-1*P_111010000*R_102[0]+P_111110000*R_112[0]+P_211010000*R_202[0]+-1*P_211110000*R_212[0]; double PR_010011000002=P_010011000*R_002[0]+-1*P_010111000*R_012[0]+P_010211000*R_022[0]+-1*P_110011000*R_102[0]+P_110111000*R_112[0]+-1*P_110211000*R_122[0]; double PR_010010001002=P_010010001*R_002[0]+-1*P_010010101*R_003[0]+-1*P_010110001*R_012[0]+P_010110101*R_013[0]+-1*P_110010001*R_102[0]+P_110010101*R_103[0]+P_110110001*R_112[0]+-1*P_110110101*R_113[0]; double PR_001020000002=P_001020000*R_002[0]+-1*P_001120000*R_012[0]+P_001220000*R_022[0]+-1*P_101020000*R_102[0]+P_101120000*R_112[0]+-1*P_101220000*R_122[0]; double PR_000021000002=P_000021000*R_002[0]+-1*P_000121000*R_012[0]+P_000221000*R_022[0]+-1*P_000321000*R_032[0]; double PR_000020001002=P_000020001*R_002[0]+-1*P_000020101*R_003[0]+-1*P_000120001*R_012[0]+P_000120101*R_013[0]+P_000220001*R_022[0]+-1*P_000220101*R_023[0]; double PR_011000010002=P_011000010*R_002[0]+-1*P_011000110*R_003[0]+-1*P_111000010*R_102[0]+P_111000110*R_103[0]+P_211000010*R_202[0]+-1*P_211000110*R_203[0]; double PR_010001010002=P_010001010*R_002[0]+-1*P_010001110*R_003[0]+-1*P_010101010*R_012[0]+P_010101110*R_013[0]+-1*P_110001010*R_102[0]+P_110001110*R_103[0]+P_110101010*R_112[0]+-1*P_110101110*R_113[0]; double PR_010000011002=P_010000011*R_002[0]+-1*P_010000111*R_003[0]+P_010000211*R_004[0]+-1*P_110000011*R_102[0]+P_110000111*R_103[0]+-1*P_110000211*R_104[0]; double PR_001010010002=P_001010010*R_002[0]+-1*P_001010110*R_003[0]+-1*P_001110010*R_012[0]+P_001110110*R_013[0]+-1*P_101010010*R_102[0]+P_101010110*R_103[0]+P_101110010*R_112[0]+-1*P_101110110*R_113[0]; double PR_000011010002=P_000011010*R_002[0]+-1*P_000011110*R_003[0]+-1*P_000111010*R_012[0]+P_000111110*R_013[0]+P_000211010*R_022[0]+-1*P_000211110*R_023[0]; double PR_000010011002=P_000010011*R_002[0]+-1*P_000010111*R_003[0]+P_000010211*R_004[0]+-1*P_000110011*R_012[0]+P_000110111*R_013[0]+-1*P_000110211*R_014[0]; double PR_001000020002=P_001000020*R_002[0]+-1*P_001000120*R_003[0]+P_001000220*R_004[0]+-1*P_101000020*R_102[0]+P_101000120*R_103[0]+-1*P_101000220*R_104[0]; double PR_000001020002=P_000001020*R_002[0]+-1*P_000001120*R_003[0]+P_000001220*R_004[0]+-1*P_000101020*R_012[0]+P_000101120*R_013[0]+-1*P_000101220*R_014[0]; double PR_000000021002=P_000000021*R_002[0]+-1*P_000000121*R_003[0]+P_000000221*R_004[0]+-1*P_000000321*R_005[0]; double PR_021000000011=P_021000000*R_011[0]+-1*P_121000000*R_111[0]+P_221000000*R_211[0]+-1*P_321000000*R_311[0]; double PR_020001000011=P_020001000*R_011[0]+-1*P_020101000*R_021[0]+-1*P_120001000*R_111[0]+P_120101000*R_121[0]+P_220001000*R_211[0]+-1*P_220101000*R_221[0]; double PR_020000001011=P_020000001*R_011[0]+-1*P_020000101*R_012[0]+-1*P_120000001*R_111[0]+P_120000101*R_112[0]+P_220000001*R_211[0]+-1*P_220000101*R_212[0]; double PR_011010000011=P_011010000*R_011[0]+-1*P_011110000*R_021[0]+-1*P_111010000*R_111[0]+P_111110000*R_121[0]+P_211010000*R_211[0]+-1*P_211110000*R_221[0]; double PR_010011000011=P_010011000*R_011[0]+-1*P_010111000*R_021[0]+P_010211000*R_031[0]+-1*P_110011000*R_111[0]+P_110111000*R_121[0]+-1*P_110211000*R_131[0]; double PR_010010001011=P_010010001*R_011[0]+-1*P_010010101*R_012[0]+-1*P_010110001*R_021[0]+P_010110101*R_022[0]+-1*P_110010001*R_111[0]+P_110010101*R_112[0]+P_110110001*R_121[0]+-1*P_110110101*R_122[0]; double PR_001020000011=P_001020000*R_011[0]+-1*P_001120000*R_021[0]+P_001220000*R_031[0]+-1*P_101020000*R_111[0]+P_101120000*R_121[0]+-1*P_101220000*R_131[0]; double PR_000021000011=P_000021000*R_011[0]+-1*P_000121000*R_021[0]+P_000221000*R_031[0]+-1*P_000321000*R_041[0]; double PR_000020001011=P_000020001*R_011[0]+-1*P_000020101*R_012[0]+-1*P_000120001*R_021[0]+P_000120101*R_022[0]+P_000220001*R_031[0]+-1*P_000220101*R_032[0]; double PR_011000010011=P_011000010*R_011[0]+-1*P_011000110*R_012[0]+-1*P_111000010*R_111[0]+P_111000110*R_112[0]+P_211000010*R_211[0]+-1*P_211000110*R_212[0]; double PR_010001010011=P_010001010*R_011[0]+-1*P_010001110*R_012[0]+-1*P_010101010*R_021[0]+P_010101110*R_022[0]+-1*P_110001010*R_111[0]+P_110001110*R_112[0]+P_110101010*R_121[0]+-1*P_110101110*R_122[0]; double PR_010000011011=P_010000011*R_011[0]+-1*P_010000111*R_012[0]+P_010000211*R_013[0]+-1*P_110000011*R_111[0]+P_110000111*R_112[0]+-1*P_110000211*R_113[0]; double PR_001010010011=P_001010010*R_011[0]+-1*P_001010110*R_012[0]+-1*P_001110010*R_021[0]+P_001110110*R_022[0]+-1*P_101010010*R_111[0]+P_101010110*R_112[0]+P_101110010*R_121[0]+-1*P_101110110*R_122[0]; double PR_000011010011=P_000011010*R_011[0]+-1*P_000011110*R_012[0]+-1*P_000111010*R_021[0]+P_000111110*R_022[0]+P_000211010*R_031[0]+-1*P_000211110*R_032[0]; double PR_000010011011=P_000010011*R_011[0]+-1*P_000010111*R_012[0]+P_000010211*R_013[0]+-1*P_000110011*R_021[0]+P_000110111*R_022[0]+-1*P_000110211*R_023[0]; double PR_001000020011=P_001000020*R_011[0]+-1*P_001000120*R_012[0]+P_001000220*R_013[0]+-1*P_101000020*R_111[0]+P_101000120*R_112[0]+-1*P_101000220*R_113[0]; double PR_000001020011=P_000001020*R_011[0]+-1*P_000001120*R_012[0]+P_000001220*R_013[0]+-1*P_000101020*R_021[0]+P_000101120*R_022[0]+-1*P_000101220*R_023[0]; double PR_000000021011=P_000000021*R_011[0]+-1*P_000000121*R_012[0]+P_000000221*R_013[0]+-1*P_000000321*R_014[0]; double PR_021000000020=P_021000000*R_020[0]+-1*P_121000000*R_120[0]+P_221000000*R_220[0]+-1*P_321000000*R_320[0]; double PR_020001000020=P_020001000*R_020[0]+-1*P_020101000*R_030[0]+-1*P_120001000*R_120[0]+P_120101000*R_130[0]+P_220001000*R_220[0]+-1*P_220101000*R_230[0]; double PR_020000001020=P_020000001*R_020[0]+-1*P_020000101*R_021[0]+-1*P_120000001*R_120[0]+P_120000101*R_121[0]+P_220000001*R_220[0]+-1*P_220000101*R_221[0]; double PR_011010000020=P_011010000*R_020[0]+-1*P_011110000*R_030[0]+-1*P_111010000*R_120[0]+P_111110000*R_130[0]+P_211010000*R_220[0]+-1*P_211110000*R_230[0]; double PR_010011000020=P_010011000*R_020[0]+-1*P_010111000*R_030[0]+P_010211000*R_040[0]+-1*P_110011000*R_120[0]+P_110111000*R_130[0]+-1*P_110211000*R_140[0]; double PR_010010001020=P_010010001*R_020[0]+-1*P_010010101*R_021[0]+-1*P_010110001*R_030[0]+P_010110101*R_031[0]+-1*P_110010001*R_120[0]+P_110010101*R_121[0]+P_110110001*R_130[0]+-1*P_110110101*R_131[0]; double PR_001020000020=P_001020000*R_020[0]+-1*P_001120000*R_030[0]+P_001220000*R_040[0]+-1*P_101020000*R_120[0]+P_101120000*R_130[0]+-1*P_101220000*R_140[0]; double PR_000021000020=P_000021000*R_020[0]+-1*P_000121000*R_030[0]+P_000221000*R_040[0]+-1*P_000321000*R_050[0]; double PR_000020001020=P_000020001*R_020[0]+-1*P_000020101*R_021[0]+-1*P_000120001*R_030[0]+P_000120101*R_031[0]+P_000220001*R_040[0]+-1*P_000220101*R_041[0]; double PR_011000010020=P_011000010*R_020[0]+-1*P_011000110*R_021[0]+-1*P_111000010*R_120[0]+P_111000110*R_121[0]+P_211000010*R_220[0]+-1*P_211000110*R_221[0]; double PR_010001010020=P_010001010*R_020[0]+-1*P_010001110*R_021[0]+-1*P_010101010*R_030[0]+P_010101110*R_031[0]+-1*P_110001010*R_120[0]+P_110001110*R_121[0]+P_110101010*R_130[0]+-1*P_110101110*R_131[0]; double PR_010000011020=P_010000011*R_020[0]+-1*P_010000111*R_021[0]+P_010000211*R_022[0]+-1*P_110000011*R_120[0]+P_110000111*R_121[0]+-1*P_110000211*R_122[0]; double PR_001010010020=P_001010010*R_020[0]+-1*P_001010110*R_021[0]+-1*P_001110010*R_030[0]+P_001110110*R_031[0]+-1*P_101010010*R_120[0]+P_101010110*R_121[0]+P_101110010*R_130[0]+-1*P_101110110*R_131[0]; double PR_000011010020=P_000011010*R_020[0]+-1*P_000011110*R_021[0]+-1*P_000111010*R_030[0]+P_000111110*R_031[0]+P_000211010*R_040[0]+-1*P_000211110*R_041[0]; double PR_000010011020=P_000010011*R_020[0]+-1*P_000010111*R_021[0]+P_000010211*R_022[0]+-1*P_000110011*R_030[0]+P_000110111*R_031[0]+-1*P_000110211*R_032[0]; double PR_001000020020=P_001000020*R_020[0]+-1*P_001000120*R_021[0]+P_001000220*R_022[0]+-1*P_101000020*R_120[0]+P_101000120*R_121[0]+-1*P_101000220*R_122[0]; double PR_000001020020=P_000001020*R_020[0]+-1*P_000001120*R_021[0]+P_000001220*R_022[0]+-1*P_000101020*R_030[0]+P_000101120*R_031[0]+-1*P_000101220*R_032[0]; double PR_000000021020=P_000000021*R_020[0]+-1*P_000000121*R_021[0]+P_000000221*R_022[0]+-1*P_000000321*R_023[0]; double PR_021000000101=P_021000000*R_101[0]+-1*P_121000000*R_201[0]+P_221000000*R_301[0]+-1*P_321000000*R_401[0]; double PR_020001000101=P_020001000*R_101[0]+-1*P_020101000*R_111[0]+-1*P_120001000*R_201[0]+P_120101000*R_211[0]+P_220001000*R_301[0]+-1*P_220101000*R_311[0]; double PR_020000001101=P_020000001*R_101[0]+-1*P_020000101*R_102[0]+-1*P_120000001*R_201[0]+P_120000101*R_202[0]+P_220000001*R_301[0]+-1*P_220000101*R_302[0]; double PR_011010000101=P_011010000*R_101[0]+-1*P_011110000*R_111[0]+-1*P_111010000*R_201[0]+P_111110000*R_211[0]+P_211010000*R_301[0]+-1*P_211110000*R_311[0]; double PR_010011000101=P_010011000*R_101[0]+-1*P_010111000*R_111[0]+P_010211000*R_121[0]+-1*P_110011000*R_201[0]+P_110111000*R_211[0]+-1*P_110211000*R_221[0]; double PR_010010001101=P_010010001*R_101[0]+-1*P_010010101*R_102[0]+-1*P_010110001*R_111[0]+P_010110101*R_112[0]+-1*P_110010001*R_201[0]+P_110010101*R_202[0]+P_110110001*R_211[0]+-1*P_110110101*R_212[0]; double PR_001020000101=P_001020000*R_101[0]+-1*P_001120000*R_111[0]+P_001220000*R_121[0]+-1*P_101020000*R_201[0]+P_101120000*R_211[0]+-1*P_101220000*R_221[0]; double PR_000021000101=P_000021000*R_101[0]+-1*P_000121000*R_111[0]+P_000221000*R_121[0]+-1*P_000321000*R_131[0]; double PR_000020001101=P_000020001*R_101[0]+-1*P_000020101*R_102[0]+-1*P_000120001*R_111[0]+P_000120101*R_112[0]+P_000220001*R_121[0]+-1*P_000220101*R_122[0]; double PR_011000010101=P_011000010*R_101[0]+-1*P_011000110*R_102[0]+-1*P_111000010*R_201[0]+P_111000110*R_202[0]+P_211000010*R_301[0]+-1*P_211000110*R_302[0]; double PR_010001010101=P_010001010*R_101[0]+-1*P_010001110*R_102[0]+-1*P_010101010*R_111[0]+P_010101110*R_112[0]+-1*P_110001010*R_201[0]+P_110001110*R_202[0]+P_110101010*R_211[0]+-1*P_110101110*R_212[0]; double PR_010000011101=P_010000011*R_101[0]+-1*P_010000111*R_102[0]+P_010000211*R_103[0]+-1*P_110000011*R_201[0]+P_110000111*R_202[0]+-1*P_110000211*R_203[0]; double PR_001010010101=P_001010010*R_101[0]+-1*P_001010110*R_102[0]+-1*P_001110010*R_111[0]+P_001110110*R_112[0]+-1*P_101010010*R_201[0]+P_101010110*R_202[0]+P_101110010*R_211[0]+-1*P_101110110*R_212[0]; double PR_000011010101=P_000011010*R_101[0]+-1*P_000011110*R_102[0]+-1*P_000111010*R_111[0]+P_000111110*R_112[0]+P_000211010*R_121[0]+-1*P_000211110*R_122[0]; double PR_000010011101=P_000010011*R_101[0]+-1*P_000010111*R_102[0]+P_000010211*R_103[0]+-1*P_000110011*R_111[0]+P_000110111*R_112[0]+-1*P_000110211*R_113[0]; double PR_001000020101=P_001000020*R_101[0]+-1*P_001000120*R_102[0]+P_001000220*R_103[0]+-1*P_101000020*R_201[0]+P_101000120*R_202[0]+-1*P_101000220*R_203[0]; double PR_000001020101=P_000001020*R_101[0]+-1*P_000001120*R_102[0]+P_000001220*R_103[0]+-1*P_000101020*R_111[0]+P_000101120*R_112[0]+-1*P_000101220*R_113[0]; double PR_000000021101=P_000000021*R_101[0]+-1*P_000000121*R_102[0]+P_000000221*R_103[0]+-1*P_000000321*R_104[0]; double PR_021000000110=P_021000000*R_110[0]+-1*P_121000000*R_210[0]+P_221000000*R_310[0]+-1*P_321000000*R_410[0]; double PR_020001000110=P_020001000*R_110[0]+-1*P_020101000*R_120[0]+-1*P_120001000*R_210[0]+P_120101000*R_220[0]+P_220001000*R_310[0]+-1*P_220101000*R_320[0]; double PR_020000001110=P_020000001*R_110[0]+-1*P_020000101*R_111[0]+-1*P_120000001*R_210[0]+P_120000101*R_211[0]+P_220000001*R_310[0]+-1*P_220000101*R_311[0]; double PR_011010000110=P_011010000*R_110[0]+-1*P_011110000*R_120[0]+-1*P_111010000*R_210[0]+P_111110000*R_220[0]+P_211010000*R_310[0]+-1*P_211110000*R_320[0]; double PR_010011000110=P_010011000*R_110[0]+-1*P_010111000*R_120[0]+P_010211000*R_130[0]+-1*P_110011000*R_210[0]+P_110111000*R_220[0]+-1*P_110211000*R_230[0]; double PR_010010001110=P_010010001*R_110[0]+-1*P_010010101*R_111[0]+-1*P_010110001*R_120[0]+P_010110101*R_121[0]+-1*P_110010001*R_210[0]+P_110010101*R_211[0]+P_110110001*R_220[0]+-1*P_110110101*R_221[0]; double PR_001020000110=P_001020000*R_110[0]+-1*P_001120000*R_120[0]+P_001220000*R_130[0]+-1*P_101020000*R_210[0]+P_101120000*R_220[0]+-1*P_101220000*R_230[0]; double PR_000021000110=P_000021000*R_110[0]+-1*P_000121000*R_120[0]+P_000221000*R_130[0]+-1*P_000321000*R_140[0]; double PR_000020001110=P_000020001*R_110[0]+-1*P_000020101*R_111[0]+-1*P_000120001*R_120[0]+P_000120101*R_121[0]+P_000220001*R_130[0]+-1*P_000220101*R_131[0]; double PR_011000010110=P_011000010*R_110[0]+-1*P_011000110*R_111[0]+-1*P_111000010*R_210[0]+P_111000110*R_211[0]+P_211000010*R_310[0]+-1*P_211000110*R_311[0]; double PR_010001010110=P_010001010*R_110[0]+-1*P_010001110*R_111[0]+-1*P_010101010*R_120[0]+P_010101110*R_121[0]+-1*P_110001010*R_210[0]+P_110001110*R_211[0]+P_110101010*R_220[0]+-1*P_110101110*R_221[0]; double PR_010000011110=P_010000011*R_110[0]+-1*P_010000111*R_111[0]+P_010000211*R_112[0]+-1*P_110000011*R_210[0]+P_110000111*R_211[0]+-1*P_110000211*R_212[0]; double PR_001010010110=P_001010010*R_110[0]+-1*P_001010110*R_111[0]+-1*P_001110010*R_120[0]+P_001110110*R_121[0]+-1*P_101010010*R_210[0]+P_101010110*R_211[0]+P_101110010*R_220[0]+-1*P_101110110*R_221[0]; double PR_000011010110=P_000011010*R_110[0]+-1*P_000011110*R_111[0]+-1*P_000111010*R_120[0]+P_000111110*R_121[0]+P_000211010*R_130[0]+-1*P_000211110*R_131[0]; double PR_000010011110=P_000010011*R_110[0]+-1*P_000010111*R_111[0]+P_000010211*R_112[0]+-1*P_000110011*R_120[0]+P_000110111*R_121[0]+-1*P_000110211*R_122[0]; double PR_001000020110=P_001000020*R_110[0]+-1*P_001000120*R_111[0]+P_001000220*R_112[0]+-1*P_101000020*R_210[0]+P_101000120*R_211[0]+-1*P_101000220*R_212[0]; double PR_000001020110=P_000001020*R_110[0]+-1*P_000001120*R_111[0]+P_000001220*R_112[0]+-1*P_000101020*R_120[0]+P_000101120*R_121[0]+-1*P_000101220*R_122[0]; double PR_000000021110=P_000000021*R_110[0]+-1*P_000000121*R_111[0]+P_000000221*R_112[0]+-1*P_000000321*R_113[0]; double PR_021000000200=P_021000000*R_200[0]+-1*P_121000000*R_300[0]+P_221000000*R_400[0]+-1*P_321000000*R_500[0]; double PR_020001000200=P_020001000*R_200[0]+-1*P_020101000*R_210[0]+-1*P_120001000*R_300[0]+P_120101000*R_310[0]+P_220001000*R_400[0]+-1*P_220101000*R_410[0]; double PR_020000001200=P_020000001*R_200[0]+-1*P_020000101*R_201[0]+-1*P_120000001*R_300[0]+P_120000101*R_301[0]+P_220000001*R_400[0]+-1*P_220000101*R_401[0]; double PR_011010000200=P_011010000*R_200[0]+-1*P_011110000*R_210[0]+-1*P_111010000*R_300[0]+P_111110000*R_310[0]+P_211010000*R_400[0]+-1*P_211110000*R_410[0]; double PR_010011000200=P_010011000*R_200[0]+-1*P_010111000*R_210[0]+P_010211000*R_220[0]+-1*P_110011000*R_300[0]+P_110111000*R_310[0]+-1*P_110211000*R_320[0]; double PR_010010001200=P_010010001*R_200[0]+-1*P_010010101*R_201[0]+-1*P_010110001*R_210[0]+P_010110101*R_211[0]+-1*P_110010001*R_300[0]+P_110010101*R_301[0]+P_110110001*R_310[0]+-1*P_110110101*R_311[0]; double PR_001020000200=P_001020000*R_200[0]+-1*P_001120000*R_210[0]+P_001220000*R_220[0]+-1*P_101020000*R_300[0]+P_101120000*R_310[0]+-1*P_101220000*R_320[0]; double PR_000021000200=P_000021000*R_200[0]+-1*P_000121000*R_210[0]+P_000221000*R_220[0]+-1*P_000321000*R_230[0]; double PR_000020001200=P_000020001*R_200[0]+-1*P_000020101*R_201[0]+-1*P_000120001*R_210[0]+P_000120101*R_211[0]+P_000220001*R_220[0]+-1*P_000220101*R_221[0]; double PR_011000010200=P_011000010*R_200[0]+-1*P_011000110*R_201[0]+-1*P_111000010*R_300[0]+P_111000110*R_301[0]+P_211000010*R_400[0]+-1*P_211000110*R_401[0]; double PR_010001010200=P_010001010*R_200[0]+-1*P_010001110*R_201[0]+-1*P_010101010*R_210[0]+P_010101110*R_211[0]+-1*P_110001010*R_300[0]+P_110001110*R_301[0]+P_110101010*R_310[0]+-1*P_110101110*R_311[0]; double PR_010000011200=P_010000011*R_200[0]+-1*P_010000111*R_201[0]+P_010000211*R_202[0]+-1*P_110000011*R_300[0]+P_110000111*R_301[0]+-1*P_110000211*R_302[0]; double PR_001010010200=P_001010010*R_200[0]+-1*P_001010110*R_201[0]+-1*P_001110010*R_210[0]+P_001110110*R_211[0]+-1*P_101010010*R_300[0]+P_101010110*R_301[0]+P_101110010*R_310[0]+-1*P_101110110*R_311[0]; double PR_000011010200=P_000011010*R_200[0]+-1*P_000011110*R_201[0]+-1*P_000111010*R_210[0]+P_000111110*R_211[0]+P_000211010*R_220[0]+-1*P_000211110*R_221[0]; double PR_000010011200=P_000010011*R_200[0]+-1*P_000010111*R_201[0]+P_000010211*R_202[0]+-1*P_000110011*R_210[0]+P_000110111*R_211[0]+-1*P_000110211*R_212[0]; double PR_001000020200=P_001000020*R_200[0]+-1*P_001000120*R_201[0]+P_001000220*R_202[0]+-1*P_101000020*R_300[0]+P_101000120*R_301[0]+-1*P_101000220*R_302[0]; double PR_000001020200=P_000001020*R_200[0]+-1*P_000001120*R_201[0]+P_000001220*R_202[0]+-1*P_000101020*R_210[0]+P_000101120*R_211[0]+-1*P_000101220*R_212[0]; double PR_000000021200=P_000000021*R_200[0]+-1*P_000000121*R_201[0]+P_000000221*R_202[0]+-1*P_000000321*R_203[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(Q_002000000*PR_021000000000+Q_102000000*PR_021000000100+Q_202000000*PR_021000000200); ans_temp[ans_id*6+0]+=Pmtrx[1]*(Q_001001000*PR_021000000000+Q_001101000*PR_021000000010+Q_101001000*PR_021000000100+Q_101101000*PR_021000000110); ans_temp[ans_id*6+0]+=Pmtrx[2]*(Q_000002000*PR_021000000000+Q_000102000*PR_021000000010+Q_000202000*PR_021000000020); ans_temp[ans_id*6+0]+=Pmtrx[3]*(Q_001000001*PR_021000000000+Q_001000101*PR_021000000001+Q_101000001*PR_021000000100+Q_101000101*PR_021000000101); ans_temp[ans_id*6+0]+=Pmtrx[4]*(Q_000001001*PR_021000000000+Q_000001101*PR_021000000001+Q_000101001*PR_021000000010+Q_000101101*PR_021000000011); ans_temp[ans_id*6+0]+=Pmtrx[5]*(Q_000000002*PR_021000000000+Q_000000102*PR_021000000001+Q_000000202*PR_021000000002); ans_temp[ans_id*6+0]+=Pmtrx[6]*(Q_002000000*PR_020001000000+Q_102000000*PR_020001000100+Q_202000000*PR_020001000200); ans_temp[ans_id*6+0]+=Pmtrx[7]*(Q_001001000*PR_020001000000+Q_001101000*PR_020001000010+Q_101001000*PR_020001000100+Q_101101000*PR_020001000110); ans_temp[ans_id*6+0]+=Pmtrx[8]*(Q_000002000*PR_020001000000+Q_000102000*PR_020001000010+Q_000202000*PR_020001000020); ans_temp[ans_id*6+0]+=Pmtrx[9]*(Q_001000001*PR_020001000000+Q_001000101*PR_020001000001+Q_101000001*PR_020001000100+Q_101000101*PR_020001000101); ans_temp[ans_id*6+0]+=Pmtrx[10]*(Q_000001001*PR_020001000000+Q_000001101*PR_020001000001+Q_000101001*PR_020001000010+Q_000101101*PR_020001000011); ans_temp[ans_id*6+0]+=Pmtrx[11]*(Q_000000002*PR_020001000000+Q_000000102*PR_020001000001+Q_000000202*PR_020001000002); ans_temp[ans_id*6+0]+=Pmtrx[12]*(Q_002000000*PR_020000001000+Q_102000000*PR_020000001100+Q_202000000*PR_020000001200); ans_temp[ans_id*6+0]+=Pmtrx[13]*(Q_001001000*PR_020000001000+Q_001101000*PR_020000001010+Q_101001000*PR_020000001100+Q_101101000*PR_020000001110); ans_temp[ans_id*6+0]+=Pmtrx[14]*(Q_000002000*PR_020000001000+Q_000102000*PR_020000001010+Q_000202000*PR_020000001020); ans_temp[ans_id*6+0]+=Pmtrx[15]*(Q_001000001*PR_020000001000+Q_001000101*PR_020000001001+Q_101000001*PR_020000001100+Q_101000101*PR_020000001101); ans_temp[ans_id*6+0]+=Pmtrx[16]*(Q_000001001*PR_020000001000+Q_000001101*PR_020000001001+Q_000101001*PR_020000001010+Q_000101101*PR_020000001011); ans_temp[ans_id*6+0]+=Pmtrx[17]*(Q_000000002*PR_020000001000+Q_000000102*PR_020000001001+Q_000000202*PR_020000001002); ans_temp[ans_id*6+1]+=Pmtrx[0]*(Q_002000000*PR_011010000000+Q_102000000*PR_011010000100+Q_202000000*PR_011010000200); ans_temp[ans_id*6+1]+=Pmtrx[1]*(Q_001001000*PR_011010000000+Q_001101000*PR_011010000010+Q_101001000*PR_011010000100+Q_101101000*PR_011010000110); ans_temp[ans_id*6+1]+=Pmtrx[2]*(Q_000002000*PR_011010000000+Q_000102000*PR_011010000010+Q_000202000*PR_011010000020); ans_temp[ans_id*6+1]+=Pmtrx[3]*(Q_001000001*PR_011010000000+Q_001000101*PR_011010000001+Q_101000001*PR_011010000100+Q_101000101*PR_011010000101); ans_temp[ans_id*6+1]+=Pmtrx[4]*(Q_000001001*PR_011010000000+Q_000001101*PR_011010000001+Q_000101001*PR_011010000010+Q_000101101*PR_011010000011); ans_temp[ans_id*6+1]+=Pmtrx[5]*(Q_000000002*PR_011010000000+Q_000000102*PR_011010000001+Q_000000202*PR_011010000002); ans_temp[ans_id*6+1]+=Pmtrx[6]*(Q_002000000*PR_010011000000+Q_102000000*PR_010011000100+Q_202000000*PR_010011000200); ans_temp[ans_id*6+1]+=Pmtrx[7]*(Q_001001000*PR_010011000000+Q_001101000*PR_010011000010+Q_101001000*PR_010011000100+Q_101101000*PR_010011000110); ans_temp[ans_id*6+1]+=Pmtrx[8]*(Q_000002000*PR_010011000000+Q_000102000*PR_010011000010+Q_000202000*PR_010011000020); ans_temp[ans_id*6+1]+=Pmtrx[9]*(Q_001000001*PR_010011000000+Q_001000101*PR_010011000001+Q_101000001*PR_010011000100+Q_101000101*PR_010011000101); ans_temp[ans_id*6+1]+=Pmtrx[10]*(Q_000001001*PR_010011000000+Q_000001101*PR_010011000001+Q_000101001*PR_010011000010+Q_000101101*PR_010011000011); ans_temp[ans_id*6+1]+=Pmtrx[11]*(Q_000000002*PR_010011000000+Q_000000102*PR_010011000001+Q_000000202*PR_010011000002); ans_temp[ans_id*6+1]+=Pmtrx[12]*(Q_002000000*PR_010010001000+Q_102000000*PR_010010001100+Q_202000000*PR_010010001200); ans_temp[ans_id*6+1]+=Pmtrx[13]*(Q_001001000*PR_010010001000+Q_001101000*PR_010010001010+Q_101001000*PR_010010001100+Q_101101000*PR_010010001110); ans_temp[ans_id*6+1]+=Pmtrx[14]*(Q_000002000*PR_010010001000+Q_000102000*PR_010010001010+Q_000202000*PR_010010001020); ans_temp[ans_id*6+1]+=Pmtrx[15]*(Q_001000001*PR_010010001000+Q_001000101*PR_010010001001+Q_101000001*PR_010010001100+Q_101000101*PR_010010001101); ans_temp[ans_id*6+1]+=Pmtrx[16]*(Q_000001001*PR_010010001000+Q_000001101*PR_010010001001+Q_000101001*PR_010010001010+Q_000101101*PR_010010001011); ans_temp[ans_id*6+1]+=Pmtrx[17]*(Q_000000002*PR_010010001000+Q_000000102*PR_010010001001+Q_000000202*PR_010010001002); ans_temp[ans_id*6+2]+=Pmtrx[0]*(Q_002000000*PR_001020000000+Q_102000000*PR_001020000100+Q_202000000*PR_001020000200); ans_temp[ans_id*6+2]+=Pmtrx[1]*(Q_001001000*PR_001020000000+Q_001101000*PR_001020000010+Q_101001000*PR_001020000100+Q_101101000*PR_001020000110); ans_temp[ans_id*6+2]+=Pmtrx[2]*(Q_000002000*PR_001020000000+Q_000102000*PR_001020000010+Q_000202000*PR_001020000020); ans_temp[ans_id*6+2]+=Pmtrx[3]*(Q_001000001*PR_001020000000+Q_001000101*PR_001020000001+Q_101000001*PR_001020000100+Q_101000101*PR_001020000101); ans_temp[ans_id*6+2]+=Pmtrx[4]*(Q_000001001*PR_001020000000+Q_000001101*PR_001020000001+Q_000101001*PR_001020000010+Q_000101101*PR_001020000011); ans_temp[ans_id*6+2]+=Pmtrx[5]*(Q_000000002*PR_001020000000+Q_000000102*PR_001020000001+Q_000000202*PR_001020000002); ans_temp[ans_id*6+2]+=Pmtrx[6]*(Q_002000000*PR_000021000000+Q_102000000*PR_000021000100+Q_202000000*PR_000021000200); ans_temp[ans_id*6+2]+=Pmtrx[7]*(Q_001001000*PR_000021000000+Q_001101000*PR_000021000010+Q_101001000*PR_000021000100+Q_101101000*PR_000021000110); ans_temp[ans_id*6+2]+=Pmtrx[8]*(Q_000002000*PR_000021000000+Q_000102000*PR_000021000010+Q_000202000*PR_000021000020); ans_temp[ans_id*6+2]+=Pmtrx[9]*(Q_001000001*PR_000021000000+Q_001000101*PR_000021000001+Q_101000001*PR_000021000100+Q_101000101*PR_000021000101); ans_temp[ans_id*6+2]+=Pmtrx[10]*(Q_000001001*PR_000021000000+Q_000001101*PR_000021000001+Q_000101001*PR_000021000010+Q_000101101*PR_000021000011); ans_temp[ans_id*6+2]+=Pmtrx[11]*(Q_000000002*PR_000021000000+Q_000000102*PR_000021000001+Q_000000202*PR_000021000002); ans_temp[ans_id*6+2]+=Pmtrx[12]*(Q_002000000*PR_000020001000+Q_102000000*PR_000020001100+Q_202000000*PR_000020001200); ans_temp[ans_id*6+2]+=Pmtrx[13]*(Q_001001000*PR_000020001000+Q_001101000*PR_000020001010+Q_101001000*PR_000020001100+Q_101101000*PR_000020001110); ans_temp[ans_id*6+2]+=Pmtrx[14]*(Q_000002000*PR_000020001000+Q_000102000*PR_000020001010+Q_000202000*PR_000020001020); ans_temp[ans_id*6+2]+=Pmtrx[15]*(Q_001000001*PR_000020001000+Q_001000101*PR_000020001001+Q_101000001*PR_000020001100+Q_101000101*PR_000020001101); ans_temp[ans_id*6+2]+=Pmtrx[16]*(Q_000001001*PR_000020001000+Q_000001101*PR_000020001001+Q_000101001*PR_000020001010+Q_000101101*PR_000020001011); ans_temp[ans_id*6+2]+=Pmtrx[17]*(Q_000000002*PR_000020001000+Q_000000102*PR_000020001001+Q_000000202*PR_000020001002); ans_temp[ans_id*6+3]+=Pmtrx[0]*(Q_002000000*PR_011000010000+Q_102000000*PR_011000010100+Q_202000000*PR_011000010200); ans_temp[ans_id*6+3]+=Pmtrx[1]*(Q_001001000*PR_011000010000+Q_001101000*PR_011000010010+Q_101001000*PR_011000010100+Q_101101000*PR_011000010110); ans_temp[ans_id*6+3]+=Pmtrx[2]*(Q_000002000*PR_011000010000+Q_000102000*PR_011000010010+Q_000202000*PR_011000010020); ans_temp[ans_id*6+3]+=Pmtrx[3]*(Q_001000001*PR_011000010000+Q_001000101*PR_011000010001+Q_101000001*PR_011000010100+Q_101000101*PR_011000010101); ans_temp[ans_id*6+3]+=Pmtrx[4]*(Q_000001001*PR_011000010000+Q_000001101*PR_011000010001+Q_000101001*PR_011000010010+Q_000101101*PR_011000010011); ans_temp[ans_id*6+3]+=Pmtrx[5]*(Q_000000002*PR_011000010000+Q_000000102*PR_011000010001+Q_000000202*PR_011000010002); ans_temp[ans_id*6+3]+=Pmtrx[6]*(Q_002000000*PR_010001010000+Q_102000000*PR_010001010100+Q_202000000*PR_010001010200); ans_temp[ans_id*6+3]+=Pmtrx[7]*(Q_001001000*PR_010001010000+Q_001101000*PR_010001010010+Q_101001000*PR_010001010100+Q_101101000*PR_010001010110); ans_temp[ans_id*6+3]+=Pmtrx[8]*(Q_000002000*PR_010001010000+Q_000102000*PR_010001010010+Q_000202000*PR_010001010020); ans_temp[ans_id*6+3]+=Pmtrx[9]*(Q_001000001*PR_010001010000+Q_001000101*PR_010001010001+Q_101000001*PR_010001010100+Q_101000101*PR_010001010101); ans_temp[ans_id*6+3]+=Pmtrx[10]*(Q_000001001*PR_010001010000+Q_000001101*PR_010001010001+Q_000101001*PR_010001010010+Q_000101101*PR_010001010011); ans_temp[ans_id*6+3]+=Pmtrx[11]*(Q_000000002*PR_010001010000+Q_000000102*PR_010001010001+Q_000000202*PR_010001010002); ans_temp[ans_id*6+3]+=Pmtrx[12]*(Q_002000000*PR_010000011000+Q_102000000*PR_010000011100+Q_202000000*PR_010000011200); ans_temp[ans_id*6+3]+=Pmtrx[13]*(Q_001001000*PR_010000011000+Q_001101000*PR_010000011010+Q_101001000*PR_010000011100+Q_101101000*PR_010000011110); ans_temp[ans_id*6+3]+=Pmtrx[14]*(Q_000002000*PR_010000011000+Q_000102000*PR_010000011010+Q_000202000*PR_010000011020); ans_temp[ans_id*6+3]+=Pmtrx[15]*(Q_001000001*PR_010000011000+Q_001000101*PR_010000011001+Q_101000001*PR_010000011100+Q_101000101*PR_010000011101); ans_temp[ans_id*6+3]+=Pmtrx[16]*(Q_000001001*PR_010000011000+Q_000001101*PR_010000011001+Q_000101001*PR_010000011010+Q_000101101*PR_010000011011); ans_temp[ans_id*6+3]+=Pmtrx[17]*(Q_000000002*PR_010000011000+Q_000000102*PR_010000011001+Q_000000202*PR_010000011002); ans_temp[ans_id*6+4]+=Pmtrx[0]*(Q_002000000*PR_001010010000+Q_102000000*PR_001010010100+Q_202000000*PR_001010010200); ans_temp[ans_id*6+4]+=Pmtrx[1]*(Q_001001000*PR_001010010000+Q_001101000*PR_001010010010+Q_101001000*PR_001010010100+Q_101101000*PR_001010010110); ans_temp[ans_id*6+4]+=Pmtrx[2]*(Q_000002000*PR_001010010000+Q_000102000*PR_001010010010+Q_000202000*PR_001010010020); ans_temp[ans_id*6+4]+=Pmtrx[3]*(Q_001000001*PR_001010010000+Q_001000101*PR_001010010001+Q_101000001*PR_001010010100+Q_101000101*PR_001010010101); ans_temp[ans_id*6+4]+=Pmtrx[4]*(Q_000001001*PR_001010010000+Q_000001101*PR_001010010001+Q_000101001*PR_001010010010+Q_000101101*PR_001010010011); ans_temp[ans_id*6+4]+=Pmtrx[5]*(Q_000000002*PR_001010010000+Q_000000102*PR_001010010001+Q_000000202*PR_001010010002); ans_temp[ans_id*6+4]+=Pmtrx[6]*(Q_002000000*PR_000011010000+Q_102000000*PR_000011010100+Q_202000000*PR_000011010200); ans_temp[ans_id*6+4]+=Pmtrx[7]*(Q_001001000*PR_000011010000+Q_001101000*PR_000011010010+Q_101001000*PR_000011010100+Q_101101000*PR_000011010110); ans_temp[ans_id*6+4]+=Pmtrx[8]*(Q_000002000*PR_000011010000+Q_000102000*PR_000011010010+Q_000202000*PR_000011010020); ans_temp[ans_id*6+4]+=Pmtrx[9]*(Q_001000001*PR_000011010000+Q_001000101*PR_000011010001+Q_101000001*PR_000011010100+Q_101000101*PR_000011010101); ans_temp[ans_id*6+4]+=Pmtrx[10]*(Q_000001001*PR_000011010000+Q_000001101*PR_000011010001+Q_000101001*PR_000011010010+Q_000101101*PR_000011010011); ans_temp[ans_id*6+4]+=Pmtrx[11]*(Q_000000002*PR_000011010000+Q_000000102*PR_000011010001+Q_000000202*PR_000011010002); ans_temp[ans_id*6+4]+=Pmtrx[12]*(Q_002000000*PR_000010011000+Q_102000000*PR_000010011100+Q_202000000*PR_000010011200); ans_temp[ans_id*6+4]+=Pmtrx[13]*(Q_001001000*PR_000010011000+Q_001101000*PR_000010011010+Q_101001000*PR_000010011100+Q_101101000*PR_000010011110); ans_temp[ans_id*6+4]+=Pmtrx[14]*(Q_000002000*PR_000010011000+Q_000102000*PR_000010011010+Q_000202000*PR_000010011020); ans_temp[ans_id*6+4]+=Pmtrx[15]*(Q_001000001*PR_000010011000+Q_001000101*PR_000010011001+Q_101000001*PR_000010011100+Q_101000101*PR_000010011101); ans_temp[ans_id*6+4]+=Pmtrx[16]*(Q_000001001*PR_000010011000+Q_000001101*PR_000010011001+Q_000101001*PR_000010011010+Q_000101101*PR_000010011011); ans_temp[ans_id*6+4]+=Pmtrx[17]*(Q_000000002*PR_000010011000+Q_000000102*PR_000010011001+Q_000000202*PR_000010011002); ans_temp[ans_id*6+5]+=Pmtrx[0]*(Q_002000000*PR_001000020000+Q_102000000*PR_001000020100+Q_202000000*PR_001000020200); ans_temp[ans_id*6+5]+=Pmtrx[1]*(Q_001001000*PR_001000020000+Q_001101000*PR_001000020010+Q_101001000*PR_001000020100+Q_101101000*PR_001000020110); ans_temp[ans_id*6+5]+=Pmtrx[2]*(Q_000002000*PR_001000020000+Q_000102000*PR_001000020010+Q_000202000*PR_001000020020); ans_temp[ans_id*6+5]+=Pmtrx[3]*(Q_001000001*PR_001000020000+Q_001000101*PR_001000020001+Q_101000001*PR_001000020100+Q_101000101*PR_001000020101); ans_temp[ans_id*6+5]+=Pmtrx[4]*(Q_000001001*PR_001000020000+Q_000001101*PR_001000020001+Q_000101001*PR_001000020010+Q_000101101*PR_001000020011); ans_temp[ans_id*6+5]+=Pmtrx[5]*(Q_000000002*PR_001000020000+Q_000000102*PR_001000020001+Q_000000202*PR_001000020002); ans_temp[ans_id*6+5]+=Pmtrx[6]*(Q_002000000*PR_000001020000+Q_102000000*PR_000001020100+Q_202000000*PR_000001020200); ans_temp[ans_id*6+5]+=Pmtrx[7]*(Q_001001000*PR_000001020000+Q_001101000*PR_000001020010+Q_101001000*PR_000001020100+Q_101101000*PR_000001020110); ans_temp[ans_id*6+5]+=Pmtrx[8]*(Q_000002000*PR_000001020000+Q_000102000*PR_000001020010+Q_000202000*PR_000001020020); ans_temp[ans_id*6+5]+=Pmtrx[9]*(Q_001000001*PR_000001020000+Q_001000101*PR_000001020001+Q_101000001*PR_000001020100+Q_101000101*PR_000001020101); ans_temp[ans_id*6+5]+=Pmtrx[10]*(Q_000001001*PR_000001020000+Q_000001101*PR_000001020001+Q_000101001*PR_000001020010+Q_000101101*PR_000001020011); ans_temp[ans_id*6+5]+=Pmtrx[11]*(Q_000000002*PR_000001020000+Q_000000102*PR_000001020001+Q_000000202*PR_000001020002); ans_temp[ans_id*6+5]+=Pmtrx[12]*(Q_002000000*PR_000000021000+Q_102000000*PR_000000021100+Q_202000000*PR_000000021200); ans_temp[ans_id*6+5]+=Pmtrx[13]*(Q_001001000*PR_000000021000+Q_001101000*PR_000000021010+Q_101001000*PR_000000021100+Q_101101000*PR_000000021110); ans_temp[ans_id*6+5]+=Pmtrx[14]*(Q_000002000*PR_000000021000+Q_000102000*PR_000000021010+Q_000202000*PR_000000021020); ans_temp[ans_id*6+5]+=Pmtrx[15]*(Q_001000001*PR_000000021000+Q_001000101*PR_000000021001+Q_101000001*PR_000000021100+Q_101000101*PR_000000021101); ans_temp[ans_id*6+5]+=Pmtrx[16]*(Q_000001001*PR_000000021000+Q_000001101*PR_000000021001+Q_000101001*PR_000000021010+Q_000101101*PR_000000021011); ans_temp[ans_id*6+5]+=Pmtrx[17]*(Q_000000002*PR_000000021000+Q_000000102*PR_000000021001+Q_000000202*PR_000000021002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } } __global__ void MD_Kq_dpsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[18]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_010[3]; Pd_010[0]=PA[ii*3+0]; Pd_010[1]=PA[ii*3+1]; Pd_010[2]=PA[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<3;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double QR_002000000003=Q_002000000*R_003[0]+-1*Q_102000000*R_103[0]+Q_202000000*R_203[0]; double QR_001001000003=Q_001001000*R_003[0]+-1*Q_001101000*R_013[0]+-1*Q_101001000*R_103[0]+Q_101101000*R_113[0]; double QR_000002000003=Q_000002000*R_003[0]+-1*Q_000102000*R_013[0]+Q_000202000*R_023[0]; double QR_001000001003=Q_001000001*R_003[0]+-1*Q_001000101*R_004[0]+-1*Q_101000001*R_103[0]+Q_101000101*R_104[0]; double QR_000001001003=Q_000001001*R_003[0]+-1*Q_000001101*R_004[0]+-1*Q_000101001*R_013[0]+Q_000101101*R_014[0]; double QR_000000002003=Q_000000002*R_003[0]+-1*Q_000000102*R_004[0]+Q_000000202*R_005[0]; double QR_002000000012=Q_002000000*R_012[0]+-1*Q_102000000*R_112[0]+Q_202000000*R_212[0]; double QR_001001000012=Q_001001000*R_012[0]+-1*Q_001101000*R_022[0]+-1*Q_101001000*R_112[0]+Q_101101000*R_122[0]; double QR_000002000012=Q_000002000*R_012[0]+-1*Q_000102000*R_022[0]+Q_000202000*R_032[0]; double QR_001000001012=Q_001000001*R_012[0]+-1*Q_001000101*R_013[0]+-1*Q_101000001*R_112[0]+Q_101000101*R_113[0]; double QR_000001001012=Q_000001001*R_012[0]+-1*Q_000001101*R_013[0]+-1*Q_000101001*R_022[0]+Q_000101101*R_023[0]; double QR_000000002012=Q_000000002*R_012[0]+-1*Q_000000102*R_013[0]+Q_000000202*R_014[0]; double QR_002000000021=Q_002000000*R_021[0]+-1*Q_102000000*R_121[0]+Q_202000000*R_221[0]; double QR_001001000021=Q_001001000*R_021[0]+-1*Q_001101000*R_031[0]+-1*Q_101001000*R_121[0]+Q_101101000*R_131[0]; double QR_000002000021=Q_000002000*R_021[0]+-1*Q_000102000*R_031[0]+Q_000202000*R_041[0]; double QR_001000001021=Q_001000001*R_021[0]+-1*Q_001000101*R_022[0]+-1*Q_101000001*R_121[0]+Q_101000101*R_122[0]; double QR_000001001021=Q_000001001*R_021[0]+-1*Q_000001101*R_022[0]+-1*Q_000101001*R_031[0]+Q_000101101*R_032[0]; double QR_000000002021=Q_000000002*R_021[0]+-1*Q_000000102*R_022[0]+Q_000000202*R_023[0]; double QR_002000000030=Q_002000000*R_030[0]+-1*Q_102000000*R_130[0]+Q_202000000*R_230[0]; double QR_001001000030=Q_001001000*R_030[0]+-1*Q_001101000*R_040[0]+-1*Q_101001000*R_130[0]+Q_101101000*R_140[0]; double QR_000002000030=Q_000002000*R_030[0]+-1*Q_000102000*R_040[0]+Q_000202000*R_050[0]; double QR_001000001030=Q_001000001*R_030[0]+-1*Q_001000101*R_031[0]+-1*Q_101000001*R_130[0]+Q_101000101*R_131[0]; double QR_000001001030=Q_000001001*R_030[0]+-1*Q_000001101*R_031[0]+-1*Q_000101001*R_040[0]+Q_000101101*R_041[0]; double QR_000000002030=Q_000000002*R_030[0]+-1*Q_000000102*R_031[0]+Q_000000202*R_032[0]; double QR_002000000102=Q_002000000*R_102[0]+-1*Q_102000000*R_202[0]+Q_202000000*R_302[0]; double QR_001001000102=Q_001001000*R_102[0]+-1*Q_001101000*R_112[0]+-1*Q_101001000*R_202[0]+Q_101101000*R_212[0]; double QR_000002000102=Q_000002000*R_102[0]+-1*Q_000102000*R_112[0]+Q_000202000*R_122[0]; double QR_001000001102=Q_001000001*R_102[0]+-1*Q_001000101*R_103[0]+-1*Q_101000001*R_202[0]+Q_101000101*R_203[0]; double QR_000001001102=Q_000001001*R_102[0]+-1*Q_000001101*R_103[0]+-1*Q_000101001*R_112[0]+Q_000101101*R_113[0]; double QR_000000002102=Q_000000002*R_102[0]+-1*Q_000000102*R_103[0]+Q_000000202*R_104[0]; double QR_002000000111=Q_002000000*R_111[0]+-1*Q_102000000*R_211[0]+Q_202000000*R_311[0]; double QR_001001000111=Q_001001000*R_111[0]+-1*Q_001101000*R_121[0]+-1*Q_101001000*R_211[0]+Q_101101000*R_221[0]; double QR_000002000111=Q_000002000*R_111[0]+-1*Q_000102000*R_121[0]+Q_000202000*R_131[0]; double QR_001000001111=Q_001000001*R_111[0]+-1*Q_001000101*R_112[0]+-1*Q_101000001*R_211[0]+Q_101000101*R_212[0]; double QR_000001001111=Q_000001001*R_111[0]+-1*Q_000001101*R_112[0]+-1*Q_000101001*R_121[0]+Q_000101101*R_122[0]; double QR_000000002111=Q_000000002*R_111[0]+-1*Q_000000102*R_112[0]+Q_000000202*R_113[0]; double QR_002000000120=Q_002000000*R_120[0]+-1*Q_102000000*R_220[0]+Q_202000000*R_320[0]; double QR_001001000120=Q_001001000*R_120[0]+-1*Q_001101000*R_130[0]+-1*Q_101001000*R_220[0]+Q_101101000*R_230[0]; double QR_000002000120=Q_000002000*R_120[0]+-1*Q_000102000*R_130[0]+Q_000202000*R_140[0]; double QR_001000001120=Q_001000001*R_120[0]+-1*Q_001000101*R_121[0]+-1*Q_101000001*R_220[0]+Q_101000101*R_221[0]; double QR_000001001120=Q_000001001*R_120[0]+-1*Q_000001101*R_121[0]+-1*Q_000101001*R_130[0]+Q_000101101*R_131[0]; double QR_000000002120=Q_000000002*R_120[0]+-1*Q_000000102*R_121[0]+Q_000000202*R_122[0]; double QR_002000000201=Q_002000000*R_201[0]+-1*Q_102000000*R_301[0]+Q_202000000*R_401[0]; double QR_001001000201=Q_001001000*R_201[0]+-1*Q_001101000*R_211[0]+-1*Q_101001000*R_301[0]+Q_101101000*R_311[0]; double QR_000002000201=Q_000002000*R_201[0]+-1*Q_000102000*R_211[0]+Q_000202000*R_221[0]; double QR_001000001201=Q_001000001*R_201[0]+-1*Q_001000101*R_202[0]+-1*Q_101000001*R_301[0]+Q_101000101*R_302[0]; double QR_000001001201=Q_000001001*R_201[0]+-1*Q_000001101*R_202[0]+-1*Q_000101001*R_211[0]+Q_000101101*R_212[0]; double QR_000000002201=Q_000000002*R_201[0]+-1*Q_000000102*R_202[0]+Q_000000202*R_203[0]; double QR_002000000210=Q_002000000*R_210[0]+-1*Q_102000000*R_310[0]+Q_202000000*R_410[0]; double QR_001001000210=Q_001001000*R_210[0]+-1*Q_001101000*R_220[0]+-1*Q_101001000*R_310[0]+Q_101101000*R_320[0]; double QR_000002000210=Q_000002000*R_210[0]+-1*Q_000102000*R_220[0]+Q_000202000*R_230[0]; double QR_001000001210=Q_001000001*R_210[0]+-1*Q_001000101*R_211[0]+-1*Q_101000001*R_310[0]+Q_101000101*R_311[0]; double QR_000001001210=Q_000001001*R_210[0]+-1*Q_000001101*R_211[0]+-1*Q_000101001*R_220[0]+Q_000101101*R_221[0]; double QR_000000002210=Q_000000002*R_210[0]+-1*Q_000000102*R_211[0]+Q_000000202*R_212[0]; double QR_002000000300=Q_002000000*R_300[0]+-1*Q_102000000*R_400[0]+Q_202000000*R_500[0]; double QR_001001000300=Q_001001000*R_300[0]+-1*Q_001101000*R_310[0]+-1*Q_101001000*R_400[0]+Q_101101000*R_410[0]; double QR_000002000300=Q_000002000*R_300[0]+-1*Q_000102000*R_310[0]+Q_000202000*R_320[0]; double QR_001000001300=Q_001000001*R_300[0]+-1*Q_001000101*R_301[0]+-1*Q_101000001*R_400[0]+Q_101000101*R_401[0]; double QR_000001001300=Q_000001001*R_300[0]+-1*Q_000001101*R_301[0]+-1*Q_000101001*R_310[0]+Q_000101101*R_311[0]; double QR_000000002300=Q_000000002*R_300[0]+-1*Q_000000102*R_301[0]+Q_000000202*R_302[0]; double Pd_101[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } double P_021000000=Pd_021[0]; double P_121000000=Pd_121[0]; double P_221000000=Pd_221[0]; double P_321000000=Pd_321[0]; double P_020001000=Pd_020[0]*Pd_001[1]; double P_020101000=Pd_020[0]*Pd_101[1]; double P_120001000=Pd_120[0]*Pd_001[1]; double P_120101000=Pd_120[0]*Pd_101[1]; double P_220001000=Pd_220[0]*Pd_001[1]; double P_220101000=Pd_220[0]*Pd_101[1]; double P_020000001=Pd_020[0]*Pd_001[2]; double P_020000101=Pd_020[0]*Pd_101[2]; double P_120000001=Pd_120[0]*Pd_001[2]; double P_120000101=Pd_120[0]*Pd_101[2]; double P_220000001=Pd_220[0]*Pd_001[2]; double P_220000101=Pd_220[0]*Pd_101[2]; double P_011010000=Pd_011[0]*Pd_010[1]; double P_011110000=Pd_011[0]*Pd_110[1]; double P_111010000=Pd_111[0]*Pd_010[1]; double P_111110000=Pd_111[0]*Pd_110[1]; double P_211010000=Pd_211[0]*Pd_010[1]; double P_211110000=Pd_211[0]*Pd_110[1]; double P_010011000=Pd_010[0]*Pd_011[1]; double P_010111000=Pd_010[0]*Pd_111[1]; double P_010211000=Pd_010[0]*Pd_211[1]; double P_110011000=Pd_110[0]*Pd_011[1]; double P_110111000=Pd_110[0]*Pd_111[1]; double P_110211000=Pd_110[0]*Pd_211[1]; double P_010010001=Pd_010[0]*Pd_010[1]*Pd_001[2]; double P_010010101=Pd_010[0]*Pd_010[1]*Pd_101[2]; double P_010110001=Pd_010[0]*Pd_110[1]*Pd_001[2]; double P_010110101=Pd_010[0]*Pd_110[1]*Pd_101[2]; double P_110010001=Pd_110[0]*Pd_010[1]*Pd_001[2]; double P_110010101=Pd_110[0]*Pd_010[1]*Pd_101[2]; double P_110110001=Pd_110[0]*Pd_110[1]*Pd_001[2]; double P_110110101=Pd_110[0]*Pd_110[1]*Pd_101[2]; double P_001020000=Pd_001[0]*Pd_020[1]; double P_001120000=Pd_001[0]*Pd_120[1]; double P_001220000=Pd_001[0]*Pd_220[1]; double P_101020000=Pd_101[0]*Pd_020[1]; double P_101120000=Pd_101[0]*Pd_120[1]; double P_101220000=Pd_101[0]*Pd_220[1]; double P_000021000=Pd_021[1]; double P_000121000=Pd_121[1]; double P_000221000=Pd_221[1]; double P_000321000=Pd_321[1]; double P_000020001=Pd_020[1]*Pd_001[2]; double P_000020101=Pd_020[1]*Pd_101[2]; double P_000120001=Pd_120[1]*Pd_001[2]; double P_000120101=Pd_120[1]*Pd_101[2]; double P_000220001=Pd_220[1]*Pd_001[2]; double P_000220101=Pd_220[1]*Pd_101[2]; double P_011000010=Pd_011[0]*Pd_010[2]; double P_011000110=Pd_011[0]*Pd_110[2]; double P_111000010=Pd_111[0]*Pd_010[2]; double P_111000110=Pd_111[0]*Pd_110[2]; double P_211000010=Pd_211[0]*Pd_010[2]; double P_211000110=Pd_211[0]*Pd_110[2]; double P_010001010=Pd_010[0]*Pd_001[1]*Pd_010[2]; double P_010001110=Pd_010[0]*Pd_001[1]*Pd_110[2]; double P_010101010=Pd_010[0]*Pd_101[1]*Pd_010[2]; double P_010101110=Pd_010[0]*Pd_101[1]*Pd_110[2]; double P_110001010=Pd_110[0]*Pd_001[1]*Pd_010[2]; double P_110001110=Pd_110[0]*Pd_001[1]*Pd_110[2]; double P_110101010=Pd_110[0]*Pd_101[1]*Pd_010[2]; double P_110101110=Pd_110[0]*Pd_101[1]*Pd_110[2]; double P_010000011=Pd_010[0]*Pd_011[2]; double P_010000111=Pd_010[0]*Pd_111[2]; double P_010000211=Pd_010[0]*Pd_211[2]; double P_110000011=Pd_110[0]*Pd_011[2]; double P_110000111=Pd_110[0]*Pd_111[2]; double P_110000211=Pd_110[0]*Pd_211[2]; double P_001010010=Pd_001[0]*Pd_010[1]*Pd_010[2]; double P_001010110=Pd_001[0]*Pd_010[1]*Pd_110[2]; double P_001110010=Pd_001[0]*Pd_110[1]*Pd_010[2]; double P_001110110=Pd_001[0]*Pd_110[1]*Pd_110[2]; double P_101010010=Pd_101[0]*Pd_010[1]*Pd_010[2]; double P_101010110=Pd_101[0]*Pd_010[1]*Pd_110[2]; double P_101110010=Pd_101[0]*Pd_110[1]*Pd_010[2]; double P_101110110=Pd_101[0]*Pd_110[1]*Pd_110[2]; double P_000011010=Pd_011[1]*Pd_010[2]; double P_000011110=Pd_011[1]*Pd_110[2]; double P_000111010=Pd_111[1]*Pd_010[2]; double P_000111110=Pd_111[1]*Pd_110[2]; double P_000211010=Pd_211[1]*Pd_010[2]; double P_000211110=Pd_211[1]*Pd_110[2]; double P_000010011=Pd_010[1]*Pd_011[2]; double P_000010111=Pd_010[1]*Pd_111[2]; double P_000010211=Pd_010[1]*Pd_211[2]; double P_000110011=Pd_110[1]*Pd_011[2]; double P_000110111=Pd_110[1]*Pd_111[2]; double P_000110211=Pd_110[1]*Pd_211[2]; double P_001000020=Pd_001[0]*Pd_020[2]; double P_001000120=Pd_001[0]*Pd_120[2]; double P_001000220=Pd_001[0]*Pd_220[2]; double P_101000020=Pd_101[0]*Pd_020[2]; double P_101000120=Pd_101[0]*Pd_120[2]; double P_101000220=Pd_101[0]*Pd_220[2]; double P_000001020=Pd_001[1]*Pd_020[2]; double P_000001120=Pd_001[1]*Pd_120[2]; double P_000001220=Pd_001[1]*Pd_220[2]; double P_000101020=Pd_101[1]*Pd_020[2]; double P_000101120=Pd_101[1]*Pd_120[2]; double P_000101220=Pd_101[1]*Pd_220[2]; double P_000000021=Pd_021[2]; double P_000000121=Pd_121[2]; double P_000000221=Pd_221[2]; double P_000000321=Pd_321[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(P_021000000*QR_002000000000+P_121000000*QR_002000000100+P_221000000*QR_002000000200+P_321000000*QR_002000000300); ans_temp[ans_id*6+0]+=Pmtrx[1]*(P_021000000*QR_001001000000+P_121000000*QR_001001000100+P_221000000*QR_001001000200+P_321000000*QR_001001000300); ans_temp[ans_id*6+0]+=Pmtrx[2]*(P_021000000*QR_000002000000+P_121000000*QR_000002000100+P_221000000*QR_000002000200+P_321000000*QR_000002000300); ans_temp[ans_id*6+0]+=Pmtrx[3]*(P_021000000*QR_001000001000+P_121000000*QR_001000001100+P_221000000*QR_001000001200+P_321000000*QR_001000001300); ans_temp[ans_id*6+0]+=Pmtrx[4]*(P_021000000*QR_000001001000+P_121000000*QR_000001001100+P_221000000*QR_000001001200+P_321000000*QR_000001001300); ans_temp[ans_id*6+0]+=Pmtrx[5]*(P_021000000*QR_000000002000+P_121000000*QR_000000002100+P_221000000*QR_000000002200+P_321000000*QR_000000002300); ans_temp[ans_id*6+0]+=Pmtrx[6]*(P_020001000*QR_002000000000+P_020101000*QR_002000000010+P_120001000*QR_002000000100+P_120101000*QR_002000000110+P_220001000*QR_002000000200+P_220101000*QR_002000000210); ans_temp[ans_id*6+0]+=Pmtrx[7]*(P_020001000*QR_001001000000+P_020101000*QR_001001000010+P_120001000*QR_001001000100+P_120101000*QR_001001000110+P_220001000*QR_001001000200+P_220101000*QR_001001000210); ans_temp[ans_id*6+0]+=Pmtrx[8]*(P_020001000*QR_000002000000+P_020101000*QR_000002000010+P_120001000*QR_000002000100+P_120101000*QR_000002000110+P_220001000*QR_000002000200+P_220101000*QR_000002000210); ans_temp[ans_id*6+0]+=Pmtrx[9]*(P_020001000*QR_001000001000+P_020101000*QR_001000001010+P_120001000*QR_001000001100+P_120101000*QR_001000001110+P_220001000*QR_001000001200+P_220101000*QR_001000001210); ans_temp[ans_id*6+0]+=Pmtrx[10]*(P_020001000*QR_000001001000+P_020101000*QR_000001001010+P_120001000*QR_000001001100+P_120101000*QR_000001001110+P_220001000*QR_000001001200+P_220101000*QR_000001001210); ans_temp[ans_id*6+0]+=Pmtrx[11]*(P_020001000*QR_000000002000+P_020101000*QR_000000002010+P_120001000*QR_000000002100+P_120101000*QR_000000002110+P_220001000*QR_000000002200+P_220101000*QR_000000002210); ans_temp[ans_id*6+0]+=Pmtrx[12]*(P_020000001*QR_002000000000+P_020000101*QR_002000000001+P_120000001*QR_002000000100+P_120000101*QR_002000000101+P_220000001*QR_002000000200+P_220000101*QR_002000000201); ans_temp[ans_id*6+0]+=Pmtrx[13]*(P_020000001*QR_001001000000+P_020000101*QR_001001000001+P_120000001*QR_001001000100+P_120000101*QR_001001000101+P_220000001*QR_001001000200+P_220000101*QR_001001000201); ans_temp[ans_id*6+0]+=Pmtrx[14]*(P_020000001*QR_000002000000+P_020000101*QR_000002000001+P_120000001*QR_000002000100+P_120000101*QR_000002000101+P_220000001*QR_000002000200+P_220000101*QR_000002000201); ans_temp[ans_id*6+0]+=Pmtrx[15]*(P_020000001*QR_001000001000+P_020000101*QR_001000001001+P_120000001*QR_001000001100+P_120000101*QR_001000001101+P_220000001*QR_001000001200+P_220000101*QR_001000001201); ans_temp[ans_id*6+0]+=Pmtrx[16]*(P_020000001*QR_000001001000+P_020000101*QR_000001001001+P_120000001*QR_000001001100+P_120000101*QR_000001001101+P_220000001*QR_000001001200+P_220000101*QR_000001001201); ans_temp[ans_id*6+0]+=Pmtrx[17]*(P_020000001*QR_000000002000+P_020000101*QR_000000002001+P_120000001*QR_000000002100+P_120000101*QR_000000002101+P_220000001*QR_000000002200+P_220000101*QR_000000002201); ans_temp[ans_id*6+1]+=Pmtrx[0]*(P_011010000*QR_002000000000+P_011110000*QR_002000000010+P_111010000*QR_002000000100+P_111110000*QR_002000000110+P_211010000*QR_002000000200+P_211110000*QR_002000000210); ans_temp[ans_id*6+1]+=Pmtrx[1]*(P_011010000*QR_001001000000+P_011110000*QR_001001000010+P_111010000*QR_001001000100+P_111110000*QR_001001000110+P_211010000*QR_001001000200+P_211110000*QR_001001000210); ans_temp[ans_id*6+1]+=Pmtrx[2]*(P_011010000*QR_000002000000+P_011110000*QR_000002000010+P_111010000*QR_000002000100+P_111110000*QR_000002000110+P_211010000*QR_000002000200+P_211110000*QR_000002000210); ans_temp[ans_id*6+1]+=Pmtrx[3]*(P_011010000*QR_001000001000+P_011110000*QR_001000001010+P_111010000*QR_001000001100+P_111110000*QR_001000001110+P_211010000*QR_001000001200+P_211110000*QR_001000001210); ans_temp[ans_id*6+1]+=Pmtrx[4]*(P_011010000*QR_000001001000+P_011110000*QR_000001001010+P_111010000*QR_000001001100+P_111110000*QR_000001001110+P_211010000*QR_000001001200+P_211110000*QR_000001001210); ans_temp[ans_id*6+1]+=Pmtrx[5]*(P_011010000*QR_000000002000+P_011110000*QR_000000002010+P_111010000*QR_000000002100+P_111110000*QR_000000002110+P_211010000*QR_000000002200+P_211110000*QR_000000002210); ans_temp[ans_id*6+1]+=Pmtrx[6]*(P_010011000*QR_002000000000+P_010111000*QR_002000000010+P_010211000*QR_002000000020+P_110011000*QR_002000000100+P_110111000*QR_002000000110+P_110211000*QR_002000000120); ans_temp[ans_id*6+1]+=Pmtrx[7]*(P_010011000*QR_001001000000+P_010111000*QR_001001000010+P_010211000*QR_001001000020+P_110011000*QR_001001000100+P_110111000*QR_001001000110+P_110211000*QR_001001000120); ans_temp[ans_id*6+1]+=Pmtrx[8]*(P_010011000*QR_000002000000+P_010111000*QR_000002000010+P_010211000*QR_000002000020+P_110011000*QR_000002000100+P_110111000*QR_000002000110+P_110211000*QR_000002000120); ans_temp[ans_id*6+1]+=Pmtrx[9]*(P_010011000*QR_001000001000+P_010111000*QR_001000001010+P_010211000*QR_001000001020+P_110011000*QR_001000001100+P_110111000*QR_001000001110+P_110211000*QR_001000001120); ans_temp[ans_id*6+1]+=Pmtrx[10]*(P_010011000*QR_000001001000+P_010111000*QR_000001001010+P_010211000*QR_000001001020+P_110011000*QR_000001001100+P_110111000*QR_000001001110+P_110211000*QR_000001001120); ans_temp[ans_id*6+1]+=Pmtrx[11]*(P_010011000*QR_000000002000+P_010111000*QR_000000002010+P_010211000*QR_000000002020+P_110011000*QR_000000002100+P_110111000*QR_000000002110+P_110211000*QR_000000002120); ans_temp[ans_id*6+1]+=Pmtrx[12]*(P_010010001*QR_002000000000+P_010010101*QR_002000000001+P_010110001*QR_002000000010+P_010110101*QR_002000000011+P_110010001*QR_002000000100+P_110010101*QR_002000000101+P_110110001*QR_002000000110+P_110110101*QR_002000000111); ans_temp[ans_id*6+1]+=Pmtrx[13]*(P_010010001*QR_001001000000+P_010010101*QR_001001000001+P_010110001*QR_001001000010+P_010110101*QR_001001000011+P_110010001*QR_001001000100+P_110010101*QR_001001000101+P_110110001*QR_001001000110+P_110110101*QR_001001000111); ans_temp[ans_id*6+1]+=Pmtrx[14]*(P_010010001*QR_000002000000+P_010010101*QR_000002000001+P_010110001*QR_000002000010+P_010110101*QR_000002000011+P_110010001*QR_000002000100+P_110010101*QR_000002000101+P_110110001*QR_000002000110+P_110110101*QR_000002000111); ans_temp[ans_id*6+1]+=Pmtrx[15]*(P_010010001*QR_001000001000+P_010010101*QR_001000001001+P_010110001*QR_001000001010+P_010110101*QR_001000001011+P_110010001*QR_001000001100+P_110010101*QR_001000001101+P_110110001*QR_001000001110+P_110110101*QR_001000001111); ans_temp[ans_id*6+1]+=Pmtrx[16]*(P_010010001*QR_000001001000+P_010010101*QR_000001001001+P_010110001*QR_000001001010+P_010110101*QR_000001001011+P_110010001*QR_000001001100+P_110010101*QR_000001001101+P_110110001*QR_000001001110+P_110110101*QR_000001001111); ans_temp[ans_id*6+1]+=Pmtrx[17]*(P_010010001*QR_000000002000+P_010010101*QR_000000002001+P_010110001*QR_000000002010+P_010110101*QR_000000002011+P_110010001*QR_000000002100+P_110010101*QR_000000002101+P_110110001*QR_000000002110+P_110110101*QR_000000002111); ans_temp[ans_id*6+2]+=Pmtrx[0]*(P_001020000*QR_002000000000+P_001120000*QR_002000000010+P_001220000*QR_002000000020+P_101020000*QR_002000000100+P_101120000*QR_002000000110+P_101220000*QR_002000000120); ans_temp[ans_id*6+2]+=Pmtrx[1]*(P_001020000*QR_001001000000+P_001120000*QR_001001000010+P_001220000*QR_001001000020+P_101020000*QR_001001000100+P_101120000*QR_001001000110+P_101220000*QR_001001000120); ans_temp[ans_id*6+2]+=Pmtrx[2]*(P_001020000*QR_000002000000+P_001120000*QR_000002000010+P_001220000*QR_000002000020+P_101020000*QR_000002000100+P_101120000*QR_000002000110+P_101220000*QR_000002000120); ans_temp[ans_id*6+2]+=Pmtrx[3]*(P_001020000*QR_001000001000+P_001120000*QR_001000001010+P_001220000*QR_001000001020+P_101020000*QR_001000001100+P_101120000*QR_001000001110+P_101220000*QR_001000001120); ans_temp[ans_id*6+2]+=Pmtrx[4]*(P_001020000*QR_000001001000+P_001120000*QR_000001001010+P_001220000*QR_000001001020+P_101020000*QR_000001001100+P_101120000*QR_000001001110+P_101220000*QR_000001001120); ans_temp[ans_id*6+2]+=Pmtrx[5]*(P_001020000*QR_000000002000+P_001120000*QR_000000002010+P_001220000*QR_000000002020+P_101020000*QR_000000002100+P_101120000*QR_000000002110+P_101220000*QR_000000002120); ans_temp[ans_id*6+2]+=Pmtrx[6]*(P_000021000*QR_002000000000+P_000121000*QR_002000000010+P_000221000*QR_002000000020+P_000321000*QR_002000000030); ans_temp[ans_id*6+2]+=Pmtrx[7]*(P_000021000*QR_001001000000+P_000121000*QR_001001000010+P_000221000*QR_001001000020+P_000321000*QR_001001000030); ans_temp[ans_id*6+2]+=Pmtrx[8]*(P_000021000*QR_000002000000+P_000121000*QR_000002000010+P_000221000*QR_000002000020+P_000321000*QR_000002000030); ans_temp[ans_id*6+2]+=Pmtrx[9]*(P_000021000*QR_001000001000+P_000121000*QR_001000001010+P_000221000*QR_001000001020+P_000321000*QR_001000001030); ans_temp[ans_id*6+2]+=Pmtrx[10]*(P_000021000*QR_000001001000+P_000121000*QR_000001001010+P_000221000*QR_000001001020+P_000321000*QR_000001001030); ans_temp[ans_id*6+2]+=Pmtrx[11]*(P_000021000*QR_000000002000+P_000121000*QR_000000002010+P_000221000*QR_000000002020+P_000321000*QR_000000002030); ans_temp[ans_id*6+2]+=Pmtrx[12]*(P_000020001*QR_002000000000+P_000020101*QR_002000000001+P_000120001*QR_002000000010+P_000120101*QR_002000000011+P_000220001*QR_002000000020+P_000220101*QR_002000000021); ans_temp[ans_id*6+2]+=Pmtrx[13]*(P_000020001*QR_001001000000+P_000020101*QR_001001000001+P_000120001*QR_001001000010+P_000120101*QR_001001000011+P_000220001*QR_001001000020+P_000220101*QR_001001000021); ans_temp[ans_id*6+2]+=Pmtrx[14]*(P_000020001*QR_000002000000+P_000020101*QR_000002000001+P_000120001*QR_000002000010+P_000120101*QR_000002000011+P_000220001*QR_000002000020+P_000220101*QR_000002000021); ans_temp[ans_id*6+2]+=Pmtrx[15]*(P_000020001*QR_001000001000+P_000020101*QR_001000001001+P_000120001*QR_001000001010+P_000120101*QR_001000001011+P_000220001*QR_001000001020+P_000220101*QR_001000001021); ans_temp[ans_id*6+2]+=Pmtrx[16]*(P_000020001*QR_000001001000+P_000020101*QR_000001001001+P_000120001*QR_000001001010+P_000120101*QR_000001001011+P_000220001*QR_000001001020+P_000220101*QR_000001001021); ans_temp[ans_id*6+2]+=Pmtrx[17]*(P_000020001*QR_000000002000+P_000020101*QR_000000002001+P_000120001*QR_000000002010+P_000120101*QR_000000002011+P_000220001*QR_000000002020+P_000220101*QR_000000002021); ans_temp[ans_id*6+3]+=Pmtrx[0]*(P_011000010*QR_002000000000+P_011000110*QR_002000000001+P_111000010*QR_002000000100+P_111000110*QR_002000000101+P_211000010*QR_002000000200+P_211000110*QR_002000000201); ans_temp[ans_id*6+3]+=Pmtrx[1]*(P_011000010*QR_001001000000+P_011000110*QR_001001000001+P_111000010*QR_001001000100+P_111000110*QR_001001000101+P_211000010*QR_001001000200+P_211000110*QR_001001000201); ans_temp[ans_id*6+3]+=Pmtrx[2]*(P_011000010*QR_000002000000+P_011000110*QR_000002000001+P_111000010*QR_000002000100+P_111000110*QR_000002000101+P_211000010*QR_000002000200+P_211000110*QR_000002000201); ans_temp[ans_id*6+3]+=Pmtrx[3]*(P_011000010*QR_001000001000+P_011000110*QR_001000001001+P_111000010*QR_001000001100+P_111000110*QR_001000001101+P_211000010*QR_001000001200+P_211000110*QR_001000001201); ans_temp[ans_id*6+3]+=Pmtrx[4]*(P_011000010*QR_000001001000+P_011000110*QR_000001001001+P_111000010*QR_000001001100+P_111000110*QR_000001001101+P_211000010*QR_000001001200+P_211000110*QR_000001001201); ans_temp[ans_id*6+3]+=Pmtrx[5]*(P_011000010*QR_000000002000+P_011000110*QR_000000002001+P_111000010*QR_000000002100+P_111000110*QR_000000002101+P_211000010*QR_000000002200+P_211000110*QR_000000002201); ans_temp[ans_id*6+3]+=Pmtrx[6]*(P_010001010*QR_002000000000+P_010001110*QR_002000000001+P_010101010*QR_002000000010+P_010101110*QR_002000000011+P_110001010*QR_002000000100+P_110001110*QR_002000000101+P_110101010*QR_002000000110+P_110101110*QR_002000000111); ans_temp[ans_id*6+3]+=Pmtrx[7]*(P_010001010*QR_001001000000+P_010001110*QR_001001000001+P_010101010*QR_001001000010+P_010101110*QR_001001000011+P_110001010*QR_001001000100+P_110001110*QR_001001000101+P_110101010*QR_001001000110+P_110101110*QR_001001000111); ans_temp[ans_id*6+3]+=Pmtrx[8]*(P_010001010*QR_000002000000+P_010001110*QR_000002000001+P_010101010*QR_000002000010+P_010101110*QR_000002000011+P_110001010*QR_000002000100+P_110001110*QR_000002000101+P_110101010*QR_000002000110+P_110101110*QR_000002000111); ans_temp[ans_id*6+3]+=Pmtrx[9]*(P_010001010*QR_001000001000+P_010001110*QR_001000001001+P_010101010*QR_001000001010+P_010101110*QR_001000001011+P_110001010*QR_001000001100+P_110001110*QR_001000001101+P_110101010*QR_001000001110+P_110101110*QR_001000001111); ans_temp[ans_id*6+3]+=Pmtrx[10]*(P_010001010*QR_000001001000+P_010001110*QR_000001001001+P_010101010*QR_000001001010+P_010101110*QR_000001001011+P_110001010*QR_000001001100+P_110001110*QR_000001001101+P_110101010*QR_000001001110+P_110101110*QR_000001001111); ans_temp[ans_id*6+3]+=Pmtrx[11]*(P_010001010*QR_000000002000+P_010001110*QR_000000002001+P_010101010*QR_000000002010+P_010101110*QR_000000002011+P_110001010*QR_000000002100+P_110001110*QR_000000002101+P_110101010*QR_000000002110+P_110101110*QR_000000002111); ans_temp[ans_id*6+3]+=Pmtrx[12]*(P_010000011*QR_002000000000+P_010000111*QR_002000000001+P_010000211*QR_002000000002+P_110000011*QR_002000000100+P_110000111*QR_002000000101+P_110000211*QR_002000000102); ans_temp[ans_id*6+3]+=Pmtrx[13]*(P_010000011*QR_001001000000+P_010000111*QR_001001000001+P_010000211*QR_001001000002+P_110000011*QR_001001000100+P_110000111*QR_001001000101+P_110000211*QR_001001000102); ans_temp[ans_id*6+3]+=Pmtrx[14]*(P_010000011*QR_000002000000+P_010000111*QR_000002000001+P_010000211*QR_000002000002+P_110000011*QR_000002000100+P_110000111*QR_000002000101+P_110000211*QR_000002000102); ans_temp[ans_id*6+3]+=Pmtrx[15]*(P_010000011*QR_001000001000+P_010000111*QR_001000001001+P_010000211*QR_001000001002+P_110000011*QR_001000001100+P_110000111*QR_001000001101+P_110000211*QR_001000001102); ans_temp[ans_id*6+3]+=Pmtrx[16]*(P_010000011*QR_000001001000+P_010000111*QR_000001001001+P_010000211*QR_000001001002+P_110000011*QR_000001001100+P_110000111*QR_000001001101+P_110000211*QR_000001001102); ans_temp[ans_id*6+3]+=Pmtrx[17]*(P_010000011*QR_000000002000+P_010000111*QR_000000002001+P_010000211*QR_000000002002+P_110000011*QR_000000002100+P_110000111*QR_000000002101+P_110000211*QR_000000002102); ans_temp[ans_id*6+4]+=Pmtrx[0]*(P_001010010*QR_002000000000+P_001010110*QR_002000000001+P_001110010*QR_002000000010+P_001110110*QR_002000000011+P_101010010*QR_002000000100+P_101010110*QR_002000000101+P_101110010*QR_002000000110+P_101110110*QR_002000000111); ans_temp[ans_id*6+4]+=Pmtrx[1]*(P_001010010*QR_001001000000+P_001010110*QR_001001000001+P_001110010*QR_001001000010+P_001110110*QR_001001000011+P_101010010*QR_001001000100+P_101010110*QR_001001000101+P_101110010*QR_001001000110+P_101110110*QR_001001000111); ans_temp[ans_id*6+4]+=Pmtrx[2]*(P_001010010*QR_000002000000+P_001010110*QR_000002000001+P_001110010*QR_000002000010+P_001110110*QR_000002000011+P_101010010*QR_000002000100+P_101010110*QR_000002000101+P_101110010*QR_000002000110+P_101110110*QR_000002000111); ans_temp[ans_id*6+4]+=Pmtrx[3]*(P_001010010*QR_001000001000+P_001010110*QR_001000001001+P_001110010*QR_001000001010+P_001110110*QR_001000001011+P_101010010*QR_001000001100+P_101010110*QR_001000001101+P_101110010*QR_001000001110+P_101110110*QR_001000001111); ans_temp[ans_id*6+4]+=Pmtrx[4]*(P_001010010*QR_000001001000+P_001010110*QR_000001001001+P_001110010*QR_000001001010+P_001110110*QR_000001001011+P_101010010*QR_000001001100+P_101010110*QR_000001001101+P_101110010*QR_000001001110+P_101110110*QR_000001001111); ans_temp[ans_id*6+4]+=Pmtrx[5]*(P_001010010*QR_000000002000+P_001010110*QR_000000002001+P_001110010*QR_000000002010+P_001110110*QR_000000002011+P_101010010*QR_000000002100+P_101010110*QR_000000002101+P_101110010*QR_000000002110+P_101110110*QR_000000002111); ans_temp[ans_id*6+4]+=Pmtrx[6]*(P_000011010*QR_002000000000+P_000011110*QR_002000000001+P_000111010*QR_002000000010+P_000111110*QR_002000000011+P_000211010*QR_002000000020+P_000211110*QR_002000000021); ans_temp[ans_id*6+4]+=Pmtrx[7]*(P_000011010*QR_001001000000+P_000011110*QR_001001000001+P_000111010*QR_001001000010+P_000111110*QR_001001000011+P_000211010*QR_001001000020+P_000211110*QR_001001000021); ans_temp[ans_id*6+4]+=Pmtrx[8]*(P_000011010*QR_000002000000+P_000011110*QR_000002000001+P_000111010*QR_000002000010+P_000111110*QR_000002000011+P_000211010*QR_000002000020+P_000211110*QR_000002000021); ans_temp[ans_id*6+4]+=Pmtrx[9]*(P_000011010*QR_001000001000+P_000011110*QR_001000001001+P_000111010*QR_001000001010+P_000111110*QR_001000001011+P_000211010*QR_001000001020+P_000211110*QR_001000001021); ans_temp[ans_id*6+4]+=Pmtrx[10]*(P_000011010*QR_000001001000+P_000011110*QR_000001001001+P_000111010*QR_000001001010+P_000111110*QR_000001001011+P_000211010*QR_000001001020+P_000211110*QR_000001001021); ans_temp[ans_id*6+4]+=Pmtrx[11]*(P_000011010*QR_000000002000+P_000011110*QR_000000002001+P_000111010*QR_000000002010+P_000111110*QR_000000002011+P_000211010*QR_000000002020+P_000211110*QR_000000002021); ans_temp[ans_id*6+4]+=Pmtrx[12]*(P_000010011*QR_002000000000+P_000010111*QR_002000000001+P_000010211*QR_002000000002+P_000110011*QR_002000000010+P_000110111*QR_002000000011+P_000110211*QR_002000000012); ans_temp[ans_id*6+4]+=Pmtrx[13]*(P_000010011*QR_001001000000+P_000010111*QR_001001000001+P_000010211*QR_001001000002+P_000110011*QR_001001000010+P_000110111*QR_001001000011+P_000110211*QR_001001000012); ans_temp[ans_id*6+4]+=Pmtrx[14]*(P_000010011*QR_000002000000+P_000010111*QR_000002000001+P_000010211*QR_000002000002+P_000110011*QR_000002000010+P_000110111*QR_000002000011+P_000110211*QR_000002000012); ans_temp[ans_id*6+4]+=Pmtrx[15]*(P_000010011*QR_001000001000+P_000010111*QR_001000001001+P_000010211*QR_001000001002+P_000110011*QR_001000001010+P_000110111*QR_001000001011+P_000110211*QR_001000001012); ans_temp[ans_id*6+4]+=Pmtrx[16]*(P_000010011*QR_000001001000+P_000010111*QR_000001001001+P_000010211*QR_000001001002+P_000110011*QR_000001001010+P_000110111*QR_000001001011+P_000110211*QR_000001001012); ans_temp[ans_id*6+4]+=Pmtrx[17]*(P_000010011*QR_000000002000+P_000010111*QR_000000002001+P_000010211*QR_000000002002+P_000110011*QR_000000002010+P_000110111*QR_000000002011+P_000110211*QR_000000002012); ans_temp[ans_id*6+5]+=Pmtrx[0]*(P_001000020*QR_002000000000+P_001000120*QR_002000000001+P_001000220*QR_002000000002+P_101000020*QR_002000000100+P_101000120*QR_002000000101+P_101000220*QR_002000000102); ans_temp[ans_id*6+5]+=Pmtrx[1]*(P_001000020*QR_001001000000+P_001000120*QR_001001000001+P_001000220*QR_001001000002+P_101000020*QR_001001000100+P_101000120*QR_001001000101+P_101000220*QR_001001000102); ans_temp[ans_id*6+5]+=Pmtrx[2]*(P_001000020*QR_000002000000+P_001000120*QR_000002000001+P_001000220*QR_000002000002+P_101000020*QR_000002000100+P_101000120*QR_000002000101+P_101000220*QR_000002000102); ans_temp[ans_id*6+5]+=Pmtrx[3]*(P_001000020*QR_001000001000+P_001000120*QR_001000001001+P_001000220*QR_001000001002+P_101000020*QR_001000001100+P_101000120*QR_001000001101+P_101000220*QR_001000001102); ans_temp[ans_id*6+5]+=Pmtrx[4]*(P_001000020*QR_000001001000+P_001000120*QR_000001001001+P_001000220*QR_000001001002+P_101000020*QR_000001001100+P_101000120*QR_000001001101+P_101000220*QR_000001001102); ans_temp[ans_id*6+5]+=Pmtrx[5]*(P_001000020*QR_000000002000+P_001000120*QR_000000002001+P_001000220*QR_000000002002+P_101000020*QR_000000002100+P_101000120*QR_000000002101+P_101000220*QR_000000002102); ans_temp[ans_id*6+5]+=Pmtrx[6]*(P_000001020*QR_002000000000+P_000001120*QR_002000000001+P_000001220*QR_002000000002+P_000101020*QR_002000000010+P_000101120*QR_002000000011+P_000101220*QR_002000000012); ans_temp[ans_id*6+5]+=Pmtrx[7]*(P_000001020*QR_001001000000+P_000001120*QR_001001000001+P_000001220*QR_001001000002+P_000101020*QR_001001000010+P_000101120*QR_001001000011+P_000101220*QR_001001000012); ans_temp[ans_id*6+5]+=Pmtrx[8]*(P_000001020*QR_000002000000+P_000001120*QR_000002000001+P_000001220*QR_000002000002+P_000101020*QR_000002000010+P_000101120*QR_000002000011+P_000101220*QR_000002000012); ans_temp[ans_id*6+5]+=Pmtrx[9]*(P_000001020*QR_001000001000+P_000001120*QR_001000001001+P_000001220*QR_001000001002+P_000101020*QR_001000001010+P_000101120*QR_001000001011+P_000101220*QR_001000001012); ans_temp[ans_id*6+5]+=Pmtrx[10]*(P_000001020*QR_000001001000+P_000001120*QR_000001001001+P_000001220*QR_000001001002+P_000101020*QR_000001001010+P_000101120*QR_000001001011+P_000101220*QR_000001001012); ans_temp[ans_id*6+5]+=Pmtrx[11]*(P_000001020*QR_000000002000+P_000001120*QR_000000002001+P_000001220*QR_000000002002+P_000101020*QR_000000002010+P_000101120*QR_000000002011+P_000101220*QR_000000002012); ans_temp[ans_id*6+5]+=Pmtrx[12]*(P_000000021*QR_002000000000+P_000000121*QR_002000000001+P_000000221*QR_002000000002+P_000000321*QR_002000000003); ans_temp[ans_id*6+5]+=Pmtrx[13]*(P_000000021*QR_001001000000+P_000000121*QR_001001000001+P_000000221*QR_001001000002+P_000000321*QR_001001000003); ans_temp[ans_id*6+5]+=Pmtrx[14]*(P_000000021*QR_000002000000+P_000000121*QR_000002000001+P_000000221*QR_000002000002+P_000000321*QR_000002000003); ans_temp[ans_id*6+5]+=Pmtrx[15]*(P_000000021*QR_001000001000+P_000000121*QR_001000001001+P_000000221*QR_001000001002+P_000000321*QR_001000001003); ans_temp[ans_id*6+5]+=Pmtrx[16]*(P_000000021*QR_000001001000+P_000000121*QR_000001001001+P_000000221*QR_000001001002+P_000000321*QR_000001001003); ans_temp[ans_id*6+5]+=Pmtrx[17]*(P_000000021*QR_000000002000+P_000000121*QR_000000002001+P_000000221*QR_000000002002+P_000000321*QR_000000002003); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } } __global__ void MD_Kp_ddsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_010[3]; temp_int2=tex1Dfetch(tex_PA,jj*3+0); Pd_010[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+1); Pd_010[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+2); Pd_010[2]=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[7]; Ft_fs_6(6,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; R_000[6]*=64*alphaT*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[6]; double R_200[5]; double R_300[4]; double R_400[3]; double R_500[2]; double R_600[1]; double R_010[6]; double R_110[5]; double R_210[4]; double R_310[3]; double R_410[2]; double R_510[1]; double R_020[5]; double R_120[4]; double R_220[3]; double R_320[2]; double R_420[1]; double R_030[4]; double R_130[3]; double R_230[2]; double R_330[1]; double R_040[3]; double R_140[2]; double R_240[1]; double R_050[2]; double R_150[1]; double R_060[1]; double R_001[6]; double R_101[5]; double R_201[4]; double R_301[3]; double R_401[2]; double R_501[1]; double R_011[5]; double R_111[4]; double R_211[3]; double R_311[2]; double R_411[1]; double R_021[4]; double R_121[3]; double R_221[2]; double R_321[1]; double R_031[3]; double R_131[2]; double R_231[1]; double R_041[2]; double R_141[1]; double R_051[1]; double R_002[5]; double R_102[4]; double R_202[3]; double R_302[2]; double R_402[1]; double R_012[4]; double R_112[3]; double R_212[2]; double R_312[1]; double R_022[3]; double R_122[2]; double R_222[1]; double R_032[2]; double R_132[1]; double R_042[1]; double R_003[4]; double R_103[3]; double R_203[2]; double R_303[1]; double R_013[3]; double R_113[2]; double R_213[1]; double R_023[2]; double R_123[1]; double R_033[1]; double R_004[3]; double R_104[2]; double R_204[1]; double R_014[2]; double R_114[1]; double R_024[1]; double R_005[2]; double R_105[1]; double R_015[1]; double R_006[1]; for(int i=0;i<6;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<6;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<6;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<5;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<5;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<5;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<5;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<4;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<4;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<4;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<4;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<4;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<4;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<4;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<4;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<4;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<3;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<3;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<3;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<3;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<3;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<3;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<3;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<3;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<3;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<3;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<3;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<3;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<3;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<2;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<2;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<2;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<2;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<2;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<2;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<2;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<2;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<2;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<2;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<2;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<2;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<2;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<2;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<2;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<2;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<2;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<2;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<2;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<2;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<2;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } for(int i=0;i<1;i++){ R_600[i]=TX*R_500[i+1]+5*R_400[i+1]; } for(int i=0;i<1;i++){ R_510[i]=TY*R_500[i+1]; } for(int i=0;i<1;i++){ R_420[i]=TX*R_320[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_330[i]=TX*R_230[i+1]+2*R_130[i+1]; } for(int i=0;i<1;i++){ R_240[i]=TY*R_230[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_150[i]=TX*R_050[i+1]; } for(int i=0;i<1;i++){ R_060[i]=TY*R_050[i+1]+5*R_040[i+1]; } for(int i=0;i<1;i++){ R_501[i]=TZ*R_500[i+1]; } for(int i=0;i<1;i++){ R_411[i]=TY*R_401[i+1]; } for(int i=0;i<1;i++){ R_321[i]=TZ*R_320[i+1]; } for(int i=0;i<1;i++){ R_231[i]=TZ*R_230[i+1]; } for(int i=0;i<1;i++){ R_141[i]=TX*R_041[i+1]; } for(int i=0;i<1;i++){ R_051[i]=TZ*R_050[i+1]; } for(int i=0;i<1;i++){ R_402[i]=TX*R_302[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_312[i]=TY*R_302[i+1]; } for(int i=0;i<1;i++){ R_222[i]=TX*R_122[i+1]+R_022[i+1]; } for(int i=0;i<1;i++){ R_132[i]=TX*R_032[i+1]; } for(int i=0;i<1;i++){ R_042[i]=TY*R_032[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_303[i]=TX*R_203[i+1]+2*R_103[i+1]; } for(int i=0;i<1;i++){ R_213[i]=TY*R_203[i+1]; } for(int i=0;i<1;i++){ R_123[i]=TX*R_023[i+1]; } for(int i=0;i<1;i++){ R_033[i]=TY*R_023[i+1]+2*R_013[i+1]; } for(int i=0;i<1;i++){ R_204[i]=TZ*R_203[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_114[i]=TX*R_014[i+1]; } for(int i=0;i<1;i++){ R_024[i]=TZ*R_023[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_105[i]=TX*R_005[i+1]; } for(int i=0;i<1;i++){ R_015[i]=TY*R_005[i+1]; } for(int i=0;i<1;i++){ R_006[i]=TZ*R_005[i+1]+5*R_004[i+1]; } double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; double Pd_022[3]; double Pd_122[3]; double Pd_222[3]; double Pd_322[3]; double Pd_422[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_022[i]=Pd_112[i]+Pd_010[i]*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_122[i]=2*Pd_212[i]+Pd_010[i]*Pd_112[i]+aPin1*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_222[i]=3*Pd_312[i]+Pd_010[i]*Pd_212[i]+aPin1*Pd_112[i]; } for(int i=0;i<3;i++){ Pd_322[i]=Pd_010[i]*Pd_312[i]+aPin1*Pd_212[i]; } for(int i=0;i<3;i++){ Pd_422[i]=aPin1*Pd_312[i]; } double P_022000000=Pd_022[0]; double P_122000000=Pd_122[0]; double P_222000000=Pd_222[0]; double P_322000000=Pd_322[0]; double P_422000000=Pd_422[0]; double P_021001000=Pd_021[0]*Pd_001[1]; double P_021101000=Pd_021[0]*Pd_101[1]; double P_121001000=Pd_121[0]*Pd_001[1]; double P_121101000=Pd_121[0]*Pd_101[1]; double P_221001000=Pd_221[0]*Pd_001[1]; double P_221101000=Pd_221[0]*Pd_101[1]; double P_321001000=Pd_321[0]*Pd_001[1]; double P_321101000=Pd_321[0]*Pd_101[1]; double P_020002000=Pd_020[0]*Pd_002[1]; double P_020102000=Pd_020[0]*Pd_102[1]; double P_020202000=Pd_020[0]*Pd_202[1]; double P_120002000=Pd_120[0]*Pd_002[1]; double P_120102000=Pd_120[0]*Pd_102[1]; double P_120202000=Pd_120[0]*Pd_202[1]; double P_220002000=Pd_220[0]*Pd_002[1]; double P_220102000=Pd_220[0]*Pd_102[1]; double P_220202000=Pd_220[0]*Pd_202[1]; double P_021000001=Pd_021[0]*Pd_001[2]; double P_021000101=Pd_021[0]*Pd_101[2]; double P_121000001=Pd_121[0]*Pd_001[2]; double P_121000101=Pd_121[0]*Pd_101[2]; double P_221000001=Pd_221[0]*Pd_001[2]; double P_221000101=Pd_221[0]*Pd_101[2]; double P_321000001=Pd_321[0]*Pd_001[2]; double P_321000101=Pd_321[0]*Pd_101[2]; double P_020001001=Pd_020[0]*Pd_001[1]*Pd_001[2]; double P_020001101=Pd_020[0]*Pd_001[1]*Pd_101[2]; double P_020101001=Pd_020[0]*Pd_101[1]*Pd_001[2]; double P_020101101=Pd_020[0]*Pd_101[1]*Pd_101[2]; double P_120001001=Pd_120[0]*Pd_001[1]*Pd_001[2]; double P_120001101=Pd_120[0]*Pd_001[1]*Pd_101[2]; double P_120101001=Pd_120[0]*Pd_101[1]*Pd_001[2]; double P_120101101=Pd_120[0]*Pd_101[1]*Pd_101[2]; double P_220001001=Pd_220[0]*Pd_001[1]*Pd_001[2]; double P_220001101=Pd_220[0]*Pd_001[1]*Pd_101[2]; double P_220101001=Pd_220[0]*Pd_101[1]*Pd_001[2]; double P_220101101=Pd_220[0]*Pd_101[1]*Pd_101[2]; double P_020000002=Pd_020[0]*Pd_002[2]; double P_020000102=Pd_020[0]*Pd_102[2]; double P_020000202=Pd_020[0]*Pd_202[2]; double P_120000002=Pd_120[0]*Pd_002[2]; double P_120000102=Pd_120[0]*Pd_102[2]; double P_120000202=Pd_120[0]*Pd_202[2]; double P_220000002=Pd_220[0]*Pd_002[2]; double P_220000102=Pd_220[0]*Pd_102[2]; double P_220000202=Pd_220[0]*Pd_202[2]; double P_012010000=Pd_012[0]*Pd_010[1]; double P_012110000=Pd_012[0]*Pd_110[1]; double P_112010000=Pd_112[0]*Pd_010[1]; double P_112110000=Pd_112[0]*Pd_110[1]; double P_212010000=Pd_212[0]*Pd_010[1]; double P_212110000=Pd_212[0]*Pd_110[1]; double P_312010000=Pd_312[0]*Pd_010[1]; double P_312110000=Pd_312[0]*Pd_110[1]; double P_011011000=Pd_011[0]*Pd_011[1]; double P_011111000=Pd_011[0]*Pd_111[1]; double P_011211000=Pd_011[0]*Pd_211[1]; double P_111011000=Pd_111[0]*Pd_011[1]; double P_111111000=Pd_111[0]*Pd_111[1]; double P_111211000=Pd_111[0]*Pd_211[1]; double P_211011000=Pd_211[0]*Pd_011[1]; double P_211111000=Pd_211[0]*Pd_111[1]; double P_211211000=Pd_211[0]*Pd_211[1]; double P_010012000=Pd_010[0]*Pd_012[1]; double P_010112000=Pd_010[0]*Pd_112[1]; double P_010212000=Pd_010[0]*Pd_212[1]; double P_010312000=Pd_010[0]*Pd_312[1]; double P_110012000=Pd_110[0]*Pd_012[1]; double P_110112000=Pd_110[0]*Pd_112[1]; double P_110212000=Pd_110[0]*Pd_212[1]; double P_110312000=Pd_110[0]*Pd_312[1]; double P_011010001=Pd_011[0]*Pd_010[1]*Pd_001[2]; double P_011010101=Pd_011[0]*Pd_010[1]*Pd_101[2]; double P_011110001=Pd_011[0]*Pd_110[1]*Pd_001[2]; double P_011110101=Pd_011[0]*Pd_110[1]*Pd_101[2]; double P_111010001=Pd_111[0]*Pd_010[1]*Pd_001[2]; double P_111010101=Pd_111[0]*Pd_010[1]*Pd_101[2]; double P_111110001=Pd_111[0]*Pd_110[1]*Pd_001[2]; double P_111110101=Pd_111[0]*Pd_110[1]*Pd_101[2]; double P_211010001=Pd_211[0]*Pd_010[1]*Pd_001[2]; double P_211010101=Pd_211[0]*Pd_010[1]*Pd_101[2]; double P_211110001=Pd_211[0]*Pd_110[1]*Pd_001[2]; double P_211110101=Pd_211[0]*Pd_110[1]*Pd_101[2]; double P_010011001=Pd_010[0]*Pd_011[1]*Pd_001[2]; double P_010011101=Pd_010[0]*Pd_011[1]*Pd_101[2]; double P_010111001=Pd_010[0]*Pd_111[1]*Pd_001[2]; double P_010111101=Pd_010[0]*Pd_111[1]*Pd_101[2]; double P_010211001=Pd_010[0]*Pd_211[1]*Pd_001[2]; double P_010211101=Pd_010[0]*Pd_211[1]*Pd_101[2]; double P_110011001=Pd_110[0]*Pd_011[1]*Pd_001[2]; double P_110011101=Pd_110[0]*Pd_011[1]*Pd_101[2]; double P_110111001=Pd_110[0]*Pd_111[1]*Pd_001[2]; double P_110111101=Pd_110[0]*Pd_111[1]*Pd_101[2]; double P_110211001=Pd_110[0]*Pd_211[1]*Pd_001[2]; double P_110211101=Pd_110[0]*Pd_211[1]*Pd_101[2]; double P_010010002=Pd_010[0]*Pd_010[1]*Pd_002[2]; double P_010010102=Pd_010[0]*Pd_010[1]*Pd_102[2]; double P_010010202=Pd_010[0]*Pd_010[1]*Pd_202[2]; double P_010110002=Pd_010[0]*Pd_110[1]*Pd_002[2]; double P_010110102=Pd_010[0]*Pd_110[1]*Pd_102[2]; double P_010110202=Pd_010[0]*Pd_110[1]*Pd_202[2]; double P_110010002=Pd_110[0]*Pd_010[1]*Pd_002[2]; double P_110010102=Pd_110[0]*Pd_010[1]*Pd_102[2]; double P_110010202=Pd_110[0]*Pd_010[1]*Pd_202[2]; double P_110110002=Pd_110[0]*Pd_110[1]*Pd_002[2]; double P_110110102=Pd_110[0]*Pd_110[1]*Pd_102[2]; double P_110110202=Pd_110[0]*Pd_110[1]*Pd_202[2]; double P_002020000=Pd_002[0]*Pd_020[1]; double P_002120000=Pd_002[0]*Pd_120[1]; double P_002220000=Pd_002[0]*Pd_220[1]; double P_102020000=Pd_102[0]*Pd_020[1]; double P_102120000=Pd_102[0]*Pd_120[1]; double P_102220000=Pd_102[0]*Pd_220[1]; double P_202020000=Pd_202[0]*Pd_020[1]; double P_202120000=Pd_202[0]*Pd_120[1]; double P_202220000=Pd_202[0]*Pd_220[1]; double P_001021000=Pd_001[0]*Pd_021[1]; double P_001121000=Pd_001[0]*Pd_121[1]; double P_001221000=Pd_001[0]*Pd_221[1]; double P_001321000=Pd_001[0]*Pd_321[1]; double P_101021000=Pd_101[0]*Pd_021[1]; double P_101121000=Pd_101[0]*Pd_121[1]; double P_101221000=Pd_101[0]*Pd_221[1]; double P_101321000=Pd_101[0]*Pd_321[1]; double P_000022000=Pd_022[1]; double P_000122000=Pd_122[1]; double P_000222000=Pd_222[1]; double P_000322000=Pd_322[1]; double P_000422000=Pd_422[1]; double P_001020001=Pd_001[0]*Pd_020[1]*Pd_001[2]; double P_001020101=Pd_001[0]*Pd_020[1]*Pd_101[2]; double P_001120001=Pd_001[0]*Pd_120[1]*Pd_001[2]; double P_001120101=Pd_001[0]*Pd_120[1]*Pd_101[2]; double P_001220001=Pd_001[0]*Pd_220[1]*Pd_001[2]; double P_001220101=Pd_001[0]*Pd_220[1]*Pd_101[2]; double P_101020001=Pd_101[0]*Pd_020[1]*Pd_001[2]; double P_101020101=Pd_101[0]*Pd_020[1]*Pd_101[2]; double P_101120001=Pd_101[0]*Pd_120[1]*Pd_001[2]; double P_101120101=Pd_101[0]*Pd_120[1]*Pd_101[2]; double P_101220001=Pd_101[0]*Pd_220[1]*Pd_001[2]; double P_101220101=Pd_101[0]*Pd_220[1]*Pd_101[2]; double P_000021001=Pd_021[1]*Pd_001[2]; double P_000021101=Pd_021[1]*Pd_101[2]; double P_000121001=Pd_121[1]*Pd_001[2]; double P_000121101=Pd_121[1]*Pd_101[2]; double P_000221001=Pd_221[1]*Pd_001[2]; double P_000221101=Pd_221[1]*Pd_101[2]; double P_000321001=Pd_321[1]*Pd_001[2]; double P_000321101=Pd_321[1]*Pd_101[2]; double P_000020002=Pd_020[1]*Pd_002[2]; double P_000020102=Pd_020[1]*Pd_102[2]; double P_000020202=Pd_020[1]*Pd_202[2]; double P_000120002=Pd_120[1]*Pd_002[2]; double P_000120102=Pd_120[1]*Pd_102[2]; double P_000120202=Pd_120[1]*Pd_202[2]; double P_000220002=Pd_220[1]*Pd_002[2]; double P_000220102=Pd_220[1]*Pd_102[2]; double P_000220202=Pd_220[1]*Pd_202[2]; double P_012000010=Pd_012[0]*Pd_010[2]; double P_012000110=Pd_012[0]*Pd_110[2]; double P_112000010=Pd_112[0]*Pd_010[2]; double P_112000110=Pd_112[0]*Pd_110[2]; double P_212000010=Pd_212[0]*Pd_010[2]; double P_212000110=Pd_212[0]*Pd_110[2]; double P_312000010=Pd_312[0]*Pd_010[2]; double P_312000110=Pd_312[0]*Pd_110[2]; double P_011001010=Pd_011[0]*Pd_001[1]*Pd_010[2]; double P_011001110=Pd_011[0]*Pd_001[1]*Pd_110[2]; double P_011101010=Pd_011[0]*Pd_101[1]*Pd_010[2]; double P_011101110=Pd_011[0]*Pd_101[1]*Pd_110[2]; double P_111001010=Pd_111[0]*Pd_001[1]*Pd_010[2]; double P_111001110=Pd_111[0]*Pd_001[1]*Pd_110[2]; double P_111101010=Pd_111[0]*Pd_101[1]*Pd_010[2]; double P_111101110=Pd_111[0]*Pd_101[1]*Pd_110[2]; double P_211001010=Pd_211[0]*Pd_001[1]*Pd_010[2]; double P_211001110=Pd_211[0]*Pd_001[1]*Pd_110[2]; double P_211101010=Pd_211[0]*Pd_101[1]*Pd_010[2]; double P_211101110=Pd_211[0]*Pd_101[1]*Pd_110[2]; double P_010002010=Pd_010[0]*Pd_002[1]*Pd_010[2]; double P_010002110=Pd_010[0]*Pd_002[1]*Pd_110[2]; double P_010102010=Pd_010[0]*Pd_102[1]*Pd_010[2]; double P_010102110=Pd_010[0]*Pd_102[1]*Pd_110[2]; double P_010202010=Pd_010[0]*Pd_202[1]*Pd_010[2]; double P_010202110=Pd_010[0]*Pd_202[1]*Pd_110[2]; double P_110002010=Pd_110[0]*Pd_002[1]*Pd_010[2]; double P_110002110=Pd_110[0]*Pd_002[1]*Pd_110[2]; double P_110102010=Pd_110[0]*Pd_102[1]*Pd_010[2]; double P_110102110=Pd_110[0]*Pd_102[1]*Pd_110[2]; double P_110202010=Pd_110[0]*Pd_202[1]*Pd_010[2]; double P_110202110=Pd_110[0]*Pd_202[1]*Pd_110[2]; double P_011000011=Pd_011[0]*Pd_011[2]; double P_011000111=Pd_011[0]*Pd_111[2]; double P_011000211=Pd_011[0]*Pd_211[2]; double P_111000011=Pd_111[0]*Pd_011[2]; double P_111000111=Pd_111[0]*Pd_111[2]; double P_111000211=Pd_111[0]*Pd_211[2]; double P_211000011=Pd_211[0]*Pd_011[2]; double P_211000111=Pd_211[0]*Pd_111[2]; double P_211000211=Pd_211[0]*Pd_211[2]; double P_010001011=Pd_010[0]*Pd_001[1]*Pd_011[2]; double P_010001111=Pd_010[0]*Pd_001[1]*Pd_111[2]; double P_010001211=Pd_010[0]*Pd_001[1]*Pd_211[2]; double P_010101011=Pd_010[0]*Pd_101[1]*Pd_011[2]; double P_010101111=Pd_010[0]*Pd_101[1]*Pd_111[2]; double P_010101211=Pd_010[0]*Pd_101[1]*Pd_211[2]; double P_110001011=Pd_110[0]*Pd_001[1]*Pd_011[2]; double P_110001111=Pd_110[0]*Pd_001[1]*Pd_111[2]; double P_110001211=Pd_110[0]*Pd_001[1]*Pd_211[2]; double P_110101011=Pd_110[0]*Pd_101[1]*Pd_011[2]; double P_110101111=Pd_110[0]*Pd_101[1]*Pd_111[2]; double P_110101211=Pd_110[0]*Pd_101[1]*Pd_211[2]; double P_010000012=Pd_010[0]*Pd_012[2]; double P_010000112=Pd_010[0]*Pd_112[2]; double P_010000212=Pd_010[0]*Pd_212[2]; double P_010000312=Pd_010[0]*Pd_312[2]; double P_110000012=Pd_110[0]*Pd_012[2]; double P_110000112=Pd_110[0]*Pd_112[2]; double P_110000212=Pd_110[0]*Pd_212[2]; double P_110000312=Pd_110[0]*Pd_312[2]; double P_002010010=Pd_002[0]*Pd_010[1]*Pd_010[2]; double P_002010110=Pd_002[0]*Pd_010[1]*Pd_110[2]; double P_002110010=Pd_002[0]*Pd_110[1]*Pd_010[2]; double P_002110110=Pd_002[0]*Pd_110[1]*Pd_110[2]; double P_102010010=Pd_102[0]*Pd_010[1]*Pd_010[2]; double P_102010110=Pd_102[0]*Pd_010[1]*Pd_110[2]; double P_102110010=Pd_102[0]*Pd_110[1]*Pd_010[2]; double P_102110110=Pd_102[0]*Pd_110[1]*Pd_110[2]; double P_202010010=Pd_202[0]*Pd_010[1]*Pd_010[2]; double P_202010110=Pd_202[0]*Pd_010[1]*Pd_110[2]; double P_202110010=Pd_202[0]*Pd_110[1]*Pd_010[2]; double P_202110110=Pd_202[0]*Pd_110[1]*Pd_110[2]; double P_001011010=Pd_001[0]*Pd_011[1]*Pd_010[2]; double P_001011110=Pd_001[0]*Pd_011[1]*Pd_110[2]; double P_001111010=Pd_001[0]*Pd_111[1]*Pd_010[2]; double P_001111110=Pd_001[0]*Pd_111[1]*Pd_110[2]; double P_001211010=Pd_001[0]*Pd_211[1]*Pd_010[2]; double P_001211110=Pd_001[0]*Pd_211[1]*Pd_110[2]; double P_101011010=Pd_101[0]*Pd_011[1]*Pd_010[2]; double P_101011110=Pd_101[0]*Pd_011[1]*Pd_110[2]; double P_101111010=Pd_101[0]*Pd_111[1]*Pd_010[2]; double P_101111110=Pd_101[0]*Pd_111[1]*Pd_110[2]; double P_101211010=Pd_101[0]*Pd_211[1]*Pd_010[2]; double P_101211110=Pd_101[0]*Pd_211[1]*Pd_110[2]; double P_000012010=Pd_012[1]*Pd_010[2]; double P_000012110=Pd_012[1]*Pd_110[2]; double P_000112010=Pd_112[1]*Pd_010[2]; double P_000112110=Pd_112[1]*Pd_110[2]; double P_000212010=Pd_212[1]*Pd_010[2]; double P_000212110=Pd_212[1]*Pd_110[2]; double P_000312010=Pd_312[1]*Pd_010[2]; double P_000312110=Pd_312[1]*Pd_110[2]; double P_001010011=Pd_001[0]*Pd_010[1]*Pd_011[2]; double P_001010111=Pd_001[0]*Pd_010[1]*Pd_111[2]; double P_001010211=Pd_001[0]*Pd_010[1]*Pd_211[2]; double P_001110011=Pd_001[0]*Pd_110[1]*Pd_011[2]; double P_001110111=Pd_001[0]*Pd_110[1]*Pd_111[2]; double P_001110211=Pd_001[0]*Pd_110[1]*Pd_211[2]; double P_101010011=Pd_101[0]*Pd_010[1]*Pd_011[2]; double P_101010111=Pd_101[0]*Pd_010[1]*Pd_111[2]; double P_101010211=Pd_101[0]*Pd_010[1]*Pd_211[2]; double P_101110011=Pd_101[0]*Pd_110[1]*Pd_011[2]; double P_101110111=Pd_101[0]*Pd_110[1]*Pd_111[2]; double P_101110211=Pd_101[0]*Pd_110[1]*Pd_211[2]; double P_000011011=Pd_011[1]*Pd_011[2]; double P_000011111=Pd_011[1]*Pd_111[2]; double P_000011211=Pd_011[1]*Pd_211[2]; double P_000111011=Pd_111[1]*Pd_011[2]; double P_000111111=Pd_111[1]*Pd_111[2]; double P_000111211=Pd_111[1]*Pd_211[2]; double P_000211011=Pd_211[1]*Pd_011[2]; double P_000211111=Pd_211[1]*Pd_111[2]; double P_000211211=Pd_211[1]*Pd_211[2]; double P_000010012=Pd_010[1]*Pd_012[2]; double P_000010112=Pd_010[1]*Pd_112[2]; double P_000010212=Pd_010[1]*Pd_212[2]; double P_000010312=Pd_010[1]*Pd_312[2]; double P_000110012=Pd_110[1]*Pd_012[2]; double P_000110112=Pd_110[1]*Pd_112[2]; double P_000110212=Pd_110[1]*Pd_212[2]; double P_000110312=Pd_110[1]*Pd_312[2]; double P_002000020=Pd_002[0]*Pd_020[2]; double P_002000120=Pd_002[0]*Pd_120[2]; double P_002000220=Pd_002[0]*Pd_220[2]; double P_102000020=Pd_102[0]*Pd_020[2]; double P_102000120=Pd_102[0]*Pd_120[2]; double P_102000220=Pd_102[0]*Pd_220[2]; double P_202000020=Pd_202[0]*Pd_020[2]; double P_202000120=Pd_202[0]*Pd_120[2]; double P_202000220=Pd_202[0]*Pd_220[2]; double P_001001020=Pd_001[0]*Pd_001[1]*Pd_020[2]; double P_001001120=Pd_001[0]*Pd_001[1]*Pd_120[2]; double P_001001220=Pd_001[0]*Pd_001[1]*Pd_220[2]; double P_001101020=Pd_001[0]*Pd_101[1]*Pd_020[2]; double P_001101120=Pd_001[0]*Pd_101[1]*Pd_120[2]; double P_001101220=Pd_001[0]*Pd_101[1]*Pd_220[2]; double P_101001020=Pd_101[0]*Pd_001[1]*Pd_020[2]; double P_101001120=Pd_101[0]*Pd_001[1]*Pd_120[2]; double P_101001220=Pd_101[0]*Pd_001[1]*Pd_220[2]; double P_101101020=Pd_101[0]*Pd_101[1]*Pd_020[2]; double P_101101120=Pd_101[0]*Pd_101[1]*Pd_120[2]; double P_101101220=Pd_101[0]*Pd_101[1]*Pd_220[2]; double P_000002020=Pd_002[1]*Pd_020[2]; double P_000002120=Pd_002[1]*Pd_120[2]; double P_000002220=Pd_002[1]*Pd_220[2]; double P_000102020=Pd_102[1]*Pd_020[2]; double P_000102120=Pd_102[1]*Pd_120[2]; double P_000102220=Pd_102[1]*Pd_220[2]; double P_000202020=Pd_202[1]*Pd_020[2]; double P_000202120=Pd_202[1]*Pd_120[2]; double P_000202220=Pd_202[1]*Pd_220[2]; double P_001000021=Pd_001[0]*Pd_021[2]; double P_001000121=Pd_001[0]*Pd_121[2]; double P_001000221=Pd_001[0]*Pd_221[2]; double P_001000321=Pd_001[0]*Pd_321[2]; double P_101000021=Pd_101[0]*Pd_021[2]; double P_101000121=Pd_101[0]*Pd_121[2]; double P_101000221=Pd_101[0]*Pd_221[2]; double P_101000321=Pd_101[0]*Pd_321[2]; double P_000001021=Pd_001[1]*Pd_021[2]; double P_000001121=Pd_001[1]*Pd_121[2]; double P_000001221=Pd_001[1]*Pd_221[2]; double P_000001321=Pd_001[1]*Pd_321[2]; double P_000101021=Pd_101[1]*Pd_021[2]; double P_000101121=Pd_101[1]*Pd_121[2]; double P_000101221=Pd_101[1]*Pd_221[2]; double P_000101321=Pd_101[1]*Pd_321[2]; double P_000000022=Pd_022[2]; double P_000000122=Pd_122[2]; double P_000000222=Pd_222[2]; double P_000000322=Pd_322[2]; double P_000000422=Pd_422[2]; double PR_022000000000=P_022000000*R_000[0]+-1*P_122000000*R_100[0]+P_222000000*R_200[0]+-1*P_322000000*R_300[0]+P_422000000*R_400[0]; double PR_021001000000=P_021001000*R_000[0]+-1*P_021101000*R_010[0]+-1*P_121001000*R_100[0]+P_121101000*R_110[0]+P_221001000*R_200[0]+-1*P_221101000*R_210[0]+-1*P_321001000*R_300[0]+P_321101000*R_310[0]; double PR_020002000000=P_020002000*R_000[0]+-1*P_020102000*R_010[0]+P_020202000*R_020[0]+-1*P_120002000*R_100[0]+P_120102000*R_110[0]+-1*P_120202000*R_120[0]+P_220002000*R_200[0]+-1*P_220102000*R_210[0]+P_220202000*R_220[0]; double PR_021000001000=P_021000001*R_000[0]+-1*P_021000101*R_001[0]+-1*P_121000001*R_100[0]+P_121000101*R_101[0]+P_221000001*R_200[0]+-1*P_221000101*R_201[0]+-1*P_321000001*R_300[0]+P_321000101*R_301[0]; double PR_020001001000=P_020001001*R_000[0]+-1*P_020001101*R_001[0]+-1*P_020101001*R_010[0]+P_020101101*R_011[0]+-1*P_120001001*R_100[0]+P_120001101*R_101[0]+P_120101001*R_110[0]+-1*P_120101101*R_111[0]+P_220001001*R_200[0]+-1*P_220001101*R_201[0]+-1*P_220101001*R_210[0]+P_220101101*R_211[0]; double PR_020000002000=P_020000002*R_000[0]+-1*P_020000102*R_001[0]+P_020000202*R_002[0]+-1*P_120000002*R_100[0]+P_120000102*R_101[0]+-1*P_120000202*R_102[0]+P_220000002*R_200[0]+-1*P_220000102*R_201[0]+P_220000202*R_202[0]; double PR_012010000000=P_012010000*R_000[0]+-1*P_012110000*R_010[0]+-1*P_112010000*R_100[0]+P_112110000*R_110[0]+P_212010000*R_200[0]+-1*P_212110000*R_210[0]+-1*P_312010000*R_300[0]+P_312110000*R_310[0]; double PR_011011000000=P_011011000*R_000[0]+-1*P_011111000*R_010[0]+P_011211000*R_020[0]+-1*P_111011000*R_100[0]+P_111111000*R_110[0]+-1*P_111211000*R_120[0]+P_211011000*R_200[0]+-1*P_211111000*R_210[0]+P_211211000*R_220[0]; double PR_010012000000=P_010012000*R_000[0]+-1*P_010112000*R_010[0]+P_010212000*R_020[0]+-1*P_010312000*R_030[0]+-1*P_110012000*R_100[0]+P_110112000*R_110[0]+-1*P_110212000*R_120[0]+P_110312000*R_130[0]; double PR_011010001000=P_011010001*R_000[0]+-1*P_011010101*R_001[0]+-1*P_011110001*R_010[0]+P_011110101*R_011[0]+-1*P_111010001*R_100[0]+P_111010101*R_101[0]+P_111110001*R_110[0]+-1*P_111110101*R_111[0]+P_211010001*R_200[0]+-1*P_211010101*R_201[0]+-1*P_211110001*R_210[0]+P_211110101*R_211[0]; double PR_010011001000=P_010011001*R_000[0]+-1*P_010011101*R_001[0]+-1*P_010111001*R_010[0]+P_010111101*R_011[0]+P_010211001*R_020[0]+-1*P_010211101*R_021[0]+-1*P_110011001*R_100[0]+P_110011101*R_101[0]+P_110111001*R_110[0]+-1*P_110111101*R_111[0]+-1*P_110211001*R_120[0]+P_110211101*R_121[0]; double PR_010010002000=P_010010002*R_000[0]+-1*P_010010102*R_001[0]+P_010010202*R_002[0]+-1*P_010110002*R_010[0]+P_010110102*R_011[0]+-1*P_010110202*R_012[0]+-1*P_110010002*R_100[0]+P_110010102*R_101[0]+-1*P_110010202*R_102[0]+P_110110002*R_110[0]+-1*P_110110102*R_111[0]+P_110110202*R_112[0]; double PR_002020000000=P_002020000*R_000[0]+-1*P_002120000*R_010[0]+P_002220000*R_020[0]+-1*P_102020000*R_100[0]+P_102120000*R_110[0]+-1*P_102220000*R_120[0]+P_202020000*R_200[0]+-1*P_202120000*R_210[0]+P_202220000*R_220[0]; double PR_001021000000=P_001021000*R_000[0]+-1*P_001121000*R_010[0]+P_001221000*R_020[0]+-1*P_001321000*R_030[0]+-1*P_101021000*R_100[0]+P_101121000*R_110[0]+-1*P_101221000*R_120[0]+P_101321000*R_130[0]; double PR_000022000000=P_000022000*R_000[0]+-1*P_000122000*R_010[0]+P_000222000*R_020[0]+-1*P_000322000*R_030[0]+P_000422000*R_040[0]; double PR_001020001000=P_001020001*R_000[0]+-1*P_001020101*R_001[0]+-1*P_001120001*R_010[0]+P_001120101*R_011[0]+P_001220001*R_020[0]+-1*P_001220101*R_021[0]+-1*P_101020001*R_100[0]+P_101020101*R_101[0]+P_101120001*R_110[0]+-1*P_101120101*R_111[0]+-1*P_101220001*R_120[0]+P_101220101*R_121[0]; double PR_000021001000=P_000021001*R_000[0]+-1*P_000021101*R_001[0]+-1*P_000121001*R_010[0]+P_000121101*R_011[0]+P_000221001*R_020[0]+-1*P_000221101*R_021[0]+-1*P_000321001*R_030[0]+P_000321101*R_031[0]; double PR_000020002000=P_000020002*R_000[0]+-1*P_000020102*R_001[0]+P_000020202*R_002[0]+-1*P_000120002*R_010[0]+P_000120102*R_011[0]+-1*P_000120202*R_012[0]+P_000220002*R_020[0]+-1*P_000220102*R_021[0]+P_000220202*R_022[0]; double PR_012000010000=P_012000010*R_000[0]+-1*P_012000110*R_001[0]+-1*P_112000010*R_100[0]+P_112000110*R_101[0]+P_212000010*R_200[0]+-1*P_212000110*R_201[0]+-1*P_312000010*R_300[0]+P_312000110*R_301[0]; double PR_011001010000=P_011001010*R_000[0]+-1*P_011001110*R_001[0]+-1*P_011101010*R_010[0]+P_011101110*R_011[0]+-1*P_111001010*R_100[0]+P_111001110*R_101[0]+P_111101010*R_110[0]+-1*P_111101110*R_111[0]+P_211001010*R_200[0]+-1*P_211001110*R_201[0]+-1*P_211101010*R_210[0]+P_211101110*R_211[0]; double PR_010002010000=P_010002010*R_000[0]+-1*P_010002110*R_001[0]+-1*P_010102010*R_010[0]+P_010102110*R_011[0]+P_010202010*R_020[0]+-1*P_010202110*R_021[0]+-1*P_110002010*R_100[0]+P_110002110*R_101[0]+P_110102010*R_110[0]+-1*P_110102110*R_111[0]+-1*P_110202010*R_120[0]+P_110202110*R_121[0]; double PR_011000011000=P_011000011*R_000[0]+-1*P_011000111*R_001[0]+P_011000211*R_002[0]+-1*P_111000011*R_100[0]+P_111000111*R_101[0]+-1*P_111000211*R_102[0]+P_211000011*R_200[0]+-1*P_211000111*R_201[0]+P_211000211*R_202[0]; double PR_010001011000=P_010001011*R_000[0]+-1*P_010001111*R_001[0]+P_010001211*R_002[0]+-1*P_010101011*R_010[0]+P_010101111*R_011[0]+-1*P_010101211*R_012[0]+-1*P_110001011*R_100[0]+P_110001111*R_101[0]+-1*P_110001211*R_102[0]+P_110101011*R_110[0]+-1*P_110101111*R_111[0]+P_110101211*R_112[0]; double PR_010000012000=P_010000012*R_000[0]+-1*P_010000112*R_001[0]+P_010000212*R_002[0]+-1*P_010000312*R_003[0]+-1*P_110000012*R_100[0]+P_110000112*R_101[0]+-1*P_110000212*R_102[0]+P_110000312*R_103[0]; double PR_002010010000=P_002010010*R_000[0]+-1*P_002010110*R_001[0]+-1*P_002110010*R_010[0]+P_002110110*R_011[0]+-1*P_102010010*R_100[0]+P_102010110*R_101[0]+P_102110010*R_110[0]+-1*P_102110110*R_111[0]+P_202010010*R_200[0]+-1*P_202010110*R_201[0]+-1*P_202110010*R_210[0]+P_202110110*R_211[0]; double PR_001011010000=P_001011010*R_000[0]+-1*P_001011110*R_001[0]+-1*P_001111010*R_010[0]+P_001111110*R_011[0]+P_001211010*R_020[0]+-1*P_001211110*R_021[0]+-1*P_101011010*R_100[0]+P_101011110*R_101[0]+P_101111010*R_110[0]+-1*P_101111110*R_111[0]+-1*P_101211010*R_120[0]+P_101211110*R_121[0]; double PR_000012010000=P_000012010*R_000[0]+-1*P_000012110*R_001[0]+-1*P_000112010*R_010[0]+P_000112110*R_011[0]+P_000212010*R_020[0]+-1*P_000212110*R_021[0]+-1*P_000312010*R_030[0]+P_000312110*R_031[0]; double PR_001010011000=P_001010011*R_000[0]+-1*P_001010111*R_001[0]+P_001010211*R_002[0]+-1*P_001110011*R_010[0]+P_001110111*R_011[0]+-1*P_001110211*R_012[0]+-1*P_101010011*R_100[0]+P_101010111*R_101[0]+-1*P_101010211*R_102[0]+P_101110011*R_110[0]+-1*P_101110111*R_111[0]+P_101110211*R_112[0]; double PR_000011011000=P_000011011*R_000[0]+-1*P_000011111*R_001[0]+P_000011211*R_002[0]+-1*P_000111011*R_010[0]+P_000111111*R_011[0]+-1*P_000111211*R_012[0]+P_000211011*R_020[0]+-1*P_000211111*R_021[0]+P_000211211*R_022[0]; double PR_000010012000=P_000010012*R_000[0]+-1*P_000010112*R_001[0]+P_000010212*R_002[0]+-1*P_000010312*R_003[0]+-1*P_000110012*R_010[0]+P_000110112*R_011[0]+-1*P_000110212*R_012[0]+P_000110312*R_013[0]; double PR_002000020000=P_002000020*R_000[0]+-1*P_002000120*R_001[0]+P_002000220*R_002[0]+-1*P_102000020*R_100[0]+P_102000120*R_101[0]+-1*P_102000220*R_102[0]+P_202000020*R_200[0]+-1*P_202000120*R_201[0]+P_202000220*R_202[0]; double PR_001001020000=P_001001020*R_000[0]+-1*P_001001120*R_001[0]+P_001001220*R_002[0]+-1*P_001101020*R_010[0]+P_001101120*R_011[0]+-1*P_001101220*R_012[0]+-1*P_101001020*R_100[0]+P_101001120*R_101[0]+-1*P_101001220*R_102[0]+P_101101020*R_110[0]+-1*P_101101120*R_111[0]+P_101101220*R_112[0]; double PR_000002020000=P_000002020*R_000[0]+-1*P_000002120*R_001[0]+P_000002220*R_002[0]+-1*P_000102020*R_010[0]+P_000102120*R_011[0]+-1*P_000102220*R_012[0]+P_000202020*R_020[0]+-1*P_000202120*R_021[0]+P_000202220*R_022[0]; double PR_001000021000=P_001000021*R_000[0]+-1*P_001000121*R_001[0]+P_001000221*R_002[0]+-1*P_001000321*R_003[0]+-1*P_101000021*R_100[0]+P_101000121*R_101[0]+-1*P_101000221*R_102[0]+P_101000321*R_103[0]; double PR_000001021000=P_000001021*R_000[0]+-1*P_000001121*R_001[0]+P_000001221*R_002[0]+-1*P_000001321*R_003[0]+-1*P_000101021*R_010[0]+P_000101121*R_011[0]+-1*P_000101221*R_012[0]+P_000101321*R_013[0]; double PR_000000022000=P_000000022*R_000[0]+-1*P_000000122*R_001[0]+P_000000222*R_002[0]+-1*P_000000322*R_003[0]+P_000000422*R_004[0]; double PR_022000000001=P_022000000*R_001[0]+-1*P_122000000*R_101[0]+P_222000000*R_201[0]+-1*P_322000000*R_301[0]+P_422000000*R_401[0]; double PR_021001000001=P_021001000*R_001[0]+-1*P_021101000*R_011[0]+-1*P_121001000*R_101[0]+P_121101000*R_111[0]+P_221001000*R_201[0]+-1*P_221101000*R_211[0]+-1*P_321001000*R_301[0]+P_321101000*R_311[0]; double PR_020002000001=P_020002000*R_001[0]+-1*P_020102000*R_011[0]+P_020202000*R_021[0]+-1*P_120002000*R_101[0]+P_120102000*R_111[0]+-1*P_120202000*R_121[0]+P_220002000*R_201[0]+-1*P_220102000*R_211[0]+P_220202000*R_221[0]; double PR_021000001001=P_021000001*R_001[0]+-1*P_021000101*R_002[0]+-1*P_121000001*R_101[0]+P_121000101*R_102[0]+P_221000001*R_201[0]+-1*P_221000101*R_202[0]+-1*P_321000001*R_301[0]+P_321000101*R_302[0]; double PR_020001001001=P_020001001*R_001[0]+-1*P_020001101*R_002[0]+-1*P_020101001*R_011[0]+P_020101101*R_012[0]+-1*P_120001001*R_101[0]+P_120001101*R_102[0]+P_120101001*R_111[0]+-1*P_120101101*R_112[0]+P_220001001*R_201[0]+-1*P_220001101*R_202[0]+-1*P_220101001*R_211[0]+P_220101101*R_212[0]; double PR_020000002001=P_020000002*R_001[0]+-1*P_020000102*R_002[0]+P_020000202*R_003[0]+-1*P_120000002*R_101[0]+P_120000102*R_102[0]+-1*P_120000202*R_103[0]+P_220000002*R_201[0]+-1*P_220000102*R_202[0]+P_220000202*R_203[0]; double PR_012010000001=P_012010000*R_001[0]+-1*P_012110000*R_011[0]+-1*P_112010000*R_101[0]+P_112110000*R_111[0]+P_212010000*R_201[0]+-1*P_212110000*R_211[0]+-1*P_312010000*R_301[0]+P_312110000*R_311[0]; double PR_011011000001=P_011011000*R_001[0]+-1*P_011111000*R_011[0]+P_011211000*R_021[0]+-1*P_111011000*R_101[0]+P_111111000*R_111[0]+-1*P_111211000*R_121[0]+P_211011000*R_201[0]+-1*P_211111000*R_211[0]+P_211211000*R_221[0]; double PR_010012000001=P_010012000*R_001[0]+-1*P_010112000*R_011[0]+P_010212000*R_021[0]+-1*P_010312000*R_031[0]+-1*P_110012000*R_101[0]+P_110112000*R_111[0]+-1*P_110212000*R_121[0]+P_110312000*R_131[0]; double PR_011010001001=P_011010001*R_001[0]+-1*P_011010101*R_002[0]+-1*P_011110001*R_011[0]+P_011110101*R_012[0]+-1*P_111010001*R_101[0]+P_111010101*R_102[0]+P_111110001*R_111[0]+-1*P_111110101*R_112[0]+P_211010001*R_201[0]+-1*P_211010101*R_202[0]+-1*P_211110001*R_211[0]+P_211110101*R_212[0]; double PR_010011001001=P_010011001*R_001[0]+-1*P_010011101*R_002[0]+-1*P_010111001*R_011[0]+P_010111101*R_012[0]+P_010211001*R_021[0]+-1*P_010211101*R_022[0]+-1*P_110011001*R_101[0]+P_110011101*R_102[0]+P_110111001*R_111[0]+-1*P_110111101*R_112[0]+-1*P_110211001*R_121[0]+P_110211101*R_122[0]; double PR_010010002001=P_010010002*R_001[0]+-1*P_010010102*R_002[0]+P_010010202*R_003[0]+-1*P_010110002*R_011[0]+P_010110102*R_012[0]+-1*P_010110202*R_013[0]+-1*P_110010002*R_101[0]+P_110010102*R_102[0]+-1*P_110010202*R_103[0]+P_110110002*R_111[0]+-1*P_110110102*R_112[0]+P_110110202*R_113[0]; double PR_002020000001=P_002020000*R_001[0]+-1*P_002120000*R_011[0]+P_002220000*R_021[0]+-1*P_102020000*R_101[0]+P_102120000*R_111[0]+-1*P_102220000*R_121[0]+P_202020000*R_201[0]+-1*P_202120000*R_211[0]+P_202220000*R_221[0]; double PR_001021000001=P_001021000*R_001[0]+-1*P_001121000*R_011[0]+P_001221000*R_021[0]+-1*P_001321000*R_031[0]+-1*P_101021000*R_101[0]+P_101121000*R_111[0]+-1*P_101221000*R_121[0]+P_101321000*R_131[0]; double PR_000022000001=P_000022000*R_001[0]+-1*P_000122000*R_011[0]+P_000222000*R_021[0]+-1*P_000322000*R_031[0]+P_000422000*R_041[0]; double PR_001020001001=P_001020001*R_001[0]+-1*P_001020101*R_002[0]+-1*P_001120001*R_011[0]+P_001120101*R_012[0]+P_001220001*R_021[0]+-1*P_001220101*R_022[0]+-1*P_101020001*R_101[0]+P_101020101*R_102[0]+P_101120001*R_111[0]+-1*P_101120101*R_112[0]+-1*P_101220001*R_121[0]+P_101220101*R_122[0]; double PR_000021001001=P_000021001*R_001[0]+-1*P_000021101*R_002[0]+-1*P_000121001*R_011[0]+P_000121101*R_012[0]+P_000221001*R_021[0]+-1*P_000221101*R_022[0]+-1*P_000321001*R_031[0]+P_000321101*R_032[0]; double PR_000020002001=P_000020002*R_001[0]+-1*P_000020102*R_002[0]+P_000020202*R_003[0]+-1*P_000120002*R_011[0]+P_000120102*R_012[0]+-1*P_000120202*R_013[0]+P_000220002*R_021[0]+-1*P_000220102*R_022[0]+P_000220202*R_023[0]; double PR_012000010001=P_012000010*R_001[0]+-1*P_012000110*R_002[0]+-1*P_112000010*R_101[0]+P_112000110*R_102[0]+P_212000010*R_201[0]+-1*P_212000110*R_202[0]+-1*P_312000010*R_301[0]+P_312000110*R_302[0]; double PR_011001010001=P_011001010*R_001[0]+-1*P_011001110*R_002[0]+-1*P_011101010*R_011[0]+P_011101110*R_012[0]+-1*P_111001010*R_101[0]+P_111001110*R_102[0]+P_111101010*R_111[0]+-1*P_111101110*R_112[0]+P_211001010*R_201[0]+-1*P_211001110*R_202[0]+-1*P_211101010*R_211[0]+P_211101110*R_212[0]; double PR_010002010001=P_010002010*R_001[0]+-1*P_010002110*R_002[0]+-1*P_010102010*R_011[0]+P_010102110*R_012[0]+P_010202010*R_021[0]+-1*P_010202110*R_022[0]+-1*P_110002010*R_101[0]+P_110002110*R_102[0]+P_110102010*R_111[0]+-1*P_110102110*R_112[0]+-1*P_110202010*R_121[0]+P_110202110*R_122[0]; double PR_011000011001=P_011000011*R_001[0]+-1*P_011000111*R_002[0]+P_011000211*R_003[0]+-1*P_111000011*R_101[0]+P_111000111*R_102[0]+-1*P_111000211*R_103[0]+P_211000011*R_201[0]+-1*P_211000111*R_202[0]+P_211000211*R_203[0]; double PR_010001011001=P_010001011*R_001[0]+-1*P_010001111*R_002[0]+P_010001211*R_003[0]+-1*P_010101011*R_011[0]+P_010101111*R_012[0]+-1*P_010101211*R_013[0]+-1*P_110001011*R_101[0]+P_110001111*R_102[0]+-1*P_110001211*R_103[0]+P_110101011*R_111[0]+-1*P_110101111*R_112[0]+P_110101211*R_113[0]; double PR_010000012001=P_010000012*R_001[0]+-1*P_010000112*R_002[0]+P_010000212*R_003[0]+-1*P_010000312*R_004[0]+-1*P_110000012*R_101[0]+P_110000112*R_102[0]+-1*P_110000212*R_103[0]+P_110000312*R_104[0]; double PR_002010010001=P_002010010*R_001[0]+-1*P_002010110*R_002[0]+-1*P_002110010*R_011[0]+P_002110110*R_012[0]+-1*P_102010010*R_101[0]+P_102010110*R_102[0]+P_102110010*R_111[0]+-1*P_102110110*R_112[0]+P_202010010*R_201[0]+-1*P_202010110*R_202[0]+-1*P_202110010*R_211[0]+P_202110110*R_212[0]; double PR_001011010001=P_001011010*R_001[0]+-1*P_001011110*R_002[0]+-1*P_001111010*R_011[0]+P_001111110*R_012[0]+P_001211010*R_021[0]+-1*P_001211110*R_022[0]+-1*P_101011010*R_101[0]+P_101011110*R_102[0]+P_101111010*R_111[0]+-1*P_101111110*R_112[0]+-1*P_101211010*R_121[0]+P_101211110*R_122[0]; double PR_000012010001=P_000012010*R_001[0]+-1*P_000012110*R_002[0]+-1*P_000112010*R_011[0]+P_000112110*R_012[0]+P_000212010*R_021[0]+-1*P_000212110*R_022[0]+-1*P_000312010*R_031[0]+P_000312110*R_032[0]; double PR_001010011001=P_001010011*R_001[0]+-1*P_001010111*R_002[0]+P_001010211*R_003[0]+-1*P_001110011*R_011[0]+P_001110111*R_012[0]+-1*P_001110211*R_013[0]+-1*P_101010011*R_101[0]+P_101010111*R_102[0]+-1*P_101010211*R_103[0]+P_101110011*R_111[0]+-1*P_101110111*R_112[0]+P_101110211*R_113[0]; double PR_000011011001=P_000011011*R_001[0]+-1*P_000011111*R_002[0]+P_000011211*R_003[0]+-1*P_000111011*R_011[0]+P_000111111*R_012[0]+-1*P_000111211*R_013[0]+P_000211011*R_021[0]+-1*P_000211111*R_022[0]+P_000211211*R_023[0]; double PR_000010012001=P_000010012*R_001[0]+-1*P_000010112*R_002[0]+P_000010212*R_003[0]+-1*P_000010312*R_004[0]+-1*P_000110012*R_011[0]+P_000110112*R_012[0]+-1*P_000110212*R_013[0]+P_000110312*R_014[0]; double PR_002000020001=P_002000020*R_001[0]+-1*P_002000120*R_002[0]+P_002000220*R_003[0]+-1*P_102000020*R_101[0]+P_102000120*R_102[0]+-1*P_102000220*R_103[0]+P_202000020*R_201[0]+-1*P_202000120*R_202[0]+P_202000220*R_203[0]; double PR_001001020001=P_001001020*R_001[0]+-1*P_001001120*R_002[0]+P_001001220*R_003[0]+-1*P_001101020*R_011[0]+P_001101120*R_012[0]+-1*P_001101220*R_013[0]+-1*P_101001020*R_101[0]+P_101001120*R_102[0]+-1*P_101001220*R_103[0]+P_101101020*R_111[0]+-1*P_101101120*R_112[0]+P_101101220*R_113[0]; double PR_000002020001=P_000002020*R_001[0]+-1*P_000002120*R_002[0]+P_000002220*R_003[0]+-1*P_000102020*R_011[0]+P_000102120*R_012[0]+-1*P_000102220*R_013[0]+P_000202020*R_021[0]+-1*P_000202120*R_022[0]+P_000202220*R_023[0]; double PR_001000021001=P_001000021*R_001[0]+-1*P_001000121*R_002[0]+P_001000221*R_003[0]+-1*P_001000321*R_004[0]+-1*P_101000021*R_101[0]+P_101000121*R_102[0]+-1*P_101000221*R_103[0]+P_101000321*R_104[0]; double PR_000001021001=P_000001021*R_001[0]+-1*P_000001121*R_002[0]+P_000001221*R_003[0]+-1*P_000001321*R_004[0]+-1*P_000101021*R_011[0]+P_000101121*R_012[0]+-1*P_000101221*R_013[0]+P_000101321*R_014[0]; double PR_000000022001=P_000000022*R_001[0]+-1*P_000000122*R_002[0]+P_000000222*R_003[0]+-1*P_000000322*R_004[0]+P_000000422*R_005[0]; double PR_022000000010=P_022000000*R_010[0]+-1*P_122000000*R_110[0]+P_222000000*R_210[0]+-1*P_322000000*R_310[0]+P_422000000*R_410[0]; double PR_021001000010=P_021001000*R_010[0]+-1*P_021101000*R_020[0]+-1*P_121001000*R_110[0]+P_121101000*R_120[0]+P_221001000*R_210[0]+-1*P_221101000*R_220[0]+-1*P_321001000*R_310[0]+P_321101000*R_320[0]; double PR_020002000010=P_020002000*R_010[0]+-1*P_020102000*R_020[0]+P_020202000*R_030[0]+-1*P_120002000*R_110[0]+P_120102000*R_120[0]+-1*P_120202000*R_130[0]+P_220002000*R_210[0]+-1*P_220102000*R_220[0]+P_220202000*R_230[0]; double PR_021000001010=P_021000001*R_010[0]+-1*P_021000101*R_011[0]+-1*P_121000001*R_110[0]+P_121000101*R_111[0]+P_221000001*R_210[0]+-1*P_221000101*R_211[0]+-1*P_321000001*R_310[0]+P_321000101*R_311[0]; double PR_020001001010=P_020001001*R_010[0]+-1*P_020001101*R_011[0]+-1*P_020101001*R_020[0]+P_020101101*R_021[0]+-1*P_120001001*R_110[0]+P_120001101*R_111[0]+P_120101001*R_120[0]+-1*P_120101101*R_121[0]+P_220001001*R_210[0]+-1*P_220001101*R_211[0]+-1*P_220101001*R_220[0]+P_220101101*R_221[0]; double PR_020000002010=P_020000002*R_010[0]+-1*P_020000102*R_011[0]+P_020000202*R_012[0]+-1*P_120000002*R_110[0]+P_120000102*R_111[0]+-1*P_120000202*R_112[0]+P_220000002*R_210[0]+-1*P_220000102*R_211[0]+P_220000202*R_212[0]; double PR_012010000010=P_012010000*R_010[0]+-1*P_012110000*R_020[0]+-1*P_112010000*R_110[0]+P_112110000*R_120[0]+P_212010000*R_210[0]+-1*P_212110000*R_220[0]+-1*P_312010000*R_310[0]+P_312110000*R_320[0]; double PR_011011000010=P_011011000*R_010[0]+-1*P_011111000*R_020[0]+P_011211000*R_030[0]+-1*P_111011000*R_110[0]+P_111111000*R_120[0]+-1*P_111211000*R_130[0]+P_211011000*R_210[0]+-1*P_211111000*R_220[0]+P_211211000*R_230[0]; double PR_010012000010=P_010012000*R_010[0]+-1*P_010112000*R_020[0]+P_010212000*R_030[0]+-1*P_010312000*R_040[0]+-1*P_110012000*R_110[0]+P_110112000*R_120[0]+-1*P_110212000*R_130[0]+P_110312000*R_140[0]; double PR_011010001010=P_011010001*R_010[0]+-1*P_011010101*R_011[0]+-1*P_011110001*R_020[0]+P_011110101*R_021[0]+-1*P_111010001*R_110[0]+P_111010101*R_111[0]+P_111110001*R_120[0]+-1*P_111110101*R_121[0]+P_211010001*R_210[0]+-1*P_211010101*R_211[0]+-1*P_211110001*R_220[0]+P_211110101*R_221[0]; double PR_010011001010=P_010011001*R_010[0]+-1*P_010011101*R_011[0]+-1*P_010111001*R_020[0]+P_010111101*R_021[0]+P_010211001*R_030[0]+-1*P_010211101*R_031[0]+-1*P_110011001*R_110[0]+P_110011101*R_111[0]+P_110111001*R_120[0]+-1*P_110111101*R_121[0]+-1*P_110211001*R_130[0]+P_110211101*R_131[0]; double PR_010010002010=P_010010002*R_010[0]+-1*P_010010102*R_011[0]+P_010010202*R_012[0]+-1*P_010110002*R_020[0]+P_010110102*R_021[0]+-1*P_010110202*R_022[0]+-1*P_110010002*R_110[0]+P_110010102*R_111[0]+-1*P_110010202*R_112[0]+P_110110002*R_120[0]+-1*P_110110102*R_121[0]+P_110110202*R_122[0]; double PR_002020000010=P_002020000*R_010[0]+-1*P_002120000*R_020[0]+P_002220000*R_030[0]+-1*P_102020000*R_110[0]+P_102120000*R_120[0]+-1*P_102220000*R_130[0]+P_202020000*R_210[0]+-1*P_202120000*R_220[0]+P_202220000*R_230[0]; double PR_001021000010=P_001021000*R_010[0]+-1*P_001121000*R_020[0]+P_001221000*R_030[0]+-1*P_001321000*R_040[0]+-1*P_101021000*R_110[0]+P_101121000*R_120[0]+-1*P_101221000*R_130[0]+P_101321000*R_140[0]; double PR_000022000010=P_000022000*R_010[0]+-1*P_000122000*R_020[0]+P_000222000*R_030[0]+-1*P_000322000*R_040[0]+P_000422000*R_050[0]; double PR_001020001010=P_001020001*R_010[0]+-1*P_001020101*R_011[0]+-1*P_001120001*R_020[0]+P_001120101*R_021[0]+P_001220001*R_030[0]+-1*P_001220101*R_031[0]+-1*P_101020001*R_110[0]+P_101020101*R_111[0]+P_101120001*R_120[0]+-1*P_101120101*R_121[0]+-1*P_101220001*R_130[0]+P_101220101*R_131[0]; double PR_000021001010=P_000021001*R_010[0]+-1*P_000021101*R_011[0]+-1*P_000121001*R_020[0]+P_000121101*R_021[0]+P_000221001*R_030[0]+-1*P_000221101*R_031[0]+-1*P_000321001*R_040[0]+P_000321101*R_041[0]; double PR_000020002010=P_000020002*R_010[0]+-1*P_000020102*R_011[0]+P_000020202*R_012[0]+-1*P_000120002*R_020[0]+P_000120102*R_021[0]+-1*P_000120202*R_022[0]+P_000220002*R_030[0]+-1*P_000220102*R_031[0]+P_000220202*R_032[0]; double PR_012000010010=P_012000010*R_010[0]+-1*P_012000110*R_011[0]+-1*P_112000010*R_110[0]+P_112000110*R_111[0]+P_212000010*R_210[0]+-1*P_212000110*R_211[0]+-1*P_312000010*R_310[0]+P_312000110*R_311[0]; double PR_011001010010=P_011001010*R_010[0]+-1*P_011001110*R_011[0]+-1*P_011101010*R_020[0]+P_011101110*R_021[0]+-1*P_111001010*R_110[0]+P_111001110*R_111[0]+P_111101010*R_120[0]+-1*P_111101110*R_121[0]+P_211001010*R_210[0]+-1*P_211001110*R_211[0]+-1*P_211101010*R_220[0]+P_211101110*R_221[0]; double PR_010002010010=P_010002010*R_010[0]+-1*P_010002110*R_011[0]+-1*P_010102010*R_020[0]+P_010102110*R_021[0]+P_010202010*R_030[0]+-1*P_010202110*R_031[0]+-1*P_110002010*R_110[0]+P_110002110*R_111[0]+P_110102010*R_120[0]+-1*P_110102110*R_121[0]+-1*P_110202010*R_130[0]+P_110202110*R_131[0]; double PR_011000011010=P_011000011*R_010[0]+-1*P_011000111*R_011[0]+P_011000211*R_012[0]+-1*P_111000011*R_110[0]+P_111000111*R_111[0]+-1*P_111000211*R_112[0]+P_211000011*R_210[0]+-1*P_211000111*R_211[0]+P_211000211*R_212[0]; double PR_010001011010=P_010001011*R_010[0]+-1*P_010001111*R_011[0]+P_010001211*R_012[0]+-1*P_010101011*R_020[0]+P_010101111*R_021[0]+-1*P_010101211*R_022[0]+-1*P_110001011*R_110[0]+P_110001111*R_111[0]+-1*P_110001211*R_112[0]+P_110101011*R_120[0]+-1*P_110101111*R_121[0]+P_110101211*R_122[0]; double PR_010000012010=P_010000012*R_010[0]+-1*P_010000112*R_011[0]+P_010000212*R_012[0]+-1*P_010000312*R_013[0]+-1*P_110000012*R_110[0]+P_110000112*R_111[0]+-1*P_110000212*R_112[0]+P_110000312*R_113[0]; double PR_002010010010=P_002010010*R_010[0]+-1*P_002010110*R_011[0]+-1*P_002110010*R_020[0]+P_002110110*R_021[0]+-1*P_102010010*R_110[0]+P_102010110*R_111[0]+P_102110010*R_120[0]+-1*P_102110110*R_121[0]+P_202010010*R_210[0]+-1*P_202010110*R_211[0]+-1*P_202110010*R_220[0]+P_202110110*R_221[0]; double PR_001011010010=P_001011010*R_010[0]+-1*P_001011110*R_011[0]+-1*P_001111010*R_020[0]+P_001111110*R_021[0]+P_001211010*R_030[0]+-1*P_001211110*R_031[0]+-1*P_101011010*R_110[0]+P_101011110*R_111[0]+P_101111010*R_120[0]+-1*P_101111110*R_121[0]+-1*P_101211010*R_130[0]+P_101211110*R_131[0]; double PR_000012010010=P_000012010*R_010[0]+-1*P_000012110*R_011[0]+-1*P_000112010*R_020[0]+P_000112110*R_021[0]+P_000212010*R_030[0]+-1*P_000212110*R_031[0]+-1*P_000312010*R_040[0]+P_000312110*R_041[0]; double PR_001010011010=P_001010011*R_010[0]+-1*P_001010111*R_011[0]+P_001010211*R_012[0]+-1*P_001110011*R_020[0]+P_001110111*R_021[0]+-1*P_001110211*R_022[0]+-1*P_101010011*R_110[0]+P_101010111*R_111[0]+-1*P_101010211*R_112[0]+P_101110011*R_120[0]+-1*P_101110111*R_121[0]+P_101110211*R_122[0]; double PR_000011011010=P_000011011*R_010[0]+-1*P_000011111*R_011[0]+P_000011211*R_012[0]+-1*P_000111011*R_020[0]+P_000111111*R_021[0]+-1*P_000111211*R_022[0]+P_000211011*R_030[0]+-1*P_000211111*R_031[0]+P_000211211*R_032[0]; double PR_000010012010=P_000010012*R_010[0]+-1*P_000010112*R_011[0]+P_000010212*R_012[0]+-1*P_000010312*R_013[0]+-1*P_000110012*R_020[0]+P_000110112*R_021[0]+-1*P_000110212*R_022[0]+P_000110312*R_023[0]; double PR_002000020010=P_002000020*R_010[0]+-1*P_002000120*R_011[0]+P_002000220*R_012[0]+-1*P_102000020*R_110[0]+P_102000120*R_111[0]+-1*P_102000220*R_112[0]+P_202000020*R_210[0]+-1*P_202000120*R_211[0]+P_202000220*R_212[0]; double PR_001001020010=P_001001020*R_010[0]+-1*P_001001120*R_011[0]+P_001001220*R_012[0]+-1*P_001101020*R_020[0]+P_001101120*R_021[0]+-1*P_001101220*R_022[0]+-1*P_101001020*R_110[0]+P_101001120*R_111[0]+-1*P_101001220*R_112[0]+P_101101020*R_120[0]+-1*P_101101120*R_121[0]+P_101101220*R_122[0]; double PR_000002020010=P_000002020*R_010[0]+-1*P_000002120*R_011[0]+P_000002220*R_012[0]+-1*P_000102020*R_020[0]+P_000102120*R_021[0]+-1*P_000102220*R_022[0]+P_000202020*R_030[0]+-1*P_000202120*R_031[0]+P_000202220*R_032[0]; double PR_001000021010=P_001000021*R_010[0]+-1*P_001000121*R_011[0]+P_001000221*R_012[0]+-1*P_001000321*R_013[0]+-1*P_101000021*R_110[0]+P_101000121*R_111[0]+-1*P_101000221*R_112[0]+P_101000321*R_113[0]; double PR_000001021010=P_000001021*R_010[0]+-1*P_000001121*R_011[0]+P_000001221*R_012[0]+-1*P_000001321*R_013[0]+-1*P_000101021*R_020[0]+P_000101121*R_021[0]+-1*P_000101221*R_022[0]+P_000101321*R_023[0]; double PR_000000022010=P_000000022*R_010[0]+-1*P_000000122*R_011[0]+P_000000222*R_012[0]+-1*P_000000322*R_013[0]+P_000000422*R_014[0]; double PR_022000000100=P_022000000*R_100[0]+-1*P_122000000*R_200[0]+P_222000000*R_300[0]+-1*P_322000000*R_400[0]+P_422000000*R_500[0]; double PR_021001000100=P_021001000*R_100[0]+-1*P_021101000*R_110[0]+-1*P_121001000*R_200[0]+P_121101000*R_210[0]+P_221001000*R_300[0]+-1*P_221101000*R_310[0]+-1*P_321001000*R_400[0]+P_321101000*R_410[0]; double PR_020002000100=P_020002000*R_100[0]+-1*P_020102000*R_110[0]+P_020202000*R_120[0]+-1*P_120002000*R_200[0]+P_120102000*R_210[0]+-1*P_120202000*R_220[0]+P_220002000*R_300[0]+-1*P_220102000*R_310[0]+P_220202000*R_320[0]; double PR_021000001100=P_021000001*R_100[0]+-1*P_021000101*R_101[0]+-1*P_121000001*R_200[0]+P_121000101*R_201[0]+P_221000001*R_300[0]+-1*P_221000101*R_301[0]+-1*P_321000001*R_400[0]+P_321000101*R_401[0]; double PR_020001001100=P_020001001*R_100[0]+-1*P_020001101*R_101[0]+-1*P_020101001*R_110[0]+P_020101101*R_111[0]+-1*P_120001001*R_200[0]+P_120001101*R_201[0]+P_120101001*R_210[0]+-1*P_120101101*R_211[0]+P_220001001*R_300[0]+-1*P_220001101*R_301[0]+-1*P_220101001*R_310[0]+P_220101101*R_311[0]; double PR_020000002100=P_020000002*R_100[0]+-1*P_020000102*R_101[0]+P_020000202*R_102[0]+-1*P_120000002*R_200[0]+P_120000102*R_201[0]+-1*P_120000202*R_202[0]+P_220000002*R_300[0]+-1*P_220000102*R_301[0]+P_220000202*R_302[0]; double PR_012010000100=P_012010000*R_100[0]+-1*P_012110000*R_110[0]+-1*P_112010000*R_200[0]+P_112110000*R_210[0]+P_212010000*R_300[0]+-1*P_212110000*R_310[0]+-1*P_312010000*R_400[0]+P_312110000*R_410[0]; double PR_011011000100=P_011011000*R_100[0]+-1*P_011111000*R_110[0]+P_011211000*R_120[0]+-1*P_111011000*R_200[0]+P_111111000*R_210[0]+-1*P_111211000*R_220[0]+P_211011000*R_300[0]+-1*P_211111000*R_310[0]+P_211211000*R_320[0]; double PR_010012000100=P_010012000*R_100[0]+-1*P_010112000*R_110[0]+P_010212000*R_120[0]+-1*P_010312000*R_130[0]+-1*P_110012000*R_200[0]+P_110112000*R_210[0]+-1*P_110212000*R_220[0]+P_110312000*R_230[0]; double PR_011010001100=P_011010001*R_100[0]+-1*P_011010101*R_101[0]+-1*P_011110001*R_110[0]+P_011110101*R_111[0]+-1*P_111010001*R_200[0]+P_111010101*R_201[0]+P_111110001*R_210[0]+-1*P_111110101*R_211[0]+P_211010001*R_300[0]+-1*P_211010101*R_301[0]+-1*P_211110001*R_310[0]+P_211110101*R_311[0]; double PR_010011001100=P_010011001*R_100[0]+-1*P_010011101*R_101[0]+-1*P_010111001*R_110[0]+P_010111101*R_111[0]+P_010211001*R_120[0]+-1*P_010211101*R_121[0]+-1*P_110011001*R_200[0]+P_110011101*R_201[0]+P_110111001*R_210[0]+-1*P_110111101*R_211[0]+-1*P_110211001*R_220[0]+P_110211101*R_221[0]; double PR_010010002100=P_010010002*R_100[0]+-1*P_010010102*R_101[0]+P_010010202*R_102[0]+-1*P_010110002*R_110[0]+P_010110102*R_111[0]+-1*P_010110202*R_112[0]+-1*P_110010002*R_200[0]+P_110010102*R_201[0]+-1*P_110010202*R_202[0]+P_110110002*R_210[0]+-1*P_110110102*R_211[0]+P_110110202*R_212[0]; double PR_002020000100=P_002020000*R_100[0]+-1*P_002120000*R_110[0]+P_002220000*R_120[0]+-1*P_102020000*R_200[0]+P_102120000*R_210[0]+-1*P_102220000*R_220[0]+P_202020000*R_300[0]+-1*P_202120000*R_310[0]+P_202220000*R_320[0]; double PR_001021000100=P_001021000*R_100[0]+-1*P_001121000*R_110[0]+P_001221000*R_120[0]+-1*P_001321000*R_130[0]+-1*P_101021000*R_200[0]+P_101121000*R_210[0]+-1*P_101221000*R_220[0]+P_101321000*R_230[0]; double PR_000022000100=P_000022000*R_100[0]+-1*P_000122000*R_110[0]+P_000222000*R_120[0]+-1*P_000322000*R_130[0]+P_000422000*R_140[0]; double PR_001020001100=P_001020001*R_100[0]+-1*P_001020101*R_101[0]+-1*P_001120001*R_110[0]+P_001120101*R_111[0]+P_001220001*R_120[0]+-1*P_001220101*R_121[0]+-1*P_101020001*R_200[0]+P_101020101*R_201[0]+P_101120001*R_210[0]+-1*P_101120101*R_211[0]+-1*P_101220001*R_220[0]+P_101220101*R_221[0]; double PR_000021001100=P_000021001*R_100[0]+-1*P_000021101*R_101[0]+-1*P_000121001*R_110[0]+P_000121101*R_111[0]+P_000221001*R_120[0]+-1*P_000221101*R_121[0]+-1*P_000321001*R_130[0]+P_000321101*R_131[0]; double PR_000020002100=P_000020002*R_100[0]+-1*P_000020102*R_101[0]+P_000020202*R_102[0]+-1*P_000120002*R_110[0]+P_000120102*R_111[0]+-1*P_000120202*R_112[0]+P_000220002*R_120[0]+-1*P_000220102*R_121[0]+P_000220202*R_122[0]; double PR_012000010100=P_012000010*R_100[0]+-1*P_012000110*R_101[0]+-1*P_112000010*R_200[0]+P_112000110*R_201[0]+P_212000010*R_300[0]+-1*P_212000110*R_301[0]+-1*P_312000010*R_400[0]+P_312000110*R_401[0]; double PR_011001010100=P_011001010*R_100[0]+-1*P_011001110*R_101[0]+-1*P_011101010*R_110[0]+P_011101110*R_111[0]+-1*P_111001010*R_200[0]+P_111001110*R_201[0]+P_111101010*R_210[0]+-1*P_111101110*R_211[0]+P_211001010*R_300[0]+-1*P_211001110*R_301[0]+-1*P_211101010*R_310[0]+P_211101110*R_311[0]; double PR_010002010100=P_010002010*R_100[0]+-1*P_010002110*R_101[0]+-1*P_010102010*R_110[0]+P_010102110*R_111[0]+P_010202010*R_120[0]+-1*P_010202110*R_121[0]+-1*P_110002010*R_200[0]+P_110002110*R_201[0]+P_110102010*R_210[0]+-1*P_110102110*R_211[0]+-1*P_110202010*R_220[0]+P_110202110*R_221[0]; double PR_011000011100=P_011000011*R_100[0]+-1*P_011000111*R_101[0]+P_011000211*R_102[0]+-1*P_111000011*R_200[0]+P_111000111*R_201[0]+-1*P_111000211*R_202[0]+P_211000011*R_300[0]+-1*P_211000111*R_301[0]+P_211000211*R_302[0]; double PR_010001011100=P_010001011*R_100[0]+-1*P_010001111*R_101[0]+P_010001211*R_102[0]+-1*P_010101011*R_110[0]+P_010101111*R_111[0]+-1*P_010101211*R_112[0]+-1*P_110001011*R_200[0]+P_110001111*R_201[0]+-1*P_110001211*R_202[0]+P_110101011*R_210[0]+-1*P_110101111*R_211[0]+P_110101211*R_212[0]; double PR_010000012100=P_010000012*R_100[0]+-1*P_010000112*R_101[0]+P_010000212*R_102[0]+-1*P_010000312*R_103[0]+-1*P_110000012*R_200[0]+P_110000112*R_201[0]+-1*P_110000212*R_202[0]+P_110000312*R_203[0]; double PR_002010010100=P_002010010*R_100[0]+-1*P_002010110*R_101[0]+-1*P_002110010*R_110[0]+P_002110110*R_111[0]+-1*P_102010010*R_200[0]+P_102010110*R_201[0]+P_102110010*R_210[0]+-1*P_102110110*R_211[0]+P_202010010*R_300[0]+-1*P_202010110*R_301[0]+-1*P_202110010*R_310[0]+P_202110110*R_311[0]; double PR_001011010100=P_001011010*R_100[0]+-1*P_001011110*R_101[0]+-1*P_001111010*R_110[0]+P_001111110*R_111[0]+P_001211010*R_120[0]+-1*P_001211110*R_121[0]+-1*P_101011010*R_200[0]+P_101011110*R_201[0]+P_101111010*R_210[0]+-1*P_101111110*R_211[0]+-1*P_101211010*R_220[0]+P_101211110*R_221[0]; double PR_000012010100=P_000012010*R_100[0]+-1*P_000012110*R_101[0]+-1*P_000112010*R_110[0]+P_000112110*R_111[0]+P_000212010*R_120[0]+-1*P_000212110*R_121[0]+-1*P_000312010*R_130[0]+P_000312110*R_131[0]; double PR_001010011100=P_001010011*R_100[0]+-1*P_001010111*R_101[0]+P_001010211*R_102[0]+-1*P_001110011*R_110[0]+P_001110111*R_111[0]+-1*P_001110211*R_112[0]+-1*P_101010011*R_200[0]+P_101010111*R_201[0]+-1*P_101010211*R_202[0]+P_101110011*R_210[0]+-1*P_101110111*R_211[0]+P_101110211*R_212[0]; double PR_000011011100=P_000011011*R_100[0]+-1*P_000011111*R_101[0]+P_000011211*R_102[0]+-1*P_000111011*R_110[0]+P_000111111*R_111[0]+-1*P_000111211*R_112[0]+P_000211011*R_120[0]+-1*P_000211111*R_121[0]+P_000211211*R_122[0]; double PR_000010012100=P_000010012*R_100[0]+-1*P_000010112*R_101[0]+P_000010212*R_102[0]+-1*P_000010312*R_103[0]+-1*P_000110012*R_110[0]+P_000110112*R_111[0]+-1*P_000110212*R_112[0]+P_000110312*R_113[0]; double PR_002000020100=P_002000020*R_100[0]+-1*P_002000120*R_101[0]+P_002000220*R_102[0]+-1*P_102000020*R_200[0]+P_102000120*R_201[0]+-1*P_102000220*R_202[0]+P_202000020*R_300[0]+-1*P_202000120*R_301[0]+P_202000220*R_302[0]; double PR_001001020100=P_001001020*R_100[0]+-1*P_001001120*R_101[0]+P_001001220*R_102[0]+-1*P_001101020*R_110[0]+P_001101120*R_111[0]+-1*P_001101220*R_112[0]+-1*P_101001020*R_200[0]+P_101001120*R_201[0]+-1*P_101001220*R_202[0]+P_101101020*R_210[0]+-1*P_101101120*R_211[0]+P_101101220*R_212[0]; double PR_000002020100=P_000002020*R_100[0]+-1*P_000002120*R_101[0]+P_000002220*R_102[0]+-1*P_000102020*R_110[0]+P_000102120*R_111[0]+-1*P_000102220*R_112[0]+P_000202020*R_120[0]+-1*P_000202120*R_121[0]+P_000202220*R_122[0]; double PR_001000021100=P_001000021*R_100[0]+-1*P_001000121*R_101[0]+P_001000221*R_102[0]+-1*P_001000321*R_103[0]+-1*P_101000021*R_200[0]+P_101000121*R_201[0]+-1*P_101000221*R_202[0]+P_101000321*R_203[0]; double PR_000001021100=P_000001021*R_100[0]+-1*P_000001121*R_101[0]+P_000001221*R_102[0]+-1*P_000001321*R_103[0]+-1*P_000101021*R_110[0]+P_000101121*R_111[0]+-1*P_000101221*R_112[0]+P_000101321*R_113[0]; double PR_000000022100=P_000000022*R_100[0]+-1*P_000000122*R_101[0]+P_000000222*R_102[0]+-1*P_000000322*R_103[0]+P_000000422*R_104[0]; double PR_022000000002=P_022000000*R_002[0]+-1*P_122000000*R_102[0]+P_222000000*R_202[0]+-1*P_322000000*R_302[0]+P_422000000*R_402[0]; double PR_021001000002=P_021001000*R_002[0]+-1*P_021101000*R_012[0]+-1*P_121001000*R_102[0]+P_121101000*R_112[0]+P_221001000*R_202[0]+-1*P_221101000*R_212[0]+-1*P_321001000*R_302[0]+P_321101000*R_312[0]; double PR_020002000002=P_020002000*R_002[0]+-1*P_020102000*R_012[0]+P_020202000*R_022[0]+-1*P_120002000*R_102[0]+P_120102000*R_112[0]+-1*P_120202000*R_122[0]+P_220002000*R_202[0]+-1*P_220102000*R_212[0]+P_220202000*R_222[0]; double PR_021000001002=P_021000001*R_002[0]+-1*P_021000101*R_003[0]+-1*P_121000001*R_102[0]+P_121000101*R_103[0]+P_221000001*R_202[0]+-1*P_221000101*R_203[0]+-1*P_321000001*R_302[0]+P_321000101*R_303[0]; double PR_020001001002=P_020001001*R_002[0]+-1*P_020001101*R_003[0]+-1*P_020101001*R_012[0]+P_020101101*R_013[0]+-1*P_120001001*R_102[0]+P_120001101*R_103[0]+P_120101001*R_112[0]+-1*P_120101101*R_113[0]+P_220001001*R_202[0]+-1*P_220001101*R_203[0]+-1*P_220101001*R_212[0]+P_220101101*R_213[0]; double PR_020000002002=P_020000002*R_002[0]+-1*P_020000102*R_003[0]+P_020000202*R_004[0]+-1*P_120000002*R_102[0]+P_120000102*R_103[0]+-1*P_120000202*R_104[0]+P_220000002*R_202[0]+-1*P_220000102*R_203[0]+P_220000202*R_204[0]; double PR_012010000002=P_012010000*R_002[0]+-1*P_012110000*R_012[0]+-1*P_112010000*R_102[0]+P_112110000*R_112[0]+P_212010000*R_202[0]+-1*P_212110000*R_212[0]+-1*P_312010000*R_302[0]+P_312110000*R_312[0]; double PR_011011000002=P_011011000*R_002[0]+-1*P_011111000*R_012[0]+P_011211000*R_022[0]+-1*P_111011000*R_102[0]+P_111111000*R_112[0]+-1*P_111211000*R_122[0]+P_211011000*R_202[0]+-1*P_211111000*R_212[0]+P_211211000*R_222[0]; double PR_010012000002=P_010012000*R_002[0]+-1*P_010112000*R_012[0]+P_010212000*R_022[0]+-1*P_010312000*R_032[0]+-1*P_110012000*R_102[0]+P_110112000*R_112[0]+-1*P_110212000*R_122[0]+P_110312000*R_132[0]; double PR_011010001002=P_011010001*R_002[0]+-1*P_011010101*R_003[0]+-1*P_011110001*R_012[0]+P_011110101*R_013[0]+-1*P_111010001*R_102[0]+P_111010101*R_103[0]+P_111110001*R_112[0]+-1*P_111110101*R_113[0]+P_211010001*R_202[0]+-1*P_211010101*R_203[0]+-1*P_211110001*R_212[0]+P_211110101*R_213[0]; double PR_010011001002=P_010011001*R_002[0]+-1*P_010011101*R_003[0]+-1*P_010111001*R_012[0]+P_010111101*R_013[0]+P_010211001*R_022[0]+-1*P_010211101*R_023[0]+-1*P_110011001*R_102[0]+P_110011101*R_103[0]+P_110111001*R_112[0]+-1*P_110111101*R_113[0]+-1*P_110211001*R_122[0]+P_110211101*R_123[0]; double PR_010010002002=P_010010002*R_002[0]+-1*P_010010102*R_003[0]+P_010010202*R_004[0]+-1*P_010110002*R_012[0]+P_010110102*R_013[0]+-1*P_010110202*R_014[0]+-1*P_110010002*R_102[0]+P_110010102*R_103[0]+-1*P_110010202*R_104[0]+P_110110002*R_112[0]+-1*P_110110102*R_113[0]+P_110110202*R_114[0]; double PR_002020000002=P_002020000*R_002[0]+-1*P_002120000*R_012[0]+P_002220000*R_022[0]+-1*P_102020000*R_102[0]+P_102120000*R_112[0]+-1*P_102220000*R_122[0]+P_202020000*R_202[0]+-1*P_202120000*R_212[0]+P_202220000*R_222[0]; double PR_001021000002=P_001021000*R_002[0]+-1*P_001121000*R_012[0]+P_001221000*R_022[0]+-1*P_001321000*R_032[0]+-1*P_101021000*R_102[0]+P_101121000*R_112[0]+-1*P_101221000*R_122[0]+P_101321000*R_132[0]; double PR_000022000002=P_000022000*R_002[0]+-1*P_000122000*R_012[0]+P_000222000*R_022[0]+-1*P_000322000*R_032[0]+P_000422000*R_042[0]; double PR_001020001002=P_001020001*R_002[0]+-1*P_001020101*R_003[0]+-1*P_001120001*R_012[0]+P_001120101*R_013[0]+P_001220001*R_022[0]+-1*P_001220101*R_023[0]+-1*P_101020001*R_102[0]+P_101020101*R_103[0]+P_101120001*R_112[0]+-1*P_101120101*R_113[0]+-1*P_101220001*R_122[0]+P_101220101*R_123[0]; double PR_000021001002=P_000021001*R_002[0]+-1*P_000021101*R_003[0]+-1*P_000121001*R_012[0]+P_000121101*R_013[0]+P_000221001*R_022[0]+-1*P_000221101*R_023[0]+-1*P_000321001*R_032[0]+P_000321101*R_033[0]; double PR_000020002002=P_000020002*R_002[0]+-1*P_000020102*R_003[0]+P_000020202*R_004[0]+-1*P_000120002*R_012[0]+P_000120102*R_013[0]+-1*P_000120202*R_014[0]+P_000220002*R_022[0]+-1*P_000220102*R_023[0]+P_000220202*R_024[0]; double PR_012000010002=P_012000010*R_002[0]+-1*P_012000110*R_003[0]+-1*P_112000010*R_102[0]+P_112000110*R_103[0]+P_212000010*R_202[0]+-1*P_212000110*R_203[0]+-1*P_312000010*R_302[0]+P_312000110*R_303[0]; double PR_011001010002=P_011001010*R_002[0]+-1*P_011001110*R_003[0]+-1*P_011101010*R_012[0]+P_011101110*R_013[0]+-1*P_111001010*R_102[0]+P_111001110*R_103[0]+P_111101010*R_112[0]+-1*P_111101110*R_113[0]+P_211001010*R_202[0]+-1*P_211001110*R_203[0]+-1*P_211101010*R_212[0]+P_211101110*R_213[0]; double PR_010002010002=P_010002010*R_002[0]+-1*P_010002110*R_003[0]+-1*P_010102010*R_012[0]+P_010102110*R_013[0]+P_010202010*R_022[0]+-1*P_010202110*R_023[0]+-1*P_110002010*R_102[0]+P_110002110*R_103[0]+P_110102010*R_112[0]+-1*P_110102110*R_113[0]+-1*P_110202010*R_122[0]+P_110202110*R_123[0]; double PR_011000011002=P_011000011*R_002[0]+-1*P_011000111*R_003[0]+P_011000211*R_004[0]+-1*P_111000011*R_102[0]+P_111000111*R_103[0]+-1*P_111000211*R_104[0]+P_211000011*R_202[0]+-1*P_211000111*R_203[0]+P_211000211*R_204[0]; double PR_010001011002=P_010001011*R_002[0]+-1*P_010001111*R_003[0]+P_010001211*R_004[0]+-1*P_010101011*R_012[0]+P_010101111*R_013[0]+-1*P_010101211*R_014[0]+-1*P_110001011*R_102[0]+P_110001111*R_103[0]+-1*P_110001211*R_104[0]+P_110101011*R_112[0]+-1*P_110101111*R_113[0]+P_110101211*R_114[0]; double PR_010000012002=P_010000012*R_002[0]+-1*P_010000112*R_003[0]+P_010000212*R_004[0]+-1*P_010000312*R_005[0]+-1*P_110000012*R_102[0]+P_110000112*R_103[0]+-1*P_110000212*R_104[0]+P_110000312*R_105[0]; double PR_002010010002=P_002010010*R_002[0]+-1*P_002010110*R_003[0]+-1*P_002110010*R_012[0]+P_002110110*R_013[0]+-1*P_102010010*R_102[0]+P_102010110*R_103[0]+P_102110010*R_112[0]+-1*P_102110110*R_113[0]+P_202010010*R_202[0]+-1*P_202010110*R_203[0]+-1*P_202110010*R_212[0]+P_202110110*R_213[0]; double PR_001011010002=P_001011010*R_002[0]+-1*P_001011110*R_003[0]+-1*P_001111010*R_012[0]+P_001111110*R_013[0]+P_001211010*R_022[0]+-1*P_001211110*R_023[0]+-1*P_101011010*R_102[0]+P_101011110*R_103[0]+P_101111010*R_112[0]+-1*P_101111110*R_113[0]+-1*P_101211010*R_122[0]+P_101211110*R_123[0]; double PR_000012010002=P_000012010*R_002[0]+-1*P_000012110*R_003[0]+-1*P_000112010*R_012[0]+P_000112110*R_013[0]+P_000212010*R_022[0]+-1*P_000212110*R_023[0]+-1*P_000312010*R_032[0]+P_000312110*R_033[0]; double PR_001010011002=P_001010011*R_002[0]+-1*P_001010111*R_003[0]+P_001010211*R_004[0]+-1*P_001110011*R_012[0]+P_001110111*R_013[0]+-1*P_001110211*R_014[0]+-1*P_101010011*R_102[0]+P_101010111*R_103[0]+-1*P_101010211*R_104[0]+P_101110011*R_112[0]+-1*P_101110111*R_113[0]+P_101110211*R_114[0]; double PR_000011011002=P_000011011*R_002[0]+-1*P_000011111*R_003[0]+P_000011211*R_004[0]+-1*P_000111011*R_012[0]+P_000111111*R_013[0]+-1*P_000111211*R_014[0]+P_000211011*R_022[0]+-1*P_000211111*R_023[0]+P_000211211*R_024[0]; double PR_000010012002=P_000010012*R_002[0]+-1*P_000010112*R_003[0]+P_000010212*R_004[0]+-1*P_000010312*R_005[0]+-1*P_000110012*R_012[0]+P_000110112*R_013[0]+-1*P_000110212*R_014[0]+P_000110312*R_015[0]; double PR_002000020002=P_002000020*R_002[0]+-1*P_002000120*R_003[0]+P_002000220*R_004[0]+-1*P_102000020*R_102[0]+P_102000120*R_103[0]+-1*P_102000220*R_104[0]+P_202000020*R_202[0]+-1*P_202000120*R_203[0]+P_202000220*R_204[0]; double PR_001001020002=P_001001020*R_002[0]+-1*P_001001120*R_003[0]+P_001001220*R_004[0]+-1*P_001101020*R_012[0]+P_001101120*R_013[0]+-1*P_001101220*R_014[0]+-1*P_101001020*R_102[0]+P_101001120*R_103[0]+-1*P_101001220*R_104[0]+P_101101020*R_112[0]+-1*P_101101120*R_113[0]+P_101101220*R_114[0]; double PR_000002020002=P_000002020*R_002[0]+-1*P_000002120*R_003[0]+P_000002220*R_004[0]+-1*P_000102020*R_012[0]+P_000102120*R_013[0]+-1*P_000102220*R_014[0]+P_000202020*R_022[0]+-1*P_000202120*R_023[0]+P_000202220*R_024[0]; double PR_001000021002=P_001000021*R_002[0]+-1*P_001000121*R_003[0]+P_001000221*R_004[0]+-1*P_001000321*R_005[0]+-1*P_101000021*R_102[0]+P_101000121*R_103[0]+-1*P_101000221*R_104[0]+P_101000321*R_105[0]; double PR_000001021002=P_000001021*R_002[0]+-1*P_000001121*R_003[0]+P_000001221*R_004[0]+-1*P_000001321*R_005[0]+-1*P_000101021*R_012[0]+P_000101121*R_013[0]+-1*P_000101221*R_014[0]+P_000101321*R_015[0]; double PR_000000022002=P_000000022*R_002[0]+-1*P_000000122*R_003[0]+P_000000222*R_004[0]+-1*P_000000322*R_005[0]+P_000000422*R_006[0]; double PR_022000000011=P_022000000*R_011[0]+-1*P_122000000*R_111[0]+P_222000000*R_211[0]+-1*P_322000000*R_311[0]+P_422000000*R_411[0]; double PR_021001000011=P_021001000*R_011[0]+-1*P_021101000*R_021[0]+-1*P_121001000*R_111[0]+P_121101000*R_121[0]+P_221001000*R_211[0]+-1*P_221101000*R_221[0]+-1*P_321001000*R_311[0]+P_321101000*R_321[0]; double PR_020002000011=P_020002000*R_011[0]+-1*P_020102000*R_021[0]+P_020202000*R_031[0]+-1*P_120002000*R_111[0]+P_120102000*R_121[0]+-1*P_120202000*R_131[0]+P_220002000*R_211[0]+-1*P_220102000*R_221[0]+P_220202000*R_231[0]; double PR_021000001011=P_021000001*R_011[0]+-1*P_021000101*R_012[0]+-1*P_121000001*R_111[0]+P_121000101*R_112[0]+P_221000001*R_211[0]+-1*P_221000101*R_212[0]+-1*P_321000001*R_311[0]+P_321000101*R_312[0]; double PR_020001001011=P_020001001*R_011[0]+-1*P_020001101*R_012[0]+-1*P_020101001*R_021[0]+P_020101101*R_022[0]+-1*P_120001001*R_111[0]+P_120001101*R_112[0]+P_120101001*R_121[0]+-1*P_120101101*R_122[0]+P_220001001*R_211[0]+-1*P_220001101*R_212[0]+-1*P_220101001*R_221[0]+P_220101101*R_222[0]; double PR_020000002011=P_020000002*R_011[0]+-1*P_020000102*R_012[0]+P_020000202*R_013[0]+-1*P_120000002*R_111[0]+P_120000102*R_112[0]+-1*P_120000202*R_113[0]+P_220000002*R_211[0]+-1*P_220000102*R_212[0]+P_220000202*R_213[0]; double PR_012010000011=P_012010000*R_011[0]+-1*P_012110000*R_021[0]+-1*P_112010000*R_111[0]+P_112110000*R_121[0]+P_212010000*R_211[0]+-1*P_212110000*R_221[0]+-1*P_312010000*R_311[0]+P_312110000*R_321[0]; double PR_011011000011=P_011011000*R_011[0]+-1*P_011111000*R_021[0]+P_011211000*R_031[0]+-1*P_111011000*R_111[0]+P_111111000*R_121[0]+-1*P_111211000*R_131[0]+P_211011000*R_211[0]+-1*P_211111000*R_221[0]+P_211211000*R_231[0]; double PR_010012000011=P_010012000*R_011[0]+-1*P_010112000*R_021[0]+P_010212000*R_031[0]+-1*P_010312000*R_041[0]+-1*P_110012000*R_111[0]+P_110112000*R_121[0]+-1*P_110212000*R_131[0]+P_110312000*R_141[0]; double PR_011010001011=P_011010001*R_011[0]+-1*P_011010101*R_012[0]+-1*P_011110001*R_021[0]+P_011110101*R_022[0]+-1*P_111010001*R_111[0]+P_111010101*R_112[0]+P_111110001*R_121[0]+-1*P_111110101*R_122[0]+P_211010001*R_211[0]+-1*P_211010101*R_212[0]+-1*P_211110001*R_221[0]+P_211110101*R_222[0]; double PR_010011001011=P_010011001*R_011[0]+-1*P_010011101*R_012[0]+-1*P_010111001*R_021[0]+P_010111101*R_022[0]+P_010211001*R_031[0]+-1*P_010211101*R_032[0]+-1*P_110011001*R_111[0]+P_110011101*R_112[0]+P_110111001*R_121[0]+-1*P_110111101*R_122[0]+-1*P_110211001*R_131[0]+P_110211101*R_132[0]; double PR_010010002011=P_010010002*R_011[0]+-1*P_010010102*R_012[0]+P_010010202*R_013[0]+-1*P_010110002*R_021[0]+P_010110102*R_022[0]+-1*P_010110202*R_023[0]+-1*P_110010002*R_111[0]+P_110010102*R_112[0]+-1*P_110010202*R_113[0]+P_110110002*R_121[0]+-1*P_110110102*R_122[0]+P_110110202*R_123[0]; double PR_002020000011=P_002020000*R_011[0]+-1*P_002120000*R_021[0]+P_002220000*R_031[0]+-1*P_102020000*R_111[0]+P_102120000*R_121[0]+-1*P_102220000*R_131[0]+P_202020000*R_211[0]+-1*P_202120000*R_221[0]+P_202220000*R_231[0]; double PR_001021000011=P_001021000*R_011[0]+-1*P_001121000*R_021[0]+P_001221000*R_031[0]+-1*P_001321000*R_041[0]+-1*P_101021000*R_111[0]+P_101121000*R_121[0]+-1*P_101221000*R_131[0]+P_101321000*R_141[0]; double PR_000022000011=P_000022000*R_011[0]+-1*P_000122000*R_021[0]+P_000222000*R_031[0]+-1*P_000322000*R_041[0]+P_000422000*R_051[0]; double PR_001020001011=P_001020001*R_011[0]+-1*P_001020101*R_012[0]+-1*P_001120001*R_021[0]+P_001120101*R_022[0]+P_001220001*R_031[0]+-1*P_001220101*R_032[0]+-1*P_101020001*R_111[0]+P_101020101*R_112[0]+P_101120001*R_121[0]+-1*P_101120101*R_122[0]+-1*P_101220001*R_131[0]+P_101220101*R_132[0]; double PR_000021001011=P_000021001*R_011[0]+-1*P_000021101*R_012[0]+-1*P_000121001*R_021[0]+P_000121101*R_022[0]+P_000221001*R_031[0]+-1*P_000221101*R_032[0]+-1*P_000321001*R_041[0]+P_000321101*R_042[0]; double PR_000020002011=P_000020002*R_011[0]+-1*P_000020102*R_012[0]+P_000020202*R_013[0]+-1*P_000120002*R_021[0]+P_000120102*R_022[0]+-1*P_000120202*R_023[0]+P_000220002*R_031[0]+-1*P_000220102*R_032[0]+P_000220202*R_033[0]; double PR_012000010011=P_012000010*R_011[0]+-1*P_012000110*R_012[0]+-1*P_112000010*R_111[0]+P_112000110*R_112[0]+P_212000010*R_211[0]+-1*P_212000110*R_212[0]+-1*P_312000010*R_311[0]+P_312000110*R_312[0]; double PR_011001010011=P_011001010*R_011[0]+-1*P_011001110*R_012[0]+-1*P_011101010*R_021[0]+P_011101110*R_022[0]+-1*P_111001010*R_111[0]+P_111001110*R_112[0]+P_111101010*R_121[0]+-1*P_111101110*R_122[0]+P_211001010*R_211[0]+-1*P_211001110*R_212[0]+-1*P_211101010*R_221[0]+P_211101110*R_222[0]; double PR_010002010011=P_010002010*R_011[0]+-1*P_010002110*R_012[0]+-1*P_010102010*R_021[0]+P_010102110*R_022[0]+P_010202010*R_031[0]+-1*P_010202110*R_032[0]+-1*P_110002010*R_111[0]+P_110002110*R_112[0]+P_110102010*R_121[0]+-1*P_110102110*R_122[0]+-1*P_110202010*R_131[0]+P_110202110*R_132[0]; double PR_011000011011=P_011000011*R_011[0]+-1*P_011000111*R_012[0]+P_011000211*R_013[0]+-1*P_111000011*R_111[0]+P_111000111*R_112[0]+-1*P_111000211*R_113[0]+P_211000011*R_211[0]+-1*P_211000111*R_212[0]+P_211000211*R_213[0]; double PR_010001011011=P_010001011*R_011[0]+-1*P_010001111*R_012[0]+P_010001211*R_013[0]+-1*P_010101011*R_021[0]+P_010101111*R_022[0]+-1*P_010101211*R_023[0]+-1*P_110001011*R_111[0]+P_110001111*R_112[0]+-1*P_110001211*R_113[0]+P_110101011*R_121[0]+-1*P_110101111*R_122[0]+P_110101211*R_123[0]; double PR_010000012011=P_010000012*R_011[0]+-1*P_010000112*R_012[0]+P_010000212*R_013[0]+-1*P_010000312*R_014[0]+-1*P_110000012*R_111[0]+P_110000112*R_112[0]+-1*P_110000212*R_113[0]+P_110000312*R_114[0]; double PR_002010010011=P_002010010*R_011[0]+-1*P_002010110*R_012[0]+-1*P_002110010*R_021[0]+P_002110110*R_022[0]+-1*P_102010010*R_111[0]+P_102010110*R_112[0]+P_102110010*R_121[0]+-1*P_102110110*R_122[0]+P_202010010*R_211[0]+-1*P_202010110*R_212[0]+-1*P_202110010*R_221[0]+P_202110110*R_222[0]; double PR_001011010011=P_001011010*R_011[0]+-1*P_001011110*R_012[0]+-1*P_001111010*R_021[0]+P_001111110*R_022[0]+P_001211010*R_031[0]+-1*P_001211110*R_032[0]+-1*P_101011010*R_111[0]+P_101011110*R_112[0]+P_101111010*R_121[0]+-1*P_101111110*R_122[0]+-1*P_101211010*R_131[0]+P_101211110*R_132[0]; double PR_000012010011=P_000012010*R_011[0]+-1*P_000012110*R_012[0]+-1*P_000112010*R_021[0]+P_000112110*R_022[0]+P_000212010*R_031[0]+-1*P_000212110*R_032[0]+-1*P_000312010*R_041[0]+P_000312110*R_042[0]; double PR_001010011011=P_001010011*R_011[0]+-1*P_001010111*R_012[0]+P_001010211*R_013[0]+-1*P_001110011*R_021[0]+P_001110111*R_022[0]+-1*P_001110211*R_023[0]+-1*P_101010011*R_111[0]+P_101010111*R_112[0]+-1*P_101010211*R_113[0]+P_101110011*R_121[0]+-1*P_101110111*R_122[0]+P_101110211*R_123[0]; double PR_000011011011=P_000011011*R_011[0]+-1*P_000011111*R_012[0]+P_000011211*R_013[0]+-1*P_000111011*R_021[0]+P_000111111*R_022[0]+-1*P_000111211*R_023[0]+P_000211011*R_031[0]+-1*P_000211111*R_032[0]+P_000211211*R_033[0]; double PR_000010012011=P_000010012*R_011[0]+-1*P_000010112*R_012[0]+P_000010212*R_013[0]+-1*P_000010312*R_014[0]+-1*P_000110012*R_021[0]+P_000110112*R_022[0]+-1*P_000110212*R_023[0]+P_000110312*R_024[0]; double PR_002000020011=P_002000020*R_011[0]+-1*P_002000120*R_012[0]+P_002000220*R_013[0]+-1*P_102000020*R_111[0]+P_102000120*R_112[0]+-1*P_102000220*R_113[0]+P_202000020*R_211[0]+-1*P_202000120*R_212[0]+P_202000220*R_213[0]; double PR_001001020011=P_001001020*R_011[0]+-1*P_001001120*R_012[0]+P_001001220*R_013[0]+-1*P_001101020*R_021[0]+P_001101120*R_022[0]+-1*P_001101220*R_023[0]+-1*P_101001020*R_111[0]+P_101001120*R_112[0]+-1*P_101001220*R_113[0]+P_101101020*R_121[0]+-1*P_101101120*R_122[0]+P_101101220*R_123[0]; double PR_000002020011=P_000002020*R_011[0]+-1*P_000002120*R_012[0]+P_000002220*R_013[0]+-1*P_000102020*R_021[0]+P_000102120*R_022[0]+-1*P_000102220*R_023[0]+P_000202020*R_031[0]+-1*P_000202120*R_032[0]+P_000202220*R_033[0]; double PR_001000021011=P_001000021*R_011[0]+-1*P_001000121*R_012[0]+P_001000221*R_013[0]+-1*P_001000321*R_014[0]+-1*P_101000021*R_111[0]+P_101000121*R_112[0]+-1*P_101000221*R_113[0]+P_101000321*R_114[0]; double PR_000001021011=P_000001021*R_011[0]+-1*P_000001121*R_012[0]+P_000001221*R_013[0]+-1*P_000001321*R_014[0]+-1*P_000101021*R_021[0]+P_000101121*R_022[0]+-1*P_000101221*R_023[0]+P_000101321*R_024[0]; double PR_000000022011=P_000000022*R_011[0]+-1*P_000000122*R_012[0]+P_000000222*R_013[0]+-1*P_000000322*R_014[0]+P_000000422*R_015[0]; double PR_022000000020=P_022000000*R_020[0]+-1*P_122000000*R_120[0]+P_222000000*R_220[0]+-1*P_322000000*R_320[0]+P_422000000*R_420[0]; double PR_021001000020=P_021001000*R_020[0]+-1*P_021101000*R_030[0]+-1*P_121001000*R_120[0]+P_121101000*R_130[0]+P_221001000*R_220[0]+-1*P_221101000*R_230[0]+-1*P_321001000*R_320[0]+P_321101000*R_330[0]; double PR_020002000020=P_020002000*R_020[0]+-1*P_020102000*R_030[0]+P_020202000*R_040[0]+-1*P_120002000*R_120[0]+P_120102000*R_130[0]+-1*P_120202000*R_140[0]+P_220002000*R_220[0]+-1*P_220102000*R_230[0]+P_220202000*R_240[0]; double PR_021000001020=P_021000001*R_020[0]+-1*P_021000101*R_021[0]+-1*P_121000001*R_120[0]+P_121000101*R_121[0]+P_221000001*R_220[0]+-1*P_221000101*R_221[0]+-1*P_321000001*R_320[0]+P_321000101*R_321[0]; double PR_020001001020=P_020001001*R_020[0]+-1*P_020001101*R_021[0]+-1*P_020101001*R_030[0]+P_020101101*R_031[0]+-1*P_120001001*R_120[0]+P_120001101*R_121[0]+P_120101001*R_130[0]+-1*P_120101101*R_131[0]+P_220001001*R_220[0]+-1*P_220001101*R_221[0]+-1*P_220101001*R_230[0]+P_220101101*R_231[0]; double PR_020000002020=P_020000002*R_020[0]+-1*P_020000102*R_021[0]+P_020000202*R_022[0]+-1*P_120000002*R_120[0]+P_120000102*R_121[0]+-1*P_120000202*R_122[0]+P_220000002*R_220[0]+-1*P_220000102*R_221[0]+P_220000202*R_222[0]; double PR_012010000020=P_012010000*R_020[0]+-1*P_012110000*R_030[0]+-1*P_112010000*R_120[0]+P_112110000*R_130[0]+P_212010000*R_220[0]+-1*P_212110000*R_230[0]+-1*P_312010000*R_320[0]+P_312110000*R_330[0]; double PR_011011000020=P_011011000*R_020[0]+-1*P_011111000*R_030[0]+P_011211000*R_040[0]+-1*P_111011000*R_120[0]+P_111111000*R_130[0]+-1*P_111211000*R_140[0]+P_211011000*R_220[0]+-1*P_211111000*R_230[0]+P_211211000*R_240[0]; double PR_010012000020=P_010012000*R_020[0]+-1*P_010112000*R_030[0]+P_010212000*R_040[0]+-1*P_010312000*R_050[0]+-1*P_110012000*R_120[0]+P_110112000*R_130[0]+-1*P_110212000*R_140[0]+P_110312000*R_150[0]; double PR_011010001020=P_011010001*R_020[0]+-1*P_011010101*R_021[0]+-1*P_011110001*R_030[0]+P_011110101*R_031[0]+-1*P_111010001*R_120[0]+P_111010101*R_121[0]+P_111110001*R_130[0]+-1*P_111110101*R_131[0]+P_211010001*R_220[0]+-1*P_211010101*R_221[0]+-1*P_211110001*R_230[0]+P_211110101*R_231[0]; double PR_010011001020=P_010011001*R_020[0]+-1*P_010011101*R_021[0]+-1*P_010111001*R_030[0]+P_010111101*R_031[0]+P_010211001*R_040[0]+-1*P_010211101*R_041[0]+-1*P_110011001*R_120[0]+P_110011101*R_121[0]+P_110111001*R_130[0]+-1*P_110111101*R_131[0]+-1*P_110211001*R_140[0]+P_110211101*R_141[0]; double PR_010010002020=P_010010002*R_020[0]+-1*P_010010102*R_021[0]+P_010010202*R_022[0]+-1*P_010110002*R_030[0]+P_010110102*R_031[0]+-1*P_010110202*R_032[0]+-1*P_110010002*R_120[0]+P_110010102*R_121[0]+-1*P_110010202*R_122[0]+P_110110002*R_130[0]+-1*P_110110102*R_131[0]+P_110110202*R_132[0]; double PR_002020000020=P_002020000*R_020[0]+-1*P_002120000*R_030[0]+P_002220000*R_040[0]+-1*P_102020000*R_120[0]+P_102120000*R_130[0]+-1*P_102220000*R_140[0]+P_202020000*R_220[0]+-1*P_202120000*R_230[0]+P_202220000*R_240[0]; double PR_001021000020=P_001021000*R_020[0]+-1*P_001121000*R_030[0]+P_001221000*R_040[0]+-1*P_001321000*R_050[0]+-1*P_101021000*R_120[0]+P_101121000*R_130[0]+-1*P_101221000*R_140[0]+P_101321000*R_150[0]; double PR_000022000020=P_000022000*R_020[0]+-1*P_000122000*R_030[0]+P_000222000*R_040[0]+-1*P_000322000*R_050[0]+P_000422000*R_060[0]; double PR_001020001020=P_001020001*R_020[0]+-1*P_001020101*R_021[0]+-1*P_001120001*R_030[0]+P_001120101*R_031[0]+P_001220001*R_040[0]+-1*P_001220101*R_041[0]+-1*P_101020001*R_120[0]+P_101020101*R_121[0]+P_101120001*R_130[0]+-1*P_101120101*R_131[0]+-1*P_101220001*R_140[0]+P_101220101*R_141[0]; double PR_000021001020=P_000021001*R_020[0]+-1*P_000021101*R_021[0]+-1*P_000121001*R_030[0]+P_000121101*R_031[0]+P_000221001*R_040[0]+-1*P_000221101*R_041[0]+-1*P_000321001*R_050[0]+P_000321101*R_051[0]; double PR_000020002020=P_000020002*R_020[0]+-1*P_000020102*R_021[0]+P_000020202*R_022[0]+-1*P_000120002*R_030[0]+P_000120102*R_031[0]+-1*P_000120202*R_032[0]+P_000220002*R_040[0]+-1*P_000220102*R_041[0]+P_000220202*R_042[0]; double PR_012000010020=P_012000010*R_020[0]+-1*P_012000110*R_021[0]+-1*P_112000010*R_120[0]+P_112000110*R_121[0]+P_212000010*R_220[0]+-1*P_212000110*R_221[0]+-1*P_312000010*R_320[0]+P_312000110*R_321[0]; double PR_011001010020=P_011001010*R_020[0]+-1*P_011001110*R_021[0]+-1*P_011101010*R_030[0]+P_011101110*R_031[0]+-1*P_111001010*R_120[0]+P_111001110*R_121[0]+P_111101010*R_130[0]+-1*P_111101110*R_131[0]+P_211001010*R_220[0]+-1*P_211001110*R_221[0]+-1*P_211101010*R_230[0]+P_211101110*R_231[0]; double PR_010002010020=P_010002010*R_020[0]+-1*P_010002110*R_021[0]+-1*P_010102010*R_030[0]+P_010102110*R_031[0]+P_010202010*R_040[0]+-1*P_010202110*R_041[0]+-1*P_110002010*R_120[0]+P_110002110*R_121[0]+P_110102010*R_130[0]+-1*P_110102110*R_131[0]+-1*P_110202010*R_140[0]+P_110202110*R_141[0]; double PR_011000011020=P_011000011*R_020[0]+-1*P_011000111*R_021[0]+P_011000211*R_022[0]+-1*P_111000011*R_120[0]+P_111000111*R_121[0]+-1*P_111000211*R_122[0]+P_211000011*R_220[0]+-1*P_211000111*R_221[0]+P_211000211*R_222[0]; double PR_010001011020=P_010001011*R_020[0]+-1*P_010001111*R_021[0]+P_010001211*R_022[0]+-1*P_010101011*R_030[0]+P_010101111*R_031[0]+-1*P_010101211*R_032[0]+-1*P_110001011*R_120[0]+P_110001111*R_121[0]+-1*P_110001211*R_122[0]+P_110101011*R_130[0]+-1*P_110101111*R_131[0]+P_110101211*R_132[0]; double PR_010000012020=P_010000012*R_020[0]+-1*P_010000112*R_021[0]+P_010000212*R_022[0]+-1*P_010000312*R_023[0]+-1*P_110000012*R_120[0]+P_110000112*R_121[0]+-1*P_110000212*R_122[0]+P_110000312*R_123[0]; double PR_002010010020=P_002010010*R_020[0]+-1*P_002010110*R_021[0]+-1*P_002110010*R_030[0]+P_002110110*R_031[0]+-1*P_102010010*R_120[0]+P_102010110*R_121[0]+P_102110010*R_130[0]+-1*P_102110110*R_131[0]+P_202010010*R_220[0]+-1*P_202010110*R_221[0]+-1*P_202110010*R_230[0]+P_202110110*R_231[0]; double PR_001011010020=P_001011010*R_020[0]+-1*P_001011110*R_021[0]+-1*P_001111010*R_030[0]+P_001111110*R_031[0]+P_001211010*R_040[0]+-1*P_001211110*R_041[0]+-1*P_101011010*R_120[0]+P_101011110*R_121[0]+P_101111010*R_130[0]+-1*P_101111110*R_131[0]+-1*P_101211010*R_140[0]+P_101211110*R_141[0]; double PR_000012010020=P_000012010*R_020[0]+-1*P_000012110*R_021[0]+-1*P_000112010*R_030[0]+P_000112110*R_031[0]+P_000212010*R_040[0]+-1*P_000212110*R_041[0]+-1*P_000312010*R_050[0]+P_000312110*R_051[0]; double PR_001010011020=P_001010011*R_020[0]+-1*P_001010111*R_021[0]+P_001010211*R_022[0]+-1*P_001110011*R_030[0]+P_001110111*R_031[0]+-1*P_001110211*R_032[0]+-1*P_101010011*R_120[0]+P_101010111*R_121[0]+-1*P_101010211*R_122[0]+P_101110011*R_130[0]+-1*P_101110111*R_131[0]+P_101110211*R_132[0]; double PR_000011011020=P_000011011*R_020[0]+-1*P_000011111*R_021[0]+P_000011211*R_022[0]+-1*P_000111011*R_030[0]+P_000111111*R_031[0]+-1*P_000111211*R_032[0]+P_000211011*R_040[0]+-1*P_000211111*R_041[0]+P_000211211*R_042[0]; double PR_000010012020=P_000010012*R_020[0]+-1*P_000010112*R_021[0]+P_000010212*R_022[0]+-1*P_000010312*R_023[0]+-1*P_000110012*R_030[0]+P_000110112*R_031[0]+-1*P_000110212*R_032[0]+P_000110312*R_033[0]; double PR_002000020020=P_002000020*R_020[0]+-1*P_002000120*R_021[0]+P_002000220*R_022[0]+-1*P_102000020*R_120[0]+P_102000120*R_121[0]+-1*P_102000220*R_122[0]+P_202000020*R_220[0]+-1*P_202000120*R_221[0]+P_202000220*R_222[0]; double PR_001001020020=P_001001020*R_020[0]+-1*P_001001120*R_021[0]+P_001001220*R_022[0]+-1*P_001101020*R_030[0]+P_001101120*R_031[0]+-1*P_001101220*R_032[0]+-1*P_101001020*R_120[0]+P_101001120*R_121[0]+-1*P_101001220*R_122[0]+P_101101020*R_130[0]+-1*P_101101120*R_131[0]+P_101101220*R_132[0]; double PR_000002020020=P_000002020*R_020[0]+-1*P_000002120*R_021[0]+P_000002220*R_022[0]+-1*P_000102020*R_030[0]+P_000102120*R_031[0]+-1*P_000102220*R_032[0]+P_000202020*R_040[0]+-1*P_000202120*R_041[0]+P_000202220*R_042[0]; double PR_001000021020=P_001000021*R_020[0]+-1*P_001000121*R_021[0]+P_001000221*R_022[0]+-1*P_001000321*R_023[0]+-1*P_101000021*R_120[0]+P_101000121*R_121[0]+-1*P_101000221*R_122[0]+P_101000321*R_123[0]; double PR_000001021020=P_000001021*R_020[0]+-1*P_000001121*R_021[0]+P_000001221*R_022[0]+-1*P_000001321*R_023[0]+-1*P_000101021*R_030[0]+P_000101121*R_031[0]+-1*P_000101221*R_032[0]+P_000101321*R_033[0]; double PR_000000022020=P_000000022*R_020[0]+-1*P_000000122*R_021[0]+P_000000222*R_022[0]+-1*P_000000322*R_023[0]+P_000000422*R_024[0]; double PR_022000000101=P_022000000*R_101[0]+-1*P_122000000*R_201[0]+P_222000000*R_301[0]+-1*P_322000000*R_401[0]+P_422000000*R_501[0]; double PR_021001000101=P_021001000*R_101[0]+-1*P_021101000*R_111[0]+-1*P_121001000*R_201[0]+P_121101000*R_211[0]+P_221001000*R_301[0]+-1*P_221101000*R_311[0]+-1*P_321001000*R_401[0]+P_321101000*R_411[0]; double PR_020002000101=P_020002000*R_101[0]+-1*P_020102000*R_111[0]+P_020202000*R_121[0]+-1*P_120002000*R_201[0]+P_120102000*R_211[0]+-1*P_120202000*R_221[0]+P_220002000*R_301[0]+-1*P_220102000*R_311[0]+P_220202000*R_321[0]; double PR_021000001101=P_021000001*R_101[0]+-1*P_021000101*R_102[0]+-1*P_121000001*R_201[0]+P_121000101*R_202[0]+P_221000001*R_301[0]+-1*P_221000101*R_302[0]+-1*P_321000001*R_401[0]+P_321000101*R_402[0]; double PR_020001001101=P_020001001*R_101[0]+-1*P_020001101*R_102[0]+-1*P_020101001*R_111[0]+P_020101101*R_112[0]+-1*P_120001001*R_201[0]+P_120001101*R_202[0]+P_120101001*R_211[0]+-1*P_120101101*R_212[0]+P_220001001*R_301[0]+-1*P_220001101*R_302[0]+-1*P_220101001*R_311[0]+P_220101101*R_312[0]; double PR_020000002101=P_020000002*R_101[0]+-1*P_020000102*R_102[0]+P_020000202*R_103[0]+-1*P_120000002*R_201[0]+P_120000102*R_202[0]+-1*P_120000202*R_203[0]+P_220000002*R_301[0]+-1*P_220000102*R_302[0]+P_220000202*R_303[0]; double PR_012010000101=P_012010000*R_101[0]+-1*P_012110000*R_111[0]+-1*P_112010000*R_201[0]+P_112110000*R_211[0]+P_212010000*R_301[0]+-1*P_212110000*R_311[0]+-1*P_312010000*R_401[0]+P_312110000*R_411[0]; double PR_011011000101=P_011011000*R_101[0]+-1*P_011111000*R_111[0]+P_011211000*R_121[0]+-1*P_111011000*R_201[0]+P_111111000*R_211[0]+-1*P_111211000*R_221[0]+P_211011000*R_301[0]+-1*P_211111000*R_311[0]+P_211211000*R_321[0]; double PR_010012000101=P_010012000*R_101[0]+-1*P_010112000*R_111[0]+P_010212000*R_121[0]+-1*P_010312000*R_131[0]+-1*P_110012000*R_201[0]+P_110112000*R_211[0]+-1*P_110212000*R_221[0]+P_110312000*R_231[0]; double PR_011010001101=P_011010001*R_101[0]+-1*P_011010101*R_102[0]+-1*P_011110001*R_111[0]+P_011110101*R_112[0]+-1*P_111010001*R_201[0]+P_111010101*R_202[0]+P_111110001*R_211[0]+-1*P_111110101*R_212[0]+P_211010001*R_301[0]+-1*P_211010101*R_302[0]+-1*P_211110001*R_311[0]+P_211110101*R_312[0]; double PR_010011001101=P_010011001*R_101[0]+-1*P_010011101*R_102[0]+-1*P_010111001*R_111[0]+P_010111101*R_112[0]+P_010211001*R_121[0]+-1*P_010211101*R_122[0]+-1*P_110011001*R_201[0]+P_110011101*R_202[0]+P_110111001*R_211[0]+-1*P_110111101*R_212[0]+-1*P_110211001*R_221[0]+P_110211101*R_222[0]; double PR_010010002101=P_010010002*R_101[0]+-1*P_010010102*R_102[0]+P_010010202*R_103[0]+-1*P_010110002*R_111[0]+P_010110102*R_112[0]+-1*P_010110202*R_113[0]+-1*P_110010002*R_201[0]+P_110010102*R_202[0]+-1*P_110010202*R_203[0]+P_110110002*R_211[0]+-1*P_110110102*R_212[0]+P_110110202*R_213[0]; double PR_002020000101=P_002020000*R_101[0]+-1*P_002120000*R_111[0]+P_002220000*R_121[0]+-1*P_102020000*R_201[0]+P_102120000*R_211[0]+-1*P_102220000*R_221[0]+P_202020000*R_301[0]+-1*P_202120000*R_311[0]+P_202220000*R_321[0]; double PR_001021000101=P_001021000*R_101[0]+-1*P_001121000*R_111[0]+P_001221000*R_121[0]+-1*P_001321000*R_131[0]+-1*P_101021000*R_201[0]+P_101121000*R_211[0]+-1*P_101221000*R_221[0]+P_101321000*R_231[0]; double PR_000022000101=P_000022000*R_101[0]+-1*P_000122000*R_111[0]+P_000222000*R_121[0]+-1*P_000322000*R_131[0]+P_000422000*R_141[0]; double PR_001020001101=P_001020001*R_101[0]+-1*P_001020101*R_102[0]+-1*P_001120001*R_111[0]+P_001120101*R_112[0]+P_001220001*R_121[0]+-1*P_001220101*R_122[0]+-1*P_101020001*R_201[0]+P_101020101*R_202[0]+P_101120001*R_211[0]+-1*P_101120101*R_212[0]+-1*P_101220001*R_221[0]+P_101220101*R_222[0]; double PR_000021001101=P_000021001*R_101[0]+-1*P_000021101*R_102[0]+-1*P_000121001*R_111[0]+P_000121101*R_112[0]+P_000221001*R_121[0]+-1*P_000221101*R_122[0]+-1*P_000321001*R_131[0]+P_000321101*R_132[0]; double PR_000020002101=P_000020002*R_101[0]+-1*P_000020102*R_102[0]+P_000020202*R_103[0]+-1*P_000120002*R_111[0]+P_000120102*R_112[0]+-1*P_000120202*R_113[0]+P_000220002*R_121[0]+-1*P_000220102*R_122[0]+P_000220202*R_123[0]; double PR_012000010101=P_012000010*R_101[0]+-1*P_012000110*R_102[0]+-1*P_112000010*R_201[0]+P_112000110*R_202[0]+P_212000010*R_301[0]+-1*P_212000110*R_302[0]+-1*P_312000010*R_401[0]+P_312000110*R_402[0]; double PR_011001010101=P_011001010*R_101[0]+-1*P_011001110*R_102[0]+-1*P_011101010*R_111[0]+P_011101110*R_112[0]+-1*P_111001010*R_201[0]+P_111001110*R_202[0]+P_111101010*R_211[0]+-1*P_111101110*R_212[0]+P_211001010*R_301[0]+-1*P_211001110*R_302[0]+-1*P_211101010*R_311[0]+P_211101110*R_312[0]; double PR_010002010101=P_010002010*R_101[0]+-1*P_010002110*R_102[0]+-1*P_010102010*R_111[0]+P_010102110*R_112[0]+P_010202010*R_121[0]+-1*P_010202110*R_122[0]+-1*P_110002010*R_201[0]+P_110002110*R_202[0]+P_110102010*R_211[0]+-1*P_110102110*R_212[0]+-1*P_110202010*R_221[0]+P_110202110*R_222[0]; double PR_011000011101=P_011000011*R_101[0]+-1*P_011000111*R_102[0]+P_011000211*R_103[0]+-1*P_111000011*R_201[0]+P_111000111*R_202[0]+-1*P_111000211*R_203[0]+P_211000011*R_301[0]+-1*P_211000111*R_302[0]+P_211000211*R_303[0]; double PR_010001011101=P_010001011*R_101[0]+-1*P_010001111*R_102[0]+P_010001211*R_103[0]+-1*P_010101011*R_111[0]+P_010101111*R_112[0]+-1*P_010101211*R_113[0]+-1*P_110001011*R_201[0]+P_110001111*R_202[0]+-1*P_110001211*R_203[0]+P_110101011*R_211[0]+-1*P_110101111*R_212[0]+P_110101211*R_213[0]; double PR_010000012101=P_010000012*R_101[0]+-1*P_010000112*R_102[0]+P_010000212*R_103[0]+-1*P_010000312*R_104[0]+-1*P_110000012*R_201[0]+P_110000112*R_202[0]+-1*P_110000212*R_203[0]+P_110000312*R_204[0]; double PR_002010010101=P_002010010*R_101[0]+-1*P_002010110*R_102[0]+-1*P_002110010*R_111[0]+P_002110110*R_112[0]+-1*P_102010010*R_201[0]+P_102010110*R_202[0]+P_102110010*R_211[0]+-1*P_102110110*R_212[0]+P_202010010*R_301[0]+-1*P_202010110*R_302[0]+-1*P_202110010*R_311[0]+P_202110110*R_312[0]; double PR_001011010101=P_001011010*R_101[0]+-1*P_001011110*R_102[0]+-1*P_001111010*R_111[0]+P_001111110*R_112[0]+P_001211010*R_121[0]+-1*P_001211110*R_122[0]+-1*P_101011010*R_201[0]+P_101011110*R_202[0]+P_101111010*R_211[0]+-1*P_101111110*R_212[0]+-1*P_101211010*R_221[0]+P_101211110*R_222[0]; double PR_000012010101=P_000012010*R_101[0]+-1*P_000012110*R_102[0]+-1*P_000112010*R_111[0]+P_000112110*R_112[0]+P_000212010*R_121[0]+-1*P_000212110*R_122[0]+-1*P_000312010*R_131[0]+P_000312110*R_132[0]; double PR_001010011101=P_001010011*R_101[0]+-1*P_001010111*R_102[0]+P_001010211*R_103[0]+-1*P_001110011*R_111[0]+P_001110111*R_112[0]+-1*P_001110211*R_113[0]+-1*P_101010011*R_201[0]+P_101010111*R_202[0]+-1*P_101010211*R_203[0]+P_101110011*R_211[0]+-1*P_101110111*R_212[0]+P_101110211*R_213[0]; double PR_000011011101=P_000011011*R_101[0]+-1*P_000011111*R_102[0]+P_000011211*R_103[0]+-1*P_000111011*R_111[0]+P_000111111*R_112[0]+-1*P_000111211*R_113[0]+P_000211011*R_121[0]+-1*P_000211111*R_122[0]+P_000211211*R_123[0]; double PR_000010012101=P_000010012*R_101[0]+-1*P_000010112*R_102[0]+P_000010212*R_103[0]+-1*P_000010312*R_104[0]+-1*P_000110012*R_111[0]+P_000110112*R_112[0]+-1*P_000110212*R_113[0]+P_000110312*R_114[0]; double PR_002000020101=P_002000020*R_101[0]+-1*P_002000120*R_102[0]+P_002000220*R_103[0]+-1*P_102000020*R_201[0]+P_102000120*R_202[0]+-1*P_102000220*R_203[0]+P_202000020*R_301[0]+-1*P_202000120*R_302[0]+P_202000220*R_303[0]; double PR_001001020101=P_001001020*R_101[0]+-1*P_001001120*R_102[0]+P_001001220*R_103[0]+-1*P_001101020*R_111[0]+P_001101120*R_112[0]+-1*P_001101220*R_113[0]+-1*P_101001020*R_201[0]+P_101001120*R_202[0]+-1*P_101001220*R_203[0]+P_101101020*R_211[0]+-1*P_101101120*R_212[0]+P_101101220*R_213[0]; double PR_000002020101=P_000002020*R_101[0]+-1*P_000002120*R_102[0]+P_000002220*R_103[0]+-1*P_000102020*R_111[0]+P_000102120*R_112[0]+-1*P_000102220*R_113[0]+P_000202020*R_121[0]+-1*P_000202120*R_122[0]+P_000202220*R_123[0]; double PR_001000021101=P_001000021*R_101[0]+-1*P_001000121*R_102[0]+P_001000221*R_103[0]+-1*P_001000321*R_104[0]+-1*P_101000021*R_201[0]+P_101000121*R_202[0]+-1*P_101000221*R_203[0]+P_101000321*R_204[0]; double PR_000001021101=P_000001021*R_101[0]+-1*P_000001121*R_102[0]+P_000001221*R_103[0]+-1*P_000001321*R_104[0]+-1*P_000101021*R_111[0]+P_000101121*R_112[0]+-1*P_000101221*R_113[0]+P_000101321*R_114[0]; double PR_000000022101=P_000000022*R_101[0]+-1*P_000000122*R_102[0]+P_000000222*R_103[0]+-1*P_000000322*R_104[0]+P_000000422*R_105[0]; double PR_022000000110=P_022000000*R_110[0]+-1*P_122000000*R_210[0]+P_222000000*R_310[0]+-1*P_322000000*R_410[0]+P_422000000*R_510[0]; double PR_021001000110=P_021001000*R_110[0]+-1*P_021101000*R_120[0]+-1*P_121001000*R_210[0]+P_121101000*R_220[0]+P_221001000*R_310[0]+-1*P_221101000*R_320[0]+-1*P_321001000*R_410[0]+P_321101000*R_420[0]; double PR_020002000110=P_020002000*R_110[0]+-1*P_020102000*R_120[0]+P_020202000*R_130[0]+-1*P_120002000*R_210[0]+P_120102000*R_220[0]+-1*P_120202000*R_230[0]+P_220002000*R_310[0]+-1*P_220102000*R_320[0]+P_220202000*R_330[0]; double PR_021000001110=P_021000001*R_110[0]+-1*P_021000101*R_111[0]+-1*P_121000001*R_210[0]+P_121000101*R_211[0]+P_221000001*R_310[0]+-1*P_221000101*R_311[0]+-1*P_321000001*R_410[0]+P_321000101*R_411[0]; double PR_020001001110=P_020001001*R_110[0]+-1*P_020001101*R_111[0]+-1*P_020101001*R_120[0]+P_020101101*R_121[0]+-1*P_120001001*R_210[0]+P_120001101*R_211[0]+P_120101001*R_220[0]+-1*P_120101101*R_221[0]+P_220001001*R_310[0]+-1*P_220001101*R_311[0]+-1*P_220101001*R_320[0]+P_220101101*R_321[0]; double PR_020000002110=P_020000002*R_110[0]+-1*P_020000102*R_111[0]+P_020000202*R_112[0]+-1*P_120000002*R_210[0]+P_120000102*R_211[0]+-1*P_120000202*R_212[0]+P_220000002*R_310[0]+-1*P_220000102*R_311[0]+P_220000202*R_312[0]; double PR_012010000110=P_012010000*R_110[0]+-1*P_012110000*R_120[0]+-1*P_112010000*R_210[0]+P_112110000*R_220[0]+P_212010000*R_310[0]+-1*P_212110000*R_320[0]+-1*P_312010000*R_410[0]+P_312110000*R_420[0]; double PR_011011000110=P_011011000*R_110[0]+-1*P_011111000*R_120[0]+P_011211000*R_130[0]+-1*P_111011000*R_210[0]+P_111111000*R_220[0]+-1*P_111211000*R_230[0]+P_211011000*R_310[0]+-1*P_211111000*R_320[0]+P_211211000*R_330[0]; double PR_010012000110=P_010012000*R_110[0]+-1*P_010112000*R_120[0]+P_010212000*R_130[0]+-1*P_010312000*R_140[0]+-1*P_110012000*R_210[0]+P_110112000*R_220[0]+-1*P_110212000*R_230[0]+P_110312000*R_240[0]; double PR_011010001110=P_011010001*R_110[0]+-1*P_011010101*R_111[0]+-1*P_011110001*R_120[0]+P_011110101*R_121[0]+-1*P_111010001*R_210[0]+P_111010101*R_211[0]+P_111110001*R_220[0]+-1*P_111110101*R_221[0]+P_211010001*R_310[0]+-1*P_211010101*R_311[0]+-1*P_211110001*R_320[0]+P_211110101*R_321[0]; double PR_010011001110=P_010011001*R_110[0]+-1*P_010011101*R_111[0]+-1*P_010111001*R_120[0]+P_010111101*R_121[0]+P_010211001*R_130[0]+-1*P_010211101*R_131[0]+-1*P_110011001*R_210[0]+P_110011101*R_211[0]+P_110111001*R_220[0]+-1*P_110111101*R_221[0]+-1*P_110211001*R_230[0]+P_110211101*R_231[0]; double PR_010010002110=P_010010002*R_110[0]+-1*P_010010102*R_111[0]+P_010010202*R_112[0]+-1*P_010110002*R_120[0]+P_010110102*R_121[0]+-1*P_010110202*R_122[0]+-1*P_110010002*R_210[0]+P_110010102*R_211[0]+-1*P_110010202*R_212[0]+P_110110002*R_220[0]+-1*P_110110102*R_221[0]+P_110110202*R_222[0]; double PR_002020000110=P_002020000*R_110[0]+-1*P_002120000*R_120[0]+P_002220000*R_130[0]+-1*P_102020000*R_210[0]+P_102120000*R_220[0]+-1*P_102220000*R_230[0]+P_202020000*R_310[0]+-1*P_202120000*R_320[0]+P_202220000*R_330[0]; double PR_001021000110=P_001021000*R_110[0]+-1*P_001121000*R_120[0]+P_001221000*R_130[0]+-1*P_001321000*R_140[0]+-1*P_101021000*R_210[0]+P_101121000*R_220[0]+-1*P_101221000*R_230[0]+P_101321000*R_240[0]; double PR_000022000110=P_000022000*R_110[0]+-1*P_000122000*R_120[0]+P_000222000*R_130[0]+-1*P_000322000*R_140[0]+P_000422000*R_150[0]; double PR_001020001110=P_001020001*R_110[0]+-1*P_001020101*R_111[0]+-1*P_001120001*R_120[0]+P_001120101*R_121[0]+P_001220001*R_130[0]+-1*P_001220101*R_131[0]+-1*P_101020001*R_210[0]+P_101020101*R_211[0]+P_101120001*R_220[0]+-1*P_101120101*R_221[0]+-1*P_101220001*R_230[0]+P_101220101*R_231[0]; double PR_000021001110=P_000021001*R_110[0]+-1*P_000021101*R_111[0]+-1*P_000121001*R_120[0]+P_000121101*R_121[0]+P_000221001*R_130[0]+-1*P_000221101*R_131[0]+-1*P_000321001*R_140[0]+P_000321101*R_141[0]; double PR_000020002110=P_000020002*R_110[0]+-1*P_000020102*R_111[0]+P_000020202*R_112[0]+-1*P_000120002*R_120[0]+P_000120102*R_121[0]+-1*P_000120202*R_122[0]+P_000220002*R_130[0]+-1*P_000220102*R_131[0]+P_000220202*R_132[0]; double PR_012000010110=P_012000010*R_110[0]+-1*P_012000110*R_111[0]+-1*P_112000010*R_210[0]+P_112000110*R_211[0]+P_212000010*R_310[0]+-1*P_212000110*R_311[0]+-1*P_312000010*R_410[0]+P_312000110*R_411[0]; double PR_011001010110=P_011001010*R_110[0]+-1*P_011001110*R_111[0]+-1*P_011101010*R_120[0]+P_011101110*R_121[0]+-1*P_111001010*R_210[0]+P_111001110*R_211[0]+P_111101010*R_220[0]+-1*P_111101110*R_221[0]+P_211001010*R_310[0]+-1*P_211001110*R_311[0]+-1*P_211101010*R_320[0]+P_211101110*R_321[0]; double PR_010002010110=P_010002010*R_110[0]+-1*P_010002110*R_111[0]+-1*P_010102010*R_120[0]+P_010102110*R_121[0]+P_010202010*R_130[0]+-1*P_010202110*R_131[0]+-1*P_110002010*R_210[0]+P_110002110*R_211[0]+P_110102010*R_220[0]+-1*P_110102110*R_221[0]+-1*P_110202010*R_230[0]+P_110202110*R_231[0]; double PR_011000011110=P_011000011*R_110[0]+-1*P_011000111*R_111[0]+P_011000211*R_112[0]+-1*P_111000011*R_210[0]+P_111000111*R_211[0]+-1*P_111000211*R_212[0]+P_211000011*R_310[0]+-1*P_211000111*R_311[0]+P_211000211*R_312[0]; double PR_010001011110=P_010001011*R_110[0]+-1*P_010001111*R_111[0]+P_010001211*R_112[0]+-1*P_010101011*R_120[0]+P_010101111*R_121[0]+-1*P_010101211*R_122[0]+-1*P_110001011*R_210[0]+P_110001111*R_211[0]+-1*P_110001211*R_212[0]+P_110101011*R_220[0]+-1*P_110101111*R_221[0]+P_110101211*R_222[0]; double PR_010000012110=P_010000012*R_110[0]+-1*P_010000112*R_111[0]+P_010000212*R_112[0]+-1*P_010000312*R_113[0]+-1*P_110000012*R_210[0]+P_110000112*R_211[0]+-1*P_110000212*R_212[0]+P_110000312*R_213[0]; double PR_002010010110=P_002010010*R_110[0]+-1*P_002010110*R_111[0]+-1*P_002110010*R_120[0]+P_002110110*R_121[0]+-1*P_102010010*R_210[0]+P_102010110*R_211[0]+P_102110010*R_220[0]+-1*P_102110110*R_221[0]+P_202010010*R_310[0]+-1*P_202010110*R_311[0]+-1*P_202110010*R_320[0]+P_202110110*R_321[0]; double PR_001011010110=P_001011010*R_110[0]+-1*P_001011110*R_111[0]+-1*P_001111010*R_120[0]+P_001111110*R_121[0]+P_001211010*R_130[0]+-1*P_001211110*R_131[0]+-1*P_101011010*R_210[0]+P_101011110*R_211[0]+P_101111010*R_220[0]+-1*P_101111110*R_221[0]+-1*P_101211010*R_230[0]+P_101211110*R_231[0]; double PR_000012010110=P_000012010*R_110[0]+-1*P_000012110*R_111[0]+-1*P_000112010*R_120[0]+P_000112110*R_121[0]+P_000212010*R_130[0]+-1*P_000212110*R_131[0]+-1*P_000312010*R_140[0]+P_000312110*R_141[0]; double PR_001010011110=P_001010011*R_110[0]+-1*P_001010111*R_111[0]+P_001010211*R_112[0]+-1*P_001110011*R_120[0]+P_001110111*R_121[0]+-1*P_001110211*R_122[0]+-1*P_101010011*R_210[0]+P_101010111*R_211[0]+-1*P_101010211*R_212[0]+P_101110011*R_220[0]+-1*P_101110111*R_221[0]+P_101110211*R_222[0]; double PR_000011011110=P_000011011*R_110[0]+-1*P_000011111*R_111[0]+P_000011211*R_112[0]+-1*P_000111011*R_120[0]+P_000111111*R_121[0]+-1*P_000111211*R_122[0]+P_000211011*R_130[0]+-1*P_000211111*R_131[0]+P_000211211*R_132[0]; double PR_000010012110=P_000010012*R_110[0]+-1*P_000010112*R_111[0]+P_000010212*R_112[0]+-1*P_000010312*R_113[0]+-1*P_000110012*R_120[0]+P_000110112*R_121[0]+-1*P_000110212*R_122[0]+P_000110312*R_123[0]; double PR_002000020110=P_002000020*R_110[0]+-1*P_002000120*R_111[0]+P_002000220*R_112[0]+-1*P_102000020*R_210[0]+P_102000120*R_211[0]+-1*P_102000220*R_212[0]+P_202000020*R_310[0]+-1*P_202000120*R_311[0]+P_202000220*R_312[0]; double PR_001001020110=P_001001020*R_110[0]+-1*P_001001120*R_111[0]+P_001001220*R_112[0]+-1*P_001101020*R_120[0]+P_001101120*R_121[0]+-1*P_001101220*R_122[0]+-1*P_101001020*R_210[0]+P_101001120*R_211[0]+-1*P_101001220*R_212[0]+P_101101020*R_220[0]+-1*P_101101120*R_221[0]+P_101101220*R_222[0]; double PR_000002020110=P_000002020*R_110[0]+-1*P_000002120*R_111[0]+P_000002220*R_112[0]+-1*P_000102020*R_120[0]+P_000102120*R_121[0]+-1*P_000102220*R_122[0]+P_000202020*R_130[0]+-1*P_000202120*R_131[0]+P_000202220*R_132[0]; double PR_001000021110=P_001000021*R_110[0]+-1*P_001000121*R_111[0]+P_001000221*R_112[0]+-1*P_001000321*R_113[0]+-1*P_101000021*R_210[0]+P_101000121*R_211[0]+-1*P_101000221*R_212[0]+P_101000321*R_213[0]; double PR_000001021110=P_000001021*R_110[0]+-1*P_000001121*R_111[0]+P_000001221*R_112[0]+-1*P_000001321*R_113[0]+-1*P_000101021*R_120[0]+P_000101121*R_121[0]+-1*P_000101221*R_122[0]+P_000101321*R_123[0]; double PR_000000022110=P_000000022*R_110[0]+-1*P_000000122*R_111[0]+P_000000222*R_112[0]+-1*P_000000322*R_113[0]+P_000000422*R_114[0]; double PR_022000000200=P_022000000*R_200[0]+-1*P_122000000*R_300[0]+P_222000000*R_400[0]+-1*P_322000000*R_500[0]+P_422000000*R_600[0]; double PR_021001000200=P_021001000*R_200[0]+-1*P_021101000*R_210[0]+-1*P_121001000*R_300[0]+P_121101000*R_310[0]+P_221001000*R_400[0]+-1*P_221101000*R_410[0]+-1*P_321001000*R_500[0]+P_321101000*R_510[0]; double PR_020002000200=P_020002000*R_200[0]+-1*P_020102000*R_210[0]+P_020202000*R_220[0]+-1*P_120002000*R_300[0]+P_120102000*R_310[0]+-1*P_120202000*R_320[0]+P_220002000*R_400[0]+-1*P_220102000*R_410[0]+P_220202000*R_420[0]; double PR_021000001200=P_021000001*R_200[0]+-1*P_021000101*R_201[0]+-1*P_121000001*R_300[0]+P_121000101*R_301[0]+P_221000001*R_400[0]+-1*P_221000101*R_401[0]+-1*P_321000001*R_500[0]+P_321000101*R_501[0]; double PR_020001001200=P_020001001*R_200[0]+-1*P_020001101*R_201[0]+-1*P_020101001*R_210[0]+P_020101101*R_211[0]+-1*P_120001001*R_300[0]+P_120001101*R_301[0]+P_120101001*R_310[0]+-1*P_120101101*R_311[0]+P_220001001*R_400[0]+-1*P_220001101*R_401[0]+-1*P_220101001*R_410[0]+P_220101101*R_411[0]; double PR_020000002200=P_020000002*R_200[0]+-1*P_020000102*R_201[0]+P_020000202*R_202[0]+-1*P_120000002*R_300[0]+P_120000102*R_301[0]+-1*P_120000202*R_302[0]+P_220000002*R_400[0]+-1*P_220000102*R_401[0]+P_220000202*R_402[0]; double PR_012010000200=P_012010000*R_200[0]+-1*P_012110000*R_210[0]+-1*P_112010000*R_300[0]+P_112110000*R_310[0]+P_212010000*R_400[0]+-1*P_212110000*R_410[0]+-1*P_312010000*R_500[0]+P_312110000*R_510[0]; double PR_011011000200=P_011011000*R_200[0]+-1*P_011111000*R_210[0]+P_011211000*R_220[0]+-1*P_111011000*R_300[0]+P_111111000*R_310[0]+-1*P_111211000*R_320[0]+P_211011000*R_400[0]+-1*P_211111000*R_410[0]+P_211211000*R_420[0]; double PR_010012000200=P_010012000*R_200[0]+-1*P_010112000*R_210[0]+P_010212000*R_220[0]+-1*P_010312000*R_230[0]+-1*P_110012000*R_300[0]+P_110112000*R_310[0]+-1*P_110212000*R_320[0]+P_110312000*R_330[0]; double PR_011010001200=P_011010001*R_200[0]+-1*P_011010101*R_201[0]+-1*P_011110001*R_210[0]+P_011110101*R_211[0]+-1*P_111010001*R_300[0]+P_111010101*R_301[0]+P_111110001*R_310[0]+-1*P_111110101*R_311[0]+P_211010001*R_400[0]+-1*P_211010101*R_401[0]+-1*P_211110001*R_410[0]+P_211110101*R_411[0]; double PR_010011001200=P_010011001*R_200[0]+-1*P_010011101*R_201[0]+-1*P_010111001*R_210[0]+P_010111101*R_211[0]+P_010211001*R_220[0]+-1*P_010211101*R_221[0]+-1*P_110011001*R_300[0]+P_110011101*R_301[0]+P_110111001*R_310[0]+-1*P_110111101*R_311[0]+-1*P_110211001*R_320[0]+P_110211101*R_321[0]; double PR_010010002200=P_010010002*R_200[0]+-1*P_010010102*R_201[0]+P_010010202*R_202[0]+-1*P_010110002*R_210[0]+P_010110102*R_211[0]+-1*P_010110202*R_212[0]+-1*P_110010002*R_300[0]+P_110010102*R_301[0]+-1*P_110010202*R_302[0]+P_110110002*R_310[0]+-1*P_110110102*R_311[0]+P_110110202*R_312[0]; double PR_002020000200=P_002020000*R_200[0]+-1*P_002120000*R_210[0]+P_002220000*R_220[0]+-1*P_102020000*R_300[0]+P_102120000*R_310[0]+-1*P_102220000*R_320[0]+P_202020000*R_400[0]+-1*P_202120000*R_410[0]+P_202220000*R_420[0]; double PR_001021000200=P_001021000*R_200[0]+-1*P_001121000*R_210[0]+P_001221000*R_220[0]+-1*P_001321000*R_230[0]+-1*P_101021000*R_300[0]+P_101121000*R_310[0]+-1*P_101221000*R_320[0]+P_101321000*R_330[0]; double PR_000022000200=P_000022000*R_200[0]+-1*P_000122000*R_210[0]+P_000222000*R_220[0]+-1*P_000322000*R_230[0]+P_000422000*R_240[0]; double PR_001020001200=P_001020001*R_200[0]+-1*P_001020101*R_201[0]+-1*P_001120001*R_210[0]+P_001120101*R_211[0]+P_001220001*R_220[0]+-1*P_001220101*R_221[0]+-1*P_101020001*R_300[0]+P_101020101*R_301[0]+P_101120001*R_310[0]+-1*P_101120101*R_311[0]+-1*P_101220001*R_320[0]+P_101220101*R_321[0]; double PR_000021001200=P_000021001*R_200[0]+-1*P_000021101*R_201[0]+-1*P_000121001*R_210[0]+P_000121101*R_211[0]+P_000221001*R_220[0]+-1*P_000221101*R_221[0]+-1*P_000321001*R_230[0]+P_000321101*R_231[0]; double PR_000020002200=P_000020002*R_200[0]+-1*P_000020102*R_201[0]+P_000020202*R_202[0]+-1*P_000120002*R_210[0]+P_000120102*R_211[0]+-1*P_000120202*R_212[0]+P_000220002*R_220[0]+-1*P_000220102*R_221[0]+P_000220202*R_222[0]; double PR_012000010200=P_012000010*R_200[0]+-1*P_012000110*R_201[0]+-1*P_112000010*R_300[0]+P_112000110*R_301[0]+P_212000010*R_400[0]+-1*P_212000110*R_401[0]+-1*P_312000010*R_500[0]+P_312000110*R_501[0]; double PR_011001010200=P_011001010*R_200[0]+-1*P_011001110*R_201[0]+-1*P_011101010*R_210[0]+P_011101110*R_211[0]+-1*P_111001010*R_300[0]+P_111001110*R_301[0]+P_111101010*R_310[0]+-1*P_111101110*R_311[0]+P_211001010*R_400[0]+-1*P_211001110*R_401[0]+-1*P_211101010*R_410[0]+P_211101110*R_411[0]; double PR_010002010200=P_010002010*R_200[0]+-1*P_010002110*R_201[0]+-1*P_010102010*R_210[0]+P_010102110*R_211[0]+P_010202010*R_220[0]+-1*P_010202110*R_221[0]+-1*P_110002010*R_300[0]+P_110002110*R_301[0]+P_110102010*R_310[0]+-1*P_110102110*R_311[0]+-1*P_110202010*R_320[0]+P_110202110*R_321[0]; double PR_011000011200=P_011000011*R_200[0]+-1*P_011000111*R_201[0]+P_011000211*R_202[0]+-1*P_111000011*R_300[0]+P_111000111*R_301[0]+-1*P_111000211*R_302[0]+P_211000011*R_400[0]+-1*P_211000111*R_401[0]+P_211000211*R_402[0]; double PR_010001011200=P_010001011*R_200[0]+-1*P_010001111*R_201[0]+P_010001211*R_202[0]+-1*P_010101011*R_210[0]+P_010101111*R_211[0]+-1*P_010101211*R_212[0]+-1*P_110001011*R_300[0]+P_110001111*R_301[0]+-1*P_110001211*R_302[0]+P_110101011*R_310[0]+-1*P_110101111*R_311[0]+P_110101211*R_312[0]; double PR_010000012200=P_010000012*R_200[0]+-1*P_010000112*R_201[0]+P_010000212*R_202[0]+-1*P_010000312*R_203[0]+-1*P_110000012*R_300[0]+P_110000112*R_301[0]+-1*P_110000212*R_302[0]+P_110000312*R_303[0]; double PR_002010010200=P_002010010*R_200[0]+-1*P_002010110*R_201[0]+-1*P_002110010*R_210[0]+P_002110110*R_211[0]+-1*P_102010010*R_300[0]+P_102010110*R_301[0]+P_102110010*R_310[0]+-1*P_102110110*R_311[0]+P_202010010*R_400[0]+-1*P_202010110*R_401[0]+-1*P_202110010*R_410[0]+P_202110110*R_411[0]; double PR_001011010200=P_001011010*R_200[0]+-1*P_001011110*R_201[0]+-1*P_001111010*R_210[0]+P_001111110*R_211[0]+P_001211010*R_220[0]+-1*P_001211110*R_221[0]+-1*P_101011010*R_300[0]+P_101011110*R_301[0]+P_101111010*R_310[0]+-1*P_101111110*R_311[0]+-1*P_101211010*R_320[0]+P_101211110*R_321[0]; double PR_000012010200=P_000012010*R_200[0]+-1*P_000012110*R_201[0]+-1*P_000112010*R_210[0]+P_000112110*R_211[0]+P_000212010*R_220[0]+-1*P_000212110*R_221[0]+-1*P_000312010*R_230[0]+P_000312110*R_231[0]; double PR_001010011200=P_001010011*R_200[0]+-1*P_001010111*R_201[0]+P_001010211*R_202[0]+-1*P_001110011*R_210[0]+P_001110111*R_211[0]+-1*P_001110211*R_212[0]+-1*P_101010011*R_300[0]+P_101010111*R_301[0]+-1*P_101010211*R_302[0]+P_101110011*R_310[0]+-1*P_101110111*R_311[0]+P_101110211*R_312[0]; double PR_000011011200=P_000011011*R_200[0]+-1*P_000011111*R_201[0]+P_000011211*R_202[0]+-1*P_000111011*R_210[0]+P_000111111*R_211[0]+-1*P_000111211*R_212[0]+P_000211011*R_220[0]+-1*P_000211111*R_221[0]+P_000211211*R_222[0]; double PR_000010012200=P_000010012*R_200[0]+-1*P_000010112*R_201[0]+P_000010212*R_202[0]+-1*P_000010312*R_203[0]+-1*P_000110012*R_210[0]+P_000110112*R_211[0]+-1*P_000110212*R_212[0]+P_000110312*R_213[0]; double PR_002000020200=P_002000020*R_200[0]+-1*P_002000120*R_201[0]+P_002000220*R_202[0]+-1*P_102000020*R_300[0]+P_102000120*R_301[0]+-1*P_102000220*R_302[0]+P_202000020*R_400[0]+-1*P_202000120*R_401[0]+P_202000220*R_402[0]; double PR_001001020200=P_001001020*R_200[0]+-1*P_001001120*R_201[0]+P_001001220*R_202[0]+-1*P_001101020*R_210[0]+P_001101120*R_211[0]+-1*P_001101220*R_212[0]+-1*P_101001020*R_300[0]+P_101001120*R_301[0]+-1*P_101001220*R_302[0]+P_101101020*R_310[0]+-1*P_101101120*R_311[0]+P_101101220*R_312[0]; double PR_000002020200=P_000002020*R_200[0]+-1*P_000002120*R_201[0]+P_000002220*R_202[0]+-1*P_000102020*R_210[0]+P_000102120*R_211[0]+-1*P_000102220*R_212[0]+P_000202020*R_220[0]+-1*P_000202120*R_221[0]+P_000202220*R_222[0]; double PR_001000021200=P_001000021*R_200[0]+-1*P_001000121*R_201[0]+P_001000221*R_202[0]+-1*P_001000321*R_203[0]+-1*P_101000021*R_300[0]+P_101000121*R_301[0]+-1*P_101000221*R_302[0]+P_101000321*R_303[0]; double PR_000001021200=P_000001021*R_200[0]+-1*P_000001121*R_201[0]+P_000001221*R_202[0]+-1*P_000001321*R_203[0]+-1*P_000101021*R_210[0]+P_000101121*R_211[0]+-1*P_000101221*R_212[0]+P_000101321*R_213[0]; double PR_000000022200=P_000000022*R_200[0]+-1*P_000000122*R_201[0]+P_000000222*R_202[0]+-1*P_000000322*R_203[0]+P_000000422*R_204[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(Q_002000000*PR_022000000000+Q_102000000*PR_022000000100+Q_202000000*PR_022000000200); ans_temp[ans_id*6+0]+=Pmtrx[1]*(Q_001001000*PR_022000000000+Q_001101000*PR_022000000010+Q_101001000*PR_022000000100+Q_101101000*PR_022000000110); ans_temp[ans_id*6+0]+=Pmtrx[2]*(Q_000002000*PR_022000000000+Q_000102000*PR_022000000010+Q_000202000*PR_022000000020); ans_temp[ans_id*6+0]+=Pmtrx[3]*(Q_001000001*PR_022000000000+Q_001000101*PR_022000000001+Q_101000001*PR_022000000100+Q_101000101*PR_022000000101); ans_temp[ans_id*6+0]+=Pmtrx[4]*(Q_000001001*PR_022000000000+Q_000001101*PR_022000000001+Q_000101001*PR_022000000010+Q_000101101*PR_022000000011); ans_temp[ans_id*6+0]+=Pmtrx[5]*(Q_000000002*PR_022000000000+Q_000000102*PR_022000000001+Q_000000202*PR_022000000002); ans_temp[ans_id*6+0]+=Pmtrx[6]*(Q_002000000*PR_021001000000+Q_102000000*PR_021001000100+Q_202000000*PR_021001000200); ans_temp[ans_id*6+0]+=Pmtrx[7]*(Q_001001000*PR_021001000000+Q_001101000*PR_021001000010+Q_101001000*PR_021001000100+Q_101101000*PR_021001000110); ans_temp[ans_id*6+0]+=Pmtrx[8]*(Q_000002000*PR_021001000000+Q_000102000*PR_021001000010+Q_000202000*PR_021001000020); ans_temp[ans_id*6+0]+=Pmtrx[9]*(Q_001000001*PR_021001000000+Q_001000101*PR_021001000001+Q_101000001*PR_021001000100+Q_101000101*PR_021001000101); ans_temp[ans_id*6+0]+=Pmtrx[10]*(Q_000001001*PR_021001000000+Q_000001101*PR_021001000001+Q_000101001*PR_021001000010+Q_000101101*PR_021001000011); ans_temp[ans_id*6+0]+=Pmtrx[11]*(Q_000000002*PR_021001000000+Q_000000102*PR_021001000001+Q_000000202*PR_021001000002); ans_temp[ans_id*6+0]+=Pmtrx[12]*(Q_002000000*PR_020002000000+Q_102000000*PR_020002000100+Q_202000000*PR_020002000200); ans_temp[ans_id*6+0]+=Pmtrx[13]*(Q_001001000*PR_020002000000+Q_001101000*PR_020002000010+Q_101001000*PR_020002000100+Q_101101000*PR_020002000110); ans_temp[ans_id*6+0]+=Pmtrx[14]*(Q_000002000*PR_020002000000+Q_000102000*PR_020002000010+Q_000202000*PR_020002000020); ans_temp[ans_id*6+0]+=Pmtrx[15]*(Q_001000001*PR_020002000000+Q_001000101*PR_020002000001+Q_101000001*PR_020002000100+Q_101000101*PR_020002000101); ans_temp[ans_id*6+0]+=Pmtrx[16]*(Q_000001001*PR_020002000000+Q_000001101*PR_020002000001+Q_000101001*PR_020002000010+Q_000101101*PR_020002000011); ans_temp[ans_id*6+0]+=Pmtrx[17]*(Q_000000002*PR_020002000000+Q_000000102*PR_020002000001+Q_000000202*PR_020002000002); ans_temp[ans_id*6+0]+=Pmtrx[18]*(Q_002000000*PR_021000001000+Q_102000000*PR_021000001100+Q_202000000*PR_021000001200); ans_temp[ans_id*6+0]+=Pmtrx[19]*(Q_001001000*PR_021000001000+Q_001101000*PR_021000001010+Q_101001000*PR_021000001100+Q_101101000*PR_021000001110); ans_temp[ans_id*6+0]+=Pmtrx[20]*(Q_000002000*PR_021000001000+Q_000102000*PR_021000001010+Q_000202000*PR_021000001020); ans_temp[ans_id*6+0]+=Pmtrx[21]*(Q_001000001*PR_021000001000+Q_001000101*PR_021000001001+Q_101000001*PR_021000001100+Q_101000101*PR_021000001101); ans_temp[ans_id*6+0]+=Pmtrx[22]*(Q_000001001*PR_021000001000+Q_000001101*PR_021000001001+Q_000101001*PR_021000001010+Q_000101101*PR_021000001011); ans_temp[ans_id*6+0]+=Pmtrx[23]*(Q_000000002*PR_021000001000+Q_000000102*PR_021000001001+Q_000000202*PR_021000001002); ans_temp[ans_id*6+0]+=Pmtrx[24]*(Q_002000000*PR_020001001000+Q_102000000*PR_020001001100+Q_202000000*PR_020001001200); ans_temp[ans_id*6+0]+=Pmtrx[25]*(Q_001001000*PR_020001001000+Q_001101000*PR_020001001010+Q_101001000*PR_020001001100+Q_101101000*PR_020001001110); ans_temp[ans_id*6+0]+=Pmtrx[26]*(Q_000002000*PR_020001001000+Q_000102000*PR_020001001010+Q_000202000*PR_020001001020); ans_temp[ans_id*6+0]+=Pmtrx[27]*(Q_001000001*PR_020001001000+Q_001000101*PR_020001001001+Q_101000001*PR_020001001100+Q_101000101*PR_020001001101); ans_temp[ans_id*6+0]+=Pmtrx[28]*(Q_000001001*PR_020001001000+Q_000001101*PR_020001001001+Q_000101001*PR_020001001010+Q_000101101*PR_020001001011); ans_temp[ans_id*6+0]+=Pmtrx[29]*(Q_000000002*PR_020001001000+Q_000000102*PR_020001001001+Q_000000202*PR_020001001002); ans_temp[ans_id*6+0]+=Pmtrx[30]*(Q_002000000*PR_020000002000+Q_102000000*PR_020000002100+Q_202000000*PR_020000002200); ans_temp[ans_id*6+0]+=Pmtrx[31]*(Q_001001000*PR_020000002000+Q_001101000*PR_020000002010+Q_101001000*PR_020000002100+Q_101101000*PR_020000002110); ans_temp[ans_id*6+0]+=Pmtrx[32]*(Q_000002000*PR_020000002000+Q_000102000*PR_020000002010+Q_000202000*PR_020000002020); ans_temp[ans_id*6+0]+=Pmtrx[33]*(Q_001000001*PR_020000002000+Q_001000101*PR_020000002001+Q_101000001*PR_020000002100+Q_101000101*PR_020000002101); ans_temp[ans_id*6+0]+=Pmtrx[34]*(Q_000001001*PR_020000002000+Q_000001101*PR_020000002001+Q_000101001*PR_020000002010+Q_000101101*PR_020000002011); ans_temp[ans_id*6+0]+=Pmtrx[35]*(Q_000000002*PR_020000002000+Q_000000102*PR_020000002001+Q_000000202*PR_020000002002); ans_temp[ans_id*6+1]+=Pmtrx[0]*(Q_002000000*PR_012010000000+Q_102000000*PR_012010000100+Q_202000000*PR_012010000200); ans_temp[ans_id*6+1]+=Pmtrx[1]*(Q_001001000*PR_012010000000+Q_001101000*PR_012010000010+Q_101001000*PR_012010000100+Q_101101000*PR_012010000110); ans_temp[ans_id*6+1]+=Pmtrx[2]*(Q_000002000*PR_012010000000+Q_000102000*PR_012010000010+Q_000202000*PR_012010000020); ans_temp[ans_id*6+1]+=Pmtrx[3]*(Q_001000001*PR_012010000000+Q_001000101*PR_012010000001+Q_101000001*PR_012010000100+Q_101000101*PR_012010000101); ans_temp[ans_id*6+1]+=Pmtrx[4]*(Q_000001001*PR_012010000000+Q_000001101*PR_012010000001+Q_000101001*PR_012010000010+Q_000101101*PR_012010000011); ans_temp[ans_id*6+1]+=Pmtrx[5]*(Q_000000002*PR_012010000000+Q_000000102*PR_012010000001+Q_000000202*PR_012010000002); ans_temp[ans_id*6+1]+=Pmtrx[6]*(Q_002000000*PR_011011000000+Q_102000000*PR_011011000100+Q_202000000*PR_011011000200); ans_temp[ans_id*6+1]+=Pmtrx[7]*(Q_001001000*PR_011011000000+Q_001101000*PR_011011000010+Q_101001000*PR_011011000100+Q_101101000*PR_011011000110); ans_temp[ans_id*6+1]+=Pmtrx[8]*(Q_000002000*PR_011011000000+Q_000102000*PR_011011000010+Q_000202000*PR_011011000020); ans_temp[ans_id*6+1]+=Pmtrx[9]*(Q_001000001*PR_011011000000+Q_001000101*PR_011011000001+Q_101000001*PR_011011000100+Q_101000101*PR_011011000101); ans_temp[ans_id*6+1]+=Pmtrx[10]*(Q_000001001*PR_011011000000+Q_000001101*PR_011011000001+Q_000101001*PR_011011000010+Q_000101101*PR_011011000011); ans_temp[ans_id*6+1]+=Pmtrx[11]*(Q_000000002*PR_011011000000+Q_000000102*PR_011011000001+Q_000000202*PR_011011000002); ans_temp[ans_id*6+1]+=Pmtrx[12]*(Q_002000000*PR_010012000000+Q_102000000*PR_010012000100+Q_202000000*PR_010012000200); ans_temp[ans_id*6+1]+=Pmtrx[13]*(Q_001001000*PR_010012000000+Q_001101000*PR_010012000010+Q_101001000*PR_010012000100+Q_101101000*PR_010012000110); ans_temp[ans_id*6+1]+=Pmtrx[14]*(Q_000002000*PR_010012000000+Q_000102000*PR_010012000010+Q_000202000*PR_010012000020); ans_temp[ans_id*6+1]+=Pmtrx[15]*(Q_001000001*PR_010012000000+Q_001000101*PR_010012000001+Q_101000001*PR_010012000100+Q_101000101*PR_010012000101); ans_temp[ans_id*6+1]+=Pmtrx[16]*(Q_000001001*PR_010012000000+Q_000001101*PR_010012000001+Q_000101001*PR_010012000010+Q_000101101*PR_010012000011); ans_temp[ans_id*6+1]+=Pmtrx[17]*(Q_000000002*PR_010012000000+Q_000000102*PR_010012000001+Q_000000202*PR_010012000002); ans_temp[ans_id*6+1]+=Pmtrx[18]*(Q_002000000*PR_011010001000+Q_102000000*PR_011010001100+Q_202000000*PR_011010001200); ans_temp[ans_id*6+1]+=Pmtrx[19]*(Q_001001000*PR_011010001000+Q_001101000*PR_011010001010+Q_101001000*PR_011010001100+Q_101101000*PR_011010001110); ans_temp[ans_id*6+1]+=Pmtrx[20]*(Q_000002000*PR_011010001000+Q_000102000*PR_011010001010+Q_000202000*PR_011010001020); ans_temp[ans_id*6+1]+=Pmtrx[21]*(Q_001000001*PR_011010001000+Q_001000101*PR_011010001001+Q_101000001*PR_011010001100+Q_101000101*PR_011010001101); ans_temp[ans_id*6+1]+=Pmtrx[22]*(Q_000001001*PR_011010001000+Q_000001101*PR_011010001001+Q_000101001*PR_011010001010+Q_000101101*PR_011010001011); ans_temp[ans_id*6+1]+=Pmtrx[23]*(Q_000000002*PR_011010001000+Q_000000102*PR_011010001001+Q_000000202*PR_011010001002); ans_temp[ans_id*6+1]+=Pmtrx[24]*(Q_002000000*PR_010011001000+Q_102000000*PR_010011001100+Q_202000000*PR_010011001200); ans_temp[ans_id*6+1]+=Pmtrx[25]*(Q_001001000*PR_010011001000+Q_001101000*PR_010011001010+Q_101001000*PR_010011001100+Q_101101000*PR_010011001110); ans_temp[ans_id*6+1]+=Pmtrx[26]*(Q_000002000*PR_010011001000+Q_000102000*PR_010011001010+Q_000202000*PR_010011001020); ans_temp[ans_id*6+1]+=Pmtrx[27]*(Q_001000001*PR_010011001000+Q_001000101*PR_010011001001+Q_101000001*PR_010011001100+Q_101000101*PR_010011001101); ans_temp[ans_id*6+1]+=Pmtrx[28]*(Q_000001001*PR_010011001000+Q_000001101*PR_010011001001+Q_000101001*PR_010011001010+Q_000101101*PR_010011001011); ans_temp[ans_id*6+1]+=Pmtrx[29]*(Q_000000002*PR_010011001000+Q_000000102*PR_010011001001+Q_000000202*PR_010011001002); ans_temp[ans_id*6+1]+=Pmtrx[30]*(Q_002000000*PR_010010002000+Q_102000000*PR_010010002100+Q_202000000*PR_010010002200); ans_temp[ans_id*6+1]+=Pmtrx[31]*(Q_001001000*PR_010010002000+Q_001101000*PR_010010002010+Q_101001000*PR_010010002100+Q_101101000*PR_010010002110); ans_temp[ans_id*6+1]+=Pmtrx[32]*(Q_000002000*PR_010010002000+Q_000102000*PR_010010002010+Q_000202000*PR_010010002020); ans_temp[ans_id*6+1]+=Pmtrx[33]*(Q_001000001*PR_010010002000+Q_001000101*PR_010010002001+Q_101000001*PR_010010002100+Q_101000101*PR_010010002101); ans_temp[ans_id*6+1]+=Pmtrx[34]*(Q_000001001*PR_010010002000+Q_000001101*PR_010010002001+Q_000101001*PR_010010002010+Q_000101101*PR_010010002011); ans_temp[ans_id*6+1]+=Pmtrx[35]*(Q_000000002*PR_010010002000+Q_000000102*PR_010010002001+Q_000000202*PR_010010002002); ans_temp[ans_id*6+2]+=Pmtrx[0]*(Q_002000000*PR_002020000000+Q_102000000*PR_002020000100+Q_202000000*PR_002020000200); ans_temp[ans_id*6+2]+=Pmtrx[1]*(Q_001001000*PR_002020000000+Q_001101000*PR_002020000010+Q_101001000*PR_002020000100+Q_101101000*PR_002020000110); ans_temp[ans_id*6+2]+=Pmtrx[2]*(Q_000002000*PR_002020000000+Q_000102000*PR_002020000010+Q_000202000*PR_002020000020); ans_temp[ans_id*6+2]+=Pmtrx[3]*(Q_001000001*PR_002020000000+Q_001000101*PR_002020000001+Q_101000001*PR_002020000100+Q_101000101*PR_002020000101); ans_temp[ans_id*6+2]+=Pmtrx[4]*(Q_000001001*PR_002020000000+Q_000001101*PR_002020000001+Q_000101001*PR_002020000010+Q_000101101*PR_002020000011); ans_temp[ans_id*6+2]+=Pmtrx[5]*(Q_000000002*PR_002020000000+Q_000000102*PR_002020000001+Q_000000202*PR_002020000002); ans_temp[ans_id*6+2]+=Pmtrx[6]*(Q_002000000*PR_001021000000+Q_102000000*PR_001021000100+Q_202000000*PR_001021000200); ans_temp[ans_id*6+2]+=Pmtrx[7]*(Q_001001000*PR_001021000000+Q_001101000*PR_001021000010+Q_101001000*PR_001021000100+Q_101101000*PR_001021000110); ans_temp[ans_id*6+2]+=Pmtrx[8]*(Q_000002000*PR_001021000000+Q_000102000*PR_001021000010+Q_000202000*PR_001021000020); ans_temp[ans_id*6+2]+=Pmtrx[9]*(Q_001000001*PR_001021000000+Q_001000101*PR_001021000001+Q_101000001*PR_001021000100+Q_101000101*PR_001021000101); ans_temp[ans_id*6+2]+=Pmtrx[10]*(Q_000001001*PR_001021000000+Q_000001101*PR_001021000001+Q_000101001*PR_001021000010+Q_000101101*PR_001021000011); ans_temp[ans_id*6+2]+=Pmtrx[11]*(Q_000000002*PR_001021000000+Q_000000102*PR_001021000001+Q_000000202*PR_001021000002); ans_temp[ans_id*6+2]+=Pmtrx[12]*(Q_002000000*PR_000022000000+Q_102000000*PR_000022000100+Q_202000000*PR_000022000200); ans_temp[ans_id*6+2]+=Pmtrx[13]*(Q_001001000*PR_000022000000+Q_001101000*PR_000022000010+Q_101001000*PR_000022000100+Q_101101000*PR_000022000110); ans_temp[ans_id*6+2]+=Pmtrx[14]*(Q_000002000*PR_000022000000+Q_000102000*PR_000022000010+Q_000202000*PR_000022000020); ans_temp[ans_id*6+2]+=Pmtrx[15]*(Q_001000001*PR_000022000000+Q_001000101*PR_000022000001+Q_101000001*PR_000022000100+Q_101000101*PR_000022000101); ans_temp[ans_id*6+2]+=Pmtrx[16]*(Q_000001001*PR_000022000000+Q_000001101*PR_000022000001+Q_000101001*PR_000022000010+Q_000101101*PR_000022000011); ans_temp[ans_id*6+2]+=Pmtrx[17]*(Q_000000002*PR_000022000000+Q_000000102*PR_000022000001+Q_000000202*PR_000022000002); ans_temp[ans_id*6+2]+=Pmtrx[18]*(Q_002000000*PR_001020001000+Q_102000000*PR_001020001100+Q_202000000*PR_001020001200); ans_temp[ans_id*6+2]+=Pmtrx[19]*(Q_001001000*PR_001020001000+Q_001101000*PR_001020001010+Q_101001000*PR_001020001100+Q_101101000*PR_001020001110); ans_temp[ans_id*6+2]+=Pmtrx[20]*(Q_000002000*PR_001020001000+Q_000102000*PR_001020001010+Q_000202000*PR_001020001020); ans_temp[ans_id*6+2]+=Pmtrx[21]*(Q_001000001*PR_001020001000+Q_001000101*PR_001020001001+Q_101000001*PR_001020001100+Q_101000101*PR_001020001101); ans_temp[ans_id*6+2]+=Pmtrx[22]*(Q_000001001*PR_001020001000+Q_000001101*PR_001020001001+Q_000101001*PR_001020001010+Q_000101101*PR_001020001011); ans_temp[ans_id*6+2]+=Pmtrx[23]*(Q_000000002*PR_001020001000+Q_000000102*PR_001020001001+Q_000000202*PR_001020001002); ans_temp[ans_id*6+2]+=Pmtrx[24]*(Q_002000000*PR_000021001000+Q_102000000*PR_000021001100+Q_202000000*PR_000021001200); ans_temp[ans_id*6+2]+=Pmtrx[25]*(Q_001001000*PR_000021001000+Q_001101000*PR_000021001010+Q_101001000*PR_000021001100+Q_101101000*PR_000021001110); ans_temp[ans_id*6+2]+=Pmtrx[26]*(Q_000002000*PR_000021001000+Q_000102000*PR_000021001010+Q_000202000*PR_000021001020); ans_temp[ans_id*6+2]+=Pmtrx[27]*(Q_001000001*PR_000021001000+Q_001000101*PR_000021001001+Q_101000001*PR_000021001100+Q_101000101*PR_000021001101); ans_temp[ans_id*6+2]+=Pmtrx[28]*(Q_000001001*PR_000021001000+Q_000001101*PR_000021001001+Q_000101001*PR_000021001010+Q_000101101*PR_000021001011); ans_temp[ans_id*6+2]+=Pmtrx[29]*(Q_000000002*PR_000021001000+Q_000000102*PR_000021001001+Q_000000202*PR_000021001002); ans_temp[ans_id*6+2]+=Pmtrx[30]*(Q_002000000*PR_000020002000+Q_102000000*PR_000020002100+Q_202000000*PR_000020002200); ans_temp[ans_id*6+2]+=Pmtrx[31]*(Q_001001000*PR_000020002000+Q_001101000*PR_000020002010+Q_101001000*PR_000020002100+Q_101101000*PR_000020002110); ans_temp[ans_id*6+2]+=Pmtrx[32]*(Q_000002000*PR_000020002000+Q_000102000*PR_000020002010+Q_000202000*PR_000020002020); ans_temp[ans_id*6+2]+=Pmtrx[33]*(Q_001000001*PR_000020002000+Q_001000101*PR_000020002001+Q_101000001*PR_000020002100+Q_101000101*PR_000020002101); ans_temp[ans_id*6+2]+=Pmtrx[34]*(Q_000001001*PR_000020002000+Q_000001101*PR_000020002001+Q_000101001*PR_000020002010+Q_000101101*PR_000020002011); ans_temp[ans_id*6+2]+=Pmtrx[35]*(Q_000000002*PR_000020002000+Q_000000102*PR_000020002001+Q_000000202*PR_000020002002); ans_temp[ans_id*6+3]+=Pmtrx[0]*(Q_002000000*PR_012000010000+Q_102000000*PR_012000010100+Q_202000000*PR_012000010200); ans_temp[ans_id*6+3]+=Pmtrx[1]*(Q_001001000*PR_012000010000+Q_001101000*PR_012000010010+Q_101001000*PR_012000010100+Q_101101000*PR_012000010110); ans_temp[ans_id*6+3]+=Pmtrx[2]*(Q_000002000*PR_012000010000+Q_000102000*PR_012000010010+Q_000202000*PR_012000010020); ans_temp[ans_id*6+3]+=Pmtrx[3]*(Q_001000001*PR_012000010000+Q_001000101*PR_012000010001+Q_101000001*PR_012000010100+Q_101000101*PR_012000010101); ans_temp[ans_id*6+3]+=Pmtrx[4]*(Q_000001001*PR_012000010000+Q_000001101*PR_012000010001+Q_000101001*PR_012000010010+Q_000101101*PR_012000010011); ans_temp[ans_id*6+3]+=Pmtrx[5]*(Q_000000002*PR_012000010000+Q_000000102*PR_012000010001+Q_000000202*PR_012000010002); ans_temp[ans_id*6+3]+=Pmtrx[6]*(Q_002000000*PR_011001010000+Q_102000000*PR_011001010100+Q_202000000*PR_011001010200); ans_temp[ans_id*6+3]+=Pmtrx[7]*(Q_001001000*PR_011001010000+Q_001101000*PR_011001010010+Q_101001000*PR_011001010100+Q_101101000*PR_011001010110); ans_temp[ans_id*6+3]+=Pmtrx[8]*(Q_000002000*PR_011001010000+Q_000102000*PR_011001010010+Q_000202000*PR_011001010020); ans_temp[ans_id*6+3]+=Pmtrx[9]*(Q_001000001*PR_011001010000+Q_001000101*PR_011001010001+Q_101000001*PR_011001010100+Q_101000101*PR_011001010101); ans_temp[ans_id*6+3]+=Pmtrx[10]*(Q_000001001*PR_011001010000+Q_000001101*PR_011001010001+Q_000101001*PR_011001010010+Q_000101101*PR_011001010011); ans_temp[ans_id*6+3]+=Pmtrx[11]*(Q_000000002*PR_011001010000+Q_000000102*PR_011001010001+Q_000000202*PR_011001010002); ans_temp[ans_id*6+3]+=Pmtrx[12]*(Q_002000000*PR_010002010000+Q_102000000*PR_010002010100+Q_202000000*PR_010002010200); ans_temp[ans_id*6+3]+=Pmtrx[13]*(Q_001001000*PR_010002010000+Q_001101000*PR_010002010010+Q_101001000*PR_010002010100+Q_101101000*PR_010002010110); ans_temp[ans_id*6+3]+=Pmtrx[14]*(Q_000002000*PR_010002010000+Q_000102000*PR_010002010010+Q_000202000*PR_010002010020); ans_temp[ans_id*6+3]+=Pmtrx[15]*(Q_001000001*PR_010002010000+Q_001000101*PR_010002010001+Q_101000001*PR_010002010100+Q_101000101*PR_010002010101); ans_temp[ans_id*6+3]+=Pmtrx[16]*(Q_000001001*PR_010002010000+Q_000001101*PR_010002010001+Q_000101001*PR_010002010010+Q_000101101*PR_010002010011); ans_temp[ans_id*6+3]+=Pmtrx[17]*(Q_000000002*PR_010002010000+Q_000000102*PR_010002010001+Q_000000202*PR_010002010002); ans_temp[ans_id*6+3]+=Pmtrx[18]*(Q_002000000*PR_011000011000+Q_102000000*PR_011000011100+Q_202000000*PR_011000011200); ans_temp[ans_id*6+3]+=Pmtrx[19]*(Q_001001000*PR_011000011000+Q_001101000*PR_011000011010+Q_101001000*PR_011000011100+Q_101101000*PR_011000011110); ans_temp[ans_id*6+3]+=Pmtrx[20]*(Q_000002000*PR_011000011000+Q_000102000*PR_011000011010+Q_000202000*PR_011000011020); ans_temp[ans_id*6+3]+=Pmtrx[21]*(Q_001000001*PR_011000011000+Q_001000101*PR_011000011001+Q_101000001*PR_011000011100+Q_101000101*PR_011000011101); ans_temp[ans_id*6+3]+=Pmtrx[22]*(Q_000001001*PR_011000011000+Q_000001101*PR_011000011001+Q_000101001*PR_011000011010+Q_000101101*PR_011000011011); ans_temp[ans_id*6+3]+=Pmtrx[23]*(Q_000000002*PR_011000011000+Q_000000102*PR_011000011001+Q_000000202*PR_011000011002); ans_temp[ans_id*6+3]+=Pmtrx[24]*(Q_002000000*PR_010001011000+Q_102000000*PR_010001011100+Q_202000000*PR_010001011200); ans_temp[ans_id*6+3]+=Pmtrx[25]*(Q_001001000*PR_010001011000+Q_001101000*PR_010001011010+Q_101001000*PR_010001011100+Q_101101000*PR_010001011110); ans_temp[ans_id*6+3]+=Pmtrx[26]*(Q_000002000*PR_010001011000+Q_000102000*PR_010001011010+Q_000202000*PR_010001011020); ans_temp[ans_id*6+3]+=Pmtrx[27]*(Q_001000001*PR_010001011000+Q_001000101*PR_010001011001+Q_101000001*PR_010001011100+Q_101000101*PR_010001011101); ans_temp[ans_id*6+3]+=Pmtrx[28]*(Q_000001001*PR_010001011000+Q_000001101*PR_010001011001+Q_000101001*PR_010001011010+Q_000101101*PR_010001011011); ans_temp[ans_id*6+3]+=Pmtrx[29]*(Q_000000002*PR_010001011000+Q_000000102*PR_010001011001+Q_000000202*PR_010001011002); ans_temp[ans_id*6+3]+=Pmtrx[30]*(Q_002000000*PR_010000012000+Q_102000000*PR_010000012100+Q_202000000*PR_010000012200); ans_temp[ans_id*6+3]+=Pmtrx[31]*(Q_001001000*PR_010000012000+Q_001101000*PR_010000012010+Q_101001000*PR_010000012100+Q_101101000*PR_010000012110); ans_temp[ans_id*6+3]+=Pmtrx[32]*(Q_000002000*PR_010000012000+Q_000102000*PR_010000012010+Q_000202000*PR_010000012020); ans_temp[ans_id*6+3]+=Pmtrx[33]*(Q_001000001*PR_010000012000+Q_001000101*PR_010000012001+Q_101000001*PR_010000012100+Q_101000101*PR_010000012101); ans_temp[ans_id*6+3]+=Pmtrx[34]*(Q_000001001*PR_010000012000+Q_000001101*PR_010000012001+Q_000101001*PR_010000012010+Q_000101101*PR_010000012011); ans_temp[ans_id*6+3]+=Pmtrx[35]*(Q_000000002*PR_010000012000+Q_000000102*PR_010000012001+Q_000000202*PR_010000012002); ans_temp[ans_id*6+4]+=Pmtrx[0]*(Q_002000000*PR_002010010000+Q_102000000*PR_002010010100+Q_202000000*PR_002010010200); ans_temp[ans_id*6+4]+=Pmtrx[1]*(Q_001001000*PR_002010010000+Q_001101000*PR_002010010010+Q_101001000*PR_002010010100+Q_101101000*PR_002010010110); ans_temp[ans_id*6+4]+=Pmtrx[2]*(Q_000002000*PR_002010010000+Q_000102000*PR_002010010010+Q_000202000*PR_002010010020); ans_temp[ans_id*6+4]+=Pmtrx[3]*(Q_001000001*PR_002010010000+Q_001000101*PR_002010010001+Q_101000001*PR_002010010100+Q_101000101*PR_002010010101); ans_temp[ans_id*6+4]+=Pmtrx[4]*(Q_000001001*PR_002010010000+Q_000001101*PR_002010010001+Q_000101001*PR_002010010010+Q_000101101*PR_002010010011); ans_temp[ans_id*6+4]+=Pmtrx[5]*(Q_000000002*PR_002010010000+Q_000000102*PR_002010010001+Q_000000202*PR_002010010002); ans_temp[ans_id*6+4]+=Pmtrx[6]*(Q_002000000*PR_001011010000+Q_102000000*PR_001011010100+Q_202000000*PR_001011010200); ans_temp[ans_id*6+4]+=Pmtrx[7]*(Q_001001000*PR_001011010000+Q_001101000*PR_001011010010+Q_101001000*PR_001011010100+Q_101101000*PR_001011010110); ans_temp[ans_id*6+4]+=Pmtrx[8]*(Q_000002000*PR_001011010000+Q_000102000*PR_001011010010+Q_000202000*PR_001011010020); ans_temp[ans_id*6+4]+=Pmtrx[9]*(Q_001000001*PR_001011010000+Q_001000101*PR_001011010001+Q_101000001*PR_001011010100+Q_101000101*PR_001011010101); ans_temp[ans_id*6+4]+=Pmtrx[10]*(Q_000001001*PR_001011010000+Q_000001101*PR_001011010001+Q_000101001*PR_001011010010+Q_000101101*PR_001011010011); ans_temp[ans_id*6+4]+=Pmtrx[11]*(Q_000000002*PR_001011010000+Q_000000102*PR_001011010001+Q_000000202*PR_001011010002); ans_temp[ans_id*6+4]+=Pmtrx[12]*(Q_002000000*PR_000012010000+Q_102000000*PR_000012010100+Q_202000000*PR_000012010200); ans_temp[ans_id*6+4]+=Pmtrx[13]*(Q_001001000*PR_000012010000+Q_001101000*PR_000012010010+Q_101001000*PR_000012010100+Q_101101000*PR_000012010110); ans_temp[ans_id*6+4]+=Pmtrx[14]*(Q_000002000*PR_000012010000+Q_000102000*PR_000012010010+Q_000202000*PR_000012010020); ans_temp[ans_id*6+4]+=Pmtrx[15]*(Q_001000001*PR_000012010000+Q_001000101*PR_000012010001+Q_101000001*PR_000012010100+Q_101000101*PR_000012010101); ans_temp[ans_id*6+4]+=Pmtrx[16]*(Q_000001001*PR_000012010000+Q_000001101*PR_000012010001+Q_000101001*PR_000012010010+Q_000101101*PR_000012010011); ans_temp[ans_id*6+4]+=Pmtrx[17]*(Q_000000002*PR_000012010000+Q_000000102*PR_000012010001+Q_000000202*PR_000012010002); ans_temp[ans_id*6+4]+=Pmtrx[18]*(Q_002000000*PR_001010011000+Q_102000000*PR_001010011100+Q_202000000*PR_001010011200); ans_temp[ans_id*6+4]+=Pmtrx[19]*(Q_001001000*PR_001010011000+Q_001101000*PR_001010011010+Q_101001000*PR_001010011100+Q_101101000*PR_001010011110); ans_temp[ans_id*6+4]+=Pmtrx[20]*(Q_000002000*PR_001010011000+Q_000102000*PR_001010011010+Q_000202000*PR_001010011020); ans_temp[ans_id*6+4]+=Pmtrx[21]*(Q_001000001*PR_001010011000+Q_001000101*PR_001010011001+Q_101000001*PR_001010011100+Q_101000101*PR_001010011101); ans_temp[ans_id*6+4]+=Pmtrx[22]*(Q_000001001*PR_001010011000+Q_000001101*PR_001010011001+Q_000101001*PR_001010011010+Q_000101101*PR_001010011011); ans_temp[ans_id*6+4]+=Pmtrx[23]*(Q_000000002*PR_001010011000+Q_000000102*PR_001010011001+Q_000000202*PR_001010011002); ans_temp[ans_id*6+4]+=Pmtrx[24]*(Q_002000000*PR_000011011000+Q_102000000*PR_000011011100+Q_202000000*PR_000011011200); ans_temp[ans_id*6+4]+=Pmtrx[25]*(Q_001001000*PR_000011011000+Q_001101000*PR_000011011010+Q_101001000*PR_000011011100+Q_101101000*PR_000011011110); ans_temp[ans_id*6+4]+=Pmtrx[26]*(Q_000002000*PR_000011011000+Q_000102000*PR_000011011010+Q_000202000*PR_000011011020); ans_temp[ans_id*6+4]+=Pmtrx[27]*(Q_001000001*PR_000011011000+Q_001000101*PR_000011011001+Q_101000001*PR_000011011100+Q_101000101*PR_000011011101); ans_temp[ans_id*6+4]+=Pmtrx[28]*(Q_000001001*PR_000011011000+Q_000001101*PR_000011011001+Q_000101001*PR_000011011010+Q_000101101*PR_000011011011); ans_temp[ans_id*6+4]+=Pmtrx[29]*(Q_000000002*PR_000011011000+Q_000000102*PR_000011011001+Q_000000202*PR_000011011002); ans_temp[ans_id*6+4]+=Pmtrx[30]*(Q_002000000*PR_000010012000+Q_102000000*PR_000010012100+Q_202000000*PR_000010012200); ans_temp[ans_id*6+4]+=Pmtrx[31]*(Q_001001000*PR_000010012000+Q_001101000*PR_000010012010+Q_101001000*PR_000010012100+Q_101101000*PR_000010012110); ans_temp[ans_id*6+4]+=Pmtrx[32]*(Q_000002000*PR_000010012000+Q_000102000*PR_000010012010+Q_000202000*PR_000010012020); ans_temp[ans_id*6+4]+=Pmtrx[33]*(Q_001000001*PR_000010012000+Q_001000101*PR_000010012001+Q_101000001*PR_000010012100+Q_101000101*PR_000010012101); ans_temp[ans_id*6+4]+=Pmtrx[34]*(Q_000001001*PR_000010012000+Q_000001101*PR_000010012001+Q_000101001*PR_000010012010+Q_000101101*PR_000010012011); ans_temp[ans_id*6+4]+=Pmtrx[35]*(Q_000000002*PR_000010012000+Q_000000102*PR_000010012001+Q_000000202*PR_000010012002); ans_temp[ans_id*6+5]+=Pmtrx[0]*(Q_002000000*PR_002000020000+Q_102000000*PR_002000020100+Q_202000000*PR_002000020200); ans_temp[ans_id*6+5]+=Pmtrx[1]*(Q_001001000*PR_002000020000+Q_001101000*PR_002000020010+Q_101001000*PR_002000020100+Q_101101000*PR_002000020110); ans_temp[ans_id*6+5]+=Pmtrx[2]*(Q_000002000*PR_002000020000+Q_000102000*PR_002000020010+Q_000202000*PR_002000020020); ans_temp[ans_id*6+5]+=Pmtrx[3]*(Q_001000001*PR_002000020000+Q_001000101*PR_002000020001+Q_101000001*PR_002000020100+Q_101000101*PR_002000020101); ans_temp[ans_id*6+5]+=Pmtrx[4]*(Q_000001001*PR_002000020000+Q_000001101*PR_002000020001+Q_000101001*PR_002000020010+Q_000101101*PR_002000020011); ans_temp[ans_id*6+5]+=Pmtrx[5]*(Q_000000002*PR_002000020000+Q_000000102*PR_002000020001+Q_000000202*PR_002000020002); ans_temp[ans_id*6+5]+=Pmtrx[6]*(Q_002000000*PR_001001020000+Q_102000000*PR_001001020100+Q_202000000*PR_001001020200); ans_temp[ans_id*6+5]+=Pmtrx[7]*(Q_001001000*PR_001001020000+Q_001101000*PR_001001020010+Q_101001000*PR_001001020100+Q_101101000*PR_001001020110); ans_temp[ans_id*6+5]+=Pmtrx[8]*(Q_000002000*PR_001001020000+Q_000102000*PR_001001020010+Q_000202000*PR_001001020020); ans_temp[ans_id*6+5]+=Pmtrx[9]*(Q_001000001*PR_001001020000+Q_001000101*PR_001001020001+Q_101000001*PR_001001020100+Q_101000101*PR_001001020101); ans_temp[ans_id*6+5]+=Pmtrx[10]*(Q_000001001*PR_001001020000+Q_000001101*PR_001001020001+Q_000101001*PR_001001020010+Q_000101101*PR_001001020011); ans_temp[ans_id*6+5]+=Pmtrx[11]*(Q_000000002*PR_001001020000+Q_000000102*PR_001001020001+Q_000000202*PR_001001020002); ans_temp[ans_id*6+5]+=Pmtrx[12]*(Q_002000000*PR_000002020000+Q_102000000*PR_000002020100+Q_202000000*PR_000002020200); ans_temp[ans_id*6+5]+=Pmtrx[13]*(Q_001001000*PR_000002020000+Q_001101000*PR_000002020010+Q_101001000*PR_000002020100+Q_101101000*PR_000002020110); ans_temp[ans_id*6+5]+=Pmtrx[14]*(Q_000002000*PR_000002020000+Q_000102000*PR_000002020010+Q_000202000*PR_000002020020); ans_temp[ans_id*6+5]+=Pmtrx[15]*(Q_001000001*PR_000002020000+Q_001000101*PR_000002020001+Q_101000001*PR_000002020100+Q_101000101*PR_000002020101); ans_temp[ans_id*6+5]+=Pmtrx[16]*(Q_000001001*PR_000002020000+Q_000001101*PR_000002020001+Q_000101001*PR_000002020010+Q_000101101*PR_000002020011); ans_temp[ans_id*6+5]+=Pmtrx[17]*(Q_000000002*PR_000002020000+Q_000000102*PR_000002020001+Q_000000202*PR_000002020002); ans_temp[ans_id*6+5]+=Pmtrx[18]*(Q_002000000*PR_001000021000+Q_102000000*PR_001000021100+Q_202000000*PR_001000021200); ans_temp[ans_id*6+5]+=Pmtrx[19]*(Q_001001000*PR_001000021000+Q_001101000*PR_001000021010+Q_101001000*PR_001000021100+Q_101101000*PR_001000021110); ans_temp[ans_id*6+5]+=Pmtrx[20]*(Q_000002000*PR_001000021000+Q_000102000*PR_001000021010+Q_000202000*PR_001000021020); ans_temp[ans_id*6+5]+=Pmtrx[21]*(Q_001000001*PR_001000021000+Q_001000101*PR_001000021001+Q_101000001*PR_001000021100+Q_101000101*PR_001000021101); ans_temp[ans_id*6+5]+=Pmtrx[22]*(Q_000001001*PR_001000021000+Q_000001101*PR_001000021001+Q_000101001*PR_001000021010+Q_000101101*PR_001000021011); ans_temp[ans_id*6+5]+=Pmtrx[23]*(Q_000000002*PR_001000021000+Q_000000102*PR_001000021001+Q_000000202*PR_001000021002); ans_temp[ans_id*6+5]+=Pmtrx[24]*(Q_002000000*PR_000001021000+Q_102000000*PR_000001021100+Q_202000000*PR_000001021200); ans_temp[ans_id*6+5]+=Pmtrx[25]*(Q_001001000*PR_000001021000+Q_001101000*PR_000001021010+Q_101001000*PR_000001021100+Q_101101000*PR_000001021110); ans_temp[ans_id*6+5]+=Pmtrx[26]*(Q_000002000*PR_000001021000+Q_000102000*PR_000001021010+Q_000202000*PR_000001021020); ans_temp[ans_id*6+5]+=Pmtrx[27]*(Q_001000001*PR_000001021000+Q_001000101*PR_000001021001+Q_101000001*PR_000001021100+Q_101000101*PR_000001021101); ans_temp[ans_id*6+5]+=Pmtrx[28]*(Q_000001001*PR_000001021000+Q_000001101*PR_000001021001+Q_000101001*PR_000001021010+Q_000101101*PR_000001021011); ans_temp[ans_id*6+5]+=Pmtrx[29]*(Q_000000002*PR_000001021000+Q_000000102*PR_000001021001+Q_000000202*PR_000001021002); ans_temp[ans_id*6+5]+=Pmtrx[30]*(Q_002000000*PR_000000022000+Q_102000000*PR_000000022100+Q_202000000*PR_000000022200); ans_temp[ans_id*6+5]+=Pmtrx[31]*(Q_001001000*PR_000000022000+Q_001101000*PR_000000022010+Q_101001000*PR_000000022100+Q_101101000*PR_000000022110); ans_temp[ans_id*6+5]+=Pmtrx[32]*(Q_000002000*PR_000000022000+Q_000102000*PR_000000022010+Q_000202000*PR_000000022020); ans_temp[ans_id*6+5]+=Pmtrx[33]*(Q_001000001*PR_000000022000+Q_001000101*PR_000000022001+Q_101000001*PR_000000022100+Q_101000101*PR_000000022101); ans_temp[ans_id*6+5]+=Pmtrx[34]*(Q_000001001*PR_000000022000+Q_000001101*PR_000000022001+Q_000101001*PR_000000022010+Q_000101101*PR_000000022011); ans_temp[ans_id*6+5]+=Pmtrx[35]*(Q_000000002*PR_000000022000+Q_000000102*PR_000000022001+Q_000000202*PR_000000022002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } } __global__ void MD_Kq_ddsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_010[3]; Pd_010[0]=PA[ii*3+0]; Pd_010[1]=PA[ii*3+1]; Pd_010[2]=PA[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[7]; Ft_fs_6(6,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; R_000[6]*=64*alphaT*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[6]; double R_200[5]; double R_300[4]; double R_400[3]; double R_500[2]; double R_600[1]; double R_010[6]; double R_110[5]; double R_210[4]; double R_310[3]; double R_410[2]; double R_510[1]; double R_020[5]; double R_120[4]; double R_220[3]; double R_320[2]; double R_420[1]; double R_030[4]; double R_130[3]; double R_230[2]; double R_330[1]; double R_040[3]; double R_140[2]; double R_240[1]; double R_050[2]; double R_150[1]; double R_060[1]; double R_001[6]; double R_101[5]; double R_201[4]; double R_301[3]; double R_401[2]; double R_501[1]; double R_011[5]; double R_111[4]; double R_211[3]; double R_311[2]; double R_411[1]; double R_021[4]; double R_121[3]; double R_221[2]; double R_321[1]; double R_031[3]; double R_131[2]; double R_231[1]; double R_041[2]; double R_141[1]; double R_051[1]; double R_002[5]; double R_102[4]; double R_202[3]; double R_302[2]; double R_402[1]; double R_012[4]; double R_112[3]; double R_212[2]; double R_312[1]; double R_022[3]; double R_122[2]; double R_222[1]; double R_032[2]; double R_132[1]; double R_042[1]; double R_003[4]; double R_103[3]; double R_203[2]; double R_303[1]; double R_013[3]; double R_113[2]; double R_213[1]; double R_023[2]; double R_123[1]; double R_033[1]; double R_004[3]; double R_104[2]; double R_204[1]; double R_014[2]; double R_114[1]; double R_024[1]; double R_005[2]; double R_105[1]; double R_015[1]; double R_006[1]; for(int i=0;i<6;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<6;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<6;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<5;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<5;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<5;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<5;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<4;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<4;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<4;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<4;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<4;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<4;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<4;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<4;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<4;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<3;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<3;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<3;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<3;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<3;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<3;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<3;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<3;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<3;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<3;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<3;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<3;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<3;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<2;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<2;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<2;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<2;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<2;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<2;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<2;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<2;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<2;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<2;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<2;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<2;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<2;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<2;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<2;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<2;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<2;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<2;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<2;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<2;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<2;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } for(int i=0;i<1;i++){ R_600[i]=TX*R_500[i+1]+5*R_400[i+1]; } for(int i=0;i<1;i++){ R_510[i]=TY*R_500[i+1]; } for(int i=0;i<1;i++){ R_420[i]=TX*R_320[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_330[i]=TX*R_230[i+1]+2*R_130[i+1]; } for(int i=0;i<1;i++){ R_240[i]=TY*R_230[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_150[i]=TX*R_050[i+1]; } for(int i=0;i<1;i++){ R_060[i]=TY*R_050[i+1]+5*R_040[i+1]; } for(int i=0;i<1;i++){ R_501[i]=TZ*R_500[i+1]; } for(int i=0;i<1;i++){ R_411[i]=TY*R_401[i+1]; } for(int i=0;i<1;i++){ R_321[i]=TZ*R_320[i+1]; } for(int i=0;i<1;i++){ R_231[i]=TZ*R_230[i+1]; } for(int i=0;i<1;i++){ R_141[i]=TX*R_041[i+1]; } for(int i=0;i<1;i++){ R_051[i]=TZ*R_050[i+1]; } for(int i=0;i<1;i++){ R_402[i]=TX*R_302[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_312[i]=TY*R_302[i+1]; } for(int i=0;i<1;i++){ R_222[i]=TX*R_122[i+1]+R_022[i+1]; } for(int i=0;i<1;i++){ R_132[i]=TX*R_032[i+1]; } for(int i=0;i<1;i++){ R_042[i]=TY*R_032[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_303[i]=TX*R_203[i+1]+2*R_103[i+1]; } for(int i=0;i<1;i++){ R_213[i]=TY*R_203[i+1]; } for(int i=0;i<1;i++){ R_123[i]=TX*R_023[i+1]; } for(int i=0;i<1;i++){ R_033[i]=TY*R_023[i+1]+2*R_013[i+1]; } for(int i=0;i<1;i++){ R_204[i]=TZ*R_203[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_114[i]=TX*R_014[i+1]; } for(int i=0;i<1;i++){ R_024[i]=TZ*R_023[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_105[i]=TX*R_005[i+1]; } for(int i=0;i<1;i++){ R_015[i]=TY*R_005[i+1]; } for(int i=0;i<1;i++){ R_006[i]=TZ*R_005[i+1]+5*R_004[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double QR_002000000003=Q_002000000*R_003[0]+-1*Q_102000000*R_103[0]+Q_202000000*R_203[0]; double QR_001001000003=Q_001001000*R_003[0]+-1*Q_001101000*R_013[0]+-1*Q_101001000*R_103[0]+Q_101101000*R_113[0]; double QR_000002000003=Q_000002000*R_003[0]+-1*Q_000102000*R_013[0]+Q_000202000*R_023[0]; double QR_001000001003=Q_001000001*R_003[0]+-1*Q_001000101*R_004[0]+-1*Q_101000001*R_103[0]+Q_101000101*R_104[0]; double QR_000001001003=Q_000001001*R_003[0]+-1*Q_000001101*R_004[0]+-1*Q_000101001*R_013[0]+Q_000101101*R_014[0]; double QR_000000002003=Q_000000002*R_003[0]+-1*Q_000000102*R_004[0]+Q_000000202*R_005[0]; double QR_002000000012=Q_002000000*R_012[0]+-1*Q_102000000*R_112[0]+Q_202000000*R_212[0]; double QR_001001000012=Q_001001000*R_012[0]+-1*Q_001101000*R_022[0]+-1*Q_101001000*R_112[0]+Q_101101000*R_122[0]; double QR_000002000012=Q_000002000*R_012[0]+-1*Q_000102000*R_022[0]+Q_000202000*R_032[0]; double QR_001000001012=Q_001000001*R_012[0]+-1*Q_001000101*R_013[0]+-1*Q_101000001*R_112[0]+Q_101000101*R_113[0]; double QR_000001001012=Q_000001001*R_012[0]+-1*Q_000001101*R_013[0]+-1*Q_000101001*R_022[0]+Q_000101101*R_023[0]; double QR_000000002012=Q_000000002*R_012[0]+-1*Q_000000102*R_013[0]+Q_000000202*R_014[0]; double QR_002000000021=Q_002000000*R_021[0]+-1*Q_102000000*R_121[0]+Q_202000000*R_221[0]; double QR_001001000021=Q_001001000*R_021[0]+-1*Q_001101000*R_031[0]+-1*Q_101001000*R_121[0]+Q_101101000*R_131[0]; double QR_000002000021=Q_000002000*R_021[0]+-1*Q_000102000*R_031[0]+Q_000202000*R_041[0]; double QR_001000001021=Q_001000001*R_021[0]+-1*Q_001000101*R_022[0]+-1*Q_101000001*R_121[0]+Q_101000101*R_122[0]; double QR_000001001021=Q_000001001*R_021[0]+-1*Q_000001101*R_022[0]+-1*Q_000101001*R_031[0]+Q_000101101*R_032[0]; double QR_000000002021=Q_000000002*R_021[0]+-1*Q_000000102*R_022[0]+Q_000000202*R_023[0]; double QR_002000000030=Q_002000000*R_030[0]+-1*Q_102000000*R_130[0]+Q_202000000*R_230[0]; double QR_001001000030=Q_001001000*R_030[0]+-1*Q_001101000*R_040[0]+-1*Q_101001000*R_130[0]+Q_101101000*R_140[0]; double QR_000002000030=Q_000002000*R_030[0]+-1*Q_000102000*R_040[0]+Q_000202000*R_050[0]; double QR_001000001030=Q_001000001*R_030[0]+-1*Q_001000101*R_031[0]+-1*Q_101000001*R_130[0]+Q_101000101*R_131[0]; double QR_000001001030=Q_000001001*R_030[0]+-1*Q_000001101*R_031[0]+-1*Q_000101001*R_040[0]+Q_000101101*R_041[0]; double QR_000000002030=Q_000000002*R_030[0]+-1*Q_000000102*R_031[0]+Q_000000202*R_032[0]; double QR_002000000102=Q_002000000*R_102[0]+-1*Q_102000000*R_202[0]+Q_202000000*R_302[0]; double QR_001001000102=Q_001001000*R_102[0]+-1*Q_001101000*R_112[0]+-1*Q_101001000*R_202[0]+Q_101101000*R_212[0]; double QR_000002000102=Q_000002000*R_102[0]+-1*Q_000102000*R_112[0]+Q_000202000*R_122[0]; double QR_001000001102=Q_001000001*R_102[0]+-1*Q_001000101*R_103[0]+-1*Q_101000001*R_202[0]+Q_101000101*R_203[0]; double QR_000001001102=Q_000001001*R_102[0]+-1*Q_000001101*R_103[0]+-1*Q_000101001*R_112[0]+Q_000101101*R_113[0]; double QR_000000002102=Q_000000002*R_102[0]+-1*Q_000000102*R_103[0]+Q_000000202*R_104[0]; double QR_002000000111=Q_002000000*R_111[0]+-1*Q_102000000*R_211[0]+Q_202000000*R_311[0]; double QR_001001000111=Q_001001000*R_111[0]+-1*Q_001101000*R_121[0]+-1*Q_101001000*R_211[0]+Q_101101000*R_221[0]; double QR_000002000111=Q_000002000*R_111[0]+-1*Q_000102000*R_121[0]+Q_000202000*R_131[0]; double QR_001000001111=Q_001000001*R_111[0]+-1*Q_001000101*R_112[0]+-1*Q_101000001*R_211[0]+Q_101000101*R_212[0]; double QR_000001001111=Q_000001001*R_111[0]+-1*Q_000001101*R_112[0]+-1*Q_000101001*R_121[0]+Q_000101101*R_122[0]; double QR_000000002111=Q_000000002*R_111[0]+-1*Q_000000102*R_112[0]+Q_000000202*R_113[0]; double QR_002000000120=Q_002000000*R_120[0]+-1*Q_102000000*R_220[0]+Q_202000000*R_320[0]; double QR_001001000120=Q_001001000*R_120[0]+-1*Q_001101000*R_130[0]+-1*Q_101001000*R_220[0]+Q_101101000*R_230[0]; double QR_000002000120=Q_000002000*R_120[0]+-1*Q_000102000*R_130[0]+Q_000202000*R_140[0]; double QR_001000001120=Q_001000001*R_120[0]+-1*Q_001000101*R_121[0]+-1*Q_101000001*R_220[0]+Q_101000101*R_221[0]; double QR_000001001120=Q_000001001*R_120[0]+-1*Q_000001101*R_121[0]+-1*Q_000101001*R_130[0]+Q_000101101*R_131[0]; double QR_000000002120=Q_000000002*R_120[0]+-1*Q_000000102*R_121[0]+Q_000000202*R_122[0]; double QR_002000000201=Q_002000000*R_201[0]+-1*Q_102000000*R_301[0]+Q_202000000*R_401[0]; double QR_001001000201=Q_001001000*R_201[0]+-1*Q_001101000*R_211[0]+-1*Q_101001000*R_301[0]+Q_101101000*R_311[0]; double QR_000002000201=Q_000002000*R_201[0]+-1*Q_000102000*R_211[0]+Q_000202000*R_221[0]; double QR_001000001201=Q_001000001*R_201[0]+-1*Q_001000101*R_202[0]+-1*Q_101000001*R_301[0]+Q_101000101*R_302[0]; double QR_000001001201=Q_000001001*R_201[0]+-1*Q_000001101*R_202[0]+-1*Q_000101001*R_211[0]+Q_000101101*R_212[0]; double QR_000000002201=Q_000000002*R_201[0]+-1*Q_000000102*R_202[0]+Q_000000202*R_203[0]; double QR_002000000210=Q_002000000*R_210[0]+-1*Q_102000000*R_310[0]+Q_202000000*R_410[0]; double QR_001001000210=Q_001001000*R_210[0]+-1*Q_001101000*R_220[0]+-1*Q_101001000*R_310[0]+Q_101101000*R_320[0]; double QR_000002000210=Q_000002000*R_210[0]+-1*Q_000102000*R_220[0]+Q_000202000*R_230[0]; double QR_001000001210=Q_001000001*R_210[0]+-1*Q_001000101*R_211[0]+-1*Q_101000001*R_310[0]+Q_101000101*R_311[0]; double QR_000001001210=Q_000001001*R_210[0]+-1*Q_000001101*R_211[0]+-1*Q_000101001*R_220[0]+Q_000101101*R_221[0]; double QR_000000002210=Q_000000002*R_210[0]+-1*Q_000000102*R_211[0]+Q_000000202*R_212[0]; double QR_002000000300=Q_002000000*R_300[0]+-1*Q_102000000*R_400[0]+Q_202000000*R_500[0]; double QR_001001000300=Q_001001000*R_300[0]+-1*Q_001101000*R_310[0]+-1*Q_101001000*R_400[0]+Q_101101000*R_410[0]; double QR_000002000300=Q_000002000*R_300[0]+-1*Q_000102000*R_310[0]+Q_000202000*R_320[0]; double QR_001000001300=Q_001000001*R_300[0]+-1*Q_001000101*R_301[0]+-1*Q_101000001*R_400[0]+Q_101000101*R_401[0]; double QR_000001001300=Q_000001001*R_300[0]+-1*Q_000001101*R_301[0]+-1*Q_000101001*R_310[0]+Q_000101101*R_311[0]; double QR_000000002300=Q_000000002*R_300[0]+-1*Q_000000102*R_301[0]+Q_000000202*R_302[0]; double QR_002000000004=Q_002000000*R_004[0]+-1*Q_102000000*R_104[0]+Q_202000000*R_204[0]; double QR_001001000004=Q_001001000*R_004[0]+-1*Q_001101000*R_014[0]+-1*Q_101001000*R_104[0]+Q_101101000*R_114[0]; double QR_000002000004=Q_000002000*R_004[0]+-1*Q_000102000*R_014[0]+Q_000202000*R_024[0]; double QR_001000001004=Q_001000001*R_004[0]+-1*Q_001000101*R_005[0]+-1*Q_101000001*R_104[0]+Q_101000101*R_105[0]; double QR_000001001004=Q_000001001*R_004[0]+-1*Q_000001101*R_005[0]+-1*Q_000101001*R_014[0]+Q_000101101*R_015[0]; double QR_000000002004=Q_000000002*R_004[0]+-1*Q_000000102*R_005[0]+Q_000000202*R_006[0]; double QR_002000000013=Q_002000000*R_013[0]+-1*Q_102000000*R_113[0]+Q_202000000*R_213[0]; double QR_001001000013=Q_001001000*R_013[0]+-1*Q_001101000*R_023[0]+-1*Q_101001000*R_113[0]+Q_101101000*R_123[0]; double QR_000002000013=Q_000002000*R_013[0]+-1*Q_000102000*R_023[0]+Q_000202000*R_033[0]; double QR_001000001013=Q_001000001*R_013[0]+-1*Q_001000101*R_014[0]+-1*Q_101000001*R_113[0]+Q_101000101*R_114[0]; double QR_000001001013=Q_000001001*R_013[0]+-1*Q_000001101*R_014[0]+-1*Q_000101001*R_023[0]+Q_000101101*R_024[0]; double QR_000000002013=Q_000000002*R_013[0]+-1*Q_000000102*R_014[0]+Q_000000202*R_015[0]; double QR_002000000022=Q_002000000*R_022[0]+-1*Q_102000000*R_122[0]+Q_202000000*R_222[0]; double QR_001001000022=Q_001001000*R_022[0]+-1*Q_001101000*R_032[0]+-1*Q_101001000*R_122[0]+Q_101101000*R_132[0]; double QR_000002000022=Q_000002000*R_022[0]+-1*Q_000102000*R_032[0]+Q_000202000*R_042[0]; double QR_001000001022=Q_001000001*R_022[0]+-1*Q_001000101*R_023[0]+-1*Q_101000001*R_122[0]+Q_101000101*R_123[0]; double QR_000001001022=Q_000001001*R_022[0]+-1*Q_000001101*R_023[0]+-1*Q_000101001*R_032[0]+Q_000101101*R_033[0]; double QR_000000002022=Q_000000002*R_022[0]+-1*Q_000000102*R_023[0]+Q_000000202*R_024[0]; double QR_002000000031=Q_002000000*R_031[0]+-1*Q_102000000*R_131[0]+Q_202000000*R_231[0]; double QR_001001000031=Q_001001000*R_031[0]+-1*Q_001101000*R_041[0]+-1*Q_101001000*R_131[0]+Q_101101000*R_141[0]; double QR_000002000031=Q_000002000*R_031[0]+-1*Q_000102000*R_041[0]+Q_000202000*R_051[0]; double QR_001000001031=Q_001000001*R_031[0]+-1*Q_001000101*R_032[0]+-1*Q_101000001*R_131[0]+Q_101000101*R_132[0]; double QR_000001001031=Q_000001001*R_031[0]+-1*Q_000001101*R_032[0]+-1*Q_000101001*R_041[0]+Q_000101101*R_042[0]; double QR_000000002031=Q_000000002*R_031[0]+-1*Q_000000102*R_032[0]+Q_000000202*R_033[0]; double QR_002000000040=Q_002000000*R_040[0]+-1*Q_102000000*R_140[0]+Q_202000000*R_240[0]; double QR_001001000040=Q_001001000*R_040[0]+-1*Q_001101000*R_050[0]+-1*Q_101001000*R_140[0]+Q_101101000*R_150[0]; double QR_000002000040=Q_000002000*R_040[0]+-1*Q_000102000*R_050[0]+Q_000202000*R_060[0]; double QR_001000001040=Q_001000001*R_040[0]+-1*Q_001000101*R_041[0]+-1*Q_101000001*R_140[0]+Q_101000101*R_141[0]; double QR_000001001040=Q_000001001*R_040[0]+-1*Q_000001101*R_041[0]+-1*Q_000101001*R_050[0]+Q_000101101*R_051[0]; double QR_000000002040=Q_000000002*R_040[0]+-1*Q_000000102*R_041[0]+Q_000000202*R_042[0]; double QR_002000000103=Q_002000000*R_103[0]+-1*Q_102000000*R_203[0]+Q_202000000*R_303[0]; double QR_001001000103=Q_001001000*R_103[0]+-1*Q_001101000*R_113[0]+-1*Q_101001000*R_203[0]+Q_101101000*R_213[0]; double QR_000002000103=Q_000002000*R_103[0]+-1*Q_000102000*R_113[0]+Q_000202000*R_123[0]; double QR_001000001103=Q_001000001*R_103[0]+-1*Q_001000101*R_104[0]+-1*Q_101000001*R_203[0]+Q_101000101*R_204[0]; double QR_000001001103=Q_000001001*R_103[0]+-1*Q_000001101*R_104[0]+-1*Q_000101001*R_113[0]+Q_000101101*R_114[0]; double QR_000000002103=Q_000000002*R_103[0]+-1*Q_000000102*R_104[0]+Q_000000202*R_105[0]; double QR_002000000112=Q_002000000*R_112[0]+-1*Q_102000000*R_212[0]+Q_202000000*R_312[0]; double QR_001001000112=Q_001001000*R_112[0]+-1*Q_001101000*R_122[0]+-1*Q_101001000*R_212[0]+Q_101101000*R_222[0]; double QR_000002000112=Q_000002000*R_112[0]+-1*Q_000102000*R_122[0]+Q_000202000*R_132[0]; double QR_001000001112=Q_001000001*R_112[0]+-1*Q_001000101*R_113[0]+-1*Q_101000001*R_212[0]+Q_101000101*R_213[0]; double QR_000001001112=Q_000001001*R_112[0]+-1*Q_000001101*R_113[0]+-1*Q_000101001*R_122[0]+Q_000101101*R_123[0]; double QR_000000002112=Q_000000002*R_112[0]+-1*Q_000000102*R_113[0]+Q_000000202*R_114[0]; double QR_002000000121=Q_002000000*R_121[0]+-1*Q_102000000*R_221[0]+Q_202000000*R_321[0]; double QR_001001000121=Q_001001000*R_121[0]+-1*Q_001101000*R_131[0]+-1*Q_101001000*R_221[0]+Q_101101000*R_231[0]; double QR_000002000121=Q_000002000*R_121[0]+-1*Q_000102000*R_131[0]+Q_000202000*R_141[0]; double QR_001000001121=Q_001000001*R_121[0]+-1*Q_001000101*R_122[0]+-1*Q_101000001*R_221[0]+Q_101000101*R_222[0]; double QR_000001001121=Q_000001001*R_121[0]+-1*Q_000001101*R_122[0]+-1*Q_000101001*R_131[0]+Q_000101101*R_132[0]; double QR_000000002121=Q_000000002*R_121[0]+-1*Q_000000102*R_122[0]+Q_000000202*R_123[0]; double QR_002000000130=Q_002000000*R_130[0]+-1*Q_102000000*R_230[0]+Q_202000000*R_330[0]; double QR_001001000130=Q_001001000*R_130[0]+-1*Q_001101000*R_140[0]+-1*Q_101001000*R_230[0]+Q_101101000*R_240[0]; double QR_000002000130=Q_000002000*R_130[0]+-1*Q_000102000*R_140[0]+Q_000202000*R_150[0]; double QR_001000001130=Q_001000001*R_130[0]+-1*Q_001000101*R_131[0]+-1*Q_101000001*R_230[0]+Q_101000101*R_231[0]; double QR_000001001130=Q_000001001*R_130[0]+-1*Q_000001101*R_131[0]+-1*Q_000101001*R_140[0]+Q_000101101*R_141[0]; double QR_000000002130=Q_000000002*R_130[0]+-1*Q_000000102*R_131[0]+Q_000000202*R_132[0]; double QR_002000000202=Q_002000000*R_202[0]+-1*Q_102000000*R_302[0]+Q_202000000*R_402[0]; double QR_001001000202=Q_001001000*R_202[0]+-1*Q_001101000*R_212[0]+-1*Q_101001000*R_302[0]+Q_101101000*R_312[0]; double QR_000002000202=Q_000002000*R_202[0]+-1*Q_000102000*R_212[0]+Q_000202000*R_222[0]; double QR_001000001202=Q_001000001*R_202[0]+-1*Q_001000101*R_203[0]+-1*Q_101000001*R_302[0]+Q_101000101*R_303[0]; double QR_000001001202=Q_000001001*R_202[0]+-1*Q_000001101*R_203[0]+-1*Q_000101001*R_212[0]+Q_000101101*R_213[0]; double QR_000000002202=Q_000000002*R_202[0]+-1*Q_000000102*R_203[0]+Q_000000202*R_204[0]; double QR_002000000211=Q_002000000*R_211[0]+-1*Q_102000000*R_311[0]+Q_202000000*R_411[0]; double QR_001001000211=Q_001001000*R_211[0]+-1*Q_001101000*R_221[0]+-1*Q_101001000*R_311[0]+Q_101101000*R_321[0]; double QR_000002000211=Q_000002000*R_211[0]+-1*Q_000102000*R_221[0]+Q_000202000*R_231[0]; double QR_001000001211=Q_001000001*R_211[0]+-1*Q_001000101*R_212[0]+-1*Q_101000001*R_311[0]+Q_101000101*R_312[0]; double QR_000001001211=Q_000001001*R_211[0]+-1*Q_000001101*R_212[0]+-1*Q_000101001*R_221[0]+Q_000101101*R_222[0]; double QR_000000002211=Q_000000002*R_211[0]+-1*Q_000000102*R_212[0]+Q_000000202*R_213[0]; double QR_002000000220=Q_002000000*R_220[0]+-1*Q_102000000*R_320[0]+Q_202000000*R_420[0]; double QR_001001000220=Q_001001000*R_220[0]+-1*Q_001101000*R_230[0]+-1*Q_101001000*R_320[0]+Q_101101000*R_330[0]; double QR_000002000220=Q_000002000*R_220[0]+-1*Q_000102000*R_230[0]+Q_000202000*R_240[0]; double QR_001000001220=Q_001000001*R_220[0]+-1*Q_001000101*R_221[0]+-1*Q_101000001*R_320[0]+Q_101000101*R_321[0]; double QR_000001001220=Q_000001001*R_220[0]+-1*Q_000001101*R_221[0]+-1*Q_000101001*R_230[0]+Q_000101101*R_231[0]; double QR_000000002220=Q_000000002*R_220[0]+-1*Q_000000102*R_221[0]+Q_000000202*R_222[0]; double QR_002000000301=Q_002000000*R_301[0]+-1*Q_102000000*R_401[0]+Q_202000000*R_501[0]; double QR_001001000301=Q_001001000*R_301[0]+-1*Q_001101000*R_311[0]+-1*Q_101001000*R_401[0]+Q_101101000*R_411[0]; double QR_000002000301=Q_000002000*R_301[0]+-1*Q_000102000*R_311[0]+Q_000202000*R_321[0]; double QR_001000001301=Q_001000001*R_301[0]+-1*Q_001000101*R_302[0]+-1*Q_101000001*R_401[0]+Q_101000101*R_402[0]; double QR_000001001301=Q_000001001*R_301[0]+-1*Q_000001101*R_302[0]+-1*Q_000101001*R_311[0]+Q_000101101*R_312[0]; double QR_000000002301=Q_000000002*R_301[0]+-1*Q_000000102*R_302[0]+Q_000000202*R_303[0]; double QR_002000000310=Q_002000000*R_310[0]+-1*Q_102000000*R_410[0]+Q_202000000*R_510[0]; double QR_001001000310=Q_001001000*R_310[0]+-1*Q_001101000*R_320[0]+-1*Q_101001000*R_410[0]+Q_101101000*R_420[0]; double QR_000002000310=Q_000002000*R_310[0]+-1*Q_000102000*R_320[0]+Q_000202000*R_330[0]; double QR_001000001310=Q_001000001*R_310[0]+-1*Q_001000101*R_311[0]+-1*Q_101000001*R_410[0]+Q_101000101*R_411[0]; double QR_000001001310=Q_000001001*R_310[0]+-1*Q_000001101*R_311[0]+-1*Q_000101001*R_320[0]+Q_000101101*R_321[0]; double QR_000000002310=Q_000000002*R_310[0]+-1*Q_000000102*R_311[0]+Q_000000202*R_312[0]; double QR_002000000400=Q_002000000*R_400[0]+-1*Q_102000000*R_500[0]+Q_202000000*R_600[0]; double QR_001001000400=Q_001001000*R_400[0]+-1*Q_001101000*R_410[0]+-1*Q_101001000*R_500[0]+Q_101101000*R_510[0]; double QR_000002000400=Q_000002000*R_400[0]+-1*Q_000102000*R_410[0]+Q_000202000*R_420[0]; double QR_001000001400=Q_001000001*R_400[0]+-1*Q_001000101*R_401[0]+-1*Q_101000001*R_500[0]+Q_101000101*R_501[0]; double QR_000001001400=Q_000001001*R_400[0]+-1*Q_000001101*R_401[0]+-1*Q_000101001*R_410[0]+Q_000101101*R_411[0]; double QR_000000002400=Q_000000002*R_400[0]+-1*Q_000000102*R_401[0]+Q_000000202*R_402[0]; double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; double Pd_022[3]; double Pd_122[3]; double Pd_222[3]; double Pd_322[3]; double Pd_422[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_022[i]=Pd_112[i]+Pd_010[i]*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_122[i]=2*Pd_212[i]+Pd_010[i]*Pd_112[i]+aPin1*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_222[i]=3*Pd_312[i]+Pd_010[i]*Pd_212[i]+aPin1*Pd_112[i]; } for(int i=0;i<3;i++){ Pd_322[i]=Pd_010[i]*Pd_312[i]+aPin1*Pd_212[i]; } for(int i=0;i<3;i++){ Pd_422[i]=aPin1*Pd_312[i]; } double P_022000000=Pd_022[0]; double P_122000000=Pd_122[0]; double P_222000000=Pd_222[0]; double P_322000000=Pd_322[0]; double P_422000000=Pd_422[0]; double P_021001000=Pd_021[0]*Pd_001[1]; double P_021101000=Pd_021[0]*Pd_101[1]; double P_121001000=Pd_121[0]*Pd_001[1]; double P_121101000=Pd_121[0]*Pd_101[1]; double P_221001000=Pd_221[0]*Pd_001[1]; double P_221101000=Pd_221[0]*Pd_101[1]; double P_321001000=Pd_321[0]*Pd_001[1]; double P_321101000=Pd_321[0]*Pd_101[1]; double P_020002000=Pd_020[0]*Pd_002[1]; double P_020102000=Pd_020[0]*Pd_102[1]; double P_020202000=Pd_020[0]*Pd_202[1]; double P_120002000=Pd_120[0]*Pd_002[1]; double P_120102000=Pd_120[0]*Pd_102[1]; double P_120202000=Pd_120[0]*Pd_202[1]; double P_220002000=Pd_220[0]*Pd_002[1]; double P_220102000=Pd_220[0]*Pd_102[1]; double P_220202000=Pd_220[0]*Pd_202[1]; double P_021000001=Pd_021[0]*Pd_001[2]; double P_021000101=Pd_021[0]*Pd_101[2]; double P_121000001=Pd_121[0]*Pd_001[2]; double P_121000101=Pd_121[0]*Pd_101[2]; double P_221000001=Pd_221[0]*Pd_001[2]; double P_221000101=Pd_221[0]*Pd_101[2]; double P_321000001=Pd_321[0]*Pd_001[2]; double P_321000101=Pd_321[0]*Pd_101[2]; double P_020001001=Pd_020[0]*Pd_001[1]*Pd_001[2]; double P_020001101=Pd_020[0]*Pd_001[1]*Pd_101[2]; double P_020101001=Pd_020[0]*Pd_101[1]*Pd_001[2]; double P_020101101=Pd_020[0]*Pd_101[1]*Pd_101[2]; double P_120001001=Pd_120[0]*Pd_001[1]*Pd_001[2]; double P_120001101=Pd_120[0]*Pd_001[1]*Pd_101[2]; double P_120101001=Pd_120[0]*Pd_101[1]*Pd_001[2]; double P_120101101=Pd_120[0]*Pd_101[1]*Pd_101[2]; double P_220001001=Pd_220[0]*Pd_001[1]*Pd_001[2]; double P_220001101=Pd_220[0]*Pd_001[1]*Pd_101[2]; double P_220101001=Pd_220[0]*Pd_101[1]*Pd_001[2]; double P_220101101=Pd_220[0]*Pd_101[1]*Pd_101[2]; double P_020000002=Pd_020[0]*Pd_002[2]; double P_020000102=Pd_020[0]*Pd_102[2]; double P_020000202=Pd_020[0]*Pd_202[2]; double P_120000002=Pd_120[0]*Pd_002[2]; double P_120000102=Pd_120[0]*Pd_102[2]; double P_120000202=Pd_120[0]*Pd_202[2]; double P_220000002=Pd_220[0]*Pd_002[2]; double P_220000102=Pd_220[0]*Pd_102[2]; double P_220000202=Pd_220[0]*Pd_202[2]; double P_012010000=Pd_012[0]*Pd_010[1]; double P_012110000=Pd_012[0]*Pd_110[1]; double P_112010000=Pd_112[0]*Pd_010[1]; double P_112110000=Pd_112[0]*Pd_110[1]; double P_212010000=Pd_212[0]*Pd_010[1]; double P_212110000=Pd_212[0]*Pd_110[1]; double P_312010000=Pd_312[0]*Pd_010[1]; double P_312110000=Pd_312[0]*Pd_110[1]; double P_011011000=Pd_011[0]*Pd_011[1]; double P_011111000=Pd_011[0]*Pd_111[1]; double P_011211000=Pd_011[0]*Pd_211[1]; double P_111011000=Pd_111[0]*Pd_011[1]; double P_111111000=Pd_111[0]*Pd_111[1]; double P_111211000=Pd_111[0]*Pd_211[1]; double P_211011000=Pd_211[0]*Pd_011[1]; double P_211111000=Pd_211[0]*Pd_111[1]; double P_211211000=Pd_211[0]*Pd_211[1]; double P_010012000=Pd_010[0]*Pd_012[1]; double P_010112000=Pd_010[0]*Pd_112[1]; double P_010212000=Pd_010[0]*Pd_212[1]; double P_010312000=Pd_010[0]*Pd_312[1]; double P_110012000=Pd_110[0]*Pd_012[1]; double P_110112000=Pd_110[0]*Pd_112[1]; double P_110212000=Pd_110[0]*Pd_212[1]; double P_110312000=Pd_110[0]*Pd_312[1]; double P_011010001=Pd_011[0]*Pd_010[1]*Pd_001[2]; double P_011010101=Pd_011[0]*Pd_010[1]*Pd_101[2]; double P_011110001=Pd_011[0]*Pd_110[1]*Pd_001[2]; double P_011110101=Pd_011[0]*Pd_110[1]*Pd_101[2]; double P_111010001=Pd_111[0]*Pd_010[1]*Pd_001[2]; double P_111010101=Pd_111[0]*Pd_010[1]*Pd_101[2]; double P_111110001=Pd_111[0]*Pd_110[1]*Pd_001[2]; double P_111110101=Pd_111[0]*Pd_110[1]*Pd_101[2]; double P_211010001=Pd_211[0]*Pd_010[1]*Pd_001[2]; double P_211010101=Pd_211[0]*Pd_010[1]*Pd_101[2]; double P_211110001=Pd_211[0]*Pd_110[1]*Pd_001[2]; double P_211110101=Pd_211[0]*Pd_110[1]*Pd_101[2]; double P_010011001=Pd_010[0]*Pd_011[1]*Pd_001[2]; double P_010011101=Pd_010[0]*Pd_011[1]*Pd_101[2]; double P_010111001=Pd_010[0]*Pd_111[1]*Pd_001[2]; double P_010111101=Pd_010[0]*Pd_111[1]*Pd_101[2]; double P_010211001=Pd_010[0]*Pd_211[1]*Pd_001[2]; double P_010211101=Pd_010[0]*Pd_211[1]*Pd_101[2]; double P_110011001=Pd_110[0]*Pd_011[1]*Pd_001[2]; double P_110011101=Pd_110[0]*Pd_011[1]*Pd_101[2]; double P_110111001=Pd_110[0]*Pd_111[1]*Pd_001[2]; double P_110111101=Pd_110[0]*Pd_111[1]*Pd_101[2]; double P_110211001=Pd_110[0]*Pd_211[1]*Pd_001[2]; double P_110211101=Pd_110[0]*Pd_211[1]*Pd_101[2]; double P_010010002=Pd_010[0]*Pd_010[1]*Pd_002[2]; double P_010010102=Pd_010[0]*Pd_010[1]*Pd_102[2]; double P_010010202=Pd_010[0]*Pd_010[1]*Pd_202[2]; double P_010110002=Pd_010[0]*Pd_110[1]*Pd_002[2]; double P_010110102=Pd_010[0]*Pd_110[1]*Pd_102[2]; double P_010110202=Pd_010[0]*Pd_110[1]*Pd_202[2]; double P_110010002=Pd_110[0]*Pd_010[1]*Pd_002[2]; double P_110010102=Pd_110[0]*Pd_010[1]*Pd_102[2]; double P_110010202=Pd_110[0]*Pd_010[1]*Pd_202[2]; double P_110110002=Pd_110[0]*Pd_110[1]*Pd_002[2]; double P_110110102=Pd_110[0]*Pd_110[1]*Pd_102[2]; double P_110110202=Pd_110[0]*Pd_110[1]*Pd_202[2]; double P_002020000=Pd_002[0]*Pd_020[1]; double P_002120000=Pd_002[0]*Pd_120[1]; double P_002220000=Pd_002[0]*Pd_220[1]; double P_102020000=Pd_102[0]*Pd_020[1]; double P_102120000=Pd_102[0]*Pd_120[1]; double P_102220000=Pd_102[0]*Pd_220[1]; double P_202020000=Pd_202[0]*Pd_020[1]; double P_202120000=Pd_202[0]*Pd_120[1]; double P_202220000=Pd_202[0]*Pd_220[1]; double P_001021000=Pd_001[0]*Pd_021[1]; double P_001121000=Pd_001[0]*Pd_121[1]; double P_001221000=Pd_001[0]*Pd_221[1]; double P_001321000=Pd_001[0]*Pd_321[1]; double P_101021000=Pd_101[0]*Pd_021[1]; double P_101121000=Pd_101[0]*Pd_121[1]; double P_101221000=Pd_101[0]*Pd_221[1]; double P_101321000=Pd_101[0]*Pd_321[1]; double P_000022000=Pd_022[1]; double P_000122000=Pd_122[1]; double P_000222000=Pd_222[1]; double P_000322000=Pd_322[1]; double P_000422000=Pd_422[1]; double P_001020001=Pd_001[0]*Pd_020[1]*Pd_001[2]; double P_001020101=Pd_001[0]*Pd_020[1]*Pd_101[2]; double P_001120001=Pd_001[0]*Pd_120[1]*Pd_001[2]; double P_001120101=Pd_001[0]*Pd_120[1]*Pd_101[2]; double P_001220001=Pd_001[0]*Pd_220[1]*Pd_001[2]; double P_001220101=Pd_001[0]*Pd_220[1]*Pd_101[2]; double P_101020001=Pd_101[0]*Pd_020[1]*Pd_001[2]; double P_101020101=Pd_101[0]*Pd_020[1]*Pd_101[2]; double P_101120001=Pd_101[0]*Pd_120[1]*Pd_001[2]; double P_101120101=Pd_101[0]*Pd_120[1]*Pd_101[2]; double P_101220001=Pd_101[0]*Pd_220[1]*Pd_001[2]; double P_101220101=Pd_101[0]*Pd_220[1]*Pd_101[2]; double P_000021001=Pd_021[1]*Pd_001[2]; double P_000021101=Pd_021[1]*Pd_101[2]; double P_000121001=Pd_121[1]*Pd_001[2]; double P_000121101=Pd_121[1]*Pd_101[2]; double P_000221001=Pd_221[1]*Pd_001[2]; double P_000221101=Pd_221[1]*Pd_101[2]; double P_000321001=Pd_321[1]*Pd_001[2]; double P_000321101=Pd_321[1]*Pd_101[2]; double P_000020002=Pd_020[1]*Pd_002[2]; double P_000020102=Pd_020[1]*Pd_102[2]; double P_000020202=Pd_020[1]*Pd_202[2]; double P_000120002=Pd_120[1]*Pd_002[2]; double P_000120102=Pd_120[1]*Pd_102[2]; double P_000120202=Pd_120[1]*Pd_202[2]; double P_000220002=Pd_220[1]*Pd_002[2]; double P_000220102=Pd_220[1]*Pd_102[2]; double P_000220202=Pd_220[1]*Pd_202[2]; double P_012000010=Pd_012[0]*Pd_010[2]; double P_012000110=Pd_012[0]*Pd_110[2]; double P_112000010=Pd_112[0]*Pd_010[2]; double P_112000110=Pd_112[0]*Pd_110[2]; double P_212000010=Pd_212[0]*Pd_010[2]; double P_212000110=Pd_212[0]*Pd_110[2]; double P_312000010=Pd_312[0]*Pd_010[2]; double P_312000110=Pd_312[0]*Pd_110[2]; double P_011001010=Pd_011[0]*Pd_001[1]*Pd_010[2]; double P_011001110=Pd_011[0]*Pd_001[1]*Pd_110[2]; double P_011101010=Pd_011[0]*Pd_101[1]*Pd_010[2]; double P_011101110=Pd_011[0]*Pd_101[1]*Pd_110[2]; double P_111001010=Pd_111[0]*Pd_001[1]*Pd_010[2]; double P_111001110=Pd_111[0]*Pd_001[1]*Pd_110[2]; double P_111101010=Pd_111[0]*Pd_101[1]*Pd_010[2]; double P_111101110=Pd_111[0]*Pd_101[1]*Pd_110[2]; double P_211001010=Pd_211[0]*Pd_001[1]*Pd_010[2]; double P_211001110=Pd_211[0]*Pd_001[1]*Pd_110[2]; double P_211101010=Pd_211[0]*Pd_101[1]*Pd_010[2]; double P_211101110=Pd_211[0]*Pd_101[1]*Pd_110[2]; double P_010002010=Pd_010[0]*Pd_002[1]*Pd_010[2]; double P_010002110=Pd_010[0]*Pd_002[1]*Pd_110[2]; double P_010102010=Pd_010[0]*Pd_102[1]*Pd_010[2]; double P_010102110=Pd_010[0]*Pd_102[1]*Pd_110[2]; double P_010202010=Pd_010[0]*Pd_202[1]*Pd_010[2]; double P_010202110=Pd_010[0]*Pd_202[1]*Pd_110[2]; double P_110002010=Pd_110[0]*Pd_002[1]*Pd_010[2]; double P_110002110=Pd_110[0]*Pd_002[1]*Pd_110[2]; double P_110102010=Pd_110[0]*Pd_102[1]*Pd_010[2]; double P_110102110=Pd_110[0]*Pd_102[1]*Pd_110[2]; double P_110202010=Pd_110[0]*Pd_202[1]*Pd_010[2]; double P_110202110=Pd_110[0]*Pd_202[1]*Pd_110[2]; double P_011000011=Pd_011[0]*Pd_011[2]; double P_011000111=Pd_011[0]*Pd_111[2]; double P_011000211=Pd_011[0]*Pd_211[2]; double P_111000011=Pd_111[0]*Pd_011[2]; double P_111000111=Pd_111[0]*Pd_111[2]; double P_111000211=Pd_111[0]*Pd_211[2]; double P_211000011=Pd_211[0]*Pd_011[2]; double P_211000111=Pd_211[0]*Pd_111[2]; double P_211000211=Pd_211[0]*Pd_211[2]; double P_010001011=Pd_010[0]*Pd_001[1]*Pd_011[2]; double P_010001111=Pd_010[0]*Pd_001[1]*Pd_111[2]; double P_010001211=Pd_010[0]*Pd_001[1]*Pd_211[2]; double P_010101011=Pd_010[0]*Pd_101[1]*Pd_011[2]; double P_010101111=Pd_010[0]*Pd_101[1]*Pd_111[2]; double P_010101211=Pd_010[0]*Pd_101[1]*Pd_211[2]; double P_110001011=Pd_110[0]*Pd_001[1]*Pd_011[2]; double P_110001111=Pd_110[0]*Pd_001[1]*Pd_111[2]; double P_110001211=Pd_110[0]*Pd_001[1]*Pd_211[2]; double P_110101011=Pd_110[0]*Pd_101[1]*Pd_011[2]; double P_110101111=Pd_110[0]*Pd_101[1]*Pd_111[2]; double P_110101211=Pd_110[0]*Pd_101[1]*Pd_211[2]; double P_010000012=Pd_010[0]*Pd_012[2]; double P_010000112=Pd_010[0]*Pd_112[2]; double P_010000212=Pd_010[0]*Pd_212[2]; double P_010000312=Pd_010[0]*Pd_312[2]; double P_110000012=Pd_110[0]*Pd_012[2]; double P_110000112=Pd_110[0]*Pd_112[2]; double P_110000212=Pd_110[0]*Pd_212[2]; double P_110000312=Pd_110[0]*Pd_312[2]; double P_002010010=Pd_002[0]*Pd_010[1]*Pd_010[2]; double P_002010110=Pd_002[0]*Pd_010[1]*Pd_110[2]; double P_002110010=Pd_002[0]*Pd_110[1]*Pd_010[2]; double P_002110110=Pd_002[0]*Pd_110[1]*Pd_110[2]; double P_102010010=Pd_102[0]*Pd_010[1]*Pd_010[2]; double P_102010110=Pd_102[0]*Pd_010[1]*Pd_110[2]; double P_102110010=Pd_102[0]*Pd_110[1]*Pd_010[2]; double P_102110110=Pd_102[0]*Pd_110[1]*Pd_110[2]; double P_202010010=Pd_202[0]*Pd_010[1]*Pd_010[2]; double P_202010110=Pd_202[0]*Pd_010[1]*Pd_110[2]; double P_202110010=Pd_202[0]*Pd_110[1]*Pd_010[2]; double P_202110110=Pd_202[0]*Pd_110[1]*Pd_110[2]; double P_001011010=Pd_001[0]*Pd_011[1]*Pd_010[2]; double P_001011110=Pd_001[0]*Pd_011[1]*Pd_110[2]; double P_001111010=Pd_001[0]*Pd_111[1]*Pd_010[2]; double P_001111110=Pd_001[0]*Pd_111[1]*Pd_110[2]; double P_001211010=Pd_001[0]*Pd_211[1]*Pd_010[2]; double P_001211110=Pd_001[0]*Pd_211[1]*Pd_110[2]; double P_101011010=Pd_101[0]*Pd_011[1]*Pd_010[2]; double P_101011110=Pd_101[0]*Pd_011[1]*Pd_110[2]; double P_101111010=Pd_101[0]*Pd_111[1]*Pd_010[2]; double P_101111110=Pd_101[0]*Pd_111[1]*Pd_110[2]; double P_101211010=Pd_101[0]*Pd_211[1]*Pd_010[2]; double P_101211110=Pd_101[0]*Pd_211[1]*Pd_110[2]; double P_000012010=Pd_012[1]*Pd_010[2]; double P_000012110=Pd_012[1]*Pd_110[2]; double P_000112010=Pd_112[1]*Pd_010[2]; double P_000112110=Pd_112[1]*Pd_110[2]; double P_000212010=Pd_212[1]*Pd_010[2]; double P_000212110=Pd_212[1]*Pd_110[2]; double P_000312010=Pd_312[1]*Pd_010[2]; double P_000312110=Pd_312[1]*Pd_110[2]; double P_001010011=Pd_001[0]*Pd_010[1]*Pd_011[2]; double P_001010111=Pd_001[0]*Pd_010[1]*Pd_111[2]; double P_001010211=Pd_001[0]*Pd_010[1]*Pd_211[2]; double P_001110011=Pd_001[0]*Pd_110[1]*Pd_011[2]; double P_001110111=Pd_001[0]*Pd_110[1]*Pd_111[2]; double P_001110211=Pd_001[0]*Pd_110[1]*Pd_211[2]; double P_101010011=Pd_101[0]*Pd_010[1]*Pd_011[2]; double P_101010111=Pd_101[0]*Pd_010[1]*Pd_111[2]; double P_101010211=Pd_101[0]*Pd_010[1]*Pd_211[2]; double P_101110011=Pd_101[0]*Pd_110[1]*Pd_011[2]; double P_101110111=Pd_101[0]*Pd_110[1]*Pd_111[2]; double P_101110211=Pd_101[0]*Pd_110[1]*Pd_211[2]; double P_000011011=Pd_011[1]*Pd_011[2]; double P_000011111=Pd_011[1]*Pd_111[2]; double P_000011211=Pd_011[1]*Pd_211[2]; double P_000111011=Pd_111[1]*Pd_011[2]; double P_000111111=Pd_111[1]*Pd_111[2]; double P_000111211=Pd_111[1]*Pd_211[2]; double P_000211011=Pd_211[1]*Pd_011[2]; double P_000211111=Pd_211[1]*Pd_111[2]; double P_000211211=Pd_211[1]*Pd_211[2]; double P_000010012=Pd_010[1]*Pd_012[2]; double P_000010112=Pd_010[1]*Pd_112[2]; double P_000010212=Pd_010[1]*Pd_212[2]; double P_000010312=Pd_010[1]*Pd_312[2]; double P_000110012=Pd_110[1]*Pd_012[2]; double P_000110112=Pd_110[1]*Pd_112[2]; double P_000110212=Pd_110[1]*Pd_212[2]; double P_000110312=Pd_110[1]*Pd_312[2]; double P_002000020=Pd_002[0]*Pd_020[2]; double P_002000120=Pd_002[0]*Pd_120[2]; double P_002000220=Pd_002[0]*Pd_220[2]; double P_102000020=Pd_102[0]*Pd_020[2]; double P_102000120=Pd_102[0]*Pd_120[2]; double P_102000220=Pd_102[0]*Pd_220[2]; double P_202000020=Pd_202[0]*Pd_020[2]; double P_202000120=Pd_202[0]*Pd_120[2]; double P_202000220=Pd_202[0]*Pd_220[2]; double P_001001020=Pd_001[0]*Pd_001[1]*Pd_020[2]; double P_001001120=Pd_001[0]*Pd_001[1]*Pd_120[2]; double P_001001220=Pd_001[0]*Pd_001[1]*Pd_220[2]; double P_001101020=Pd_001[0]*Pd_101[1]*Pd_020[2]; double P_001101120=Pd_001[0]*Pd_101[1]*Pd_120[2]; double P_001101220=Pd_001[0]*Pd_101[1]*Pd_220[2]; double P_101001020=Pd_101[0]*Pd_001[1]*Pd_020[2]; double P_101001120=Pd_101[0]*Pd_001[1]*Pd_120[2]; double P_101001220=Pd_101[0]*Pd_001[1]*Pd_220[2]; double P_101101020=Pd_101[0]*Pd_101[1]*Pd_020[2]; double P_101101120=Pd_101[0]*Pd_101[1]*Pd_120[2]; double P_101101220=Pd_101[0]*Pd_101[1]*Pd_220[2]; double P_000002020=Pd_002[1]*Pd_020[2]; double P_000002120=Pd_002[1]*Pd_120[2]; double P_000002220=Pd_002[1]*Pd_220[2]; double P_000102020=Pd_102[1]*Pd_020[2]; double P_000102120=Pd_102[1]*Pd_120[2]; double P_000102220=Pd_102[1]*Pd_220[2]; double P_000202020=Pd_202[1]*Pd_020[2]; double P_000202120=Pd_202[1]*Pd_120[2]; double P_000202220=Pd_202[1]*Pd_220[2]; double P_001000021=Pd_001[0]*Pd_021[2]; double P_001000121=Pd_001[0]*Pd_121[2]; double P_001000221=Pd_001[0]*Pd_221[2]; double P_001000321=Pd_001[0]*Pd_321[2]; double P_101000021=Pd_101[0]*Pd_021[2]; double P_101000121=Pd_101[0]*Pd_121[2]; double P_101000221=Pd_101[0]*Pd_221[2]; double P_101000321=Pd_101[0]*Pd_321[2]; double P_000001021=Pd_001[1]*Pd_021[2]; double P_000001121=Pd_001[1]*Pd_121[2]; double P_000001221=Pd_001[1]*Pd_221[2]; double P_000001321=Pd_001[1]*Pd_321[2]; double P_000101021=Pd_101[1]*Pd_021[2]; double P_000101121=Pd_101[1]*Pd_121[2]; double P_000101221=Pd_101[1]*Pd_221[2]; double P_000101321=Pd_101[1]*Pd_321[2]; double P_000000022=Pd_022[2]; double P_000000122=Pd_122[2]; double P_000000222=Pd_222[2]; double P_000000322=Pd_322[2]; double P_000000422=Pd_422[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(P_022000000*QR_002000000000+P_122000000*QR_002000000100+P_222000000*QR_002000000200+P_322000000*QR_002000000300+P_422000000*QR_002000000400); ans_temp[ans_id*6+0]+=Pmtrx[1]*(P_022000000*QR_001001000000+P_122000000*QR_001001000100+P_222000000*QR_001001000200+P_322000000*QR_001001000300+P_422000000*QR_001001000400); ans_temp[ans_id*6+0]+=Pmtrx[2]*(P_022000000*QR_000002000000+P_122000000*QR_000002000100+P_222000000*QR_000002000200+P_322000000*QR_000002000300+P_422000000*QR_000002000400); ans_temp[ans_id*6+0]+=Pmtrx[3]*(P_022000000*QR_001000001000+P_122000000*QR_001000001100+P_222000000*QR_001000001200+P_322000000*QR_001000001300+P_422000000*QR_001000001400); ans_temp[ans_id*6+0]+=Pmtrx[4]*(P_022000000*QR_000001001000+P_122000000*QR_000001001100+P_222000000*QR_000001001200+P_322000000*QR_000001001300+P_422000000*QR_000001001400); ans_temp[ans_id*6+0]+=Pmtrx[5]*(P_022000000*QR_000000002000+P_122000000*QR_000000002100+P_222000000*QR_000000002200+P_322000000*QR_000000002300+P_422000000*QR_000000002400); ans_temp[ans_id*6+0]+=Pmtrx[6]*(P_021001000*QR_002000000000+P_021101000*QR_002000000010+P_121001000*QR_002000000100+P_121101000*QR_002000000110+P_221001000*QR_002000000200+P_221101000*QR_002000000210+P_321001000*QR_002000000300+P_321101000*QR_002000000310); ans_temp[ans_id*6+0]+=Pmtrx[7]*(P_021001000*QR_001001000000+P_021101000*QR_001001000010+P_121001000*QR_001001000100+P_121101000*QR_001001000110+P_221001000*QR_001001000200+P_221101000*QR_001001000210+P_321001000*QR_001001000300+P_321101000*QR_001001000310); ans_temp[ans_id*6+0]+=Pmtrx[8]*(P_021001000*QR_000002000000+P_021101000*QR_000002000010+P_121001000*QR_000002000100+P_121101000*QR_000002000110+P_221001000*QR_000002000200+P_221101000*QR_000002000210+P_321001000*QR_000002000300+P_321101000*QR_000002000310); ans_temp[ans_id*6+0]+=Pmtrx[9]*(P_021001000*QR_001000001000+P_021101000*QR_001000001010+P_121001000*QR_001000001100+P_121101000*QR_001000001110+P_221001000*QR_001000001200+P_221101000*QR_001000001210+P_321001000*QR_001000001300+P_321101000*QR_001000001310); ans_temp[ans_id*6+0]+=Pmtrx[10]*(P_021001000*QR_000001001000+P_021101000*QR_000001001010+P_121001000*QR_000001001100+P_121101000*QR_000001001110+P_221001000*QR_000001001200+P_221101000*QR_000001001210+P_321001000*QR_000001001300+P_321101000*QR_000001001310); ans_temp[ans_id*6+0]+=Pmtrx[11]*(P_021001000*QR_000000002000+P_021101000*QR_000000002010+P_121001000*QR_000000002100+P_121101000*QR_000000002110+P_221001000*QR_000000002200+P_221101000*QR_000000002210+P_321001000*QR_000000002300+P_321101000*QR_000000002310); ans_temp[ans_id*6+0]+=Pmtrx[12]*(P_020002000*QR_002000000000+P_020102000*QR_002000000010+P_020202000*QR_002000000020+P_120002000*QR_002000000100+P_120102000*QR_002000000110+P_120202000*QR_002000000120+P_220002000*QR_002000000200+P_220102000*QR_002000000210+P_220202000*QR_002000000220); ans_temp[ans_id*6+0]+=Pmtrx[13]*(P_020002000*QR_001001000000+P_020102000*QR_001001000010+P_020202000*QR_001001000020+P_120002000*QR_001001000100+P_120102000*QR_001001000110+P_120202000*QR_001001000120+P_220002000*QR_001001000200+P_220102000*QR_001001000210+P_220202000*QR_001001000220); ans_temp[ans_id*6+0]+=Pmtrx[14]*(P_020002000*QR_000002000000+P_020102000*QR_000002000010+P_020202000*QR_000002000020+P_120002000*QR_000002000100+P_120102000*QR_000002000110+P_120202000*QR_000002000120+P_220002000*QR_000002000200+P_220102000*QR_000002000210+P_220202000*QR_000002000220); ans_temp[ans_id*6+0]+=Pmtrx[15]*(P_020002000*QR_001000001000+P_020102000*QR_001000001010+P_020202000*QR_001000001020+P_120002000*QR_001000001100+P_120102000*QR_001000001110+P_120202000*QR_001000001120+P_220002000*QR_001000001200+P_220102000*QR_001000001210+P_220202000*QR_001000001220); ans_temp[ans_id*6+0]+=Pmtrx[16]*(P_020002000*QR_000001001000+P_020102000*QR_000001001010+P_020202000*QR_000001001020+P_120002000*QR_000001001100+P_120102000*QR_000001001110+P_120202000*QR_000001001120+P_220002000*QR_000001001200+P_220102000*QR_000001001210+P_220202000*QR_000001001220); ans_temp[ans_id*6+0]+=Pmtrx[17]*(P_020002000*QR_000000002000+P_020102000*QR_000000002010+P_020202000*QR_000000002020+P_120002000*QR_000000002100+P_120102000*QR_000000002110+P_120202000*QR_000000002120+P_220002000*QR_000000002200+P_220102000*QR_000000002210+P_220202000*QR_000000002220); ans_temp[ans_id*6+0]+=Pmtrx[18]*(P_021000001*QR_002000000000+P_021000101*QR_002000000001+P_121000001*QR_002000000100+P_121000101*QR_002000000101+P_221000001*QR_002000000200+P_221000101*QR_002000000201+P_321000001*QR_002000000300+P_321000101*QR_002000000301); ans_temp[ans_id*6+0]+=Pmtrx[19]*(P_021000001*QR_001001000000+P_021000101*QR_001001000001+P_121000001*QR_001001000100+P_121000101*QR_001001000101+P_221000001*QR_001001000200+P_221000101*QR_001001000201+P_321000001*QR_001001000300+P_321000101*QR_001001000301); ans_temp[ans_id*6+0]+=Pmtrx[20]*(P_021000001*QR_000002000000+P_021000101*QR_000002000001+P_121000001*QR_000002000100+P_121000101*QR_000002000101+P_221000001*QR_000002000200+P_221000101*QR_000002000201+P_321000001*QR_000002000300+P_321000101*QR_000002000301); ans_temp[ans_id*6+0]+=Pmtrx[21]*(P_021000001*QR_001000001000+P_021000101*QR_001000001001+P_121000001*QR_001000001100+P_121000101*QR_001000001101+P_221000001*QR_001000001200+P_221000101*QR_001000001201+P_321000001*QR_001000001300+P_321000101*QR_001000001301); ans_temp[ans_id*6+0]+=Pmtrx[22]*(P_021000001*QR_000001001000+P_021000101*QR_000001001001+P_121000001*QR_000001001100+P_121000101*QR_000001001101+P_221000001*QR_000001001200+P_221000101*QR_000001001201+P_321000001*QR_000001001300+P_321000101*QR_000001001301); ans_temp[ans_id*6+0]+=Pmtrx[23]*(P_021000001*QR_000000002000+P_021000101*QR_000000002001+P_121000001*QR_000000002100+P_121000101*QR_000000002101+P_221000001*QR_000000002200+P_221000101*QR_000000002201+P_321000001*QR_000000002300+P_321000101*QR_000000002301); ans_temp[ans_id*6+0]+=Pmtrx[24]*(P_020001001*QR_002000000000+P_020001101*QR_002000000001+P_020101001*QR_002000000010+P_020101101*QR_002000000011+P_120001001*QR_002000000100+P_120001101*QR_002000000101+P_120101001*QR_002000000110+P_120101101*QR_002000000111+P_220001001*QR_002000000200+P_220001101*QR_002000000201+P_220101001*QR_002000000210+P_220101101*QR_002000000211); ans_temp[ans_id*6+0]+=Pmtrx[25]*(P_020001001*QR_001001000000+P_020001101*QR_001001000001+P_020101001*QR_001001000010+P_020101101*QR_001001000011+P_120001001*QR_001001000100+P_120001101*QR_001001000101+P_120101001*QR_001001000110+P_120101101*QR_001001000111+P_220001001*QR_001001000200+P_220001101*QR_001001000201+P_220101001*QR_001001000210+P_220101101*QR_001001000211); ans_temp[ans_id*6+0]+=Pmtrx[26]*(P_020001001*QR_000002000000+P_020001101*QR_000002000001+P_020101001*QR_000002000010+P_020101101*QR_000002000011+P_120001001*QR_000002000100+P_120001101*QR_000002000101+P_120101001*QR_000002000110+P_120101101*QR_000002000111+P_220001001*QR_000002000200+P_220001101*QR_000002000201+P_220101001*QR_000002000210+P_220101101*QR_000002000211); ans_temp[ans_id*6+0]+=Pmtrx[27]*(P_020001001*QR_001000001000+P_020001101*QR_001000001001+P_020101001*QR_001000001010+P_020101101*QR_001000001011+P_120001001*QR_001000001100+P_120001101*QR_001000001101+P_120101001*QR_001000001110+P_120101101*QR_001000001111+P_220001001*QR_001000001200+P_220001101*QR_001000001201+P_220101001*QR_001000001210+P_220101101*QR_001000001211); ans_temp[ans_id*6+0]+=Pmtrx[28]*(P_020001001*QR_000001001000+P_020001101*QR_000001001001+P_020101001*QR_000001001010+P_020101101*QR_000001001011+P_120001001*QR_000001001100+P_120001101*QR_000001001101+P_120101001*QR_000001001110+P_120101101*QR_000001001111+P_220001001*QR_000001001200+P_220001101*QR_000001001201+P_220101001*QR_000001001210+P_220101101*QR_000001001211); ans_temp[ans_id*6+0]+=Pmtrx[29]*(P_020001001*QR_000000002000+P_020001101*QR_000000002001+P_020101001*QR_000000002010+P_020101101*QR_000000002011+P_120001001*QR_000000002100+P_120001101*QR_000000002101+P_120101001*QR_000000002110+P_120101101*QR_000000002111+P_220001001*QR_000000002200+P_220001101*QR_000000002201+P_220101001*QR_000000002210+P_220101101*QR_000000002211); ans_temp[ans_id*6+0]+=Pmtrx[30]*(P_020000002*QR_002000000000+P_020000102*QR_002000000001+P_020000202*QR_002000000002+P_120000002*QR_002000000100+P_120000102*QR_002000000101+P_120000202*QR_002000000102+P_220000002*QR_002000000200+P_220000102*QR_002000000201+P_220000202*QR_002000000202); ans_temp[ans_id*6+0]+=Pmtrx[31]*(P_020000002*QR_001001000000+P_020000102*QR_001001000001+P_020000202*QR_001001000002+P_120000002*QR_001001000100+P_120000102*QR_001001000101+P_120000202*QR_001001000102+P_220000002*QR_001001000200+P_220000102*QR_001001000201+P_220000202*QR_001001000202); ans_temp[ans_id*6+0]+=Pmtrx[32]*(P_020000002*QR_000002000000+P_020000102*QR_000002000001+P_020000202*QR_000002000002+P_120000002*QR_000002000100+P_120000102*QR_000002000101+P_120000202*QR_000002000102+P_220000002*QR_000002000200+P_220000102*QR_000002000201+P_220000202*QR_000002000202); ans_temp[ans_id*6+0]+=Pmtrx[33]*(P_020000002*QR_001000001000+P_020000102*QR_001000001001+P_020000202*QR_001000001002+P_120000002*QR_001000001100+P_120000102*QR_001000001101+P_120000202*QR_001000001102+P_220000002*QR_001000001200+P_220000102*QR_001000001201+P_220000202*QR_001000001202); ans_temp[ans_id*6+0]+=Pmtrx[34]*(P_020000002*QR_000001001000+P_020000102*QR_000001001001+P_020000202*QR_000001001002+P_120000002*QR_000001001100+P_120000102*QR_000001001101+P_120000202*QR_000001001102+P_220000002*QR_000001001200+P_220000102*QR_000001001201+P_220000202*QR_000001001202); ans_temp[ans_id*6+0]+=Pmtrx[35]*(P_020000002*QR_000000002000+P_020000102*QR_000000002001+P_020000202*QR_000000002002+P_120000002*QR_000000002100+P_120000102*QR_000000002101+P_120000202*QR_000000002102+P_220000002*QR_000000002200+P_220000102*QR_000000002201+P_220000202*QR_000000002202); ans_temp[ans_id*6+1]+=Pmtrx[0]*(P_012010000*QR_002000000000+P_012110000*QR_002000000010+P_112010000*QR_002000000100+P_112110000*QR_002000000110+P_212010000*QR_002000000200+P_212110000*QR_002000000210+P_312010000*QR_002000000300+P_312110000*QR_002000000310); ans_temp[ans_id*6+1]+=Pmtrx[1]*(P_012010000*QR_001001000000+P_012110000*QR_001001000010+P_112010000*QR_001001000100+P_112110000*QR_001001000110+P_212010000*QR_001001000200+P_212110000*QR_001001000210+P_312010000*QR_001001000300+P_312110000*QR_001001000310); ans_temp[ans_id*6+1]+=Pmtrx[2]*(P_012010000*QR_000002000000+P_012110000*QR_000002000010+P_112010000*QR_000002000100+P_112110000*QR_000002000110+P_212010000*QR_000002000200+P_212110000*QR_000002000210+P_312010000*QR_000002000300+P_312110000*QR_000002000310); ans_temp[ans_id*6+1]+=Pmtrx[3]*(P_012010000*QR_001000001000+P_012110000*QR_001000001010+P_112010000*QR_001000001100+P_112110000*QR_001000001110+P_212010000*QR_001000001200+P_212110000*QR_001000001210+P_312010000*QR_001000001300+P_312110000*QR_001000001310); ans_temp[ans_id*6+1]+=Pmtrx[4]*(P_012010000*QR_000001001000+P_012110000*QR_000001001010+P_112010000*QR_000001001100+P_112110000*QR_000001001110+P_212010000*QR_000001001200+P_212110000*QR_000001001210+P_312010000*QR_000001001300+P_312110000*QR_000001001310); ans_temp[ans_id*6+1]+=Pmtrx[5]*(P_012010000*QR_000000002000+P_012110000*QR_000000002010+P_112010000*QR_000000002100+P_112110000*QR_000000002110+P_212010000*QR_000000002200+P_212110000*QR_000000002210+P_312010000*QR_000000002300+P_312110000*QR_000000002310); ans_temp[ans_id*6+1]+=Pmtrx[6]*(P_011011000*QR_002000000000+P_011111000*QR_002000000010+P_011211000*QR_002000000020+P_111011000*QR_002000000100+P_111111000*QR_002000000110+P_111211000*QR_002000000120+P_211011000*QR_002000000200+P_211111000*QR_002000000210+P_211211000*QR_002000000220); ans_temp[ans_id*6+1]+=Pmtrx[7]*(P_011011000*QR_001001000000+P_011111000*QR_001001000010+P_011211000*QR_001001000020+P_111011000*QR_001001000100+P_111111000*QR_001001000110+P_111211000*QR_001001000120+P_211011000*QR_001001000200+P_211111000*QR_001001000210+P_211211000*QR_001001000220); ans_temp[ans_id*6+1]+=Pmtrx[8]*(P_011011000*QR_000002000000+P_011111000*QR_000002000010+P_011211000*QR_000002000020+P_111011000*QR_000002000100+P_111111000*QR_000002000110+P_111211000*QR_000002000120+P_211011000*QR_000002000200+P_211111000*QR_000002000210+P_211211000*QR_000002000220); ans_temp[ans_id*6+1]+=Pmtrx[9]*(P_011011000*QR_001000001000+P_011111000*QR_001000001010+P_011211000*QR_001000001020+P_111011000*QR_001000001100+P_111111000*QR_001000001110+P_111211000*QR_001000001120+P_211011000*QR_001000001200+P_211111000*QR_001000001210+P_211211000*QR_001000001220); ans_temp[ans_id*6+1]+=Pmtrx[10]*(P_011011000*QR_000001001000+P_011111000*QR_000001001010+P_011211000*QR_000001001020+P_111011000*QR_000001001100+P_111111000*QR_000001001110+P_111211000*QR_000001001120+P_211011000*QR_000001001200+P_211111000*QR_000001001210+P_211211000*QR_000001001220); ans_temp[ans_id*6+1]+=Pmtrx[11]*(P_011011000*QR_000000002000+P_011111000*QR_000000002010+P_011211000*QR_000000002020+P_111011000*QR_000000002100+P_111111000*QR_000000002110+P_111211000*QR_000000002120+P_211011000*QR_000000002200+P_211111000*QR_000000002210+P_211211000*QR_000000002220); ans_temp[ans_id*6+1]+=Pmtrx[12]*(P_010012000*QR_002000000000+P_010112000*QR_002000000010+P_010212000*QR_002000000020+P_010312000*QR_002000000030+P_110012000*QR_002000000100+P_110112000*QR_002000000110+P_110212000*QR_002000000120+P_110312000*QR_002000000130); ans_temp[ans_id*6+1]+=Pmtrx[13]*(P_010012000*QR_001001000000+P_010112000*QR_001001000010+P_010212000*QR_001001000020+P_010312000*QR_001001000030+P_110012000*QR_001001000100+P_110112000*QR_001001000110+P_110212000*QR_001001000120+P_110312000*QR_001001000130); ans_temp[ans_id*6+1]+=Pmtrx[14]*(P_010012000*QR_000002000000+P_010112000*QR_000002000010+P_010212000*QR_000002000020+P_010312000*QR_000002000030+P_110012000*QR_000002000100+P_110112000*QR_000002000110+P_110212000*QR_000002000120+P_110312000*QR_000002000130); ans_temp[ans_id*6+1]+=Pmtrx[15]*(P_010012000*QR_001000001000+P_010112000*QR_001000001010+P_010212000*QR_001000001020+P_010312000*QR_001000001030+P_110012000*QR_001000001100+P_110112000*QR_001000001110+P_110212000*QR_001000001120+P_110312000*QR_001000001130); ans_temp[ans_id*6+1]+=Pmtrx[16]*(P_010012000*QR_000001001000+P_010112000*QR_000001001010+P_010212000*QR_000001001020+P_010312000*QR_000001001030+P_110012000*QR_000001001100+P_110112000*QR_000001001110+P_110212000*QR_000001001120+P_110312000*QR_000001001130); ans_temp[ans_id*6+1]+=Pmtrx[17]*(P_010012000*QR_000000002000+P_010112000*QR_000000002010+P_010212000*QR_000000002020+P_010312000*QR_000000002030+P_110012000*QR_000000002100+P_110112000*QR_000000002110+P_110212000*QR_000000002120+P_110312000*QR_000000002130); ans_temp[ans_id*6+1]+=Pmtrx[18]*(P_011010001*QR_002000000000+P_011010101*QR_002000000001+P_011110001*QR_002000000010+P_011110101*QR_002000000011+P_111010001*QR_002000000100+P_111010101*QR_002000000101+P_111110001*QR_002000000110+P_111110101*QR_002000000111+P_211010001*QR_002000000200+P_211010101*QR_002000000201+P_211110001*QR_002000000210+P_211110101*QR_002000000211); ans_temp[ans_id*6+1]+=Pmtrx[19]*(P_011010001*QR_001001000000+P_011010101*QR_001001000001+P_011110001*QR_001001000010+P_011110101*QR_001001000011+P_111010001*QR_001001000100+P_111010101*QR_001001000101+P_111110001*QR_001001000110+P_111110101*QR_001001000111+P_211010001*QR_001001000200+P_211010101*QR_001001000201+P_211110001*QR_001001000210+P_211110101*QR_001001000211); ans_temp[ans_id*6+1]+=Pmtrx[20]*(P_011010001*QR_000002000000+P_011010101*QR_000002000001+P_011110001*QR_000002000010+P_011110101*QR_000002000011+P_111010001*QR_000002000100+P_111010101*QR_000002000101+P_111110001*QR_000002000110+P_111110101*QR_000002000111+P_211010001*QR_000002000200+P_211010101*QR_000002000201+P_211110001*QR_000002000210+P_211110101*QR_000002000211); ans_temp[ans_id*6+1]+=Pmtrx[21]*(P_011010001*QR_001000001000+P_011010101*QR_001000001001+P_011110001*QR_001000001010+P_011110101*QR_001000001011+P_111010001*QR_001000001100+P_111010101*QR_001000001101+P_111110001*QR_001000001110+P_111110101*QR_001000001111+P_211010001*QR_001000001200+P_211010101*QR_001000001201+P_211110001*QR_001000001210+P_211110101*QR_001000001211); ans_temp[ans_id*6+1]+=Pmtrx[22]*(P_011010001*QR_000001001000+P_011010101*QR_000001001001+P_011110001*QR_000001001010+P_011110101*QR_000001001011+P_111010001*QR_000001001100+P_111010101*QR_000001001101+P_111110001*QR_000001001110+P_111110101*QR_000001001111+P_211010001*QR_000001001200+P_211010101*QR_000001001201+P_211110001*QR_000001001210+P_211110101*QR_000001001211); ans_temp[ans_id*6+1]+=Pmtrx[23]*(P_011010001*QR_000000002000+P_011010101*QR_000000002001+P_011110001*QR_000000002010+P_011110101*QR_000000002011+P_111010001*QR_000000002100+P_111010101*QR_000000002101+P_111110001*QR_000000002110+P_111110101*QR_000000002111+P_211010001*QR_000000002200+P_211010101*QR_000000002201+P_211110001*QR_000000002210+P_211110101*QR_000000002211); ans_temp[ans_id*6+1]+=Pmtrx[24]*(P_010011001*QR_002000000000+P_010011101*QR_002000000001+P_010111001*QR_002000000010+P_010111101*QR_002000000011+P_010211001*QR_002000000020+P_010211101*QR_002000000021+P_110011001*QR_002000000100+P_110011101*QR_002000000101+P_110111001*QR_002000000110+P_110111101*QR_002000000111+P_110211001*QR_002000000120+P_110211101*QR_002000000121); ans_temp[ans_id*6+1]+=Pmtrx[25]*(P_010011001*QR_001001000000+P_010011101*QR_001001000001+P_010111001*QR_001001000010+P_010111101*QR_001001000011+P_010211001*QR_001001000020+P_010211101*QR_001001000021+P_110011001*QR_001001000100+P_110011101*QR_001001000101+P_110111001*QR_001001000110+P_110111101*QR_001001000111+P_110211001*QR_001001000120+P_110211101*QR_001001000121); ans_temp[ans_id*6+1]+=Pmtrx[26]*(P_010011001*QR_000002000000+P_010011101*QR_000002000001+P_010111001*QR_000002000010+P_010111101*QR_000002000011+P_010211001*QR_000002000020+P_010211101*QR_000002000021+P_110011001*QR_000002000100+P_110011101*QR_000002000101+P_110111001*QR_000002000110+P_110111101*QR_000002000111+P_110211001*QR_000002000120+P_110211101*QR_000002000121); ans_temp[ans_id*6+1]+=Pmtrx[27]*(P_010011001*QR_001000001000+P_010011101*QR_001000001001+P_010111001*QR_001000001010+P_010111101*QR_001000001011+P_010211001*QR_001000001020+P_010211101*QR_001000001021+P_110011001*QR_001000001100+P_110011101*QR_001000001101+P_110111001*QR_001000001110+P_110111101*QR_001000001111+P_110211001*QR_001000001120+P_110211101*QR_001000001121); ans_temp[ans_id*6+1]+=Pmtrx[28]*(P_010011001*QR_000001001000+P_010011101*QR_000001001001+P_010111001*QR_000001001010+P_010111101*QR_000001001011+P_010211001*QR_000001001020+P_010211101*QR_000001001021+P_110011001*QR_000001001100+P_110011101*QR_000001001101+P_110111001*QR_000001001110+P_110111101*QR_000001001111+P_110211001*QR_000001001120+P_110211101*QR_000001001121); ans_temp[ans_id*6+1]+=Pmtrx[29]*(P_010011001*QR_000000002000+P_010011101*QR_000000002001+P_010111001*QR_000000002010+P_010111101*QR_000000002011+P_010211001*QR_000000002020+P_010211101*QR_000000002021+P_110011001*QR_000000002100+P_110011101*QR_000000002101+P_110111001*QR_000000002110+P_110111101*QR_000000002111+P_110211001*QR_000000002120+P_110211101*QR_000000002121); ans_temp[ans_id*6+1]+=Pmtrx[30]*(P_010010002*QR_002000000000+P_010010102*QR_002000000001+P_010010202*QR_002000000002+P_010110002*QR_002000000010+P_010110102*QR_002000000011+P_010110202*QR_002000000012+P_110010002*QR_002000000100+P_110010102*QR_002000000101+P_110010202*QR_002000000102+P_110110002*QR_002000000110+P_110110102*QR_002000000111+P_110110202*QR_002000000112); ans_temp[ans_id*6+1]+=Pmtrx[31]*(P_010010002*QR_001001000000+P_010010102*QR_001001000001+P_010010202*QR_001001000002+P_010110002*QR_001001000010+P_010110102*QR_001001000011+P_010110202*QR_001001000012+P_110010002*QR_001001000100+P_110010102*QR_001001000101+P_110010202*QR_001001000102+P_110110002*QR_001001000110+P_110110102*QR_001001000111+P_110110202*QR_001001000112); ans_temp[ans_id*6+1]+=Pmtrx[32]*(P_010010002*QR_000002000000+P_010010102*QR_000002000001+P_010010202*QR_000002000002+P_010110002*QR_000002000010+P_010110102*QR_000002000011+P_010110202*QR_000002000012+P_110010002*QR_000002000100+P_110010102*QR_000002000101+P_110010202*QR_000002000102+P_110110002*QR_000002000110+P_110110102*QR_000002000111+P_110110202*QR_000002000112); ans_temp[ans_id*6+1]+=Pmtrx[33]*(P_010010002*QR_001000001000+P_010010102*QR_001000001001+P_010010202*QR_001000001002+P_010110002*QR_001000001010+P_010110102*QR_001000001011+P_010110202*QR_001000001012+P_110010002*QR_001000001100+P_110010102*QR_001000001101+P_110010202*QR_001000001102+P_110110002*QR_001000001110+P_110110102*QR_001000001111+P_110110202*QR_001000001112); ans_temp[ans_id*6+1]+=Pmtrx[34]*(P_010010002*QR_000001001000+P_010010102*QR_000001001001+P_010010202*QR_000001001002+P_010110002*QR_000001001010+P_010110102*QR_000001001011+P_010110202*QR_000001001012+P_110010002*QR_000001001100+P_110010102*QR_000001001101+P_110010202*QR_000001001102+P_110110002*QR_000001001110+P_110110102*QR_000001001111+P_110110202*QR_000001001112); ans_temp[ans_id*6+1]+=Pmtrx[35]*(P_010010002*QR_000000002000+P_010010102*QR_000000002001+P_010010202*QR_000000002002+P_010110002*QR_000000002010+P_010110102*QR_000000002011+P_010110202*QR_000000002012+P_110010002*QR_000000002100+P_110010102*QR_000000002101+P_110010202*QR_000000002102+P_110110002*QR_000000002110+P_110110102*QR_000000002111+P_110110202*QR_000000002112); ans_temp[ans_id*6+2]+=Pmtrx[0]*(P_002020000*QR_002000000000+P_002120000*QR_002000000010+P_002220000*QR_002000000020+P_102020000*QR_002000000100+P_102120000*QR_002000000110+P_102220000*QR_002000000120+P_202020000*QR_002000000200+P_202120000*QR_002000000210+P_202220000*QR_002000000220); ans_temp[ans_id*6+2]+=Pmtrx[1]*(P_002020000*QR_001001000000+P_002120000*QR_001001000010+P_002220000*QR_001001000020+P_102020000*QR_001001000100+P_102120000*QR_001001000110+P_102220000*QR_001001000120+P_202020000*QR_001001000200+P_202120000*QR_001001000210+P_202220000*QR_001001000220); ans_temp[ans_id*6+2]+=Pmtrx[2]*(P_002020000*QR_000002000000+P_002120000*QR_000002000010+P_002220000*QR_000002000020+P_102020000*QR_000002000100+P_102120000*QR_000002000110+P_102220000*QR_000002000120+P_202020000*QR_000002000200+P_202120000*QR_000002000210+P_202220000*QR_000002000220); ans_temp[ans_id*6+2]+=Pmtrx[3]*(P_002020000*QR_001000001000+P_002120000*QR_001000001010+P_002220000*QR_001000001020+P_102020000*QR_001000001100+P_102120000*QR_001000001110+P_102220000*QR_001000001120+P_202020000*QR_001000001200+P_202120000*QR_001000001210+P_202220000*QR_001000001220); ans_temp[ans_id*6+2]+=Pmtrx[4]*(P_002020000*QR_000001001000+P_002120000*QR_000001001010+P_002220000*QR_000001001020+P_102020000*QR_000001001100+P_102120000*QR_000001001110+P_102220000*QR_000001001120+P_202020000*QR_000001001200+P_202120000*QR_000001001210+P_202220000*QR_000001001220); ans_temp[ans_id*6+2]+=Pmtrx[5]*(P_002020000*QR_000000002000+P_002120000*QR_000000002010+P_002220000*QR_000000002020+P_102020000*QR_000000002100+P_102120000*QR_000000002110+P_102220000*QR_000000002120+P_202020000*QR_000000002200+P_202120000*QR_000000002210+P_202220000*QR_000000002220); ans_temp[ans_id*6+2]+=Pmtrx[6]*(P_001021000*QR_002000000000+P_001121000*QR_002000000010+P_001221000*QR_002000000020+P_001321000*QR_002000000030+P_101021000*QR_002000000100+P_101121000*QR_002000000110+P_101221000*QR_002000000120+P_101321000*QR_002000000130); ans_temp[ans_id*6+2]+=Pmtrx[7]*(P_001021000*QR_001001000000+P_001121000*QR_001001000010+P_001221000*QR_001001000020+P_001321000*QR_001001000030+P_101021000*QR_001001000100+P_101121000*QR_001001000110+P_101221000*QR_001001000120+P_101321000*QR_001001000130); ans_temp[ans_id*6+2]+=Pmtrx[8]*(P_001021000*QR_000002000000+P_001121000*QR_000002000010+P_001221000*QR_000002000020+P_001321000*QR_000002000030+P_101021000*QR_000002000100+P_101121000*QR_000002000110+P_101221000*QR_000002000120+P_101321000*QR_000002000130); ans_temp[ans_id*6+2]+=Pmtrx[9]*(P_001021000*QR_001000001000+P_001121000*QR_001000001010+P_001221000*QR_001000001020+P_001321000*QR_001000001030+P_101021000*QR_001000001100+P_101121000*QR_001000001110+P_101221000*QR_001000001120+P_101321000*QR_001000001130); ans_temp[ans_id*6+2]+=Pmtrx[10]*(P_001021000*QR_000001001000+P_001121000*QR_000001001010+P_001221000*QR_000001001020+P_001321000*QR_000001001030+P_101021000*QR_000001001100+P_101121000*QR_000001001110+P_101221000*QR_000001001120+P_101321000*QR_000001001130); ans_temp[ans_id*6+2]+=Pmtrx[11]*(P_001021000*QR_000000002000+P_001121000*QR_000000002010+P_001221000*QR_000000002020+P_001321000*QR_000000002030+P_101021000*QR_000000002100+P_101121000*QR_000000002110+P_101221000*QR_000000002120+P_101321000*QR_000000002130); ans_temp[ans_id*6+2]+=Pmtrx[12]*(P_000022000*QR_002000000000+P_000122000*QR_002000000010+P_000222000*QR_002000000020+P_000322000*QR_002000000030+P_000422000*QR_002000000040); ans_temp[ans_id*6+2]+=Pmtrx[13]*(P_000022000*QR_001001000000+P_000122000*QR_001001000010+P_000222000*QR_001001000020+P_000322000*QR_001001000030+P_000422000*QR_001001000040); ans_temp[ans_id*6+2]+=Pmtrx[14]*(P_000022000*QR_000002000000+P_000122000*QR_000002000010+P_000222000*QR_000002000020+P_000322000*QR_000002000030+P_000422000*QR_000002000040); ans_temp[ans_id*6+2]+=Pmtrx[15]*(P_000022000*QR_001000001000+P_000122000*QR_001000001010+P_000222000*QR_001000001020+P_000322000*QR_001000001030+P_000422000*QR_001000001040); ans_temp[ans_id*6+2]+=Pmtrx[16]*(P_000022000*QR_000001001000+P_000122000*QR_000001001010+P_000222000*QR_000001001020+P_000322000*QR_000001001030+P_000422000*QR_000001001040); ans_temp[ans_id*6+2]+=Pmtrx[17]*(P_000022000*QR_000000002000+P_000122000*QR_000000002010+P_000222000*QR_000000002020+P_000322000*QR_000000002030+P_000422000*QR_000000002040); ans_temp[ans_id*6+2]+=Pmtrx[18]*(P_001020001*QR_002000000000+P_001020101*QR_002000000001+P_001120001*QR_002000000010+P_001120101*QR_002000000011+P_001220001*QR_002000000020+P_001220101*QR_002000000021+P_101020001*QR_002000000100+P_101020101*QR_002000000101+P_101120001*QR_002000000110+P_101120101*QR_002000000111+P_101220001*QR_002000000120+P_101220101*QR_002000000121); ans_temp[ans_id*6+2]+=Pmtrx[19]*(P_001020001*QR_001001000000+P_001020101*QR_001001000001+P_001120001*QR_001001000010+P_001120101*QR_001001000011+P_001220001*QR_001001000020+P_001220101*QR_001001000021+P_101020001*QR_001001000100+P_101020101*QR_001001000101+P_101120001*QR_001001000110+P_101120101*QR_001001000111+P_101220001*QR_001001000120+P_101220101*QR_001001000121); ans_temp[ans_id*6+2]+=Pmtrx[20]*(P_001020001*QR_000002000000+P_001020101*QR_000002000001+P_001120001*QR_000002000010+P_001120101*QR_000002000011+P_001220001*QR_000002000020+P_001220101*QR_000002000021+P_101020001*QR_000002000100+P_101020101*QR_000002000101+P_101120001*QR_000002000110+P_101120101*QR_000002000111+P_101220001*QR_000002000120+P_101220101*QR_000002000121); ans_temp[ans_id*6+2]+=Pmtrx[21]*(P_001020001*QR_001000001000+P_001020101*QR_001000001001+P_001120001*QR_001000001010+P_001120101*QR_001000001011+P_001220001*QR_001000001020+P_001220101*QR_001000001021+P_101020001*QR_001000001100+P_101020101*QR_001000001101+P_101120001*QR_001000001110+P_101120101*QR_001000001111+P_101220001*QR_001000001120+P_101220101*QR_001000001121); ans_temp[ans_id*6+2]+=Pmtrx[22]*(P_001020001*QR_000001001000+P_001020101*QR_000001001001+P_001120001*QR_000001001010+P_001120101*QR_000001001011+P_001220001*QR_000001001020+P_001220101*QR_000001001021+P_101020001*QR_000001001100+P_101020101*QR_000001001101+P_101120001*QR_000001001110+P_101120101*QR_000001001111+P_101220001*QR_000001001120+P_101220101*QR_000001001121); ans_temp[ans_id*6+2]+=Pmtrx[23]*(P_001020001*QR_000000002000+P_001020101*QR_000000002001+P_001120001*QR_000000002010+P_001120101*QR_000000002011+P_001220001*QR_000000002020+P_001220101*QR_000000002021+P_101020001*QR_000000002100+P_101020101*QR_000000002101+P_101120001*QR_000000002110+P_101120101*QR_000000002111+P_101220001*QR_000000002120+P_101220101*QR_000000002121); ans_temp[ans_id*6+2]+=Pmtrx[24]*(P_000021001*QR_002000000000+P_000021101*QR_002000000001+P_000121001*QR_002000000010+P_000121101*QR_002000000011+P_000221001*QR_002000000020+P_000221101*QR_002000000021+P_000321001*QR_002000000030+P_000321101*QR_002000000031); ans_temp[ans_id*6+2]+=Pmtrx[25]*(P_000021001*QR_001001000000+P_000021101*QR_001001000001+P_000121001*QR_001001000010+P_000121101*QR_001001000011+P_000221001*QR_001001000020+P_000221101*QR_001001000021+P_000321001*QR_001001000030+P_000321101*QR_001001000031); ans_temp[ans_id*6+2]+=Pmtrx[26]*(P_000021001*QR_000002000000+P_000021101*QR_000002000001+P_000121001*QR_000002000010+P_000121101*QR_000002000011+P_000221001*QR_000002000020+P_000221101*QR_000002000021+P_000321001*QR_000002000030+P_000321101*QR_000002000031); ans_temp[ans_id*6+2]+=Pmtrx[27]*(P_000021001*QR_001000001000+P_000021101*QR_001000001001+P_000121001*QR_001000001010+P_000121101*QR_001000001011+P_000221001*QR_001000001020+P_000221101*QR_001000001021+P_000321001*QR_001000001030+P_000321101*QR_001000001031); ans_temp[ans_id*6+2]+=Pmtrx[28]*(P_000021001*QR_000001001000+P_000021101*QR_000001001001+P_000121001*QR_000001001010+P_000121101*QR_000001001011+P_000221001*QR_000001001020+P_000221101*QR_000001001021+P_000321001*QR_000001001030+P_000321101*QR_000001001031); ans_temp[ans_id*6+2]+=Pmtrx[29]*(P_000021001*QR_000000002000+P_000021101*QR_000000002001+P_000121001*QR_000000002010+P_000121101*QR_000000002011+P_000221001*QR_000000002020+P_000221101*QR_000000002021+P_000321001*QR_000000002030+P_000321101*QR_000000002031); ans_temp[ans_id*6+2]+=Pmtrx[30]*(P_000020002*QR_002000000000+P_000020102*QR_002000000001+P_000020202*QR_002000000002+P_000120002*QR_002000000010+P_000120102*QR_002000000011+P_000120202*QR_002000000012+P_000220002*QR_002000000020+P_000220102*QR_002000000021+P_000220202*QR_002000000022); ans_temp[ans_id*6+2]+=Pmtrx[31]*(P_000020002*QR_001001000000+P_000020102*QR_001001000001+P_000020202*QR_001001000002+P_000120002*QR_001001000010+P_000120102*QR_001001000011+P_000120202*QR_001001000012+P_000220002*QR_001001000020+P_000220102*QR_001001000021+P_000220202*QR_001001000022); ans_temp[ans_id*6+2]+=Pmtrx[32]*(P_000020002*QR_000002000000+P_000020102*QR_000002000001+P_000020202*QR_000002000002+P_000120002*QR_000002000010+P_000120102*QR_000002000011+P_000120202*QR_000002000012+P_000220002*QR_000002000020+P_000220102*QR_000002000021+P_000220202*QR_000002000022); ans_temp[ans_id*6+2]+=Pmtrx[33]*(P_000020002*QR_001000001000+P_000020102*QR_001000001001+P_000020202*QR_001000001002+P_000120002*QR_001000001010+P_000120102*QR_001000001011+P_000120202*QR_001000001012+P_000220002*QR_001000001020+P_000220102*QR_001000001021+P_000220202*QR_001000001022); ans_temp[ans_id*6+2]+=Pmtrx[34]*(P_000020002*QR_000001001000+P_000020102*QR_000001001001+P_000020202*QR_000001001002+P_000120002*QR_000001001010+P_000120102*QR_000001001011+P_000120202*QR_000001001012+P_000220002*QR_000001001020+P_000220102*QR_000001001021+P_000220202*QR_000001001022); ans_temp[ans_id*6+2]+=Pmtrx[35]*(P_000020002*QR_000000002000+P_000020102*QR_000000002001+P_000020202*QR_000000002002+P_000120002*QR_000000002010+P_000120102*QR_000000002011+P_000120202*QR_000000002012+P_000220002*QR_000000002020+P_000220102*QR_000000002021+P_000220202*QR_000000002022); ans_temp[ans_id*6+3]+=Pmtrx[0]*(P_012000010*QR_002000000000+P_012000110*QR_002000000001+P_112000010*QR_002000000100+P_112000110*QR_002000000101+P_212000010*QR_002000000200+P_212000110*QR_002000000201+P_312000010*QR_002000000300+P_312000110*QR_002000000301); ans_temp[ans_id*6+3]+=Pmtrx[1]*(P_012000010*QR_001001000000+P_012000110*QR_001001000001+P_112000010*QR_001001000100+P_112000110*QR_001001000101+P_212000010*QR_001001000200+P_212000110*QR_001001000201+P_312000010*QR_001001000300+P_312000110*QR_001001000301); ans_temp[ans_id*6+3]+=Pmtrx[2]*(P_012000010*QR_000002000000+P_012000110*QR_000002000001+P_112000010*QR_000002000100+P_112000110*QR_000002000101+P_212000010*QR_000002000200+P_212000110*QR_000002000201+P_312000010*QR_000002000300+P_312000110*QR_000002000301); ans_temp[ans_id*6+3]+=Pmtrx[3]*(P_012000010*QR_001000001000+P_012000110*QR_001000001001+P_112000010*QR_001000001100+P_112000110*QR_001000001101+P_212000010*QR_001000001200+P_212000110*QR_001000001201+P_312000010*QR_001000001300+P_312000110*QR_001000001301); ans_temp[ans_id*6+3]+=Pmtrx[4]*(P_012000010*QR_000001001000+P_012000110*QR_000001001001+P_112000010*QR_000001001100+P_112000110*QR_000001001101+P_212000010*QR_000001001200+P_212000110*QR_000001001201+P_312000010*QR_000001001300+P_312000110*QR_000001001301); ans_temp[ans_id*6+3]+=Pmtrx[5]*(P_012000010*QR_000000002000+P_012000110*QR_000000002001+P_112000010*QR_000000002100+P_112000110*QR_000000002101+P_212000010*QR_000000002200+P_212000110*QR_000000002201+P_312000010*QR_000000002300+P_312000110*QR_000000002301); ans_temp[ans_id*6+3]+=Pmtrx[6]*(P_011001010*QR_002000000000+P_011001110*QR_002000000001+P_011101010*QR_002000000010+P_011101110*QR_002000000011+P_111001010*QR_002000000100+P_111001110*QR_002000000101+P_111101010*QR_002000000110+P_111101110*QR_002000000111+P_211001010*QR_002000000200+P_211001110*QR_002000000201+P_211101010*QR_002000000210+P_211101110*QR_002000000211); ans_temp[ans_id*6+3]+=Pmtrx[7]*(P_011001010*QR_001001000000+P_011001110*QR_001001000001+P_011101010*QR_001001000010+P_011101110*QR_001001000011+P_111001010*QR_001001000100+P_111001110*QR_001001000101+P_111101010*QR_001001000110+P_111101110*QR_001001000111+P_211001010*QR_001001000200+P_211001110*QR_001001000201+P_211101010*QR_001001000210+P_211101110*QR_001001000211); ans_temp[ans_id*6+3]+=Pmtrx[8]*(P_011001010*QR_000002000000+P_011001110*QR_000002000001+P_011101010*QR_000002000010+P_011101110*QR_000002000011+P_111001010*QR_000002000100+P_111001110*QR_000002000101+P_111101010*QR_000002000110+P_111101110*QR_000002000111+P_211001010*QR_000002000200+P_211001110*QR_000002000201+P_211101010*QR_000002000210+P_211101110*QR_000002000211); ans_temp[ans_id*6+3]+=Pmtrx[9]*(P_011001010*QR_001000001000+P_011001110*QR_001000001001+P_011101010*QR_001000001010+P_011101110*QR_001000001011+P_111001010*QR_001000001100+P_111001110*QR_001000001101+P_111101010*QR_001000001110+P_111101110*QR_001000001111+P_211001010*QR_001000001200+P_211001110*QR_001000001201+P_211101010*QR_001000001210+P_211101110*QR_001000001211); ans_temp[ans_id*6+3]+=Pmtrx[10]*(P_011001010*QR_000001001000+P_011001110*QR_000001001001+P_011101010*QR_000001001010+P_011101110*QR_000001001011+P_111001010*QR_000001001100+P_111001110*QR_000001001101+P_111101010*QR_000001001110+P_111101110*QR_000001001111+P_211001010*QR_000001001200+P_211001110*QR_000001001201+P_211101010*QR_000001001210+P_211101110*QR_000001001211); ans_temp[ans_id*6+3]+=Pmtrx[11]*(P_011001010*QR_000000002000+P_011001110*QR_000000002001+P_011101010*QR_000000002010+P_011101110*QR_000000002011+P_111001010*QR_000000002100+P_111001110*QR_000000002101+P_111101010*QR_000000002110+P_111101110*QR_000000002111+P_211001010*QR_000000002200+P_211001110*QR_000000002201+P_211101010*QR_000000002210+P_211101110*QR_000000002211); ans_temp[ans_id*6+3]+=Pmtrx[12]*(P_010002010*QR_002000000000+P_010002110*QR_002000000001+P_010102010*QR_002000000010+P_010102110*QR_002000000011+P_010202010*QR_002000000020+P_010202110*QR_002000000021+P_110002010*QR_002000000100+P_110002110*QR_002000000101+P_110102010*QR_002000000110+P_110102110*QR_002000000111+P_110202010*QR_002000000120+P_110202110*QR_002000000121); ans_temp[ans_id*6+3]+=Pmtrx[13]*(P_010002010*QR_001001000000+P_010002110*QR_001001000001+P_010102010*QR_001001000010+P_010102110*QR_001001000011+P_010202010*QR_001001000020+P_010202110*QR_001001000021+P_110002010*QR_001001000100+P_110002110*QR_001001000101+P_110102010*QR_001001000110+P_110102110*QR_001001000111+P_110202010*QR_001001000120+P_110202110*QR_001001000121); ans_temp[ans_id*6+3]+=Pmtrx[14]*(P_010002010*QR_000002000000+P_010002110*QR_000002000001+P_010102010*QR_000002000010+P_010102110*QR_000002000011+P_010202010*QR_000002000020+P_010202110*QR_000002000021+P_110002010*QR_000002000100+P_110002110*QR_000002000101+P_110102010*QR_000002000110+P_110102110*QR_000002000111+P_110202010*QR_000002000120+P_110202110*QR_000002000121); ans_temp[ans_id*6+3]+=Pmtrx[15]*(P_010002010*QR_001000001000+P_010002110*QR_001000001001+P_010102010*QR_001000001010+P_010102110*QR_001000001011+P_010202010*QR_001000001020+P_010202110*QR_001000001021+P_110002010*QR_001000001100+P_110002110*QR_001000001101+P_110102010*QR_001000001110+P_110102110*QR_001000001111+P_110202010*QR_001000001120+P_110202110*QR_001000001121); ans_temp[ans_id*6+3]+=Pmtrx[16]*(P_010002010*QR_000001001000+P_010002110*QR_000001001001+P_010102010*QR_000001001010+P_010102110*QR_000001001011+P_010202010*QR_000001001020+P_010202110*QR_000001001021+P_110002010*QR_000001001100+P_110002110*QR_000001001101+P_110102010*QR_000001001110+P_110102110*QR_000001001111+P_110202010*QR_000001001120+P_110202110*QR_000001001121); ans_temp[ans_id*6+3]+=Pmtrx[17]*(P_010002010*QR_000000002000+P_010002110*QR_000000002001+P_010102010*QR_000000002010+P_010102110*QR_000000002011+P_010202010*QR_000000002020+P_010202110*QR_000000002021+P_110002010*QR_000000002100+P_110002110*QR_000000002101+P_110102010*QR_000000002110+P_110102110*QR_000000002111+P_110202010*QR_000000002120+P_110202110*QR_000000002121); ans_temp[ans_id*6+3]+=Pmtrx[18]*(P_011000011*QR_002000000000+P_011000111*QR_002000000001+P_011000211*QR_002000000002+P_111000011*QR_002000000100+P_111000111*QR_002000000101+P_111000211*QR_002000000102+P_211000011*QR_002000000200+P_211000111*QR_002000000201+P_211000211*QR_002000000202); ans_temp[ans_id*6+3]+=Pmtrx[19]*(P_011000011*QR_001001000000+P_011000111*QR_001001000001+P_011000211*QR_001001000002+P_111000011*QR_001001000100+P_111000111*QR_001001000101+P_111000211*QR_001001000102+P_211000011*QR_001001000200+P_211000111*QR_001001000201+P_211000211*QR_001001000202); ans_temp[ans_id*6+3]+=Pmtrx[20]*(P_011000011*QR_000002000000+P_011000111*QR_000002000001+P_011000211*QR_000002000002+P_111000011*QR_000002000100+P_111000111*QR_000002000101+P_111000211*QR_000002000102+P_211000011*QR_000002000200+P_211000111*QR_000002000201+P_211000211*QR_000002000202); ans_temp[ans_id*6+3]+=Pmtrx[21]*(P_011000011*QR_001000001000+P_011000111*QR_001000001001+P_011000211*QR_001000001002+P_111000011*QR_001000001100+P_111000111*QR_001000001101+P_111000211*QR_001000001102+P_211000011*QR_001000001200+P_211000111*QR_001000001201+P_211000211*QR_001000001202); ans_temp[ans_id*6+3]+=Pmtrx[22]*(P_011000011*QR_000001001000+P_011000111*QR_000001001001+P_011000211*QR_000001001002+P_111000011*QR_000001001100+P_111000111*QR_000001001101+P_111000211*QR_000001001102+P_211000011*QR_000001001200+P_211000111*QR_000001001201+P_211000211*QR_000001001202); ans_temp[ans_id*6+3]+=Pmtrx[23]*(P_011000011*QR_000000002000+P_011000111*QR_000000002001+P_011000211*QR_000000002002+P_111000011*QR_000000002100+P_111000111*QR_000000002101+P_111000211*QR_000000002102+P_211000011*QR_000000002200+P_211000111*QR_000000002201+P_211000211*QR_000000002202); ans_temp[ans_id*6+3]+=Pmtrx[24]*(P_010001011*QR_002000000000+P_010001111*QR_002000000001+P_010001211*QR_002000000002+P_010101011*QR_002000000010+P_010101111*QR_002000000011+P_010101211*QR_002000000012+P_110001011*QR_002000000100+P_110001111*QR_002000000101+P_110001211*QR_002000000102+P_110101011*QR_002000000110+P_110101111*QR_002000000111+P_110101211*QR_002000000112); ans_temp[ans_id*6+3]+=Pmtrx[25]*(P_010001011*QR_001001000000+P_010001111*QR_001001000001+P_010001211*QR_001001000002+P_010101011*QR_001001000010+P_010101111*QR_001001000011+P_010101211*QR_001001000012+P_110001011*QR_001001000100+P_110001111*QR_001001000101+P_110001211*QR_001001000102+P_110101011*QR_001001000110+P_110101111*QR_001001000111+P_110101211*QR_001001000112); ans_temp[ans_id*6+3]+=Pmtrx[26]*(P_010001011*QR_000002000000+P_010001111*QR_000002000001+P_010001211*QR_000002000002+P_010101011*QR_000002000010+P_010101111*QR_000002000011+P_010101211*QR_000002000012+P_110001011*QR_000002000100+P_110001111*QR_000002000101+P_110001211*QR_000002000102+P_110101011*QR_000002000110+P_110101111*QR_000002000111+P_110101211*QR_000002000112); ans_temp[ans_id*6+3]+=Pmtrx[27]*(P_010001011*QR_001000001000+P_010001111*QR_001000001001+P_010001211*QR_001000001002+P_010101011*QR_001000001010+P_010101111*QR_001000001011+P_010101211*QR_001000001012+P_110001011*QR_001000001100+P_110001111*QR_001000001101+P_110001211*QR_001000001102+P_110101011*QR_001000001110+P_110101111*QR_001000001111+P_110101211*QR_001000001112); ans_temp[ans_id*6+3]+=Pmtrx[28]*(P_010001011*QR_000001001000+P_010001111*QR_000001001001+P_010001211*QR_000001001002+P_010101011*QR_000001001010+P_010101111*QR_000001001011+P_010101211*QR_000001001012+P_110001011*QR_000001001100+P_110001111*QR_000001001101+P_110001211*QR_000001001102+P_110101011*QR_000001001110+P_110101111*QR_000001001111+P_110101211*QR_000001001112); ans_temp[ans_id*6+3]+=Pmtrx[29]*(P_010001011*QR_000000002000+P_010001111*QR_000000002001+P_010001211*QR_000000002002+P_010101011*QR_000000002010+P_010101111*QR_000000002011+P_010101211*QR_000000002012+P_110001011*QR_000000002100+P_110001111*QR_000000002101+P_110001211*QR_000000002102+P_110101011*QR_000000002110+P_110101111*QR_000000002111+P_110101211*QR_000000002112); ans_temp[ans_id*6+3]+=Pmtrx[30]*(P_010000012*QR_002000000000+P_010000112*QR_002000000001+P_010000212*QR_002000000002+P_010000312*QR_002000000003+P_110000012*QR_002000000100+P_110000112*QR_002000000101+P_110000212*QR_002000000102+P_110000312*QR_002000000103); ans_temp[ans_id*6+3]+=Pmtrx[31]*(P_010000012*QR_001001000000+P_010000112*QR_001001000001+P_010000212*QR_001001000002+P_010000312*QR_001001000003+P_110000012*QR_001001000100+P_110000112*QR_001001000101+P_110000212*QR_001001000102+P_110000312*QR_001001000103); ans_temp[ans_id*6+3]+=Pmtrx[32]*(P_010000012*QR_000002000000+P_010000112*QR_000002000001+P_010000212*QR_000002000002+P_010000312*QR_000002000003+P_110000012*QR_000002000100+P_110000112*QR_000002000101+P_110000212*QR_000002000102+P_110000312*QR_000002000103); ans_temp[ans_id*6+3]+=Pmtrx[33]*(P_010000012*QR_001000001000+P_010000112*QR_001000001001+P_010000212*QR_001000001002+P_010000312*QR_001000001003+P_110000012*QR_001000001100+P_110000112*QR_001000001101+P_110000212*QR_001000001102+P_110000312*QR_001000001103); ans_temp[ans_id*6+3]+=Pmtrx[34]*(P_010000012*QR_000001001000+P_010000112*QR_000001001001+P_010000212*QR_000001001002+P_010000312*QR_000001001003+P_110000012*QR_000001001100+P_110000112*QR_000001001101+P_110000212*QR_000001001102+P_110000312*QR_000001001103); ans_temp[ans_id*6+3]+=Pmtrx[35]*(P_010000012*QR_000000002000+P_010000112*QR_000000002001+P_010000212*QR_000000002002+P_010000312*QR_000000002003+P_110000012*QR_000000002100+P_110000112*QR_000000002101+P_110000212*QR_000000002102+P_110000312*QR_000000002103); ans_temp[ans_id*6+4]+=Pmtrx[0]*(P_002010010*QR_002000000000+P_002010110*QR_002000000001+P_002110010*QR_002000000010+P_002110110*QR_002000000011+P_102010010*QR_002000000100+P_102010110*QR_002000000101+P_102110010*QR_002000000110+P_102110110*QR_002000000111+P_202010010*QR_002000000200+P_202010110*QR_002000000201+P_202110010*QR_002000000210+P_202110110*QR_002000000211); ans_temp[ans_id*6+4]+=Pmtrx[1]*(P_002010010*QR_001001000000+P_002010110*QR_001001000001+P_002110010*QR_001001000010+P_002110110*QR_001001000011+P_102010010*QR_001001000100+P_102010110*QR_001001000101+P_102110010*QR_001001000110+P_102110110*QR_001001000111+P_202010010*QR_001001000200+P_202010110*QR_001001000201+P_202110010*QR_001001000210+P_202110110*QR_001001000211); ans_temp[ans_id*6+4]+=Pmtrx[2]*(P_002010010*QR_000002000000+P_002010110*QR_000002000001+P_002110010*QR_000002000010+P_002110110*QR_000002000011+P_102010010*QR_000002000100+P_102010110*QR_000002000101+P_102110010*QR_000002000110+P_102110110*QR_000002000111+P_202010010*QR_000002000200+P_202010110*QR_000002000201+P_202110010*QR_000002000210+P_202110110*QR_000002000211); ans_temp[ans_id*6+4]+=Pmtrx[3]*(P_002010010*QR_001000001000+P_002010110*QR_001000001001+P_002110010*QR_001000001010+P_002110110*QR_001000001011+P_102010010*QR_001000001100+P_102010110*QR_001000001101+P_102110010*QR_001000001110+P_102110110*QR_001000001111+P_202010010*QR_001000001200+P_202010110*QR_001000001201+P_202110010*QR_001000001210+P_202110110*QR_001000001211); ans_temp[ans_id*6+4]+=Pmtrx[4]*(P_002010010*QR_000001001000+P_002010110*QR_000001001001+P_002110010*QR_000001001010+P_002110110*QR_000001001011+P_102010010*QR_000001001100+P_102010110*QR_000001001101+P_102110010*QR_000001001110+P_102110110*QR_000001001111+P_202010010*QR_000001001200+P_202010110*QR_000001001201+P_202110010*QR_000001001210+P_202110110*QR_000001001211); ans_temp[ans_id*6+4]+=Pmtrx[5]*(P_002010010*QR_000000002000+P_002010110*QR_000000002001+P_002110010*QR_000000002010+P_002110110*QR_000000002011+P_102010010*QR_000000002100+P_102010110*QR_000000002101+P_102110010*QR_000000002110+P_102110110*QR_000000002111+P_202010010*QR_000000002200+P_202010110*QR_000000002201+P_202110010*QR_000000002210+P_202110110*QR_000000002211); ans_temp[ans_id*6+4]+=Pmtrx[6]*(P_001011010*QR_002000000000+P_001011110*QR_002000000001+P_001111010*QR_002000000010+P_001111110*QR_002000000011+P_001211010*QR_002000000020+P_001211110*QR_002000000021+P_101011010*QR_002000000100+P_101011110*QR_002000000101+P_101111010*QR_002000000110+P_101111110*QR_002000000111+P_101211010*QR_002000000120+P_101211110*QR_002000000121); ans_temp[ans_id*6+4]+=Pmtrx[7]*(P_001011010*QR_001001000000+P_001011110*QR_001001000001+P_001111010*QR_001001000010+P_001111110*QR_001001000011+P_001211010*QR_001001000020+P_001211110*QR_001001000021+P_101011010*QR_001001000100+P_101011110*QR_001001000101+P_101111010*QR_001001000110+P_101111110*QR_001001000111+P_101211010*QR_001001000120+P_101211110*QR_001001000121); ans_temp[ans_id*6+4]+=Pmtrx[8]*(P_001011010*QR_000002000000+P_001011110*QR_000002000001+P_001111010*QR_000002000010+P_001111110*QR_000002000011+P_001211010*QR_000002000020+P_001211110*QR_000002000021+P_101011010*QR_000002000100+P_101011110*QR_000002000101+P_101111010*QR_000002000110+P_101111110*QR_000002000111+P_101211010*QR_000002000120+P_101211110*QR_000002000121); ans_temp[ans_id*6+4]+=Pmtrx[9]*(P_001011010*QR_001000001000+P_001011110*QR_001000001001+P_001111010*QR_001000001010+P_001111110*QR_001000001011+P_001211010*QR_001000001020+P_001211110*QR_001000001021+P_101011010*QR_001000001100+P_101011110*QR_001000001101+P_101111010*QR_001000001110+P_101111110*QR_001000001111+P_101211010*QR_001000001120+P_101211110*QR_001000001121); ans_temp[ans_id*6+4]+=Pmtrx[10]*(P_001011010*QR_000001001000+P_001011110*QR_000001001001+P_001111010*QR_000001001010+P_001111110*QR_000001001011+P_001211010*QR_000001001020+P_001211110*QR_000001001021+P_101011010*QR_000001001100+P_101011110*QR_000001001101+P_101111010*QR_000001001110+P_101111110*QR_000001001111+P_101211010*QR_000001001120+P_101211110*QR_000001001121); ans_temp[ans_id*6+4]+=Pmtrx[11]*(P_001011010*QR_000000002000+P_001011110*QR_000000002001+P_001111010*QR_000000002010+P_001111110*QR_000000002011+P_001211010*QR_000000002020+P_001211110*QR_000000002021+P_101011010*QR_000000002100+P_101011110*QR_000000002101+P_101111010*QR_000000002110+P_101111110*QR_000000002111+P_101211010*QR_000000002120+P_101211110*QR_000000002121); ans_temp[ans_id*6+4]+=Pmtrx[12]*(P_000012010*QR_002000000000+P_000012110*QR_002000000001+P_000112010*QR_002000000010+P_000112110*QR_002000000011+P_000212010*QR_002000000020+P_000212110*QR_002000000021+P_000312010*QR_002000000030+P_000312110*QR_002000000031); ans_temp[ans_id*6+4]+=Pmtrx[13]*(P_000012010*QR_001001000000+P_000012110*QR_001001000001+P_000112010*QR_001001000010+P_000112110*QR_001001000011+P_000212010*QR_001001000020+P_000212110*QR_001001000021+P_000312010*QR_001001000030+P_000312110*QR_001001000031); ans_temp[ans_id*6+4]+=Pmtrx[14]*(P_000012010*QR_000002000000+P_000012110*QR_000002000001+P_000112010*QR_000002000010+P_000112110*QR_000002000011+P_000212010*QR_000002000020+P_000212110*QR_000002000021+P_000312010*QR_000002000030+P_000312110*QR_000002000031); ans_temp[ans_id*6+4]+=Pmtrx[15]*(P_000012010*QR_001000001000+P_000012110*QR_001000001001+P_000112010*QR_001000001010+P_000112110*QR_001000001011+P_000212010*QR_001000001020+P_000212110*QR_001000001021+P_000312010*QR_001000001030+P_000312110*QR_001000001031); ans_temp[ans_id*6+4]+=Pmtrx[16]*(P_000012010*QR_000001001000+P_000012110*QR_000001001001+P_000112010*QR_000001001010+P_000112110*QR_000001001011+P_000212010*QR_000001001020+P_000212110*QR_000001001021+P_000312010*QR_000001001030+P_000312110*QR_000001001031); ans_temp[ans_id*6+4]+=Pmtrx[17]*(P_000012010*QR_000000002000+P_000012110*QR_000000002001+P_000112010*QR_000000002010+P_000112110*QR_000000002011+P_000212010*QR_000000002020+P_000212110*QR_000000002021+P_000312010*QR_000000002030+P_000312110*QR_000000002031); ans_temp[ans_id*6+4]+=Pmtrx[18]*(P_001010011*QR_002000000000+P_001010111*QR_002000000001+P_001010211*QR_002000000002+P_001110011*QR_002000000010+P_001110111*QR_002000000011+P_001110211*QR_002000000012+P_101010011*QR_002000000100+P_101010111*QR_002000000101+P_101010211*QR_002000000102+P_101110011*QR_002000000110+P_101110111*QR_002000000111+P_101110211*QR_002000000112); ans_temp[ans_id*6+4]+=Pmtrx[19]*(P_001010011*QR_001001000000+P_001010111*QR_001001000001+P_001010211*QR_001001000002+P_001110011*QR_001001000010+P_001110111*QR_001001000011+P_001110211*QR_001001000012+P_101010011*QR_001001000100+P_101010111*QR_001001000101+P_101010211*QR_001001000102+P_101110011*QR_001001000110+P_101110111*QR_001001000111+P_101110211*QR_001001000112); ans_temp[ans_id*6+4]+=Pmtrx[20]*(P_001010011*QR_000002000000+P_001010111*QR_000002000001+P_001010211*QR_000002000002+P_001110011*QR_000002000010+P_001110111*QR_000002000011+P_001110211*QR_000002000012+P_101010011*QR_000002000100+P_101010111*QR_000002000101+P_101010211*QR_000002000102+P_101110011*QR_000002000110+P_101110111*QR_000002000111+P_101110211*QR_000002000112); ans_temp[ans_id*6+4]+=Pmtrx[21]*(P_001010011*QR_001000001000+P_001010111*QR_001000001001+P_001010211*QR_001000001002+P_001110011*QR_001000001010+P_001110111*QR_001000001011+P_001110211*QR_001000001012+P_101010011*QR_001000001100+P_101010111*QR_001000001101+P_101010211*QR_001000001102+P_101110011*QR_001000001110+P_101110111*QR_001000001111+P_101110211*QR_001000001112); ans_temp[ans_id*6+4]+=Pmtrx[22]*(P_001010011*QR_000001001000+P_001010111*QR_000001001001+P_001010211*QR_000001001002+P_001110011*QR_000001001010+P_001110111*QR_000001001011+P_001110211*QR_000001001012+P_101010011*QR_000001001100+P_101010111*QR_000001001101+P_101010211*QR_000001001102+P_101110011*QR_000001001110+P_101110111*QR_000001001111+P_101110211*QR_000001001112); ans_temp[ans_id*6+4]+=Pmtrx[23]*(P_001010011*QR_000000002000+P_001010111*QR_000000002001+P_001010211*QR_000000002002+P_001110011*QR_000000002010+P_001110111*QR_000000002011+P_001110211*QR_000000002012+P_101010011*QR_000000002100+P_101010111*QR_000000002101+P_101010211*QR_000000002102+P_101110011*QR_000000002110+P_101110111*QR_000000002111+P_101110211*QR_000000002112); ans_temp[ans_id*6+4]+=Pmtrx[24]*(P_000011011*QR_002000000000+P_000011111*QR_002000000001+P_000011211*QR_002000000002+P_000111011*QR_002000000010+P_000111111*QR_002000000011+P_000111211*QR_002000000012+P_000211011*QR_002000000020+P_000211111*QR_002000000021+P_000211211*QR_002000000022); ans_temp[ans_id*6+4]+=Pmtrx[25]*(P_000011011*QR_001001000000+P_000011111*QR_001001000001+P_000011211*QR_001001000002+P_000111011*QR_001001000010+P_000111111*QR_001001000011+P_000111211*QR_001001000012+P_000211011*QR_001001000020+P_000211111*QR_001001000021+P_000211211*QR_001001000022); ans_temp[ans_id*6+4]+=Pmtrx[26]*(P_000011011*QR_000002000000+P_000011111*QR_000002000001+P_000011211*QR_000002000002+P_000111011*QR_000002000010+P_000111111*QR_000002000011+P_000111211*QR_000002000012+P_000211011*QR_000002000020+P_000211111*QR_000002000021+P_000211211*QR_000002000022); ans_temp[ans_id*6+4]+=Pmtrx[27]*(P_000011011*QR_001000001000+P_000011111*QR_001000001001+P_000011211*QR_001000001002+P_000111011*QR_001000001010+P_000111111*QR_001000001011+P_000111211*QR_001000001012+P_000211011*QR_001000001020+P_000211111*QR_001000001021+P_000211211*QR_001000001022); ans_temp[ans_id*6+4]+=Pmtrx[28]*(P_000011011*QR_000001001000+P_000011111*QR_000001001001+P_000011211*QR_000001001002+P_000111011*QR_000001001010+P_000111111*QR_000001001011+P_000111211*QR_000001001012+P_000211011*QR_000001001020+P_000211111*QR_000001001021+P_000211211*QR_000001001022); ans_temp[ans_id*6+4]+=Pmtrx[29]*(P_000011011*QR_000000002000+P_000011111*QR_000000002001+P_000011211*QR_000000002002+P_000111011*QR_000000002010+P_000111111*QR_000000002011+P_000111211*QR_000000002012+P_000211011*QR_000000002020+P_000211111*QR_000000002021+P_000211211*QR_000000002022); ans_temp[ans_id*6+4]+=Pmtrx[30]*(P_000010012*QR_002000000000+P_000010112*QR_002000000001+P_000010212*QR_002000000002+P_000010312*QR_002000000003+P_000110012*QR_002000000010+P_000110112*QR_002000000011+P_000110212*QR_002000000012+P_000110312*QR_002000000013); ans_temp[ans_id*6+4]+=Pmtrx[31]*(P_000010012*QR_001001000000+P_000010112*QR_001001000001+P_000010212*QR_001001000002+P_000010312*QR_001001000003+P_000110012*QR_001001000010+P_000110112*QR_001001000011+P_000110212*QR_001001000012+P_000110312*QR_001001000013); ans_temp[ans_id*6+4]+=Pmtrx[32]*(P_000010012*QR_000002000000+P_000010112*QR_000002000001+P_000010212*QR_000002000002+P_000010312*QR_000002000003+P_000110012*QR_000002000010+P_000110112*QR_000002000011+P_000110212*QR_000002000012+P_000110312*QR_000002000013); ans_temp[ans_id*6+4]+=Pmtrx[33]*(P_000010012*QR_001000001000+P_000010112*QR_001000001001+P_000010212*QR_001000001002+P_000010312*QR_001000001003+P_000110012*QR_001000001010+P_000110112*QR_001000001011+P_000110212*QR_001000001012+P_000110312*QR_001000001013); ans_temp[ans_id*6+4]+=Pmtrx[34]*(P_000010012*QR_000001001000+P_000010112*QR_000001001001+P_000010212*QR_000001001002+P_000010312*QR_000001001003+P_000110012*QR_000001001010+P_000110112*QR_000001001011+P_000110212*QR_000001001012+P_000110312*QR_000001001013); ans_temp[ans_id*6+4]+=Pmtrx[35]*(P_000010012*QR_000000002000+P_000010112*QR_000000002001+P_000010212*QR_000000002002+P_000010312*QR_000000002003+P_000110012*QR_000000002010+P_000110112*QR_000000002011+P_000110212*QR_000000002012+P_000110312*QR_000000002013); ans_temp[ans_id*6+5]+=Pmtrx[0]*(P_002000020*QR_002000000000+P_002000120*QR_002000000001+P_002000220*QR_002000000002+P_102000020*QR_002000000100+P_102000120*QR_002000000101+P_102000220*QR_002000000102+P_202000020*QR_002000000200+P_202000120*QR_002000000201+P_202000220*QR_002000000202); ans_temp[ans_id*6+5]+=Pmtrx[1]*(P_002000020*QR_001001000000+P_002000120*QR_001001000001+P_002000220*QR_001001000002+P_102000020*QR_001001000100+P_102000120*QR_001001000101+P_102000220*QR_001001000102+P_202000020*QR_001001000200+P_202000120*QR_001001000201+P_202000220*QR_001001000202); ans_temp[ans_id*6+5]+=Pmtrx[2]*(P_002000020*QR_000002000000+P_002000120*QR_000002000001+P_002000220*QR_000002000002+P_102000020*QR_000002000100+P_102000120*QR_000002000101+P_102000220*QR_000002000102+P_202000020*QR_000002000200+P_202000120*QR_000002000201+P_202000220*QR_000002000202); ans_temp[ans_id*6+5]+=Pmtrx[3]*(P_002000020*QR_001000001000+P_002000120*QR_001000001001+P_002000220*QR_001000001002+P_102000020*QR_001000001100+P_102000120*QR_001000001101+P_102000220*QR_001000001102+P_202000020*QR_001000001200+P_202000120*QR_001000001201+P_202000220*QR_001000001202); ans_temp[ans_id*6+5]+=Pmtrx[4]*(P_002000020*QR_000001001000+P_002000120*QR_000001001001+P_002000220*QR_000001001002+P_102000020*QR_000001001100+P_102000120*QR_000001001101+P_102000220*QR_000001001102+P_202000020*QR_000001001200+P_202000120*QR_000001001201+P_202000220*QR_000001001202); ans_temp[ans_id*6+5]+=Pmtrx[5]*(P_002000020*QR_000000002000+P_002000120*QR_000000002001+P_002000220*QR_000000002002+P_102000020*QR_000000002100+P_102000120*QR_000000002101+P_102000220*QR_000000002102+P_202000020*QR_000000002200+P_202000120*QR_000000002201+P_202000220*QR_000000002202); ans_temp[ans_id*6+5]+=Pmtrx[6]*(P_001001020*QR_002000000000+P_001001120*QR_002000000001+P_001001220*QR_002000000002+P_001101020*QR_002000000010+P_001101120*QR_002000000011+P_001101220*QR_002000000012+P_101001020*QR_002000000100+P_101001120*QR_002000000101+P_101001220*QR_002000000102+P_101101020*QR_002000000110+P_101101120*QR_002000000111+P_101101220*QR_002000000112); ans_temp[ans_id*6+5]+=Pmtrx[7]*(P_001001020*QR_001001000000+P_001001120*QR_001001000001+P_001001220*QR_001001000002+P_001101020*QR_001001000010+P_001101120*QR_001001000011+P_001101220*QR_001001000012+P_101001020*QR_001001000100+P_101001120*QR_001001000101+P_101001220*QR_001001000102+P_101101020*QR_001001000110+P_101101120*QR_001001000111+P_101101220*QR_001001000112); ans_temp[ans_id*6+5]+=Pmtrx[8]*(P_001001020*QR_000002000000+P_001001120*QR_000002000001+P_001001220*QR_000002000002+P_001101020*QR_000002000010+P_001101120*QR_000002000011+P_001101220*QR_000002000012+P_101001020*QR_000002000100+P_101001120*QR_000002000101+P_101001220*QR_000002000102+P_101101020*QR_000002000110+P_101101120*QR_000002000111+P_101101220*QR_000002000112); ans_temp[ans_id*6+5]+=Pmtrx[9]*(P_001001020*QR_001000001000+P_001001120*QR_001000001001+P_001001220*QR_001000001002+P_001101020*QR_001000001010+P_001101120*QR_001000001011+P_001101220*QR_001000001012+P_101001020*QR_001000001100+P_101001120*QR_001000001101+P_101001220*QR_001000001102+P_101101020*QR_001000001110+P_101101120*QR_001000001111+P_101101220*QR_001000001112); ans_temp[ans_id*6+5]+=Pmtrx[10]*(P_001001020*QR_000001001000+P_001001120*QR_000001001001+P_001001220*QR_000001001002+P_001101020*QR_000001001010+P_001101120*QR_000001001011+P_001101220*QR_000001001012+P_101001020*QR_000001001100+P_101001120*QR_000001001101+P_101001220*QR_000001001102+P_101101020*QR_000001001110+P_101101120*QR_000001001111+P_101101220*QR_000001001112); ans_temp[ans_id*6+5]+=Pmtrx[11]*(P_001001020*QR_000000002000+P_001001120*QR_000000002001+P_001001220*QR_000000002002+P_001101020*QR_000000002010+P_001101120*QR_000000002011+P_001101220*QR_000000002012+P_101001020*QR_000000002100+P_101001120*QR_000000002101+P_101001220*QR_000000002102+P_101101020*QR_000000002110+P_101101120*QR_000000002111+P_101101220*QR_000000002112); ans_temp[ans_id*6+5]+=Pmtrx[12]*(P_000002020*QR_002000000000+P_000002120*QR_002000000001+P_000002220*QR_002000000002+P_000102020*QR_002000000010+P_000102120*QR_002000000011+P_000102220*QR_002000000012+P_000202020*QR_002000000020+P_000202120*QR_002000000021+P_000202220*QR_002000000022); ans_temp[ans_id*6+5]+=Pmtrx[13]*(P_000002020*QR_001001000000+P_000002120*QR_001001000001+P_000002220*QR_001001000002+P_000102020*QR_001001000010+P_000102120*QR_001001000011+P_000102220*QR_001001000012+P_000202020*QR_001001000020+P_000202120*QR_001001000021+P_000202220*QR_001001000022); ans_temp[ans_id*6+5]+=Pmtrx[14]*(P_000002020*QR_000002000000+P_000002120*QR_000002000001+P_000002220*QR_000002000002+P_000102020*QR_000002000010+P_000102120*QR_000002000011+P_000102220*QR_000002000012+P_000202020*QR_000002000020+P_000202120*QR_000002000021+P_000202220*QR_000002000022); ans_temp[ans_id*6+5]+=Pmtrx[15]*(P_000002020*QR_001000001000+P_000002120*QR_001000001001+P_000002220*QR_001000001002+P_000102020*QR_001000001010+P_000102120*QR_001000001011+P_000102220*QR_001000001012+P_000202020*QR_001000001020+P_000202120*QR_001000001021+P_000202220*QR_001000001022); ans_temp[ans_id*6+5]+=Pmtrx[16]*(P_000002020*QR_000001001000+P_000002120*QR_000001001001+P_000002220*QR_000001001002+P_000102020*QR_000001001010+P_000102120*QR_000001001011+P_000102220*QR_000001001012+P_000202020*QR_000001001020+P_000202120*QR_000001001021+P_000202220*QR_000001001022); ans_temp[ans_id*6+5]+=Pmtrx[17]*(P_000002020*QR_000000002000+P_000002120*QR_000000002001+P_000002220*QR_000000002002+P_000102020*QR_000000002010+P_000102120*QR_000000002011+P_000102220*QR_000000002012+P_000202020*QR_000000002020+P_000202120*QR_000000002021+P_000202220*QR_000000002022); ans_temp[ans_id*6+5]+=Pmtrx[18]*(P_001000021*QR_002000000000+P_001000121*QR_002000000001+P_001000221*QR_002000000002+P_001000321*QR_002000000003+P_101000021*QR_002000000100+P_101000121*QR_002000000101+P_101000221*QR_002000000102+P_101000321*QR_002000000103); ans_temp[ans_id*6+5]+=Pmtrx[19]*(P_001000021*QR_001001000000+P_001000121*QR_001001000001+P_001000221*QR_001001000002+P_001000321*QR_001001000003+P_101000021*QR_001001000100+P_101000121*QR_001001000101+P_101000221*QR_001001000102+P_101000321*QR_001001000103); ans_temp[ans_id*6+5]+=Pmtrx[20]*(P_001000021*QR_000002000000+P_001000121*QR_000002000001+P_001000221*QR_000002000002+P_001000321*QR_000002000003+P_101000021*QR_000002000100+P_101000121*QR_000002000101+P_101000221*QR_000002000102+P_101000321*QR_000002000103); ans_temp[ans_id*6+5]+=Pmtrx[21]*(P_001000021*QR_001000001000+P_001000121*QR_001000001001+P_001000221*QR_001000001002+P_001000321*QR_001000001003+P_101000021*QR_001000001100+P_101000121*QR_001000001101+P_101000221*QR_001000001102+P_101000321*QR_001000001103); ans_temp[ans_id*6+5]+=Pmtrx[22]*(P_001000021*QR_000001001000+P_001000121*QR_000001001001+P_001000221*QR_000001001002+P_001000321*QR_000001001003+P_101000021*QR_000001001100+P_101000121*QR_000001001101+P_101000221*QR_000001001102+P_101000321*QR_000001001103); ans_temp[ans_id*6+5]+=Pmtrx[23]*(P_001000021*QR_000000002000+P_001000121*QR_000000002001+P_001000221*QR_000000002002+P_001000321*QR_000000002003+P_101000021*QR_000000002100+P_101000121*QR_000000002101+P_101000221*QR_000000002102+P_101000321*QR_000000002103); ans_temp[ans_id*6+5]+=Pmtrx[24]*(P_000001021*QR_002000000000+P_000001121*QR_002000000001+P_000001221*QR_002000000002+P_000001321*QR_002000000003+P_000101021*QR_002000000010+P_000101121*QR_002000000011+P_000101221*QR_002000000012+P_000101321*QR_002000000013); ans_temp[ans_id*6+5]+=Pmtrx[25]*(P_000001021*QR_001001000000+P_000001121*QR_001001000001+P_000001221*QR_001001000002+P_000001321*QR_001001000003+P_000101021*QR_001001000010+P_000101121*QR_001001000011+P_000101221*QR_001001000012+P_000101321*QR_001001000013); ans_temp[ans_id*6+5]+=Pmtrx[26]*(P_000001021*QR_000002000000+P_000001121*QR_000002000001+P_000001221*QR_000002000002+P_000001321*QR_000002000003+P_000101021*QR_000002000010+P_000101121*QR_000002000011+P_000101221*QR_000002000012+P_000101321*QR_000002000013); ans_temp[ans_id*6+5]+=Pmtrx[27]*(P_000001021*QR_001000001000+P_000001121*QR_001000001001+P_000001221*QR_001000001002+P_000001321*QR_001000001003+P_000101021*QR_001000001010+P_000101121*QR_001000001011+P_000101221*QR_001000001012+P_000101321*QR_001000001013); ans_temp[ans_id*6+5]+=Pmtrx[28]*(P_000001021*QR_000001001000+P_000001121*QR_000001001001+P_000001221*QR_000001001002+P_000001321*QR_000001001003+P_000101021*QR_000001001010+P_000101121*QR_000001001011+P_000101221*QR_000001001012+P_000101321*QR_000001001013); ans_temp[ans_id*6+5]+=Pmtrx[29]*(P_000001021*QR_000000002000+P_000001121*QR_000000002001+P_000001221*QR_000000002002+P_000001321*QR_000000002003+P_000101021*QR_000000002010+P_000101121*QR_000000002011+P_000101221*QR_000000002012+P_000101321*QR_000000002013); ans_temp[ans_id*6+5]+=Pmtrx[30]*(P_000000022*QR_002000000000+P_000000122*QR_002000000001+P_000000222*QR_002000000002+P_000000322*QR_002000000003+P_000000422*QR_002000000004); ans_temp[ans_id*6+5]+=Pmtrx[31]*(P_000000022*QR_001001000000+P_000000122*QR_001001000001+P_000000222*QR_001001000002+P_000000322*QR_001001000003+P_000000422*QR_001001000004); ans_temp[ans_id*6+5]+=Pmtrx[32]*(P_000000022*QR_000002000000+P_000000122*QR_000002000001+P_000000222*QR_000002000002+P_000000322*QR_000002000003+P_000000422*QR_000002000004); ans_temp[ans_id*6+5]+=Pmtrx[33]*(P_000000022*QR_001000001000+P_000000122*QR_001000001001+P_000000222*QR_001000001002+P_000000322*QR_001000001003+P_000000422*QR_001000001004); ans_temp[ans_id*6+5]+=Pmtrx[34]*(P_000000022*QR_000001001000+P_000000122*QR_000001001001+P_000000222*QR_000001001002+P_000000322*QR_000001001003+P_000000422*QR_000001001004); ans_temp[ans_id*6+5]+=Pmtrx[35]*(P_000000022*QR_000000002000+P_000000122*QR_000000002001+P_000000222*QR_000000002002+P_000000322*QR_000000002003+P_000000422*QR_000000002004); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } }
f8b7ff120e5d20d651907fe59315a7cda837492e.cu
#include<math.h> #include"Boys_gpu.h" #define PI 3.1415926535897932 #define P25 17.4934183276248620 #define NTHREAD 64 texture<int2,1,cudaReadModeElementType> tex_P; texture<int2,1,cudaReadModeElementType> tex_Zta; texture<int2,1,cudaReadModeElementType> tex_pp; texture<float,1,cudaReadModeElementType> tex_K2_p; texture<int2,1,cudaReadModeElementType> tex_PA; texture<int2,1,cudaReadModeElementType> tex_PB; texture<unsigned int,1,cudaReadModeElementType> tex_id_bra; texture<int2,1,cudaReadModeElementType> tex_Q; texture<int2,1,cudaReadModeElementType> tex_Eta; texture<int2,1,cudaReadModeElementType> tex_pq; texture<float,1,cudaReadModeElementType> tex_K2_q; texture<int2,1,cudaReadModeElementType> tex_QC; texture<int2,1,cudaReadModeElementType> tex_QD; texture<unsigned int,1,cudaReadModeElementType> tex_id_ket; void MD_texture_binding_bra_sd(double * P_d,double * PA_d,double * PB_d,\ double * alphaP_d,double * pp_d,float * K2_p_d,unsigned int * id_bra_d,\ unsigned int primit_len){ cudaBindTexture(0, tex_P, P_d, sizeof(double)*primit_len*3); cudaBindTexture(0, tex_Zta, alphaP_d, sizeof(double)*primit_len); cudaBindTexture(0, tex_pp, pp_d, sizeof(double)*primit_len); cudaBindTexture(0, tex_K2_p, K2_p_d, sizeof(float)*primit_len); cudaBindTexture(0, tex_PA, PA_d, sizeof(double)*primit_len*3); cudaBindTexture(0, tex_PB, PB_d, sizeof(double)*primit_len*3); cudaBindTexture(0, tex_id_bra, id_bra_d, sizeof(unsigned int)*primit_len); } void MD_texture_unbind_bra_sd(){ cudaUnbindTexture(tex_P); cudaUnbindTexture(tex_Zta); cudaUnbindTexture(tex_pp); cudaUnbindTexture(tex_K2_p); cudaUnbindTexture(tex_PA); cudaUnbindTexture(tex_PB); cudaUnbindTexture(tex_id_bra); } void MD_texture_binding_ket_sd(double * Q_d,double * QC_d,double * QD_d,\ double * alphaQ_d,double * pq_d,float * K2_q_d,unsigned int * id_ket_d,\ unsigned int primit_len){ cudaBindTexture(0, tex_Q, Q_d, sizeof(double)*primit_len*3); cudaBindTexture(0, tex_Eta, alphaQ_d, sizeof(double)*primit_len); cudaBindTexture(0, tex_pq, pq_d, sizeof(double)*primit_len); cudaBindTexture(0, tex_K2_q, K2_q_d, sizeof(float)*primit_len); cudaBindTexture(0, tex_QC, QC_d, sizeof(double)*primit_len*3); cudaBindTexture(0, tex_QD, QD_d, sizeof(double)*primit_len*3); cudaBindTexture(0, tex_id_ket, id_ket_d, sizeof(unsigned int)*primit_len); } void MD_texture_unbind_ket_sd(){ cudaUnbindTexture(tex_Q); cudaUnbindTexture(tex_Eta); cudaUnbindTexture(tex_pq); cudaUnbindTexture(tex_K2_q); cudaUnbindTexture(tex_QC); cudaUnbindTexture(tex_QD); cudaUnbindTexture(tex_id_ket); } __global__ void MD_Kp_sdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*1]; for(int i=0;i<1;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; if(i_contrc_bra>j_contrc_ket){ if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=0.0; } } continue; } for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=2*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[5]; Ft_fs_4(4,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[4]; double R_200[3]; double R_300[2]; double R_400[1]; double R_010[4]; double R_110[3]; double R_210[2]; double R_310[1]; double R_020[3]; double R_120[2]; double R_220[1]; double R_030[2]; double R_130[1]; double R_040[1]; double R_001[4]; double R_101[3]; double R_201[2]; double R_301[1]; double R_011[3]; double R_111[2]; double R_211[1]; double R_021[2]; double R_121[1]; double R_031[1]; double R_002[3]; double R_102[2]; double R_202[1]; double R_012[2]; double R_112[1]; double R_022[1]; double R_003[2]; double R_103[1]; double R_013[1]; double R_004[1]; for(int i=0;i<4;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<4;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<4;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<3;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<3;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<3;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<3;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<2;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<2;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<2;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<2;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<2;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<2;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<2;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<2;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<2;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<2;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<1;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<1;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<1;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<1;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<1;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<1;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<1;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<1;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<1;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<1;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<1;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<1;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<1;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } double P_002000000=Pd_002[0]; double P_102000000=Pd_102[0]; double P_202000000=Pd_202[0]; double P_001001000=Pd_001[0]*Pd_001[1]; double P_001101000=Pd_001[0]*Pd_101[1]; double P_101001000=Pd_101[0]*Pd_001[1]; double P_101101000=Pd_101[0]*Pd_101[1]; double P_000002000=Pd_002[1]; double P_000102000=Pd_102[1]; double P_000202000=Pd_202[1]; double P_001000001=Pd_001[0]*Pd_001[2]; double P_001000101=Pd_001[0]*Pd_101[2]; double P_101000001=Pd_101[0]*Pd_001[2]; double P_101000101=Pd_101[0]*Pd_101[2]; double P_000001001=Pd_001[1]*Pd_001[2]; double P_000001101=Pd_001[1]*Pd_101[2]; double P_000101001=Pd_101[1]*Pd_001[2]; double P_000101101=Pd_101[1]*Pd_101[2]; double P_000000002=Pd_002[2]; double P_000000102=Pd_102[2]; double P_000000202=Pd_202[2]; double PR_002000000000=P_002000000*R_000[0]+-1*P_102000000*R_100[0]+P_202000000*R_200[0]; double PR_001001000000=P_001001000*R_000[0]+-1*P_001101000*R_010[0]+-1*P_101001000*R_100[0]+P_101101000*R_110[0]; double PR_000002000000=P_000002000*R_000[0]+-1*P_000102000*R_010[0]+P_000202000*R_020[0]; double PR_001000001000=P_001000001*R_000[0]+-1*P_001000101*R_001[0]+-1*P_101000001*R_100[0]+P_101000101*R_101[0]; double PR_000001001000=P_000001001*R_000[0]+-1*P_000001101*R_001[0]+-1*P_000101001*R_010[0]+P_000101101*R_011[0]; double PR_000000002000=P_000000002*R_000[0]+-1*P_000000102*R_001[0]+P_000000202*R_002[0]; double PR_002000000001=P_002000000*R_001[0]+-1*P_102000000*R_101[0]+P_202000000*R_201[0]; double PR_001001000001=P_001001000*R_001[0]+-1*P_001101000*R_011[0]+-1*P_101001000*R_101[0]+P_101101000*R_111[0]; double PR_000002000001=P_000002000*R_001[0]+-1*P_000102000*R_011[0]+P_000202000*R_021[0]; double PR_001000001001=P_001000001*R_001[0]+-1*P_001000101*R_002[0]+-1*P_101000001*R_101[0]+P_101000101*R_102[0]; double PR_000001001001=P_000001001*R_001[0]+-1*P_000001101*R_002[0]+-1*P_000101001*R_011[0]+P_000101101*R_012[0]; double PR_000000002001=P_000000002*R_001[0]+-1*P_000000102*R_002[0]+P_000000202*R_003[0]; double PR_002000000010=P_002000000*R_010[0]+-1*P_102000000*R_110[0]+P_202000000*R_210[0]; double PR_001001000010=P_001001000*R_010[0]+-1*P_001101000*R_020[0]+-1*P_101001000*R_110[0]+P_101101000*R_120[0]; double PR_000002000010=P_000002000*R_010[0]+-1*P_000102000*R_020[0]+P_000202000*R_030[0]; double PR_001000001010=P_001000001*R_010[0]+-1*P_001000101*R_011[0]+-1*P_101000001*R_110[0]+P_101000101*R_111[0]; double PR_000001001010=P_000001001*R_010[0]+-1*P_000001101*R_011[0]+-1*P_000101001*R_020[0]+P_000101101*R_021[0]; double PR_000000002010=P_000000002*R_010[0]+-1*P_000000102*R_011[0]+P_000000202*R_012[0]; double PR_002000000100=P_002000000*R_100[0]+-1*P_102000000*R_200[0]+P_202000000*R_300[0]; double PR_001001000100=P_001001000*R_100[0]+-1*P_001101000*R_110[0]+-1*P_101001000*R_200[0]+P_101101000*R_210[0]; double PR_000002000100=P_000002000*R_100[0]+-1*P_000102000*R_110[0]+P_000202000*R_120[0]; double PR_001000001100=P_001000001*R_100[0]+-1*P_001000101*R_101[0]+-1*P_101000001*R_200[0]+P_101000101*R_201[0]; double PR_000001001100=P_000001001*R_100[0]+-1*P_000001101*R_101[0]+-1*P_000101001*R_110[0]+P_000101101*R_111[0]; double PR_000000002100=P_000000002*R_100[0]+-1*P_000000102*R_101[0]+P_000000202*R_102[0]; double PR_002000000002=P_002000000*R_002[0]+-1*P_102000000*R_102[0]+P_202000000*R_202[0]; double PR_001001000002=P_001001000*R_002[0]+-1*P_001101000*R_012[0]+-1*P_101001000*R_102[0]+P_101101000*R_112[0]; double PR_000002000002=P_000002000*R_002[0]+-1*P_000102000*R_012[0]+P_000202000*R_022[0]; double PR_001000001002=P_001000001*R_002[0]+-1*P_001000101*R_003[0]+-1*P_101000001*R_102[0]+P_101000101*R_103[0]; double PR_000001001002=P_000001001*R_002[0]+-1*P_000001101*R_003[0]+-1*P_000101001*R_012[0]+P_000101101*R_013[0]; double PR_000000002002=P_000000002*R_002[0]+-1*P_000000102*R_003[0]+P_000000202*R_004[0]; double PR_002000000011=P_002000000*R_011[0]+-1*P_102000000*R_111[0]+P_202000000*R_211[0]; double PR_001001000011=P_001001000*R_011[0]+-1*P_001101000*R_021[0]+-1*P_101001000*R_111[0]+P_101101000*R_121[0]; double PR_000002000011=P_000002000*R_011[0]+-1*P_000102000*R_021[0]+P_000202000*R_031[0]; double PR_001000001011=P_001000001*R_011[0]+-1*P_001000101*R_012[0]+-1*P_101000001*R_111[0]+P_101000101*R_112[0]; double PR_000001001011=P_000001001*R_011[0]+-1*P_000001101*R_012[0]+-1*P_000101001*R_021[0]+P_000101101*R_022[0]; double PR_000000002011=P_000000002*R_011[0]+-1*P_000000102*R_012[0]+P_000000202*R_013[0]; double PR_002000000020=P_002000000*R_020[0]+-1*P_102000000*R_120[0]+P_202000000*R_220[0]; double PR_001001000020=P_001001000*R_020[0]+-1*P_001101000*R_030[0]+-1*P_101001000*R_120[0]+P_101101000*R_130[0]; double PR_000002000020=P_000002000*R_020[0]+-1*P_000102000*R_030[0]+P_000202000*R_040[0]; double PR_001000001020=P_001000001*R_020[0]+-1*P_001000101*R_021[0]+-1*P_101000001*R_120[0]+P_101000101*R_121[0]; double PR_000001001020=P_000001001*R_020[0]+-1*P_000001101*R_021[0]+-1*P_000101001*R_030[0]+P_000101101*R_031[0]; double PR_000000002020=P_000000002*R_020[0]+-1*P_000000102*R_021[0]+P_000000202*R_022[0]; double PR_002000000101=P_002000000*R_101[0]+-1*P_102000000*R_201[0]+P_202000000*R_301[0]; double PR_001001000101=P_001001000*R_101[0]+-1*P_001101000*R_111[0]+-1*P_101001000*R_201[0]+P_101101000*R_211[0]; double PR_000002000101=P_000002000*R_101[0]+-1*P_000102000*R_111[0]+P_000202000*R_121[0]; double PR_001000001101=P_001000001*R_101[0]+-1*P_001000101*R_102[0]+-1*P_101000001*R_201[0]+P_101000101*R_202[0]; double PR_000001001101=P_000001001*R_101[0]+-1*P_000001101*R_102[0]+-1*P_000101001*R_111[0]+P_000101101*R_112[0]; double PR_000000002101=P_000000002*R_101[0]+-1*P_000000102*R_102[0]+P_000000202*R_103[0]; double PR_002000000110=P_002000000*R_110[0]+-1*P_102000000*R_210[0]+P_202000000*R_310[0]; double PR_001001000110=P_001001000*R_110[0]+-1*P_001101000*R_120[0]+-1*P_101001000*R_210[0]+P_101101000*R_220[0]; double PR_000002000110=P_000002000*R_110[0]+-1*P_000102000*R_120[0]+P_000202000*R_130[0]; double PR_001000001110=P_001000001*R_110[0]+-1*P_001000101*R_111[0]+-1*P_101000001*R_210[0]+P_101000101*R_211[0]; double PR_000001001110=P_000001001*R_110[0]+-1*P_000001101*R_111[0]+-1*P_000101001*R_120[0]+P_000101101*R_121[0]; double PR_000000002110=P_000000002*R_110[0]+-1*P_000000102*R_111[0]+P_000000202*R_112[0]; double PR_002000000200=P_002000000*R_200[0]+-1*P_102000000*R_300[0]+P_202000000*R_400[0]; double PR_001001000200=P_001001000*R_200[0]+-1*P_001101000*R_210[0]+-1*P_101001000*R_300[0]+P_101101000*R_310[0]; double PR_000002000200=P_000002000*R_200[0]+-1*P_000102000*R_210[0]+P_000202000*R_220[0]; double PR_001000001200=P_001000001*R_200[0]+-1*P_001000101*R_201[0]+-1*P_101000001*R_300[0]+P_101000101*R_301[0]; double PR_000001001200=P_000001001*R_200[0]+-1*P_000001101*R_201[0]+-1*P_000101001*R_210[0]+P_000101101*R_211[0]; double PR_000000002200=P_000000002*R_200[0]+-1*P_000000102*R_201[0]+P_000000202*R_202[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*1+0]+=Pmtrx[0]*(Q_002000000*PR_002000000000+Q_102000000*PR_002000000100+Q_202000000*PR_002000000200); ans_temp[ans_id*1+0]+=Pmtrx[1]*(Q_001001000*PR_002000000000+Q_001101000*PR_002000000010+Q_101001000*PR_002000000100+Q_101101000*PR_002000000110); ans_temp[ans_id*1+0]+=Pmtrx[2]*(Q_000002000*PR_002000000000+Q_000102000*PR_002000000010+Q_000202000*PR_002000000020); ans_temp[ans_id*1+0]+=Pmtrx[3]*(Q_001000001*PR_002000000000+Q_001000101*PR_002000000001+Q_101000001*PR_002000000100+Q_101000101*PR_002000000101); ans_temp[ans_id*1+0]+=Pmtrx[4]*(Q_000001001*PR_002000000000+Q_000001101*PR_002000000001+Q_000101001*PR_002000000010+Q_000101101*PR_002000000011); ans_temp[ans_id*1+0]+=Pmtrx[5]*(Q_000000002*PR_002000000000+Q_000000102*PR_002000000001+Q_000000202*PR_002000000002); ans_temp[ans_id*1+0]+=Pmtrx[6]*(Q_002000000*PR_001001000000+Q_102000000*PR_001001000100+Q_202000000*PR_001001000200); ans_temp[ans_id*1+0]+=Pmtrx[7]*(Q_001001000*PR_001001000000+Q_001101000*PR_001001000010+Q_101001000*PR_001001000100+Q_101101000*PR_001001000110); ans_temp[ans_id*1+0]+=Pmtrx[8]*(Q_000002000*PR_001001000000+Q_000102000*PR_001001000010+Q_000202000*PR_001001000020); ans_temp[ans_id*1+0]+=Pmtrx[9]*(Q_001000001*PR_001001000000+Q_001000101*PR_001001000001+Q_101000001*PR_001001000100+Q_101000101*PR_001001000101); ans_temp[ans_id*1+0]+=Pmtrx[10]*(Q_000001001*PR_001001000000+Q_000001101*PR_001001000001+Q_000101001*PR_001001000010+Q_000101101*PR_001001000011); ans_temp[ans_id*1+0]+=Pmtrx[11]*(Q_000000002*PR_001001000000+Q_000000102*PR_001001000001+Q_000000202*PR_001001000002); ans_temp[ans_id*1+0]+=Pmtrx[12]*(Q_002000000*PR_000002000000+Q_102000000*PR_000002000100+Q_202000000*PR_000002000200); ans_temp[ans_id*1+0]+=Pmtrx[13]*(Q_001001000*PR_000002000000+Q_001101000*PR_000002000010+Q_101001000*PR_000002000100+Q_101101000*PR_000002000110); ans_temp[ans_id*1+0]+=Pmtrx[14]*(Q_000002000*PR_000002000000+Q_000102000*PR_000002000010+Q_000202000*PR_000002000020); ans_temp[ans_id*1+0]+=Pmtrx[15]*(Q_001000001*PR_000002000000+Q_001000101*PR_000002000001+Q_101000001*PR_000002000100+Q_101000101*PR_000002000101); ans_temp[ans_id*1+0]+=Pmtrx[16]*(Q_000001001*PR_000002000000+Q_000001101*PR_000002000001+Q_000101001*PR_000002000010+Q_000101101*PR_000002000011); ans_temp[ans_id*1+0]+=Pmtrx[17]*(Q_000000002*PR_000002000000+Q_000000102*PR_000002000001+Q_000000202*PR_000002000002); ans_temp[ans_id*1+0]+=Pmtrx[18]*(Q_002000000*PR_001000001000+Q_102000000*PR_001000001100+Q_202000000*PR_001000001200); ans_temp[ans_id*1+0]+=Pmtrx[19]*(Q_001001000*PR_001000001000+Q_001101000*PR_001000001010+Q_101001000*PR_001000001100+Q_101101000*PR_001000001110); ans_temp[ans_id*1+0]+=Pmtrx[20]*(Q_000002000*PR_001000001000+Q_000102000*PR_001000001010+Q_000202000*PR_001000001020); ans_temp[ans_id*1+0]+=Pmtrx[21]*(Q_001000001*PR_001000001000+Q_001000101*PR_001000001001+Q_101000001*PR_001000001100+Q_101000101*PR_001000001101); ans_temp[ans_id*1+0]+=Pmtrx[22]*(Q_000001001*PR_001000001000+Q_000001101*PR_001000001001+Q_000101001*PR_001000001010+Q_000101101*PR_001000001011); ans_temp[ans_id*1+0]+=Pmtrx[23]*(Q_000000002*PR_001000001000+Q_000000102*PR_001000001001+Q_000000202*PR_001000001002); ans_temp[ans_id*1+0]+=Pmtrx[24]*(Q_002000000*PR_000001001000+Q_102000000*PR_000001001100+Q_202000000*PR_000001001200); ans_temp[ans_id*1+0]+=Pmtrx[25]*(Q_001001000*PR_000001001000+Q_001101000*PR_000001001010+Q_101001000*PR_000001001100+Q_101101000*PR_000001001110); ans_temp[ans_id*1+0]+=Pmtrx[26]*(Q_000002000*PR_000001001000+Q_000102000*PR_000001001010+Q_000202000*PR_000001001020); ans_temp[ans_id*1+0]+=Pmtrx[27]*(Q_001000001*PR_000001001000+Q_001000101*PR_000001001001+Q_101000001*PR_000001001100+Q_101000101*PR_000001001101); ans_temp[ans_id*1+0]+=Pmtrx[28]*(Q_000001001*PR_000001001000+Q_000001101*PR_000001001001+Q_000101001*PR_000001001010+Q_000101101*PR_000001001011); ans_temp[ans_id*1+0]+=Pmtrx[29]*(Q_000000002*PR_000001001000+Q_000000102*PR_000001001001+Q_000000202*PR_000001001002); ans_temp[ans_id*1+0]+=Pmtrx[30]*(Q_002000000*PR_000000002000+Q_102000000*PR_000000002100+Q_202000000*PR_000000002200); ans_temp[ans_id*1+0]+=Pmtrx[31]*(Q_001001000*PR_000000002000+Q_001101000*PR_000000002010+Q_101001000*PR_000000002100+Q_101101000*PR_000000002110); ans_temp[ans_id*1+0]+=Pmtrx[32]*(Q_000002000*PR_000000002000+Q_000102000*PR_000000002010+Q_000202000*PR_000000002020); ans_temp[ans_id*1+0]+=Pmtrx[33]*(Q_001000001*PR_000000002000+Q_001000101*PR_000000002001+Q_101000001*PR_000000002100+Q_101000101*PR_000000002101); ans_temp[ans_id*1+0]+=Pmtrx[34]*(Q_000001001*PR_000000002000+Q_000001101*PR_000000002001+Q_000101001*PR_000000002010+Q_000101101*PR_000000002011); ans_temp[ans_id*1+0]+=Pmtrx[35]*(Q_000000002*PR_000000002000+Q_000000102*PR_000000002001+Q_000000202*PR_000000002002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<1;ians++){ ans_temp[tId_x*1+ians]+=ans_temp[(tId_x+num_thread)*1+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=ans_temp[(tId_x)*1+ians]; } } } } } __global__ void MD_Kq_sdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*1]; for(int i=0;i<1;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; if(i_contrc_bra>j_contrc_ket){ if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=0.0; } } continue; } for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=2*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[5]; Ft_fs_4(4,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[4]; double R_200[3]; double R_300[2]; double R_400[1]; double R_010[4]; double R_110[3]; double R_210[2]; double R_310[1]; double R_020[3]; double R_120[2]; double R_220[1]; double R_030[2]; double R_130[1]; double R_040[1]; double R_001[4]; double R_101[3]; double R_201[2]; double R_301[1]; double R_011[3]; double R_111[2]; double R_211[1]; double R_021[2]; double R_121[1]; double R_031[1]; double R_002[3]; double R_102[2]; double R_202[1]; double R_012[2]; double R_112[1]; double R_022[1]; double R_003[2]; double R_103[1]; double R_013[1]; double R_004[1]; for(int i=0;i<4;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<4;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<4;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<3;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<3;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<3;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<3;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<2;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<2;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<2;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<2;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<2;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<2;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<2;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<2;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<2;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<2;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<1;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<1;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<1;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<1;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<1;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<1;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<1;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<1;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<1;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<1;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<1;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<1;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<1;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<1;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } double P_002000000=Pd_002[0]; double P_102000000=Pd_102[0]; double P_202000000=Pd_202[0]; double P_001001000=Pd_001[0]*Pd_001[1]; double P_001101000=Pd_001[0]*Pd_101[1]; double P_101001000=Pd_101[0]*Pd_001[1]; double P_101101000=Pd_101[0]*Pd_101[1]; double P_000002000=Pd_002[1]; double P_000102000=Pd_102[1]; double P_000202000=Pd_202[1]; double P_001000001=Pd_001[0]*Pd_001[2]; double P_001000101=Pd_001[0]*Pd_101[2]; double P_101000001=Pd_101[0]*Pd_001[2]; double P_101000101=Pd_101[0]*Pd_101[2]; double P_000001001=Pd_001[1]*Pd_001[2]; double P_000001101=Pd_001[1]*Pd_101[2]; double P_000101001=Pd_101[1]*Pd_001[2]; double P_000101101=Pd_101[1]*Pd_101[2]; double P_000000002=Pd_002[2]; double P_000000102=Pd_102[2]; double P_000000202=Pd_202[2]; ans_temp[ans_id*1+0]+=Pmtrx[0]*(P_002000000*QR_002000000000+P_102000000*QR_002000000100+P_202000000*QR_002000000200); ans_temp[ans_id*1+0]+=Pmtrx[1]*(P_002000000*QR_001001000000+P_102000000*QR_001001000100+P_202000000*QR_001001000200); ans_temp[ans_id*1+0]+=Pmtrx[2]*(P_002000000*QR_000002000000+P_102000000*QR_000002000100+P_202000000*QR_000002000200); ans_temp[ans_id*1+0]+=Pmtrx[3]*(P_002000000*QR_001000001000+P_102000000*QR_001000001100+P_202000000*QR_001000001200); ans_temp[ans_id*1+0]+=Pmtrx[4]*(P_002000000*QR_000001001000+P_102000000*QR_000001001100+P_202000000*QR_000001001200); ans_temp[ans_id*1+0]+=Pmtrx[5]*(P_002000000*QR_000000002000+P_102000000*QR_000000002100+P_202000000*QR_000000002200); ans_temp[ans_id*1+0]+=Pmtrx[6]*(P_001001000*QR_002000000000+P_001101000*QR_002000000010+P_101001000*QR_002000000100+P_101101000*QR_002000000110); ans_temp[ans_id*1+0]+=Pmtrx[7]*(P_001001000*QR_001001000000+P_001101000*QR_001001000010+P_101001000*QR_001001000100+P_101101000*QR_001001000110); ans_temp[ans_id*1+0]+=Pmtrx[8]*(P_001001000*QR_000002000000+P_001101000*QR_000002000010+P_101001000*QR_000002000100+P_101101000*QR_000002000110); ans_temp[ans_id*1+0]+=Pmtrx[9]*(P_001001000*QR_001000001000+P_001101000*QR_001000001010+P_101001000*QR_001000001100+P_101101000*QR_001000001110); ans_temp[ans_id*1+0]+=Pmtrx[10]*(P_001001000*QR_000001001000+P_001101000*QR_000001001010+P_101001000*QR_000001001100+P_101101000*QR_000001001110); ans_temp[ans_id*1+0]+=Pmtrx[11]*(P_001001000*QR_000000002000+P_001101000*QR_000000002010+P_101001000*QR_000000002100+P_101101000*QR_000000002110); ans_temp[ans_id*1+0]+=Pmtrx[12]*(P_000002000*QR_002000000000+P_000102000*QR_002000000010+P_000202000*QR_002000000020); ans_temp[ans_id*1+0]+=Pmtrx[13]*(P_000002000*QR_001001000000+P_000102000*QR_001001000010+P_000202000*QR_001001000020); ans_temp[ans_id*1+0]+=Pmtrx[14]*(P_000002000*QR_000002000000+P_000102000*QR_000002000010+P_000202000*QR_000002000020); ans_temp[ans_id*1+0]+=Pmtrx[15]*(P_000002000*QR_001000001000+P_000102000*QR_001000001010+P_000202000*QR_001000001020); ans_temp[ans_id*1+0]+=Pmtrx[16]*(P_000002000*QR_000001001000+P_000102000*QR_000001001010+P_000202000*QR_000001001020); ans_temp[ans_id*1+0]+=Pmtrx[17]*(P_000002000*QR_000000002000+P_000102000*QR_000000002010+P_000202000*QR_000000002020); ans_temp[ans_id*1+0]+=Pmtrx[18]*(P_001000001*QR_002000000000+P_001000101*QR_002000000001+P_101000001*QR_002000000100+P_101000101*QR_002000000101); ans_temp[ans_id*1+0]+=Pmtrx[19]*(P_001000001*QR_001001000000+P_001000101*QR_001001000001+P_101000001*QR_001001000100+P_101000101*QR_001001000101); ans_temp[ans_id*1+0]+=Pmtrx[20]*(P_001000001*QR_000002000000+P_001000101*QR_000002000001+P_101000001*QR_000002000100+P_101000101*QR_000002000101); ans_temp[ans_id*1+0]+=Pmtrx[21]*(P_001000001*QR_001000001000+P_001000101*QR_001000001001+P_101000001*QR_001000001100+P_101000101*QR_001000001101); ans_temp[ans_id*1+0]+=Pmtrx[22]*(P_001000001*QR_000001001000+P_001000101*QR_000001001001+P_101000001*QR_000001001100+P_101000101*QR_000001001101); ans_temp[ans_id*1+0]+=Pmtrx[23]*(P_001000001*QR_000000002000+P_001000101*QR_000000002001+P_101000001*QR_000000002100+P_101000101*QR_000000002101); ans_temp[ans_id*1+0]+=Pmtrx[24]*(P_000001001*QR_002000000000+P_000001101*QR_002000000001+P_000101001*QR_002000000010+P_000101101*QR_002000000011); ans_temp[ans_id*1+0]+=Pmtrx[25]*(P_000001001*QR_001001000000+P_000001101*QR_001001000001+P_000101001*QR_001001000010+P_000101101*QR_001001000011); ans_temp[ans_id*1+0]+=Pmtrx[26]*(P_000001001*QR_000002000000+P_000001101*QR_000002000001+P_000101001*QR_000002000010+P_000101101*QR_000002000011); ans_temp[ans_id*1+0]+=Pmtrx[27]*(P_000001001*QR_001000001000+P_000001101*QR_001000001001+P_000101001*QR_001000001010+P_000101101*QR_001000001011); ans_temp[ans_id*1+0]+=Pmtrx[28]*(P_000001001*QR_000001001000+P_000001101*QR_000001001001+P_000101001*QR_000001001010+P_000101101*QR_000001001011); ans_temp[ans_id*1+0]+=Pmtrx[29]*(P_000001001*QR_000000002000+P_000001101*QR_000000002001+P_000101001*QR_000000002010+P_000101101*QR_000000002011); ans_temp[ans_id*1+0]+=Pmtrx[30]*(P_000000002*QR_002000000000+P_000000102*QR_002000000001+P_000000202*QR_002000000002); ans_temp[ans_id*1+0]+=Pmtrx[31]*(P_000000002*QR_001001000000+P_000000102*QR_001001000001+P_000000202*QR_001001000002); ans_temp[ans_id*1+0]+=Pmtrx[32]*(P_000000002*QR_000002000000+P_000000102*QR_000002000001+P_000000202*QR_000002000002); ans_temp[ans_id*1+0]+=Pmtrx[33]*(P_000000002*QR_001000001000+P_000000102*QR_001000001001+P_000000202*QR_001000001002); ans_temp[ans_id*1+0]+=Pmtrx[34]*(P_000000002*QR_000001001000+P_000000102*QR_000001001001+P_000000202*QR_000001001002); ans_temp[ans_id*1+0]+=Pmtrx[35]*(P_000000002*QR_000000002000+P_000000102*QR_000000002001+P_000000202*QR_000000002002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<1;ians++){ ans_temp[tId_x*1+ians]+=ans_temp[(tId_x+num_thread)*1+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<1;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*1+ians]=ans_temp[(tId_x)*1+ians]; } } } } } __global__ void MD_Kp_pdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*3]; for(int i=0;i<3;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_010[3]; temp_int2=tex1Dfetch(tex_PA,jj*3+0); Pd_010[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+1); Pd_010[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+2); Pd_010[2]=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } double P_012000000=Pd_012[0]; double P_112000000=Pd_112[0]; double P_212000000=Pd_212[0]; double P_312000000=Pd_312[0]; double P_011001000=Pd_011[0]*Pd_001[1]; double P_011101000=Pd_011[0]*Pd_101[1]; double P_111001000=Pd_111[0]*Pd_001[1]; double P_111101000=Pd_111[0]*Pd_101[1]; double P_211001000=Pd_211[0]*Pd_001[1]; double P_211101000=Pd_211[0]*Pd_101[1]; double P_010002000=Pd_010[0]*Pd_002[1]; double P_010102000=Pd_010[0]*Pd_102[1]; double P_010202000=Pd_010[0]*Pd_202[1]; double P_110002000=Pd_110[0]*Pd_002[1]; double P_110102000=Pd_110[0]*Pd_102[1]; double P_110202000=Pd_110[0]*Pd_202[1]; double P_011000001=Pd_011[0]*Pd_001[2]; double P_011000101=Pd_011[0]*Pd_101[2]; double P_111000001=Pd_111[0]*Pd_001[2]; double P_111000101=Pd_111[0]*Pd_101[2]; double P_211000001=Pd_211[0]*Pd_001[2]; double P_211000101=Pd_211[0]*Pd_101[2]; double P_010001001=Pd_010[0]*Pd_001[1]*Pd_001[2]; double P_010001101=Pd_010[0]*Pd_001[1]*Pd_101[2]; double P_010101001=Pd_010[0]*Pd_101[1]*Pd_001[2]; double P_010101101=Pd_010[0]*Pd_101[1]*Pd_101[2]; double P_110001001=Pd_110[0]*Pd_001[1]*Pd_001[2]; double P_110001101=Pd_110[0]*Pd_001[1]*Pd_101[2]; double P_110101001=Pd_110[0]*Pd_101[1]*Pd_001[2]; double P_110101101=Pd_110[0]*Pd_101[1]*Pd_101[2]; double P_010000002=Pd_010[0]*Pd_002[2]; double P_010000102=Pd_010[0]*Pd_102[2]; double P_010000202=Pd_010[0]*Pd_202[2]; double P_110000002=Pd_110[0]*Pd_002[2]; double P_110000102=Pd_110[0]*Pd_102[2]; double P_110000202=Pd_110[0]*Pd_202[2]; double P_002010000=Pd_002[0]*Pd_010[1]; double P_002110000=Pd_002[0]*Pd_110[1]; double P_102010000=Pd_102[0]*Pd_010[1]; double P_102110000=Pd_102[0]*Pd_110[1]; double P_202010000=Pd_202[0]*Pd_010[1]; double P_202110000=Pd_202[0]*Pd_110[1]; double P_001011000=Pd_001[0]*Pd_011[1]; double P_001111000=Pd_001[0]*Pd_111[1]; double P_001211000=Pd_001[0]*Pd_211[1]; double P_101011000=Pd_101[0]*Pd_011[1]; double P_101111000=Pd_101[0]*Pd_111[1]; double P_101211000=Pd_101[0]*Pd_211[1]; double P_000012000=Pd_012[1]; double P_000112000=Pd_112[1]; double P_000212000=Pd_212[1]; double P_000312000=Pd_312[1]; double P_001010001=Pd_001[0]*Pd_010[1]*Pd_001[2]; double P_001010101=Pd_001[0]*Pd_010[1]*Pd_101[2]; double P_001110001=Pd_001[0]*Pd_110[1]*Pd_001[2]; double P_001110101=Pd_001[0]*Pd_110[1]*Pd_101[2]; double P_101010001=Pd_101[0]*Pd_010[1]*Pd_001[2]; double P_101010101=Pd_101[0]*Pd_010[1]*Pd_101[2]; double P_101110001=Pd_101[0]*Pd_110[1]*Pd_001[2]; double P_101110101=Pd_101[0]*Pd_110[1]*Pd_101[2]; double P_000011001=Pd_011[1]*Pd_001[2]; double P_000011101=Pd_011[1]*Pd_101[2]; double P_000111001=Pd_111[1]*Pd_001[2]; double P_000111101=Pd_111[1]*Pd_101[2]; double P_000211001=Pd_211[1]*Pd_001[2]; double P_000211101=Pd_211[1]*Pd_101[2]; double P_000010002=Pd_010[1]*Pd_002[2]; double P_000010102=Pd_010[1]*Pd_102[2]; double P_000010202=Pd_010[1]*Pd_202[2]; double P_000110002=Pd_110[1]*Pd_002[2]; double P_000110102=Pd_110[1]*Pd_102[2]; double P_000110202=Pd_110[1]*Pd_202[2]; double P_002000010=Pd_002[0]*Pd_010[2]; double P_002000110=Pd_002[0]*Pd_110[2]; double P_102000010=Pd_102[0]*Pd_010[2]; double P_102000110=Pd_102[0]*Pd_110[2]; double P_202000010=Pd_202[0]*Pd_010[2]; double P_202000110=Pd_202[0]*Pd_110[2]; double P_001001010=Pd_001[0]*Pd_001[1]*Pd_010[2]; double P_001001110=Pd_001[0]*Pd_001[1]*Pd_110[2]; double P_001101010=Pd_001[0]*Pd_101[1]*Pd_010[2]; double P_001101110=Pd_001[0]*Pd_101[1]*Pd_110[2]; double P_101001010=Pd_101[0]*Pd_001[1]*Pd_010[2]; double P_101001110=Pd_101[0]*Pd_001[1]*Pd_110[2]; double P_101101010=Pd_101[0]*Pd_101[1]*Pd_010[2]; double P_101101110=Pd_101[0]*Pd_101[1]*Pd_110[2]; double P_000002010=Pd_002[1]*Pd_010[2]; double P_000002110=Pd_002[1]*Pd_110[2]; double P_000102010=Pd_102[1]*Pd_010[2]; double P_000102110=Pd_102[1]*Pd_110[2]; double P_000202010=Pd_202[1]*Pd_010[2]; double P_000202110=Pd_202[1]*Pd_110[2]; double P_001000011=Pd_001[0]*Pd_011[2]; double P_001000111=Pd_001[0]*Pd_111[2]; double P_001000211=Pd_001[0]*Pd_211[2]; double P_101000011=Pd_101[0]*Pd_011[2]; double P_101000111=Pd_101[0]*Pd_111[2]; double P_101000211=Pd_101[0]*Pd_211[2]; double P_000001011=Pd_001[1]*Pd_011[2]; double P_000001111=Pd_001[1]*Pd_111[2]; double P_000001211=Pd_001[1]*Pd_211[2]; double P_000101011=Pd_101[1]*Pd_011[2]; double P_000101111=Pd_101[1]*Pd_111[2]; double P_000101211=Pd_101[1]*Pd_211[2]; double P_000000012=Pd_012[2]; double P_000000112=Pd_112[2]; double P_000000212=Pd_212[2]; double P_000000312=Pd_312[2]; double PR_012000000000=P_012000000*R_000[0]+-1*P_112000000*R_100[0]+P_212000000*R_200[0]+-1*P_312000000*R_300[0]; double PR_011001000000=P_011001000*R_000[0]+-1*P_011101000*R_010[0]+-1*P_111001000*R_100[0]+P_111101000*R_110[0]+P_211001000*R_200[0]+-1*P_211101000*R_210[0]; double PR_010002000000=P_010002000*R_000[0]+-1*P_010102000*R_010[0]+P_010202000*R_020[0]+-1*P_110002000*R_100[0]+P_110102000*R_110[0]+-1*P_110202000*R_120[0]; double PR_011000001000=P_011000001*R_000[0]+-1*P_011000101*R_001[0]+-1*P_111000001*R_100[0]+P_111000101*R_101[0]+P_211000001*R_200[0]+-1*P_211000101*R_201[0]; double PR_010001001000=P_010001001*R_000[0]+-1*P_010001101*R_001[0]+-1*P_010101001*R_010[0]+P_010101101*R_011[0]+-1*P_110001001*R_100[0]+P_110001101*R_101[0]+P_110101001*R_110[0]+-1*P_110101101*R_111[0]; double PR_010000002000=P_010000002*R_000[0]+-1*P_010000102*R_001[0]+P_010000202*R_002[0]+-1*P_110000002*R_100[0]+P_110000102*R_101[0]+-1*P_110000202*R_102[0]; double PR_002010000000=P_002010000*R_000[0]+-1*P_002110000*R_010[0]+-1*P_102010000*R_100[0]+P_102110000*R_110[0]+P_202010000*R_200[0]+-1*P_202110000*R_210[0]; double PR_001011000000=P_001011000*R_000[0]+-1*P_001111000*R_010[0]+P_001211000*R_020[0]+-1*P_101011000*R_100[0]+P_101111000*R_110[0]+-1*P_101211000*R_120[0]; double PR_000012000000=P_000012000*R_000[0]+-1*P_000112000*R_010[0]+P_000212000*R_020[0]+-1*P_000312000*R_030[0]; double PR_001010001000=P_001010001*R_000[0]+-1*P_001010101*R_001[0]+-1*P_001110001*R_010[0]+P_001110101*R_011[0]+-1*P_101010001*R_100[0]+P_101010101*R_101[0]+P_101110001*R_110[0]+-1*P_101110101*R_111[0]; double PR_000011001000=P_000011001*R_000[0]+-1*P_000011101*R_001[0]+-1*P_000111001*R_010[0]+P_000111101*R_011[0]+P_000211001*R_020[0]+-1*P_000211101*R_021[0]; double PR_000010002000=P_000010002*R_000[0]+-1*P_000010102*R_001[0]+P_000010202*R_002[0]+-1*P_000110002*R_010[0]+P_000110102*R_011[0]+-1*P_000110202*R_012[0]; double PR_002000010000=P_002000010*R_000[0]+-1*P_002000110*R_001[0]+-1*P_102000010*R_100[0]+P_102000110*R_101[0]+P_202000010*R_200[0]+-1*P_202000110*R_201[0]; double PR_001001010000=P_001001010*R_000[0]+-1*P_001001110*R_001[0]+-1*P_001101010*R_010[0]+P_001101110*R_011[0]+-1*P_101001010*R_100[0]+P_101001110*R_101[0]+P_101101010*R_110[0]+-1*P_101101110*R_111[0]; double PR_000002010000=P_000002010*R_000[0]+-1*P_000002110*R_001[0]+-1*P_000102010*R_010[0]+P_000102110*R_011[0]+P_000202010*R_020[0]+-1*P_000202110*R_021[0]; double PR_001000011000=P_001000011*R_000[0]+-1*P_001000111*R_001[0]+P_001000211*R_002[0]+-1*P_101000011*R_100[0]+P_101000111*R_101[0]+-1*P_101000211*R_102[0]; double PR_000001011000=P_000001011*R_000[0]+-1*P_000001111*R_001[0]+P_000001211*R_002[0]+-1*P_000101011*R_010[0]+P_000101111*R_011[0]+-1*P_000101211*R_012[0]; double PR_000000012000=P_000000012*R_000[0]+-1*P_000000112*R_001[0]+P_000000212*R_002[0]+-1*P_000000312*R_003[0]; double PR_012000000001=P_012000000*R_001[0]+-1*P_112000000*R_101[0]+P_212000000*R_201[0]+-1*P_312000000*R_301[0]; double PR_011001000001=P_011001000*R_001[0]+-1*P_011101000*R_011[0]+-1*P_111001000*R_101[0]+P_111101000*R_111[0]+P_211001000*R_201[0]+-1*P_211101000*R_211[0]; double PR_010002000001=P_010002000*R_001[0]+-1*P_010102000*R_011[0]+P_010202000*R_021[0]+-1*P_110002000*R_101[0]+P_110102000*R_111[0]+-1*P_110202000*R_121[0]; double PR_011000001001=P_011000001*R_001[0]+-1*P_011000101*R_002[0]+-1*P_111000001*R_101[0]+P_111000101*R_102[0]+P_211000001*R_201[0]+-1*P_211000101*R_202[0]; double PR_010001001001=P_010001001*R_001[0]+-1*P_010001101*R_002[0]+-1*P_010101001*R_011[0]+P_010101101*R_012[0]+-1*P_110001001*R_101[0]+P_110001101*R_102[0]+P_110101001*R_111[0]+-1*P_110101101*R_112[0]; double PR_010000002001=P_010000002*R_001[0]+-1*P_010000102*R_002[0]+P_010000202*R_003[0]+-1*P_110000002*R_101[0]+P_110000102*R_102[0]+-1*P_110000202*R_103[0]; double PR_002010000001=P_002010000*R_001[0]+-1*P_002110000*R_011[0]+-1*P_102010000*R_101[0]+P_102110000*R_111[0]+P_202010000*R_201[0]+-1*P_202110000*R_211[0]; double PR_001011000001=P_001011000*R_001[0]+-1*P_001111000*R_011[0]+P_001211000*R_021[0]+-1*P_101011000*R_101[0]+P_101111000*R_111[0]+-1*P_101211000*R_121[0]; double PR_000012000001=P_000012000*R_001[0]+-1*P_000112000*R_011[0]+P_000212000*R_021[0]+-1*P_000312000*R_031[0]; double PR_001010001001=P_001010001*R_001[0]+-1*P_001010101*R_002[0]+-1*P_001110001*R_011[0]+P_001110101*R_012[0]+-1*P_101010001*R_101[0]+P_101010101*R_102[0]+P_101110001*R_111[0]+-1*P_101110101*R_112[0]; double PR_000011001001=P_000011001*R_001[0]+-1*P_000011101*R_002[0]+-1*P_000111001*R_011[0]+P_000111101*R_012[0]+P_000211001*R_021[0]+-1*P_000211101*R_022[0]; double PR_000010002001=P_000010002*R_001[0]+-1*P_000010102*R_002[0]+P_000010202*R_003[0]+-1*P_000110002*R_011[0]+P_000110102*R_012[0]+-1*P_000110202*R_013[0]; double PR_002000010001=P_002000010*R_001[0]+-1*P_002000110*R_002[0]+-1*P_102000010*R_101[0]+P_102000110*R_102[0]+P_202000010*R_201[0]+-1*P_202000110*R_202[0]; double PR_001001010001=P_001001010*R_001[0]+-1*P_001001110*R_002[0]+-1*P_001101010*R_011[0]+P_001101110*R_012[0]+-1*P_101001010*R_101[0]+P_101001110*R_102[0]+P_101101010*R_111[0]+-1*P_101101110*R_112[0]; double PR_000002010001=P_000002010*R_001[0]+-1*P_000002110*R_002[0]+-1*P_000102010*R_011[0]+P_000102110*R_012[0]+P_000202010*R_021[0]+-1*P_000202110*R_022[0]; double PR_001000011001=P_001000011*R_001[0]+-1*P_001000111*R_002[0]+P_001000211*R_003[0]+-1*P_101000011*R_101[0]+P_101000111*R_102[0]+-1*P_101000211*R_103[0]; double PR_000001011001=P_000001011*R_001[0]+-1*P_000001111*R_002[0]+P_000001211*R_003[0]+-1*P_000101011*R_011[0]+P_000101111*R_012[0]+-1*P_000101211*R_013[0]; double PR_000000012001=P_000000012*R_001[0]+-1*P_000000112*R_002[0]+P_000000212*R_003[0]+-1*P_000000312*R_004[0]; double PR_012000000010=P_012000000*R_010[0]+-1*P_112000000*R_110[0]+P_212000000*R_210[0]+-1*P_312000000*R_310[0]; double PR_011001000010=P_011001000*R_010[0]+-1*P_011101000*R_020[0]+-1*P_111001000*R_110[0]+P_111101000*R_120[0]+P_211001000*R_210[0]+-1*P_211101000*R_220[0]; double PR_010002000010=P_010002000*R_010[0]+-1*P_010102000*R_020[0]+P_010202000*R_030[0]+-1*P_110002000*R_110[0]+P_110102000*R_120[0]+-1*P_110202000*R_130[0]; double PR_011000001010=P_011000001*R_010[0]+-1*P_011000101*R_011[0]+-1*P_111000001*R_110[0]+P_111000101*R_111[0]+P_211000001*R_210[0]+-1*P_211000101*R_211[0]; double PR_010001001010=P_010001001*R_010[0]+-1*P_010001101*R_011[0]+-1*P_010101001*R_020[0]+P_010101101*R_021[0]+-1*P_110001001*R_110[0]+P_110001101*R_111[0]+P_110101001*R_120[0]+-1*P_110101101*R_121[0]; double PR_010000002010=P_010000002*R_010[0]+-1*P_010000102*R_011[0]+P_010000202*R_012[0]+-1*P_110000002*R_110[0]+P_110000102*R_111[0]+-1*P_110000202*R_112[0]; double PR_002010000010=P_002010000*R_010[0]+-1*P_002110000*R_020[0]+-1*P_102010000*R_110[0]+P_102110000*R_120[0]+P_202010000*R_210[0]+-1*P_202110000*R_220[0]; double PR_001011000010=P_001011000*R_010[0]+-1*P_001111000*R_020[0]+P_001211000*R_030[0]+-1*P_101011000*R_110[0]+P_101111000*R_120[0]+-1*P_101211000*R_130[0]; double PR_000012000010=P_000012000*R_010[0]+-1*P_000112000*R_020[0]+P_000212000*R_030[0]+-1*P_000312000*R_040[0]; double PR_001010001010=P_001010001*R_010[0]+-1*P_001010101*R_011[0]+-1*P_001110001*R_020[0]+P_001110101*R_021[0]+-1*P_101010001*R_110[0]+P_101010101*R_111[0]+P_101110001*R_120[0]+-1*P_101110101*R_121[0]; double PR_000011001010=P_000011001*R_010[0]+-1*P_000011101*R_011[0]+-1*P_000111001*R_020[0]+P_000111101*R_021[0]+P_000211001*R_030[0]+-1*P_000211101*R_031[0]; double PR_000010002010=P_000010002*R_010[0]+-1*P_000010102*R_011[0]+P_000010202*R_012[0]+-1*P_000110002*R_020[0]+P_000110102*R_021[0]+-1*P_000110202*R_022[0]; double PR_002000010010=P_002000010*R_010[0]+-1*P_002000110*R_011[0]+-1*P_102000010*R_110[0]+P_102000110*R_111[0]+P_202000010*R_210[0]+-1*P_202000110*R_211[0]; double PR_001001010010=P_001001010*R_010[0]+-1*P_001001110*R_011[0]+-1*P_001101010*R_020[0]+P_001101110*R_021[0]+-1*P_101001010*R_110[0]+P_101001110*R_111[0]+P_101101010*R_120[0]+-1*P_101101110*R_121[0]; double PR_000002010010=P_000002010*R_010[0]+-1*P_000002110*R_011[0]+-1*P_000102010*R_020[0]+P_000102110*R_021[0]+P_000202010*R_030[0]+-1*P_000202110*R_031[0]; double PR_001000011010=P_001000011*R_010[0]+-1*P_001000111*R_011[0]+P_001000211*R_012[0]+-1*P_101000011*R_110[0]+P_101000111*R_111[0]+-1*P_101000211*R_112[0]; double PR_000001011010=P_000001011*R_010[0]+-1*P_000001111*R_011[0]+P_000001211*R_012[0]+-1*P_000101011*R_020[0]+P_000101111*R_021[0]+-1*P_000101211*R_022[0]; double PR_000000012010=P_000000012*R_010[0]+-1*P_000000112*R_011[0]+P_000000212*R_012[0]+-1*P_000000312*R_013[0]; double PR_012000000100=P_012000000*R_100[0]+-1*P_112000000*R_200[0]+P_212000000*R_300[0]+-1*P_312000000*R_400[0]; double PR_011001000100=P_011001000*R_100[0]+-1*P_011101000*R_110[0]+-1*P_111001000*R_200[0]+P_111101000*R_210[0]+P_211001000*R_300[0]+-1*P_211101000*R_310[0]; double PR_010002000100=P_010002000*R_100[0]+-1*P_010102000*R_110[0]+P_010202000*R_120[0]+-1*P_110002000*R_200[0]+P_110102000*R_210[0]+-1*P_110202000*R_220[0]; double PR_011000001100=P_011000001*R_100[0]+-1*P_011000101*R_101[0]+-1*P_111000001*R_200[0]+P_111000101*R_201[0]+P_211000001*R_300[0]+-1*P_211000101*R_301[0]; double PR_010001001100=P_010001001*R_100[0]+-1*P_010001101*R_101[0]+-1*P_010101001*R_110[0]+P_010101101*R_111[0]+-1*P_110001001*R_200[0]+P_110001101*R_201[0]+P_110101001*R_210[0]+-1*P_110101101*R_211[0]; double PR_010000002100=P_010000002*R_100[0]+-1*P_010000102*R_101[0]+P_010000202*R_102[0]+-1*P_110000002*R_200[0]+P_110000102*R_201[0]+-1*P_110000202*R_202[0]; double PR_002010000100=P_002010000*R_100[0]+-1*P_002110000*R_110[0]+-1*P_102010000*R_200[0]+P_102110000*R_210[0]+P_202010000*R_300[0]+-1*P_202110000*R_310[0]; double PR_001011000100=P_001011000*R_100[0]+-1*P_001111000*R_110[0]+P_001211000*R_120[0]+-1*P_101011000*R_200[0]+P_101111000*R_210[0]+-1*P_101211000*R_220[0]; double PR_000012000100=P_000012000*R_100[0]+-1*P_000112000*R_110[0]+P_000212000*R_120[0]+-1*P_000312000*R_130[0]; double PR_001010001100=P_001010001*R_100[0]+-1*P_001010101*R_101[0]+-1*P_001110001*R_110[0]+P_001110101*R_111[0]+-1*P_101010001*R_200[0]+P_101010101*R_201[0]+P_101110001*R_210[0]+-1*P_101110101*R_211[0]; double PR_000011001100=P_000011001*R_100[0]+-1*P_000011101*R_101[0]+-1*P_000111001*R_110[0]+P_000111101*R_111[0]+P_000211001*R_120[0]+-1*P_000211101*R_121[0]; double PR_000010002100=P_000010002*R_100[0]+-1*P_000010102*R_101[0]+P_000010202*R_102[0]+-1*P_000110002*R_110[0]+P_000110102*R_111[0]+-1*P_000110202*R_112[0]; double PR_002000010100=P_002000010*R_100[0]+-1*P_002000110*R_101[0]+-1*P_102000010*R_200[0]+P_102000110*R_201[0]+P_202000010*R_300[0]+-1*P_202000110*R_301[0]; double PR_001001010100=P_001001010*R_100[0]+-1*P_001001110*R_101[0]+-1*P_001101010*R_110[0]+P_001101110*R_111[0]+-1*P_101001010*R_200[0]+P_101001110*R_201[0]+P_101101010*R_210[0]+-1*P_101101110*R_211[0]; double PR_000002010100=P_000002010*R_100[0]+-1*P_000002110*R_101[0]+-1*P_000102010*R_110[0]+P_000102110*R_111[0]+P_000202010*R_120[0]+-1*P_000202110*R_121[0]; double PR_001000011100=P_001000011*R_100[0]+-1*P_001000111*R_101[0]+P_001000211*R_102[0]+-1*P_101000011*R_200[0]+P_101000111*R_201[0]+-1*P_101000211*R_202[0]; double PR_000001011100=P_000001011*R_100[0]+-1*P_000001111*R_101[0]+P_000001211*R_102[0]+-1*P_000101011*R_110[0]+P_000101111*R_111[0]+-1*P_000101211*R_112[0]; double PR_000000012100=P_000000012*R_100[0]+-1*P_000000112*R_101[0]+P_000000212*R_102[0]+-1*P_000000312*R_103[0]; double PR_012000000002=P_012000000*R_002[0]+-1*P_112000000*R_102[0]+P_212000000*R_202[0]+-1*P_312000000*R_302[0]; double PR_011001000002=P_011001000*R_002[0]+-1*P_011101000*R_012[0]+-1*P_111001000*R_102[0]+P_111101000*R_112[0]+P_211001000*R_202[0]+-1*P_211101000*R_212[0]; double PR_010002000002=P_010002000*R_002[0]+-1*P_010102000*R_012[0]+P_010202000*R_022[0]+-1*P_110002000*R_102[0]+P_110102000*R_112[0]+-1*P_110202000*R_122[0]; double PR_011000001002=P_011000001*R_002[0]+-1*P_011000101*R_003[0]+-1*P_111000001*R_102[0]+P_111000101*R_103[0]+P_211000001*R_202[0]+-1*P_211000101*R_203[0]; double PR_010001001002=P_010001001*R_002[0]+-1*P_010001101*R_003[0]+-1*P_010101001*R_012[0]+P_010101101*R_013[0]+-1*P_110001001*R_102[0]+P_110001101*R_103[0]+P_110101001*R_112[0]+-1*P_110101101*R_113[0]; double PR_010000002002=P_010000002*R_002[0]+-1*P_010000102*R_003[0]+P_010000202*R_004[0]+-1*P_110000002*R_102[0]+P_110000102*R_103[0]+-1*P_110000202*R_104[0]; double PR_002010000002=P_002010000*R_002[0]+-1*P_002110000*R_012[0]+-1*P_102010000*R_102[0]+P_102110000*R_112[0]+P_202010000*R_202[0]+-1*P_202110000*R_212[0]; double PR_001011000002=P_001011000*R_002[0]+-1*P_001111000*R_012[0]+P_001211000*R_022[0]+-1*P_101011000*R_102[0]+P_101111000*R_112[0]+-1*P_101211000*R_122[0]; double PR_000012000002=P_000012000*R_002[0]+-1*P_000112000*R_012[0]+P_000212000*R_022[0]+-1*P_000312000*R_032[0]; double PR_001010001002=P_001010001*R_002[0]+-1*P_001010101*R_003[0]+-1*P_001110001*R_012[0]+P_001110101*R_013[0]+-1*P_101010001*R_102[0]+P_101010101*R_103[0]+P_101110001*R_112[0]+-1*P_101110101*R_113[0]; double PR_000011001002=P_000011001*R_002[0]+-1*P_000011101*R_003[0]+-1*P_000111001*R_012[0]+P_000111101*R_013[0]+P_000211001*R_022[0]+-1*P_000211101*R_023[0]; double PR_000010002002=P_000010002*R_002[0]+-1*P_000010102*R_003[0]+P_000010202*R_004[0]+-1*P_000110002*R_012[0]+P_000110102*R_013[0]+-1*P_000110202*R_014[0]; double PR_002000010002=P_002000010*R_002[0]+-1*P_002000110*R_003[0]+-1*P_102000010*R_102[0]+P_102000110*R_103[0]+P_202000010*R_202[0]+-1*P_202000110*R_203[0]; double PR_001001010002=P_001001010*R_002[0]+-1*P_001001110*R_003[0]+-1*P_001101010*R_012[0]+P_001101110*R_013[0]+-1*P_101001010*R_102[0]+P_101001110*R_103[0]+P_101101010*R_112[0]+-1*P_101101110*R_113[0]; double PR_000002010002=P_000002010*R_002[0]+-1*P_000002110*R_003[0]+-1*P_000102010*R_012[0]+P_000102110*R_013[0]+P_000202010*R_022[0]+-1*P_000202110*R_023[0]; double PR_001000011002=P_001000011*R_002[0]+-1*P_001000111*R_003[0]+P_001000211*R_004[0]+-1*P_101000011*R_102[0]+P_101000111*R_103[0]+-1*P_101000211*R_104[0]; double PR_000001011002=P_000001011*R_002[0]+-1*P_000001111*R_003[0]+P_000001211*R_004[0]+-1*P_000101011*R_012[0]+P_000101111*R_013[0]+-1*P_000101211*R_014[0]; double PR_000000012002=P_000000012*R_002[0]+-1*P_000000112*R_003[0]+P_000000212*R_004[0]+-1*P_000000312*R_005[0]; double PR_012000000011=P_012000000*R_011[0]+-1*P_112000000*R_111[0]+P_212000000*R_211[0]+-1*P_312000000*R_311[0]; double PR_011001000011=P_011001000*R_011[0]+-1*P_011101000*R_021[0]+-1*P_111001000*R_111[0]+P_111101000*R_121[0]+P_211001000*R_211[0]+-1*P_211101000*R_221[0]; double PR_010002000011=P_010002000*R_011[0]+-1*P_010102000*R_021[0]+P_010202000*R_031[0]+-1*P_110002000*R_111[0]+P_110102000*R_121[0]+-1*P_110202000*R_131[0]; double PR_011000001011=P_011000001*R_011[0]+-1*P_011000101*R_012[0]+-1*P_111000001*R_111[0]+P_111000101*R_112[0]+P_211000001*R_211[0]+-1*P_211000101*R_212[0]; double PR_010001001011=P_010001001*R_011[0]+-1*P_010001101*R_012[0]+-1*P_010101001*R_021[0]+P_010101101*R_022[0]+-1*P_110001001*R_111[0]+P_110001101*R_112[0]+P_110101001*R_121[0]+-1*P_110101101*R_122[0]; double PR_010000002011=P_010000002*R_011[0]+-1*P_010000102*R_012[0]+P_010000202*R_013[0]+-1*P_110000002*R_111[0]+P_110000102*R_112[0]+-1*P_110000202*R_113[0]; double PR_002010000011=P_002010000*R_011[0]+-1*P_002110000*R_021[0]+-1*P_102010000*R_111[0]+P_102110000*R_121[0]+P_202010000*R_211[0]+-1*P_202110000*R_221[0]; double PR_001011000011=P_001011000*R_011[0]+-1*P_001111000*R_021[0]+P_001211000*R_031[0]+-1*P_101011000*R_111[0]+P_101111000*R_121[0]+-1*P_101211000*R_131[0]; double PR_000012000011=P_000012000*R_011[0]+-1*P_000112000*R_021[0]+P_000212000*R_031[0]+-1*P_000312000*R_041[0]; double PR_001010001011=P_001010001*R_011[0]+-1*P_001010101*R_012[0]+-1*P_001110001*R_021[0]+P_001110101*R_022[0]+-1*P_101010001*R_111[0]+P_101010101*R_112[0]+P_101110001*R_121[0]+-1*P_101110101*R_122[0]; double PR_000011001011=P_000011001*R_011[0]+-1*P_000011101*R_012[0]+-1*P_000111001*R_021[0]+P_000111101*R_022[0]+P_000211001*R_031[0]+-1*P_000211101*R_032[0]; double PR_000010002011=P_000010002*R_011[0]+-1*P_000010102*R_012[0]+P_000010202*R_013[0]+-1*P_000110002*R_021[0]+P_000110102*R_022[0]+-1*P_000110202*R_023[0]; double PR_002000010011=P_002000010*R_011[0]+-1*P_002000110*R_012[0]+-1*P_102000010*R_111[0]+P_102000110*R_112[0]+P_202000010*R_211[0]+-1*P_202000110*R_212[0]; double PR_001001010011=P_001001010*R_011[0]+-1*P_001001110*R_012[0]+-1*P_001101010*R_021[0]+P_001101110*R_022[0]+-1*P_101001010*R_111[0]+P_101001110*R_112[0]+P_101101010*R_121[0]+-1*P_101101110*R_122[0]; double PR_000002010011=P_000002010*R_011[0]+-1*P_000002110*R_012[0]+-1*P_000102010*R_021[0]+P_000102110*R_022[0]+P_000202010*R_031[0]+-1*P_000202110*R_032[0]; double PR_001000011011=P_001000011*R_011[0]+-1*P_001000111*R_012[0]+P_001000211*R_013[0]+-1*P_101000011*R_111[0]+P_101000111*R_112[0]+-1*P_101000211*R_113[0]; double PR_000001011011=P_000001011*R_011[0]+-1*P_000001111*R_012[0]+P_000001211*R_013[0]+-1*P_000101011*R_021[0]+P_000101111*R_022[0]+-1*P_000101211*R_023[0]; double PR_000000012011=P_000000012*R_011[0]+-1*P_000000112*R_012[0]+P_000000212*R_013[0]+-1*P_000000312*R_014[0]; double PR_012000000020=P_012000000*R_020[0]+-1*P_112000000*R_120[0]+P_212000000*R_220[0]+-1*P_312000000*R_320[0]; double PR_011001000020=P_011001000*R_020[0]+-1*P_011101000*R_030[0]+-1*P_111001000*R_120[0]+P_111101000*R_130[0]+P_211001000*R_220[0]+-1*P_211101000*R_230[0]; double PR_010002000020=P_010002000*R_020[0]+-1*P_010102000*R_030[0]+P_010202000*R_040[0]+-1*P_110002000*R_120[0]+P_110102000*R_130[0]+-1*P_110202000*R_140[0]; double PR_011000001020=P_011000001*R_020[0]+-1*P_011000101*R_021[0]+-1*P_111000001*R_120[0]+P_111000101*R_121[0]+P_211000001*R_220[0]+-1*P_211000101*R_221[0]; double PR_010001001020=P_010001001*R_020[0]+-1*P_010001101*R_021[0]+-1*P_010101001*R_030[0]+P_010101101*R_031[0]+-1*P_110001001*R_120[0]+P_110001101*R_121[0]+P_110101001*R_130[0]+-1*P_110101101*R_131[0]; double PR_010000002020=P_010000002*R_020[0]+-1*P_010000102*R_021[0]+P_010000202*R_022[0]+-1*P_110000002*R_120[0]+P_110000102*R_121[0]+-1*P_110000202*R_122[0]; double PR_002010000020=P_002010000*R_020[0]+-1*P_002110000*R_030[0]+-1*P_102010000*R_120[0]+P_102110000*R_130[0]+P_202010000*R_220[0]+-1*P_202110000*R_230[0]; double PR_001011000020=P_001011000*R_020[0]+-1*P_001111000*R_030[0]+P_001211000*R_040[0]+-1*P_101011000*R_120[0]+P_101111000*R_130[0]+-1*P_101211000*R_140[0]; double PR_000012000020=P_000012000*R_020[0]+-1*P_000112000*R_030[0]+P_000212000*R_040[0]+-1*P_000312000*R_050[0]; double PR_001010001020=P_001010001*R_020[0]+-1*P_001010101*R_021[0]+-1*P_001110001*R_030[0]+P_001110101*R_031[0]+-1*P_101010001*R_120[0]+P_101010101*R_121[0]+P_101110001*R_130[0]+-1*P_101110101*R_131[0]; double PR_000011001020=P_000011001*R_020[0]+-1*P_000011101*R_021[0]+-1*P_000111001*R_030[0]+P_000111101*R_031[0]+P_000211001*R_040[0]+-1*P_000211101*R_041[0]; double PR_000010002020=P_000010002*R_020[0]+-1*P_000010102*R_021[0]+P_000010202*R_022[0]+-1*P_000110002*R_030[0]+P_000110102*R_031[0]+-1*P_000110202*R_032[0]; double PR_002000010020=P_002000010*R_020[0]+-1*P_002000110*R_021[0]+-1*P_102000010*R_120[0]+P_102000110*R_121[0]+P_202000010*R_220[0]+-1*P_202000110*R_221[0]; double PR_001001010020=P_001001010*R_020[0]+-1*P_001001110*R_021[0]+-1*P_001101010*R_030[0]+P_001101110*R_031[0]+-1*P_101001010*R_120[0]+P_101001110*R_121[0]+P_101101010*R_130[0]+-1*P_101101110*R_131[0]; double PR_000002010020=P_000002010*R_020[0]+-1*P_000002110*R_021[0]+-1*P_000102010*R_030[0]+P_000102110*R_031[0]+P_000202010*R_040[0]+-1*P_000202110*R_041[0]; double PR_001000011020=P_001000011*R_020[0]+-1*P_001000111*R_021[0]+P_001000211*R_022[0]+-1*P_101000011*R_120[0]+P_101000111*R_121[0]+-1*P_101000211*R_122[0]; double PR_000001011020=P_000001011*R_020[0]+-1*P_000001111*R_021[0]+P_000001211*R_022[0]+-1*P_000101011*R_030[0]+P_000101111*R_031[0]+-1*P_000101211*R_032[0]; double PR_000000012020=P_000000012*R_020[0]+-1*P_000000112*R_021[0]+P_000000212*R_022[0]+-1*P_000000312*R_023[0]; double PR_012000000101=P_012000000*R_101[0]+-1*P_112000000*R_201[0]+P_212000000*R_301[0]+-1*P_312000000*R_401[0]; double PR_011001000101=P_011001000*R_101[0]+-1*P_011101000*R_111[0]+-1*P_111001000*R_201[0]+P_111101000*R_211[0]+P_211001000*R_301[0]+-1*P_211101000*R_311[0]; double PR_010002000101=P_010002000*R_101[0]+-1*P_010102000*R_111[0]+P_010202000*R_121[0]+-1*P_110002000*R_201[0]+P_110102000*R_211[0]+-1*P_110202000*R_221[0]; double PR_011000001101=P_011000001*R_101[0]+-1*P_011000101*R_102[0]+-1*P_111000001*R_201[0]+P_111000101*R_202[0]+P_211000001*R_301[0]+-1*P_211000101*R_302[0]; double PR_010001001101=P_010001001*R_101[0]+-1*P_010001101*R_102[0]+-1*P_010101001*R_111[0]+P_010101101*R_112[0]+-1*P_110001001*R_201[0]+P_110001101*R_202[0]+P_110101001*R_211[0]+-1*P_110101101*R_212[0]; double PR_010000002101=P_010000002*R_101[0]+-1*P_010000102*R_102[0]+P_010000202*R_103[0]+-1*P_110000002*R_201[0]+P_110000102*R_202[0]+-1*P_110000202*R_203[0]; double PR_002010000101=P_002010000*R_101[0]+-1*P_002110000*R_111[0]+-1*P_102010000*R_201[0]+P_102110000*R_211[0]+P_202010000*R_301[0]+-1*P_202110000*R_311[0]; double PR_001011000101=P_001011000*R_101[0]+-1*P_001111000*R_111[0]+P_001211000*R_121[0]+-1*P_101011000*R_201[0]+P_101111000*R_211[0]+-1*P_101211000*R_221[0]; double PR_000012000101=P_000012000*R_101[0]+-1*P_000112000*R_111[0]+P_000212000*R_121[0]+-1*P_000312000*R_131[0]; double PR_001010001101=P_001010001*R_101[0]+-1*P_001010101*R_102[0]+-1*P_001110001*R_111[0]+P_001110101*R_112[0]+-1*P_101010001*R_201[0]+P_101010101*R_202[0]+P_101110001*R_211[0]+-1*P_101110101*R_212[0]; double PR_000011001101=P_000011001*R_101[0]+-1*P_000011101*R_102[0]+-1*P_000111001*R_111[0]+P_000111101*R_112[0]+P_000211001*R_121[0]+-1*P_000211101*R_122[0]; double PR_000010002101=P_000010002*R_101[0]+-1*P_000010102*R_102[0]+P_000010202*R_103[0]+-1*P_000110002*R_111[0]+P_000110102*R_112[0]+-1*P_000110202*R_113[0]; double PR_002000010101=P_002000010*R_101[0]+-1*P_002000110*R_102[0]+-1*P_102000010*R_201[0]+P_102000110*R_202[0]+P_202000010*R_301[0]+-1*P_202000110*R_302[0]; double PR_001001010101=P_001001010*R_101[0]+-1*P_001001110*R_102[0]+-1*P_001101010*R_111[0]+P_001101110*R_112[0]+-1*P_101001010*R_201[0]+P_101001110*R_202[0]+P_101101010*R_211[0]+-1*P_101101110*R_212[0]; double PR_000002010101=P_000002010*R_101[0]+-1*P_000002110*R_102[0]+-1*P_000102010*R_111[0]+P_000102110*R_112[0]+P_000202010*R_121[0]+-1*P_000202110*R_122[0]; double PR_001000011101=P_001000011*R_101[0]+-1*P_001000111*R_102[0]+P_001000211*R_103[0]+-1*P_101000011*R_201[0]+P_101000111*R_202[0]+-1*P_101000211*R_203[0]; double PR_000001011101=P_000001011*R_101[0]+-1*P_000001111*R_102[0]+P_000001211*R_103[0]+-1*P_000101011*R_111[0]+P_000101111*R_112[0]+-1*P_000101211*R_113[0]; double PR_000000012101=P_000000012*R_101[0]+-1*P_000000112*R_102[0]+P_000000212*R_103[0]+-1*P_000000312*R_104[0]; double PR_012000000110=P_012000000*R_110[0]+-1*P_112000000*R_210[0]+P_212000000*R_310[0]+-1*P_312000000*R_410[0]; double PR_011001000110=P_011001000*R_110[0]+-1*P_011101000*R_120[0]+-1*P_111001000*R_210[0]+P_111101000*R_220[0]+P_211001000*R_310[0]+-1*P_211101000*R_320[0]; double PR_010002000110=P_010002000*R_110[0]+-1*P_010102000*R_120[0]+P_010202000*R_130[0]+-1*P_110002000*R_210[0]+P_110102000*R_220[0]+-1*P_110202000*R_230[0]; double PR_011000001110=P_011000001*R_110[0]+-1*P_011000101*R_111[0]+-1*P_111000001*R_210[0]+P_111000101*R_211[0]+P_211000001*R_310[0]+-1*P_211000101*R_311[0]; double PR_010001001110=P_010001001*R_110[0]+-1*P_010001101*R_111[0]+-1*P_010101001*R_120[0]+P_010101101*R_121[0]+-1*P_110001001*R_210[0]+P_110001101*R_211[0]+P_110101001*R_220[0]+-1*P_110101101*R_221[0]; double PR_010000002110=P_010000002*R_110[0]+-1*P_010000102*R_111[0]+P_010000202*R_112[0]+-1*P_110000002*R_210[0]+P_110000102*R_211[0]+-1*P_110000202*R_212[0]; double PR_002010000110=P_002010000*R_110[0]+-1*P_002110000*R_120[0]+-1*P_102010000*R_210[0]+P_102110000*R_220[0]+P_202010000*R_310[0]+-1*P_202110000*R_320[0]; double PR_001011000110=P_001011000*R_110[0]+-1*P_001111000*R_120[0]+P_001211000*R_130[0]+-1*P_101011000*R_210[0]+P_101111000*R_220[0]+-1*P_101211000*R_230[0]; double PR_000012000110=P_000012000*R_110[0]+-1*P_000112000*R_120[0]+P_000212000*R_130[0]+-1*P_000312000*R_140[0]; double PR_001010001110=P_001010001*R_110[0]+-1*P_001010101*R_111[0]+-1*P_001110001*R_120[0]+P_001110101*R_121[0]+-1*P_101010001*R_210[0]+P_101010101*R_211[0]+P_101110001*R_220[0]+-1*P_101110101*R_221[0]; double PR_000011001110=P_000011001*R_110[0]+-1*P_000011101*R_111[0]+-1*P_000111001*R_120[0]+P_000111101*R_121[0]+P_000211001*R_130[0]+-1*P_000211101*R_131[0]; double PR_000010002110=P_000010002*R_110[0]+-1*P_000010102*R_111[0]+P_000010202*R_112[0]+-1*P_000110002*R_120[0]+P_000110102*R_121[0]+-1*P_000110202*R_122[0]; double PR_002000010110=P_002000010*R_110[0]+-1*P_002000110*R_111[0]+-1*P_102000010*R_210[0]+P_102000110*R_211[0]+P_202000010*R_310[0]+-1*P_202000110*R_311[0]; double PR_001001010110=P_001001010*R_110[0]+-1*P_001001110*R_111[0]+-1*P_001101010*R_120[0]+P_001101110*R_121[0]+-1*P_101001010*R_210[0]+P_101001110*R_211[0]+P_101101010*R_220[0]+-1*P_101101110*R_221[0]; double PR_000002010110=P_000002010*R_110[0]+-1*P_000002110*R_111[0]+-1*P_000102010*R_120[0]+P_000102110*R_121[0]+P_000202010*R_130[0]+-1*P_000202110*R_131[0]; double PR_001000011110=P_001000011*R_110[0]+-1*P_001000111*R_111[0]+P_001000211*R_112[0]+-1*P_101000011*R_210[0]+P_101000111*R_211[0]+-1*P_101000211*R_212[0]; double PR_000001011110=P_000001011*R_110[0]+-1*P_000001111*R_111[0]+P_000001211*R_112[0]+-1*P_000101011*R_120[0]+P_000101111*R_121[0]+-1*P_000101211*R_122[0]; double PR_000000012110=P_000000012*R_110[0]+-1*P_000000112*R_111[0]+P_000000212*R_112[0]+-1*P_000000312*R_113[0]; double PR_012000000200=P_012000000*R_200[0]+-1*P_112000000*R_300[0]+P_212000000*R_400[0]+-1*P_312000000*R_500[0]; double PR_011001000200=P_011001000*R_200[0]+-1*P_011101000*R_210[0]+-1*P_111001000*R_300[0]+P_111101000*R_310[0]+P_211001000*R_400[0]+-1*P_211101000*R_410[0]; double PR_010002000200=P_010002000*R_200[0]+-1*P_010102000*R_210[0]+P_010202000*R_220[0]+-1*P_110002000*R_300[0]+P_110102000*R_310[0]+-1*P_110202000*R_320[0]; double PR_011000001200=P_011000001*R_200[0]+-1*P_011000101*R_201[0]+-1*P_111000001*R_300[0]+P_111000101*R_301[0]+P_211000001*R_400[0]+-1*P_211000101*R_401[0]; double PR_010001001200=P_010001001*R_200[0]+-1*P_010001101*R_201[0]+-1*P_010101001*R_210[0]+P_010101101*R_211[0]+-1*P_110001001*R_300[0]+P_110001101*R_301[0]+P_110101001*R_310[0]+-1*P_110101101*R_311[0]; double PR_010000002200=P_010000002*R_200[0]+-1*P_010000102*R_201[0]+P_010000202*R_202[0]+-1*P_110000002*R_300[0]+P_110000102*R_301[0]+-1*P_110000202*R_302[0]; double PR_002010000200=P_002010000*R_200[0]+-1*P_002110000*R_210[0]+-1*P_102010000*R_300[0]+P_102110000*R_310[0]+P_202010000*R_400[0]+-1*P_202110000*R_410[0]; double PR_001011000200=P_001011000*R_200[0]+-1*P_001111000*R_210[0]+P_001211000*R_220[0]+-1*P_101011000*R_300[0]+P_101111000*R_310[0]+-1*P_101211000*R_320[0]; double PR_000012000200=P_000012000*R_200[0]+-1*P_000112000*R_210[0]+P_000212000*R_220[0]+-1*P_000312000*R_230[0]; double PR_001010001200=P_001010001*R_200[0]+-1*P_001010101*R_201[0]+-1*P_001110001*R_210[0]+P_001110101*R_211[0]+-1*P_101010001*R_300[0]+P_101010101*R_301[0]+P_101110001*R_310[0]+-1*P_101110101*R_311[0]; double PR_000011001200=P_000011001*R_200[0]+-1*P_000011101*R_201[0]+-1*P_000111001*R_210[0]+P_000111101*R_211[0]+P_000211001*R_220[0]+-1*P_000211101*R_221[0]; double PR_000010002200=P_000010002*R_200[0]+-1*P_000010102*R_201[0]+P_000010202*R_202[0]+-1*P_000110002*R_210[0]+P_000110102*R_211[0]+-1*P_000110202*R_212[0]; double PR_002000010200=P_002000010*R_200[0]+-1*P_002000110*R_201[0]+-1*P_102000010*R_300[0]+P_102000110*R_301[0]+P_202000010*R_400[0]+-1*P_202000110*R_401[0]; double PR_001001010200=P_001001010*R_200[0]+-1*P_001001110*R_201[0]+-1*P_001101010*R_210[0]+P_001101110*R_211[0]+-1*P_101001010*R_300[0]+P_101001110*R_301[0]+P_101101010*R_310[0]+-1*P_101101110*R_311[0]; double PR_000002010200=P_000002010*R_200[0]+-1*P_000002110*R_201[0]+-1*P_000102010*R_210[0]+P_000102110*R_211[0]+P_000202010*R_220[0]+-1*P_000202110*R_221[0]; double PR_001000011200=P_001000011*R_200[0]+-1*P_001000111*R_201[0]+P_001000211*R_202[0]+-1*P_101000011*R_300[0]+P_101000111*R_301[0]+-1*P_101000211*R_302[0]; double PR_000001011200=P_000001011*R_200[0]+-1*P_000001111*R_201[0]+P_000001211*R_202[0]+-1*P_000101011*R_210[0]+P_000101111*R_211[0]+-1*P_000101211*R_212[0]; double PR_000000012200=P_000000012*R_200[0]+-1*P_000000112*R_201[0]+P_000000212*R_202[0]+-1*P_000000312*R_203[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*3+0]+=Pmtrx[0]*(Q_002000000*PR_012000000000+Q_102000000*PR_012000000100+Q_202000000*PR_012000000200); ans_temp[ans_id*3+0]+=Pmtrx[1]*(Q_001001000*PR_012000000000+Q_001101000*PR_012000000010+Q_101001000*PR_012000000100+Q_101101000*PR_012000000110); ans_temp[ans_id*3+0]+=Pmtrx[2]*(Q_000002000*PR_012000000000+Q_000102000*PR_012000000010+Q_000202000*PR_012000000020); ans_temp[ans_id*3+0]+=Pmtrx[3]*(Q_001000001*PR_012000000000+Q_001000101*PR_012000000001+Q_101000001*PR_012000000100+Q_101000101*PR_012000000101); ans_temp[ans_id*3+0]+=Pmtrx[4]*(Q_000001001*PR_012000000000+Q_000001101*PR_012000000001+Q_000101001*PR_012000000010+Q_000101101*PR_012000000011); ans_temp[ans_id*3+0]+=Pmtrx[5]*(Q_000000002*PR_012000000000+Q_000000102*PR_012000000001+Q_000000202*PR_012000000002); ans_temp[ans_id*3+0]+=Pmtrx[6]*(Q_002000000*PR_011001000000+Q_102000000*PR_011001000100+Q_202000000*PR_011001000200); ans_temp[ans_id*3+0]+=Pmtrx[7]*(Q_001001000*PR_011001000000+Q_001101000*PR_011001000010+Q_101001000*PR_011001000100+Q_101101000*PR_011001000110); ans_temp[ans_id*3+0]+=Pmtrx[8]*(Q_000002000*PR_011001000000+Q_000102000*PR_011001000010+Q_000202000*PR_011001000020); ans_temp[ans_id*3+0]+=Pmtrx[9]*(Q_001000001*PR_011001000000+Q_001000101*PR_011001000001+Q_101000001*PR_011001000100+Q_101000101*PR_011001000101); ans_temp[ans_id*3+0]+=Pmtrx[10]*(Q_000001001*PR_011001000000+Q_000001101*PR_011001000001+Q_000101001*PR_011001000010+Q_000101101*PR_011001000011); ans_temp[ans_id*3+0]+=Pmtrx[11]*(Q_000000002*PR_011001000000+Q_000000102*PR_011001000001+Q_000000202*PR_011001000002); ans_temp[ans_id*3+0]+=Pmtrx[12]*(Q_002000000*PR_010002000000+Q_102000000*PR_010002000100+Q_202000000*PR_010002000200); ans_temp[ans_id*3+0]+=Pmtrx[13]*(Q_001001000*PR_010002000000+Q_001101000*PR_010002000010+Q_101001000*PR_010002000100+Q_101101000*PR_010002000110); ans_temp[ans_id*3+0]+=Pmtrx[14]*(Q_000002000*PR_010002000000+Q_000102000*PR_010002000010+Q_000202000*PR_010002000020); ans_temp[ans_id*3+0]+=Pmtrx[15]*(Q_001000001*PR_010002000000+Q_001000101*PR_010002000001+Q_101000001*PR_010002000100+Q_101000101*PR_010002000101); ans_temp[ans_id*3+0]+=Pmtrx[16]*(Q_000001001*PR_010002000000+Q_000001101*PR_010002000001+Q_000101001*PR_010002000010+Q_000101101*PR_010002000011); ans_temp[ans_id*3+0]+=Pmtrx[17]*(Q_000000002*PR_010002000000+Q_000000102*PR_010002000001+Q_000000202*PR_010002000002); ans_temp[ans_id*3+0]+=Pmtrx[18]*(Q_002000000*PR_011000001000+Q_102000000*PR_011000001100+Q_202000000*PR_011000001200); ans_temp[ans_id*3+0]+=Pmtrx[19]*(Q_001001000*PR_011000001000+Q_001101000*PR_011000001010+Q_101001000*PR_011000001100+Q_101101000*PR_011000001110); ans_temp[ans_id*3+0]+=Pmtrx[20]*(Q_000002000*PR_011000001000+Q_000102000*PR_011000001010+Q_000202000*PR_011000001020); ans_temp[ans_id*3+0]+=Pmtrx[21]*(Q_001000001*PR_011000001000+Q_001000101*PR_011000001001+Q_101000001*PR_011000001100+Q_101000101*PR_011000001101); ans_temp[ans_id*3+0]+=Pmtrx[22]*(Q_000001001*PR_011000001000+Q_000001101*PR_011000001001+Q_000101001*PR_011000001010+Q_000101101*PR_011000001011); ans_temp[ans_id*3+0]+=Pmtrx[23]*(Q_000000002*PR_011000001000+Q_000000102*PR_011000001001+Q_000000202*PR_011000001002); ans_temp[ans_id*3+0]+=Pmtrx[24]*(Q_002000000*PR_010001001000+Q_102000000*PR_010001001100+Q_202000000*PR_010001001200); ans_temp[ans_id*3+0]+=Pmtrx[25]*(Q_001001000*PR_010001001000+Q_001101000*PR_010001001010+Q_101001000*PR_010001001100+Q_101101000*PR_010001001110); ans_temp[ans_id*3+0]+=Pmtrx[26]*(Q_000002000*PR_010001001000+Q_000102000*PR_010001001010+Q_000202000*PR_010001001020); ans_temp[ans_id*3+0]+=Pmtrx[27]*(Q_001000001*PR_010001001000+Q_001000101*PR_010001001001+Q_101000001*PR_010001001100+Q_101000101*PR_010001001101); ans_temp[ans_id*3+0]+=Pmtrx[28]*(Q_000001001*PR_010001001000+Q_000001101*PR_010001001001+Q_000101001*PR_010001001010+Q_000101101*PR_010001001011); ans_temp[ans_id*3+0]+=Pmtrx[29]*(Q_000000002*PR_010001001000+Q_000000102*PR_010001001001+Q_000000202*PR_010001001002); ans_temp[ans_id*3+0]+=Pmtrx[30]*(Q_002000000*PR_010000002000+Q_102000000*PR_010000002100+Q_202000000*PR_010000002200); ans_temp[ans_id*3+0]+=Pmtrx[31]*(Q_001001000*PR_010000002000+Q_001101000*PR_010000002010+Q_101001000*PR_010000002100+Q_101101000*PR_010000002110); ans_temp[ans_id*3+0]+=Pmtrx[32]*(Q_000002000*PR_010000002000+Q_000102000*PR_010000002010+Q_000202000*PR_010000002020); ans_temp[ans_id*3+0]+=Pmtrx[33]*(Q_001000001*PR_010000002000+Q_001000101*PR_010000002001+Q_101000001*PR_010000002100+Q_101000101*PR_010000002101); ans_temp[ans_id*3+0]+=Pmtrx[34]*(Q_000001001*PR_010000002000+Q_000001101*PR_010000002001+Q_000101001*PR_010000002010+Q_000101101*PR_010000002011); ans_temp[ans_id*3+0]+=Pmtrx[35]*(Q_000000002*PR_010000002000+Q_000000102*PR_010000002001+Q_000000202*PR_010000002002); ans_temp[ans_id*3+1]+=Pmtrx[0]*(Q_002000000*PR_002010000000+Q_102000000*PR_002010000100+Q_202000000*PR_002010000200); ans_temp[ans_id*3+1]+=Pmtrx[1]*(Q_001001000*PR_002010000000+Q_001101000*PR_002010000010+Q_101001000*PR_002010000100+Q_101101000*PR_002010000110); ans_temp[ans_id*3+1]+=Pmtrx[2]*(Q_000002000*PR_002010000000+Q_000102000*PR_002010000010+Q_000202000*PR_002010000020); ans_temp[ans_id*3+1]+=Pmtrx[3]*(Q_001000001*PR_002010000000+Q_001000101*PR_002010000001+Q_101000001*PR_002010000100+Q_101000101*PR_002010000101); ans_temp[ans_id*3+1]+=Pmtrx[4]*(Q_000001001*PR_002010000000+Q_000001101*PR_002010000001+Q_000101001*PR_002010000010+Q_000101101*PR_002010000011); ans_temp[ans_id*3+1]+=Pmtrx[5]*(Q_000000002*PR_002010000000+Q_000000102*PR_002010000001+Q_000000202*PR_002010000002); ans_temp[ans_id*3+1]+=Pmtrx[6]*(Q_002000000*PR_001011000000+Q_102000000*PR_001011000100+Q_202000000*PR_001011000200); ans_temp[ans_id*3+1]+=Pmtrx[7]*(Q_001001000*PR_001011000000+Q_001101000*PR_001011000010+Q_101001000*PR_001011000100+Q_101101000*PR_001011000110); ans_temp[ans_id*3+1]+=Pmtrx[8]*(Q_000002000*PR_001011000000+Q_000102000*PR_001011000010+Q_000202000*PR_001011000020); ans_temp[ans_id*3+1]+=Pmtrx[9]*(Q_001000001*PR_001011000000+Q_001000101*PR_001011000001+Q_101000001*PR_001011000100+Q_101000101*PR_001011000101); ans_temp[ans_id*3+1]+=Pmtrx[10]*(Q_000001001*PR_001011000000+Q_000001101*PR_001011000001+Q_000101001*PR_001011000010+Q_000101101*PR_001011000011); ans_temp[ans_id*3+1]+=Pmtrx[11]*(Q_000000002*PR_001011000000+Q_000000102*PR_001011000001+Q_000000202*PR_001011000002); ans_temp[ans_id*3+1]+=Pmtrx[12]*(Q_002000000*PR_000012000000+Q_102000000*PR_000012000100+Q_202000000*PR_000012000200); ans_temp[ans_id*3+1]+=Pmtrx[13]*(Q_001001000*PR_000012000000+Q_001101000*PR_000012000010+Q_101001000*PR_000012000100+Q_101101000*PR_000012000110); ans_temp[ans_id*3+1]+=Pmtrx[14]*(Q_000002000*PR_000012000000+Q_000102000*PR_000012000010+Q_000202000*PR_000012000020); ans_temp[ans_id*3+1]+=Pmtrx[15]*(Q_001000001*PR_000012000000+Q_001000101*PR_000012000001+Q_101000001*PR_000012000100+Q_101000101*PR_000012000101); ans_temp[ans_id*3+1]+=Pmtrx[16]*(Q_000001001*PR_000012000000+Q_000001101*PR_000012000001+Q_000101001*PR_000012000010+Q_000101101*PR_000012000011); ans_temp[ans_id*3+1]+=Pmtrx[17]*(Q_000000002*PR_000012000000+Q_000000102*PR_000012000001+Q_000000202*PR_000012000002); ans_temp[ans_id*3+1]+=Pmtrx[18]*(Q_002000000*PR_001010001000+Q_102000000*PR_001010001100+Q_202000000*PR_001010001200); ans_temp[ans_id*3+1]+=Pmtrx[19]*(Q_001001000*PR_001010001000+Q_001101000*PR_001010001010+Q_101001000*PR_001010001100+Q_101101000*PR_001010001110); ans_temp[ans_id*3+1]+=Pmtrx[20]*(Q_000002000*PR_001010001000+Q_000102000*PR_001010001010+Q_000202000*PR_001010001020); ans_temp[ans_id*3+1]+=Pmtrx[21]*(Q_001000001*PR_001010001000+Q_001000101*PR_001010001001+Q_101000001*PR_001010001100+Q_101000101*PR_001010001101); ans_temp[ans_id*3+1]+=Pmtrx[22]*(Q_000001001*PR_001010001000+Q_000001101*PR_001010001001+Q_000101001*PR_001010001010+Q_000101101*PR_001010001011); ans_temp[ans_id*3+1]+=Pmtrx[23]*(Q_000000002*PR_001010001000+Q_000000102*PR_001010001001+Q_000000202*PR_001010001002); ans_temp[ans_id*3+1]+=Pmtrx[24]*(Q_002000000*PR_000011001000+Q_102000000*PR_000011001100+Q_202000000*PR_000011001200); ans_temp[ans_id*3+1]+=Pmtrx[25]*(Q_001001000*PR_000011001000+Q_001101000*PR_000011001010+Q_101001000*PR_000011001100+Q_101101000*PR_000011001110); ans_temp[ans_id*3+1]+=Pmtrx[26]*(Q_000002000*PR_000011001000+Q_000102000*PR_000011001010+Q_000202000*PR_000011001020); ans_temp[ans_id*3+1]+=Pmtrx[27]*(Q_001000001*PR_000011001000+Q_001000101*PR_000011001001+Q_101000001*PR_000011001100+Q_101000101*PR_000011001101); ans_temp[ans_id*3+1]+=Pmtrx[28]*(Q_000001001*PR_000011001000+Q_000001101*PR_000011001001+Q_000101001*PR_000011001010+Q_000101101*PR_000011001011); ans_temp[ans_id*3+1]+=Pmtrx[29]*(Q_000000002*PR_000011001000+Q_000000102*PR_000011001001+Q_000000202*PR_000011001002); ans_temp[ans_id*3+1]+=Pmtrx[30]*(Q_002000000*PR_000010002000+Q_102000000*PR_000010002100+Q_202000000*PR_000010002200); ans_temp[ans_id*3+1]+=Pmtrx[31]*(Q_001001000*PR_000010002000+Q_001101000*PR_000010002010+Q_101001000*PR_000010002100+Q_101101000*PR_000010002110); ans_temp[ans_id*3+1]+=Pmtrx[32]*(Q_000002000*PR_000010002000+Q_000102000*PR_000010002010+Q_000202000*PR_000010002020); ans_temp[ans_id*3+1]+=Pmtrx[33]*(Q_001000001*PR_000010002000+Q_001000101*PR_000010002001+Q_101000001*PR_000010002100+Q_101000101*PR_000010002101); ans_temp[ans_id*3+1]+=Pmtrx[34]*(Q_000001001*PR_000010002000+Q_000001101*PR_000010002001+Q_000101001*PR_000010002010+Q_000101101*PR_000010002011); ans_temp[ans_id*3+1]+=Pmtrx[35]*(Q_000000002*PR_000010002000+Q_000000102*PR_000010002001+Q_000000202*PR_000010002002); ans_temp[ans_id*3+2]+=Pmtrx[0]*(Q_002000000*PR_002000010000+Q_102000000*PR_002000010100+Q_202000000*PR_002000010200); ans_temp[ans_id*3+2]+=Pmtrx[1]*(Q_001001000*PR_002000010000+Q_001101000*PR_002000010010+Q_101001000*PR_002000010100+Q_101101000*PR_002000010110); ans_temp[ans_id*3+2]+=Pmtrx[2]*(Q_000002000*PR_002000010000+Q_000102000*PR_002000010010+Q_000202000*PR_002000010020); ans_temp[ans_id*3+2]+=Pmtrx[3]*(Q_001000001*PR_002000010000+Q_001000101*PR_002000010001+Q_101000001*PR_002000010100+Q_101000101*PR_002000010101); ans_temp[ans_id*3+2]+=Pmtrx[4]*(Q_000001001*PR_002000010000+Q_000001101*PR_002000010001+Q_000101001*PR_002000010010+Q_000101101*PR_002000010011); ans_temp[ans_id*3+2]+=Pmtrx[5]*(Q_000000002*PR_002000010000+Q_000000102*PR_002000010001+Q_000000202*PR_002000010002); ans_temp[ans_id*3+2]+=Pmtrx[6]*(Q_002000000*PR_001001010000+Q_102000000*PR_001001010100+Q_202000000*PR_001001010200); ans_temp[ans_id*3+2]+=Pmtrx[7]*(Q_001001000*PR_001001010000+Q_001101000*PR_001001010010+Q_101001000*PR_001001010100+Q_101101000*PR_001001010110); ans_temp[ans_id*3+2]+=Pmtrx[8]*(Q_000002000*PR_001001010000+Q_000102000*PR_001001010010+Q_000202000*PR_001001010020); ans_temp[ans_id*3+2]+=Pmtrx[9]*(Q_001000001*PR_001001010000+Q_001000101*PR_001001010001+Q_101000001*PR_001001010100+Q_101000101*PR_001001010101); ans_temp[ans_id*3+2]+=Pmtrx[10]*(Q_000001001*PR_001001010000+Q_000001101*PR_001001010001+Q_000101001*PR_001001010010+Q_000101101*PR_001001010011); ans_temp[ans_id*3+2]+=Pmtrx[11]*(Q_000000002*PR_001001010000+Q_000000102*PR_001001010001+Q_000000202*PR_001001010002); ans_temp[ans_id*3+2]+=Pmtrx[12]*(Q_002000000*PR_000002010000+Q_102000000*PR_000002010100+Q_202000000*PR_000002010200); ans_temp[ans_id*3+2]+=Pmtrx[13]*(Q_001001000*PR_000002010000+Q_001101000*PR_000002010010+Q_101001000*PR_000002010100+Q_101101000*PR_000002010110); ans_temp[ans_id*3+2]+=Pmtrx[14]*(Q_000002000*PR_000002010000+Q_000102000*PR_000002010010+Q_000202000*PR_000002010020); ans_temp[ans_id*3+2]+=Pmtrx[15]*(Q_001000001*PR_000002010000+Q_001000101*PR_000002010001+Q_101000001*PR_000002010100+Q_101000101*PR_000002010101); ans_temp[ans_id*3+2]+=Pmtrx[16]*(Q_000001001*PR_000002010000+Q_000001101*PR_000002010001+Q_000101001*PR_000002010010+Q_000101101*PR_000002010011); ans_temp[ans_id*3+2]+=Pmtrx[17]*(Q_000000002*PR_000002010000+Q_000000102*PR_000002010001+Q_000000202*PR_000002010002); ans_temp[ans_id*3+2]+=Pmtrx[18]*(Q_002000000*PR_001000011000+Q_102000000*PR_001000011100+Q_202000000*PR_001000011200); ans_temp[ans_id*3+2]+=Pmtrx[19]*(Q_001001000*PR_001000011000+Q_001101000*PR_001000011010+Q_101001000*PR_001000011100+Q_101101000*PR_001000011110); ans_temp[ans_id*3+2]+=Pmtrx[20]*(Q_000002000*PR_001000011000+Q_000102000*PR_001000011010+Q_000202000*PR_001000011020); ans_temp[ans_id*3+2]+=Pmtrx[21]*(Q_001000001*PR_001000011000+Q_001000101*PR_001000011001+Q_101000001*PR_001000011100+Q_101000101*PR_001000011101); ans_temp[ans_id*3+2]+=Pmtrx[22]*(Q_000001001*PR_001000011000+Q_000001101*PR_001000011001+Q_000101001*PR_001000011010+Q_000101101*PR_001000011011); ans_temp[ans_id*3+2]+=Pmtrx[23]*(Q_000000002*PR_001000011000+Q_000000102*PR_001000011001+Q_000000202*PR_001000011002); ans_temp[ans_id*3+2]+=Pmtrx[24]*(Q_002000000*PR_000001011000+Q_102000000*PR_000001011100+Q_202000000*PR_000001011200); ans_temp[ans_id*3+2]+=Pmtrx[25]*(Q_001001000*PR_000001011000+Q_001101000*PR_000001011010+Q_101001000*PR_000001011100+Q_101101000*PR_000001011110); ans_temp[ans_id*3+2]+=Pmtrx[26]*(Q_000002000*PR_000001011000+Q_000102000*PR_000001011010+Q_000202000*PR_000001011020); ans_temp[ans_id*3+2]+=Pmtrx[27]*(Q_001000001*PR_000001011000+Q_001000101*PR_000001011001+Q_101000001*PR_000001011100+Q_101000101*PR_000001011101); ans_temp[ans_id*3+2]+=Pmtrx[28]*(Q_000001001*PR_000001011000+Q_000001101*PR_000001011001+Q_000101001*PR_000001011010+Q_000101101*PR_000001011011); ans_temp[ans_id*3+2]+=Pmtrx[29]*(Q_000000002*PR_000001011000+Q_000000102*PR_000001011001+Q_000000202*PR_000001011002); ans_temp[ans_id*3+2]+=Pmtrx[30]*(Q_002000000*PR_000000012000+Q_102000000*PR_000000012100+Q_202000000*PR_000000012200); ans_temp[ans_id*3+2]+=Pmtrx[31]*(Q_001001000*PR_000000012000+Q_001101000*PR_000000012010+Q_101001000*PR_000000012100+Q_101101000*PR_000000012110); ans_temp[ans_id*3+2]+=Pmtrx[32]*(Q_000002000*PR_000000012000+Q_000102000*PR_000000012010+Q_000202000*PR_000000012020); ans_temp[ans_id*3+2]+=Pmtrx[33]*(Q_001000001*PR_000000012000+Q_001000101*PR_000000012001+Q_101000001*PR_000000012100+Q_101000101*PR_000000012101); ans_temp[ans_id*3+2]+=Pmtrx[34]*(Q_000001001*PR_000000012000+Q_000001101*PR_000000012001+Q_000101001*PR_000000012010+Q_000101101*PR_000000012011); ans_temp[ans_id*3+2]+=Pmtrx[35]*(Q_000000002*PR_000000012000+Q_000000102*PR_000000012001+Q_000000202*PR_000000012002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<3;ians++){ ans_temp[tId_x*3+ians]+=ans_temp[(tId_x+num_thread)*3+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<3;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*3+ians]=ans_temp[(tId_x)*3+ians]; } } } } } __global__ void MD_Kq_pdsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*3]; for(int i=0;i<3;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_010[3]; Pd_010[0]=PA[ii*3+0]; Pd_010[1]=PA[ii*3+1]; Pd_010[2]=PA[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double QR_002000000003=Q_002000000*R_003[0]+-1*Q_102000000*R_103[0]+Q_202000000*R_203[0]; double QR_001001000003=Q_001001000*R_003[0]+-1*Q_001101000*R_013[0]+-1*Q_101001000*R_103[0]+Q_101101000*R_113[0]; double QR_000002000003=Q_000002000*R_003[0]+-1*Q_000102000*R_013[0]+Q_000202000*R_023[0]; double QR_001000001003=Q_001000001*R_003[0]+-1*Q_001000101*R_004[0]+-1*Q_101000001*R_103[0]+Q_101000101*R_104[0]; double QR_000001001003=Q_000001001*R_003[0]+-1*Q_000001101*R_004[0]+-1*Q_000101001*R_013[0]+Q_000101101*R_014[0]; double QR_000000002003=Q_000000002*R_003[0]+-1*Q_000000102*R_004[0]+Q_000000202*R_005[0]; double QR_002000000012=Q_002000000*R_012[0]+-1*Q_102000000*R_112[0]+Q_202000000*R_212[0]; double QR_001001000012=Q_001001000*R_012[0]+-1*Q_001101000*R_022[0]+-1*Q_101001000*R_112[0]+Q_101101000*R_122[0]; double QR_000002000012=Q_000002000*R_012[0]+-1*Q_000102000*R_022[0]+Q_000202000*R_032[0]; double QR_001000001012=Q_001000001*R_012[0]+-1*Q_001000101*R_013[0]+-1*Q_101000001*R_112[0]+Q_101000101*R_113[0]; double QR_000001001012=Q_000001001*R_012[0]+-1*Q_000001101*R_013[0]+-1*Q_000101001*R_022[0]+Q_000101101*R_023[0]; double QR_000000002012=Q_000000002*R_012[0]+-1*Q_000000102*R_013[0]+Q_000000202*R_014[0]; double QR_002000000021=Q_002000000*R_021[0]+-1*Q_102000000*R_121[0]+Q_202000000*R_221[0]; double QR_001001000021=Q_001001000*R_021[0]+-1*Q_001101000*R_031[0]+-1*Q_101001000*R_121[0]+Q_101101000*R_131[0]; double QR_000002000021=Q_000002000*R_021[0]+-1*Q_000102000*R_031[0]+Q_000202000*R_041[0]; double QR_001000001021=Q_001000001*R_021[0]+-1*Q_001000101*R_022[0]+-1*Q_101000001*R_121[0]+Q_101000101*R_122[0]; double QR_000001001021=Q_000001001*R_021[0]+-1*Q_000001101*R_022[0]+-1*Q_000101001*R_031[0]+Q_000101101*R_032[0]; double QR_000000002021=Q_000000002*R_021[0]+-1*Q_000000102*R_022[0]+Q_000000202*R_023[0]; double QR_002000000030=Q_002000000*R_030[0]+-1*Q_102000000*R_130[0]+Q_202000000*R_230[0]; double QR_001001000030=Q_001001000*R_030[0]+-1*Q_001101000*R_040[0]+-1*Q_101001000*R_130[0]+Q_101101000*R_140[0]; double QR_000002000030=Q_000002000*R_030[0]+-1*Q_000102000*R_040[0]+Q_000202000*R_050[0]; double QR_001000001030=Q_001000001*R_030[0]+-1*Q_001000101*R_031[0]+-1*Q_101000001*R_130[0]+Q_101000101*R_131[0]; double QR_000001001030=Q_000001001*R_030[0]+-1*Q_000001101*R_031[0]+-1*Q_000101001*R_040[0]+Q_000101101*R_041[0]; double QR_000000002030=Q_000000002*R_030[0]+-1*Q_000000102*R_031[0]+Q_000000202*R_032[0]; double QR_002000000102=Q_002000000*R_102[0]+-1*Q_102000000*R_202[0]+Q_202000000*R_302[0]; double QR_001001000102=Q_001001000*R_102[0]+-1*Q_001101000*R_112[0]+-1*Q_101001000*R_202[0]+Q_101101000*R_212[0]; double QR_000002000102=Q_000002000*R_102[0]+-1*Q_000102000*R_112[0]+Q_000202000*R_122[0]; double QR_001000001102=Q_001000001*R_102[0]+-1*Q_001000101*R_103[0]+-1*Q_101000001*R_202[0]+Q_101000101*R_203[0]; double QR_000001001102=Q_000001001*R_102[0]+-1*Q_000001101*R_103[0]+-1*Q_000101001*R_112[0]+Q_000101101*R_113[0]; double QR_000000002102=Q_000000002*R_102[0]+-1*Q_000000102*R_103[0]+Q_000000202*R_104[0]; double QR_002000000111=Q_002000000*R_111[0]+-1*Q_102000000*R_211[0]+Q_202000000*R_311[0]; double QR_001001000111=Q_001001000*R_111[0]+-1*Q_001101000*R_121[0]+-1*Q_101001000*R_211[0]+Q_101101000*R_221[0]; double QR_000002000111=Q_000002000*R_111[0]+-1*Q_000102000*R_121[0]+Q_000202000*R_131[0]; double QR_001000001111=Q_001000001*R_111[0]+-1*Q_001000101*R_112[0]+-1*Q_101000001*R_211[0]+Q_101000101*R_212[0]; double QR_000001001111=Q_000001001*R_111[0]+-1*Q_000001101*R_112[0]+-1*Q_000101001*R_121[0]+Q_000101101*R_122[0]; double QR_000000002111=Q_000000002*R_111[0]+-1*Q_000000102*R_112[0]+Q_000000202*R_113[0]; double QR_002000000120=Q_002000000*R_120[0]+-1*Q_102000000*R_220[0]+Q_202000000*R_320[0]; double QR_001001000120=Q_001001000*R_120[0]+-1*Q_001101000*R_130[0]+-1*Q_101001000*R_220[0]+Q_101101000*R_230[0]; double QR_000002000120=Q_000002000*R_120[0]+-1*Q_000102000*R_130[0]+Q_000202000*R_140[0]; double QR_001000001120=Q_001000001*R_120[0]+-1*Q_001000101*R_121[0]+-1*Q_101000001*R_220[0]+Q_101000101*R_221[0]; double QR_000001001120=Q_000001001*R_120[0]+-1*Q_000001101*R_121[0]+-1*Q_000101001*R_130[0]+Q_000101101*R_131[0]; double QR_000000002120=Q_000000002*R_120[0]+-1*Q_000000102*R_121[0]+Q_000000202*R_122[0]; double QR_002000000201=Q_002000000*R_201[0]+-1*Q_102000000*R_301[0]+Q_202000000*R_401[0]; double QR_001001000201=Q_001001000*R_201[0]+-1*Q_001101000*R_211[0]+-1*Q_101001000*R_301[0]+Q_101101000*R_311[0]; double QR_000002000201=Q_000002000*R_201[0]+-1*Q_000102000*R_211[0]+Q_000202000*R_221[0]; double QR_001000001201=Q_001000001*R_201[0]+-1*Q_001000101*R_202[0]+-1*Q_101000001*R_301[0]+Q_101000101*R_302[0]; double QR_000001001201=Q_000001001*R_201[0]+-1*Q_000001101*R_202[0]+-1*Q_000101001*R_211[0]+Q_000101101*R_212[0]; double QR_000000002201=Q_000000002*R_201[0]+-1*Q_000000102*R_202[0]+Q_000000202*R_203[0]; double QR_002000000210=Q_002000000*R_210[0]+-1*Q_102000000*R_310[0]+Q_202000000*R_410[0]; double QR_001001000210=Q_001001000*R_210[0]+-1*Q_001101000*R_220[0]+-1*Q_101001000*R_310[0]+Q_101101000*R_320[0]; double QR_000002000210=Q_000002000*R_210[0]+-1*Q_000102000*R_220[0]+Q_000202000*R_230[0]; double QR_001000001210=Q_001000001*R_210[0]+-1*Q_001000101*R_211[0]+-1*Q_101000001*R_310[0]+Q_101000101*R_311[0]; double QR_000001001210=Q_000001001*R_210[0]+-1*Q_000001101*R_211[0]+-1*Q_000101001*R_220[0]+Q_000101101*R_221[0]; double QR_000000002210=Q_000000002*R_210[0]+-1*Q_000000102*R_211[0]+Q_000000202*R_212[0]; double QR_002000000300=Q_002000000*R_300[0]+-1*Q_102000000*R_400[0]+Q_202000000*R_500[0]; double QR_001001000300=Q_001001000*R_300[0]+-1*Q_001101000*R_310[0]+-1*Q_101001000*R_400[0]+Q_101101000*R_410[0]; double QR_000002000300=Q_000002000*R_300[0]+-1*Q_000102000*R_310[0]+Q_000202000*R_320[0]; double QR_001000001300=Q_001000001*R_300[0]+-1*Q_001000101*R_301[0]+-1*Q_101000001*R_400[0]+Q_101000101*R_401[0]; double QR_000001001300=Q_000001001*R_300[0]+-1*Q_000001101*R_301[0]+-1*Q_000101001*R_310[0]+Q_000101101*R_311[0]; double QR_000000002300=Q_000000002*R_300[0]+-1*Q_000000102*R_301[0]+Q_000000202*R_302[0]; double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } double P_012000000=Pd_012[0]; double P_112000000=Pd_112[0]; double P_212000000=Pd_212[0]; double P_312000000=Pd_312[0]; double P_011001000=Pd_011[0]*Pd_001[1]; double P_011101000=Pd_011[0]*Pd_101[1]; double P_111001000=Pd_111[0]*Pd_001[1]; double P_111101000=Pd_111[0]*Pd_101[1]; double P_211001000=Pd_211[0]*Pd_001[1]; double P_211101000=Pd_211[0]*Pd_101[1]; double P_010002000=Pd_010[0]*Pd_002[1]; double P_010102000=Pd_010[0]*Pd_102[1]; double P_010202000=Pd_010[0]*Pd_202[1]; double P_110002000=Pd_110[0]*Pd_002[1]; double P_110102000=Pd_110[0]*Pd_102[1]; double P_110202000=Pd_110[0]*Pd_202[1]; double P_011000001=Pd_011[0]*Pd_001[2]; double P_011000101=Pd_011[0]*Pd_101[2]; double P_111000001=Pd_111[0]*Pd_001[2]; double P_111000101=Pd_111[0]*Pd_101[2]; double P_211000001=Pd_211[0]*Pd_001[2]; double P_211000101=Pd_211[0]*Pd_101[2]; double P_010001001=Pd_010[0]*Pd_001[1]*Pd_001[2]; double P_010001101=Pd_010[0]*Pd_001[1]*Pd_101[2]; double P_010101001=Pd_010[0]*Pd_101[1]*Pd_001[2]; double P_010101101=Pd_010[0]*Pd_101[1]*Pd_101[2]; double P_110001001=Pd_110[0]*Pd_001[1]*Pd_001[2]; double P_110001101=Pd_110[0]*Pd_001[1]*Pd_101[2]; double P_110101001=Pd_110[0]*Pd_101[1]*Pd_001[2]; double P_110101101=Pd_110[0]*Pd_101[1]*Pd_101[2]; double P_010000002=Pd_010[0]*Pd_002[2]; double P_010000102=Pd_010[0]*Pd_102[2]; double P_010000202=Pd_010[0]*Pd_202[2]; double P_110000002=Pd_110[0]*Pd_002[2]; double P_110000102=Pd_110[0]*Pd_102[2]; double P_110000202=Pd_110[0]*Pd_202[2]; double P_002010000=Pd_002[0]*Pd_010[1]; double P_002110000=Pd_002[0]*Pd_110[1]; double P_102010000=Pd_102[0]*Pd_010[1]; double P_102110000=Pd_102[0]*Pd_110[1]; double P_202010000=Pd_202[0]*Pd_010[1]; double P_202110000=Pd_202[0]*Pd_110[1]; double P_001011000=Pd_001[0]*Pd_011[1]; double P_001111000=Pd_001[0]*Pd_111[1]; double P_001211000=Pd_001[0]*Pd_211[1]; double P_101011000=Pd_101[0]*Pd_011[1]; double P_101111000=Pd_101[0]*Pd_111[1]; double P_101211000=Pd_101[0]*Pd_211[1]; double P_000012000=Pd_012[1]; double P_000112000=Pd_112[1]; double P_000212000=Pd_212[1]; double P_000312000=Pd_312[1]; double P_001010001=Pd_001[0]*Pd_010[1]*Pd_001[2]; double P_001010101=Pd_001[0]*Pd_010[1]*Pd_101[2]; double P_001110001=Pd_001[0]*Pd_110[1]*Pd_001[2]; double P_001110101=Pd_001[0]*Pd_110[1]*Pd_101[2]; double P_101010001=Pd_101[0]*Pd_010[1]*Pd_001[2]; double P_101010101=Pd_101[0]*Pd_010[1]*Pd_101[2]; double P_101110001=Pd_101[0]*Pd_110[1]*Pd_001[2]; double P_101110101=Pd_101[0]*Pd_110[1]*Pd_101[2]; double P_000011001=Pd_011[1]*Pd_001[2]; double P_000011101=Pd_011[1]*Pd_101[2]; double P_000111001=Pd_111[1]*Pd_001[2]; double P_000111101=Pd_111[1]*Pd_101[2]; double P_000211001=Pd_211[1]*Pd_001[2]; double P_000211101=Pd_211[1]*Pd_101[2]; double P_000010002=Pd_010[1]*Pd_002[2]; double P_000010102=Pd_010[1]*Pd_102[2]; double P_000010202=Pd_010[1]*Pd_202[2]; double P_000110002=Pd_110[1]*Pd_002[2]; double P_000110102=Pd_110[1]*Pd_102[2]; double P_000110202=Pd_110[1]*Pd_202[2]; double P_002000010=Pd_002[0]*Pd_010[2]; double P_002000110=Pd_002[0]*Pd_110[2]; double P_102000010=Pd_102[0]*Pd_010[2]; double P_102000110=Pd_102[0]*Pd_110[2]; double P_202000010=Pd_202[0]*Pd_010[2]; double P_202000110=Pd_202[0]*Pd_110[2]; double P_001001010=Pd_001[0]*Pd_001[1]*Pd_010[2]; double P_001001110=Pd_001[0]*Pd_001[1]*Pd_110[2]; double P_001101010=Pd_001[0]*Pd_101[1]*Pd_010[2]; double P_001101110=Pd_001[0]*Pd_101[1]*Pd_110[2]; double P_101001010=Pd_101[0]*Pd_001[1]*Pd_010[2]; double P_101001110=Pd_101[0]*Pd_001[1]*Pd_110[2]; double P_101101010=Pd_101[0]*Pd_101[1]*Pd_010[2]; double P_101101110=Pd_101[0]*Pd_101[1]*Pd_110[2]; double P_000002010=Pd_002[1]*Pd_010[2]; double P_000002110=Pd_002[1]*Pd_110[2]; double P_000102010=Pd_102[1]*Pd_010[2]; double P_000102110=Pd_102[1]*Pd_110[2]; double P_000202010=Pd_202[1]*Pd_010[2]; double P_000202110=Pd_202[1]*Pd_110[2]; double P_001000011=Pd_001[0]*Pd_011[2]; double P_001000111=Pd_001[0]*Pd_111[2]; double P_001000211=Pd_001[0]*Pd_211[2]; double P_101000011=Pd_101[0]*Pd_011[2]; double P_101000111=Pd_101[0]*Pd_111[2]; double P_101000211=Pd_101[0]*Pd_211[2]; double P_000001011=Pd_001[1]*Pd_011[2]; double P_000001111=Pd_001[1]*Pd_111[2]; double P_000001211=Pd_001[1]*Pd_211[2]; double P_000101011=Pd_101[1]*Pd_011[2]; double P_000101111=Pd_101[1]*Pd_111[2]; double P_000101211=Pd_101[1]*Pd_211[2]; double P_000000012=Pd_012[2]; double P_000000112=Pd_112[2]; double P_000000212=Pd_212[2]; double P_000000312=Pd_312[2]; ans_temp[ans_id*3+0]+=Pmtrx[0]*(P_012000000*QR_002000000000+P_112000000*QR_002000000100+P_212000000*QR_002000000200+P_312000000*QR_002000000300); ans_temp[ans_id*3+0]+=Pmtrx[1]*(P_012000000*QR_001001000000+P_112000000*QR_001001000100+P_212000000*QR_001001000200+P_312000000*QR_001001000300); ans_temp[ans_id*3+0]+=Pmtrx[2]*(P_012000000*QR_000002000000+P_112000000*QR_000002000100+P_212000000*QR_000002000200+P_312000000*QR_000002000300); ans_temp[ans_id*3+0]+=Pmtrx[3]*(P_012000000*QR_001000001000+P_112000000*QR_001000001100+P_212000000*QR_001000001200+P_312000000*QR_001000001300); ans_temp[ans_id*3+0]+=Pmtrx[4]*(P_012000000*QR_000001001000+P_112000000*QR_000001001100+P_212000000*QR_000001001200+P_312000000*QR_000001001300); ans_temp[ans_id*3+0]+=Pmtrx[5]*(P_012000000*QR_000000002000+P_112000000*QR_000000002100+P_212000000*QR_000000002200+P_312000000*QR_000000002300); ans_temp[ans_id*3+0]+=Pmtrx[6]*(P_011001000*QR_002000000000+P_011101000*QR_002000000010+P_111001000*QR_002000000100+P_111101000*QR_002000000110+P_211001000*QR_002000000200+P_211101000*QR_002000000210); ans_temp[ans_id*3+0]+=Pmtrx[7]*(P_011001000*QR_001001000000+P_011101000*QR_001001000010+P_111001000*QR_001001000100+P_111101000*QR_001001000110+P_211001000*QR_001001000200+P_211101000*QR_001001000210); ans_temp[ans_id*3+0]+=Pmtrx[8]*(P_011001000*QR_000002000000+P_011101000*QR_000002000010+P_111001000*QR_000002000100+P_111101000*QR_000002000110+P_211001000*QR_000002000200+P_211101000*QR_000002000210); ans_temp[ans_id*3+0]+=Pmtrx[9]*(P_011001000*QR_001000001000+P_011101000*QR_001000001010+P_111001000*QR_001000001100+P_111101000*QR_001000001110+P_211001000*QR_001000001200+P_211101000*QR_001000001210); ans_temp[ans_id*3+0]+=Pmtrx[10]*(P_011001000*QR_000001001000+P_011101000*QR_000001001010+P_111001000*QR_000001001100+P_111101000*QR_000001001110+P_211001000*QR_000001001200+P_211101000*QR_000001001210); ans_temp[ans_id*3+0]+=Pmtrx[11]*(P_011001000*QR_000000002000+P_011101000*QR_000000002010+P_111001000*QR_000000002100+P_111101000*QR_000000002110+P_211001000*QR_000000002200+P_211101000*QR_000000002210); ans_temp[ans_id*3+0]+=Pmtrx[12]*(P_010002000*QR_002000000000+P_010102000*QR_002000000010+P_010202000*QR_002000000020+P_110002000*QR_002000000100+P_110102000*QR_002000000110+P_110202000*QR_002000000120); ans_temp[ans_id*3+0]+=Pmtrx[13]*(P_010002000*QR_001001000000+P_010102000*QR_001001000010+P_010202000*QR_001001000020+P_110002000*QR_001001000100+P_110102000*QR_001001000110+P_110202000*QR_001001000120); ans_temp[ans_id*3+0]+=Pmtrx[14]*(P_010002000*QR_000002000000+P_010102000*QR_000002000010+P_010202000*QR_000002000020+P_110002000*QR_000002000100+P_110102000*QR_000002000110+P_110202000*QR_000002000120); ans_temp[ans_id*3+0]+=Pmtrx[15]*(P_010002000*QR_001000001000+P_010102000*QR_001000001010+P_010202000*QR_001000001020+P_110002000*QR_001000001100+P_110102000*QR_001000001110+P_110202000*QR_001000001120); ans_temp[ans_id*3+0]+=Pmtrx[16]*(P_010002000*QR_000001001000+P_010102000*QR_000001001010+P_010202000*QR_000001001020+P_110002000*QR_000001001100+P_110102000*QR_000001001110+P_110202000*QR_000001001120); ans_temp[ans_id*3+0]+=Pmtrx[17]*(P_010002000*QR_000000002000+P_010102000*QR_000000002010+P_010202000*QR_000000002020+P_110002000*QR_000000002100+P_110102000*QR_000000002110+P_110202000*QR_000000002120); ans_temp[ans_id*3+0]+=Pmtrx[18]*(P_011000001*QR_002000000000+P_011000101*QR_002000000001+P_111000001*QR_002000000100+P_111000101*QR_002000000101+P_211000001*QR_002000000200+P_211000101*QR_002000000201); ans_temp[ans_id*3+0]+=Pmtrx[19]*(P_011000001*QR_001001000000+P_011000101*QR_001001000001+P_111000001*QR_001001000100+P_111000101*QR_001001000101+P_211000001*QR_001001000200+P_211000101*QR_001001000201); ans_temp[ans_id*3+0]+=Pmtrx[20]*(P_011000001*QR_000002000000+P_011000101*QR_000002000001+P_111000001*QR_000002000100+P_111000101*QR_000002000101+P_211000001*QR_000002000200+P_211000101*QR_000002000201); ans_temp[ans_id*3+0]+=Pmtrx[21]*(P_011000001*QR_001000001000+P_011000101*QR_001000001001+P_111000001*QR_001000001100+P_111000101*QR_001000001101+P_211000001*QR_001000001200+P_211000101*QR_001000001201); ans_temp[ans_id*3+0]+=Pmtrx[22]*(P_011000001*QR_000001001000+P_011000101*QR_000001001001+P_111000001*QR_000001001100+P_111000101*QR_000001001101+P_211000001*QR_000001001200+P_211000101*QR_000001001201); ans_temp[ans_id*3+0]+=Pmtrx[23]*(P_011000001*QR_000000002000+P_011000101*QR_000000002001+P_111000001*QR_000000002100+P_111000101*QR_000000002101+P_211000001*QR_000000002200+P_211000101*QR_000000002201); ans_temp[ans_id*3+0]+=Pmtrx[24]*(P_010001001*QR_002000000000+P_010001101*QR_002000000001+P_010101001*QR_002000000010+P_010101101*QR_002000000011+P_110001001*QR_002000000100+P_110001101*QR_002000000101+P_110101001*QR_002000000110+P_110101101*QR_002000000111); ans_temp[ans_id*3+0]+=Pmtrx[25]*(P_010001001*QR_001001000000+P_010001101*QR_001001000001+P_010101001*QR_001001000010+P_010101101*QR_001001000011+P_110001001*QR_001001000100+P_110001101*QR_001001000101+P_110101001*QR_001001000110+P_110101101*QR_001001000111); ans_temp[ans_id*3+0]+=Pmtrx[26]*(P_010001001*QR_000002000000+P_010001101*QR_000002000001+P_010101001*QR_000002000010+P_010101101*QR_000002000011+P_110001001*QR_000002000100+P_110001101*QR_000002000101+P_110101001*QR_000002000110+P_110101101*QR_000002000111); ans_temp[ans_id*3+0]+=Pmtrx[27]*(P_010001001*QR_001000001000+P_010001101*QR_001000001001+P_010101001*QR_001000001010+P_010101101*QR_001000001011+P_110001001*QR_001000001100+P_110001101*QR_001000001101+P_110101001*QR_001000001110+P_110101101*QR_001000001111); ans_temp[ans_id*3+0]+=Pmtrx[28]*(P_010001001*QR_000001001000+P_010001101*QR_000001001001+P_010101001*QR_000001001010+P_010101101*QR_000001001011+P_110001001*QR_000001001100+P_110001101*QR_000001001101+P_110101001*QR_000001001110+P_110101101*QR_000001001111); ans_temp[ans_id*3+0]+=Pmtrx[29]*(P_010001001*QR_000000002000+P_010001101*QR_000000002001+P_010101001*QR_000000002010+P_010101101*QR_000000002011+P_110001001*QR_000000002100+P_110001101*QR_000000002101+P_110101001*QR_000000002110+P_110101101*QR_000000002111); ans_temp[ans_id*3+0]+=Pmtrx[30]*(P_010000002*QR_002000000000+P_010000102*QR_002000000001+P_010000202*QR_002000000002+P_110000002*QR_002000000100+P_110000102*QR_002000000101+P_110000202*QR_002000000102); ans_temp[ans_id*3+0]+=Pmtrx[31]*(P_010000002*QR_001001000000+P_010000102*QR_001001000001+P_010000202*QR_001001000002+P_110000002*QR_001001000100+P_110000102*QR_001001000101+P_110000202*QR_001001000102); ans_temp[ans_id*3+0]+=Pmtrx[32]*(P_010000002*QR_000002000000+P_010000102*QR_000002000001+P_010000202*QR_000002000002+P_110000002*QR_000002000100+P_110000102*QR_000002000101+P_110000202*QR_000002000102); ans_temp[ans_id*3+0]+=Pmtrx[33]*(P_010000002*QR_001000001000+P_010000102*QR_001000001001+P_010000202*QR_001000001002+P_110000002*QR_001000001100+P_110000102*QR_001000001101+P_110000202*QR_001000001102); ans_temp[ans_id*3+0]+=Pmtrx[34]*(P_010000002*QR_000001001000+P_010000102*QR_000001001001+P_010000202*QR_000001001002+P_110000002*QR_000001001100+P_110000102*QR_000001001101+P_110000202*QR_000001001102); ans_temp[ans_id*3+0]+=Pmtrx[35]*(P_010000002*QR_000000002000+P_010000102*QR_000000002001+P_010000202*QR_000000002002+P_110000002*QR_000000002100+P_110000102*QR_000000002101+P_110000202*QR_000000002102); ans_temp[ans_id*3+1]+=Pmtrx[0]*(P_002010000*QR_002000000000+P_002110000*QR_002000000010+P_102010000*QR_002000000100+P_102110000*QR_002000000110+P_202010000*QR_002000000200+P_202110000*QR_002000000210); ans_temp[ans_id*3+1]+=Pmtrx[1]*(P_002010000*QR_001001000000+P_002110000*QR_001001000010+P_102010000*QR_001001000100+P_102110000*QR_001001000110+P_202010000*QR_001001000200+P_202110000*QR_001001000210); ans_temp[ans_id*3+1]+=Pmtrx[2]*(P_002010000*QR_000002000000+P_002110000*QR_000002000010+P_102010000*QR_000002000100+P_102110000*QR_000002000110+P_202010000*QR_000002000200+P_202110000*QR_000002000210); ans_temp[ans_id*3+1]+=Pmtrx[3]*(P_002010000*QR_001000001000+P_002110000*QR_001000001010+P_102010000*QR_001000001100+P_102110000*QR_001000001110+P_202010000*QR_001000001200+P_202110000*QR_001000001210); ans_temp[ans_id*3+1]+=Pmtrx[4]*(P_002010000*QR_000001001000+P_002110000*QR_000001001010+P_102010000*QR_000001001100+P_102110000*QR_000001001110+P_202010000*QR_000001001200+P_202110000*QR_000001001210); ans_temp[ans_id*3+1]+=Pmtrx[5]*(P_002010000*QR_000000002000+P_002110000*QR_000000002010+P_102010000*QR_000000002100+P_102110000*QR_000000002110+P_202010000*QR_000000002200+P_202110000*QR_000000002210); ans_temp[ans_id*3+1]+=Pmtrx[6]*(P_001011000*QR_002000000000+P_001111000*QR_002000000010+P_001211000*QR_002000000020+P_101011000*QR_002000000100+P_101111000*QR_002000000110+P_101211000*QR_002000000120); ans_temp[ans_id*3+1]+=Pmtrx[7]*(P_001011000*QR_001001000000+P_001111000*QR_001001000010+P_001211000*QR_001001000020+P_101011000*QR_001001000100+P_101111000*QR_001001000110+P_101211000*QR_001001000120); ans_temp[ans_id*3+1]+=Pmtrx[8]*(P_001011000*QR_000002000000+P_001111000*QR_000002000010+P_001211000*QR_000002000020+P_101011000*QR_000002000100+P_101111000*QR_000002000110+P_101211000*QR_000002000120); ans_temp[ans_id*3+1]+=Pmtrx[9]*(P_001011000*QR_001000001000+P_001111000*QR_001000001010+P_001211000*QR_001000001020+P_101011000*QR_001000001100+P_101111000*QR_001000001110+P_101211000*QR_001000001120); ans_temp[ans_id*3+1]+=Pmtrx[10]*(P_001011000*QR_000001001000+P_001111000*QR_000001001010+P_001211000*QR_000001001020+P_101011000*QR_000001001100+P_101111000*QR_000001001110+P_101211000*QR_000001001120); ans_temp[ans_id*3+1]+=Pmtrx[11]*(P_001011000*QR_000000002000+P_001111000*QR_000000002010+P_001211000*QR_000000002020+P_101011000*QR_000000002100+P_101111000*QR_000000002110+P_101211000*QR_000000002120); ans_temp[ans_id*3+1]+=Pmtrx[12]*(P_000012000*QR_002000000000+P_000112000*QR_002000000010+P_000212000*QR_002000000020+P_000312000*QR_002000000030); ans_temp[ans_id*3+1]+=Pmtrx[13]*(P_000012000*QR_001001000000+P_000112000*QR_001001000010+P_000212000*QR_001001000020+P_000312000*QR_001001000030); ans_temp[ans_id*3+1]+=Pmtrx[14]*(P_000012000*QR_000002000000+P_000112000*QR_000002000010+P_000212000*QR_000002000020+P_000312000*QR_000002000030); ans_temp[ans_id*3+1]+=Pmtrx[15]*(P_000012000*QR_001000001000+P_000112000*QR_001000001010+P_000212000*QR_001000001020+P_000312000*QR_001000001030); ans_temp[ans_id*3+1]+=Pmtrx[16]*(P_000012000*QR_000001001000+P_000112000*QR_000001001010+P_000212000*QR_000001001020+P_000312000*QR_000001001030); ans_temp[ans_id*3+1]+=Pmtrx[17]*(P_000012000*QR_000000002000+P_000112000*QR_000000002010+P_000212000*QR_000000002020+P_000312000*QR_000000002030); ans_temp[ans_id*3+1]+=Pmtrx[18]*(P_001010001*QR_002000000000+P_001010101*QR_002000000001+P_001110001*QR_002000000010+P_001110101*QR_002000000011+P_101010001*QR_002000000100+P_101010101*QR_002000000101+P_101110001*QR_002000000110+P_101110101*QR_002000000111); ans_temp[ans_id*3+1]+=Pmtrx[19]*(P_001010001*QR_001001000000+P_001010101*QR_001001000001+P_001110001*QR_001001000010+P_001110101*QR_001001000011+P_101010001*QR_001001000100+P_101010101*QR_001001000101+P_101110001*QR_001001000110+P_101110101*QR_001001000111); ans_temp[ans_id*3+1]+=Pmtrx[20]*(P_001010001*QR_000002000000+P_001010101*QR_000002000001+P_001110001*QR_000002000010+P_001110101*QR_000002000011+P_101010001*QR_000002000100+P_101010101*QR_000002000101+P_101110001*QR_000002000110+P_101110101*QR_000002000111); ans_temp[ans_id*3+1]+=Pmtrx[21]*(P_001010001*QR_001000001000+P_001010101*QR_001000001001+P_001110001*QR_001000001010+P_001110101*QR_001000001011+P_101010001*QR_001000001100+P_101010101*QR_001000001101+P_101110001*QR_001000001110+P_101110101*QR_001000001111); ans_temp[ans_id*3+1]+=Pmtrx[22]*(P_001010001*QR_000001001000+P_001010101*QR_000001001001+P_001110001*QR_000001001010+P_001110101*QR_000001001011+P_101010001*QR_000001001100+P_101010101*QR_000001001101+P_101110001*QR_000001001110+P_101110101*QR_000001001111); ans_temp[ans_id*3+1]+=Pmtrx[23]*(P_001010001*QR_000000002000+P_001010101*QR_000000002001+P_001110001*QR_000000002010+P_001110101*QR_000000002011+P_101010001*QR_000000002100+P_101010101*QR_000000002101+P_101110001*QR_000000002110+P_101110101*QR_000000002111); ans_temp[ans_id*3+1]+=Pmtrx[24]*(P_000011001*QR_002000000000+P_000011101*QR_002000000001+P_000111001*QR_002000000010+P_000111101*QR_002000000011+P_000211001*QR_002000000020+P_000211101*QR_002000000021); ans_temp[ans_id*3+1]+=Pmtrx[25]*(P_000011001*QR_001001000000+P_000011101*QR_001001000001+P_000111001*QR_001001000010+P_000111101*QR_001001000011+P_000211001*QR_001001000020+P_000211101*QR_001001000021); ans_temp[ans_id*3+1]+=Pmtrx[26]*(P_000011001*QR_000002000000+P_000011101*QR_000002000001+P_000111001*QR_000002000010+P_000111101*QR_000002000011+P_000211001*QR_000002000020+P_000211101*QR_000002000021); ans_temp[ans_id*3+1]+=Pmtrx[27]*(P_000011001*QR_001000001000+P_000011101*QR_001000001001+P_000111001*QR_001000001010+P_000111101*QR_001000001011+P_000211001*QR_001000001020+P_000211101*QR_001000001021); ans_temp[ans_id*3+1]+=Pmtrx[28]*(P_000011001*QR_000001001000+P_000011101*QR_000001001001+P_000111001*QR_000001001010+P_000111101*QR_000001001011+P_000211001*QR_000001001020+P_000211101*QR_000001001021); ans_temp[ans_id*3+1]+=Pmtrx[29]*(P_000011001*QR_000000002000+P_000011101*QR_000000002001+P_000111001*QR_000000002010+P_000111101*QR_000000002011+P_000211001*QR_000000002020+P_000211101*QR_000000002021); ans_temp[ans_id*3+1]+=Pmtrx[30]*(P_000010002*QR_002000000000+P_000010102*QR_002000000001+P_000010202*QR_002000000002+P_000110002*QR_002000000010+P_000110102*QR_002000000011+P_000110202*QR_002000000012); ans_temp[ans_id*3+1]+=Pmtrx[31]*(P_000010002*QR_001001000000+P_000010102*QR_001001000001+P_000010202*QR_001001000002+P_000110002*QR_001001000010+P_000110102*QR_001001000011+P_000110202*QR_001001000012); ans_temp[ans_id*3+1]+=Pmtrx[32]*(P_000010002*QR_000002000000+P_000010102*QR_000002000001+P_000010202*QR_000002000002+P_000110002*QR_000002000010+P_000110102*QR_000002000011+P_000110202*QR_000002000012); ans_temp[ans_id*3+1]+=Pmtrx[33]*(P_000010002*QR_001000001000+P_000010102*QR_001000001001+P_000010202*QR_001000001002+P_000110002*QR_001000001010+P_000110102*QR_001000001011+P_000110202*QR_001000001012); ans_temp[ans_id*3+1]+=Pmtrx[34]*(P_000010002*QR_000001001000+P_000010102*QR_000001001001+P_000010202*QR_000001001002+P_000110002*QR_000001001010+P_000110102*QR_000001001011+P_000110202*QR_000001001012); ans_temp[ans_id*3+1]+=Pmtrx[35]*(P_000010002*QR_000000002000+P_000010102*QR_000000002001+P_000010202*QR_000000002002+P_000110002*QR_000000002010+P_000110102*QR_000000002011+P_000110202*QR_000000002012); ans_temp[ans_id*3+2]+=Pmtrx[0]*(P_002000010*QR_002000000000+P_002000110*QR_002000000001+P_102000010*QR_002000000100+P_102000110*QR_002000000101+P_202000010*QR_002000000200+P_202000110*QR_002000000201); ans_temp[ans_id*3+2]+=Pmtrx[1]*(P_002000010*QR_001001000000+P_002000110*QR_001001000001+P_102000010*QR_001001000100+P_102000110*QR_001001000101+P_202000010*QR_001001000200+P_202000110*QR_001001000201); ans_temp[ans_id*3+2]+=Pmtrx[2]*(P_002000010*QR_000002000000+P_002000110*QR_000002000001+P_102000010*QR_000002000100+P_102000110*QR_000002000101+P_202000010*QR_000002000200+P_202000110*QR_000002000201); ans_temp[ans_id*3+2]+=Pmtrx[3]*(P_002000010*QR_001000001000+P_002000110*QR_001000001001+P_102000010*QR_001000001100+P_102000110*QR_001000001101+P_202000010*QR_001000001200+P_202000110*QR_001000001201); ans_temp[ans_id*3+2]+=Pmtrx[4]*(P_002000010*QR_000001001000+P_002000110*QR_000001001001+P_102000010*QR_000001001100+P_102000110*QR_000001001101+P_202000010*QR_000001001200+P_202000110*QR_000001001201); ans_temp[ans_id*3+2]+=Pmtrx[5]*(P_002000010*QR_000000002000+P_002000110*QR_000000002001+P_102000010*QR_000000002100+P_102000110*QR_000000002101+P_202000010*QR_000000002200+P_202000110*QR_000000002201); ans_temp[ans_id*3+2]+=Pmtrx[6]*(P_001001010*QR_002000000000+P_001001110*QR_002000000001+P_001101010*QR_002000000010+P_001101110*QR_002000000011+P_101001010*QR_002000000100+P_101001110*QR_002000000101+P_101101010*QR_002000000110+P_101101110*QR_002000000111); ans_temp[ans_id*3+2]+=Pmtrx[7]*(P_001001010*QR_001001000000+P_001001110*QR_001001000001+P_001101010*QR_001001000010+P_001101110*QR_001001000011+P_101001010*QR_001001000100+P_101001110*QR_001001000101+P_101101010*QR_001001000110+P_101101110*QR_001001000111); ans_temp[ans_id*3+2]+=Pmtrx[8]*(P_001001010*QR_000002000000+P_001001110*QR_000002000001+P_001101010*QR_000002000010+P_001101110*QR_000002000011+P_101001010*QR_000002000100+P_101001110*QR_000002000101+P_101101010*QR_000002000110+P_101101110*QR_000002000111); ans_temp[ans_id*3+2]+=Pmtrx[9]*(P_001001010*QR_001000001000+P_001001110*QR_001000001001+P_001101010*QR_001000001010+P_001101110*QR_001000001011+P_101001010*QR_001000001100+P_101001110*QR_001000001101+P_101101010*QR_001000001110+P_101101110*QR_001000001111); ans_temp[ans_id*3+2]+=Pmtrx[10]*(P_001001010*QR_000001001000+P_001001110*QR_000001001001+P_001101010*QR_000001001010+P_001101110*QR_000001001011+P_101001010*QR_000001001100+P_101001110*QR_000001001101+P_101101010*QR_000001001110+P_101101110*QR_000001001111); ans_temp[ans_id*3+2]+=Pmtrx[11]*(P_001001010*QR_000000002000+P_001001110*QR_000000002001+P_001101010*QR_000000002010+P_001101110*QR_000000002011+P_101001010*QR_000000002100+P_101001110*QR_000000002101+P_101101010*QR_000000002110+P_101101110*QR_000000002111); ans_temp[ans_id*3+2]+=Pmtrx[12]*(P_000002010*QR_002000000000+P_000002110*QR_002000000001+P_000102010*QR_002000000010+P_000102110*QR_002000000011+P_000202010*QR_002000000020+P_000202110*QR_002000000021); ans_temp[ans_id*3+2]+=Pmtrx[13]*(P_000002010*QR_001001000000+P_000002110*QR_001001000001+P_000102010*QR_001001000010+P_000102110*QR_001001000011+P_000202010*QR_001001000020+P_000202110*QR_001001000021); ans_temp[ans_id*3+2]+=Pmtrx[14]*(P_000002010*QR_000002000000+P_000002110*QR_000002000001+P_000102010*QR_000002000010+P_000102110*QR_000002000011+P_000202010*QR_000002000020+P_000202110*QR_000002000021); ans_temp[ans_id*3+2]+=Pmtrx[15]*(P_000002010*QR_001000001000+P_000002110*QR_001000001001+P_000102010*QR_001000001010+P_000102110*QR_001000001011+P_000202010*QR_001000001020+P_000202110*QR_001000001021); ans_temp[ans_id*3+2]+=Pmtrx[16]*(P_000002010*QR_000001001000+P_000002110*QR_000001001001+P_000102010*QR_000001001010+P_000102110*QR_000001001011+P_000202010*QR_000001001020+P_000202110*QR_000001001021); ans_temp[ans_id*3+2]+=Pmtrx[17]*(P_000002010*QR_000000002000+P_000002110*QR_000000002001+P_000102010*QR_000000002010+P_000102110*QR_000000002011+P_000202010*QR_000000002020+P_000202110*QR_000000002021); ans_temp[ans_id*3+2]+=Pmtrx[18]*(P_001000011*QR_002000000000+P_001000111*QR_002000000001+P_001000211*QR_002000000002+P_101000011*QR_002000000100+P_101000111*QR_002000000101+P_101000211*QR_002000000102); ans_temp[ans_id*3+2]+=Pmtrx[19]*(P_001000011*QR_001001000000+P_001000111*QR_001001000001+P_001000211*QR_001001000002+P_101000011*QR_001001000100+P_101000111*QR_001001000101+P_101000211*QR_001001000102); ans_temp[ans_id*3+2]+=Pmtrx[20]*(P_001000011*QR_000002000000+P_001000111*QR_000002000001+P_001000211*QR_000002000002+P_101000011*QR_000002000100+P_101000111*QR_000002000101+P_101000211*QR_000002000102); ans_temp[ans_id*3+2]+=Pmtrx[21]*(P_001000011*QR_001000001000+P_001000111*QR_001000001001+P_001000211*QR_001000001002+P_101000011*QR_001000001100+P_101000111*QR_001000001101+P_101000211*QR_001000001102); ans_temp[ans_id*3+2]+=Pmtrx[22]*(P_001000011*QR_000001001000+P_001000111*QR_000001001001+P_001000211*QR_000001001002+P_101000011*QR_000001001100+P_101000111*QR_000001001101+P_101000211*QR_000001001102); ans_temp[ans_id*3+2]+=Pmtrx[23]*(P_001000011*QR_000000002000+P_001000111*QR_000000002001+P_001000211*QR_000000002002+P_101000011*QR_000000002100+P_101000111*QR_000000002101+P_101000211*QR_000000002102); ans_temp[ans_id*3+2]+=Pmtrx[24]*(P_000001011*QR_002000000000+P_000001111*QR_002000000001+P_000001211*QR_002000000002+P_000101011*QR_002000000010+P_000101111*QR_002000000011+P_000101211*QR_002000000012); ans_temp[ans_id*3+2]+=Pmtrx[25]*(P_000001011*QR_001001000000+P_000001111*QR_001001000001+P_000001211*QR_001001000002+P_000101011*QR_001001000010+P_000101111*QR_001001000011+P_000101211*QR_001001000012); ans_temp[ans_id*3+2]+=Pmtrx[26]*(P_000001011*QR_000002000000+P_000001111*QR_000002000001+P_000001211*QR_000002000002+P_000101011*QR_000002000010+P_000101111*QR_000002000011+P_000101211*QR_000002000012); ans_temp[ans_id*3+2]+=Pmtrx[27]*(P_000001011*QR_001000001000+P_000001111*QR_001000001001+P_000001211*QR_001000001002+P_000101011*QR_001000001010+P_000101111*QR_001000001011+P_000101211*QR_001000001012); ans_temp[ans_id*3+2]+=Pmtrx[28]*(P_000001011*QR_000001001000+P_000001111*QR_000001001001+P_000001211*QR_000001001002+P_000101011*QR_000001001010+P_000101111*QR_000001001011+P_000101211*QR_000001001012); ans_temp[ans_id*3+2]+=Pmtrx[29]*(P_000001011*QR_000000002000+P_000001111*QR_000000002001+P_000001211*QR_000000002002+P_000101011*QR_000000002010+P_000101111*QR_000000002011+P_000101211*QR_000000002012); ans_temp[ans_id*3+2]+=Pmtrx[30]*(P_000000012*QR_002000000000+P_000000112*QR_002000000001+P_000000212*QR_002000000002+P_000000312*QR_002000000003); ans_temp[ans_id*3+2]+=Pmtrx[31]*(P_000000012*QR_001001000000+P_000000112*QR_001001000001+P_000000212*QR_001001000002+P_000000312*QR_001001000003); ans_temp[ans_id*3+2]+=Pmtrx[32]*(P_000000012*QR_000002000000+P_000000112*QR_000002000001+P_000000212*QR_000002000002+P_000000312*QR_000002000003); ans_temp[ans_id*3+2]+=Pmtrx[33]*(P_000000012*QR_001000001000+P_000000112*QR_001000001001+P_000000212*QR_001000001002+P_000000312*QR_001000001003); ans_temp[ans_id*3+2]+=Pmtrx[34]*(P_000000012*QR_000001001000+P_000000112*QR_000001001001+P_000000212*QR_000001001002+P_000000312*QR_000001001003); ans_temp[ans_id*3+2]+=Pmtrx[35]*(P_000000012*QR_000000002000+P_000000112*QR_000000002001+P_000000212*QR_000000002002+P_000000312*QR_000000002003); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<3;ians++){ ans_temp[tId_x*3+ians]+=ans_temp[(tId_x+num_thread)*3+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<3;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*3+ians]=ans_temp[(tId_x)*3+ians]; } } } } } __global__ void MD_Kp_dpsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[18]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<3;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_010[3]; temp_int2=tex1Dfetch(tex_PA,jj*3+0); Pd_010[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+1); Pd_010[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+2); Pd_010[2]=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Pd_101[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } double P_021000000=Pd_021[0]; double P_121000000=Pd_121[0]; double P_221000000=Pd_221[0]; double P_321000000=Pd_321[0]; double P_020001000=Pd_020[0]*Pd_001[1]; double P_020101000=Pd_020[0]*Pd_101[1]; double P_120001000=Pd_120[0]*Pd_001[1]; double P_120101000=Pd_120[0]*Pd_101[1]; double P_220001000=Pd_220[0]*Pd_001[1]; double P_220101000=Pd_220[0]*Pd_101[1]; double P_020000001=Pd_020[0]*Pd_001[2]; double P_020000101=Pd_020[0]*Pd_101[2]; double P_120000001=Pd_120[0]*Pd_001[2]; double P_120000101=Pd_120[0]*Pd_101[2]; double P_220000001=Pd_220[0]*Pd_001[2]; double P_220000101=Pd_220[0]*Pd_101[2]; double P_011010000=Pd_011[0]*Pd_010[1]; double P_011110000=Pd_011[0]*Pd_110[1]; double P_111010000=Pd_111[0]*Pd_010[1]; double P_111110000=Pd_111[0]*Pd_110[1]; double P_211010000=Pd_211[0]*Pd_010[1]; double P_211110000=Pd_211[0]*Pd_110[1]; double P_010011000=Pd_010[0]*Pd_011[1]; double P_010111000=Pd_010[0]*Pd_111[1]; double P_010211000=Pd_010[0]*Pd_211[1]; double P_110011000=Pd_110[0]*Pd_011[1]; double P_110111000=Pd_110[0]*Pd_111[1]; double P_110211000=Pd_110[0]*Pd_211[1]; double P_010010001=Pd_010[0]*Pd_010[1]*Pd_001[2]; double P_010010101=Pd_010[0]*Pd_010[1]*Pd_101[2]; double P_010110001=Pd_010[0]*Pd_110[1]*Pd_001[2]; double P_010110101=Pd_010[0]*Pd_110[1]*Pd_101[2]; double P_110010001=Pd_110[0]*Pd_010[1]*Pd_001[2]; double P_110010101=Pd_110[0]*Pd_010[1]*Pd_101[2]; double P_110110001=Pd_110[0]*Pd_110[1]*Pd_001[2]; double P_110110101=Pd_110[0]*Pd_110[1]*Pd_101[2]; double P_001020000=Pd_001[0]*Pd_020[1]; double P_001120000=Pd_001[0]*Pd_120[1]; double P_001220000=Pd_001[0]*Pd_220[1]; double P_101020000=Pd_101[0]*Pd_020[1]; double P_101120000=Pd_101[0]*Pd_120[1]; double P_101220000=Pd_101[0]*Pd_220[1]; double P_000021000=Pd_021[1]; double P_000121000=Pd_121[1]; double P_000221000=Pd_221[1]; double P_000321000=Pd_321[1]; double P_000020001=Pd_020[1]*Pd_001[2]; double P_000020101=Pd_020[1]*Pd_101[2]; double P_000120001=Pd_120[1]*Pd_001[2]; double P_000120101=Pd_120[1]*Pd_101[2]; double P_000220001=Pd_220[1]*Pd_001[2]; double P_000220101=Pd_220[1]*Pd_101[2]; double P_011000010=Pd_011[0]*Pd_010[2]; double P_011000110=Pd_011[0]*Pd_110[2]; double P_111000010=Pd_111[0]*Pd_010[2]; double P_111000110=Pd_111[0]*Pd_110[2]; double P_211000010=Pd_211[0]*Pd_010[2]; double P_211000110=Pd_211[0]*Pd_110[2]; double P_010001010=Pd_010[0]*Pd_001[1]*Pd_010[2]; double P_010001110=Pd_010[0]*Pd_001[1]*Pd_110[2]; double P_010101010=Pd_010[0]*Pd_101[1]*Pd_010[2]; double P_010101110=Pd_010[0]*Pd_101[1]*Pd_110[2]; double P_110001010=Pd_110[0]*Pd_001[1]*Pd_010[2]; double P_110001110=Pd_110[0]*Pd_001[1]*Pd_110[2]; double P_110101010=Pd_110[0]*Pd_101[1]*Pd_010[2]; double P_110101110=Pd_110[0]*Pd_101[1]*Pd_110[2]; double P_010000011=Pd_010[0]*Pd_011[2]; double P_010000111=Pd_010[0]*Pd_111[2]; double P_010000211=Pd_010[0]*Pd_211[2]; double P_110000011=Pd_110[0]*Pd_011[2]; double P_110000111=Pd_110[0]*Pd_111[2]; double P_110000211=Pd_110[0]*Pd_211[2]; double P_001010010=Pd_001[0]*Pd_010[1]*Pd_010[2]; double P_001010110=Pd_001[0]*Pd_010[1]*Pd_110[2]; double P_001110010=Pd_001[0]*Pd_110[1]*Pd_010[2]; double P_001110110=Pd_001[0]*Pd_110[1]*Pd_110[2]; double P_101010010=Pd_101[0]*Pd_010[1]*Pd_010[2]; double P_101010110=Pd_101[0]*Pd_010[1]*Pd_110[2]; double P_101110010=Pd_101[0]*Pd_110[1]*Pd_010[2]; double P_101110110=Pd_101[0]*Pd_110[1]*Pd_110[2]; double P_000011010=Pd_011[1]*Pd_010[2]; double P_000011110=Pd_011[1]*Pd_110[2]; double P_000111010=Pd_111[1]*Pd_010[2]; double P_000111110=Pd_111[1]*Pd_110[2]; double P_000211010=Pd_211[1]*Pd_010[2]; double P_000211110=Pd_211[1]*Pd_110[2]; double P_000010011=Pd_010[1]*Pd_011[2]; double P_000010111=Pd_010[1]*Pd_111[2]; double P_000010211=Pd_010[1]*Pd_211[2]; double P_000110011=Pd_110[1]*Pd_011[2]; double P_000110111=Pd_110[1]*Pd_111[2]; double P_000110211=Pd_110[1]*Pd_211[2]; double P_001000020=Pd_001[0]*Pd_020[2]; double P_001000120=Pd_001[0]*Pd_120[2]; double P_001000220=Pd_001[0]*Pd_220[2]; double P_101000020=Pd_101[0]*Pd_020[2]; double P_101000120=Pd_101[0]*Pd_120[2]; double P_101000220=Pd_101[0]*Pd_220[2]; double P_000001020=Pd_001[1]*Pd_020[2]; double P_000001120=Pd_001[1]*Pd_120[2]; double P_000001220=Pd_001[1]*Pd_220[2]; double P_000101020=Pd_101[1]*Pd_020[2]; double P_000101120=Pd_101[1]*Pd_120[2]; double P_000101220=Pd_101[1]*Pd_220[2]; double P_000000021=Pd_021[2]; double P_000000121=Pd_121[2]; double P_000000221=Pd_221[2]; double P_000000321=Pd_321[2]; double PR_021000000000=P_021000000*R_000[0]+-1*P_121000000*R_100[0]+P_221000000*R_200[0]+-1*P_321000000*R_300[0]; double PR_020001000000=P_020001000*R_000[0]+-1*P_020101000*R_010[0]+-1*P_120001000*R_100[0]+P_120101000*R_110[0]+P_220001000*R_200[0]+-1*P_220101000*R_210[0]; double PR_020000001000=P_020000001*R_000[0]+-1*P_020000101*R_001[0]+-1*P_120000001*R_100[0]+P_120000101*R_101[0]+P_220000001*R_200[0]+-1*P_220000101*R_201[0]; double PR_011010000000=P_011010000*R_000[0]+-1*P_011110000*R_010[0]+-1*P_111010000*R_100[0]+P_111110000*R_110[0]+P_211010000*R_200[0]+-1*P_211110000*R_210[0]; double PR_010011000000=P_010011000*R_000[0]+-1*P_010111000*R_010[0]+P_010211000*R_020[0]+-1*P_110011000*R_100[0]+P_110111000*R_110[0]+-1*P_110211000*R_120[0]; double PR_010010001000=P_010010001*R_000[0]+-1*P_010010101*R_001[0]+-1*P_010110001*R_010[0]+P_010110101*R_011[0]+-1*P_110010001*R_100[0]+P_110010101*R_101[0]+P_110110001*R_110[0]+-1*P_110110101*R_111[0]; double PR_001020000000=P_001020000*R_000[0]+-1*P_001120000*R_010[0]+P_001220000*R_020[0]+-1*P_101020000*R_100[0]+P_101120000*R_110[0]+-1*P_101220000*R_120[0]; double PR_000021000000=P_000021000*R_000[0]+-1*P_000121000*R_010[0]+P_000221000*R_020[0]+-1*P_000321000*R_030[0]; double PR_000020001000=P_000020001*R_000[0]+-1*P_000020101*R_001[0]+-1*P_000120001*R_010[0]+P_000120101*R_011[0]+P_000220001*R_020[0]+-1*P_000220101*R_021[0]; double PR_011000010000=P_011000010*R_000[0]+-1*P_011000110*R_001[0]+-1*P_111000010*R_100[0]+P_111000110*R_101[0]+P_211000010*R_200[0]+-1*P_211000110*R_201[0]; double PR_010001010000=P_010001010*R_000[0]+-1*P_010001110*R_001[0]+-1*P_010101010*R_010[0]+P_010101110*R_011[0]+-1*P_110001010*R_100[0]+P_110001110*R_101[0]+P_110101010*R_110[0]+-1*P_110101110*R_111[0]; double PR_010000011000=P_010000011*R_000[0]+-1*P_010000111*R_001[0]+P_010000211*R_002[0]+-1*P_110000011*R_100[0]+P_110000111*R_101[0]+-1*P_110000211*R_102[0]; double PR_001010010000=P_001010010*R_000[0]+-1*P_001010110*R_001[0]+-1*P_001110010*R_010[0]+P_001110110*R_011[0]+-1*P_101010010*R_100[0]+P_101010110*R_101[0]+P_101110010*R_110[0]+-1*P_101110110*R_111[0]; double PR_000011010000=P_000011010*R_000[0]+-1*P_000011110*R_001[0]+-1*P_000111010*R_010[0]+P_000111110*R_011[0]+P_000211010*R_020[0]+-1*P_000211110*R_021[0]; double PR_000010011000=P_000010011*R_000[0]+-1*P_000010111*R_001[0]+P_000010211*R_002[0]+-1*P_000110011*R_010[0]+P_000110111*R_011[0]+-1*P_000110211*R_012[0]; double PR_001000020000=P_001000020*R_000[0]+-1*P_001000120*R_001[0]+P_001000220*R_002[0]+-1*P_101000020*R_100[0]+P_101000120*R_101[0]+-1*P_101000220*R_102[0]; double PR_000001020000=P_000001020*R_000[0]+-1*P_000001120*R_001[0]+P_000001220*R_002[0]+-1*P_000101020*R_010[0]+P_000101120*R_011[0]+-1*P_000101220*R_012[0]; double PR_000000021000=P_000000021*R_000[0]+-1*P_000000121*R_001[0]+P_000000221*R_002[0]+-1*P_000000321*R_003[0]; double PR_021000000001=P_021000000*R_001[0]+-1*P_121000000*R_101[0]+P_221000000*R_201[0]+-1*P_321000000*R_301[0]; double PR_020001000001=P_020001000*R_001[0]+-1*P_020101000*R_011[0]+-1*P_120001000*R_101[0]+P_120101000*R_111[0]+P_220001000*R_201[0]+-1*P_220101000*R_211[0]; double PR_020000001001=P_020000001*R_001[0]+-1*P_020000101*R_002[0]+-1*P_120000001*R_101[0]+P_120000101*R_102[0]+P_220000001*R_201[0]+-1*P_220000101*R_202[0]; double PR_011010000001=P_011010000*R_001[0]+-1*P_011110000*R_011[0]+-1*P_111010000*R_101[0]+P_111110000*R_111[0]+P_211010000*R_201[0]+-1*P_211110000*R_211[0]; double PR_010011000001=P_010011000*R_001[0]+-1*P_010111000*R_011[0]+P_010211000*R_021[0]+-1*P_110011000*R_101[0]+P_110111000*R_111[0]+-1*P_110211000*R_121[0]; double PR_010010001001=P_010010001*R_001[0]+-1*P_010010101*R_002[0]+-1*P_010110001*R_011[0]+P_010110101*R_012[0]+-1*P_110010001*R_101[0]+P_110010101*R_102[0]+P_110110001*R_111[0]+-1*P_110110101*R_112[0]; double PR_001020000001=P_001020000*R_001[0]+-1*P_001120000*R_011[0]+P_001220000*R_021[0]+-1*P_101020000*R_101[0]+P_101120000*R_111[0]+-1*P_101220000*R_121[0]; double PR_000021000001=P_000021000*R_001[0]+-1*P_000121000*R_011[0]+P_000221000*R_021[0]+-1*P_000321000*R_031[0]; double PR_000020001001=P_000020001*R_001[0]+-1*P_000020101*R_002[0]+-1*P_000120001*R_011[0]+P_000120101*R_012[0]+P_000220001*R_021[0]+-1*P_000220101*R_022[0]; double PR_011000010001=P_011000010*R_001[0]+-1*P_011000110*R_002[0]+-1*P_111000010*R_101[0]+P_111000110*R_102[0]+P_211000010*R_201[0]+-1*P_211000110*R_202[0]; double PR_010001010001=P_010001010*R_001[0]+-1*P_010001110*R_002[0]+-1*P_010101010*R_011[0]+P_010101110*R_012[0]+-1*P_110001010*R_101[0]+P_110001110*R_102[0]+P_110101010*R_111[0]+-1*P_110101110*R_112[0]; double PR_010000011001=P_010000011*R_001[0]+-1*P_010000111*R_002[0]+P_010000211*R_003[0]+-1*P_110000011*R_101[0]+P_110000111*R_102[0]+-1*P_110000211*R_103[0]; double PR_001010010001=P_001010010*R_001[0]+-1*P_001010110*R_002[0]+-1*P_001110010*R_011[0]+P_001110110*R_012[0]+-1*P_101010010*R_101[0]+P_101010110*R_102[0]+P_101110010*R_111[0]+-1*P_101110110*R_112[0]; double PR_000011010001=P_000011010*R_001[0]+-1*P_000011110*R_002[0]+-1*P_000111010*R_011[0]+P_000111110*R_012[0]+P_000211010*R_021[0]+-1*P_000211110*R_022[0]; double PR_000010011001=P_000010011*R_001[0]+-1*P_000010111*R_002[0]+P_000010211*R_003[0]+-1*P_000110011*R_011[0]+P_000110111*R_012[0]+-1*P_000110211*R_013[0]; double PR_001000020001=P_001000020*R_001[0]+-1*P_001000120*R_002[0]+P_001000220*R_003[0]+-1*P_101000020*R_101[0]+P_101000120*R_102[0]+-1*P_101000220*R_103[0]; double PR_000001020001=P_000001020*R_001[0]+-1*P_000001120*R_002[0]+P_000001220*R_003[0]+-1*P_000101020*R_011[0]+P_000101120*R_012[0]+-1*P_000101220*R_013[0]; double PR_000000021001=P_000000021*R_001[0]+-1*P_000000121*R_002[0]+P_000000221*R_003[0]+-1*P_000000321*R_004[0]; double PR_021000000010=P_021000000*R_010[0]+-1*P_121000000*R_110[0]+P_221000000*R_210[0]+-1*P_321000000*R_310[0]; double PR_020001000010=P_020001000*R_010[0]+-1*P_020101000*R_020[0]+-1*P_120001000*R_110[0]+P_120101000*R_120[0]+P_220001000*R_210[0]+-1*P_220101000*R_220[0]; double PR_020000001010=P_020000001*R_010[0]+-1*P_020000101*R_011[0]+-1*P_120000001*R_110[0]+P_120000101*R_111[0]+P_220000001*R_210[0]+-1*P_220000101*R_211[0]; double PR_011010000010=P_011010000*R_010[0]+-1*P_011110000*R_020[0]+-1*P_111010000*R_110[0]+P_111110000*R_120[0]+P_211010000*R_210[0]+-1*P_211110000*R_220[0]; double PR_010011000010=P_010011000*R_010[0]+-1*P_010111000*R_020[0]+P_010211000*R_030[0]+-1*P_110011000*R_110[0]+P_110111000*R_120[0]+-1*P_110211000*R_130[0]; double PR_010010001010=P_010010001*R_010[0]+-1*P_010010101*R_011[0]+-1*P_010110001*R_020[0]+P_010110101*R_021[0]+-1*P_110010001*R_110[0]+P_110010101*R_111[0]+P_110110001*R_120[0]+-1*P_110110101*R_121[0]; double PR_001020000010=P_001020000*R_010[0]+-1*P_001120000*R_020[0]+P_001220000*R_030[0]+-1*P_101020000*R_110[0]+P_101120000*R_120[0]+-1*P_101220000*R_130[0]; double PR_000021000010=P_000021000*R_010[0]+-1*P_000121000*R_020[0]+P_000221000*R_030[0]+-1*P_000321000*R_040[0]; double PR_000020001010=P_000020001*R_010[0]+-1*P_000020101*R_011[0]+-1*P_000120001*R_020[0]+P_000120101*R_021[0]+P_000220001*R_030[0]+-1*P_000220101*R_031[0]; double PR_011000010010=P_011000010*R_010[0]+-1*P_011000110*R_011[0]+-1*P_111000010*R_110[0]+P_111000110*R_111[0]+P_211000010*R_210[0]+-1*P_211000110*R_211[0]; double PR_010001010010=P_010001010*R_010[0]+-1*P_010001110*R_011[0]+-1*P_010101010*R_020[0]+P_010101110*R_021[0]+-1*P_110001010*R_110[0]+P_110001110*R_111[0]+P_110101010*R_120[0]+-1*P_110101110*R_121[0]; double PR_010000011010=P_010000011*R_010[0]+-1*P_010000111*R_011[0]+P_010000211*R_012[0]+-1*P_110000011*R_110[0]+P_110000111*R_111[0]+-1*P_110000211*R_112[0]; double PR_001010010010=P_001010010*R_010[0]+-1*P_001010110*R_011[0]+-1*P_001110010*R_020[0]+P_001110110*R_021[0]+-1*P_101010010*R_110[0]+P_101010110*R_111[0]+P_101110010*R_120[0]+-1*P_101110110*R_121[0]; double PR_000011010010=P_000011010*R_010[0]+-1*P_000011110*R_011[0]+-1*P_000111010*R_020[0]+P_000111110*R_021[0]+P_000211010*R_030[0]+-1*P_000211110*R_031[0]; double PR_000010011010=P_000010011*R_010[0]+-1*P_000010111*R_011[0]+P_000010211*R_012[0]+-1*P_000110011*R_020[0]+P_000110111*R_021[0]+-1*P_000110211*R_022[0]; double PR_001000020010=P_001000020*R_010[0]+-1*P_001000120*R_011[0]+P_001000220*R_012[0]+-1*P_101000020*R_110[0]+P_101000120*R_111[0]+-1*P_101000220*R_112[0]; double PR_000001020010=P_000001020*R_010[0]+-1*P_000001120*R_011[0]+P_000001220*R_012[0]+-1*P_000101020*R_020[0]+P_000101120*R_021[0]+-1*P_000101220*R_022[0]; double PR_000000021010=P_000000021*R_010[0]+-1*P_000000121*R_011[0]+P_000000221*R_012[0]+-1*P_000000321*R_013[0]; double PR_021000000100=P_021000000*R_100[0]+-1*P_121000000*R_200[0]+P_221000000*R_300[0]+-1*P_321000000*R_400[0]; double PR_020001000100=P_020001000*R_100[0]+-1*P_020101000*R_110[0]+-1*P_120001000*R_200[0]+P_120101000*R_210[0]+P_220001000*R_300[0]+-1*P_220101000*R_310[0]; double PR_020000001100=P_020000001*R_100[0]+-1*P_020000101*R_101[0]+-1*P_120000001*R_200[0]+P_120000101*R_201[0]+P_220000001*R_300[0]+-1*P_220000101*R_301[0]; double PR_011010000100=P_011010000*R_100[0]+-1*P_011110000*R_110[0]+-1*P_111010000*R_200[0]+P_111110000*R_210[0]+P_211010000*R_300[0]+-1*P_211110000*R_310[0]; double PR_010011000100=P_010011000*R_100[0]+-1*P_010111000*R_110[0]+P_010211000*R_120[0]+-1*P_110011000*R_200[0]+P_110111000*R_210[0]+-1*P_110211000*R_220[0]; double PR_010010001100=P_010010001*R_100[0]+-1*P_010010101*R_101[0]+-1*P_010110001*R_110[0]+P_010110101*R_111[0]+-1*P_110010001*R_200[0]+P_110010101*R_201[0]+P_110110001*R_210[0]+-1*P_110110101*R_211[0]; double PR_001020000100=P_001020000*R_100[0]+-1*P_001120000*R_110[0]+P_001220000*R_120[0]+-1*P_101020000*R_200[0]+P_101120000*R_210[0]+-1*P_101220000*R_220[0]; double PR_000021000100=P_000021000*R_100[0]+-1*P_000121000*R_110[0]+P_000221000*R_120[0]+-1*P_000321000*R_130[0]; double PR_000020001100=P_000020001*R_100[0]+-1*P_000020101*R_101[0]+-1*P_000120001*R_110[0]+P_000120101*R_111[0]+P_000220001*R_120[0]+-1*P_000220101*R_121[0]; double PR_011000010100=P_011000010*R_100[0]+-1*P_011000110*R_101[0]+-1*P_111000010*R_200[0]+P_111000110*R_201[0]+P_211000010*R_300[0]+-1*P_211000110*R_301[0]; double PR_010001010100=P_010001010*R_100[0]+-1*P_010001110*R_101[0]+-1*P_010101010*R_110[0]+P_010101110*R_111[0]+-1*P_110001010*R_200[0]+P_110001110*R_201[0]+P_110101010*R_210[0]+-1*P_110101110*R_211[0]; double PR_010000011100=P_010000011*R_100[0]+-1*P_010000111*R_101[0]+P_010000211*R_102[0]+-1*P_110000011*R_200[0]+P_110000111*R_201[0]+-1*P_110000211*R_202[0]; double PR_001010010100=P_001010010*R_100[0]+-1*P_001010110*R_101[0]+-1*P_001110010*R_110[0]+P_001110110*R_111[0]+-1*P_101010010*R_200[0]+P_101010110*R_201[0]+P_101110010*R_210[0]+-1*P_101110110*R_211[0]; double PR_000011010100=P_000011010*R_100[0]+-1*P_000011110*R_101[0]+-1*P_000111010*R_110[0]+P_000111110*R_111[0]+P_000211010*R_120[0]+-1*P_000211110*R_121[0]; double PR_000010011100=P_000010011*R_100[0]+-1*P_000010111*R_101[0]+P_000010211*R_102[0]+-1*P_000110011*R_110[0]+P_000110111*R_111[0]+-1*P_000110211*R_112[0]; double PR_001000020100=P_001000020*R_100[0]+-1*P_001000120*R_101[0]+P_001000220*R_102[0]+-1*P_101000020*R_200[0]+P_101000120*R_201[0]+-1*P_101000220*R_202[0]; double PR_000001020100=P_000001020*R_100[0]+-1*P_000001120*R_101[0]+P_000001220*R_102[0]+-1*P_000101020*R_110[0]+P_000101120*R_111[0]+-1*P_000101220*R_112[0]; double PR_000000021100=P_000000021*R_100[0]+-1*P_000000121*R_101[0]+P_000000221*R_102[0]+-1*P_000000321*R_103[0]; double PR_021000000002=P_021000000*R_002[0]+-1*P_121000000*R_102[0]+P_221000000*R_202[0]+-1*P_321000000*R_302[0]; double PR_020001000002=P_020001000*R_002[0]+-1*P_020101000*R_012[0]+-1*P_120001000*R_102[0]+P_120101000*R_112[0]+P_220001000*R_202[0]+-1*P_220101000*R_212[0]; double PR_020000001002=P_020000001*R_002[0]+-1*P_020000101*R_003[0]+-1*P_120000001*R_102[0]+P_120000101*R_103[0]+P_220000001*R_202[0]+-1*P_220000101*R_203[0]; double PR_011010000002=P_011010000*R_002[0]+-1*P_011110000*R_012[0]+-1*P_111010000*R_102[0]+P_111110000*R_112[0]+P_211010000*R_202[0]+-1*P_211110000*R_212[0]; double PR_010011000002=P_010011000*R_002[0]+-1*P_010111000*R_012[0]+P_010211000*R_022[0]+-1*P_110011000*R_102[0]+P_110111000*R_112[0]+-1*P_110211000*R_122[0]; double PR_010010001002=P_010010001*R_002[0]+-1*P_010010101*R_003[0]+-1*P_010110001*R_012[0]+P_010110101*R_013[0]+-1*P_110010001*R_102[0]+P_110010101*R_103[0]+P_110110001*R_112[0]+-1*P_110110101*R_113[0]; double PR_001020000002=P_001020000*R_002[0]+-1*P_001120000*R_012[0]+P_001220000*R_022[0]+-1*P_101020000*R_102[0]+P_101120000*R_112[0]+-1*P_101220000*R_122[0]; double PR_000021000002=P_000021000*R_002[0]+-1*P_000121000*R_012[0]+P_000221000*R_022[0]+-1*P_000321000*R_032[0]; double PR_000020001002=P_000020001*R_002[0]+-1*P_000020101*R_003[0]+-1*P_000120001*R_012[0]+P_000120101*R_013[0]+P_000220001*R_022[0]+-1*P_000220101*R_023[0]; double PR_011000010002=P_011000010*R_002[0]+-1*P_011000110*R_003[0]+-1*P_111000010*R_102[0]+P_111000110*R_103[0]+P_211000010*R_202[0]+-1*P_211000110*R_203[0]; double PR_010001010002=P_010001010*R_002[0]+-1*P_010001110*R_003[0]+-1*P_010101010*R_012[0]+P_010101110*R_013[0]+-1*P_110001010*R_102[0]+P_110001110*R_103[0]+P_110101010*R_112[0]+-1*P_110101110*R_113[0]; double PR_010000011002=P_010000011*R_002[0]+-1*P_010000111*R_003[0]+P_010000211*R_004[0]+-1*P_110000011*R_102[0]+P_110000111*R_103[0]+-1*P_110000211*R_104[0]; double PR_001010010002=P_001010010*R_002[0]+-1*P_001010110*R_003[0]+-1*P_001110010*R_012[0]+P_001110110*R_013[0]+-1*P_101010010*R_102[0]+P_101010110*R_103[0]+P_101110010*R_112[0]+-1*P_101110110*R_113[0]; double PR_000011010002=P_000011010*R_002[0]+-1*P_000011110*R_003[0]+-1*P_000111010*R_012[0]+P_000111110*R_013[0]+P_000211010*R_022[0]+-1*P_000211110*R_023[0]; double PR_000010011002=P_000010011*R_002[0]+-1*P_000010111*R_003[0]+P_000010211*R_004[0]+-1*P_000110011*R_012[0]+P_000110111*R_013[0]+-1*P_000110211*R_014[0]; double PR_001000020002=P_001000020*R_002[0]+-1*P_001000120*R_003[0]+P_001000220*R_004[0]+-1*P_101000020*R_102[0]+P_101000120*R_103[0]+-1*P_101000220*R_104[0]; double PR_000001020002=P_000001020*R_002[0]+-1*P_000001120*R_003[0]+P_000001220*R_004[0]+-1*P_000101020*R_012[0]+P_000101120*R_013[0]+-1*P_000101220*R_014[0]; double PR_000000021002=P_000000021*R_002[0]+-1*P_000000121*R_003[0]+P_000000221*R_004[0]+-1*P_000000321*R_005[0]; double PR_021000000011=P_021000000*R_011[0]+-1*P_121000000*R_111[0]+P_221000000*R_211[0]+-1*P_321000000*R_311[0]; double PR_020001000011=P_020001000*R_011[0]+-1*P_020101000*R_021[0]+-1*P_120001000*R_111[0]+P_120101000*R_121[0]+P_220001000*R_211[0]+-1*P_220101000*R_221[0]; double PR_020000001011=P_020000001*R_011[0]+-1*P_020000101*R_012[0]+-1*P_120000001*R_111[0]+P_120000101*R_112[0]+P_220000001*R_211[0]+-1*P_220000101*R_212[0]; double PR_011010000011=P_011010000*R_011[0]+-1*P_011110000*R_021[0]+-1*P_111010000*R_111[0]+P_111110000*R_121[0]+P_211010000*R_211[0]+-1*P_211110000*R_221[0]; double PR_010011000011=P_010011000*R_011[0]+-1*P_010111000*R_021[0]+P_010211000*R_031[0]+-1*P_110011000*R_111[0]+P_110111000*R_121[0]+-1*P_110211000*R_131[0]; double PR_010010001011=P_010010001*R_011[0]+-1*P_010010101*R_012[0]+-1*P_010110001*R_021[0]+P_010110101*R_022[0]+-1*P_110010001*R_111[0]+P_110010101*R_112[0]+P_110110001*R_121[0]+-1*P_110110101*R_122[0]; double PR_001020000011=P_001020000*R_011[0]+-1*P_001120000*R_021[0]+P_001220000*R_031[0]+-1*P_101020000*R_111[0]+P_101120000*R_121[0]+-1*P_101220000*R_131[0]; double PR_000021000011=P_000021000*R_011[0]+-1*P_000121000*R_021[0]+P_000221000*R_031[0]+-1*P_000321000*R_041[0]; double PR_000020001011=P_000020001*R_011[0]+-1*P_000020101*R_012[0]+-1*P_000120001*R_021[0]+P_000120101*R_022[0]+P_000220001*R_031[0]+-1*P_000220101*R_032[0]; double PR_011000010011=P_011000010*R_011[0]+-1*P_011000110*R_012[0]+-1*P_111000010*R_111[0]+P_111000110*R_112[0]+P_211000010*R_211[0]+-1*P_211000110*R_212[0]; double PR_010001010011=P_010001010*R_011[0]+-1*P_010001110*R_012[0]+-1*P_010101010*R_021[0]+P_010101110*R_022[0]+-1*P_110001010*R_111[0]+P_110001110*R_112[0]+P_110101010*R_121[0]+-1*P_110101110*R_122[0]; double PR_010000011011=P_010000011*R_011[0]+-1*P_010000111*R_012[0]+P_010000211*R_013[0]+-1*P_110000011*R_111[0]+P_110000111*R_112[0]+-1*P_110000211*R_113[0]; double PR_001010010011=P_001010010*R_011[0]+-1*P_001010110*R_012[0]+-1*P_001110010*R_021[0]+P_001110110*R_022[0]+-1*P_101010010*R_111[0]+P_101010110*R_112[0]+P_101110010*R_121[0]+-1*P_101110110*R_122[0]; double PR_000011010011=P_000011010*R_011[0]+-1*P_000011110*R_012[0]+-1*P_000111010*R_021[0]+P_000111110*R_022[0]+P_000211010*R_031[0]+-1*P_000211110*R_032[0]; double PR_000010011011=P_000010011*R_011[0]+-1*P_000010111*R_012[0]+P_000010211*R_013[0]+-1*P_000110011*R_021[0]+P_000110111*R_022[0]+-1*P_000110211*R_023[0]; double PR_001000020011=P_001000020*R_011[0]+-1*P_001000120*R_012[0]+P_001000220*R_013[0]+-1*P_101000020*R_111[0]+P_101000120*R_112[0]+-1*P_101000220*R_113[0]; double PR_000001020011=P_000001020*R_011[0]+-1*P_000001120*R_012[0]+P_000001220*R_013[0]+-1*P_000101020*R_021[0]+P_000101120*R_022[0]+-1*P_000101220*R_023[0]; double PR_000000021011=P_000000021*R_011[0]+-1*P_000000121*R_012[0]+P_000000221*R_013[0]+-1*P_000000321*R_014[0]; double PR_021000000020=P_021000000*R_020[0]+-1*P_121000000*R_120[0]+P_221000000*R_220[0]+-1*P_321000000*R_320[0]; double PR_020001000020=P_020001000*R_020[0]+-1*P_020101000*R_030[0]+-1*P_120001000*R_120[0]+P_120101000*R_130[0]+P_220001000*R_220[0]+-1*P_220101000*R_230[0]; double PR_020000001020=P_020000001*R_020[0]+-1*P_020000101*R_021[0]+-1*P_120000001*R_120[0]+P_120000101*R_121[0]+P_220000001*R_220[0]+-1*P_220000101*R_221[0]; double PR_011010000020=P_011010000*R_020[0]+-1*P_011110000*R_030[0]+-1*P_111010000*R_120[0]+P_111110000*R_130[0]+P_211010000*R_220[0]+-1*P_211110000*R_230[0]; double PR_010011000020=P_010011000*R_020[0]+-1*P_010111000*R_030[0]+P_010211000*R_040[0]+-1*P_110011000*R_120[0]+P_110111000*R_130[0]+-1*P_110211000*R_140[0]; double PR_010010001020=P_010010001*R_020[0]+-1*P_010010101*R_021[0]+-1*P_010110001*R_030[0]+P_010110101*R_031[0]+-1*P_110010001*R_120[0]+P_110010101*R_121[0]+P_110110001*R_130[0]+-1*P_110110101*R_131[0]; double PR_001020000020=P_001020000*R_020[0]+-1*P_001120000*R_030[0]+P_001220000*R_040[0]+-1*P_101020000*R_120[0]+P_101120000*R_130[0]+-1*P_101220000*R_140[0]; double PR_000021000020=P_000021000*R_020[0]+-1*P_000121000*R_030[0]+P_000221000*R_040[0]+-1*P_000321000*R_050[0]; double PR_000020001020=P_000020001*R_020[0]+-1*P_000020101*R_021[0]+-1*P_000120001*R_030[0]+P_000120101*R_031[0]+P_000220001*R_040[0]+-1*P_000220101*R_041[0]; double PR_011000010020=P_011000010*R_020[0]+-1*P_011000110*R_021[0]+-1*P_111000010*R_120[0]+P_111000110*R_121[0]+P_211000010*R_220[0]+-1*P_211000110*R_221[0]; double PR_010001010020=P_010001010*R_020[0]+-1*P_010001110*R_021[0]+-1*P_010101010*R_030[0]+P_010101110*R_031[0]+-1*P_110001010*R_120[0]+P_110001110*R_121[0]+P_110101010*R_130[0]+-1*P_110101110*R_131[0]; double PR_010000011020=P_010000011*R_020[0]+-1*P_010000111*R_021[0]+P_010000211*R_022[0]+-1*P_110000011*R_120[0]+P_110000111*R_121[0]+-1*P_110000211*R_122[0]; double PR_001010010020=P_001010010*R_020[0]+-1*P_001010110*R_021[0]+-1*P_001110010*R_030[0]+P_001110110*R_031[0]+-1*P_101010010*R_120[0]+P_101010110*R_121[0]+P_101110010*R_130[0]+-1*P_101110110*R_131[0]; double PR_000011010020=P_000011010*R_020[0]+-1*P_000011110*R_021[0]+-1*P_000111010*R_030[0]+P_000111110*R_031[0]+P_000211010*R_040[0]+-1*P_000211110*R_041[0]; double PR_000010011020=P_000010011*R_020[0]+-1*P_000010111*R_021[0]+P_000010211*R_022[0]+-1*P_000110011*R_030[0]+P_000110111*R_031[0]+-1*P_000110211*R_032[0]; double PR_001000020020=P_001000020*R_020[0]+-1*P_001000120*R_021[0]+P_001000220*R_022[0]+-1*P_101000020*R_120[0]+P_101000120*R_121[0]+-1*P_101000220*R_122[0]; double PR_000001020020=P_000001020*R_020[0]+-1*P_000001120*R_021[0]+P_000001220*R_022[0]+-1*P_000101020*R_030[0]+P_000101120*R_031[0]+-1*P_000101220*R_032[0]; double PR_000000021020=P_000000021*R_020[0]+-1*P_000000121*R_021[0]+P_000000221*R_022[0]+-1*P_000000321*R_023[0]; double PR_021000000101=P_021000000*R_101[0]+-1*P_121000000*R_201[0]+P_221000000*R_301[0]+-1*P_321000000*R_401[0]; double PR_020001000101=P_020001000*R_101[0]+-1*P_020101000*R_111[0]+-1*P_120001000*R_201[0]+P_120101000*R_211[0]+P_220001000*R_301[0]+-1*P_220101000*R_311[0]; double PR_020000001101=P_020000001*R_101[0]+-1*P_020000101*R_102[0]+-1*P_120000001*R_201[0]+P_120000101*R_202[0]+P_220000001*R_301[0]+-1*P_220000101*R_302[0]; double PR_011010000101=P_011010000*R_101[0]+-1*P_011110000*R_111[0]+-1*P_111010000*R_201[0]+P_111110000*R_211[0]+P_211010000*R_301[0]+-1*P_211110000*R_311[0]; double PR_010011000101=P_010011000*R_101[0]+-1*P_010111000*R_111[0]+P_010211000*R_121[0]+-1*P_110011000*R_201[0]+P_110111000*R_211[0]+-1*P_110211000*R_221[0]; double PR_010010001101=P_010010001*R_101[0]+-1*P_010010101*R_102[0]+-1*P_010110001*R_111[0]+P_010110101*R_112[0]+-1*P_110010001*R_201[0]+P_110010101*R_202[0]+P_110110001*R_211[0]+-1*P_110110101*R_212[0]; double PR_001020000101=P_001020000*R_101[0]+-1*P_001120000*R_111[0]+P_001220000*R_121[0]+-1*P_101020000*R_201[0]+P_101120000*R_211[0]+-1*P_101220000*R_221[0]; double PR_000021000101=P_000021000*R_101[0]+-1*P_000121000*R_111[0]+P_000221000*R_121[0]+-1*P_000321000*R_131[0]; double PR_000020001101=P_000020001*R_101[0]+-1*P_000020101*R_102[0]+-1*P_000120001*R_111[0]+P_000120101*R_112[0]+P_000220001*R_121[0]+-1*P_000220101*R_122[0]; double PR_011000010101=P_011000010*R_101[0]+-1*P_011000110*R_102[0]+-1*P_111000010*R_201[0]+P_111000110*R_202[0]+P_211000010*R_301[0]+-1*P_211000110*R_302[0]; double PR_010001010101=P_010001010*R_101[0]+-1*P_010001110*R_102[0]+-1*P_010101010*R_111[0]+P_010101110*R_112[0]+-1*P_110001010*R_201[0]+P_110001110*R_202[0]+P_110101010*R_211[0]+-1*P_110101110*R_212[0]; double PR_010000011101=P_010000011*R_101[0]+-1*P_010000111*R_102[0]+P_010000211*R_103[0]+-1*P_110000011*R_201[0]+P_110000111*R_202[0]+-1*P_110000211*R_203[0]; double PR_001010010101=P_001010010*R_101[0]+-1*P_001010110*R_102[0]+-1*P_001110010*R_111[0]+P_001110110*R_112[0]+-1*P_101010010*R_201[0]+P_101010110*R_202[0]+P_101110010*R_211[0]+-1*P_101110110*R_212[0]; double PR_000011010101=P_000011010*R_101[0]+-1*P_000011110*R_102[0]+-1*P_000111010*R_111[0]+P_000111110*R_112[0]+P_000211010*R_121[0]+-1*P_000211110*R_122[0]; double PR_000010011101=P_000010011*R_101[0]+-1*P_000010111*R_102[0]+P_000010211*R_103[0]+-1*P_000110011*R_111[0]+P_000110111*R_112[0]+-1*P_000110211*R_113[0]; double PR_001000020101=P_001000020*R_101[0]+-1*P_001000120*R_102[0]+P_001000220*R_103[0]+-1*P_101000020*R_201[0]+P_101000120*R_202[0]+-1*P_101000220*R_203[0]; double PR_000001020101=P_000001020*R_101[0]+-1*P_000001120*R_102[0]+P_000001220*R_103[0]+-1*P_000101020*R_111[0]+P_000101120*R_112[0]+-1*P_000101220*R_113[0]; double PR_000000021101=P_000000021*R_101[0]+-1*P_000000121*R_102[0]+P_000000221*R_103[0]+-1*P_000000321*R_104[0]; double PR_021000000110=P_021000000*R_110[0]+-1*P_121000000*R_210[0]+P_221000000*R_310[0]+-1*P_321000000*R_410[0]; double PR_020001000110=P_020001000*R_110[0]+-1*P_020101000*R_120[0]+-1*P_120001000*R_210[0]+P_120101000*R_220[0]+P_220001000*R_310[0]+-1*P_220101000*R_320[0]; double PR_020000001110=P_020000001*R_110[0]+-1*P_020000101*R_111[0]+-1*P_120000001*R_210[0]+P_120000101*R_211[0]+P_220000001*R_310[0]+-1*P_220000101*R_311[0]; double PR_011010000110=P_011010000*R_110[0]+-1*P_011110000*R_120[0]+-1*P_111010000*R_210[0]+P_111110000*R_220[0]+P_211010000*R_310[0]+-1*P_211110000*R_320[0]; double PR_010011000110=P_010011000*R_110[0]+-1*P_010111000*R_120[0]+P_010211000*R_130[0]+-1*P_110011000*R_210[0]+P_110111000*R_220[0]+-1*P_110211000*R_230[0]; double PR_010010001110=P_010010001*R_110[0]+-1*P_010010101*R_111[0]+-1*P_010110001*R_120[0]+P_010110101*R_121[0]+-1*P_110010001*R_210[0]+P_110010101*R_211[0]+P_110110001*R_220[0]+-1*P_110110101*R_221[0]; double PR_001020000110=P_001020000*R_110[0]+-1*P_001120000*R_120[0]+P_001220000*R_130[0]+-1*P_101020000*R_210[0]+P_101120000*R_220[0]+-1*P_101220000*R_230[0]; double PR_000021000110=P_000021000*R_110[0]+-1*P_000121000*R_120[0]+P_000221000*R_130[0]+-1*P_000321000*R_140[0]; double PR_000020001110=P_000020001*R_110[0]+-1*P_000020101*R_111[0]+-1*P_000120001*R_120[0]+P_000120101*R_121[0]+P_000220001*R_130[0]+-1*P_000220101*R_131[0]; double PR_011000010110=P_011000010*R_110[0]+-1*P_011000110*R_111[0]+-1*P_111000010*R_210[0]+P_111000110*R_211[0]+P_211000010*R_310[0]+-1*P_211000110*R_311[0]; double PR_010001010110=P_010001010*R_110[0]+-1*P_010001110*R_111[0]+-1*P_010101010*R_120[0]+P_010101110*R_121[0]+-1*P_110001010*R_210[0]+P_110001110*R_211[0]+P_110101010*R_220[0]+-1*P_110101110*R_221[0]; double PR_010000011110=P_010000011*R_110[0]+-1*P_010000111*R_111[0]+P_010000211*R_112[0]+-1*P_110000011*R_210[0]+P_110000111*R_211[0]+-1*P_110000211*R_212[0]; double PR_001010010110=P_001010010*R_110[0]+-1*P_001010110*R_111[0]+-1*P_001110010*R_120[0]+P_001110110*R_121[0]+-1*P_101010010*R_210[0]+P_101010110*R_211[0]+P_101110010*R_220[0]+-1*P_101110110*R_221[0]; double PR_000011010110=P_000011010*R_110[0]+-1*P_000011110*R_111[0]+-1*P_000111010*R_120[0]+P_000111110*R_121[0]+P_000211010*R_130[0]+-1*P_000211110*R_131[0]; double PR_000010011110=P_000010011*R_110[0]+-1*P_000010111*R_111[0]+P_000010211*R_112[0]+-1*P_000110011*R_120[0]+P_000110111*R_121[0]+-1*P_000110211*R_122[0]; double PR_001000020110=P_001000020*R_110[0]+-1*P_001000120*R_111[0]+P_001000220*R_112[0]+-1*P_101000020*R_210[0]+P_101000120*R_211[0]+-1*P_101000220*R_212[0]; double PR_000001020110=P_000001020*R_110[0]+-1*P_000001120*R_111[0]+P_000001220*R_112[0]+-1*P_000101020*R_120[0]+P_000101120*R_121[0]+-1*P_000101220*R_122[0]; double PR_000000021110=P_000000021*R_110[0]+-1*P_000000121*R_111[0]+P_000000221*R_112[0]+-1*P_000000321*R_113[0]; double PR_021000000200=P_021000000*R_200[0]+-1*P_121000000*R_300[0]+P_221000000*R_400[0]+-1*P_321000000*R_500[0]; double PR_020001000200=P_020001000*R_200[0]+-1*P_020101000*R_210[0]+-1*P_120001000*R_300[0]+P_120101000*R_310[0]+P_220001000*R_400[0]+-1*P_220101000*R_410[0]; double PR_020000001200=P_020000001*R_200[0]+-1*P_020000101*R_201[0]+-1*P_120000001*R_300[0]+P_120000101*R_301[0]+P_220000001*R_400[0]+-1*P_220000101*R_401[0]; double PR_011010000200=P_011010000*R_200[0]+-1*P_011110000*R_210[0]+-1*P_111010000*R_300[0]+P_111110000*R_310[0]+P_211010000*R_400[0]+-1*P_211110000*R_410[0]; double PR_010011000200=P_010011000*R_200[0]+-1*P_010111000*R_210[0]+P_010211000*R_220[0]+-1*P_110011000*R_300[0]+P_110111000*R_310[0]+-1*P_110211000*R_320[0]; double PR_010010001200=P_010010001*R_200[0]+-1*P_010010101*R_201[0]+-1*P_010110001*R_210[0]+P_010110101*R_211[0]+-1*P_110010001*R_300[0]+P_110010101*R_301[0]+P_110110001*R_310[0]+-1*P_110110101*R_311[0]; double PR_001020000200=P_001020000*R_200[0]+-1*P_001120000*R_210[0]+P_001220000*R_220[0]+-1*P_101020000*R_300[0]+P_101120000*R_310[0]+-1*P_101220000*R_320[0]; double PR_000021000200=P_000021000*R_200[0]+-1*P_000121000*R_210[0]+P_000221000*R_220[0]+-1*P_000321000*R_230[0]; double PR_000020001200=P_000020001*R_200[0]+-1*P_000020101*R_201[0]+-1*P_000120001*R_210[0]+P_000120101*R_211[0]+P_000220001*R_220[0]+-1*P_000220101*R_221[0]; double PR_011000010200=P_011000010*R_200[0]+-1*P_011000110*R_201[0]+-1*P_111000010*R_300[0]+P_111000110*R_301[0]+P_211000010*R_400[0]+-1*P_211000110*R_401[0]; double PR_010001010200=P_010001010*R_200[0]+-1*P_010001110*R_201[0]+-1*P_010101010*R_210[0]+P_010101110*R_211[0]+-1*P_110001010*R_300[0]+P_110001110*R_301[0]+P_110101010*R_310[0]+-1*P_110101110*R_311[0]; double PR_010000011200=P_010000011*R_200[0]+-1*P_010000111*R_201[0]+P_010000211*R_202[0]+-1*P_110000011*R_300[0]+P_110000111*R_301[0]+-1*P_110000211*R_302[0]; double PR_001010010200=P_001010010*R_200[0]+-1*P_001010110*R_201[0]+-1*P_001110010*R_210[0]+P_001110110*R_211[0]+-1*P_101010010*R_300[0]+P_101010110*R_301[0]+P_101110010*R_310[0]+-1*P_101110110*R_311[0]; double PR_000011010200=P_000011010*R_200[0]+-1*P_000011110*R_201[0]+-1*P_000111010*R_210[0]+P_000111110*R_211[0]+P_000211010*R_220[0]+-1*P_000211110*R_221[0]; double PR_000010011200=P_000010011*R_200[0]+-1*P_000010111*R_201[0]+P_000010211*R_202[0]+-1*P_000110011*R_210[0]+P_000110111*R_211[0]+-1*P_000110211*R_212[0]; double PR_001000020200=P_001000020*R_200[0]+-1*P_001000120*R_201[0]+P_001000220*R_202[0]+-1*P_101000020*R_300[0]+P_101000120*R_301[0]+-1*P_101000220*R_302[0]; double PR_000001020200=P_000001020*R_200[0]+-1*P_000001120*R_201[0]+P_000001220*R_202[0]+-1*P_000101020*R_210[0]+P_000101120*R_211[0]+-1*P_000101220*R_212[0]; double PR_000000021200=P_000000021*R_200[0]+-1*P_000000121*R_201[0]+P_000000221*R_202[0]+-1*P_000000321*R_203[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(Q_002000000*PR_021000000000+Q_102000000*PR_021000000100+Q_202000000*PR_021000000200); ans_temp[ans_id*6+0]+=Pmtrx[1]*(Q_001001000*PR_021000000000+Q_001101000*PR_021000000010+Q_101001000*PR_021000000100+Q_101101000*PR_021000000110); ans_temp[ans_id*6+0]+=Pmtrx[2]*(Q_000002000*PR_021000000000+Q_000102000*PR_021000000010+Q_000202000*PR_021000000020); ans_temp[ans_id*6+0]+=Pmtrx[3]*(Q_001000001*PR_021000000000+Q_001000101*PR_021000000001+Q_101000001*PR_021000000100+Q_101000101*PR_021000000101); ans_temp[ans_id*6+0]+=Pmtrx[4]*(Q_000001001*PR_021000000000+Q_000001101*PR_021000000001+Q_000101001*PR_021000000010+Q_000101101*PR_021000000011); ans_temp[ans_id*6+0]+=Pmtrx[5]*(Q_000000002*PR_021000000000+Q_000000102*PR_021000000001+Q_000000202*PR_021000000002); ans_temp[ans_id*6+0]+=Pmtrx[6]*(Q_002000000*PR_020001000000+Q_102000000*PR_020001000100+Q_202000000*PR_020001000200); ans_temp[ans_id*6+0]+=Pmtrx[7]*(Q_001001000*PR_020001000000+Q_001101000*PR_020001000010+Q_101001000*PR_020001000100+Q_101101000*PR_020001000110); ans_temp[ans_id*6+0]+=Pmtrx[8]*(Q_000002000*PR_020001000000+Q_000102000*PR_020001000010+Q_000202000*PR_020001000020); ans_temp[ans_id*6+0]+=Pmtrx[9]*(Q_001000001*PR_020001000000+Q_001000101*PR_020001000001+Q_101000001*PR_020001000100+Q_101000101*PR_020001000101); ans_temp[ans_id*6+0]+=Pmtrx[10]*(Q_000001001*PR_020001000000+Q_000001101*PR_020001000001+Q_000101001*PR_020001000010+Q_000101101*PR_020001000011); ans_temp[ans_id*6+0]+=Pmtrx[11]*(Q_000000002*PR_020001000000+Q_000000102*PR_020001000001+Q_000000202*PR_020001000002); ans_temp[ans_id*6+0]+=Pmtrx[12]*(Q_002000000*PR_020000001000+Q_102000000*PR_020000001100+Q_202000000*PR_020000001200); ans_temp[ans_id*6+0]+=Pmtrx[13]*(Q_001001000*PR_020000001000+Q_001101000*PR_020000001010+Q_101001000*PR_020000001100+Q_101101000*PR_020000001110); ans_temp[ans_id*6+0]+=Pmtrx[14]*(Q_000002000*PR_020000001000+Q_000102000*PR_020000001010+Q_000202000*PR_020000001020); ans_temp[ans_id*6+0]+=Pmtrx[15]*(Q_001000001*PR_020000001000+Q_001000101*PR_020000001001+Q_101000001*PR_020000001100+Q_101000101*PR_020000001101); ans_temp[ans_id*6+0]+=Pmtrx[16]*(Q_000001001*PR_020000001000+Q_000001101*PR_020000001001+Q_000101001*PR_020000001010+Q_000101101*PR_020000001011); ans_temp[ans_id*6+0]+=Pmtrx[17]*(Q_000000002*PR_020000001000+Q_000000102*PR_020000001001+Q_000000202*PR_020000001002); ans_temp[ans_id*6+1]+=Pmtrx[0]*(Q_002000000*PR_011010000000+Q_102000000*PR_011010000100+Q_202000000*PR_011010000200); ans_temp[ans_id*6+1]+=Pmtrx[1]*(Q_001001000*PR_011010000000+Q_001101000*PR_011010000010+Q_101001000*PR_011010000100+Q_101101000*PR_011010000110); ans_temp[ans_id*6+1]+=Pmtrx[2]*(Q_000002000*PR_011010000000+Q_000102000*PR_011010000010+Q_000202000*PR_011010000020); ans_temp[ans_id*6+1]+=Pmtrx[3]*(Q_001000001*PR_011010000000+Q_001000101*PR_011010000001+Q_101000001*PR_011010000100+Q_101000101*PR_011010000101); ans_temp[ans_id*6+1]+=Pmtrx[4]*(Q_000001001*PR_011010000000+Q_000001101*PR_011010000001+Q_000101001*PR_011010000010+Q_000101101*PR_011010000011); ans_temp[ans_id*6+1]+=Pmtrx[5]*(Q_000000002*PR_011010000000+Q_000000102*PR_011010000001+Q_000000202*PR_011010000002); ans_temp[ans_id*6+1]+=Pmtrx[6]*(Q_002000000*PR_010011000000+Q_102000000*PR_010011000100+Q_202000000*PR_010011000200); ans_temp[ans_id*6+1]+=Pmtrx[7]*(Q_001001000*PR_010011000000+Q_001101000*PR_010011000010+Q_101001000*PR_010011000100+Q_101101000*PR_010011000110); ans_temp[ans_id*6+1]+=Pmtrx[8]*(Q_000002000*PR_010011000000+Q_000102000*PR_010011000010+Q_000202000*PR_010011000020); ans_temp[ans_id*6+1]+=Pmtrx[9]*(Q_001000001*PR_010011000000+Q_001000101*PR_010011000001+Q_101000001*PR_010011000100+Q_101000101*PR_010011000101); ans_temp[ans_id*6+1]+=Pmtrx[10]*(Q_000001001*PR_010011000000+Q_000001101*PR_010011000001+Q_000101001*PR_010011000010+Q_000101101*PR_010011000011); ans_temp[ans_id*6+1]+=Pmtrx[11]*(Q_000000002*PR_010011000000+Q_000000102*PR_010011000001+Q_000000202*PR_010011000002); ans_temp[ans_id*6+1]+=Pmtrx[12]*(Q_002000000*PR_010010001000+Q_102000000*PR_010010001100+Q_202000000*PR_010010001200); ans_temp[ans_id*6+1]+=Pmtrx[13]*(Q_001001000*PR_010010001000+Q_001101000*PR_010010001010+Q_101001000*PR_010010001100+Q_101101000*PR_010010001110); ans_temp[ans_id*6+1]+=Pmtrx[14]*(Q_000002000*PR_010010001000+Q_000102000*PR_010010001010+Q_000202000*PR_010010001020); ans_temp[ans_id*6+1]+=Pmtrx[15]*(Q_001000001*PR_010010001000+Q_001000101*PR_010010001001+Q_101000001*PR_010010001100+Q_101000101*PR_010010001101); ans_temp[ans_id*6+1]+=Pmtrx[16]*(Q_000001001*PR_010010001000+Q_000001101*PR_010010001001+Q_000101001*PR_010010001010+Q_000101101*PR_010010001011); ans_temp[ans_id*6+1]+=Pmtrx[17]*(Q_000000002*PR_010010001000+Q_000000102*PR_010010001001+Q_000000202*PR_010010001002); ans_temp[ans_id*6+2]+=Pmtrx[0]*(Q_002000000*PR_001020000000+Q_102000000*PR_001020000100+Q_202000000*PR_001020000200); ans_temp[ans_id*6+2]+=Pmtrx[1]*(Q_001001000*PR_001020000000+Q_001101000*PR_001020000010+Q_101001000*PR_001020000100+Q_101101000*PR_001020000110); ans_temp[ans_id*6+2]+=Pmtrx[2]*(Q_000002000*PR_001020000000+Q_000102000*PR_001020000010+Q_000202000*PR_001020000020); ans_temp[ans_id*6+2]+=Pmtrx[3]*(Q_001000001*PR_001020000000+Q_001000101*PR_001020000001+Q_101000001*PR_001020000100+Q_101000101*PR_001020000101); ans_temp[ans_id*6+2]+=Pmtrx[4]*(Q_000001001*PR_001020000000+Q_000001101*PR_001020000001+Q_000101001*PR_001020000010+Q_000101101*PR_001020000011); ans_temp[ans_id*6+2]+=Pmtrx[5]*(Q_000000002*PR_001020000000+Q_000000102*PR_001020000001+Q_000000202*PR_001020000002); ans_temp[ans_id*6+2]+=Pmtrx[6]*(Q_002000000*PR_000021000000+Q_102000000*PR_000021000100+Q_202000000*PR_000021000200); ans_temp[ans_id*6+2]+=Pmtrx[7]*(Q_001001000*PR_000021000000+Q_001101000*PR_000021000010+Q_101001000*PR_000021000100+Q_101101000*PR_000021000110); ans_temp[ans_id*6+2]+=Pmtrx[8]*(Q_000002000*PR_000021000000+Q_000102000*PR_000021000010+Q_000202000*PR_000021000020); ans_temp[ans_id*6+2]+=Pmtrx[9]*(Q_001000001*PR_000021000000+Q_001000101*PR_000021000001+Q_101000001*PR_000021000100+Q_101000101*PR_000021000101); ans_temp[ans_id*6+2]+=Pmtrx[10]*(Q_000001001*PR_000021000000+Q_000001101*PR_000021000001+Q_000101001*PR_000021000010+Q_000101101*PR_000021000011); ans_temp[ans_id*6+2]+=Pmtrx[11]*(Q_000000002*PR_000021000000+Q_000000102*PR_000021000001+Q_000000202*PR_000021000002); ans_temp[ans_id*6+2]+=Pmtrx[12]*(Q_002000000*PR_000020001000+Q_102000000*PR_000020001100+Q_202000000*PR_000020001200); ans_temp[ans_id*6+2]+=Pmtrx[13]*(Q_001001000*PR_000020001000+Q_001101000*PR_000020001010+Q_101001000*PR_000020001100+Q_101101000*PR_000020001110); ans_temp[ans_id*6+2]+=Pmtrx[14]*(Q_000002000*PR_000020001000+Q_000102000*PR_000020001010+Q_000202000*PR_000020001020); ans_temp[ans_id*6+2]+=Pmtrx[15]*(Q_001000001*PR_000020001000+Q_001000101*PR_000020001001+Q_101000001*PR_000020001100+Q_101000101*PR_000020001101); ans_temp[ans_id*6+2]+=Pmtrx[16]*(Q_000001001*PR_000020001000+Q_000001101*PR_000020001001+Q_000101001*PR_000020001010+Q_000101101*PR_000020001011); ans_temp[ans_id*6+2]+=Pmtrx[17]*(Q_000000002*PR_000020001000+Q_000000102*PR_000020001001+Q_000000202*PR_000020001002); ans_temp[ans_id*6+3]+=Pmtrx[0]*(Q_002000000*PR_011000010000+Q_102000000*PR_011000010100+Q_202000000*PR_011000010200); ans_temp[ans_id*6+3]+=Pmtrx[1]*(Q_001001000*PR_011000010000+Q_001101000*PR_011000010010+Q_101001000*PR_011000010100+Q_101101000*PR_011000010110); ans_temp[ans_id*6+3]+=Pmtrx[2]*(Q_000002000*PR_011000010000+Q_000102000*PR_011000010010+Q_000202000*PR_011000010020); ans_temp[ans_id*6+3]+=Pmtrx[3]*(Q_001000001*PR_011000010000+Q_001000101*PR_011000010001+Q_101000001*PR_011000010100+Q_101000101*PR_011000010101); ans_temp[ans_id*6+3]+=Pmtrx[4]*(Q_000001001*PR_011000010000+Q_000001101*PR_011000010001+Q_000101001*PR_011000010010+Q_000101101*PR_011000010011); ans_temp[ans_id*6+3]+=Pmtrx[5]*(Q_000000002*PR_011000010000+Q_000000102*PR_011000010001+Q_000000202*PR_011000010002); ans_temp[ans_id*6+3]+=Pmtrx[6]*(Q_002000000*PR_010001010000+Q_102000000*PR_010001010100+Q_202000000*PR_010001010200); ans_temp[ans_id*6+3]+=Pmtrx[7]*(Q_001001000*PR_010001010000+Q_001101000*PR_010001010010+Q_101001000*PR_010001010100+Q_101101000*PR_010001010110); ans_temp[ans_id*6+3]+=Pmtrx[8]*(Q_000002000*PR_010001010000+Q_000102000*PR_010001010010+Q_000202000*PR_010001010020); ans_temp[ans_id*6+3]+=Pmtrx[9]*(Q_001000001*PR_010001010000+Q_001000101*PR_010001010001+Q_101000001*PR_010001010100+Q_101000101*PR_010001010101); ans_temp[ans_id*6+3]+=Pmtrx[10]*(Q_000001001*PR_010001010000+Q_000001101*PR_010001010001+Q_000101001*PR_010001010010+Q_000101101*PR_010001010011); ans_temp[ans_id*6+3]+=Pmtrx[11]*(Q_000000002*PR_010001010000+Q_000000102*PR_010001010001+Q_000000202*PR_010001010002); ans_temp[ans_id*6+3]+=Pmtrx[12]*(Q_002000000*PR_010000011000+Q_102000000*PR_010000011100+Q_202000000*PR_010000011200); ans_temp[ans_id*6+3]+=Pmtrx[13]*(Q_001001000*PR_010000011000+Q_001101000*PR_010000011010+Q_101001000*PR_010000011100+Q_101101000*PR_010000011110); ans_temp[ans_id*6+3]+=Pmtrx[14]*(Q_000002000*PR_010000011000+Q_000102000*PR_010000011010+Q_000202000*PR_010000011020); ans_temp[ans_id*6+3]+=Pmtrx[15]*(Q_001000001*PR_010000011000+Q_001000101*PR_010000011001+Q_101000001*PR_010000011100+Q_101000101*PR_010000011101); ans_temp[ans_id*6+3]+=Pmtrx[16]*(Q_000001001*PR_010000011000+Q_000001101*PR_010000011001+Q_000101001*PR_010000011010+Q_000101101*PR_010000011011); ans_temp[ans_id*6+3]+=Pmtrx[17]*(Q_000000002*PR_010000011000+Q_000000102*PR_010000011001+Q_000000202*PR_010000011002); ans_temp[ans_id*6+4]+=Pmtrx[0]*(Q_002000000*PR_001010010000+Q_102000000*PR_001010010100+Q_202000000*PR_001010010200); ans_temp[ans_id*6+4]+=Pmtrx[1]*(Q_001001000*PR_001010010000+Q_001101000*PR_001010010010+Q_101001000*PR_001010010100+Q_101101000*PR_001010010110); ans_temp[ans_id*6+4]+=Pmtrx[2]*(Q_000002000*PR_001010010000+Q_000102000*PR_001010010010+Q_000202000*PR_001010010020); ans_temp[ans_id*6+4]+=Pmtrx[3]*(Q_001000001*PR_001010010000+Q_001000101*PR_001010010001+Q_101000001*PR_001010010100+Q_101000101*PR_001010010101); ans_temp[ans_id*6+4]+=Pmtrx[4]*(Q_000001001*PR_001010010000+Q_000001101*PR_001010010001+Q_000101001*PR_001010010010+Q_000101101*PR_001010010011); ans_temp[ans_id*6+4]+=Pmtrx[5]*(Q_000000002*PR_001010010000+Q_000000102*PR_001010010001+Q_000000202*PR_001010010002); ans_temp[ans_id*6+4]+=Pmtrx[6]*(Q_002000000*PR_000011010000+Q_102000000*PR_000011010100+Q_202000000*PR_000011010200); ans_temp[ans_id*6+4]+=Pmtrx[7]*(Q_001001000*PR_000011010000+Q_001101000*PR_000011010010+Q_101001000*PR_000011010100+Q_101101000*PR_000011010110); ans_temp[ans_id*6+4]+=Pmtrx[8]*(Q_000002000*PR_000011010000+Q_000102000*PR_000011010010+Q_000202000*PR_000011010020); ans_temp[ans_id*6+4]+=Pmtrx[9]*(Q_001000001*PR_000011010000+Q_001000101*PR_000011010001+Q_101000001*PR_000011010100+Q_101000101*PR_000011010101); ans_temp[ans_id*6+4]+=Pmtrx[10]*(Q_000001001*PR_000011010000+Q_000001101*PR_000011010001+Q_000101001*PR_000011010010+Q_000101101*PR_000011010011); ans_temp[ans_id*6+4]+=Pmtrx[11]*(Q_000000002*PR_000011010000+Q_000000102*PR_000011010001+Q_000000202*PR_000011010002); ans_temp[ans_id*6+4]+=Pmtrx[12]*(Q_002000000*PR_000010011000+Q_102000000*PR_000010011100+Q_202000000*PR_000010011200); ans_temp[ans_id*6+4]+=Pmtrx[13]*(Q_001001000*PR_000010011000+Q_001101000*PR_000010011010+Q_101001000*PR_000010011100+Q_101101000*PR_000010011110); ans_temp[ans_id*6+4]+=Pmtrx[14]*(Q_000002000*PR_000010011000+Q_000102000*PR_000010011010+Q_000202000*PR_000010011020); ans_temp[ans_id*6+4]+=Pmtrx[15]*(Q_001000001*PR_000010011000+Q_001000101*PR_000010011001+Q_101000001*PR_000010011100+Q_101000101*PR_000010011101); ans_temp[ans_id*6+4]+=Pmtrx[16]*(Q_000001001*PR_000010011000+Q_000001101*PR_000010011001+Q_000101001*PR_000010011010+Q_000101101*PR_000010011011); ans_temp[ans_id*6+4]+=Pmtrx[17]*(Q_000000002*PR_000010011000+Q_000000102*PR_000010011001+Q_000000202*PR_000010011002); ans_temp[ans_id*6+5]+=Pmtrx[0]*(Q_002000000*PR_001000020000+Q_102000000*PR_001000020100+Q_202000000*PR_001000020200); ans_temp[ans_id*6+5]+=Pmtrx[1]*(Q_001001000*PR_001000020000+Q_001101000*PR_001000020010+Q_101001000*PR_001000020100+Q_101101000*PR_001000020110); ans_temp[ans_id*6+5]+=Pmtrx[2]*(Q_000002000*PR_001000020000+Q_000102000*PR_001000020010+Q_000202000*PR_001000020020); ans_temp[ans_id*6+5]+=Pmtrx[3]*(Q_001000001*PR_001000020000+Q_001000101*PR_001000020001+Q_101000001*PR_001000020100+Q_101000101*PR_001000020101); ans_temp[ans_id*6+5]+=Pmtrx[4]*(Q_000001001*PR_001000020000+Q_000001101*PR_001000020001+Q_000101001*PR_001000020010+Q_000101101*PR_001000020011); ans_temp[ans_id*6+5]+=Pmtrx[5]*(Q_000000002*PR_001000020000+Q_000000102*PR_001000020001+Q_000000202*PR_001000020002); ans_temp[ans_id*6+5]+=Pmtrx[6]*(Q_002000000*PR_000001020000+Q_102000000*PR_000001020100+Q_202000000*PR_000001020200); ans_temp[ans_id*6+5]+=Pmtrx[7]*(Q_001001000*PR_000001020000+Q_001101000*PR_000001020010+Q_101001000*PR_000001020100+Q_101101000*PR_000001020110); ans_temp[ans_id*6+5]+=Pmtrx[8]*(Q_000002000*PR_000001020000+Q_000102000*PR_000001020010+Q_000202000*PR_000001020020); ans_temp[ans_id*6+5]+=Pmtrx[9]*(Q_001000001*PR_000001020000+Q_001000101*PR_000001020001+Q_101000001*PR_000001020100+Q_101000101*PR_000001020101); ans_temp[ans_id*6+5]+=Pmtrx[10]*(Q_000001001*PR_000001020000+Q_000001101*PR_000001020001+Q_000101001*PR_000001020010+Q_000101101*PR_000001020011); ans_temp[ans_id*6+5]+=Pmtrx[11]*(Q_000000002*PR_000001020000+Q_000000102*PR_000001020001+Q_000000202*PR_000001020002); ans_temp[ans_id*6+5]+=Pmtrx[12]*(Q_002000000*PR_000000021000+Q_102000000*PR_000000021100+Q_202000000*PR_000000021200); ans_temp[ans_id*6+5]+=Pmtrx[13]*(Q_001001000*PR_000000021000+Q_001101000*PR_000000021010+Q_101001000*PR_000000021100+Q_101101000*PR_000000021110); ans_temp[ans_id*6+5]+=Pmtrx[14]*(Q_000002000*PR_000000021000+Q_000102000*PR_000000021010+Q_000202000*PR_000000021020); ans_temp[ans_id*6+5]+=Pmtrx[15]*(Q_001000001*PR_000000021000+Q_001000101*PR_000000021001+Q_101000001*PR_000000021100+Q_101000101*PR_000000021101); ans_temp[ans_id*6+5]+=Pmtrx[16]*(Q_000001001*PR_000000021000+Q_000001101*PR_000000021001+Q_000101001*PR_000000021010+Q_000101101*PR_000000021011); ans_temp[ans_id*6+5]+=Pmtrx[17]*(Q_000000002*PR_000000021000+Q_000000102*PR_000000021001+Q_000000202*PR_000000021002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } } __global__ void MD_Kq_dpsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[18]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_010[3]; Pd_010[0]=PA[ii*3+0]; Pd_010[1]=PA[ii*3+1]; Pd_010[2]=PA[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<3;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[6]; Ft_fs_5(5,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[5]; double R_200[4]; double R_300[3]; double R_400[2]; double R_500[1]; double R_010[5]; double R_110[4]; double R_210[3]; double R_310[2]; double R_410[1]; double R_020[4]; double R_120[3]; double R_220[2]; double R_320[1]; double R_030[3]; double R_130[2]; double R_230[1]; double R_040[2]; double R_140[1]; double R_050[1]; double R_001[5]; double R_101[4]; double R_201[3]; double R_301[2]; double R_401[1]; double R_011[4]; double R_111[3]; double R_211[2]; double R_311[1]; double R_021[3]; double R_121[2]; double R_221[1]; double R_031[2]; double R_131[1]; double R_041[1]; double R_002[4]; double R_102[3]; double R_202[2]; double R_302[1]; double R_012[3]; double R_112[2]; double R_212[1]; double R_022[2]; double R_122[1]; double R_032[1]; double R_003[3]; double R_103[2]; double R_203[1]; double R_013[2]; double R_113[1]; double R_023[1]; double R_004[2]; double R_104[1]; double R_014[1]; double R_005[1]; for(int i=0;i<5;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<5;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<5;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<4;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<4;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<4;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<4;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<3;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<3;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<3;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<3;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<3;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<3;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<3;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<3;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<3;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<3;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<2;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<2;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<2;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<2;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<2;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<2;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<2;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<2;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<2;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<2;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<2;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<2;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<2;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<2;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<1;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<1;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<1;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<1;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<1;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<1;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<1;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<1;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<1;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<1;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<1;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<1;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<1;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<1;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<1;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<1;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<1;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<1;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<1;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<1;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<1;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double QR_002000000003=Q_002000000*R_003[0]+-1*Q_102000000*R_103[0]+Q_202000000*R_203[0]; double QR_001001000003=Q_001001000*R_003[0]+-1*Q_001101000*R_013[0]+-1*Q_101001000*R_103[0]+Q_101101000*R_113[0]; double QR_000002000003=Q_000002000*R_003[0]+-1*Q_000102000*R_013[0]+Q_000202000*R_023[0]; double QR_001000001003=Q_001000001*R_003[0]+-1*Q_001000101*R_004[0]+-1*Q_101000001*R_103[0]+Q_101000101*R_104[0]; double QR_000001001003=Q_000001001*R_003[0]+-1*Q_000001101*R_004[0]+-1*Q_000101001*R_013[0]+Q_000101101*R_014[0]; double QR_000000002003=Q_000000002*R_003[0]+-1*Q_000000102*R_004[0]+Q_000000202*R_005[0]; double QR_002000000012=Q_002000000*R_012[0]+-1*Q_102000000*R_112[0]+Q_202000000*R_212[0]; double QR_001001000012=Q_001001000*R_012[0]+-1*Q_001101000*R_022[0]+-1*Q_101001000*R_112[0]+Q_101101000*R_122[0]; double QR_000002000012=Q_000002000*R_012[0]+-1*Q_000102000*R_022[0]+Q_000202000*R_032[0]; double QR_001000001012=Q_001000001*R_012[0]+-1*Q_001000101*R_013[0]+-1*Q_101000001*R_112[0]+Q_101000101*R_113[0]; double QR_000001001012=Q_000001001*R_012[0]+-1*Q_000001101*R_013[0]+-1*Q_000101001*R_022[0]+Q_000101101*R_023[0]; double QR_000000002012=Q_000000002*R_012[0]+-1*Q_000000102*R_013[0]+Q_000000202*R_014[0]; double QR_002000000021=Q_002000000*R_021[0]+-1*Q_102000000*R_121[0]+Q_202000000*R_221[0]; double QR_001001000021=Q_001001000*R_021[0]+-1*Q_001101000*R_031[0]+-1*Q_101001000*R_121[0]+Q_101101000*R_131[0]; double QR_000002000021=Q_000002000*R_021[0]+-1*Q_000102000*R_031[0]+Q_000202000*R_041[0]; double QR_001000001021=Q_001000001*R_021[0]+-1*Q_001000101*R_022[0]+-1*Q_101000001*R_121[0]+Q_101000101*R_122[0]; double QR_000001001021=Q_000001001*R_021[0]+-1*Q_000001101*R_022[0]+-1*Q_000101001*R_031[0]+Q_000101101*R_032[0]; double QR_000000002021=Q_000000002*R_021[0]+-1*Q_000000102*R_022[0]+Q_000000202*R_023[0]; double QR_002000000030=Q_002000000*R_030[0]+-1*Q_102000000*R_130[0]+Q_202000000*R_230[0]; double QR_001001000030=Q_001001000*R_030[0]+-1*Q_001101000*R_040[0]+-1*Q_101001000*R_130[0]+Q_101101000*R_140[0]; double QR_000002000030=Q_000002000*R_030[0]+-1*Q_000102000*R_040[0]+Q_000202000*R_050[0]; double QR_001000001030=Q_001000001*R_030[0]+-1*Q_001000101*R_031[0]+-1*Q_101000001*R_130[0]+Q_101000101*R_131[0]; double QR_000001001030=Q_000001001*R_030[0]+-1*Q_000001101*R_031[0]+-1*Q_000101001*R_040[0]+Q_000101101*R_041[0]; double QR_000000002030=Q_000000002*R_030[0]+-1*Q_000000102*R_031[0]+Q_000000202*R_032[0]; double QR_002000000102=Q_002000000*R_102[0]+-1*Q_102000000*R_202[0]+Q_202000000*R_302[0]; double QR_001001000102=Q_001001000*R_102[0]+-1*Q_001101000*R_112[0]+-1*Q_101001000*R_202[0]+Q_101101000*R_212[0]; double QR_000002000102=Q_000002000*R_102[0]+-1*Q_000102000*R_112[0]+Q_000202000*R_122[0]; double QR_001000001102=Q_001000001*R_102[0]+-1*Q_001000101*R_103[0]+-1*Q_101000001*R_202[0]+Q_101000101*R_203[0]; double QR_000001001102=Q_000001001*R_102[0]+-1*Q_000001101*R_103[0]+-1*Q_000101001*R_112[0]+Q_000101101*R_113[0]; double QR_000000002102=Q_000000002*R_102[0]+-1*Q_000000102*R_103[0]+Q_000000202*R_104[0]; double QR_002000000111=Q_002000000*R_111[0]+-1*Q_102000000*R_211[0]+Q_202000000*R_311[0]; double QR_001001000111=Q_001001000*R_111[0]+-1*Q_001101000*R_121[0]+-1*Q_101001000*R_211[0]+Q_101101000*R_221[0]; double QR_000002000111=Q_000002000*R_111[0]+-1*Q_000102000*R_121[0]+Q_000202000*R_131[0]; double QR_001000001111=Q_001000001*R_111[0]+-1*Q_001000101*R_112[0]+-1*Q_101000001*R_211[0]+Q_101000101*R_212[0]; double QR_000001001111=Q_000001001*R_111[0]+-1*Q_000001101*R_112[0]+-1*Q_000101001*R_121[0]+Q_000101101*R_122[0]; double QR_000000002111=Q_000000002*R_111[0]+-1*Q_000000102*R_112[0]+Q_000000202*R_113[0]; double QR_002000000120=Q_002000000*R_120[0]+-1*Q_102000000*R_220[0]+Q_202000000*R_320[0]; double QR_001001000120=Q_001001000*R_120[0]+-1*Q_001101000*R_130[0]+-1*Q_101001000*R_220[0]+Q_101101000*R_230[0]; double QR_000002000120=Q_000002000*R_120[0]+-1*Q_000102000*R_130[0]+Q_000202000*R_140[0]; double QR_001000001120=Q_001000001*R_120[0]+-1*Q_001000101*R_121[0]+-1*Q_101000001*R_220[0]+Q_101000101*R_221[0]; double QR_000001001120=Q_000001001*R_120[0]+-1*Q_000001101*R_121[0]+-1*Q_000101001*R_130[0]+Q_000101101*R_131[0]; double QR_000000002120=Q_000000002*R_120[0]+-1*Q_000000102*R_121[0]+Q_000000202*R_122[0]; double QR_002000000201=Q_002000000*R_201[0]+-1*Q_102000000*R_301[0]+Q_202000000*R_401[0]; double QR_001001000201=Q_001001000*R_201[0]+-1*Q_001101000*R_211[0]+-1*Q_101001000*R_301[0]+Q_101101000*R_311[0]; double QR_000002000201=Q_000002000*R_201[0]+-1*Q_000102000*R_211[0]+Q_000202000*R_221[0]; double QR_001000001201=Q_001000001*R_201[0]+-1*Q_001000101*R_202[0]+-1*Q_101000001*R_301[0]+Q_101000101*R_302[0]; double QR_000001001201=Q_000001001*R_201[0]+-1*Q_000001101*R_202[0]+-1*Q_000101001*R_211[0]+Q_000101101*R_212[0]; double QR_000000002201=Q_000000002*R_201[0]+-1*Q_000000102*R_202[0]+Q_000000202*R_203[0]; double QR_002000000210=Q_002000000*R_210[0]+-1*Q_102000000*R_310[0]+Q_202000000*R_410[0]; double QR_001001000210=Q_001001000*R_210[0]+-1*Q_001101000*R_220[0]+-1*Q_101001000*R_310[0]+Q_101101000*R_320[0]; double QR_000002000210=Q_000002000*R_210[0]+-1*Q_000102000*R_220[0]+Q_000202000*R_230[0]; double QR_001000001210=Q_001000001*R_210[0]+-1*Q_001000101*R_211[0]+-1*Q_101000001*R_310[0]+Q_101000101*R_311[0]; double QR_000001001210=Q_000001001*R_210[0]+-1*Q_000001101*R_211[0]+-1*Q_000101001*R_220[0]+Q_000101101*R_221[0]; double QR_000000002210=Q_000000002*R_210[0]+-1*Q_000000102*R_211[0]+Q_000000202*R_212[0]; double QR_002000000300=Q_002000000*R_300[0]+-1*Q_102000000*R_400[0]+Q_202000000*R_500[0]; double QR_001001000300=Q_001001000*R_300[0]+-1*Q_001101000*R_310[0]+-1*Q_101001000*R_400[0]+Q_101101000*R_410[0]; double QR_000002000300=Q_000002000*R_300[0]+-1*Q_000102000*R_310[0]+Q_000202000*R_320[0]; double QR_001000001300=Q_001000001*R_300[0]+-1*Q_001000101*R_301[0]+-1*Q_101000001*R_400[0]+Q_101000101*R_401[0]; double QR_000001001300=Q_000001001*R_300[0]+-1*Q_000001101*R_301[0]+-1*Q_000101001*R_310[0]+Q_000101101*R_311[0]; double QR_000000002300=Q_000000002*R_300[0]+-1*Q_000000102*R_301[0]+Q_000000202*R_302[0]; double Pd_101[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } double P_021000000=Pd_021[0]; double P_121000000=Pd_121[0]; double P_221000000=Pd_221[0]; double P_321000000=Pd_321[0]; double P_020001000=Pd_020[0]*Pd_001[1]; double P_020101000=Pd_020[0]*Pd_101[1]; double P_120001000=Pd_120[0]*Pd_001[1]; double P_120101000=Pd_120[0]*Pd_101[1]; double P_220001000=Pd_220[0]*Pd_001[1]; double P_220101000=Pd_220[0]*Pd_101[1]; double P_020000001=Pd_020[0]*Pd_001[2]; double P_020000101=Pd_020[0]*Pd_101[2]; double P_120000001=Pd_120[0]*Pd_001[2]; double P_120000101=Pd_120[0]*Pd_101[2]; double P_220000001=Pd_220[0]*Pd_001[2]; double P_220000101=Pd_220[0]*Pd_101[2]; double P_011010000=Pd_011[0]*Pd_010[1]; double P_011110000=Pd_011[0]*Pd_110[1]; double P_111010000=Pd_111[0]*Pd_010[1]; double P_111110000=Pd_111[0]*Pd_110[1]; double P_211010000=Pd_211[0]*Pd_010[1]; double P_211110000=Pd_211[0]*Pd_110[1]; double P_010011000=Pd_010[0]*Pd_011[1]; double P_010111000=Pd_010[0]*Pd_111[1]; double P_010211000=Pd_010[0]*Pd_211[1]; double P_110011000=Pd_110[0]*Pd_011[1]; double P_110111000=Pd_110[0]*Pd_111[1]; double P_110211000=Pd_110[0]*Pd_211[1]; double P_010010001=Pd_010[0]*Pd_010[1]*Pd_001[2]; double P_010010101=Pd_010[0]*Pd_010[1]*Pd_101[2]; double P_010110001=Pd_010[0]*Pd_110[1]*Pd_001[2]; double P_010110101=Pd_010[0]*Pd_110[1]*Pd_101[2]; double P_110010001=Pd_110[0]*Pd_010[1]*Pd_001[2]; double P_110010101=Pd_110[0]*Pd_010[1]*Pd_101[2]; double P_110110001=Pd_110[0]*Pd_110[1]*Pd_001[2]; double P_110110101=Pd_110[0]*Pd_110[1]*Pd_101[2]; double P_001020000=Pd_001[0]*Pd_020[1]; double P_001120000=Pd_001[0]*Pd_120[1]; double P_001220000=Pd_001[0]*Pd_220[1]; double P_101020000=Pd_101[0]*Pd_020[1]; double P_101120000=Pd_101[0]*Pd_120[1]; double P_101220000=Pd_101[0]*Pd_220[1]; double P_000021000=Pd_021[1]; double P_000121000=Pd_121[1]; double P_000221000=Pd_221[1]; double P_000321000=Pd_321[1]; double P_000020001=Pd_020[1]*Pd_001[2]; double P_000020101=Pd_020[1]*Pd_101[2]; double P_000120001=Pd_120[1]*Pd_001[2]; double P_000120101=Pd_120[1]*Pd_101[2]; double P_000220001=Pd_220[1]*Pd_001[2]; double P_000220101=Pd_220[1]*Pd_101[2]; double P_011000010=Pd_011[0]*Pd_010[2]; double P_011000110=Pd_011[0]*Pd_110[2]; double P_111000010=Pd_111[0]*Pd_010[2]; double P_111000110=Pd_111[0]*Pd_110[2]; double P_211000010=Pd_211[0]*Pd_010[2]; double P_211000110=Pd_211[0]*Pd_110[2]; double P_010001010=Pd_010[0]*Pd_001[1]*Pd_010[2]; double P_010001110=Pd_010[0]*Pd_001[1]*Pd_110[2]; double P_010101010=Pd_010[0]*Pd_101[1]*Pd_010[2]; double P_010101110=Pd_010[0]*Pd_101[1]*Pd_110[2]; double P_110001010=Pd_110[0]*Pd_001[1]*Pd_010[2]; double P_110001110=Pd_110[0]*Pd_001[1]*Pd_110[2]; double P_110101010=Pd_110[0]*Pd_101[1]*Pd_010[2]; double P_110101110=Pd_110[0]*Pd_101[1]*Pd_110[2]; double P_010000011=Pd_010[0]*Pd_011[2]; double P_010000111=Pd_010[0]*Pd_111[2]; double P_010000211=Pd_010[0]*Pd_211[2]; double P_110000011=Pd_110[0]*Pd_011[2]; double P_110000111=Pd_110[0]*Pd_111[2]; double P_110000211=Pd_110[0]*Pd_211[2]; double P_001010010=Pd_001[0]*Pd_010[1]*Pd_010[2]; double P_001010110=Pd_001[0]*Pd_010[1]*Pd_110[2]; double P_001110010=Pd_001[0]*Pd_110[1]*Pd_010[2]; double P_001110110=Pd_001[0]*Pd_110[1]*Pd_110[2]; double P_101010010=Pd_101[0]*Pd_010[1]*Pd_010[2]; double P_101010110=Pd_101[0]*Pd_010[1]*Pd_110[2]; double P_101110010=Pd_101[0]*Pd_110[1]*Pd_010[2]; double P_101110110=Pd_101[0]*Pd_110[1]*Pd_110[2]; double P_000011010=Pd_011[1]*Pd_010[2]; double P_000011110=Pd_011[1]*Pd_110[2]; double P_000111010=Pd_111[1]*Pd_010[2]; double P_000111110=Pd_111[1]*Pd_110[2]; double P_000211010=Pd_211[1]*Pd_010[2]; double P_000211110=Pd_211[1]*Pd_110[2]; double P_000010011=Pd_010[1]*Pd_011[2]; double P_000010111=Pd_010[1]*Pd_111[2]; double P_000010211=Pd_010[1]*Pd_211[2]; double P_000110011=Pd_110[1]*Pd_011[2]; double P_000110111=Pd_110[1]*Pd_111[2]; double P_000110211=Pd_110[1]*Pd_211[2]; double P_001000020=Pd_001[0]*Pd_020[2]; double P_001000120=Pd_001[0]*Pd_120[2]; double P_001000220=Pd_001[0]*Pd_220[2]; double P_101000020=Pd_101[0]*Pd_020[2]; double P_101000120=Pd_101[0]*Pd_120[2]; double P_101000220=Pd_101[0]*Pd_220[2]; double P_000001020=Pd_001[1]*Pd_020[2]; double P_000001120=Pd_001[1]*Pd_120[2]; double P_000001220=Pd_001[1]*Pd_220[2]; double P_000101020=Pd_101[1]*Pd_020[2]; double P_000101120=Pd_101[1]*Pd_120[2]; double P_000101220=Pd_101[1]*Pd_220[2]; double P_000000021=Pd_021[2]; double P_000000121=Pd_121[2]; double P_000000221=Pd_221[2]; double P_000000321=Pd_321[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(P_021000000*QR_002000000000+P_121000000*QR_002000000100+P_221000000*QR_002000000200+P_321000000*QR_002000000300); ans_temp[ans_id*6+0]+=Pmtrx[1]*(P_021000000*QR_001001000000+P_121000000*QR_001001000100+P_221000000*QR_001001000200+P_321000000*QR_001001000300); ans_temp[ans_id*6+0]+=Pmtrx[2]*(P_021000000*QR_000002000000+P_121000000*QR_000002000100+P_221000000*QR_000002000200+P_321000000*QR_000002000300); ans_temp[ans_id*6+0]+=Pmtrx[3]*(P_021000000*QR_001000001000+P_121000000*QR_001000001100+P_221000000*QR_001000001200+P_321000000*QR_001000001300); ans_temp[ans_id*6+0]+=Pmtrx[4]*(P_021000000*QR_000001001000+P_121000000*QR_000001001100+P_221000000*QR_000001001200+P_321000000*QR_000001001300); ans_temp[ans_id*6+0]+=Pmtrx[5]*(P_021000000*QR_000000002000+P_121000000*QR_000000002100+P_221000000*QR_000000002200+P_321000000*QR_000000002300); ans_temp[ans_id*6+0]+=Pmtrx[6]*(P_020001000*QR_002000000000+P_020101000*QR_002000000010+P_120001000*QR_002000000100+P_120101000*QR_002000000110+P_220001000*QR_002000000200+P_220101000*QR_002000000210); ans_temp[ans_id*6+0]+=Pmtrx[7]*(P_020001000*QR_001001000000+P_020101000*QR_001001000010+P_120001000*QR_001001000100+P_120101000*QR_001001000110+P_220001000*QR_001001000200+P_220101000*QR_001001000210); ans_temp[ans_id*6+0]+=Pmtrx[8]*(P_020001000*QR_000002000000+P_020101000*QR_000002000010+P_120001000*QR_000002000100+P_120101000*QR_000002000110+P_220001000*QR_000002000200+P_220101000*QR_000002000210); ans_temp[ans_id*6+0]+=Pmtrx[9]*(P_020001000*QR_001000001000+P_020101000*QR_001000001010+P_120001000*QR_001000001100+P_120101000*QR_001000001110+P_220001000*QR_001000001200+P_220101000*QR_001000001210); ans_temp[ans_id*6+0]+=Pmtrx[10]*(P_020001000*QR_000001001000+P_020101000*QR_000001001010+P_120001000*QR_000001001100+P_120101000*QR_000001001110+P_220001000*QR_000001001200+P_220101000*QR_000001001210); ans_temp[ans_id*6+0]+=Pmtrx[11]*(P_020001000*QR_000000002000+P_020101000*QR_000000002010+P_120001000*QR_000000002100+P_120101000*QR_000000002110+P_220001000*QR_000000002200+P_220101000*QR_000000002210); ans_temp[ans_id*6+0]+=Pmtrx[12]*(P_020000001*QR_002000000000+P_020000101*QR_002000000001+P_120000001*QR_002000000100+P_120000101*QR_002000000101+P_220000001*QR_002000000200+P_220000101*QR_002000000201); ans_temp[ans_id*6+0]+=Pmtrx[13]*(P_020000001*QR_001001000000+P_020000101*QR_001001000001+P_120000001*QR_001001000100+P_120000101*QR_001001000101+P_220000001*QR_001001000200+P_220000101*QR_001001000201); ans_temp[ans_id*6+0]+=Pmtrx[14]*(P_020000001*QR_000002000000+P_020000101*QR_000002000001+P_120000001*QR_000002000100+P_120000101*QR_000002000101+P_220000001*QR_000002000200+P_220000101*QR_000002000201); ans_temp[ans_id*6+0]+=Pmtrx[15]*(P_020000001*QR_001000001000+P_020000101*QR_001000001001+P_120000001*QR_001000001100+P_120000101*QR_001000001101+P_220000001*QR_001000001200+P_220000101*QR_001000001201); ans_temp[ans_id*6+0]+=Pmtrx[16]*(P_020000001*QR_000001001000+P_020000101*QR_000001001001+P_120000001*QR_000001001100+P_120000101*QR_000001001101+P_220000001*QR_000001001200+P_220000101*QR_000001001201); ans_temp[ans_id*6+0]+=Pmtrx[17]*(P_020000001*QR_000000002000+P_020000101*QR_000000002001+P_120000001*QR_000000002100+P_120000101*QR_000000002101+P_220000001*QR_000000002200+P_220000101*QR_000000002201); ans_temp[ans_id*6+1]+=Pmtrx[0]*(P_011010000*QR_002000000000+P_011110000*QR_002000000010+P_111010000*QR_002000000100+P_111110000*QR_002000000110+P_211010000*QR_002000000200+P_211110000*QR_002000000210); ans_temp[ans_id*6+1]+=Pmtrx[1]*(P_011010000*QR_001001000000+P_011110000*QR_001001000010+P_111010000*QR_001001000100+P_111110000*QR_001001000110+P_211010000*QR_001001000200+P_211110000*QR_001001000210); ans_temp[ans_id*6+1]+=Pmtrx[2]*(P_011010000*QR_000002000000+P_011110000*QR_000002000010+P_111010000*QR_000002000100+P_111110000*QR_000002000110+P_211010000*QR_000002000200+P_211110000*QR_000002000210); ans_temp[ans_id*6+1]+=Pmtrx[3]*(P_011010000*QR_001000001000+P_011110000*QR_001000001010+P_111010000*QR_001000001100+P_111110000*QR_001000001110+P_211010000*QR_001000001200+P_211110000*QR_001000001210); ans_temp[ans_id*6+1]+=Pmtrx[4]*(P_011010000*QR_000001001000+P_011110000*QR_000001001010+P_111010000*QR_000001001100+P_111110000*QR_000001001110+P_211010000*QR_000001001200+P_211110000*QR_000001001210); ans_temp[ans_id*6+1]+=Pmtrx[5]*(P_011010000*QR_000000002000+P_011110000*QR_000000002010+P_111010000*QR_000000002100+P_111110000*QR_000000002110+P_211010000*QR_000000002200+P_211110000*QR_000000002210); ans_temp[ans_id*6+1]+=Pmtrx[6]*(P_010011000*QR_002000000000+P_010111000*QR_002000000010+P_010211000*QR_002000000020+P_110011000*QR_002000000100+P_110111000*QR_002000000110+P_110211000*QR_002000000120); ans_temp[ans_id*6+1]+=Pmtrx[7]*(P_010011000*QR_001001000000+P_010111000*QR_001001000010+P_010211000*QR_001001000020+P_110011000*QR_001001000100+P_110111000*QR_001001000110+P_110211000*QR_001001000120); ans_temp[ans_id*6+1]+=Pmtrx[8]*(P_010011000*QR_000002000000+P_010111000*QR_000002000010+P_010211000*QR_000002000020+P_110011000*QR_000002000100+P_110111000*QR_000002000110+P_110211000*QR_000002000120); ans_temp[ans_id*6+1]+=Pmtrx[9]*(P_010011000*QR_001000001000+P_010111000*QR_001000001010+P_010211000*QR_001000001020+P_110011000*QR_001000001100+P_110111000*QR_001000001110+P_110211000*QR_001000001120); ans_temp[ans_id*6+1]+=Pmtrx[10]*(P_010011000*QR_000001001000+P_010111000*QR_000001001010+P_010211000*QR_000001001020+P_110011000*QR_000001001100+P_110111000*QR_000001001110+P_110211000*QR_000001001120); ans_temp[ans_id*6+1]+=Pmtrx[11]*(P_010011000*QR_000000002000+P_010111000*QR_000000002010+P_010211000*QR_000000002020+P_110011000*QR_000000002100+P_110111000*QR_000000002110+P_110211000*QR_000000002120); ans_temp[ans_id*6+1]+=Pmtrx[12]*(P_010010001*QR_002000000000+P_010010101*QR_002000000001+P_010110001*QR_002000000010+P_010110101*QR_002000000011+P_110010001*QR_002000000100+P_110010101*QR_002000000101+P_110110001*QR_002000000110+P_110110101*QR_002000000111); ans_temp[ans_id*6+1]+=Pmtrx[13]*(P_010010001*QR_001001000000+P_010010101*QR_001001000001+P_010110001*QR_001001000010+P_010110101*QR_001001000011+P_110010001*QR_001001000100+P_110010101*QR_001001000101+P_110110001*QR_001001000110+P_110110101*QR_001001000111); ans_temp[ans_id*6+1]+=Pmtrx[14]*(P_010010001*QR_000002000000+P_010010101*QR_000002000001+P_010110001*QR_000002000010+P_010110101*QR_000002000011+P_110010001*QR_000002000100+P_110010101*QR_000002000101+P_110110001*QR_000002000110+P_110110101*QR_000002000111); ans_temp[ans_id*6+1]+=Pmtrx[15]*(P_010010001*QR_001000001000+P_010010101*QR_001000001001+P_010110001*QR_001000001010+P_010110101*QR_001000001011+P_110010001*QR_001000001100+P_110010101*QR_001000001101+P_110110001*QR_001000001110+P_110110101*QR_001000001111); ans_temp[ans_id*6+1]+=Pmtrx[16]*(P_010010001*QR_000001001000+P_010010101*QR_000001001001+P_010110001*QR_000001001010+P_010110101*QR_000001001011+P_110010001*QR_000001001100+P_110010101*QR_000001001101+P_110110001*QR_000001001110+P_110110101*QR_000001001111); ans_temp[ans_id*6+1]+=Pmtrx[17]*(P_010010001*QR_000000002000+P_010010101*QR_000000002001+P_010110001*QR_000000002010+P_010110101*QR_000000002011+P_110010001*QR_000000002100+P_110010101*QR_000000002101+P_110110001*QR_000000002110+P_110110101*QR_000000002111); ans_temp[ans_id*6+2]+=Pmtrx[0]*(P_001020000*QR_002000000000+P_001120000*QR_002000000010+P_001220000*QR_002000000020+P_101020000*QR_002000000100+P_101120000*QR_002000000110+P_101220000*QR_002000000120); ans_temp[ans_id*6+2]+=Pmtrx[1]*(P_001020000*QR_001001000000+P_001120000*QR_001001000010+P_001220000*QR_001001000020+P_101020000*QR_001001000100+P_101120000*QR_001001000110+P_101220000*QR_001001000120); ans_temp[ans_id*6+2]+=Pmtrx[2]*(P_001020000*QR_000002000000+P_001120000*QR_000002000010+P_001220000*QR_000002000020+P_101020000*QR_000002000100+P_101120000*QR_000002000110+P_101220000*QR_000002000120); ans_temp[ans_id*6+2]+=Pmtrx[3]*(P_001020000*QR_001000001000+P_001120000*QR_001000001010+P_001220000*QR_001000001020+P_101020000*QR_001000001100+P_101120000*QR_001000001110+P_101220000*QR_001000001120); ans_temp[ans_id*6+2]+=Pmtrx[4]*(P_001020000*QR_000001001000+P_001120000*QR_000001001010+P_001220000*QR_000001001020+P_101020000*QR_000001001100+P_101120000*QR_000001001110+P_101220000*QR_000001001120); ans_temp[ans_id*6+2]+=Pmtrx[5]*(P_001020000*QR_000000002000+P_001120000*QR_000000002010+P_001220000*QR_000000002020+P_101020000*QR_000000002100+P_101120000*QR_000000002110+P_101220000*QR_000000002120); ans_temp[ans_id*6+2]+=Pmtrx[6]*(P_000021000*QR_002000000000+P_000121000*QR_002000000010+P_000221000*QR_002000000020+P_000321000*QR_002000000030); ans_temp[ans_id*6+2]+=Pmtrx[7]*(P_000021000*QR_001001000000+P_000121000*QR_001001000010+P_000221000*QR_001001000020+P_000321000*QR_001001000030); ans_temp[ans_id*6+2]+=Pmtrx[8]*(P_000021000*QR_000002000000+P_000121000*QR_000002000010+P_000221000*QR_000002000020+P_000321000*QR_000002000030); ans_temp[ans_id*6+2]+=Pmtrx[9]*(P_000021000*QR_001000001000+P_000121000*QR_001000001010+P_000221000*QR_001000001020+P_000321000*QR_001000001030); ans_temp[ans_id*6+2]+=Pmtrx[10]*(P_000021000*QR_000001001000+P_000121000*QR_000001001010+P_000221000*QR_000001001020+P_000321000*QR_000001001030); ans_temp[ans_id*6+2]+=Pmtrx[11]*(P_000021000*QR_000000002000+P_000121000*QR_000000002010+P_000221000*QR_000000002020+P_000321000*QR_000000002030); ans_temp[ans_id*6+2]+=Pmtrx[12]*(P_000020001*QR_002000000000+P_000020101*QR_002000000001+P_000120001*QR_002000000010+P_000120101*QR_002000000011+P_000220001*QR_002000000020+P_000220101*QR_002000000021); ans_temp[ans_id*6+2]+=Pmtrx[13]*(P_000020001*QR_001001000000+P_000020101*QR_001001000001+P_000120001*QR_001001000010+P_000120101*QR_001001000011+P_000220001*QR_001001000020+P_000220101*QR_001001000021); ans_temp[ans_id*6+2]+=Pmtrx[14]*(P_000020001*QR_000002000000+P_000020101*QR_000002000001+P_000120001*QR_000002000010+P_000120101*QR_000002000011+P_000220001*QR_000002000020+P_000220101*QR_000002000021); ans_temp[ans_id*6+2]+=Pmtrx[15]*(P_000020001*QR_001000001000+P_000020101*QR_001000001001+P_000120001*QR_001000001010+P_000120101*QR_001000001011+P_000220001*QR_001000001020+P_000220101*QR_001000001021); ans_temp[ans_id*6+2]+=Pmtrx[16]*(P_000020001*QR_000001001000+P_000020101*QR_000001001001+P_000120001*QR_000001001010+P_000120101*QR_000001001011+P_000220001*QR_000001001020+P_000220101*QR_000001001021); ans_temp[ans_id*6+2]+=Pmtrx[17]*(P_000020001*QR_000000002000+P_000020101*QR_000000002001+P_000120001*QR_000000002010+P_000120101*QR_000000002011+P_000220001*QR_000000002020+P_000220101*QR_000000002021); ans_temp[ans_id*6+3]+=Pmtrx[0]*(P_011000010*QR_002000000000+P_011000110*QR_002000000001+P_111000010*QR_002000000100+P_111000110*QR_002000000101+P_211000010*QR_002000000200+P_211000110*QR_002000000201); ans_temp[ans_id*6+3]+=Pmtrx[1]*(P_011000010*QR_001001000000+P_011000110*QR_001001000001+P_111000010*QR_001001000100+P_111000110*QR_001001000101+P_211000010*QR_001001000200+P_211000110*QR_001001000201); ans_temp[ans_id*6+3]+=Pmtrx[2]*(P_011000010*QR_000002000000+P_011000110*QR_000002000001+P_111000010*QR_000002000100+P_111000110*QR_000002000101+P_211000010*QR_000002000200+P_211000110*QR_000002000201); ans_temp[ans_id*6+3]+=Pmtrx[3]*(P_011000010*QR_001000001000+P_011000110*QR_001000001001+P_111000010*QR_001000001100+P_111000110*QR_001000001101+P_211000010*QR_001000001200+P_211000110*QR_001000001201); ans_temp[ans_id*6+3]+=Pmtrx[4]*(P_011000010*QR_000001001000+P_011000110*QR_000001001001+P_111000010*QR_000001001100+P_111000110*QR_000001001101+P_211000010*QR_000001001200+P_211000110*QR_000001001201); ans_temp[ans_id*6+3]+=Pmtrx[5]*(P_011000010*QR_000000002000+P_011000110*QR_000000002001+P_111000010*QR_000000002100+P_111000110*QR_000000002101+P_211000010*QR_000000002200+P_211000110*QR_000000002201); ans_temp[ans_id*6+3]+=Pmtrx[6]*(P_010001010*QR_002000000000+P_010001110*QR_002000000001+P_010101010*QR_002000000010+P_010101110*QR_002000000011+P_110001010*QR_002000000100+P_110001110*QR_002000000101+P_110101010*QR_002000000110+P_110101110*QR_002000000111); ans_temp[ans_id*6+3]+=Pmtrx[7]*(P_010001010*QR_001001000000+P_010001110*QR_001001000001+P_010101010*QR_001001000010+P_010101110*QR_001001000011+P_110001010*QR_001001000100+P_110001110*QR_001001000101+P_110101010*QR_001001000110+P_110101110*QR_001001000111); ans_temp[ans_id*6+3]+=Pmtrx[8]*(P_010001010*QR_000002000000+P_010001110*QR_000002000001+P_010101010*QR_000002000010+P_010101110*QR_000002000011+P_110001010*QR_000002000100+P_110001110*QR_000002000101+P_110101010*QR_000002000110+P_110101110*QR_000002000111); ans_temp[ans_id*6+3]+=Pmtrx[9]*(P_010001010*QR_001000001000+P_010001110*QR_001000001001+P_010101010*QR_001000001010+P_010101110*QR_001000001011+P_110001010*QR_001000001100+P_110001110*QR_001000001101+P_110101010*QR_001000001110+P_110101110*QR_001000001111); ans_temp[ans_id*6+3]+=Pmtrx[10]*(P_010001010*QR_000001001000+P_010001110*QR_000001001001+P_010101010*QR_000001001010+P_010101110*QR_000001001011+P_110001010*QR_000001001100+P_110001110*QR_000001001101+P_110101010*QR_000001001110+P_110101110*QR_000001001111); ans_temp[ans_id*6+3]+=Pmtrx[11]*(P_010001010*QR_000000002000+P_010001110*QR_000000002001+P_010101010*QR_000000002010+P_010101110*QR_000000002011+P_110001010*QR_000000002100+P_110001110*QR_000000002101+P_110101010*QR_000000002110+P_110101110*QR_000000002111); ans_temp[ans_id*6+3]+=Pmtrx[12]*(P_010000011*QR_002000000000+P_010000111*QR_002000000001+P_010000211*QR_002000000002+P_110000011*QR_002000000100+P_110000111*QR_002000000101+P_110000211*QR_002000000102); ans_temp[ans_id*6+3]+=Pmtrx[13]*(P_010000011*QR_001001000000+P_010000111*QR_001001000001+P_010000211*QR_001001000002+P_110000011*QR_001001000100+P_110000111*QR_001001000101+P_110000211*QR_001001000102); ans_temp[ans_id*6+3]+=Pmtrx[14]*(P_010000011*QR_000002000000+P_010000111*QR_000002000001+P_010000211*QR_000002000002+P_110000011*QR_000002000100+P_110000111*QR_000002000101+P_110000211*QR_000002000102); ans_temp[ans_id*6+3]+=Pmtrx[15]*(P_010000011*QR_001000001000+P_010000111*QR_001000001001+P_010000211*QR_001000001002+P_110000011*QR_001000001100+P_110000111*QR_001000001101+P_110000211*QR_001000001102); ans_temp[ans_id*6+3]+=Pmtrx[16]*(P_010000011*QR_000001001000+P_010000111*QR_000001001001+P_010000211*QR_000001001002+P_110000011*QR_000001001100+P_110000111*QR_000001001101+P_110000211*QR_000001001102); ans_temp[ans_id*6+3]+=Pmtrx[17]*(P_010000011*QR_000000002000+P_010000111*QR_000000002001+P_010000211*QR_000000002002+P_110000011*QR_000000002100+P_110000111*QR_000000002101+P_110000211*QR_000000002102); ans_temp[ans_id*6+4]+=Pmtrx[0]*(P_001010010*QR_002000000000+P_001010110*QR_002000000001+P_001110010*QR_002000000010+P_001110110*QR_002000000011+P_101010010*QR_002000000100+P_101010110*QR_002000000101+P_101110010*QR_002000000110+P_101110110*QR_002000000111); ans_temp[ans_id*6+4]+=Pmtrx[1]*(P_001010010*QR_001001000000+P_001010110*QR_001001000001+P_001110010*QR_001001000010+P_001110110*QR_001001000011+P_101010010*QR_001001000100+P_101010110*QR_001001000101+P_101110010*QR_001001000110+P_101110110*QR_001001000111); ans_temp[ans_id*6+4]+=Pmtrx[2]*(P_001010010*QR_000002000000+P_001010110*QR_000002000001+P_001110010*QR_000002000010+P_001110110*QR_000002000011+P_101010010*QR_000002000100+P_101010110*QR_000002000101+P_101110010*QR_000002000110+P_101110110*QR_000002000111); ans_temp[ans_id*6+4]+=Pmtrx[3]*(P_001010010*QR_001000001000+P_001010110*QR_001000001001+P_001110010*QR_001000001010+P_001110110*QR_001000001011+P_101010010*QR_001000001100+P_101010110*QR_001000001101+P_101110010*QR_001000001110+P_101110110*QR_001000001111); ans_temp[ans_id*6+4]+=Pmtrx[4]*(P_001010010*QR_000001001000+P_001010110*QR_000001001001+P_001110010*QR_000001001010+P_001110110*QR_000001001011+P_101010010*QR_000001001100+P_101010110*QR_000001001101+P_101110010*QR_000001001110+P_101110110*QR_000001001111); ans_temp[ans_id*6+4]+=Pmtrx[5]*(P_001010010*QR_000000002000+P_001010110*QR_000000002001+P_001110010*QR_000000002010+P_001110110*QR_000000002011+P_101010010*QR_000000002100+P_101010110*QR_000000002101+P_101110010*QR_000000002110+P_101110110*QR_000000002111); ans_temp[ans_id*6+4]+=Pmtrx[6]*(P_000011010*QR_002000000000+P_000011110*QR_002000000001+P_000111010*QR_002000000010+P_000111110*QR_002000000011+P_000211010*QR_002000000020+P_000211110*QR_002000000021); ans_temp[ans_id*6+4]+=Pmtrx[7]*(P_000011010*QR_001001000000+P_000011110*QR_001001000001+P_000111010*QR_001001000010+P_000111110*QR_001001000011+P_000211010*QR_001001000020+P_000211110*QR_001001000021); ans_temp[ans_id*6+4]+=Pmtrx[8]*(P_000011010*QR_000002000000+P_000011110*QR_000002000001+P_000111010*QR_000002000010+P_000111110*QR_000002000011+P_000211010*QR_000002000020+P_000211110*QR_000002000021); ans_temp[ans_id*6+4]+=Pmtrx[9]*(P_000011010*QR_001000001000+P_000011110*QR_001000001001+P_000111010*QR_001000001010+P_000111110*QR_001000001011+P_000211010*QR_001000001020+P_000211110*QR_001000001021); ans_temp[ans_id*6+4]+=Pmtrx[10]*(P_000011010*QR_000001001000+P_000011110*QR_000001001001+P_000111010*QR_000001001010+P_000111110*QR_000001001011+P_000211010*QR_000001001020+P_000211110*QR_000001001021); ans_temp[ans_id*6+4]+=Pmtrx[11]*(P_000011010*QR_000000002000+P_000011110*QR_000000002001+P_000111010*QR_000000002010+P_000111110*QR_000000002011+P_000211010*QR_000000002020+P_000211110*QR_000000002021); ans_temp[ans_id*6+4]+=Pmtrx[12]*(P_000010011*QR_002000000000+P_000010111*QR_002000000001+P_000010211*QR_002000000002+P_000110011*QR_002000000010+P_000110111*QR_002000000011+P_000110211*QR_002000000012); ans_temp[ans_id*6+4]+=Pmtrx[13]*(P_000010011*QR_001001000000+P_000010111*QR_001001000001+P_000010211*QR_001001000002+P_000110011*QR_001001000010+P_000110111*QR_001001000011+P_000110211*QR_001001000012); ans_temp[ans_id*6+4]+=Pmtrx[14]*(P_000010011*QR_000002000000+P_000010111*QR_000002000001+P_000010211*QR_000002000002+P_000110011*QR_000002000010+P_000110111*QR_000002000011+P_000110211*QR_000002000012); ans_temp[ans_id*6+4]+=Pmtrx[15]*(P_000010011*QR_001000001000+P_000010111*QR_001000001001+P_000010211*QR_001000001002+P_000110011*QR_001000001010+P_000110111*QR_001000001011+P_000110211*QR_001000001012); ans_temp[ans_id*6+4]+=Pmtrx[16]*(P_000010011*QR_000001001000+P_000010111*QR_000001001001+P_000010211*QR_000001001002+P_000110011*QR_000001001010+P_000110111*QR_000001001011+P_000110211*QR_000001001012); ans_temp[ans_id*6+4]+=Pmtrx[17]*(P_000010011*QR_000000002000+P_000010111*QR_000000002001+P_000010211*QR_000000002002+P_000110011*QR_000000002010+P_000110111*QR_000000002011+P_000110211*QR_000000002012); ans_temp[ans_id*6+5]+=Pmtrx[0]*(P_001000020*QR_002000000000+P_001000120*QR_002000000001+P_001000220*QR_002000000002+P_101000020*QR_002000000100+P_101000120*QR_002000000101+P_101000220*QR_002000000102); ans_temp[ans_id*6+5]+=Pmtrx[1]*(P_001000020*QR_001001000000+P_001000120*QR_001001000001+P_001000220*QR_001001000002+P_101000020*QR_001001000100+P_101000120*QR_001001000101+P_101000220*QR_001001000102); ans_temp[ans_id*6+5]+=Pmtrx[2]*(P_001000020*QR_000002000000+P_001000120*QR_000002000001+P_001000220*QR_000002000002+P_101000020*QR_000002000100+P_101000120*QR_000002000101+P_101000220*QR_000002000102); ans_temp[ans_id*6+5]+=Pmtrx[3]*(P_001000020*QR_001000001000+P_001000120*QR_001000001001+P_001000220*QR_001000001002+P_101000020*QR_001000001100+P_101000120*QR_001000001101+P_101000220*QR_001000001102); ans_temp[ans_id*6+5]+=Pmtrx[4]*(P_001000020*QR_000001001000+P_001000120*QR_000001001001+P_001000220*QR_000001001002+P_101000020*QR_000001001100+P_101000120*QR_000001001101+P_101000220*QR_000001001102); ans_temp[ans_id*6+5]+=Pmtrx[5]*(P_001000020*QR_000000002000+P_001000120*QR_000000002001+P_001000220*QR_000000002002+P_101000020*QR_000000002100+P_101000120*QR_000000002101+P_101000220*QR_000000002102); ans_temp[ans_id*6+5]+=Pmtrx[6]*(P_000001020*QR_002000000000+P_000001120*QR_002000000001+P_000001220*QR_002000000002+P_000101020*QR_002000000010+P_000101120*QR_002000000011+P_000101220*QR_002000000012); ans_temp[ans_id*6+5]+=Pmtrx[7]*(P_000001020*QR_001001000000+P_000001120*QR_001001000001+P_000001220*QR_001001000002+P_000101020*QR_001001000010+P_000101120*QR_001001000011+P_000101220*QR_001001000012); ans_temp[ans_id*6+5]+=Pmtrx[8]*(P_000001020*QR_000002000000+P_000001120*QR_000002000001+P_000001220*QR_000002000002+P_000101020*QR_000002000010+P_000101120*QR_000002000011+P_000101220*QR_000002000012); ans_temp[ans_id*6+5]+=Pmtrx[9]*(P_000001020*QR_001000001000+P_000001120*QR_001000001001+P_000001220*QR_001000001002+P_000101020*QR_001000001010+P_000101120*QR_001000001011+P_000101220*QR_001000001012); ans_temp[ans_id*6+5]+=Pmtrx[10]*(P_000001020*QR_000001001000+P_000001120*QR_000001001001+P_000001220*QR_000001001002+P_000101020*QR_000001001010+P_000101120*QR_000001001011+P_000101220*QR_000001001012); ans_temp[ans_id*6+5]+=Pmtrx[11]*(P_000001020*QR_000000002000+P_000001120*QR_000000002001+P_000001220*QR_000000002002+P_000101020*QR_000000002010+P_000101120*QR_000000002011+P_000101220*QR_000000002012); ans_temp[ans_id*6+5]+=Pmtrx[12]*(P_000000021*QR_002000000000+P_000000121*QR_002000000001+P_000000221*QR_002000000002+P_000000321*QR_002000000003); ans_temp[ans_id*6+5]+=Pmtrx[13]*(P_000000021*QR_001001000000+P_000000121*QR_001001000001+P_000000221*QR_001001000002+P_000000321*QR_001001000003); ans_temp[ans_id*6+5]+=Pmtrx[14]*(P_000000021*QR_000002000000+P_000000121*QR_000002000001+P_000000221*QR_000002000002+P_000000321*QR_000002000003); ans_temp[ans_id*6+5]+=Pmtrx[15]*(P_000000021*QR_001000001000+P_000000121*QR_001000001001+P_000000221*QR_001000001002+P_000000321*QR_001000001003); ans_temp[ans_id*6+5]+=Pmtrx[16]*(P_000000021*QR_000001001000+P_000000121*QR_000001001001+P_000000221*QR_000001001002+P_000000321*QR_000001001003); ans_temp[ans_id*6+5]+=Pmtrx[17]*(P_000000021*QR_000000002000+P_000000121*QR_000000002001+P_000000221*QR_000000002002+P_000000321*QR_000000002003); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } } __global__ void MD_Kp_ddsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_ket_start;ii<primit_ket_end;ii++){ unsigned int id_ket=id_ket_in[ii]; double QX=Q[ii*3+0]; double QY=Q[ii*3+1]; double QZ=Q[ii*3+2]; double Qd_001[3]; Qd_001[0]=QD[ii*3+0]; Qd_001[1]=QD[ii*3+1]; Qd_001[2]=QD[ii*3+2]; double Eta=Eta_in[ii]; double pq=pq_in[ii]; float K2_q=K2_q_in[ii]; double aQin1=1/(2*Eta); for(unsigned int j=tId_x;j<primit_bra_end-primit_bra_start;j+=tdis){ unsigned int jj=primit_bra_start+j; unsigned int id_bra=tex1Dfetch(tex_id_bra,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_p=tex1Dfetch(tex_K2_p,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Zta,jj); double Zta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pp,jj); double pp=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+0); double PX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+1); double PY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_P,jj*3+2); double PZ=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_010[3]; temp_int2=tex1Dfetch(tex_PA,jj*3+0); Pd_010[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+1); Pd_010[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PA,jj*3+2); Pd_010[2]=__hiloint2double(temp_int2.y,temp_int2.x); double Pd_001[3]; temp_int2=tex1Dfetch(tex_PB,jj*3+0); Pd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+1); Pd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_PB,jj*3+2); Pd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[7]; Ft_fs_6(6,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; R_000[6]*=64*alphaT*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aPin1=1/(2*Zta); double R_100[6]; double R_200[5]; double R_300[4]; double R_400[3]; double R_500[2]; double R_600[1]; double R_010[6]; double R_110[5]; double R_210[4]; double R_310[3]; double R_410[2]; double R_510[1]; double R_020[5]; double R_120[4]; double R_220[3]; double R_320[2]; double R_420[1]; double R_030[4]; double R_130[3]; double R_230[2]; double R_330[1]; double R_040[3]; double R_140[2]; double R_240[1]; double R_050[2]; double R_150[1]; double R_060[1]; double R_001[6]; double R_101[5]; double R_201[4]; double R_301[3]; double R_401[2]; double R_501[1]; double R_011[5]; double R_111[4]; double R_211[3]; double R_311[2]; double R_411[1]; double R_021[4]; double R_121[3]; double R_221[2]; double R_321[1]; double R_031[3]; double R_131[2]; double R_231[1]; double R_041[2]; double R_141[1]; double R_051[1]; double R_002[5]; double R_102[4]; double R_202[3]; double R_302[2]; double R_402[1]; double R_012[4]; double R_112[3]; double R_212[2]; double R_312[1]; double R_022[3]; double R_122[2]; double R_222[1]; double R_032[2]; double R_132[1]; double R_042[1]; double R_003[4]; double R_103[3]; double R_203[2]; double R_303[1]; double R_013[3]; double R_113[2]; double R_213[1]; double R_023[2]; double R_123[1]; double R_033[1]; double R_004[3]; double R_104[2]; double R_204[1]; double R_014[2]; double R_114[1]; double R_024[1]; double R_005[2]; double R_105[1]; double R_015[1]; double R_006[1]; for(int i=0;i<6;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<6;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<6;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<5;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<5;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<5;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<5;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<4;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<4;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<4;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<4;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<4;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<4;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<4;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<4;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<4;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<3;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<3;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<3;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<3;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<3;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<3;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<3;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<3;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<3;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<3;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<3;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<3;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<3;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<2;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<2;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<2;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<2;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<2;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<2;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<2;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<2;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<2;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<2;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<2;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<2;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<2;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<2;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<2;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<2;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<2;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<2;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<2;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<2;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<2;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } for(int i=0;i<1;i++){ R_600[i]=TX*R_500[i+1]+5*R_400[i+1]; } for(int i=0;i<1;i++){ R_510[i]=TY*R_500[i+1]; } for(int i=0;i<1;i++){ R_420[i]=TX*R_320[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_330[i]=TX*R_230[i+1]+2*R_130[i+1]; } for(int i=0;i<1;i++){ R_240[i]=TY*R_230[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_150[i]=TX*R_050[i+1]; } for(int i=0;i<1;i++){ R_060[i]=TY*R_050[i+1]+5*R_040[i+1]; } for(int i=0;i<1;i++){ R_501[i]=TZ*R_500[i+1]; } for(int i=0;i<1;i++){ R_411[i]=TY*R_401[i+1]; } for(int i=0;i<1;i++){ R_321[i]=TZ*R_320[i+1]; } for(int i=0;i<1;i++){ R_231[i]=TZ*R_230[i+1]; } for(int i=0;i<1;i++){ R_141[i]=TX*R_041[i+1]; } for(int i=0;i<1;i++){ R_051[i]=TZ*R_050[i+1]; } for(int i=0;i<1;i++){ R_402[i]=TX*R_302[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_312[i]=TY*R_302[i+1]; } for(int i=0;i<1;i++){ R_222[i]=TX*R_122[i+1]+R_022[i+1]; } for(int i=0;i<1;i++){ R_132[i]=TX*R_032[i+1]; } for(int i=0;i<1;i++){ R_042[i]=TY*R_032[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_303[i]=TX*R_203[i+1]+2*R_103[i+1]; } for(int i=0;i<1;i++){ R_213[i]=TY*R_203[i+1]; } for(int i=0;i<1;i++){ R_123[i]=TX*R_023[i+1]; } for(int i=0;i<1;i++){ R_033[i]=TY*R_023[i+1]+2*R_013[i+1]; } for(int i=0;i<1;i++){ R_204[i]=TZ*R_203[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_114[i]=TX*R_014[i+1]; } for(int i=0;i<1;i++){ R_024[i]=TZ*R_023[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_105[i]=TX*R_005[i+1]; } for(int i=0;i<1;i++){ R_015[i]=TY*R_005[i+1]; } for(int i=0;i<1;i++){ R_006[i]=TZ*R_005[i+1]+5*R_004[i+1]; } double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; double Pd_022[3]; double Pd_122[3]; double Pd_222[3]; double Pd_322[3]; double Pd_422[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_022[i]=Pd_112[i]+Pd_010[i]*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_122[i]=2*Pd_212[i]+Pd_010[i]*Pd_112[i]+aPin1*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_222[i]=3*Pd_312[i]+Pd_010[i]*Pd_212[i]+aPin1*Pd_112[i]; } for(int i=0;i<3;i++){ Pd_322[i]=Pd_010[i]*Pd_312[i]+aPin1*Pd_212[i]; } for(int i=0;i<3;i++){ Pd_422[i]=aPin1*Pd_312[i]; } double P_022000000=Pd_022[0]; double P_122000000=Pd_122[0]; double P_222000000=Pd_222[0]; double P_322000000=Pd_322[0]; double P_422000000=Pd_422[0]; double P_021001000=Pd_021[0]*Pd_001[1]; double P_021101000=Pd_021[0]*Pd_101[1]; double P_121001000=Pd_121[0]*Pd_001[1]; double P_121101000=Pd_121[0]*Pd_101[1]; double P_221001000=Pd_221[0]*Pd_001[1]; double P_221101000=Pd_221[0]*Pd_101[1]; double P_321001000=Pd_321[0]*Pd_001[1]; double P_321101000=Pd_321[0]*Pd_101[1]; double P_020002000=Pd_020[0]*Pd_002[1]; double P_020102000=Pd_020[0]*Pd_102[1]; double P_020202000=Pd_020[0]*Pd_202[1]; double P_120002000=Pd_120[0]*Pd_002[1]; double P_120102000=Pd_120[0]*Pd_102[1]; double P_120202000=Pd_120[0]*Pd_202[1]; double P_220002000=Pd_220[0]*Pd_002[1]; double P_220102000=Pd_220[0]*Pd_102[1]; double P_220202000=Pd_220[0]*Pd_202[1]; double P_021000001=Pd_021[0]*Pd_001[2]; double P_021000101=Pd_021[0]*Pd_101[2]; double P_121000001=Pd_121[0]*Pd_001[2]; double P_121000101=Pd_121[0]*Pd_101[2]; double P_221000001=Pd_221[0]*Pd_001[2]; double P_221000101=Pd_221[0]*Pd_101[2]; double P_321000001=Pd_321[0]*Pd_001[2]; double P_321000101=Pd_321[0]*Pd_101[2]; double P_020001001=Pd_020[0]*Pd_001[1]*Pd_001[2]; double P_020001101=Pd_020[0]*Pd_001[1]*Pd_101[2]; double P_020101001=Pd_020[0]*Pd_101[1]*Pd_001[2]; double P_020101101=Pd_020[0]*Pd_101[1]*Pd_101[2]; double P_120001001=Pd_120[0]*Pd_001[1]*Pd_001[2]; double P_120001101=Pd_120[0]*Pd_001[1]*Pd_101[2]; double P_120101001=Pd_120[0]*Pd_101[1]*Pd_001[2]; double P_120101101=Pd_120[0]*Pd_101[1]*Pd_101[2]; double P_220001001=Pd_220[0]*Pd_001[1]*Pd_001[2]; double P_220001101=Pd_220[0]*Pd_001[1]*Pd_101[2]; double P_220101001=Pd_220[0]*Pd_101[1]*Pd_001[2]; double P_220101101=Pd_220[0]*Pd_101[1]*Pd_101[2]; double P_020000002=Pd_020[0]*Pd_002[2]; double P_020000102=Pd_020[0]*Pd_102[2]; double P_020000202=Pd_020[0]*Pd_202[2]; double P_120000002=Pd_120[0]*Pd_002[2]; double P_120000102=Pd_120[0]*Pd_102[2]; double P_120000202=Pd_120[0]*Pd_202[2]; double P_220000002=Pd_220[0]*Pd_002[2]; double P_220000102=Pd_220[0]*Pd_102[2]; double P_220000202=Pd_220[0]*Pd_202[2]; double P_012010000=Pd_012[0]*Pd_010[1]; double P_012110000=Pd_012[0]*Pd_110[1]; double P_112010000=Pd_112[0]*Pd_010[1]; double P_112110000=Pd_112[0]*Pd_110[1]; double P_212010000=Pd_212[0]*Pd_010[1]; double P_212110000=Pd_212[0]*Pd_110[1]; double P_312010000=Pd_312[0]*Pd_010[1]; double P_312110000=Pd_312[0]*Pd_110[1]; double P_011011000=Pd_011[0]*Pd_011[1]; double P_011111000=Pd_011[0]*Pd_111[1]; double P_011211000=Pd_011[0]*Pd_211[1]; double P_111011000=Pd_111[0]*Pd_011[1]; double P_111111000=Pd_111[0]*Pd_111[1]; double P_111211000=Pd_111[0]*Pd_211[1]; double P_211011000=Pd_211[0]*Pd_011[1]; double P_211111000=Pd_211[0]*Pd_111[1]; double P_211211000=Pd_211[0]*Pd_211[1]; double P_010012000=Pd_010[0]*Pd_012[1]; double P_010112000=Pd_010[0]*Pd_112[1]; double P_010212000=Pd_010[0]*Pd_212[1]; double P_010312000=Pd_010[0]*Pd_312[1]; double P_110012000=Pd_110[0]*Pd_012[1]; double P_110112000=Pd_110[0]*Pd_112[1]; double P_110212000=Pd_110[0]*Pd_212[1]; double P_110312000=Pd_110[0]*Pd_312[1]; double P_011010001=Pd_011[0]*Pd_010[1]*Pd_001[2]; double P_011010101=Pd_011[0]*Pd_010[1]*Pd_101[2]; double P_011110001=Pd_011[0]*Pd_110[1]*Pd_001[2]; double P_011110101=Pd_011[0]*Pd_110[1]*Pd_101[2]; double P_111010001=Pd_111[0]*Pd_010[1]*Pd_001[2]; double P_111010101=Pd_111[0]*Pd_010[1]*Pd_101[2]; double P_111110001=Pd_111[0]*Pd_110[1]*Pd_001[2]; double P_111110101=Pd_111[0]*Pd_110[1]*Pd_101[2]; double P_211010001=Pd_211[0]*Pd_010[1]*Pd_001[2]; double P_211010101=Pd_211[0]*Pd_010[1]*Pd_101[2]; double P_211110001=Pd_211[0]*Pd_110[1]*Pd_001[2]; double P_211110101=Pd_211[0]*Pd_110[1]*Pd_101[2]; double P_010011001=Pd_010[0]*Pd_011[1]*Pd_001[2]; double P_010011101=Pd_010[0]*Pd_011[1]*Pd_101[2]; double P_010111001=Pd_010[0]*Pd_111[1]*Pd_001[2]; double P_010111101=Pd_010[0]*Pd_111[1]*Pd_101[2]; double P_010211001=Pd_010[0]*Pd_211[1]*Pd_001[2]; double P_010211101=Pd_010[0]*Pd_211[1]*Pd_101[2]; double P_110011001=Pd_110[0]*Pd_011[1]*Pd_001[2]; double P_110011101=Pd_110[0]*Pd_011[1]*Pd_101[2]; double P_110111001=Pd_110[0]*Pd_111[1]*Pd_001[2]; double P_110111101=Pd_110[0]*Pd_111[1]*Pd_101[2]; double P_110211001=Pd_110[0]*Pd_211[1]*Pd_001[2]; double P_110211101=Pd_110[0]*Pd_211[1]*Pd_101[2]; double P_010010002=Pd_010[0]*Pd_010[1]*Pd_002[2]; double P_010010102=Pd_010[0]*Pd_010[1]*Pd_102[2]; double P_010010202=Pd_010[0]*Pd_010[1]*Pd_202[2]; double P_010110002=Pd_010[0]*Pd_110[1]*Pd_002[2]; double P_010110102=Pd_010[0]*Pd_110[1]*Pd_102[2]; double P_010110202=Pd_010[0]*Pd_110[1]*Pd_202[2]; double P_110010002=Pd_110[0]*Pd_010[1]*Pd_002[2]; double P_110010102=Pd_110[0]*Pd_010[1]*Pd_102[2]; double P_110010202=Pd_110[0]*Pd_010[1]*Pd_202[2]; double P_110110002=Pd_110[0]*Pd_110[1]*Pd_002[2]; double P_110110102=Pd_110[0]*Pd_110[1]*Pd_102[2]; double P_110110202=Pd_110[0]*Pd_110[1]*Pd_202[2]; double P_002020000=Pd_002[0]*Pd_020[1]; double P_002120000=Pd_002[0]*Pd_120[1]; double P_002220000=Pd_002[0]*Pd_220[1]; double P_102020000=Pd_102[0]*Pd_020[1]; double P_102120000=Pd_102[0]*Pd_120[1]; double P_102220000=Pd_102[0]*Pd_220[1]; double P_202020000=Pd_202[0]*Pd_020[1]; double P_202120000=Pd_202[0]*Pd_120[1]; double P_202220000=Pd_202[0]*Pd_220[1]; double P_001021000=Pd_001[0]*Pd_021[1]; double P_001121000=Pd_001[0]*Pd_121[1]; double P_001221000=Pd_001[0]*Pd_221[1]; double P_001321000=Pd_001[0]*Pd_321[1]; double P_101021000=Pd_101[0]*Pd_021[1]; double P_101121000=Pd_101[0]*Pd_121[1]; double P_101221000=Pd_101[0]*Pd_221[1]; double P_101321000=Pd_101[0]*Pd_321[1]; double P_000022000=Pd_022[1]; double P_000122000=Pd_122[1]; double P_000222000=Pd_222[1]; double P_000322000=Pd_322[1]; double P_000422000=Pd_422[1]; double P_001020001=Pd_001[0]*Pd_020[1]*Pd_001[2]; double P_001020101=Pd_001[0]*Pd_020[1]*Pd_101[2]; double P_001120001=Pd_001[0]*Pd_120[1]*Pd_001[2]; double P_001120101=Pd_001[0]*Pd_120[1]*Pd_101[2]; double P_001220001=Pd_001[0]*Pd_220[1]*Pd_001[2]; double P_001220101=Pd_001[0]*Pd_220[1]*Pd_101[2]; double P_101020001=Pd_101[0]*Pd_020[1]*Pd_001[2]; double P_101020101=Pd_101[0]*Pd_020[1]*Pd_101[2]; double P_101120001=Pd_101[0]*Pd_120[1]*Pd_001[2]; double P_101120101=Pd_101[0]*Pd_120[1]*Pd_101[2]; double P_101220001=Pd_101[0]*Pd_220[1]*Pd_001[2]; double P_101220101=Pd_101[0]*Pd_220[1]*Pd_101[2]; double P_000021001=Pd_021[1]*Pd_001[2]; double P_000021101=Pd_021[1]*Pd_101[2]; double P_000121001=Pd_121[1]*Pd_001[2]; double P_000121101=Pd_121[1]*Pd_101[2]; double P_000221001=Pd_221[1]*Pd_001[2]; double P_000221101=Pd_221[1]*Pd_101[2]; double P_000321001=Pd_321[1]*Pd_001[2]; double P_000321101=Pd_321[1]*Pd_101[2]; double P_000020002=Pd_020[1]*Pd_002[2]; double P_000020102=Pd_020[1]*Pd_102[2]; double P_000020202=Pd_020[1]*Pd_202[2]; double P_000120002=Pd_120[1]*Pd_002[2]; double P_000120102=Pd_120[1]*Pd_102[2]; double P_000120202=Pd_120[1]*Pd_202[2]; double P_000220002=Pd_220[1]*Pd_002[2]; double P_000220102=Pd_220[1]*Pd_102[2]; double P_000220202=Pd_220[1]*Pd_202[2]; double P_012000010=Pd_012[0]*Pd_010[2]; double P_012000110=Pd_012[0]*Pd_110[2]; double P_112000010=Pd_112[0]*Pd_010[2]; double P_112000110=Pd_112[0]*Pd_110[2]; double P_212000010=Pd_212[0]*Pd_010[2]; double P_212000110=Pd_212[0]*Pd_110[2]; double P_312000010=Pd_312[0]*Pd_010[2]; double P_312000110=Pd_312[0]*Pd_110[2]; double P_011001010=Pd_011[0]*Pd_001[1]*Pd_010[2]; double P_011001110=Pd_011[0]*Pd_001[1]*Pd_110[2]; double P_011101010=Pd_011[0]*Pd_101[1]*Pd_010[2]; double P_011101110=Pd_011[0]*Pd_101[1]*Pd_110[2]; double P_111001010=Pd_111[0]*Pd_001[1]*Pd_010[2]; double P_111001110=Pd_111[0]*Pd_001[1]*Pd_110[2]; double P_111101010=Pd_111[0]*Pd_101[1]*Pd_010[2]; double P_111101110=Pd_111[0]*Pd_101[1]*Pd_110[2]; double P_211001010=Pd_211[0]*Pd_001[1]*Pd_010[2]; double P_211001110=Pd_211[0]*Pd_001[1]*Pd_110[2]; double P_211101010=Pd_211[0]*Pd_101[1]*Pd_010[2]; double P_211101110=Pd_211[0]*Pd_101[1]*Pd_110[2]; double P_010002010=Pd_010[0]*Pd_002[1]*Pd_010[2]; double P_010002110=Pd_010[0]*Pd_002[1]*Pd_110[2]; double P_010102010=Pd_010[0]*Pd_102[1]*Pd_010[2]; double P_010102110=Pd_010[0]*Pd_102[1]*Pd_110[2]; double P_010202010=Pd_010[0]*Pd_202[1]*Pd_010[2]; double P_010202110=Pd_010[0]*Pd_202[1]*Pd_110[2]; double P_110002010=Pd_110[0]*Pd_002[1]*Pd_010[2]; double P_110002110=Pd_110[0]*Pd_002[1]*Pd_110[2]; double P_110102010=Pd_110[0]*Pd_102[1]*Pd_010[2]; double P_110102110=Pd_110[0]*Pd_102[1]*Pd_110[2]; double P_110202010=Pd_110[0]*Pd_202[1]*Pd_010[2]; double P_110202110=Pd_110[0]*Pd_202[1]*Pd_110[2]; double P_011000011=Pd_011[0]*Pd_011[2]; double P_011000111=Pd_011[0]*Pd_111[2]; double P_011000211=Pd_011[0]*Pd_211[2]; double P_111000011=Pd_111[0]*Pd_011[2]; double P_111000111=Pd_111[0]*Pd_111[2]; double P_111000211=Pd_111[0]*Pd_211[2]; double P_211000011=Pd_211[0]*Pd_011[2]; double P_211000111=Pd_211[0]*Pd_111[2]; double P_211000211=Pd_211[0]*Pd_211[2]; double P_010001011=Pd_010[0]*Pd_001[1]*Pd_011[2]; double P_010001111=Pd_010[0]*Pd_001[1]*Pd_111[2]; double P_010001211=Pd_010[0]*Pd_001[1]*Pd_211[2]; double P_010101011=Pd_010[0]*Pd_101[1]*Pd_011[2]; double P_010101111=Pd_010[0]*Pd_101[1]*Pd_111[2]; double P_010101211=Pd_010[0]*Pd_101[1]*Pd_211[2]; double P_110001011=Pd_110[0]*Pd_001[1]*Pd_011[2]; double P_110001111=Pd_110[0]*Pd_001[1]*Pd_111[2]; double P_110001211=Pd_110[0]*Pd_001[1]*Pd_211[2]; double P_110101011=Pd_110[0]*Pd_101[1]*Pd_011[2]; double P_110101111=Pd_110[0]*Pd_101[1]*Pd_111[2]; double P_110101211=Pd_110[0]*Pd_101[1]*Pd_211[2]; double P_010000012=Pd_010[0]*Pd_012[2]; double P_010000112=Pd_010[0]*Pd_112[2]; double P_010000212=Pd_010[0]*Pd_212[2]; double P_010000312=Pd_010[0]*Pd_312[2]; double P_110000012=Pd_110[0]*Pd_012[2]; double P_110000112=Pd_110[0]*Pd_112[2]; double P_110000212=Pd_110[0]*Pd_212[2]; double P_110000312=Pd_110[0]*Pd_312[2]; double P_002010010=Pd_002[0]*Pd_010[1]*Pd_010[2]; double P_002010110=Pd_002[0]*Pd_010[1]*Pd_110[2]; double P_002110010=Pd_002[0]*Pd_110[1]*Pd_010[2]; double P_002110110=Pd_002[0]*Pd_110[1]*Pd_110[2]; double P_102010010=Pd_102[0]*Pd_010[1]*Pd_010[2]; double P_102010110=Pd_102[0]*Pd_010[1]*Pd_110[2]; double P_102110010=Pd_102[0]*Pd_110[1]*Pd_010[2]; double P_102110110=Pd_102[0]*Pd_110[1]*Pd_110[2]; double P_202010010=Pd_202[0]*Pd_010[1]*Pd_010[2]; double P_202010110=Pd_202[0]*Pd_010[1]*Pd_110[2]; double P_202110010=Pd_202[0]*Pd_110[1]*Pd_010[2]; double P_202110110=Pd_202[0]*Pd_110[1]*Pd_110[2]; double P_001011010=Pd_001[0]*Pd_011[1]*Pd_010[2]; double P_001011110=Pd_001[0]*Pd_011[1]*Pd_110[2]; double P_001111010=Pd_001[0]*Pd_111[1]*Pd_010[2]; double P_001111110=Pd_001[0]*Pd_111[1]*Pd_110[2]; double P_001211010=Pd_001[0]*Pd_211[1]*Pd_010[2]; double P_001211110=Pd_001[0]*Pd_211[1]*Pd_110[2]; double P_101011010=Pd_101[0]*Pd_011[1]*Pd_010[2]; double P_101011110=Pd_101[0]*Pd_011[1]*Pd_110[2]; double P_101111010=Pd_101[0]*Pd_111[1]*Pd_010[2]; double P_101111110=Pd_101[0]*Pd_111[1]*Pd_110[2]; double P_101211010=Pd_101[0]*Pd_211[1]*Pd_010[2]; double P_101211110=Pd_101[0]*Pd_211[1]*Pd_110[2]; double P_000012010=Pd_012[1]*Pd_010[2]; double P_000012110=Pd_012[1]*Pd_110[2]; double P_000112010=Pd_112[1]*Pd_010[2]; double P_000112110=Pd_112[1]*Pd_110[2]; double P_000212010=Pd_212[1]*Pd_010[2]; double P_000212110=Pd_212[1]*Pd_110[2]; double P_000312010=Pd_312[1]*Pd_010[2]; double P_000312110=Pd_312[1]*Pd_110[2]; double P_001010011=Pd_001[0]*Pd_010[1]*Pd_011[2]; double P_001010111=Pd_001[0]*Pd_010[1]*Pd_111[2]; double P_001010211=Pd_001[0]*Pd_010[1]*Pd_211[2]; double P_001110011=Pd_001[0]*Pd_110[1]*Pd_011[2]; double P_001110111=Pd_001[0]*Pd_110[1]*Pd_111[2]; double P_001110211=Pd_001[0]*Pd_110[1]*Pd_211[2]; double P_101010011=Pd_101[0]*Pd_010[1]*Pd_011[2]; double P_101010111=Pd_101[0]*Pd_010[1]*Pd_111[2]; double P_101010211=Pd_101[0]*Pd_010[1]*Pd_211[2]; double P_101110011=Pd_101[0]*Pd_110[1]*Pd_011[2]; double P_101110111=Pd_101[0]*Pd_110[1]*Pd_111[2]; double P_101110211=Pd_101[0]*Pd_110[1]*Pd_211[2]; double P_000011011=Pd_011[1]*Pd_011[2]; double P_000011111=Pd_011[1]*Pd_111[2]; double P_000011211=Pd_011[1]*Pd_211[2]; double P_000111011=Pd_111[1]*Pd_011[2]; double P_000111111=Pd_111[1]*Pd_111[2]; double P_000111211=Pd_111[1]*Pd_211[2]; double P_000211011=Pd_211[1]*Pd_011[2]; double P_000211111=Pd_211[1]*Pd_111[2]; double P_000211211=Pd_211[1]*Pd_211[2]; double P_000010012=Pd_010[1]*Pd_012[2]; double P_000010112=Pd_010[1]*Pd_112[2]; double P_000010212=Pd_010[1]*Pd_212[2]; double P_000010312=Pd_010[1]*Pd_312[2]; double P_000110012=Pd_110[1]*Pd_012[2]; double P_000110112=Pd_110[1]*Pd_112[2]; double P_000110212=Pd_110[1]*Pd_212[2]; double P_000110312=Pd_110[1]*Pd_312[2]; double P_002000020=Pd_002[0]*Pd_020[2]; double P_002000120=Pd_002[0]*Pd_120[2]; double P_002000220=Pd_002[0]*Pd_220[2]; double P_102000020=Pd_102[0]*Pd_020[2]; double P_102000120=Pd_102[0]*Pd_120[2]; double P_102000220=Pd_102[0]*Pd_220[2]; double P_202000020=Pd_202[0]*Pd_020[2]; double P_202000120=Pd_202[0]*Pd_120[2]; double P_202000220=Pd_202[0]*Pd_220[2]; double P_001001020=Pd_001[0]*Pd_001[1]*Pd_020[2]; double P_001001120=Pd_001[0]*Pd_001[1]*Pd_120[2]; double P_001001220=Pd_001[0]*Pd_001[1]*Pd_220[2]; double P_001101020=Pd_001[0]*Pd_101[1]*Pd_020[2]; double P_001101120=Pd_001[0]*Pd_101[1]*Pd_120[2]; double P_001101220=Pd_001[0]*Pd_101[1]*Pd_220[2]; double P_101001020=Pd_101[0]*Pd_001[1]*Pd_020[2]; double P_101001120=Pd_101[0]*Pd_001[1]*Pd_120[2]; double P_101001220=Pd_101[0]*Pd_001[1]*Pd_220[2]; double P_101101020=Pd_101[0]*Pd_101[1]*Pd_020[2]; double P_101101120=Pd_101[0]*Pd_101[1]*Pd_120[2]; double P_101101220=Pd_101[0]*Pd_101[1]*Pd_220[2]; double P_000002020=Pd_002[1]*Pd_020[2]; double P_000002120=Pd_002[1]*Pd_120[2]; double P_000002220=Pd_002[1]*Pd_220[2]; double P_000102020=Pd_102[1]*Pd_020[2]; double P_000102120=Pd_102[1]*Pd_120[2]; double P_000102220=Pd_102[1]*Pd_220[2]; double P_000202020=Pd_202[1]*Pd_020[2]; double P_000202120=Pd_202[1]*Pd_120[2]; double P_000202220=Pd_202[1]*Pd_220[2]; double P_001000021=Pd_001[0]*Pd_021[2]; double P_001000121=Pd_001[0]*Pd_121[2]; double P_001000221=Pd_001[0]*Pd_221[2]; double P_001000321=Pd_001[0]*Pd_321[2]; double P_101000021=Pd_101[0]*Pd_021[2]; double P_101000121=Pd_101[0]*Pd_121[2]; double P_101000221=Pd_101[0]*Pd_221[2]; double P_101000321=Pd_101[0]*Pd_321[2]; double P_000001021=Pd_001[1]*Pd_021[2]; double P_000001121=Pd_001[1]*Pd_121[2]; double P_000001221=Pd_001[1]*Pd_221[2]; double P_000001321=Pd_001[1]*Pd_321[2]; double P_000101021=Pd_101[1]*Pd_021[2]; double P_000101121=Pd_101[1]*Pd_121[2]; double P_000101221=Pd_101[1]*Pd_221[2]; double P_000101321=Pd_101[1]*Pd_321[2]; double P_000000022=Pd_022[2]; double P_000000122=Pd_122[2]; double P_000000222=Pd_222[2]; double P_000000322=Pd_322[2]; double P_000000422=Pd_422[2]; double PR_022000000000=P_022000000*R_000[0]+-1*P_122000000*R_100[0]+P_222000000*R_200[0]+-1*P_322000000*R_300[0]+P_422000000*R_400[0]; double PR_021001000000=P_021001000*R_000[0]+-1*P_021101000*R_010[0]+-1*P_121001000*R_100[0]+P_121101000*R_110[0]+P_221001000*R_200[0]+-1*P_221101000*R_210[0]+-1*P_321001000*R_300[0]+P_321101000*R_310[0]; double PR_020002000000=P_020002000*R_000[0]+-1*P_020102000*R_010[0]+P_020202000*R_020[0]+-1*P_120002000*R_100[0]+P_120102000*R_110[0]+-1*P_120202000*R_120[0]+P_220002000*R_200[0]+-1*P_220102000*R_210[0]+P_220202000*R_220[0]; double PR_021000001000=P_021000001*R_000[0]+-1*P_021000101*R_001[0]+-1*P_121000001*R_100[0]+P_121000101*R_101[0]+P_221000001*R_200[0]+-1*P_221000101*R_201[0]+-1*P_321000001*R_300[0]+P_321000101*R_301[0]; double PR_020001001000=P_020001001*R_000[0]+-1*P_020001101*R_001[0]+-1*P_020101001*R_010[0]+P_020101101*R_011[0]+-1*P_120001001*R_100[0]+P_120001101*R_101[0]+P_120101001*R_110[0]+-1*P_120101101*R_111[0]+P_220001001*R_200[0]+-1*P_220001101*R_201[0]+-1*P_220101001*R_210[0]+P_220101101*R_211[0]; double PR_020000002000=P_020000002*R_000[0]+-1*P_020000102*R_001[0]+P_020000202*R_002[0]+-1*P_120000002*R_100[0]+P_120000102*R_101[0]+-1*P_120000202*R_102[0]+P_220000002*R_200[0]+-1*P_220000102*R_201[0]+P_220000202*R_202[0]; double PR_012010000000=P_012010000*R_000[0]+-1*P_012110000*R_010[0]+-1*P_112010000*R_100[0]+P_112110000*R_110[0]+P_212010000*R_200[0]+-1*P_212110000*R_210[0]+-1*P_312010000*R_300[0]+P_312110000*R_310[0]; double PR_011011000000=P_011011000*R_000[0]+-1*P_011111000*R_010[0]+P_011211000*R_020[0]+-1*P_111011000*R_100[0]+P_111111000*R_110[0]+-1*P_111211000*R_120[0]+P_211011000*R_200[0]+-1*P_211111000*R_210[0]+P_211211000*R_220[0]; double PR_010012000000=P_010012000*R_000[0]+-1*P_010112000*R_010[0]+P_010212000*R_020[0]+-1*P_010312000*R_030[0]+-1*P_110012000*R_100[0]+P_110112000*R_110[0]+-1*P_110212000*R_120[0]+P_110312000*R_130[0]; double PR_011010001000=P_011010001*R_000[0]+-1*P_011010101*R_001[0]+-1*P_011110001*R_010[0]+P_011110101*R_011[0]+-1*P_111010001*R_100[0]+P_111010101*R_101[0]+P_111110001*R_110[0]+-1*P_111110101*R_111[0]+P_211010001*R_200[0]+-1*P_211010101*R_201[0]+-1*P_211110001*R_210[0]+P_211110101*R_211[0]; double PR_010011001000=P_010011001*R_000[0]+-1*P_010011101*R_001[0]+-1*P_010111001*R_010[0]+P_010111101*R_011[0]+P_010211001*R_020[0]+-1*P_010211101*R_021[0]+-1*P_110011001*R_100[0]+P_110011101*R_101[0]+P_110111001*R_110[0]+-1*P_110111101*R_111[0]+-1*P_110211001*R_120[0]+P_110211101*R_121[0]; double PR_010010002000=P_010010002*R_000[0]+-1*P_010010102*R_001[0]+P_010010202*R_002[0]+-1*P_010110002*R_010[0]+P_010110102*R_011[0]+-1*P_010110202*R_012[0]+-1*P_110010002*R_100[0]+P_110010102*R_101[0]+-1*P_110010202*R_102[0]+P_110110002*R_110[0]+-1*P_110110102*R_111[0]+P_110110202*R_112[0]; double PR_002020000000=P_002020000*R_000[0]+-1*P_002120000*R_010[0]+P_002220000*R_020[0]+-1*P_102020000*R_100[0]+P_102120000*R_110[0]+-1*P_102220000*R_120[0]+P_202020000*R_200[0]+-1*P_202120000*R_210[0]+P_202220000*R_220[0]; double PR_001021000000=P_001021000*R_000[0]+-1*P_001121000*R_010[0]+P_001221000*R_020[0]+-1*P_001321000*R_030[0]+-1*P_101021000*R_100[0]+P_101121000*R_110[0]+-1*P_101221000*R_120[0]+P_101321000*R_130[0]; double PR_000022000000=P_000022000*R_000[0]+-1*P_000122000*R_010[0]+P_000222000*R_020[0]+-1*P_000322000*R_030[0]+P_000422000*R_040[0]; double PR_001020001000=P_001020001*R_000[0]+-1*P_001020101*R_001[0]+-1*P_001120001*R_010[0]+P_001120101*R_011[0]+P_001220001*R_020[0]+-1*P_001220101*R_021[0]+-1*P_101020001*R_100[0]+P_101020101*R_101[0]+P_101120001*R_110[0]+-1*P_101120101*R_111[0]+-1*P_101220001*R_120[0]+P_101220101*R_121[0]; double PR_000021001000=P_000021001*R_000[0]+-1*P_000021101*R_001[0]+-1*P_000121001*R_010[0]+P_000121101*R_011[0]+P_000221001*R_020[0]+-1*P_000221101*R_021[0]+-1*P_000321001*R_030[0]+P_000321101*R_031[0]; double PR_000020002000=P_000020002*R_000[0]+-1*P_000020102*R_001[0]+P_000020202*R_002[0]+-1*P_000120002*R_010[0]+P_000120102*R_011[0]+-1*P_000120202*R_012[0]+P_000220002*R_020[0]+-1*P_000220102*R_021[0]+P_000220202*R_022[0]; double PR_012000010000=P_012000010*R_000[0]+-1*P_012000110*R_001[0]+-1*P_112000010*R_100[0]+P_112000110*R_101[0]+P_212000010*R_200[0]+-1*P_212000110*R_201[0]+-1*P_312000010*R_300[0]+P_312000110*R_301[0]; double PR_011001010000=P_011001010*R_000[0]+-1*P_011001110*R_001[0]+-1*P_011101010*R_010[0]+P_011101110*R_011[0]+-1*P_111001010*R_100[0]+P_111001110*R_101[0]+P_111101010*R_110[0]+-1*P_111101110*R_111[0]+P_211001010*R_200[0]+-1*P_211001110*R_201[0]+-1*P_211101010*R_210[0]+P_211101110*R_211[0]; double PR_010002010000=P_010002010*R_000[0]+-1*P_010002110*R_001[0]+-1*P_010102010*R_010[0]+P_010102110*R_011[0]+P_010202010*R_020[0]+-1*P_010202110*R_021[0]+-1*P_110002010*R_100[0]+P_110002110*R_101[0]+P_110102010*R_110[0]+-1*P_110102110*R_111[0]+-1*P_110202010*R_120[0]+P_110202110*R_121[0]; double PR_011000011000=P_011000011*R_000[0]+-1*P_011000111*R_001[0]+P_011000211*R_002[0]+-1*P_111000011*R_100[0]+P_111000111*R_101[0]+-1*P_111000211*R_102[0]+P_211000011*R_200[0]+-1*P_211000111*R_201[0]+P_211000211*R_202[0]; double PR_010001011000=P_010001011*R_000[0]+-1*P_010001111*R_001[0]+P_010001211*R_002[0]+-1*P_010101011*R_010[0]+P_010101111*R_011[0]+-1*P_010101211*R_012[0]+-1*P_110001011*R_100[0]+P_110001111*R_101[0]+-1*P_110001211*R_102[0]+P_110101011*R_110[0]+-1*P_110101111*R_111[0]+P_110101211*R_112[0]; double PR_010000012000=P_010000012*R_000[0]+-1*P_010000112*R_001[0]+P_010000212*R_002[0]+-1*P_010000312*R_003[0]+-1*P_110000012*R_100[0]+P_110000112*R_101[0]+-1*P_110000212*R_102[0]+P_110000312*R_103[0]; double PR_002010010000=P_002010010*R_000[0]+-1*P_002010110*R_001[0]+-1*P_002110010*R_010[0]+P_002110110*R_011[0]+-1*P_102010010*R_100[0]+P_102010110*R_101[0]+P_102110010*R_110[0]+-1*P_102110110*R_111[0]+P_202010010*R_200[0]+-1*P_202010110*R_201[0]+-1*P_202110010*R_210[0]+P_202110110*R_211[0]; double PR_001011010000=P_001011010*R_000[0]+-1*P_001011110*R_001[0]+-1*P_001111010*R_010[0]+P_001111110*R_011[0]+P_001211010*R_020[0]+-1*P_001211110*R_021[0]+-1*P_101011010*R_100[0]+P_101011110*R_101[0]+P_101111010*R_110[0]+-1*P_101111110*R_111[0]+-1*P_101211010*R_120[0]+P_101211110*R_121[0]; double PR_000012010000=P_000012010*R_000[0]+-1*P_000012110*R_001[0]+-1*P_000112010*R_010[0]+P_000112110*R_011[0]+P_000212010*R_020[0]+-1*P_000212110*R_021[0]+-1*P_000312010*R_030[0]+P_000312110*R_031[0]; double PR_001010011000=P_001010011*R_000[0]+-1*P_001010111*R_001[0]+P_001010211*R_002[0]+-1*P_001110011*R_010[0]+P_001110111*R_011[0]+-1*P_001110211*R_012[0]+-1*P_101010011*R_100[0]+P_101010111*R_101[0]+-1*P_101010211*R_102[0]+P_101110011*R_110[0]+-1*P_101110111*R_111[0]+P_101110211*R_112[0]; double PR_000011011000=P_000011011*R_000[0]+-1*P_000011111*R_001[0]+P_000011211*R_002[0]+-1*P_000111011*R_010[0]+P_000111111*R_011[0]+-1*P_000111211*R_012[0]+P_000211011*R_020[0]+-1*P_000211111*R_021[0]+P_000211211*R_022[0]; double PR_000010012000=P_000010012*R_000[0]+-1*P_000010112*R_001[0]+P_000010212*R_002[0]+-1*P_000010312*R_003[0]+-1*P_000110012*R_010[0]+P_000110112*R_011[0]+-1*P_000110212*R_012[0]+P_000110312*R_013[0]; double PR_002000020000=P_002000020*R_000[0]+-1*P_002000120*R_001[0]+P_002000220*R_002[0]+-1*P_102000020*R_100[0]+P_102000120*R_101[0]+-1*P_102000220*R_102[0]+P_202000020*R_200[0]+-1*P_202000120*R_201[0]+P_202000220*R_202[0]; double PR_001001020000=P_001001020*R_000[0]+-1*P_001001120*R_001[0]+P_001001220*R_002[0]+-1*P_001101020*R_010[0]+P_001101120*R_011[0]+-1*P_001101220*R_012[0]+-1*P_101001020*R_100[0]+P_101001120*R_101[0]+-1*P_101001220*R_102[0]+P_101101020*R_110[0]+-1*P_101101120*R_111[0]+P_101101220*R_112[0]; double PR_000002020000=P_000002020*R_000[0]+-1*P_000002120*R_001[0]+P_000002220*R_002[0]+-1*P_000102020*R_010[0]+P_000102120*R_011[0]+-1*P_000102220*R_012[0]+P_000202020*R_020[0]+-1*P_000202120*R_021[0]+P_000202220*R_022[0]; double PR_001000021000=P_001000021*R_000[0]+-1*P_001000121*R_001[0]+P_001000221*R_002[0]+-1*P_001000321*R_003[0]+-1*P_101000021*R_100[0]+P_101000121*R_101[0]+-1*P_101000221*R_102[0]+P_101000321*R_103[0]; double PR_000001021000=P_000001021*R_000[0]+-1*P_000001121*R_001[0]+P_000001221*R_002[0]+-1*P_000001321*R_003[0]+-1*P_000101021*R_010[0]+P_000101121*R_011[0]+-1*P_000101221*R_012[0]+P_000101321*R_013[0]; double PR_000000022000=P_000000022*R_000[0]+-1*P_000000122*R_001[0]+P_000000222*R_002[0]+-1*P_000000322*R_003[0]+P_000000422*R_004[0]; double PR_022000000001=P_022000000*R_001[0]+-1*P_122000000*R_101[0]+P_222000000*R_201[0]+-1*P_322000000*R_301[0]+P_422000000*R_401[0]; double PR_021001000001=P_021001000*R_001[0]+-1*P_021101000*R_011[0]+-1*P_121001000*R_101[0]+P_121101000*R_111[0]+P_221001000*R_201[0]+-1*P_221101000*R_211[0]+-1*P_321001000*R_301[0]+P_321101000*R_311[0]; double PR_020002000001=P_020002000*R_001[0]+-1*P_020102000*R_011[0]+P_020202000*R_021[0]+-1*P_120002000*R_101[0]+P_120102000*R_111[0]+-1*P_120202000*R_121[0]+P_220002000*R_201[0]+-1*P_220102000*R_211[0]+P_220202000*R_221[0]; double PR_021000001001=P_021000001*R_001[0]+-1*P_021000101*R_002[0]+-1*P_121000001*R_101[0]+P_121000101*R_102[0]+P_221000001*R_201[0]+-1*P_221000101*R_202[0]+-1*P_321000001*R_301[0]+P_321000101*R_302[0]; double PR_020001001001=P_020001001*R_001[0]+-1*P_020001101*R_002[0]+-1*P_020101001*R_011[0]+P_020101101*R_012[0]+-1*P_120001001*R_101[0]+P_120001101*R_102[0]+P_120101001*R_111[0]+-1*P_120101101*R_112[0]+P_220001001*R_201[0]+-1*P_220001101*R_202[0]+-1*P_220101001*R_211[0]+P_220101101*R_212[0]; double PR_020000002001=P_020000002*R_001[0]+-1*P_020000102*R_002[0]+P_020000202*R_003[0]+-1*P_120000002*R_101[0]+P_120000102*R_102[0]+-1*P_120000202*R_103[0]+P_220000002*R_201[0]+-1*P_220000102*R_202[0]+P_220000202*R_203[0]; double PR_012010000001=P_012010000*R_001[0]+-1*P_012110000*R_011[0]+-1*P_112010000*R_101[0]+P_112110000*R_111[0]+P_212010000*R_201[0]+-1*P_212110000*R_211[0]+-1*P_312010000*R_301[0]+P_312110000*R_311[0]; double PR_011011000001=P_011011000*R_001[0]+-1*P_011111000*R_011[0]+P_011211000*R_021[0]+-1*P_111011000*R_101[0]+P_111111000*R_111[0]+-1*P_111211000*R_121[0]+P_211011000*R_201[0]+-1*P_211111000*R_211[0]+P_211211000*R_221[0]; double PR_010012000001=P_010012000*R_001[0]+-1*P_010112000*R_011[0]+P_010212000*R_021[0]+-1*P_010312000*R_031[0]+-1*P_110012000*R_101[0]+P_110112000*R_111[0]+-1*P_110212000*R_121[0]+P_110312000*R_131[0]; double PR_011010001001=P_011010001*R_001[0]+-1*P_011010101*R_002[0]+-1*P_011110001*R_011[0]+P_011110101*R_012[0]+-1*P_111010001*R_101[0]+P_111010101*R_102[0]+P_111110001*R_111[0]+-1*P_111110101*R_112[0]+P_211010001*R_201[0]+-1*P_211010101*R_202[0]+-1*P_211110001*R_211[0]+P_211110101*R_212[0]; double PR_010011001001=P_010011001*R_001[0]+-1*P_010011101*R_002[0]+-1*P_010111001*R_011[0]+P_010111101*R_012[0]+P_010211001*R_021[0]+-1*P_010211101*R_022[0]+-1*P_110011001*R_101[0]+P_110011101*R_102[0]+P_110111001*R_111[0]+-1*P_110111101*R_112[0]+-1*P_110211001*R_121[0]+P_110211101*R_122[0]; double PR_010010002001=P_010010002*R_001[0]+-1*P_010010102*R_002[0]+P_010010202*R_003[0]+-1*P_010110002*R_011[0]+P_010110102*R_012[0]+-1*P_010110202*R_013[0]+-1*P_110010002*R_101[0]+P_110010102*R_102[0]+-1*P_110010202*R_103[0]+P_110110002*R_111[0]+-1*P_110110102*R_112[0]+P_110110202*R_113[0]; double PR_002020000001=P_002020000*R_001[0]+-1*P_002120000*R_011[0]+P_002220000*R_021[0]+-1*P_102020000*R_101[0]+P_102120000*R_111[0]+-1*P_102220000*R_121[0]+P_202020000*R_201[0]+-1*P_202120000*R_211[0]+P_202220000*R_221[0]; double PR_001021000001=P_001021000*R_001[0]+-1*P_001121000*R_011[0]+P_001221000*R_021[0]+-1*P_001321000*R_031[0]+-1*P_101021000*R_101[0]+P_101121000*R_111[0]+-1*P_101221000*R_121[0]+P_101321000*R_131[0]; double PR_000022000001=P_000022000*R_001[0]+-1*P_000122000*R_011[0]+P_000222000*R_021[0]+-1*P_000322000*R_031[0]+P_000422000*R_041[0]; double PR_001020001001=P_001020001*R_001[0]+-1*P_001020101*R_002[0]+-1*P_001120001*R_011[0]+P_001120101*R_012[0]+P_001220001*R_021[0]+-1*P_001220101*R_022[0]+-1*P_101020001*R_101[0]+P_101020101*R_102[0]+P_101120001*R_111[0]+-1*P_101120101*R_112[0]+-1*P_101220001*R_121[0]+P_101220101*R_122[0]; double PR_000021001001=P_000021001*R_001[0]+-1*P_000021101*R_002[0]+-1*P_000121001*R_011[0]+P_000121101*R_012[0]+P_000221001*R_021[0]+-1*P_000221101*R_022[0]+-1*P_000321001*R_031[0]+P_000321101*R_032[0]; double PR_000020002001=P_000020002*R_001[0]+-1*P_000020102*R_002[0]+P_000020202*R_003[0]+-1*P_000120002*R_011[0]+P_000120102*R_012[0]+-1*P_000120202*R_013[0]+P_000220002*R_021[0]+-1*P_000220102*R_022[0]+P_000220202*R_023[0]; double PR_012000010001=P_012000010*R_001[0]+-1*P_012000110*R_002[0]+-1*P_112000010*R_101[0]+P_112000110*R_102[0]+P_212000010*R_201[0]+-1*P_212000110*R_202[0]+-1*P_312000010*R_301[0]+P_312000110*R_302[0]; double PR_011001010001=P_011001010*R_001[0]+-1*P_011001110*R_002[0]+-1*P_011101010*R_011[0]+P_011101110*R_012[0]+-1*P_111001010*R_101[0]+P_111001110*R_102[0]+P_111101010*R_111[0]+-1*P_111101110*R_112[0]+P_211001010*R_201[0]+-1*P_211001110*R_202[0]+-1*P_211101010*R_211[0]+P_211101110*R_212[0]; double PR_010002010001=P_010002010*R_001[0]+-1*P_010002110*R_002[0]+-1*P_010102010*R_011[0]+P_010102110*R_012[0]+P_010202010*R_021[0]+-1*P_010202110*R_022[0]+-1*P_110002010*R_101[0]+P_110002110*R_102[0]+P_110102010*R_111[0]+-1*P_110102110*R_112[0]+-1*P_110202010*R_121[0]+P_110202110*R_122[0]; double PR_011000011001=P_011000011*R_001[0]+-1*P_011000111*R_002[0]+P_011000211*R_003[0]+-1*P_111000011*R_101[0]+P_111000111*R_102[0]+-1*P_111000211*R_103[0]+P_211000011*R_201[0]+-1*P_211000111*R_202[0]+P_211000211*R_203[0]; double PR_010001011001=P_010001011*R_001[0]+-1*P_010001111*R_002[0]+P_010001211*R_003[0]+-1*P_010101011*R_011[0]+P_010101111*R_012[0]+-1*P_010101211*R_013[0]+-1*P_110001011*R_101[0]+P_110001111*R_102[0]+-1*P_110001211*R_103[0]+P_110101011*R_111[0]+-1*P_110101111*R_112[0]+P_110101211*R_113[0]; double PR_010000012001=P_010000012*R_001[0]+-1*P_010000112*R_002[0]+P_010000212*R_003[0]+-1*P_010000312*R_004[0]+-1*P_110000012*R_101[0]+P_110000112*R_102[0]+-1*P_110000212*R_103[0]+P_110000312*R_104[0]; double PR_002010010001=P_002010010*R_001[0]+-1*P_002010110*R_002[0]+-1*P_002110010*R_011[0]+P_002110110*R_012[0]+-1*P_102010010*R_101[0]+P_102010110*R_102[0]+P_102110010*R_111[0]+-1*P_102110110*R_112[0]+P_202010010*R_201[0]+-1*P_202010110*R_202[0]+-1*P_202110010*R_211[0]+P_202110110*R_212[0]; double PR_001011010001=P_001011010*R_001[0]+-1*P_001011110*R_002[0]+-1*P_001111010*R_011[0]+P_001111110*R_012[0]+P_001211010*R_021[0]+-1*P_001211110*R_022[0]+-1*P_101011010*R_101[0]+P_101011110*R_102[0]+P_101111010*R_111[0]+-1*P_101111110*R_112[0]+-1*P_101211010*R_121[0]+P_101211110*R_122[0]; double PR_000012010001=P_000012010*R_001[0]+-1*P_000012110*R_002[0]+-1*P_000112010*R_011[0]+P_000112110*R_012[0]+P_000212010*R_021[0]+-1*P_000212110*R_022[0]+-1*P_000312010*R_031[0]+P_000312110*R_032[0]; double PR_001010011001=P_001010011*R_001[0]+-1*P_001010111*R_002[0]+P_001010211*R_003[0]+-1*P_001110011*R_011[0]+P_001110111*R_012[0]+-1*P_001110211*R_013[0]+-1*P_101010011*R_101[0]+P_101010111*R_102[0]+-1*P_101010211*R_103[0]+P_101110011*R_111[0]+-1*P_101110111*R_112[0]+P_101110211*R_113[0]; double PR_000011011001=P_000011011*R_001[0]+-1*P_000011111*R_002[0]+P_000011211*R_003[0]+-1*P_000111011*R_011[0]+P_000111111*R_012[0]+-1*P_000111211*R_013[0]+P_000211011*R_021[0]+-1*P_000211111*R_022[0]+P_000211211*R_023[0]; double PR_000010012001=P_000010012*R_001[0]+-1*P_000010112*R_002[0]+P_000010212*R_003[0]+-1*P_000010312*R_004[0]+-1*P_000110012*R_011[0]+P_000110112*R_012[0]+-1*P_000110212*R_013[0]+P_000110312*R_014[0]; double PR_002000020001=P_002000020*R_001[0]+-1*P_002000120*R_002[0]+P_002000220*R_003[0]+-1*P_102000020*R_101[0]+P_102000120*R_102[0]+-1*P_102000220*R_103[0]+P_202000020*R_201[0]+-1*P_202000120*R_202[0]+P_202000220*R_203[0]; double PR_001001020001=P_001001020*R_001[0]+-1*P_001001120*R_002[0]+P_001001220*R_003[0]+-1*P_001101020*R_011[0]+P_001101120*R_012[0]+-1*P_001101220*R_013[0]+-1*P_101001020*R_101[0]+P_101001120*R_102[0]+-1*P_101001220*R_103[0]+P_101101020*R_111[0]+-1*P_101101120*R_112[0]+P_101101220*R_113[0]; double PR_000002020001=P_000002020*R_001[0]+-1*P_000002120*R_002[0]+P_000002220*R_003[0]+-1*P_000102020*R_011[0]+P_000102120*R_012[0]+-1*P_000102220*R_013[0]+P_000202020*R_021[0]+-1*P_000202120*R_022[0]+P_000202220*R_023[0]; double PR_001000021001=P_001000021*R_001[0]+-1*P_001000121*R_002[0]+P_001000221*R_003[0]+-1*P_001000321*R_004[0]+-1*P_101000021*R_101[0]+P_101000121*R_102[0]+-1*P_101000221*R_103[0]+P_101000321*R_104[0]; double PR_000001021001=P_000001021*R_001[0]+-1*P_000001121*R_002[0]+P_000001221*R_003[0]+-1*P_000001321*R_004[0]+-1*P_000101021*R_011[0]+P_000101121*R_012[0]+-1*P_000101221*R_013[0]+P_000101321*R_014[0]; double PR_000000022001=P_000000022*R_001[0]+-1*P_000000122*R_002[0]+P_000000222*R_003[0]+-1*P_000000322*R_004[0]+P_000000422*R_005[0]; double PR_022000000010=P_022000000*R_010[0]+-1*P_122000000*R_110[0]+P_222000000*R_210[0]+-1*P_322000000*R_310[0]+P_422000000*R_410[0]; double PR_021001000010=P_021001000*R_010[0]+-1*P_021101000*R_020[0]+-1*P_121001000*R_110[0]+P_121101000*R_120[0]+P_221001000*R_210[0]+-1*P_221101000*R_220[0]+-1*P_321001000*R_310[0]+P_321101000*R_320[0]; double PR_020002000010=P_020002000*R_010[0]+-1*P_020102000*R_020[0]+P_020202000*R_030[0]+-1*P_120002000*R_110[0]+P_120102000*R_120[0]+-1*P_120202000*R_130[0]+P_220002000*R_210[0]+-1*P_220102000*R_220[0]+P_220202000*R_230[0]; double PR_021000001010=P_021000001*R_010[0]+-1*P_021000101*R_011[0]+-1*P_121000001*R_110[0]+P_121000101*R_111[0]+P_221000001*R_210[0]+-1*P_221000101*R_211[0]+-1*P_321000001*R_310[0]+P_321000101*R_311[0]; double PR_020001001010=P_020001001*R_010[0]+-1*P_020001101*R_011[0]+-1*P_020101001*R_020[0]+P_020101101*R_021[0]+-1*P_120001001*R_110[0]+P_120001101*R_111[0]+P_120101001*R_120[0]+-1*P_120101101*R_121[0]+P_220001001*R_210[0]+-1*P_220001101*R_211[0]+-1*P_220101001*R_220[0]+P_220101101*R_221[0]; double PR_020000002010=P_020000002*R_010[0]+-1*P_020000102*R_011[0]+P_020000202*R_012[0]+-1*P_120000002*R_110[0]+P_120000102*R_111[0]+-1*P_120000202*R_112[0]+P_220000002*R_210[0]+-1*P_220000102*R_211[0]+P_220000202*R_212[0]; double PR_012010000010=P_012010000*R_010[0]+-1*P_012110000*R_020[0]+-1*P_112010000*R_110[0]+P_112110000*R_120[0]+P_212010000*R_210[0]+-1*P_212110000*R_220[0]+-1*P_312010000*R_310[0]+P_312110000*R_320[0]; double PR_011011000010=P_011011000*R_010[0]+-1*P_011111000*R_020[0]+P_011211000*R_030[0]+-1*P_111011000*R_110[0]+P_111111000*R_120[0]+-1*P_111211000*R_130[0]+P_211011000*R_210[0]+-1*P_211111000*R_220[0]+P_211211000*R_230[0]; double PR_010012000010=P_010012000*R_010[0]+-1*P_010112000*R_020[0]+P_010212000*R_030[0]+-1*P_010312000*R_040[0]+-1*P_110012000*R_110[0]+P_110112000*R_120[0]+-1*P_110212000*R_130[0]+P_110312000*R_140[0]; double PR_011010001010=P_011010001*R_010[0]+-1*P_011010101*R_011[0]+-1*P_011110001*R_020[0]+P_011110101*R_021[0]+-1*P_111010001*R_110[0]+P_111010101*R_111[0]+P_111110001*R_120[0]+-1*P_111110101*R_121[0]+P_211010001*R_210[0]+-1*P_211010101*R_211[0]+-1*P_211110001*R_220[0]+P_211110101*R_221[0]; double PR_010011001010=P_010011001*R_010[0]+-1*P_010011101*R_011[0]+-1*P_010111001*R_020[0]+P_010111101*R_021[0]+P_010211001*R_030[0]+-1*P_010211101*R_031[0]+-1*P_110011001*R_110[0]+P_110011101*R_111[0]+P_110111001*R_120[0]+-1*P_110111101*R_121[0]+-1*P_110211001*R_130[0]+P_110211101*R_131[0]; double PR_010010002010=P_010010002*R_010[0]+-1*P_010010102*R_011[0]+P_010010202*R_012[0]+-1*P_010110002*R_020[0]+P_010110102*R_021[0]+-1*P_010110202*R_022[0]+-1*P_110010002*R_110[0]+P_110010102*R_111[0]+-1*P_110010202*R_112[0]+P_110110002*R_120[0]+-1*P_110110102*R_121[0]+P_110110202*R_122[0]; double PR_002020000010=P_002020000*R_010[0]+-1*P_002120000*R_020[0]+P_002220000*R_030[0]+-1*P_102020000*R_110[0]+P_102120000*R_120[0]+-1*P_102220000*R_130[0]+P_202020000*R_210[0]+-1*P_202120000*R_220[0]+P_202220000*R_230[0]; double PR_001021000010=P_001021000*R_010[0]+-1*P_001121000*R_020[0]+P_001221000*R_030[0]+-1*P_001321000*R_040[0]+-1*P_101021000*R_110[0]+P_101121000*R_120[0]+-1*P_101221000*R_130[0]+P_101321000*R_140[0]; double PR_000022000010=P_000022000*R_010[0]+-1*P_000122000*R_020[0]+P_000222000*R_030[0]+-1*P_000322000*R_040[0]+P_000422000*R_050[0]; double PR_001020001010=P_001020001*R_010[0]+-1*P_001020101*R_011[0]+-1*P_001120001*R_020[0]+P_001120101*R_021[0]+P_001220001*R_030[0]+-1*P_001220101*R_031[0]+-1*P_101020001*R_110[0]+P_101020101*R_111[0]+P_101120001*R_120[0]+-1*P_101120101*R_121[0]+-1*P_101220001*R_130[0]+P_101220101*R_131[0]; double PR_000021001010=P_000021001*R_010[0]+-1*P_000021101*R_011[0]+-1*P_000121001*R_020[0]+P_000121101*R_021[0]+P_000221001*R_030[0]+-1*P_000221101*R_031[0]+-1*P_000321001*R_040[0]+P_000321101*R_041[0]; double PR_000020002010=P_000020002*R_010[0]+-1*P_000020102*R_011[0]+P_000020202*R_012[0]+-1*P_000120002*R_020[0]+P_000120102*R_021[0]+-1*P_000120202*R_022[0]+P_000220002*R_030[0]+-1*P_000220102*R_031[0]+P_000220202*R_032[0]; double PR_012000010010=P_012000010*R_010[0]+-1*P_012000110*R_011[0]+-1*P_112000010*R_110[0]+P_112000110*R_111[0]+P_212000010*R_210[0]+-1*P_212000110*R_211[0]+-1*P_312000010*R_310[0]+P_312000110*R_311[0]; double PR_011001010010=P_011001010*R_010[0]+-1*P_011001110*R_011[0]+-1*P_011101010*R_020[0]+P_011101110*R_021[0]+-1*P_111001010*R_110[0]+P_111001110*R_111[0]+P_111101010*R_120[0]+-1*P_111101110*R_121[0]+P_211001010*R_210[0]+-1*P_211001110*R_211[0]+-1*P_211101010*R_220[0]+P_211101110*R_221[0]; double PR_010002010010=P_010002010*R_010[0]+-1*P_010002110*R_011[0]+-1*P_010102010*R_020[0]+P_010102110*R_021[0]+P_010202010*R_030[0]+-1*P_010202110*R_031[0]+-1*P_110002010*R_110[0]+P_110002110*R_111[0]+P_110102010*R_120[0]+-1*P_110102110*R_121[0]+-1*P_110202010*R_130[0]+P_110202110*R_131[0]; double PR_011000011010=P_011000011*R_010[0]+-1*P_011000111*R_011[0]+P_011000211*R_012[0]+-1*P_111000011*R_110[0]+P_111000111*R_111[0]+-1*P_111000211*R_112[0]+P_211000011*R_210[0]+-1*P_211000111*R_211[0]+P_211000211*R_212[0]; double PR_010001011010=P_010001011*R_010[0]+-1*P_010001111*R_011[0]+P_010001211*R_012[0]+-1*P_010101011*R_020[0]+P_010101111*R_021[0]+-1*P_010101211*R_022[0]+-1*P_110001011*R_110[0]+P_110001111*R_111[0]+-1*P_110001211*R_112[0]+P_110101011*R_120[0]+-1*P_110101111*R_121[0]+P_110101211*R_122[0]; double PR_010000012010=P_010000012*R_010[0]+-1*P_010000112*R_011[0]+P_010000212*R_012[0]+-1*P_010000312*R_013[0]+-1*P_110000012*R_110[0]+P_110000112*R_111[0]+-1*P_110000212*R_112[0]+P_110000312*R_113[0]; double PR_002010010010=P_002010010*R_010[0]+-1*P_002010110*R_011[0]+-1*P_002110010*R_020[0]+P_002110110*R_021[0]+-1*P_102010010*R_110[0]+P_102010110*R_111[0]+P_102110010*R_120[0]+-1*P_102110110*R_121[0]+P_202010010*R_210[0]+-1*P_202010110*R_211[0]+-1*P_202110010*R_220[0]+P_202110110*R_221[0]; double PR_001011010010=P_001011010*R_010[0]+-1*P_001011110*R_011[0]+-1*P_001111010*R_020[0]+P_001111110*R_021[0]+P_001211010*R_030[0]+-1*P_001211110*R_031[0]+-1*P_101011010*R_110[0]+P_101011110*R_111[0]+P_101111010*R_120[0]+-1*P_101111110*R_121[0]+-1*P_101211010*R_130[0]+P_101211110*R_131[0]; double PR_000012010010=P_000012010*R_010[0]+-1*P_000012110*R_011[0]+-1*P_000112010*R_020[0]+P_000112110*R_021[0]+P_000212010*R_030[0]+-1*P_000212110*R_031[0]+-1*P_000312010*R_040[0]+P_000312110*R_041[0]; double PR_001010011010=P_001010011*R_010[0]+-1*P_001010111*R_011[0]+P_001010211*R_012[0]+-1*P_001110011*R_020[0]+P_001110111*R_021[0]+-1*P_001110211*R_022[0]+-1*P_101010011*R_110[0]+P_101010111*R_111[0]+-1*P_101010211*R_112[0]+P_101110011*R_120[0]+-1*P_101110111*R_121[0]+P_101110211*R_122[0]; double PR_000011011010=P_000011011*R_010[0]+-1*P_000011111*R_011[0]+P_000011211*R_012[0]+-1*P_000111011*R_020[0]+P_000111111*R_021[0]+-1*P_000111211*R_022[0]+P_000211011*R_030[0]+-1*P_000211111*R_031[0]+P_000211211*R_032[0]; double PR_000010012010=P_000010012*R_010[0]+-1*P_000010112*R_011[0]+P_000010212*R_012[0]+-1*P_000010312*R_013[0]+-1*P_000110012*R_020[0]+P_000110112*R_021[0]+-1*P_000110212*R_022[0]+P_000110312*R_023[0]; double PR_002000020010=P_002000020*R_010[0]+-1*P_002000120*R_011[0]+P_002000220*R_012[0]+-1*P_102000020*R_110[0]+P_102000120*R_111[0]+-1*P_102000220*R_112[0]+P_202000020*R_210[0]+-1*P_202000120*R_211[0]+P_202000220*R_212[0]; double PR_001001020010=P_001001020*R_010[0]+-1*P_001001120*R_011[0]+P_001001220*R_012[0]+-1*P_001101020*R_020[0]+P_001101120*R_021[0]+-1*P_001101220*R_022[0]+-1*P_101001020*R_110[0]+P_101001120*R_111[0]+-1*P_101001220*R_112[0]+P_101101020*R_120[0]+-1*P_101101120*R_121[0]+P_101101220*R_122[0]; double PR_000002020010=P_000002020*R_010[0]+-1*P_000002120*R_011[0]+P_000002220*R_012[0]+-1*P_000102020*R_020[0]+P_000102120*R_021[0]+-1*P_000102220*R_022[0]+P_000202020*R_030[0]+-1*P_000202120*R_031[0]+P_000202220*R_032[0]; double PR_001000021010=P_001000021*R_010[0]+-1*P_001000121*R_011[0]+P_001000221*R_012[0]+-1*P_001000321*R_013[0]+-1*P_101000021*R_110[0]+P_101000121*R_111[0]+-1*P_101000221*R_112[0]+P_101000321*R_113[0]; double PR_000001021010=P_000001021*R_010[0]+-1*P_000001121*R_011[0]+P_000001221*R_012[0]+-1*P_000001321*R_013[0]+-1*P_000101021*R_020[0]+P_000101121*R_021[0]+-1*P_000101221*R_022[0]+P_000101321*R_023[0]; double PR_000000022010=P_000000022*R_010[0]+-1*P_000000122*R_011[0]+P_000000222*R_012[0]+-1*P_000000322*R_013[0]+P_000000422*R_014[0]; double PR_022000000100=P_022000000*R_100[0]+-1*P_122000000*R_200[0]+P_222000000*R_300[0]+-1*P_322000000*R_400[0]+P_422000000*R_500[0]; double PR_021001000100=P_021001000*R_100[0]+-1*P_021101000*R_110[0]+-1*P_121001000*R_200[0]+P_121101000*R_210[0]+P_221001000*R_300[0]+-1*P_221101000*R_310[0]+-1*P_321001000*R_400[0]+P_321101000*R_410[0]; double PR_020002000100=P_020002000*R_100[0]+-1*P_020102000*R_110[0]+P_020202000*R_120[0]+-1*P_120002000*R_200[0]+P_120102000*R_210[0]+-1*P_120202000*R_220[0]+P_220002000*R_300[0]+-1*P_220102000*R_310[0]+P_220202000*R_320[0]; double PR_021000001100=P_021000001*R_100[0]+-1*P_021000101*R_101[0]+-1*P_121000001*R_200[0]+P_121000101*R_201[0]+P_221000001*R_300[0]+-1*P_221000101*R_301[0]+-1*P_321000001*R_400[0]+P_321000101*R_401[0]; double PR_020001001100=P_020001001*R_100[0]+-1*P_020001101*R_101[0]+-1*P_020101001*R_110[0]+P_020101101*R_111[0]+-1*P_120001001*R_200[0]+P_120001101*R_201[0]+P_120101001*R_210[0]+-1*P_120101101*R_211[0]+P_220001001*R_300[0]+-1*P_220001101*R_301[0]+-1*P_220101001*R_310[0]+P_220101101*R_311[0]; double PR_020000002100=P_020000002*R_100[0]+-1*P_020000102*R_101[0]+P_020000202*R_102[0]+-1*P_120000002*R_200[0]+P_120000102*R_201[0]+-1*P_120000202*R_202[0]+P_220000002*R_300[0]+-1*P_220000102*R_301[0]+P_220000202*R_302[0]; double PR_012010000100=P_012010000*R_100[0]+-1*P_012110000*R_110[0]+-1*P_112010000*R_200[0]+P_112110000*R_210[0]+P_212010000*R_300[0]+-1*P_212110000*R_310[0]+-1*P_312010000*R_400[0]+P_312110000*R_410[0]; double PR_011011000100=P_011011000*R_100[0]+-1*P_011111000*R_110[0]+P_011211000*R_120[0]+-1*P_111011000*R_200[0]+P_111111000*R_210[0]+-1*P_111211000*R_220[0]+P_211011000*R_300[0]+-1*P_211111000*R_310[0]+P_211211000*R_320[0]; double PR_010012000100=P_010012000*R_100[0]+-1*P_010112000*R_110[0]+P_010212000*R_120[0]+-1*P_010312000*R_130[0]+-1*P_110012000*R_200[0]+P_110112000*R_210[0]+-1*P_110212000*R_220[0]+P_110312000*R_230[0]; double PR_011010001100=P_011010001*R_100[0]+-1*P_011010101*R_101[0]+-1*P_011110001*R_110[0]+P_011110101*R_111[0]+-1*P_111010001*R_200[0]+P_111010101*R_201[0]+P_111110001*R_210[0]+-1*P_111110101*R_211[0]+P_211010001*R_300[0]+-1*P_211010101*R_301[0]+-1*P_211110001*R_310[0]+P_211110101*R_311[0]; double PR_010011001100=P_010011001*R_100[0]+-1*P_010011101*R_101[0]+-1*P_010111001*R_110[0]+P_010111101*R_111[0]+P_010211001*R_120[0]+-1*P_010211101*R_121[0]+-1*P_110011001*R_200[0]+P_110011101*R_201[0]+P_110111001*R_210[0]+-1*P_110111101*R_211[0]+-1*P_110211001*R_220[0]+P_110211101*R_221[0]; double PR_010010002100=P_010010002*R_100[0]+-1*P_010010102*R_101[0]+P_010010202*R_102[0]+-1*P_010110002*R_110[0]+P_010110102*R_111[0]+-1*P_010110202*R_112[0]+-1*P_110010002*R_200[0]+P_110010102*R_201[0]+-1*P_110010202*R_202[0]+P_110110002*R_210[0]+-1*P_110110102*R_211[0]+P_110110202*R_212[0]; double PR_002020000100=P_002020000*R_100[0]+-1*P_002120000*R_110[0]+P_002220000*R_120[0]+-1*P_102020000*R_200[0]+P_102120000*R_210[0]+-1*P_102220000*R_220[0]+P_202020000*R_300[0]+-1*P_202120000*R_310[0]+P_202220000*R_320[0]; double PR_001021000100=P_001021000*R_100[0]+-1*P_001121000*R_110[0]+P_001221000*R_120[0]+-1*P_001321000*R_130[0]+-1*P_101021000*R_200[0]+P_101121000*R_210[0]+-1*P_101221000*R_220[0]+P_101321000*R_230[0]; double PR_000022000100=P_000022000*R_100[0]+-1*P_000122000*R_110[0]+P_000222000*R_120[0]+-1*P_000322000*R_130[0]+P_000422000*R_140[0]; double PR_001020001100=P_001020001*R_100[0]+-1*P_001020101*R_101[0]+-1*P_001120001*R_110[0]+P_001120101*R_111[0]+P_001220001*R_120[0]+-1*P_001220101*R_121[0]+-1*P_101020001*R_200[0]+P_101020101*R_201[0]+P_101120001*R_210[0]+-1*P_101120101*R_211[0]+-1*P_101220001*R_220[0]+P_101220101*R_221[0]; double PR_000021001100=P_000021001*R_100[0]+-1*P_000021101*R_101[0]+-1*P_000121001*R_110[0]+P_000121101*R_111[0]+P_000221001*R_120[0]+-1*P_000221101*R_121[0]+-1*P_000321001*R_130[0]+P_000321101*R_131[0]; double PR_000020002100=P_000020002*R_100[0]+-1*P_000020102*R_101[0]+P_000020202*R_102[0]+-1*P_000120002*R_110[0]+P_000120102*R_111[0]+-1*P_000120202*R_112[0]+P_000220002*R_120[0]+-1*P_000220102*R_121[0]+P_000220202*R_122[0]; double PR_012000010100=P_012000010*R_100[0]+-1*P_012000110*R_101[0]+-1*P_112000010*R_200[0]+P_112000110*R_201[0]+P_212000010*R_300[0]+-1*P_212000110*R_301[0]+-1*P_312000010*R_400[0]+P_312000110*R_401[0]; double PR_011001010100=P_011001010*R_100[0]+-1*P_011001110*R_101[0]+-1*P_011101010*R_110[0]+P_011101110*R_111[0]+-1*P_111001010*R_200[0]+P_111001110*R_201[0]+P_111101010*R_210[0]+-1*P_111101110*R_211[0]+P_211001010*R_300[0]+-1*P_211001110*R_301[0]+-1*P_211101010*R_310[0]+P_211101110*R_311[0]; double PR_010002010100=P_010002010*R_100[0]+-1*P_010002110*R_101[0]+-1*P_010102010*R_110[0]+P_010102110*R_111[0]+P_010202010*R_120[0]+-1*P_010202110*R_121[0]+-1*P_110002010*R_200[0]+P_110002110*R_201[0]+P_110102010*R_210[0]+-1*P_110102110*R_211[0]+-1*P_110202010*R_220[0]+P_110202110*R_221[0]; double PR_011000011100=P_011000011*R_100[0]+-1*P_011000111*R_101[0]+P_011000211*R_102[0]+-1*P_111000011*R_200[0]+P_111000111*R_201[0]+-1*P_111000211*R_202[0]+P_211000011*R_300[0]+-1*P_211000111*R_301[0]+P_211000211*R_302[0]; double PR_010001011100=P_010001011*R_100[0]+-1*P_010001111*R_101[0]+P_010001211*R_102[0]+-1*P_010101011*R_110[0]+P_010101111*R_111[0]+-1*P_010101211*R_112[0]+-1*P_110001011*R_200[0]+P_110001111*R_201[0]+-1*P_110001211*R_202[0]+P_110101011*R_210[0]+-1*P_110101111*R_211[0]+P_110101211*R_212[0]; double PR_010000012100=P_010000012*R_100[0]+-1*P_010000112*R_101[0]+P_010000212*R_102[0]+-1*P_010000312*R_103[0]+-1*P_110000012*R_200[0]+P_110000112*R_201[0]+-1*P_110000212*R_202[0]+P_110000312*R_203[0]; double PR_002010010100=P_002010010*R_100[0]+-1*P_002010110*R_101[0]+-1*P_002110010*R_110[0]+P_002110110*R_111[0]+-1*P_102010010*R_200[0]+P_102010110*R_201[0]+P_102110010*R_210[0]+-1*P_102110110*R_211[0]+P_202010010*R_300[0]+-1*P_202010110*R_301[0]+-1*P_202110010*R_310[0]+P_202110110*R_311[0]; double PR_001011010100=P_001011010*R_100[0]+-1*P_001011110*R_101[0]+-1*P_001111010*R_110[0]+P_001111110*R_111[0]+P_001211010*R_120[0]+-1*P_001211110*R_121[0]+-1*P_101011010*R_200[0]+P_101011110*R_201[0]+P_101111010*R_210[0]+-1*P_101111110*R_211[0]+-1*P_101211010*R_220[0]+P_101211110*R_221[0]; double PR_000012010100=P_000012010*R_100[0]+-1*P_000012110*R_101[0]+-1*P_000112010*R_110[0]+P_000112110*R_111[0]+P_000212010*R_120[0]+-1*P_000212110*R_121[0]+-1*P_000312010*R_130[0]+P_000312110*R_131[0]; double PR_001010011100=P_001010011*R_100[0]+-1*P_001010111*R_101[0]+P_001010211*R_102[0]+-1*P_001110011*R_110[0]+P_001110111*R_111[0]+-1*P_001110211*R_112[0]+-1*P_101010011*R_200[0]+P_101010111*R_201[0]+-1*P_101010211*R_202[0]+P_101110011*R_210[0]+-1*P_101110111*R_211[0]+P_101110211*R_212[0]; double PR_000011011100=P_000011011*R_100[0]+-1*P_000011111*R_101[0]+P_000011211*R_102[0]+-1*P_000111011*R_110[0]+P_000111111*R_111[0]+-1*P_000111211*R_112[0]+P_000211011*R_120[0]+-1*P_000211111*R_121[0]+P_000211211*R_122[0]; double PR_000010012100=P_000010012*R_100[0]+-1*P_000010112*R_101[0]+P_000010212*R_102[0]+-1*P_000010312*R_103[0]+-1*P_000110012*R_110[0]+P_000110112*R_111[0]+-1*P_000110212*R_112[0]+P_000110312*R_113[0]; double PR_002000020100=P_002000020*R_100[0]+-1*P_002000120*R_101[0]+P_002000220*R_102[0]+-1*P_102000020*R_200[0]+P_102000120*R_201[0]+-1*P_102000220*R_202[0]+P_202000020*R_300[0]+-1*P_202000120*R_301[0]+P_202000220*R_302[0]; double PR_001001020100=P_001001020*R_100[0]+-1*P_001001120*R_101[0]+P_001001220*R_102[0]+-1*P_001101020*R_110[0]+P_001101120*R_111[0]+-1*P_001101220*R_112[0]+-1*P_101001020*R_200[0]+P_101001120*R_201[0]+-1*P_101001220*R_202[0]+P_101101020*R_210[0]+-1*P_101101120*R_211[0]+P_101101220*R_212[0]; double PR_000002020100=P_000002020*R_100[0]+-1*P_000002120*R_101[0]+P_000002220*R_102[0]+-1*P_000102020*R_110[0]+P_000102120*R_111[0]+-1*P_000102220*R_112[0]+P_000202020*R_120[0]+-1*P_000202120*R_121[0]+P_000202220*R_122[0]; double PR_001000021100=P_001000021*R_100[0]+-1*P_001000121*R_101[0]+P_001000221*R_102[0]+-1*P_001000321*R_103[0]+-1*P_101000021*R_200[0]+P_101000121*R_201[0]+-1*P_101000221*R_202[0]+P_101000321*R_203[0]; double PR_000001021100=P_000001021*R_100[0]+-1*P_000001121*R_101[0]+P_000001221*R_102[0]+-1*P_000001321*R_103[0]+-1*P_000101021*R_110[0]+P_000101121*R_111[0]+-1*P_000101221*R_112[0]+P_000101321*R_113[0]; double PR_000000022100=P_000000022*R_100[0]+-1*P_000000122*R_101[0]+P_000000222*R_102[0]+-1*P_000000322*R_103[0]+P_000000422*R_104[0]; double PR_022000000002=P_022000000*R_002[0]+-1*P_122000000*R_102[0]+P_222000000*R_202[0]+-1*P_322000000*R_302[0]+P_422000000*R_402[0]; double PR_021001000002=P_021001000*R_002[0]+-1*P_021101000*R_012[0]+-1*P_121001000*R_102[0]+P_121101000*R_112[0]+P_221001000*R_202[0]+-1*P_221101000*R_212[0]+-1*P_321001000*R_302[0]+P_321101000*R_312[0]; double PR_020002000002=P_020002000*R_002[0]+-1*P_020102000*R_012[0]+P_020202000*R_022[0]+-1*P_120002000*R_102[0]+P_120102000*R_112[0]+-1*P_120202000*R_122[0]+P_220002000*R_202[0]+-1*P_220102000*R_212[0]+P_220202000*R_222[0]; double PR_021000001002=P_021000001*R_002[0]+-1*P_021000101*R_003[0]+-1*P_121000001*R_102[0]+P_121000101*R_103[0]+P_221000001*R_202[0]+-1*P_221000101*R_203[0]+-1*P_321000001*R_302[0]+P_321000101*R_303[0]; double PR_020001001002=P_020001001*R_002[0]+-1*P_020001101*R_003[0]+-1*P_020101001*R_012[0]+P_020101101*R_013[0]+-1*P_120001001*R_102[0]+P_120001101*R_103[0]+P_120101001*R_112[0]+-1*P_120101101*R_113[0]+P_220001001*R_202[0]+-1*P_220001101*R_203[0]+-1*P_220101001*R_212[0]+P_220101101*R_213[0]; double PR_020000002002=P_020000002*R_002[0]+-1*P_020000102*R_003[0]+P_020000202*R_004[0]+-1*P_120000002*R_102[0]+P_120000102*R_103[0]+-1*P_120000202*R_104[0]+P_220000002*R_202[0]+-1*P_220000102*R_203[0]+P_220000202*R_204[0]; double PR_012010000002=P_012010000*R_002[0]+-1*P_012110000*R_012[0]+-1*P_112010000*R_102[0]+P_112110000*R_112[0]+P_212010000*R_202[0]+-1*P_212110000*R_212[0]+-1*P_312010000*R_302[0]+P_312110000*R_312[0]; double PR_011011000002=P_011011000*R_002[0]+-1*P_011111000*R_012[0]+P_011211000*R_022[0]+-1*P_111011000*R_102[0]+P_111111000*R_112[0]+-1*P_111211000*R_122[0]+P_211011000*R_202[0]+-1*P_211111000*R_212[0]+P_211211000*R_222[0]; double PR_010012000002=P_010012000*R_002[0]+-1*P_010112000*R_012[0]+P_010212000*R_022[0]+-1*P_010312000*R_032[0]+-1*P_110012000*R_102[0]+P_110112000*R_112[0]+-1*P_110212000*R_122[0]+P_110312000*R_132[0]; double PR_011010001002=P_011010001*R_002[0]+-1*P_011010101*R_003[0]+-1*P_011110001*R_012[0]+P_011110101*R_013[0]+-1*P_111010001*R_102[0]+P_111010101*R_103[0]+P_111110001*R_112[0]+-1*P_111110101*R_113[0]+P_211010001*R_202[0]+-1*P_211010101*R_203[0]+-1*P_211110001*R_212[0]+P_211110101*R_213[0]; double PR_010011001002=P_010011001*R_002[0]+-1*P_010011101*R_003[0]+-1*P_010111001*R_012[0]+P_010111101*R_013[0]+P_010211001*R_022[0]+-1*P_010211101*R_023[0]+-1*P_110011001*R_102[0]+P_110011101*R_103[0]+P_110111001*R_112[0]+-1*P_110111101*R_113[0]+-1*P_110211001*R_122[0]+P_110211101*R_123[0]; double PR_010010002002=P_010010002*R_002[0]+-1*P_010010102*R_003[0]+P_010010202*R_004[0]+-1*P_010110002*R_012[0]+P_010110102*R_013[0]+-1*P_010110202*R_014[0]+-1*P_110010002*R_102[0]+P_110010102*R_103[0]+-1*P_110010202*R_104[0]+P_110110002*R_112[0]+-1*P_110110102*R_113[0]+P_110110202*R_114[0]; double PR_002020000002=P_002020000*R_002[0]+-1*P_002120000*R_012[0]+P_002220000*R_022[0]+-1*P_102020000*R_102[0]+P_102120000*R_112[0]+-1*P_102220000*R_122[0]+P_202020000*R_202[0]+-1*P_202120000*R_212[0]+P_202220000*R_222[0]; double PR_001021000002=P_001021000*R_002[0]+-1*P_001121000*R_012[0]+P_001221000*R_022[0]+-1*P_001321000*R_032[0]+-1*P_101021000*R_102[0]+P_101121000*R_112[0]+-1*P_101221000*R_122[0]+P_101321000*R_132[0]; double PR_000022000002=P_000022000*R_002[0]+-1*P_000122000*R_012[0]+P_000222000*R_022[0]+-1*P_000322000*R_032[0]+P_000422000*R_042[0]; double PR_001020001002=P_001020001*R_002[0]+-1*P_001020101*R_003[0]+-1*P_001120001*R_012[0]+P_001120101*R_013[0]+P_001220001*R_022[0]+-1*P_001220101*R_023[0]+-1*P_101020001*R_102[0]+P_101020101*R_103[0]+P_101120001*R_112[0]+-1*P_101120101*R_113[0]+-1*P_101220001*R_122[0]+P_101220101*R_123[0]; double PR_000021001002=P_000021001*R_002[0]+-1*P_000021101*R_003[0]+-1*P_000121001*R_012[0]+P_000121101*R_013[0]+P_000221001*R_022[0]+-1*P_000221101*R_023[0]+-1*P_000321001*R_032[0]+P_000321101*R_033[0]; double PR_000020002002=P_000020002*R_002[0]+-1*P_000020102*R_003[0]+P_000020202*R_004[0]+-1*P_000120002*R_012[0]+P_000120102*R_013[0]+-1*P_000120202*R_014[0]+P_000220002*R_022[0]+-1*P_000220102*R_023[0]+P_000220202*R_024[0]; double PR_012000010002=P_012000010*R_002[0]+-1*P_012000110*R_003[0]+-1*P_112000010*R_102[0]+P_112000110*R_103[0]+P_212000010*R_202[0]+-1*P_212000110*R_203[0]+-1*P_312000010*R_302[0]+P_312000110*R_303[0]; double PR_011001010002=P_011001010*R_002[0]+-1*P_011001110*R_003[0]+-1*P_011101010*R_012[0]+P_011101110*R_013[0]+-1*P_111001010*R_102[0]+P_111001110*R_103[0]+P_111101010*R_112[0]+-1*P_111101110*R_113[0]+P_211001010*R_202[0]+-1*P_211001110*R_203[0]+-1*P_211101010*R_212[0]+P_211101110*R_213[0]; double PR_010002010002=P_010002010*R_002[0]+-1*P_010002110*R_003[0]+-1*P_010102010*R_012[0]+P_010102110*R_013[0]+P_010202010*R_022[0]+-1*P_010202110*R_023[0]+-1*P_110002010*R_102[0]+P_110002110*R_103[0]+P_110102010*R_112[0]+-1*P_110102110*R_113[0]+-1*P_110202010*R_122[0]+P_110202110*R_123[0]; double PR_011000011002=P_011000011*R_002[0]+-1*P_011000111*R_003[0]+P_011000211*R_004[0]+-1*P_111000011*R_102[0]+P_111000111*R_103[0]+-1*P_111000211*R_104[0]+P_211000011*R_202[0]+-1*P_211000111*R_203[0]+P_211000211*R_204[0]; double PR_010001011002=P_010001011*R_002[0]+-1*P_010001111*R_003[0]+P_010001211*R_004[0]+-1*P_010101011*R_012[0]+P_010101111*R_013[0]+-1*P_010101211*R_014[0]+-1*P_110001011*R_102[0]+P_110001111*R_103[0]+-1*P_110001211*R_104[0]+P_110101011*R_112[0]+-1*P_110101111*R_113[0]+P_110101211*R_114[0]; double PR_010000012002=P_010000012*R_002[0]+-1*P_010000112*R_003[0]+P_010000212*R_004[0]+-1*P_010000312*R_005[0]+-1*P_110000012*R_102[0]+P_110000112*R_103[0]+-1*P_110000212*R_104[0]+P_110000312*R_105[0]; double PR_002010010002=P_002010010*R_002[0]+-1*P_002010110*R_003[0]+-1*P_002110010*R_012[0]+P_002110110*R_013[0]+-1*P_102010010*R_102[0]+P_102010110*R_103[0]+P_102110010*R_112[0]+-1*P_102110110*R_113[0]+P_202010010*R_202[0]+-1*P_202010110*R_203[0]+-1*P_202110010*R_212[0]+P_202110110*R_213[0]; double PR_001011010002=P_001011010*R_002[0]+-1*P_001011110*R_003[0]+-1*P_001111010*R_012[0]+P_001111110*R_013[0]+P_001211010*R_022[0]+-1*P_001211110*R_023[0]+-1*P_101011010*R_102[0]+P_101011110*R_103[0]+P_101111010*R_112[0]+-1*P_101111110*R_113[0]+-1*P_101211010*R_122[0]+P_101211110*R_123[0]; double PR_000012010002=P_000012010*R_002[0]+-1*P_000012110*R_003[0]+-1*P_000112010*R_012[0]+P_000112110*R_013[0]+P_000212010*R_022[0]+-1*P_000212110*R_023[0]+-1*P_000312010*R_032[0]+P_000312110*R_033[0]; double PR_001010011002=P_001010011*R_002[0]+-1*P_001010111*R_003[0]+P_001010211*R_004[0]+-1*P_001110011*R_012[0]+P_001110111*R_013[0]+-1*P_001110211*R_014[0]+-1*P_101010011*R_102[0]+P_101010111*R_103[0]+-1*P_101010211*R_104[0]+P_101110011*R_112[0]+-1*P_101110111*R_113[0]+P_101110211*R_114[0]; double PR_000011011002=P_000011011*R_002[0]+-1*P_000011111*R_003[0]+P_000011211*R_004[0]+-1*P_000111011*R_012[0]+P_000111111*R_013[0]+-1*P_000111211*R_014[0]+P_000211011*R_022[0]+-1*P_000211111*R_023[0]+P_000211211*R_024[0]; double PR_000010012002=P_000010012*R_002[0]+-1*P_000010112*R_003[0]+P_000010212*R_004[0]+-1*P_000010312*R_005[0]+-1*P_000110012*R_012[0]+P_000110112*R_013[0]+-1*P_000110212*R_014[0]+P_000110312*R_015[0]; double PR_002000020002=P_002000020*R_002[0]+-1*P_002000120*R_003[0]+P_002000220*R_004[0]+-1*P_102000020*R_102[0]+P_102000120*R_103[0]+-1*P_102000220*R_104[0]+P_202000020*R_202[0]+-1*P_202000120*R_203[0]+P_202000220*R_204[0]; double PR_001001020002=P_001001020*R_002[0]+-1*P_001001120*R_003[0]+P_001001220*R_004[0]+-1*P_001101020*R_012[0]+P_001101120*R_013[0]+-1*P_001101220*R_014[0]+-1*P_101001020*R_102[0]+P_101001120*R_103[0]+-1*P_101001220*R_104[0]+P_101101020*R_112[0]+-1*P_101101120*R_113[0]+P_101101220*R_114[0]; double PR_000002020002=P_000002020*R_002[0]+-1*P_000002120*R_003[0]+P_000002220*R_004[0]+-1*P_000102020*R_012[0]+P_000102120*R_013[0]+-1*P_000102220*R_014[0]+P_000202020*R_022[0]+-1*P_000202120*R_023[0]+P_000202220*R_024[0]; double PR_001000021002=P_001000021*R_002[0]+-1*P_001000121*R_003[0]+P_001000221*R_004[0]+-1*P_001000321*R_005[0]+-1*P_101000021*R_102[0]+P_101000121*R_103[0]+-1*P_101000221*R_104[0]+P_101000321*R_105[0]; double PR_000001021002=P_000001021*R_002[0]+-1*P_000001121*R_003[0]+P_000001221*R_004[0]+-1*P_000001321*R_005[0]+-1*P_000101021*R_012[0]+P_000101121*R_013[0]+-1*P_000101221*R_014[0]+P_000101321*R_015[0]; double PR_000000022002=P_000000022*R_002[0]+-1*P_000000122*R_003[0]+P_000000222*R_004[0]+-1*P_000000322*R_005[0]+P_000000422*R_006[0]; double PR_022000000011=P_022000000*R_011[0]+-1*P_122000000*R_111[0]+P_222000000*R_211[0]+-1*P_322000000*R_311[0]+P_422000000*R_411[0]; double PR_021001000011=P_021001000*R_011[0]+-1*P_021101000*R_021[0]+-1*P_121001000*R_111[0]+P_121101000*R_121[0]+P_221001000*R_211[0]+-1*P_221101000*R_221[0]+-1*P_321001000*R_311[0]+P_321101000*R_321[0]; double PR_020002000011=P_020002000*R_011[0]+-1*P_020102000*R_021[0]+P_020202000*R_031[0]+-1*P_120002000*R_111[0]+P_120102000*R_121[0]+-1*P_120202000*R_131[0]+P_220002000*R_211[0]+-1*P_220102000*R_221[0]+P_220202000*R_231[0]; double PR_021000001011=P_021000001*R_011[0]+-1*P_021000101*R_012[0]+-1*P_121000001*R_111[0]+P_121000101*R_112[0]+P_221000001*R_211[0]+-1*P_221000101*R_212[0]+-1*P_321000001*R_311[0]+P_321000101*R_312[0]; double PR_020001001011=P_020001001*R_011[0]+-1*P_020001101*R_012[0]+-1*P_020101001*R_021[0]+P_020101101*R_022[0]+-1*P_120001001*R_111[0]+P_120001101*R_112[0]+P_120101001*R_121[0]+-1*P_120101101*R_122[0]+P_220001001*R_211[0]+-1*P_220001101*R_212[0]+-1*P_220101001*R_221[0]+P_220101101*R_222[0]; double PR_020000002011=P_020000002*R_011[0]+-1*P_020000102*R_012[0]+P_020000202*R_013[0]+-1*P_120000002*R_111[0]+P_120000102*R_112[0]+-1*P_120000202*R_113[0]+P_220000002*R_211[0]+-1*P_220000102*R_212[0]+P_220000202*R_213[0]; double PR_012010000011=P_012010000*R_011[0]+-1*P_012110000*R_021[0]+-1*P_112010000*R_111[0]+P_112110000*R_121[0]+P_212010000*R_211[0]+-1*P_212110000*R_221[0]+-1*P_312010000*R_311[0]+P_312110000*R_321[0]; double PR_011011000011=P_011011000*R_011[0]+-1*P_011111000*R_021[0]+P_011211000*R_031[0]+-1*P_111011000*R_111[0]+P_111111000*R_121[0]+-1*P_111211000*R_131[0]+P_211011000*R_211[0]+-1*P_211111000*R_221[0]+P_211211000*R_231[0]; double PR_010012000011=P_010012000*R_011[0]+-1*P_010112000*R_021[0]+P_010212000*R_031[0]+-1*P_010312000*R_041[0]+-1*P_110012000*R_111[0]+P_110112000*R_121[0]+-1*P_110212000*R_131[0]+P_110312000*R_141[0]; double PR_011010001011=P_011010001*R_011[0]+-1*P_011010101*R_012[0]+-1*P_011110001*R_021[0]+P_011110101*R_022[0]+-1*P_111010001*R_111[0]+P_111010101*R_112[0]+P_111110001*R_121[0]+-1*P_111110101*R_122[0]+P_211010001*R_211[0]+-1*P_211010101*R_212[0]+-1*P_211110001*R_221[0]+P_211110101*R_222[0]; double PR_010011001011=P_010011001*R_011[0]+-1*P_010011101*R_012[0]+-1*P_010111001*R_021[0]+P_010111101*R_022[0]+P_010211001*R_031[0]+-1*P_010211101*R_032[0]+-1*P_110011001*R_111[0]+P_110011101*R_112[0]+P_110111001*R_121[0]+-1*P_110111101*R_122[0]+-1*P_110211001*R_131[0]+P_110211101*R_132[0]; double PR_010010002011=P_010010002*R_011[0]+-1*P_010010102*R_012[0]+P_010010202*R_013[0]+-1*P_010110002*R_021[0]+P_010110102*R_022[0]+-1*P_010110202*R_023[0]+-1*P_110010002*R_111[0]+P_110010102*R_112[0]+-1*P_110010202*R_113[0]+P_110110002*R_121[0]+-1*P_110110102*R_122[0]+P_110110202*R_123[0]; double PR_002020000011=P_002020000*R_011[0]+-1*P_002120000*R_021[0]+P_002220000*R_031[0]+-1*P_102020000*R_111[0]+P_102120000*R_121[0]+-1*P_102220000*R_131[0]+P_202020000*R_211[0]+-1*P_202120000*R_221[0]+P_202220000*R_231[0]; double PR_001021000011=P_001021000*R_011[0]+-1*P_001121000*R_021[0]+P_001221000*R_031[0]+-1*P_001321000*R_041[0]+-1*P_101021000*R_111[0]+P_101121000*R_121[0]+-1*P_101221000*R_131[0]+P_101321000*R_141[0]; double PR_000022000011=P_000022000*R_011[0]+-1*P_000122000*R_021[0]+P_000222000*R_031[0]+-1*P_000322000*R_041[0]+P_000422000*R_051[0]; double PR_001020001011=P_001020001*R_011[0]+-1*P_001020101*R_012[0]+-1*P_001120001*R_021[0]+P_001120101*R_022[0]+P_001220001*R_031[0]+-1*P_001220101*R_032[0]+-1*P_101020001*R_111[0]+P_101020101*R_112[0]+P_101120001*R_121[0]+-1*P_101120101*R_122[0]+-1*P_101220001*R_131[0]+P_101220101*R_132[0]; double PR_000021001011=P_000021001*R_011[0]+-1*P_000021101*R_012[0]+-1*P_000121001*R_021[0]+P_000121101*R_022[0]+P_000221001*R_031[0]+-1*P_000221101*R_032[0]+-1*P_000321001*R_041[0]+P_000321101*R_042[0]; double PR_000020002011=P_000020002*R_011[0]+-1*P_000020102*R_012[0]+P_000020202*R_013[0]+-1*P_000120002*R_021[0]+P_000120102*R_022[0]+-1*P_000120202*R_023[0]+P_000220002*R_031[0]+-1*P_000220102*R_032[0]+P_000220202*R_033[0]; double PR_012000010011=P_012000010*R_011[0]+-1*P_012000110*R_012[0]+-1*P_112000010*R_111[0]+P_112000110*R_112[0]+P_212000010*R_211[0]+-1*P_212000110*R_212[0]+-1*P_312000010*R_311[0]+P_312000110*R_312[0]; double PR_011001010011=P_011001010*R_011[0]+-1*P_011001110*R_012[0]+-1*P_011101010*R_021[0]+P_011101110*R_022[0]+-1*P_111001010*R_111[0]+P_111001110*R_112[0]+P_111101010*R_121[0]+-1*P_111101110*R_122[0]+P_211001010*R_211[0]+-1*P_211001110*R_212[0]+-1*P_211101010*R_221[0]+P_211101110*R_222[0]; double PR_010002010011=P_010002010*R_011[0]+-1*P_010002110*R_012[0]+-1*P_010102010*R_021[0]+P_010102110*R_022[0]+P_010202010*R_031[0]+-1*P_010202110*R_032[0]+-1*P_110002010*R_111[0]+P_110002110*R_112[0]+P_110102010*R_121[0]+-1*P_110102110*R_122[0]+-1*P_110202010*R_131[0]+P_110202110*R_132[0]; double PR_011000011011=P_011000011*R_011[0]+-1*P_011000111*R_012[0]+P_011000211*R_013[0]+-1*P_111000011*R_111[0]+P_111000111*R_112[0]+-1*P_111000211*R_113[0]+P_211000011*R_211[0]+-1*P_211000111*R_212[0]+P_211000211*R_213[0]; double PR_010001011011=P_010001011*R_011[0]+-1*P_010001111*R_012[0]+P_010001211*R_013[0]+-1*P_010101011*R_021[0]+P_010101111*R_022[0]+-1*P_010101211*R_023[0]+-1*P_110001011*R_111[0]+P_110001111*R_112[0]+-1*P_110001211*R_113[0]+P_110101011*R_121[0]+-1*P_110101111*R_122[0]+P_110101211*R_123[0]; double PR_010000012011=P_010000012*R_011[0]+-1*P_010000112*R_012[0]+P_010000212*R_013[0]+-1*P_010000312*R_014[0]+-1*P_110000012*R_111[0]+P_110000112*R_112[0]+-1*P_110000212*R_113[0]+P_110000312*R_114[0]; double PR_002010010011=P_002010010*R_011[0]+-1*P_002010110*R_012[0]+-1*P_002110010*R_021[0]+P_002110110*R_022[0]+-1*P_102010010*R_111[0]+P_102010110*R_112[0]+P_102110010*R_121[0]+-1*P_102110110*R_122[0]+P_202010010*R_211[0]+-1*P_202010110*R_212[0]+-1*P_202110010*R_221[0]+P_202110110*R_222[0]; double PR_001011010011=P_001011010*R_011[0]+-1*P_001011110*R_012[0]+-1*P_001111010*R_021[0]+P_001111110*R_022[0]+P_001211010*R_031[0]+-1*P_001211110*R_032[0]+-1*P_101011010*R_111[0]+P_101011110*R_112[0]+P_101111010*R_121[0]+-1*P_101111110*R_122[0]+-1*P_101211010*R_131[0]+P_101211110*R_132[0]; double PR_000012010011=P_000012010*R_011[0]+-1*P_000012110*R_012[0]+-1*P_000112010*R_021[0]+P_000112110*R_022[0]+P_000212010*R_031[0]+-1*P_000212110*R_032[0]+-1*P_000312010*R_041[0]+P_000312110*R_042[0]; double PR_001010011011=P_001010011*R_011[0]+-1*P_001010111*R_012[0]+P_001010211*R_013[0]+-1*P_001110011*R_021[0]+P_001110111*R_022[0]+-1*P_001110211*R_023[0]+-1*P_101010011*R_111[0]+P_101010111*R_112[0]+-1*P_101010211*R_113[0]+P_101110011*R_121[0]+-1*P_101110111*R_122[0]+P_101110211*R_123[0]; double PR_000011011011=P_000011011*R_011[0]+-1*P_000011111*R_012[0]+P_000011211*R_013[0]+-1*P_000111011*R_021[0]+P_000111111*R_022[0]+-1*P_000111211*R_023[0]+P_000211011*R_031[0]+-1*P_000211111*R_032[0]+P_000211211*R_033[0]; double PR_000010012011=P_000010012*R_011[0]+-1*P_000010112*R_012[0]+P_000010212*R_013[0]+-1*P_000010312*R_014[0]+-1*P_000110012*R_021[0]+P_000110112*R_022[0]+-1*P_000110212*R_023[0]+P_000110312*R_024[0]; double PR_002000020011=P_002000020*R_011[0]+-1*P_002000120*R_012[0]+P_002000220*R_013[0]+-1*P_102000020*R_111[0]+P_102000120*R_112[0]+-1*P_102000220*R_113[0]+P_202000020*R_211[0]+-1*P_202000120*R_212[0]+P_202000220*R_213[0]; double PR_001001020011=P_001001020*R_011[0]+-1*P_001001120*R_012[0]+P_001001220*R_013[0]+-1*P_001101020*R_021[0]+P_001101120*R_022[0]+-1*P_001101220*R_023[0]+-1*P_101001020*R_111[0]+P_101001120*R_112[0]+-1*P_101001220*R_113[0]+P_101101020*R_121[0]+-1*P_101101120*R_122[0]+P_101101220*R_123[0]; double PR_000002020011=P_000002020*R_011[0]+-1*P_000002120*R_012[0]+P_000002220*R_013[0]+-1*P_000102020*R_021[0]+P_000102120*R_022[0]+-1*P_000102220*R_023[0]+P_000202020*R_031[0]+-1*P_000202120*R_032[0]+P_000202220*R_033[0]; double PR_001000021011=P_001000021*R_011[0]+-1*P_001000121*R_012[0]+P_001000221*R_013[0]+-1*P_001000321*R_014[0]+-1*P_101000021*R_111[0]+P_101000121*R_112[0]+-1*P_101000221*R_113[0]+P_101000321*R_114[0]; double PR_000001021011=P_000001021*R_011[0]+-1*P_000001121*R_012[0]+P_000001221*R_013[0]+-1*P_000001321*R_014[0]+-1*P_000101021*R_021[0]+P_000101121*R_022[0]+-1*P_000101221*R_023[0]+P_000101321*R_024[0]; double PR_000000022011=P_000000022*R_011[0]+-1*P_000000122*R_012[0]+P_000000222*R_013[0]+-1*P_000000322*R_014[0]+P_000000422*R_015[0]; double PR_022000000020=P_022000000*R_020[0]+-1*P_122000000*R_120[0]+P_222000000*R_220[0]+-1*P_322000000*R_320[0]+P_422000000*R_420[0]; double PR_021001000020=P_021001000*R_020[0]+-1*P_021101000*R_030[0]+-1*P_121001000*R_120[0]+P_121101000*R_130[0]+P_221001000*R_220[0]+-1*P_221101000*R_230[0]+-1*P_321001000*R_320[0]+P_321101000*R_330[0]; double PR_020002000020=P_020002000*R_020[0]+-1*P_020102000*R_030[0]+P_020202000*R_040[0]+-1*P_120002000*R_120[0]+P_120102000*R_130[0]+-1*P_120202000*R_140[0]+P_220002000*R_220[0]+-1*P_220102000*R_230[0]+P_220202000*R_240[0]; double PR_021000001020=P_021000001*R_020[0]+-1*P_021000101*R_021[0]+-1*P_121000001*R_120[0]+P_121000101*R_121[0]+P_221000001*R_220[0]+-1*P_221000101*R_221[0]+-1*P_321000001*R_320[0]+P_321000101*R_321[0]; double PR_020001001020=P_020001001*R_020[0]+-1*P_020001101*R_021[0]+-1*P_020101001*R_030[0]+P_020101101*R_031[0]+-1*P_120001001*R_120[0]+P_120001101*R_121[0]+P_120101001*R_130[0]+-1*P_120101101*R_131[0]+P_220001001*R_220[0]+-1*P_220001101*R_221[0]+-1*P_220101001*R_230[0]+P_220101101*R_231[0]; double PR_020000002020=P_020000002*R_020[0]+-1*P_020000102*R_021[0]+P_020000202*R_022[0]+-1*P_120000002*R_120[0]+P_120000102*R_121[0]+-1*P_120000202*R_122[0]+P_220000002*R_220[0]+-1*P_220000102*R_221[0]+P_220000202*R_222[0]; double PR_012010000020=P_012010000*R_020[0]+-1*P_012110000*R_030[0]+-1*P_112010000*R_120[0]+P_112110000*R_130[0]+P_212010000*R_220[0]+-1*P_212110000*R_230[0]+-1*P_312010000*R_320[0]+P_312110000*R_330[0]; double PR_011011000020=P_011011000*R_020[0]+-1*P_011111000*R_030[0]+P_011211000*R_040[0]+-1*P_111011000*R_120[0]+P_111111000*R_130[0]+-1*P_111211000*R_140[0]+P_211011000*R_220[0]+-1*P_211111000*R_230[0]+P_211211000*R_240[0]; double PR_010012000020=P_010012000*R_020[0]+-1*P_010112000*R_030[0]+P_010212000*R_040[0]+-1*P_010312000*R_050[0]+-1*P_110012000*R_120[0]+P_110112000*R_130[0]+-1*P_110212000*R_140[0]+P_110312000*R_150[0]; double PR_011010001020=P_011010001*R_020[0]+-1*P_011010101*R_021[0]+-1*P_011110001*R_030[0]+P_011110101*R_031[0]+-1*P_111010001*R_120[0]+P_111010101*R_121[0]+P_111110001*R_130[0]+-1*P_111110101*R_131[0]+P_211010001*R_220[0]+-1*P_211010101*R_221[0]+-1*P_211110001*R_230[0]+P_211110101*R_231[0]; double PR_010011001020=P_010011001*R_020[0]+-1*P_010011101*R_021[0]+-1*P_010111001*R_030[0]+P_010111101*R_031[0]+P_010211001*R_040[0]+-1*P_010211101*R_041[0]+-1*P_110011001*R_120[0]+P_110011101*R_121[0]+P_110111001*R_130[0]+-1*P_110111101*R_131[0]+-1*P_110211001*R_140[0]+P_110211101*R_141[0]; double PR_010010002020=P_010010002*R_020[0]+-1*P_010010102*R_021[0]+P_010010202*R_022[0]+-1*P_010110002*R_030[0]+P_010110102*R_031[0]+-1*P_010110202*R_032[0]+-1*P_110010002*R_120[0]+P_110010102*R_121[0]+-1*P_110010202*R_122[0]+P_110110002*R_130[0]+-1*P_110110102*R_131[0]+P_110110202*R_132[0]; double PR_002020000020=P_002020000*R_020[0]+-1*P_002120000*R_030[0]+P_002220000*R_040[0]+-1*P_102020000*R_120[0]+P_102120000*R_130[0]+-1*P_102220000*R_140[0]+P_202020000*R_220[0]+-1*P_202120000*R_230[0]+P_202220000*R_240[0]; double PR_001021000020=P_001021000*R_020[0]+-1*P_001121000*R_030[0]+P_001221000*R_040[0]+-1*P_001321000*R_050[0]+-1*P_101021000*R_120[0]+P_101121000*R_130[0]+-1*P_101221000*R_140[0]+P_101321000*R_150[0]; double PR_000022000020=P_000022000*R_020[0]+-1*P_000122000*R_030[0]+P_000222000*R_040[0]+-1*P_000322000*R_050[0]+P_000422000*R_060[0]; double PR_001020001020=P_001020001*R_020[0]+-1*P_001020101*R_021[0]+-1*P_001120001*R_030[0]+P_001120101*R_031[0]+P_001220001*R_040[0]+-1*P_001220101*R_041[0]+-1*P_101020001*R_120[0]+P_101020101*R_121[0]+P_101120001*R_130[0]+-1*P_101120101*R_131[0]+-1*P_101220001*R_140[0]+P_101220101*R_141[0]; double PR_000021001020=P_000021001*R_020[0]+-1*P_000021101*R_021[0]+-1*P_000121001*R_030[0]+P_000121101*R_031[0]+P_000221001*R_040[0]+-1*P_000221101*R_041[0]+-1*P_000321001*R_050[0]+P_000321101*R_051[0]; double PR_000020002020=P_000020002*R_020[0]+-1*P_000020102*R_021[0]+P_000020202*R_022[0]+-1*P_000120002*R_030[0]+P_000120102*R_031[0]+-1*P_000120202*R_032[0]+P_000220002*R_040[0]+-1*P_000220102*R_041[0]+P_000220202*R_042[0]; double PR_012000010020=P_012000010*R_020[0]+-1*P_012000110*R_021[0]+-1*P_112000010*R_120[0]+P_112000110*R_121[0]+P_212000010*R_220[0]+-1*P_212000110*R_221[0]+-1*P_312000010*R_320[0]+P_312000110*R_321[0]; double PR_011001010020=P_011001010*R_020[0]+-1*P_011001110*R_021[0]+-1*P_011101010*R_030[0]+P_011101110*R_031[0]+-1*P_111001010*R_120[0]+P_111001110*R_121[0]+P_111101010*R_130[0]+-1*P_111101110*R_131[0]+P_211001010*R_220[0]+-1*P_211001110*R_221[0]+-1*P_211101010*R_230[0]+P_211101110*R_231[0]; double PR_010002010020=P_010002010*R_020[0]+-1*P_010002110*R_021[0]+-1*P_010102010*R_030[0]+P_010102110*R_031[0]+P_010202010*R_040[0]+-1*P_010202110*R_041[0]+-1*P_110002010*R_120[0]+P_110002110*R_121[0]+P_110102010*R_130[0]+-1*P_110102110*R_131[0]+-1*P_110202010*R_140[0]+P_110202110*R_141[0]; double PR_011000011020=P_011000011*R_020[0]+-1*P_011000111*R_021[0]+P_011000211*R_022[0]+-1*P_111000011*R_120[0]+P_111000111*R_121[0]+-1*P_111000211*R_122[0]+P_211000011*R_220[0]+-1*P_211000111*R_221[0]+P_211000211*R_222[0]; double PR_010001011020=P_010001011*R_020[0]+-1*P_010001111*R_021[0]+P_010001211*R_022[0]+-1*P_010101011*R_030[0]+P_010101111*R_031[0]+-1*P_010101211*R_032[0]+-1*P_110001011*R_120[0]+P_110001111*R_121[0]+-1*P_110001211*R_122[0]+P_110101011*R_130[0]+-1*P_110101111*R_131[0]+P_110101211*R_132[0]; double PR_010000012020=P_010000012*R_020[0]+-1*P_010000112*R_021[0]+P_010000212*R_022[0]+-1*P_010000312*R_023[0]+-1*P_110000012*R_120[0]+P_110000112*R_121[0]+-1*P_110000212*R_122[0]+P_110000312*R_123[0]; double PR_002010010020=P_002010010*R_020[0]+-1*P_002010110*R_021[0]+-1*P_002110010*R_030[0]+P_002110110*R_031[0]+-1*P_102010010*R_120[0]+P_102010110*R_121[0]+P_102110010*R_130[0]+-1*P_102110110*R_131[0]+P_202010010*R_220[0]+-1*P_202010110*R_221[0]+-1*P_202110010*R_230[0]+P_202110110*R_231[0]; double PR_001011010020=P_001011010*R_020[0]+-1*P_001011110*R_021[0]+-1*P_001111010*R_030[0]+P_001111110*R_031[0]+P_001211010*R_040[0]+-1*P_001211110*R_041[0]+-1*P_101011010*R_120[0]+P_101011110*R_121[0]+P_101111010*R_130[0]+-1*P_101111110*R_131[0]+-1*P_101211010*R_140[0]+P_101211110*R_141[0]; double PR_000012010020=P_000012010*R_020[0]+-1*P_000012110*R_021[0]+-1*P_000112010*R_030[0]+P_000112110*R_031[0]+P_000212010*R_040[0]+-1*P_000212110*R_041[0]+-1*P_000312010*R_050[0]+P_000312110*R_051[0]; double PR_001010011020=P_001010011*R_020[0]+-1*P_001010111*R_021[0]+P_001010211*R_022[0]+-1*P_001110011*R_030[0]+P_001110111*R_031[0]+-1*P_001110211*R_032[0]+-1*P_101010011*R_120[0]+P_101010111*R_121[0]+-1*P_101010211*R_122[0]+P_101110011*R_130[0]+-1*P_101110111*R_131[0]+P_101110211*R_132[0]; double PR_000011011020=P_000011011*R_020[0]+-1*P_000011111*R_021[0]+P_000011211*R_022[0]+-1*P_000111011*R_030[0]+P_000111111*R_031[0]+-1*P_000111211*R_032[0]+P_000211011*R_040[0]+-1*P_000211111*R_041[0]+P_000211211*R_042[0]; double PR_000010012020=P_000010012*R_020[0]+-1*P_000010112*R_021[0]+P_000010212*R_022[0]+-1*P_000010312*R_023[0]+-1*P_000110012*R_030[0]+P_000110112*R_031[0]+-1*P_000110212*R_032[0]+P_000110312*R_033[0]; double PR_002000020020=P_002000020*R_020[0]+-1*P_002000120*R_021[0]+P_002000220*R_022[0]+-1*P_102000020*R_120[0]+P_102000120*R_121[0]+-1*P_102000220*R_122[0]+P_202000020*R_220[0]+-1*P_202000120*R_221[0]+P_202000220*R_222[0]; double PR_001001020020=P_001001020*R_020[0]+-1*P_001001120*R_021[0]+P_001001220*R_022[0]+-1*P_001101020*R_030[0]+P_001101120*R_031[0]+-1*P_001101220*R_032[0]+-1*P_101001020*R_120[0]+P_101001120*R_121[0]+-1*P_101001220*R_122[0]+P_101101020*R_130[0]+-1*P_101101120*R_131[0]+P_101101220*R_132[0]; double PR_000002020020=P_000002020*R_020[0]+-1*P_000002120*R_021[0]+P_000002220*R_022[0]+-1*P_000102020*R_030[0]+P_000102120*R_031[0]+-1*P_000102220*R_032[0]+P_000202020*R_040[0]+-1*P_000202120*R_041[0]+P_000202220*R_042[0]; double PR_001000021020=P_001000021*R_020[0]+-1*P_001000121*R_021[0]+P_001000221*R_022[0]+-1*P_001000321*R_023[0]+-1*P_101000021*R_120[0]+P_101000121*R_121[0]+-1*P_101000221*R_122[0]+P_101000321*R_123[0]; double PR_000001021020=P_000001021*R_020[0]+-1*P_000001121*R_021[0]+P_000001221*R_022[0]+-1*P_000001321*R_023[0]+-1*P_000101021*R_030[0]+P_000101121*R_031[0]+-1*P_000101221*R_032[0]+P_000101321*R_033[0]; double PR_000000022020=P_000000022*R_020[0]+-1*P_000000122*R_021[0]+P_000000222*R_022[0]+-1*P_000000322*R_023[0]+P_000000422*R_024[0]; double PR_022000000101=P_022000000*R_101[0]+-1*P_122000000*R_201[0]+P_222000000*R_301[0]+-1*P_322000000*R_401[0]+P_422000000*R_501[0]; double PR_021001000101=P_021001000*R_101[0]+-1*P_021101000*R_111[0]+-1*P_121001000*R_201[0]+P_121101000*R_211[0]+P_221001000*R_301[0]+-1*P_221101000*R_311[0]+-1*P_321001000*R_401[0]+P_321101000*R_411[0]; double PR_020002000101=P_020002000*R_101[0]+-1*P_020102000*R_111[0]+P_020202000*R_121[0]+-1*P_120002000*R_201[0]+P_120102000*R_211[0]+-1*P_120202000*R_221[0]+P_220002000*R_301[0]+-1*P_220102000*R_311[0]+P_220202000*R_321[0]; double PR_021000001101=P_021000001*R_101[0]+-1*P_021000101*R_102[0]+-1*P_121000001*R_201[0]+P_121000101*R_202[0]+P_221000001*R_301[0]+-1*P_221000101*R_302[0]+-1*P_321000001*R_401[0]+P_321000101*R_402[0]; double PR_020001001101=P_020001001*R_101[0]+-1*P_020001101*R_102[0]+-1*P_020101001*R_111[0]+P_020101101*R_112[0]+-1*P_120001001*R_201[0]+P_120001101*R_202[0]+P_120101001*R_211[0]+-1*P_120101101*R_212[0]+P_220001001*R_301[0]+-1*P_220001101*R_302[0]+-1*P_220101001*R_311[0]+P_220101101*R_312[0]; double PR_020000002101=P_020000002*R_101[0]+-1*P_020000102*R_102[0]+P_020000202*R_103[0]+-1*P_120000002*R_201[0]+P_120000102*R_202[0]+-1*P_120000202*R_203[0]+P_220000002*R_301[0]+-1*P_220000102*R_302[0]+P_220000202*R_303[0]; double PR_012010000101=P_012010000*R_101[0]+-1*P_012110000*R_111[0]+-1*P_112010000*R_201[0]+P_112110000*R_211[0]+P_212010000*R_301[0]+-1*P_212110000*R_311[0]+-1*P_312010000*R_401[0]+P_312110000*R_411[0]; double PR_011011000101=P_011011000*R_101[0]+-1*P_011111000*R_111[0]+P_011211000*R_121[0]+-1*P_111011000*R_201[0]+P_111111000*R_211[0]+-1*P_111211000*R_221[0]+P_211011000*R_301[0]+-1*P_211111000*R_311[0]+P_211211000*R_321[0]; double PR_010012000101=P_010012000*R_101[0]+-1*P_010112000*R_111[0]+P_010212000*R_121[0]+-1*P_010312000*R_131[0]+-1*P_110012000*R_201[0]+P_110112000*R_211[0]+-1*P_110212000*R_221[0]+P_110312000*R_231[0]; double PR_011010001101=P_011010001*R_101[0]+-1*P_011010101*R_102[0]+-1*P_011110001*R_111[0]+P_011110101*R_112[0]+-1*P_111010001*R_201[0]+P_111010101*R_202[0]+P_111110001*R_211[0]+-1*P_111110101*R_212[0]+P_211010001*R_301[0]+-1*P_211010101*R_302[0]+-1*P_211110001*R_311[0]+P_211110101*R_312[0]; double PR_010011001101=P_010011001*R_101[0]+-1*P_010011101*R_102[0]+-1*P_010111001*R_111[0]+P_010111101*R_112[0]+P_010211001*R_121[0]+-1*P_010211101*R_122[0]+-1*P_110011001*R_201[0]+P_110011101*R_202[0]+P_110111001*R_211[0]+-1*P_110111101*R_212[0]+-1*P_110211001*R_221[0]+P_110211101*R_222[0]; double PR_010010002101=P_010010002*R_101[0]+-1*P_010010102*R_102[0]+P_010010202*R_103[0]+-1*P_010110002*R_111[0]+P_010110102*R_112[0]+-1*P_010110202*R_113[0]+-1*P_110010002*R_201[0]+P_110010102*R_202[0]+-1*P_110010202*R_203[0]+P_110110002*R_211[0]+-1*P_110110102*R_212[0]+P_110110202*R_213[0]; double PR_002020000101=P_002020000*R_101[0]+-1*P_002120000*R_111[0]+P_002220000*R_121[0]+-1*P_102020000*R_201[0]+P_102120000*R_211[0]+-1*P_102220000*R_221[0]+P_202020000*R_301[0]+-1*P_202120000*R_311[0]+P_202220000*R_321[0]; double PR_001021000101=P_001021000*R_101[0]+-1*P_001121000*R_111[0]+P_001221000*R_121[0]+-1*P_001321000*R_131[0]+-1*P_101021000*R_201[0]+P_101121000*R_211[0]+-1*P_101221000*R_221[0]+P_101321000*R_231[0]; double PR_000022000101=P_000022000*R_101[0]+-1*P_000122000*R_111[0]+P_000222000*R_121[0]+-1*P_000322000*R_131[0]+P_000422000*R_141[0]; double PR_001020001101=P_001020001*R_101[0]+-1*P_001020101*R_102[0]+-1*P_001120001*R_111[0]+P_001120101*R_112[0]+P_001220001*R_121[0]+-1*P_001220101*R_122[0]+-1*P_101020001*R_201[0]+P_101020101*R_202[0]+P_101120001*R_211[0]+-1*P_101120101*R_212[0]+-1*P_101220001*R_221[0]+P_101220101*R_222[0]; double PR_000021001101=P_000021001*R_101[0]+-1*P_000021101*R_102[0]+-1*P_000121001*R_111[0]+P_000121101*R_112[0]+P_000221001*R_121[0]+-1*P_000221101*R_122[0]+-1*P_000321001*R_131[0]+P_000321101*R_132[0]; double PR_000020002101=P_000020002*R_101[0]+-1*P_000020102*R_102[0]+P_000020202*R_103[0]+-1*P_000120002*R_111[0]+P_000120102*R_112[0]+-1*P_000120202*R_113[0]+P_000220002*R_121[0]+-1*P_000220102*R_122[0]+P_000220202*R_123[0]; double PR_012000010101=P_012000010*R_101[0]+-1*P_012000110*R_102[0]+-1*P_112000010*R_201[0]+P_112000110*R_202[0]+P_212000010*R_301[0]+-1*P_212000110*R_302[0]+-1*P_312000010*R_401[0]+P_312000110*R_402[0]; double PR_011001010101=P_011001010*R_101[0]+-1*P_011001110*R_102[0]+-1*P_011101010*R_111[0]+P_011101110*R_112[0]+-1*P_111001010*R_201[0]+P_111001110*R_202[0]+P_111101010*R_211[0]+-1*P_111101110*R_212[0]+P_211001010*R_301[0]+-1*P_211001110*R_302[0]+-1*P_211101010*R_311[0]+P_211101110*R_312[0]; double PR_010002010101=P_010002010*R_101[0]+-1*P_010002110*R_102[0]+-1*P_010102010*R_111[0]+P_010102110*R_112[0]+P_010202010*R_121[0]+-1*P_010202110*R_122[0]+-1*P_110002010*R_201[0]+P_110002110*R_202[0]+P_110102010*R_211[0]+-1*P_110102110*R_212[0]+-1*P_110202010*R_221[0]+P_110202110*R_222[0]; double PR_011000011101=P_011000011*R_101[0]+-1*P_011000111*R_102[0]+P_011000211*R_103[0]+-1*P_111000011*R_201[0]+P_111000111*R_202[0]+-1*P_111000211*R_203[0]+P_211000011*R_301[0]+-1*P_211000111*R_302[0]+P_211000211*R_303[0]; double PR_010001011101=P_010001011*R_101[0]+-1*P_010001111*R_102[0]+P_010001211*R_103[0]+-1*P_010101011*R_111[0]+P_010101111*R_112[0]+-1*P_010101211*R_113[0]+-1*P_110001011*R_201[0]+P_110001111*R_202[0]+-1*P_110001211*R_203[0]+P_110101011*R_211[0]+-1*P_110101111*R_212[0]+P_110101211*R_213[0]; double PR_010000012101=P_010000012*R_101[0]+-1*P_010000112*R_102[0]+P_010000212*R_103[0]+-1*P_010000312*R_104[0]+-1*P_110000012*R_201[0]+P_110000112*R_202[0]+-1*P_110000212*R_203[0]+P_110000312*R_204[0]; double PR_002010010101=P_002010010*R_101[0]+-1*P_002010110*R_102[0]+-1*P_002110010*R_111[0]+P_002110110*R_112[0]+-1*P_102010010*R_201[0]+P_102010110*R_202[0]+P_102110010*R_211[0]+-1*P_102110110*R_212[0]+P_202010010*R_301[0]+-1*P_202010110*R_302[0]+-1*P_202110010*R_311[0]+P_202110110*R_312[0]; double PR_001011010101=P_001011010*R_101[0]+-1*P_001011110*R_102[0]+-1*P_001111010*R_111[0]+P_001111110*R_112[0]+P_001211010*R_121[0]+-1*P_001211110*R_122[0]+-1*P_101011010*R_201[0]+P_101011110*R_202[0]+P_101111010*R_211[0]+-1*P_101111110*R_212[0]+-1*P_101211010*R_221[0]+P_101211110*R_222[0]; double PR_000012010101=P_000012010*R_101[0]+-1*P_000012110*R_102[0]+-1*P_000112010*R_111[0]+P_000112110*R_112[0]+P_000212010*R_121[0]+-1*P_000212110*R_122[0]+-1*P_000312010*R_131[0]+P_000312110*R_132[0]; double PR_001010011101=P_001010011*R_101[0]+-1*P_001010111*R_102[0]+P_001010211*R_103[0]+-1*P_001110011*R_111[0]+P_001110111*R_112[0]+-1*P_001110211*R_113[0]+-1*P_101010011*R_201[0]+P_101010111*R_202[0]+-1*P_101010211*R_203[0]+P_101110011*R_211[0]+-1*P_101110111*R_212[0]+P_101110211*R_213[0]; double PR_000011011101=P_000011011*R_101[0]+-1*P_000011111*R_102[0]+P_000011211*R_103[0]+-1*P_000111011*R_111[0]+P_000111111*R_112[0]+-1*P_000111211*R_113[0]+P_000211011*R_121[0]+-1*P_000211111*R_122[0]+P_000211211*R_123[0]; double PR_000010012101=P_000010012*R_101[0]+-1*P_000010112*R_102[0]+P_000010212*R_103[0]+-1*P_000010312*R_104[0]+-1*P_000110012*R_111[0]+P_000110112*R_112[0]+-1*P_000110212*R_113[0]+P_000110312*R_114[0]; double PR_002000020101=P_002000020*R_101[0]+-1*P_002000120*R_102[0]+P_002000220*R_103[0]+-1*P_102000020*R_201[0]+P_102000120*R_202[0]+-1*P_102000220*R_203[0]+P_202000020*R_301[0]+-1*P_202000120*R_302[0]+P_202000220*R_303[0]; double PR_001001020101=P_001001020*R_101[0]+-1*P_001001120*R_102[0]+P_001001220*R_103[0]+-1*P_001101020*R_111[0]+P_001101120*R_112[0]+-1*P_001101220*R_113[0]+-1*P_101001020*R_201[0]+P_101001120*R_202[0]+-1*P_101001220*R_203[0]+P_101101020*R_211[0]+-1*P_101101120*R_212[0]+P_101101220*R_213[0]; double PR_000002020101=P_000002020*R_101[0]+-1*P_000002120*R_102[0]+P_000002220*R_103[0]+-1*P_000102020*R_111[0]+P_000102120*R_112[0]+-1*P_000102220*R_113[0]+P_000202020*R_121[0]+-1*P_000202120*R_122[0]+P_000202220*R_123[0]; double PR_001000021101=P_001000021*R_101[0]+-1*P_001000121*R_102[0]+P_001000221*R_103[0]+-1*P_001000321*R_104[0]+-1*P_101000021*R_201[0]+P_101000121*R_202[0]+-1*P_101000221*R_203[0]+P_101000321*R_204[0]; double PR_000001021101=P_000001021*R_101[0]+-1*P_000001121*R_102[0]+P_000001221*R_103[0]+-1*P_000001321*R_104[0]+-1*P_000101021*R_111[0]+P_000101121*R_112[0]+-1*P_000101221*R_113[0]+P_000101321*R_114[0]; double PR_000000022101=P_000000022*R_101[0]+-1*P_000000122*R_102[0]+P_000000222*R_103[0]+-1*P_000000322*R_104[0]+P_000000422*R_105[0]; double PR_022000000110=P_022000000*R_110[0]+-1*P_122000000*R_210[0]+P_222000000*R_310[0]+-1*P_322000000*R_410[0]+P_422000000*R_510[0]; double PR_021001000110=P_021001000*R_110[0]+-1*P_021101000*R_120[0]+-1*P_121001000*R_210[0]+P_121101000*R_220[0]+P_221001000*R_310[0]+-1*P_221101000*R_320[0]+-1*P_321001000*R_410[0]+P_321101000*R_420[0]; double PR_020002000110=P_020002000*R_110[0]+-1*P_020102000*R_120[0]+P_020202000*R_130[0]+-1*P_120002000*R_210[0]+P_120102000*R_220[0]+-1*P_120202000*R_230[0]+P_220002000*R_310[0]+-1*P_220102000*R_320[0]+P_220202000*R_330[0]; double PR_021000001110=P_021000001*R_110[0]+-1*P_021000101*R_111[0]+-1*P_121000001*R_210[0]+P_121000101*R_211[0]+P_221000001*R_310[0]+-1*P_221000101*R_311[0]+-1*P_321000001*R_410[0]+P_321000101*R_411[0]; double PR_020001001110=P_020001001*R_110[0]+-1*P_020001101*R_111[0]+-1*P_020101001*R_120[0]+P_020101101*R_121[0]+-1*P_120001001*R_210[0]+P_120001101*R_211[0]+P_120101001*R_220[0]+-1*P_120101101*R_221[0]+P_220001001*R_310[0]+-1*P_220001101*R_311[0]+-1*P_220101001*R_320[0]+P_220101101*R_321[0]; double PR_020000002110=P_020000002*R_110[0]+-1*P_020000102*R_111[0]+P_020000202*R_112[0]+-1*P_120000002*R_210[0]+P_120000102*R_211[0]+-1*P_120000202*R_212[0]+P_220000002*R_310[0]+-1*P_220000102*R_311[0]+P_220000202*R_312[0]; double PR_012010000110=P_012010000*R_110[0]+-1*P_012110000*R_120[0]+-1*P_112010000*R_210[0]+P_112110000*R_220[0]+P_212010000*R_310[0]+-1*P_212110000*R_320[0]+-1*P_312010000*R_410[0]+P_312110000*R_420[0]; double PR_011011000110=P_011011000*R_110[0]+-1*P_011111000*R_120[0]+P_011211000*R_130[0]+-1*P_111011000*R_210[0]+P_111111000*R_220[0]+-1*P_111211000*R_230[0]+P_211011000*R_310[0]+-1*P_211111000*R_320[0]+P_211211000*R_330[0]; double PR_010012000110=P_010012000*R_110[0]+-1*P_010112000*R_120[0]+P_010212000*R_130[0]+-1*P_010312000*R_140[0]+-1*P_110012000*R_210[0]+P_110112000*R_220[0]+-1*P_110212000*R_230[0]+P_110312000*R_240[0]; double PR_011010001110=P_011010001*R_110[0]+-1*P_011010101*R_111[0]+-1*P_011110001*R_120[0]+P_011110101*R_121[0]+-1*P_111010001*R_210[0]+P_111010101*R_211[0]+P_111110001*R_220[0]+-1*P_111110101*R_221[0]+P_211010001*R_310[0]+-1*P_211010101*R_311[0]+-1*P_211110001*R_320[0]+P_211110101*R_321[0]; double PR_010011001110=P_010011001*R_110[0]+-1*P_010011101*R_111[0]+-1*P_010111001*R_120[0]+P_010111101*R_121[0]+P_010211001*R_130[0]+-1*P_010211101*R_131[0]+-1*P_110011001*R_210[0]+P_110011101*R_211[0]+P_110111001*R_220[0]+-1*P_110111101*R_221[0]+-1*P_110211001*R_230[0]+P_110211101*R_231[0]; double PR_010010002110=P_010010002*R_110[0]+-1*P_010010102*R_111[0]+P_010010202*R_112[0]+-1*P_010110002*R_120[0]+P_010110102*R_121[0]+-1*P_010110202*R_122[0]+-1*P_110010002*R_210[0]+P_110010102*R_211[0]+-1*P_110010202*R_212[0]+P_110110002*R_220[0]+-1*P_110110102*R_221[0]+P_110110202*R_222[0]; double PR_002020000110=P_002020000*R_110[0]+-1*P_002120000*R_120[0]+P_002220000*R_130[0]+-1*P_102020000*R_210[0]+P_102120000*R_220[0]+-1*P_102220000*R_230[0]+P_202020000*R_310[0]+-1*P_202120000*R_320[0]+P_202220000*R_330[0]; double PR_001021000110=P_001021000*R_110[0]+-1*P_001121000*R_120[0]+P_001221000*R_130[0]+-1*P_001321000*R_140[0]+-1*P_101021000*R_210[0]+P_101121000*R_220[0]+-1*P_101221000*R_230[0]+P_101321000*R_240[0]; double PR_000022000110=P_000022000*R_110[0]+-1*P_000122000*R_120[0]+P_000222000*R_130[0]+-1*P_000322000*R_140[0]+P_000422000*R_150[0]; double PR_001020001110=P_001020001*R_110[0]+-1*P_001020101*R_111[0]+-1*P_001120001*R_120[0]+P_001120101*R_121[0]+P_001220001*R_130[0]+-1*P_001220101*R_131[0]+-1*P_101020001*R_210[0]+P_101020101*R_211[0]+P_101120001*R_220[0]+-1*P_101120101*R_221[0]+-1*P_101220001*R_230[0]+P_101220101*R_231[0]; double PR_000021001110=P_000021001*R_110[0]+-1*P_000021101*R_111[0]+-1*P_000121001*R_120[0]+P_000121101*R_121[0]+P_000221001*R_130[0]+-1*P_000221101*R_131[0]+-1*P_000321001*R_140[0]+P_000321101*R_141[0]; double PR_000020002110=P_000020002*R_110[0]+-1*P_000020102*R_111[0]+P_000020202*R_112[0]+-1*P_000120002*R_120[0]+P_000120102*R_121[0]+-1*P_000120202*R_122[0]+P_000220002*R_130[0]+-1*P_000220102*R_131[0]+P_000220202*R_132[0]; double PR_012000010110=P_012000010*R_110[0]+-1*P_012000110*R_111[0]+-1*P_112000010*R_210[0]+P_112000110*R_211[0]+P_212000010*R_310[0]+-1*P_212000110*R_311[0]+-1*P_312000010*R_410[0]+P_312000110*R_411[0]; double PR_011001010110=P_011001010*R_110[0]+-1*P_011001110*R_111[0]+-1*P_011101010*R_120[0]+P_011101110*R_121[0]+-1*P_111001010*R_210[0]+P_111001110*R_211[0]+P_111101010*R_220[0]+-1*P_111101110*R_221[0]+P_211001010*R_310[0]+-1*P_211001110*R_311[0]+-1*P_211101010*R_320[0]+P_211101110*R_321[0]; double PR_010002010110=P_010002010*R_110[0]+-1*P_010002110*R_111[0]+-1*P_010102010*R_120[0]+P_010102110*R_121[0]+P_010202010*R_130[0]+-1*P_010202110*R_131[0]+-1*P_110002010*R_210[0]+P_110002110*R_211[0]+P_110102010*R_220[0]+-1*P_110102110*R_221[0]+-1*P_110202010*R_230[0]+P_110202110*R_231[0]; double PR_011000011110=P_011000011*R_110[0]+-1*P_011000111*R_111[0]+P_011000211*R_112[0]+-1*P_111000011*R_210[0]+P_111000111*R_211[0]+-1*P_111000211*R_212[0]+P_211000011*R_310[0]+-1*P_211000111*R_311[0]+P_211000211*R_312[0]; double PR_010001011110=P_010001011*R_110[0]+-1*P_010001111*R_111[0]+P_010001211*R_112[0]+-1*P_010101011*R_120[0]+P_010101111*R_121[0]+-1*P_010101211*R_122[0]+-1*P_110001011*R_210[0]+P_110001111*R_211[0]+-1*P_110001211*R_212[0]+P_110101011*R_220[0]+-1*P_110101111*R_221[0]+P_110101211*R_222[0]; double PR_010000012110=P_010000012*R_110[0]+-1*P_010000112*R_111[0]+P_010000212*R_112[0]+-1*P_010000312*R_113[0]+-1*P_110000012*R_210[0]+P_110000112*R_211[0]+-1*P_110000212*R_212[0]+P_110000312*R_213[0]; double PR_002010010110=P_002010010*R_110[0]+-1*P_002010110*R_111[0]+-1*P_002110010*R_120[0]+P_002110110*R_121[0]+-1*P_102010010*R_210[0]+P_102010110*R_211[0]+P_102110010*R_220[0]+-1*P_102110110*R_221[0]+P_202010010*R_310[0]+-1*P_202010110*R_311[0]+-1*P_202110010*R_320[0]+P_202110110*R_321[0]; double PR_001011010110=P_001011010*R_110[0]+-1*P_001011110*R_111[0]+-1*P_001111010*R_120[0]+P_001111110*R_121[0]+P_001211010*R_130[0]+-1*P_001211110*R_131[0]+-1*P_101011010*R_210[0]+P_101011110*R_211[0]+P_101111010*R_220[0]+-1*P_101111110*R_221[0]+-1*P_101211010*R_230[0]+P_101211110*R_231[0]; double PR_000012010110=P_000012010*R_110[0]+-1*P_000012110*R_111[0]+-1*P_000112010*R_120[0]+P_000112110*R_121[0]+P_000212010*R_130[0]+-1*P_000212110*R_131[0]+-1*P_000312010*R_140[0]+P_000312110*R_141[0]; double PR_001010011110=P_001010011*R_110[0]+-1*P_001010111*R_111[0]+P_001010211*R_112[0]+-1*P_001110011*R_120[0]+P_001110111*R_121[0]+-1*P_001110211*R_122[0]+-1*P_101010011*R_210[0]+P_101010111*R_211[0]+-1*P_101010211*R_212[0]+P_101110011*R_220[0]+-1*P_101110111*R_221[0]+P_101110211*R_222[0]; double PR_000011011110=P_000011011*R_110[0]+-1*P_000011111*R_111[0]+P_000011211*R_112[0]+-1*P_000111011*R_120[0]+P_000111111*R_121[0]+-1*P_000111211*R_122[0]+P_000211011*R_130[0]+-1*P_000211111*R_131[0]+P_000211211*R_132[0]; double PR_000010012110=P_000010012*R_110[0]+-1*P_000010112*R_111[0]+P_000010212*R_112[0]+-1*P_000010312*R_113[0]+-1*P_000110012*R_120[0]+P_000110112*R_121[0]+-1*P_000110212*R_122[0]+P_000110312*R_123[0]; double PR_002000020110=P_002000020*R_110[0]+-1*P_002000120*R_111[0]+P_002000220*R_112[0]+-1*P_102000020*R_210[0]+P_102000120*R_211[0]+-1*P_102000220*R_212[0]+P_202000020*R_310[0]+-1*P_202000120*R_311[0]+P_202000220*R_312[0]; double PR_001001020110=P_001001020*R_110[0]+-1*P_001001120*R_111[0]+P_001001220*R_112[0]+-1*P_001101020*R_120[0]+P_001101120*R_121[0]+-1*P_001101220*R_122[0]+-1*P_101001020*R_210[0]+P_101001120*R_211[0]+-1*P_101001220*R_212[0]+P_101101020*R_220[0]+-1*P_101101120*R_221[0]+P_101101220*R_222[0]; double PR_000002020110=P_000002020*R_110[0]+-1*P_000002120*R_111[0]+P_000002220*R_112[0]+-1*P_000102020*R_120[0]+P_000102120*R_121[0]+-1*P_000102220*R_122[0]+P_000202020*R_130[0]+-1*P_000202120*R_131[0]+P_000202220*R_132[0]; double PR_001000021110=P_001000021*R_110[0]+-1*P_001000121*R_111[0]+P_001000221*R_112[0]+-1*P_001000321*R_113[0]+-1*P_101000021*R_210[0]+P_101000121*R_211[0]+-1*P_101000221*R_212[0]+P_101000321*R_213[0]; double PR_000001021110=P_000001021*R_110[0]+-1*P_000001121*R_111[0]+P_000001221*R_112[0]+-1*P_000001321*R_113[0]+-1*P_000101021*R_120[0]+P_000101121*R_121[0]+-1*P_000101221*R_122[0]+P_000101321*R_123[0]; double PR_000000022110=P_000000022*R_110[0]+-1*P_000000122*R_111[0]+P_000000222*R_112[0]+-1*P_000000322*R_113[0]+P_000000422*R_114[0]; double PR_022000000200=P_022000000*R_200[0]+-1*P_122000000*R_300[0]+P_222000000*R_400[0]+-1*P_322000000*R_500[0]+P_422000000*R_600[0]; double PR_021001000200=P_021001000*R_200[0]+-1*P_021101000*R_210[0]+-1*P_121001000*R_300[0]+P_121101000*R_310[0]+P_221001000*R_400[0]+-1*P_221101000*R_410[0]+-1*P_321001000*R_500[0]+P_321101000*R_510[0]; double PR_020002000200=P_020002000*R_200[0]+-1*P_020102000*R_210[0]+P_020202000*R_220[0]+-1*P_120002000*R_300[0]+P_120102000*R_310[0]+-1*P_120202000*R_320[0]+P_220002000*R_400[0]+-1*P_220102000*R_410[0]+P_220202000*R_420[0]; double PR_021000001200=P_021000001*R_200[0]+-1*P_021000101*R_201[0]+-1*P_121000001*R_300[0]+P_121000101*R_301[0]+P_221000001*R_400[0]+-1*P_221000101*R_401[0]+-1*P_321000001*R_500[0]+P_321000101*R_501[0]; double PR_020001001200=P_020001001*R_200[0]+-1*P_020001101*R_201[0]+-1*P_020101001*R_210[0]+P_020101101*R_211[0]+-1*P_120001001*R_300[0]+P_120001101*R_301[0]+P_120101001*R_310[0]+-1*P_120101101*R_311[0]+P_220001001*R_400[0]+-1*P_220001101*R_401[0]+-1*P_220101001*R_410[0]+P_220101101*R_411[0]; double PR_020000002200=P_020000002*R_200[0]+-1*P_020000102*R_201[0]+P_020000202*R_202[0]+-1*P_120000002*R_300[0]+P_120000102*R_301[0]+-1*P_120000202*R_302[0]+P_220000002*R_400[0]+-1*P_220000102*R_401[0]+P_220000202*R_402[0]; double PR_012010000200=P_012010000*R_200[0]+-1*P_012110000*R_210[0]+-1*P_112010000*R_300[0]+P_112110000*R_310[0]+P_212010000*R_400[0]+-1*P_212110000*R_410[0]+-1*P_312010000*R_500[0]+P_312110000*R_510[0]; double PR_011011000200=P_011011000*R_200[0]+-1*P_011111000*R_210[0]+P_011211000*R_220[0]+-1*P_111011000*R_300[0]+P_111111000*R_310[0]+-1*P_111211000*R_320[0]+P_211011000*R_400[0]+-1*P_211111000*R_410[0]+P_211211000*R_420[0]; double PR_010012000200=P_010012000*R_200[0]+-1*P_010112000*R_210[0]+P_010212000*R_220[0]+-1*P_010312000*R_230[0]+-1*P_110012000*R_300[0]+P_110112000*R_310[0]+-1*P_110212000*R_320[0]+P_110312000*R_330[0]; double PR_011010001200=P_011010001*R_200[0]+-1*P_011010101*R_201[0]+-1*P_011110001*R_210[0]+P_011110101*R_211[0]+-1*P_111010001*R_300[0]+P_111010101*R_301[0]+P_111110001*R_310[0]+-1*P_111110101*R_311[0]+P_211010001*R_400[0]+-1*P_211010101*R_401[0]+-1*P_211110001*R_410[0]+P_211110101*R_411[0]; double PR_010011001200=P_010011001*R_200[0]+-1*P_010011101*R_201[0]+-1*P_010111001*R_210[0]+P_010111101*R_211[0]+P_010211001*R_220[0]+-1*P_010211101*R_221[0]+-1*P_110011001*R_300[0]+P_110011101*R_301[0]+P_110111001*R_310[0]+-1*P_110111101*R_311[0]+-1*P_110211001*R_320[0]+P_110211101*R_321[0]; double PR_010010002200=P_010010002*R_200[0]+-1*P_010010102*R_201[0]+P_010010202*R_202[0]+-1*P_010110002*R_210[0]+P_010110102*R_211[0]+-1*P_010110202*R_212[0]+-1*P_110010002*R_300[0]+P_110010102*R_301[0]+-1*P_110010202*R_302[0]+P_110110002*R_310[0]+-1*P_110110102*R_311[0]+P_110110202*R_312[0]; double PR_002020000200=P_002020000*R_200[0]+-1*P_002120000*R_210[0]+P_002220000*R_220[0]+-1*P_102020000*R_300[0]+P_102120000*R_310[0]+-1*P_102220000*R_320[0]+P_202020000*R_400[0]+-1*P_202120000*R_410[0]+P_202220000*R_420[0]; double PR_001021000200=P_001021000*R_200[0]+-1*P_001121000*R_210[0]+P_001221000*R_220[0]+-1*P_001321000*R_230[0]+-1*P_101021000*R_300[0]+P_101121000*R_310[0]+-1*P_101221000*R_320[0]+P_101321000*R_330[0]; double PR_000022000200=P_000022000*R_200[0]+-1*P_000122000*R_210[0]+P_000222000*R_220[0]+-1*P_000322000*R_230[0]+P_000422000*R_240[0]; double PR_001020001200=P_001020001*R_200[0]+-1*P_001020101*R_201[0]+-1*P_001120001*R_210[0]+P_001120101*R_211[0]+P_001220001*R_220[0]+-1*P_001220101*R_221[0]+-1*P_101020001*R_300[0]+P_101020101*R_301[0]+P_101120001*R_310[0]+-1*P_101120101*R_311[0]+-1*P_101220001*R_320[0]+P_101220101*R_321[0]; double PR_000021001200=P_000021001*R_200[0]+-1*P_000021101*R_201[0]+-1*P_000121001*R_210[0]+P_000121101*R_211[0]+P_000221001*R_220[0]+-1*P_000221101*R_221[0]+-1*P_000321001*R_230[0]+P_000321101*R_231[0]; double PR_000020002200=P_000020002*R_200[0]+-1*P_000020102*R_201[0]+P_000020202*R_202[0]+-1*P_000120002*R_210[0]+P_000120102*R_211[0]+-1*P_000120202*R_212[0]+P_000220002*R_220[0]+-1*P_000220102*R_221[0]+P_000220202*R_222[0]; double PR_012000010200=P_012000010*R_200[0]+-1*P_012000110*R_201[0]+-1*P_112000010*R_300[0]+P_112000110*R_301[0]+P_212000010*R_400[0]+-1*P_212000110*R_401[0]+-1*P_312000010*R_500[0]+P_312000110*R_501[0]; double PR_011001010200=P_011001010*R_200[0]+-1*P_011001110*R_201[0]+-1*P_011101010*R_210[0]+P_011101110*R_211[0]+-1*P_111001010*R_300[0]+P_111001110*R_301[0]+P_111101010*R_310[0]+-1*P_111101110*R_311[0]+P_211001010*R_400[0]+-1*P_211001110*R_401[0]+-1*P_211101010*R_410[0]+P_211101110*R_411[0]; double PR_010002010200=P_010002010*R_200[0]+-1*P_010002110*R_201[0]+-1*P_010102010*R_210[0]+P_010102110*R_211[0]+P_010202010*R_220[0]+-1*P_010202110*R_221[0]+-1*P_110002010*R_300[0]+P_110002110*R_301[0]+P_110102010*R_310[0]+-1*P_110102110*R_311[0]+-1*P_110202010*R_320[0]+P_110202110*R_321[0]; double PR_011000011200=P_011000011*R_200[0]+-1*P_011000111*R_201[0]+P_011000211*R_202[0]+-1*P_111000011*R_300[0]+P_111000111*R_301[0]+-1*P_111000211*R_302[0]+P_211000011*R_400[0]+-1*P_211000111*R_401[0]+P_211000211*R_402[0]; double PR_010001011200=P_010001011*R_200[0]+-1*P_010001111*R_201[0]+P_010001211*R_202[0]+-1*P_010101011*R_210[0]+P_010101111*R_211[0]+-1*P_010101211*R_212[0]+-1*P_110001011*R_300[0]+P_110001111*R_301[0]+-1*P_110001211*R_302[0]+P_110101011*R_310[0]+-1*P_110101111*R_311[0]+P_110101211*R_312[0]; double PR_010000012200=P_010000012*R_200[0]+-1*P_010000112*R_201[0]+P_010000212*R_202[0]+-1*P_010000312*R_203[0]+-1*P_110000012*R_300[0]+P_110000112*R_301[0]+-1*P_110000212*R_302[0]+P_110000312*R_303[0]; double PR_002010010200=P_002010010*R_200[0]+-1*P_002010110*R_201[0]+-1*P_002110010*R_210[0]+P_002110110*R_211[0]+-1*P_102010010*R_300[0]+P_102010110*R_301[0]+P_102110010*R_310[0]+-1*P_102110110*R_311[0]+P_202010010*R_400[0]+-1*P_202010110*R_401[0]+-1*P_202110010*R_410[0]+P_202110110*R_411[0]; double PR_001011010200=P_001011010*R_200[0]+-1*P_001011110*R_201[0]+-1*P_001111010*R_210[0]+P_001111110*R_211[0]+P_001211010*R_220[0]+-1*P_001211110*R_221[0]+-1*P_101011010*R_300[0]+P_101011110*R_301[0]+P_101111010*R_310[0]+-1*P_101111110*R_311[0]+-1*P_101211010*R_320[0]+P_101211110*R_321[0]; double PR_000012010200=P_000012010*R_200[0]+-1*P_000012110*R_201[0]+-1*P_000112010*R_210[0]+P_000112110*R_211[0]+P_000212010*R_220[0]+-1*P_000212110*R_221[0]+-1*P_000312010*R_230[0]+P_000312110*R_231[0]; double PR_001010011200=P_001010011*R_200[0]+-1*P_001010111*R_201[0]+P_001010211*R_202[0]+-1*P_001110011*R_210[0]+P_001110111*R_211[0]+-1*P_001110211*R_212[0]+-1*P_101010011*R_300[0]+P_101010111*R_301[0]+-1*P_101010211*R_302[0]+P_101110011*R_310[0]+-1*P_101110111*R_311[0]+P_101110211*R_312[0]; double PR_000011011200=P_000011011*R_200[0]+-1*P_000011111*R_201[0]+P_000011211*R_202[0]+-1*P_000111011*R_210[0]+P_000111111*R_211[0]+-1*P_000111211*R_212[0]+P_000211011*R_220[0]+-1*P_000211111*R_221[0]+P_000211211*R_222[0]; double PR_000010012200=P_000010012*R_200[0]+-1*P_000010112*R_201[0]+P_000010212*R_202[0]+-1*P_000010312*R_203[0]+-1*P_000110012*R_210[0]+P_000110112*R_211[0]+-1*P_000110212*R_212[0]+P_000110312*R_213[0]; double PR_002000020200=P_002000020*R_200[0]+-1*P_002000120*R_201[0]+P_002000220*R_202[0]+-1*P_102000020*R_300[0]+P_102000120*R_301[0]+-1*P_102000220*R_302[0]+P_202000020*R_400[0]+-1*P_202000120*R_401[0]+P_202000220*R_402[0]; double PR_001001020200=P_001001020*R_200[0]+-1*P_001001120*R_201[0]+P_001001220*R_202[0]+-1*P_001101020*R_210[0]+P_001101120*R_211[0]+-1*P_001101220*R_212[0]+-1*P_101001020*R_300[0]+P_101001120*R_301[0]+-1*P_101001220*R_302[0]+P_101101020*R_310[0]+-1*P_101101120*R_311[0]+P_101101220*R_312[0]; double PR_000002020200=P_000002020*R_200[0]+-1*P_000002120*R_201[0]+P_000002220*R_202[0]+-1*P_000102020*R_210[0]+P_000102120*R_211[0]+-1*P_000102220*R_212[0]+P_000202020*R_220[0]+-1*P_000202120*R_221[0]+P_000202220*R_222[0]; double PR_001000021200=P_001000021*R_200[0]+-1*P_001000121*R_201[0]+P_001000221*R_202[0]+-1*P_001000321*R_203[0]+-1*P_101000021*R_300[0]+P_101000121*R_301[0]+-1*P_101000221*R_302[0]+P_101000321*R_303[0]; double PR_000001021200=P_000001021*R_200[0]+-1*P_000001121*R_201[0]+P_000001221*R_202[0]+-1*P_000001321*R_203[0]+-1*P_000101021*R_210[0]+P_000101121*R_211[0]+-1*P_000101221*R_212[0]+P_000101321*R_213[0]; double PR_000000022200=P_000000022*R_200[0]+-1*P_000000122*R_201[0]+P_000000222*R_202[0]+-1*P_000000322*R_203[0]+P_000000422*R_204[0]; double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(Q_002000000*PR_022000000000+Q_102000000*PR_022000000100+Q_202000000*PR_022000000200); ans_temp[ans_id*6+0]+=Pmtrx[1]*(Q_001001000*PR_022000000000+Q_001101000*PR_022000000010+Q_101001000*PR_022000000100+Q_101101000*PR_022000000110); ans_temp[ans_id*6+0]+=Pmtrx[2]*(Q_000002000*PR_022000000000+Q_000102000*PR_022000000010+Q_000202000*PR_022000000020); ans_temp[ans_id*6+0]+=Pmtrx[3]*(Q_001000001*PR_022000000000+Q_001000101*PR_022000000001+Q_101000001*PR_022000000100+Q_101000101*PR_022000000101); ans_temp[ans_id*6+0]+=Pmtrx[4]*(Q_000001001*PR_022000000000+Q_000001101*PR_022000000001+Q_000101001*PR_022000000010+Q_000101101*PR_022000000011); ans_temp[ans_id*6+0]+=Pmtrx[5]*(Q_000000002*PR_022000000000+Q_000000102*PR_022000000001+Q_000000202*PR_022000000002); ans_temp[ans_id*6+0]+=Pmtrx[6]*(Q_002000000*PR_021001000000+Q_102000000*PR_021001000100+Q_202000000*PR_021001000200); ans_temp[ans_id*6+0]+=Pmtrx[7]*(Q_001001000*PR_021001000000+Q_001101000*PR_021001000010+Q_101001000*PR_021001000100+Q_101101000*PR_021001000110); ans_temp[ans_id*6+0]+=Pmtrx[8]*(Q_000002000*PR_021001000000+Q_000102000*PR_021001000010+Q_000202000*PR_021001000020); ans_temp[ans_id*6+0]+=Pmtrx[9]*(Q_001000001*PR_021001000000+Q_001000101*PR_021001000001+Q_101000001*PR_021001000100+Q_101000101*PR_021001000101); ans_temp[ans_id*6+0]+=Pmtrx[10]*(Q_000001001*PR_021001000000+Q_000001101*PR_021001000001+Q_000101001*PR_021001000010+Q_000101101*PR_021001000011); ans_temp[ans_id*6+0]+=Pmtrx[11]*(Q_000000002*PR_021001000000+Q_000000102*PR_021001000001+Q_000000202*PR_021001000002); ans_temp[ans_id*6+0]+=Pmtrx[12]*(Q_002000000*PR_020002000000+Q_102000000*PR_020002000100+Q_202000000*PR_020002000200); ans_temp[ans_id*6+0]+=Pmtrx[13]*(Q_001001000*PR_020002000000+Q_001101000*PR_020002000010+Q_101001000*PR_020002000100+Q_101101000*PR_020002000110); ans_temp[ans_id*6+0]+=Pmtrx[14]*(Q_000002000*PR_020002000000+Q_000102000*PR_020002000010+Q_000202000*PR_020002000020); ans_temp[ans_id*6+0]+=Pmtrx[15]*(Q_001000001*PR_020002000000+Q_001000101*PR_020002000001+Q_101000001*PR_020002000100+Q_101000101*PR_020002000101); ans_temp[ans_id*6+0]+=Pmtrx[16]*(Q_000001001*PR_020002000000+Q_000001101*PR_020002000001+Q_000101001*PR_020002000010+Q_000101101*PR_020002000011); ans_temp[ans_id*6+0]+=Pmtrx[17]*(Q_000000002*PR_020002000000+Q_000000102*PR_020002000001+Q_000000202*PR_020002000002); ans_temp[ans_id*6+0]+=Pmtrx[18]*(Q_002000000*PR_021000001000+Q_102000000*PR_021000001100+Q_202000000*PR_021000001200); ans_temp[ans_id*6+0]+=Pmtrx[19]*(Q_001001000*PR_021000001000+Q_001101000*PR_021000001010+Q_101001000*PR_021000001100+Q_101101000*PR_021000001110); ans_temp[ans_id*6+0]+=Pmtrx[20]*(Q_000002000*PR_021000001000+Q_000102000*PR_021000001010+Q_000202000*PR_021000001020); ans_temp[ans_id*6+0]+=Pmtrx[21]*(Q_001000001*PR_021000001000+Q_001000101*PR_021000001001+Q_101000001*PR_021000001100+Q_101000101*PR_021000001101); ans_temp[ans_id*6+0]+=Pmtrx[22]*(Q_000001001*PR_021000001000+Q_000001101*PR_021000001001+Q_000101001*PR_021000001010+Q_000101101*PR_021000001011); ans_temp[ans_id*6+0]+=Pmtrx[23]*(Q_000000002*PR_021000001000+Q_000000102*PR_021000001001+Q_000000202*PR_021000001002); ans_temp[ans_id*6+0]+=Pmtrx[24]*(Q_002000000*PR_020001001000+Q_102000000*PR_020001001100+Q_202000000*PR_020001001200); ans_temp[ans_id*6+0]+=Pmtrx[25]*(Q_001001000*PR_020001001000+Q_001101000*PR_020001001010+Q_101001000*PR_020001001100+Q_101101000*PR_020001001110); ans_temp[ans_id*6+0]+=Pmtrx[26]*(Q_000002000*PR_020001001000+Q_000102000*PR_020001001010+Q_000202000*PR_020001001020); ans_temp[ans_id*6+0]+=Pmtrx[27]*(Q_001000001*PR_020001001000+Q_001000101*PR_020001001001+Q_101000001*PR_020001001100+Q_101000101*PR_020001001101); ans_temp[ans_id*6+0]+=Pmtrx[28]*(Q_000001001*PR_020001001000+Q_000001101*PR_020001001001+Q_000101001*PR_020001001010+Q_000101101*PR_020001001011); ans_temp[ans_id*6+0]+=Pmtrx[29]*(Q_000000002*PR_020001001000+Q_000000102*PR_020001001001+Q_000000202*PR_020001001002); ans_temp[ans_id*6+0]+=Pmtrx[30]*(Q_002000000*PR_020000002000+Q_102000000*PR_020000002100+Q_202000000*PR_020000002200); ans_temp[ans_id*6+0]+=Pmtrx[31]*(Q_001001000*PR_020000002000+Q_001101000*PR_020000002010+Q_101001000*PR_020000002100+Q_101101000*PR_020000002110); ans_temp[ans_id*6+0]+=Pmtrx[32]*(Q_000002000*PR_020000002000+Q_000102000*PR_020000002010+Q_000202000*PR_020000002020); ans_temp[ans_id*6+0]+=Pmtrx[33]*(Q_001000001*PR_020000002000+Q_001000101*PR_020000002001+Q_101000001*PR_020000002100+Q_101000101*PR_020000002101); ans_temp[ans_id*6+0]+=Pmtrx[34]*(Q_000001001*PR_020000002000+Q_000001101*PR_020000002001+Q_000101001*PR_020000002010+Q_000101101*PR_020000002011); ans_temp[ans_id*6+0]+=Pmtrx[35]*(Q_000000002*PR_020000002000+Q_000000102*PR_020000002001+Q_000000202*PR_020000002002); ans_temp[ans_id*6+1]+=Pmtrx[0]*(Q_002000000*PR_012010000000+Q_102000000*PR_012010000100+Q_202000000*PR_012010000200); ans_temp[ans_id*6+1]+=Pmtrx[1]*(Q_001001000*PR_012010000000+Q_001101000*PR_012010000010+Q_101001000*PR_012010000100+Q_101101000*PR_012010000110); ans_temp[ans_id*6+1]+=Pmtrx[2]*(Q_000002000*PR_012010000000+Q_000102000*PR_012010000010+Q_000202000*PR_012010000020); ans_temp[ans_id*6+1]+=Pmtrx[3]*(Q_001000001*PR_012010000000+Q_001000101*PR_012010000001+Q_101000001*PR_012010000100+Q_101000101*PR_012010000101); ans_temp[ans_id*6+1]+=Pmtrx[4]*(Q_000001001*PR_012010000000+Q_000001101*PR_012010000001+Q_000101001*PR_012010000010+Q_000101101*PR_012010000011); ans_temp[ans_id*6+1]+=Pmtrx[5]*(Q_000000002*PR_012010000000+Q_000000102*PR_012010000001+Q_000000202*PR_012010000002); ans_temp[ans_id*6+1]+=Pmtrx[6]*(Q_002000000*PR_011011000000+Q_102000000*PR_011011000100+Q_202000000*PR_011011000200); ans_temp[ans_id*6+1]+=Pmtrx[7]*(Q_001001000*PR_011011000000+Q_001101000*PR_011011000010+Q_101001000*PR_011011000100+Q_101101000*PR_011011000110); ans_temp[ans_id*6+1]+=Pmtrx[8]*(Q_000002000*PR_011011000000+Q_000102000*PR_011011000010+Q_000202000*PR_011011000020); ans_temp[ans_id*6+1]+=Pmtrx[9]*(Q_001000001*PR_011011000000+Q_001000101*PR_011011000001+Q_101000001*PR_011011000100+Q_101000101*PR_011011000101); ans_temp[ans_id*6+1]+=Pmtrx[10]*(Q_000001001*PR_011011000000+Q_000001101*PR_011011000001+Q_000101001*PR_011011000010+Q_000101101*PR_011011000011); ans_temp[ans_id*6+1]+=Pmtrx[11]*(Q_000000002*PR_011011000000+Q_000000102*PR_011011000001+Q_000000202*PR_011011000002); ans_temp[ans_id*6+1]+=Pmtrx[12]*(Q_002000000*PR_010012000000+Q_102000000*PR_010012000100+Q_202000000*PR_010012000200); ans_temp[ans_id*6+1]+=Pmtrx[13]*(Q_001001000*PR_010012000000+Q_001101000*PR_010012000010+Q_101001000*PR_010012000100+Q_101101000*PR_010012000110); ans_temp[ans_id*6+1]+=Pmtrx[14]*(Q_000002000*PR_010012000000+Q_000102000*PR_010012000010+Q_000202000*PR_010012000020); ans_temp[ans_id*6+1]+=Pmtrx[15]*(Q_001000001*PR_010012000000+Q_001000101*PR_010012000001+Q_101000001*PR_010012000100+Q_101000101*PR_010012000101); ans_temp[ans_id*6+1]+=Pmtrx[16]*(Q_000001001*PR_010012000000+Q_000001101*PR_010012000001+Q_000101001*PR_010012000010+Q_000101101*PR_010012000011); ans_temp[ans_id*6+1]+=Pmtrx[17]*(Q_000000002*PR_010012000000+Q_000000102*PR_010012000001+Q_000000202*PR_010012000002); ans_temp[ans_id*6+1]+=Pmtrx[18]*(Q_002000000*PR_011010001000+Q_102000000*PR_011010001100+Q_202000000*PR_011010001200); ans_temp[ans_id*6+1]+=Pmtrx[19]*(Q_001001000*PR_011010001000+Q_001101000*PR_011010001010+Q_101001000*PR_011010001100+Q_101101000*PR_011010001110); ans_temp[ans_id*6+1]+=Pmtrx[20]*(Q_000002000*PR_011010001000+Q_000102000*PR_011010001010+Q_000202000*PR_011010001020); ans_temp[ans_id*6+1]+=Pmtrx[21]*(Q_001000001*PR_011010001000+Q_001000101*PR_011010001001+Q_101000001*PR_011010001100+Q_101000101*PR_011010001101); ans_temp[ans_id*6+1]+=Pmtrx[22]*(Q_000001001*PR_011010001000+Q_000001101*PR_011010001001+Q_000101001*PR_011010001010+Q_000101101*PR_011010001011); ans_temp[ans_id*6+1]+=Pmtrx[23]*(Q_000000002*PR_011010001000+Q_000000102*PR_011010001001+Q_000000202*PR_011010001002); ans_temp[ans_id*6+1]+=Pmtrx[24]*(Q_002000000*PR_010011001000+Q_102000000*PR_010011001100+Q_202000000*PR_010011001200); ans_temp[ans_id*6+1]+=Pmtrx[25]*(Q_001001000*PR_010011001000+Q_001101000*PR_010011001010+Q_101001000*PR_010011001100+Q_101101000*PR_010011001110); ans_temp[ans_id*6+1]+=Pmtrx[26]*(Q_000002000*PR_010011001000+Q_000102000*PR_010011001010+Q_000202000*PR_010011001020); ans_temp[ans_id*6+1]+=Pmtrx[27]*(Q_001000001*PR_010011001000+Q_001000101*PR_010011001001+Q_101000001*PR_010011001100+Q_101000101*PR_010011001101); ans_temp[ans_id*6+1]+=Pmtrx[28]*(Q_000001001*PR_010011001000+Q_000001101*PR_010011001001+Q_000101001*PR_010011001010+Q_000101101*PR_010011001011); ans_temp[ans_id*6+1]+=Pmtrx[29]*(Q_000000002*PR_010011001000+Q_000000102*PR_010011001001+Q_000000202*PR_010011001002); ans_temp[ans_id*6+1]+=Pmtrx[30]*(Q_002000000*PR_010010002000+Q_102000000*PR_010010002100+Q_202000000*PR_010010002200); ans_temp[ans_id*6+1]+=Pmtrx[31]*(Q_001001000*PR_010010002000+Q_001101000*PR_010010002010+Q_101001000*PR_010010002100+Q_101101000*PR_010010002110); ans_temp[ans_id*6+1]+=Pmtrx[32]*(Q_000002000*PR_010010002000+Q_000102000*PR_010010002010+Q_000202000*PR_010010002020); ans_temp[ans_id*6+1]+=Pmtrx[33]*(Q_001000001*PR_010010002000+Q_001000101*PR_010010002001+Q_101000001*PR_010010002100+Q_101000101*PR_010010002101); ans_temp[ans_id*6+1]+=Pmtrx[34]*(Q_000001001*PR_010010002000+Q_000001101*PR_010010002001+Q_000101001*PR_010010002010+Q_000101101*PR_010010002011); ans_temp[ans_id*6+1]+=Pmtrx[35]*(Q_000000002*PR_010010002000+Q_000000102*PR_010010002001+Q_000000202*PR_010010002002); ans_temp[ans_id*6+2]+=Pmtrx[0]*(Q_002000000*PR_002020000000+Q_102000000*PR_002020000100+Q_202000000*PR_002020000200); ans_temp[ans_id*6+2]+=Pmtrx[1]*(Q_001001000*PR_002020000000+Q_001101000*PR_002020000010+Q_101001000*PR_002020000100+Q_101101000*PR_002020000110); ans_temp[ans_id*6+2]+=Pmtrx[2]*(Q_000002000*PR_002020000000+Q_000102000*PR_002020000010+Q_000202000*PR_002020000020); ans_temp[ans_id*6+2]+=Pmtrx[3]*(Q_001000001*PR_002020000000+Q_001000101*PR_002020000001+Q_101000001*PR_002020000100+Q_101000101*PR_002020000101); ans_temp[ans_id*6+2]+=Pmtrx[4]*(Q_000001001*PR_002020000000+Q_000001101*PR_002020000001+Q_000101001*PR_002020000010+Q_000101101*PR_002020000011); ans_temp[ans_id*6+2]+=Pmtrx[5]*(Q_000000002*PR_002020000000+Q_000000102*PR_002020000001+Q_000000202*PR_002020000002); ans_temp[ans_id*6+2]+=Pmtrx[6]*(Q_002000000*PR_001021000000+Q_102000000*PR_001021000100+Q_202000000*PR_001021000200); ans_temp[ans_id*6+2]+=Pmtrx[7]*(Q_001001000*PR_001021000000+Q_001101000*PR_001021000010+Q_101001000*PR_001021000100+Q_101101000*PR_001021000110); ans_temp[ans_id*6+2]+=Pmtrx[8]*(Q_000002000*PR_001021000000+Q_000102000*PR_001021000010+Q_000202000*PR_001021000020); ans_temp[ans_id*6+2]+=Pmtrx[9]*(Q_001000001*PR_001021000000+Q_001000101*PR_001021000001+Q_101000001*PR_001021000100+Q_101000101*PR_001021000101); ans_temp[ans_id*6+2]+=Pmtrx[10]*(Q_000001001*PR_001021000000+Q_000001101*PR_001021000001+Q_000101001*PR_001021000010+Q_000101101*PR_001021000011); ans_temp[ans_id*6+2]+=Pmtrx[11]*(Q_000000002*PR_001021000000+Q_000000102*PR_001021000001+Q_000000202*PR_001021000002); ans_temp[ans_id*6+2]+=Pmtrx[12]*(Q_002000000*PR_000022000000+Q_102000000*PR_000022000100+Q_202000000*PR_000022000200); ans_temp[ans_id*6+2]+=Pmtrx[13]*(Q_001001000*PR_000022000000+Q_001101000*PR_000022000010+Q_101001000*PR_000022000100+Q_101101000*PR_000022000110); ans_temp[ans_id*6+2]+=Pmtrx[14]*(Q_000002000*PR_000022000000+Q_000102000*PR_000022000010+Q_000202000*PR_000022000020); ans_temp[ans_id*6+2]+=Pmtrx[15]*(Q_001000001*PR_000022000000+Q_001000101*PR_000022000001+Q_101000001*PR_000022000100+Q_101000101*PR_000022000101); ans_temp[ans_id*6+2]+=Pmtrx[16]*(Q_000001001*PR_000022000000+Q_000001101*PR_000022000001+Q_000101001*PR_000022000010+Q_000101101*PR_000022000011); ans_temp[ans_id*6+2]+=Pmtrx[17]*(Q_000000002*PR_000022000000+Q_000000102*PR_000022000001+Q_000000202*PR_000022000002); ans_temp[ans_id*6+2]+=Pmtrx[18]*(Q_002000000*PR_001020001000+Q_102000000*PR_001020001100+Q_202000000*PR_001020001200); ans_temp[ans_id*6+2]+=Pmtrx[19]*(Q_001001000*PR_001020001000+Q_001101000*PR_001020001010+Q_101001000*PR_001020001100+Q_101101000*PR_001020001110); ans_temp[ans_id*6+2]+=Pmtrx[20]*(Q_000002000*PR_001020001000+Q_000102000*PR_001020001010+Q_000202000*PR_001020001020); ans_temp[ans_id*6+2]+=Pmtrx[21]*(Q_001000001*PR_001020001000+Q_001000101*PR_001020001001+Q_101000001*PR_001020001100+Q_101000101*PR_001020001101); ans_temp[ans_id*6+2]+=Pmtrx[22]*(Q_000001001*PR_001020001000+Q_000001101*PR_001020001001+Q_000101001*PR_001020001010+Q_000101101*PR_001020001011); ans_temp[ans_id*6+2]+=Pmtrx[23]*(Q_000000002*PR_001020001000+Q_000000102*PR_001020001001+Q_000000202*PR_001020001002); ans_temp[ans_id*6+2]+=Pmtrx[24]*(Q_002000000*PR_000021001000+Q_102000000*PR_000021001100+Q_202000000*PR_000021001200); ans_temp[ans_id*6+2]+=Pmtrx[25]*(Q_001001000*PR_000021001000+Q_001101000*PR_000021001010+Q_101001000*PR_000021001100+Q_101101000*PR_000021001110); ans_temp[ans_id*6+2]+=Pmtrx[26]*(Q_000002000*PR_000021001000+Q_000102000*PR_000021001010+Q_000202000*PR_000021001020); ans_temp[ans_id*6+2]+=Pmtrx[27]*(Q_001000001*PR_000021001000+Q_001000101*PR_000021001001+Q_101000001*PR_000021001100+Q_101000101*PR_000021001101); ans_temp[ans_id*6+2]+=Pmtrx[28]*(Q_000001001*PR_000021001000+Q_000001101*PR_000021001001+Q_000101001*PR_000021001010+Q_000101101*PR_000021001011); ans_temp[ans_id*6+2]+=Pmtrx[29]*(Q_000000002*PR_000021001000+Q_000000102*PR_000021001001+Q_000000202*PR_000021001002); ans_temp[ans_id*6+2]+=Pmtrx[30]*(Q_002000000*PR_000020002000+Q_102000000*PR_000020002100+Q_202000000*PR_000020002200); ans_temp[ans_id*6+2]+=Pmtrx[31]*(Q_001001000*PR_000020002000+Q_001101000*PR_000020002010+Q_101001000*PR_000020002100+Q_101101000*PR_000020002110); ans_temp[ans_id*6+2]+=Pmtrx[32]*(Q_000002000*PR_000020002000+Q_000102000*PR_000020002010+Q_000202000*PR_000020002020); ans_temp[ans_id*6+2]+=Pmtrx[33]*(Q_001000001*PR_000020002000+Q_001000101*PR_000020002001+Q_101000001*PR_000020002100+Q_101000101*PR_000020002101); ans_temp[ans_id*6+2]+=Pmtrx[34]*(Q_000001001*PR_000020002000+Q_000001101*PR_000020002001+Q_000101001*PR_000020002010+Q_000101101*PR_000020002011); ans_temp[ans_id*6+2]+=Pmtrx[35]*(Q_000000002*PR_000020002000+Q_000000102*PR_000020002001+Q_000000202*PR_000020002002); ans_temp[ans_id*6+3]+=Pmtrx[0]*(Q_002000000*PR_012000010000+Q_102000000*PR_012000010100+Q_202000000*PR_012000010200); ans_temp[ans_id*6+3]+=Pmtrx[1]*(Q_001001000*PR_012000010000+Q_001101000*PR_012000010010+Q_101001000*PR_012000010100+Q_101101000*PR_012000010110); ans_temp[ans_id*6+3]+=Pmtrx[2]*(Q_000002000*PR_012000010000+Q_000102000*PR_012000010010+Q_000202000*PR_012000010020); ans_temp[ans_id*6+3]+=Pmtrx[3]*(Q_001000001*PR_012000010000+Q_001000101*PR_012000010001+Q_101000001*PR_012000010100+Q_101000101*PR_012000010101); ans_temp[ans_id*6+3]+=Pmtrx[4]*(Q_000001001*PR_012000010000+Q_000001101*PR_012000010001+Q_000101001*PR_012000010010+Q_000101101*PR_012000010011); ans_temp[ans_id*6+3]+=Pmtrx[5]*(Q_000000002*PR_012000010000+Q_000000102*PR_012000010001+Q_000000202*PR_012000010002); ans_temp[ans_id*6+3]+=Pmtrx[6]*(Q_002000000*PR_011001010000+Q_102000000*PR_011001010100+Q_202000000*PR_011001010200); ans_temp[ans_id*6+3]+=Pmtrx[7]*(Q_001001000*PR_011001010000+Q_001101000*PR_011001010010+Q_101001000*PR_011001010100+Q_101101000*PR_011001010110); ans_temp[ans_id*6+3]+=Pmtrx[8]*(Q_000002000*PR_011001010000+Q_000102000*PR_011001010010+Q_000202000*PR_011001010020); ans_temp[ans_id*6+3]+=Pmtrx[9]*(Q_001000001*PR_011001010000+Q_001000101*PR_011001010001+Q_101000001*PR_011001010100+Q_101000101*PR_011001010101); ans_temp[ans_id*6+3]+=Pmtrx[10]*(Q_000001001*PR_011001010000+Q_000001101*PR_011001010001+Q_000101001*PR_011001010010+Q_000101101*PR_011001010011); ans_temp[ans_id*6+3]+=Pmtrx[11]*(Q_000000002*PR_011001010000+Q_000000102*PR_011001010001+Q_000000202*PR_011001010002); ans_temp[ans_id*6+3]+=Pmtrx[12]*(Q_002000000*PR_010002010000+Q_102000000*PR_010002010100+Q_202000000*PR_010002010200); ans_temp[ans_id*6+3]+=Pmtrx[13]*(Q_001001000*PR_010002010000+Q_001101000*PR_010002010010+Q_101001000*PR_010002010100+Q_101101000*PR_010002010110); ans_temp[ans_id*6+3]+=Pmtrx[14]*(Q_000002000*PR_010002010000+Q_000102000*PR_010002010010+Q_000202000*PR_010002010020); ans_temp[ans_id*6+3]+=Pmtrx[15]*(Q_001000001*PR_010002010000+Q_001000101*PR_010002010001+Q_101000001*PR_010002010100+Q_101000101*PR_010002010101); ans_temp[ans_id*6+3]+=Pmtrx[16]*(Q_000001001*PR_010002010000+Q_000001101*PR_010002010001+Q_000101001*PR_010002010010+Q_000101101*PR_010002010011); ans_temp[ans_id*6+3]+=Pmtrx[17]*(Q_000000002*PR_010002010000+Q_000000102*PR_010002010001+Q_000000202*PR_010002010002); ans_temp[ans_id*6+3]+=Pmtrx[18]*(Q_002000000*PR_011000011000+Q_102000000*PR_011000011100+Q_202000000*PR_011000011200); ans_temp[ans_id*6+3]+=Pmtrx[19]*(Q_001001000*PR_011000011000+Q_001101000*PR_011000011010+Q_101001000*PR_011000011100+Q_101101000*PR_011000011110); ans_temp[ans_id*6+3]+=Pmtrx[20]*(Q_000002000*PR_011000011000+Q_000102000*PR_011000011010+Q_000202000*PR_011000011020); ans_temp[ans_id*6+3]+=Pmtrx[21]*(Q_001000001*PR_011000011000+Q_001000101*PR_011000011001+Q_101000001*PR_011000011100+Q_101000101*PR_011000011101); ans_temp[ans_id*6+3]+=Pmtrx[22]*(Q_000001001*PR_011000011000+Q_000001101*PR_011000011001+Q_000101001*PR_011000011010+Q_000101101*PR_011000011011); ans_temp[ans_id*6+3]+=Pmtrx[23]*(Q_000000002*PR_011000011000+Q_000000102*PR_011000011001+Q_000000202*PR_011000011002); ans_temp[ans_id*6+3]+=Pmtrx[24]*(Q_002000000*PR_010001011000+Q_102000000*PR_010001011100+Q_202000000*PR_010001011200); ans_temp[ans_id*6+3]+=Pmtrx[25]*(Q_001001000*PR_010001011000+Q_001101000*PR_010001011010+Q_101001000*PR_010001011100+Q_101101000*PR_010001011110); ans_temp[ans_id*6+3]+=Pmtrx[26]*(Q_000002000*PR_010001011000+Q_000102000*PR_010001011010+Q_000202000*PR_010001011020); ans_temp[ans_id*6+3]+=Pmtrx[27]*(Q_001000001*PR_010001011000+Q_001000101*PR_010001011001+Q_101000001*PR_010001011100+Q_101000101*PR_010001011101); ans_temp[ans_id*6+3]+=Pmtrx[28]*(Q_000001001*PR_010001011000+Q_000001101*PR_010001011001+Q_000101001*PR_010001011010+Q_000101101*PR_010001011011); ans_temp[ans_id*6+3]+=Pmtrx[29]*(Q_000000002*PR_010001011000+Q_000000102*PR_010001011001+Q_000000202*PR_010001011002); ans_temp[ans_id*6+3]+=Pmtrx[30]*(Q_002000000*PR_010000012000+Q_102000000*PR_010000012100+Q_202000000*PR_010000012200); ans_temp[ans_id*6+3]+=Pmtrx[31]*(Q_001001000*PR_010000012000+Q_001101000*PR_010000012010+Q_101001000*PR_010000012100+Q_101101000*PR_010000012110); ans_temp[ans_id*6+3]+=Pmtrx[32]*(Q_000002000*PR_010000012000+Q_000102000*PR_010000012010+Q_000202000*PR_010000012020); ans_temp[ans_id*6+3]+=Pmtrx[33]*(Q_001000001*PR_010000012000+Q_001000101*PR_010000012001+Q_101000001*PR_010000012100+Q_101000101*PR_010000012101); ans_temp[ans_id*6+3]+=Pmtrx[34]*(Q_000001001*PR_010000012000+Q_000001101*PR_010000012001+Q_000101001*PR_010000012010+Q_000101101*PR_010000012011); ans_temp[ans_id*6+3]+=Pmtrx[35]*(Q_000000002*PR_010000012000+Q_000000102*PR_010000012001+Q_000000202*PR_010000012002); ans_temp[ans_id*6+4]+=Pmtrx[0]*(Q_002000000*PR_002010010000+Q_102000000*PR_002010010100+Q_202000000*PR_002010010200); ans_temp[ans_id*6+4]+=Pmtrx[1]*(Q_001001000*PR_002010010000+Q_001101000*PR_002010010010+Q_101001000*PR_002010010100+Q_101101000*PR_002010010110); ans_temp[ans_id*6+4]+=Pmtrx[2]*(Q_000002000*PR_002010010000+Q_000102000*PR_002010010010+Q_000202000*PR_002010010020); ans_temp[ans_id*6+4]+=Pmtrx[3]*(Q_001000001*PR_002010010000+Q_001000101*PR_002010010001+Q_101000001*PR_002010010100+Q_101000101*PR_002010010101); ans_temp[ans_id*6+4]+=Pmtrx[4]*(Q_000001001*PR_002010010000+Q_000001101*PR_002010010001+Q_000101001*PR_002010010010+Q_000101101*PR_002010010011); ans_temp[ans_id*6+4]+=Pmtrx[5]*(Q_000000002*PR_002010010000+Q_000000102*PR_002010010001+Q_000000202*PR_002010010002); ans_temp[ans_id*6+4]+=Pmtrx[6]*(Q_002000000*PR_001011010000+Q_102000000*PR_001011010100+Q_202000000*PR_001011010200); ans_temp[ans_id*6+4]+=Pmtrx[7]*(Q_001001000*PR_001011010000+Q_001101000*PR_001011010010+Q_101001000*PR_001011010100+Q_101101000*PR_001011010110); ans_temp[ans_id*6+4]+=Pmtrx[8]*(Q_000002000*PR_001011010000+Q_000102000*PR_001011010010+Q_000202000*PR_001011010020); ans_temp[ans_id*6+4]+=Pmtrx[9]*(Q_001000001*PR_001011010000+Q_001000101*PR_001011010001+Q_101000001*PR_001011010100+Q_101000101*PR_001011010101); ans_temp[ans_id*6+4]+=Pmtrx[10]*(Q_000001001*PR_001011010000+Q_000001101*PR_001011010001+Q_000101001*PR_001011010010+Q_000101101*PR_001011010011); ans_temp[ans_id*6+4]+=Pmtrx[11]*(Q_000000002*PR_001011010000+Q_000000102*PR_001011010001+Q_000000202*PR_001011010002); ans_temp[ans_id*6+4]+=Pmtrx[12]*(Q_002000000*PR_000012010000+Q_102000000*PR_000012010100+Q_202000000*PR_000012010200); ans_temp[ans_id*6+4]+=Pmtrx[13]*(Q_001001000*PR_000012010000+Q_001101000*PR_000012010010+Q_101001000*PR_000012010100+Q_101101000*PR_000012010110); ans_temp[ans_id*6+4]+=Pmtrx[14]*(Q_000002000*PR_000012010000+Q_000102000*PR_000012010010+Q_000202000*PR_000012010020); ans_temp[ans_id*6+4]+=Pmtrx[15]*(Q_001000001*PR_000012010000+Q_001000101*PR_000012010001+Q_101000001*PR_000012010100+Q_101000101*PR_000012010101); ans_temp[ans_id*6+4]+=Pmtrx[16]*(Q_000001001*PR_000012010000+Q_000001101*PR_000012010001+Q_000101001*PR_000012010010+Q_000101101*PR_000012010011); ans_temp[ans_id*6+4]+=Pmtrx[17]*(Q_000000002*PR_000012010000+Q_000000102*PR_000012010001+Q_000000202*PR_000012010002); ans_temp[ans_id*6+4]+=Pmtrx[18]*(Q_002000000*PR_001010011000+Q_102000000*PR_001010011100+Q_202000000*PR_001010011200); ans_temp[ans_id*6+4]+=Pmtrx[19]*(Q_001001000*PR_001010011000+Q_001101000*PR_001010011010+Q_101001000*PR_001010011100+Q_101101000*PR_001010011110); ans_temp[ans_id*6+4]+=Pmtrx[20]*(Q_000002000*PR_001010011000+Q_000102000*PR_001010011010+Q_000202000*PR_001010011020); ans_temp[ans_id*6+4]+=Pmtrx[21]*(Q_001000001*PR_001010011000+Q_001000101*PR_001010011001+Q_101000001*PR_001010011100+Q_101000101*PR_001010011101); ans_temp[ans_id*6+4]+=Pmtrx[22]*(Q_000001001*PR_001010011000+Q_000001101*PR_001010011001+Q_000101001*PR_001010011010+Q_000101101*PR_001010011011); ans_temp[ans_id*6+4]+=Pmtrx[23]*(Q_000000002*PR_001010011000+Q_000000102*PR_001010011001+Q_000000202*PR_001010011002); ans_temp[ans_id*6+4]+=Pmtrx[24]*(Q_002000000*PR_000011011000+Q_102000000*PR_000011011100+Q_202000000*PR_000011011200); ans_temp[ans_id*6+4]+=Pmtrx[25]*(Q_001001000*PR_000011011000+Q_001101000*PR_000011011010+Q_101001000*PR_000011011100+Q_101101000*PR_000011011110); ans_temp[ans_id*6+4]+=Pmtrx[26]*(Q_000002000*PR_000011011000+Q_000102000*PR_000011011010+Q_000202000*PR_000011011020); ans_temp[ans_id*6+4]+=Pmtrx[27]*(Q_001000001*PR_000011011000+Q_001000101*PR_000011011001+Q_101000001*PR_000011011100+Q_101000101*PR_000011011101); ans_temp[ans_id*6+4]+=Pmtrx[28]*(Q_000001001*PR_000011011000+Q_000001101*PR_000011011001+Q_000101001*PR_000011011010+Q_000101101*PR_000011011011); ans_temp[ans_id*6+4]+=Pmtrx[29]*(Q_000000002*PR_000011011000+Q_000000102*PR_000011011001+Q_000000202*PR_000011011002); ans_temp[ans_id*6+4]+=Pmtrx[30]*(Q_002000000*PR_000010012000+Q_102000000*PR_000010012100+Q_202000000*PR_000010012200); ans_temp[ans_id*6+4]+=Pmtrx[31]*(Q_001001000*PR_000010012000+Q_001101000*PR_000010012010+Q_101001000*PR_000010012100+Q_101101000*PR_000010012110); ans_temp[ans_id*6+4]+=Pmtrx[32]*(Q_000002000*PR_000010012000+Q_000102000*PR_000010012010+Q_000202000*PR_000010012020); ans_temp[ans_id*6+4]+=Pmtrx[33]*(Q_001000001*PR_000010012000+Q_001000101*PR_000010012001+Q_101000001*PR_000010012100+Q_101000101*PR_000010012101); ans_temp[ans_id*6+4]+=Pmtrx[34]*(Q_000001001*PR_000010012000+Q_000001101*PR_000010012001+Q_000101001*PR_000010012010+Q_000101101*PR_000010012011); ans_temp[ans_id*6+4]+=Pmtrx[35]*(Q_000000002*PR_000010012000+Q_000000102*PR_000010012001+Q_000000202*PR_000010012002); ans_temp[ans_id*6+5]+=Pmtrx[0]*(Q_002000000*PR_002000020000+Q_102000000*PR_002000020100+Q_202000000*PR_002000020200); ans_temp[ans_id*6+5]+=Pmtrx[1]*(Q_001001000*PR_002000020000+Q_001101000*PR_002000020010+Q_101001000*PR_002000020100+Q_101101000*PR_002000020110); ans_temp[ans_id*6+5]+=Pmtrx[2]*(Q_000002000*PR_002000020000+Q_000102000*PR_002000020010+Q_000202000*PR_002000020020); ans_temp[ans_id*6+5]+=Pmtrx[3]*(Q_001000001*PR_002000020000+Q_001000101*PR_002000020001+Q_101000001*PR_002000020100+Q_101000101*PR_002000020101); ans_temp[ans_id*6+5]+=Pmtrx[4]*(Q_000001001*PR_002000020000+Q_000001101*PR_002000020001+Q_000101001*PR_002000020010+Q_000101101*PR_002000020011); ans_temp[ans_id*6+5]+=Pmtrx[5]*(Q_000000002*PR_002000020000+Q_000000102*PR_002000020001+Q_000000202*PR_002000020002); ans_temp[ans_id*6+5]+=Pmtrx[6]*(Q_002000000*PR_001001020000+Q_102000000*PR_001001020100+Q_202000000*PR_001001020200); ans_temp[ans_id*6+5]+=Pmtrx[7]*(Q_001001000*PR_001001020000+Q_001101000*PR_001001020010+Q_101001000*PR_001001020100+Q_101101000*PR_001001020110); ans_temp[ans_id*6+5]+=Pmtrx[8]*(Q_000002000*PR_001001020000+Q_000102000*PR_001001020010+Q_000202000*PR_001001020020); ans_temp[ans_id*6+5]+=Pmtrx[9]*(Q_001000001*PR_001001020000+Q_001000101*PR_001001020001+Q_101000001*PR_001001020100+Q_101000101*PR_001001020101); ans_temp[ans_id*6+5]+=Pmtrx[10]*(Q_000001001*PR_001001020000+Q_000001101*PR_001001020001+Q_000101001*PR_001001020010+Q_000101101*PR_001001020011); ans_temp[ans_id*6+5]+=Pmtrx[11]*(Q_000000002*PR_001001020000+Q_000000102*PR_001001020001+Q_000000202*PR_001001020002); ans_temp[ans_id*6+5]+=Pmtrx[12]*(Q_002000000*PR_000002020000+Q_102000000*PR_000002020100+Q_202000000*PR_000002020200); ans_temp[ans_id*6+5]+=Pmtrx[13]*(Q_001001000*PR_000002020000+Q_001101000*PR_000002020010+Q_101001000*PR_000002020100+Q_101101000*PR_000002020110); ans_temp[ans_id*6+5]+=Pmtrx[14]*(Q_000002000*PR_000002020000+Q_000102000*PR_000002020010+Q_000202000*PR_000002020020); ans_temp[ans_id*6+5]+=Pmtrx[15]*(Q_001000001*PR_000002020000+Q_001000101*PR_000002020001+Q_101000001*PR_000002020100+Q_101000101*PR_000002020101); ans_temp[ans_id*6+5]+=Pmtrx[16]*(Q_000001001*PR_000002020000+Q_000001101*PR_000002020001+Q_000101001*PR_000002020010+Q_000101101*PR_000002020011); ans_temp[ans_id*6+5]+=Pmtrx[17]*(Q_000000002*PR_000002020000+Q_000000102*PR_000002020001+Q_000000202*PR_000002020002); ans_temp[ans_id*6+5]+=Pmtrx[18]*(Q_002000000*PR_001000021000+Q_102000000*PR_001000021100+Q_202000000*PR_001000021200); ans_temp[ans_id*6+5]+=Pmtrx[19]*(Q_001001000*PR_001000021000+Q_001101000*PR_001000021010+Q_101001000*PR_001000021100+Q_101101000*PR_001000021110); ans_temp[ans_id*6+5]+=Pmtrx[20]*(Q_000002000*PR_001000021000+Q_000102000*PR_001000021010+Q_000202000*PR_001000021020); ans_temp[ans_id*6+5]+=Pmtrx[21]*(Q_001000001*PR_001000021000+Q_001000101*PR_001000021001+Q_101000001*PR_001000021100+Q_101000101*PR_001000021101); ans_temp[ans_id*6+5]+=Pmtrx[22]*(Q_000001001*PR_001000021000+Q_000001101*PR_001000021001+Q_000101001*PR_001000021010+Q_000101101*PR_001000021011); ans_temp[ans_id*6+5]+=Pmtrx[23]*(Q_000000002*PR_001000021000+Q_000000102*PR_001000021001+Q_000000202*PR_001000021002); ans_temp[ans_id*6+5]+=Pmtrx[24]*(Q_002000000*PR_000001021000+Q_102000000*PR_000001021100+Q_202000000*PR_000001021200); ans_temp[ans_id*6+5]+=Pmtrx[25]*(Q_001001000*PR_000001021000+Q_001101000*PR_000001021010+Q_101001000*PR_000001021100+Q_101101000*PR_000001021110); ans_temp[ans_id*6+5]+=Pmtrx[26]*(Q_000002000*PR_000001021000+Q_000102000*PR_000001021010+Q_000202000*PR_000001021020); ans_temp[ans_id*6+5]+=Pmtrx[27]*(Q_001000001*PR_000001021000+Q_001000101*PR_000001021001+Q_101000001*PR_000001021100+Q_101000101*PR_000001021101); ans_temp[ans_id*6+5]+=Pmtrx[28]*(Q_000001001*PR_000001021000+Q_000001101*PR_000001021001+Q_000101001*PR_000001021010+Q_000101101*PR_000001021011); ans_temp[ans_id*6+5]+=Pmtrx[29]*(Q_000000002*PR_000001021000+Q_000000102*PR_000001021001+Q_000000202*PR_000001021002); ans_temp[ans_id*6+5]+=Pmtrx[30]*(Q_002000000*PR_000000022000+Q_102000000*PR_000000022100+Q_202000000*PR_000000022200); ans_temp[ans_id*6+5]+=Pmtrx[31]*(Q_001001000*PR_000000022000+Q_001101000*PR_000000022010+Q_101001000*PR_000000022100+Q_101101000*PR_000000022110); ans_temp[ans_id*6+5]+=Pmtrx[32]*(Q_000002000*PR_000000022000+Q_000102000*PR_000000022010+Q_000202000*PR_000000022020); ans_temp[ans_id*6+5]+=Pmtrx[33]*(Q_001000001*PR_000000022000+Q_001000101*PR_000000022001+Q_101000001*PR_000000022100+Q_101000101*PR_000000022101); ans_temp[ans_id*6+5]+=Pmtrx[34]*(Q_000001001*PR_000000022000+Q_000001101*PR_000000022001+Q_000101001*PR_000000022010+Q_000101101*PR_000000022011); ans_temp[ans_id*6+5]+=Pmtrx[35]*(Q_000000002*PR_000000022000+Q_000000102*PR_000000022001+Q_000000202*PR_000000022002); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } } __global__ void MD_Kq_ddsd_fs(unsigned int contrc_bra_num,unsigned int contrc_ket_num,\ unsigned int * contrc_bra_id,\ unsigned int * contrc_ket_id,\ unsigned int mtrx_len,\ double * Pmtrx_in,\ double * P,\ double * PA,\ double * PB,\ double * Zta_in,\ double * pp_in,\ float * K2_p_in,\ unsigned int * id_bra_in,\ double * Q,\ double * QC,\ double * QD,\ double * Eta_in,\ double * pq_in,\ float * K2_q_in,\ unsigned int * id_ket_in,\ double * ans){ unsigned int tId_x = threadIdx.x; unsigned int bId_x = blockIdx.x; unsigned int bId_y = blockIdx.y; unsigned int tdis = blockDim.x; unsigned int bdis_x = gridDim.x; unsigned int bdis_y = gridDim.y; unsigned int ans_id=tId_x; double Pmtrx[36]={0.0}; __shared__ double ans_temp[NTHREAD*6]; for(int i=0;i<6;i++){ ans_temp[i*tdis+tId_x]=0.0; } for(unsigned int i_contrc_bra=bId_x;i_contrc_bra<contrc_bra_num;i_contrc_bra+=bdis_x){ for(unsigned int j_contrc_ket=bId_y;j_contrc_ket<contrc_ket_num;j_contrc_ket+=bdis_y){ unsigned int primit_bra_start = contrc_bra_id[i_contrc_bra ]; unsigned int primit_bra_end = contrc_bra_id[i_contrc_bra+1]; unsigned int primit_ket_start = contrc_ket_id[j_contrc_ket ]; unsigned int primit_ket_end = contrc_ket_id[j_contrc_ket+1]; for(unsigned int ii=primit_bra_start;ii<primit_bra_end;ii++){ unsigned int id_bra=id_bra_in[ii]; double PX=P[ii*3+0]; double PY=P[ii*3+1]; double PZ=P[ii*3+2]; double Pd_010[3]; Pd_010[0]=PA[ii*3+0]; Pd_010[1]=PA[ii*3+1]; Pd_010[2]=PA[ii*3+2]; double Pd_001[3]; Pd_001[0]=PB[ii*3+0]; Pd_001[1]=PB[ii*3+1]; Pd_001[2]=PB[ii*3+2]; double Zta=Zta_in[ii]; double pp=pp_in[ii]; float K2_p=K2_p_in[ii]; double aPin1=1/(2*Zta); for(unsigned int j=tId_x;j<primit_ket_end-primit_ket_start;j+=tdis){ unsigned int jj=primit_ket_start+j; unsigned int id_ket=tex1Dfetch(tex_id_ket,jj); double P_max=0.0; for(int p_j=0;p_j<6;p_j++){ for(int p_i=0;p_i<6;p_i++){ Pmtrx[p_i*6+p_j]=Pmtrx_in[(id_ket+p_j)*mtrx_len+(id_bra+p_i)]; double temp_P=fabsf(Pmtrx[p_i*6+p_j]); if(temp_P>P_max) P_max=temp_P; } } float K2_q=tex1Dfetch(tex_K2_q,jj); if(fabsf(K2_p*K2_q)<1.0E-14){ break; } if(fabsf(P_max*K2_p*K2_q)<1.0E-14) continue; int2 temp_int2; temp_int2=tex1Dfetch(tex_Eta,jj); double Eta=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_pq,jj); double pq=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+0); double QX=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+1); double QY=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_Q,jj*3+2); double QZ=__hiloint2double(temp_int2.y,temp_int2.x); double Qd_001[3]; temp_int2=tex1Dfetch(tex_QD,jj*3+0); Qd_001[0]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+1); Qd_001[1]=__hiloint2double(temp_int2.y,temp_int2.x); temp_int2=tex1Dfetch(tex_QD,jj*3+2); Qd_001[2]=__hiloint2double(temp_int2.y,temp_int2.x); double alphaT=rsqrt(Eta+Zta); double lmd=4*P25*pp*pq*alphaT; alphaT=Eta*Zta*alphaT*alphaT; double TX=PX-QX; double TY=PY-QY; double TZ=PZ-QZ; double T=alphaT*(TX*TX+TY*TY+TZ*TZ); double R_000[7]; Ft_fs_6(6,T,R_000); R_000[0]*=lmd; R_000[1]*=-2*alphaT*lmd; R_000[2]*=4*alphaT*alphaT*lmd; R_000[3]*=-8*alphaT*alphaT*alphaT*lmd; R_000[4]*=16*alphaT*alphaT*alphaT*alphaT*lmd; R_000[5]*=-32*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; R_000[6]*=64*alphaT*alphaT*alphaT*alphaT*alphaT*alphaT*lmd; double aQin1=1/(2*Eta); double R_100[6]; double R_200[5]; double R_300[4]; double R_400[3]; double R_500[2]; double R_600[1]; double R_010[6]; double R_110[5]; double R_210[4]; double R_310[3]; double R_410[2]; double R_510[1]; double R_020[5]; double R_120[4]; double R_220[3]; double R_320[2]; double R_420[1]; double R_030[4]; double R_130[3]; double R_230[2]; double R_330[1]; double R_040[3]; double R_140[2]; double R_240[1]; double R_050[2]; double R_150[1]; double R_060[1]; double R_001[6]; double R_101[5]; double R_201[4]; double R_301[3]; double R_401[2]; double R_501[1]; double R_011[5]; double R_111[4]; double R_211[3]; double R_311[2]; double R_411[1]; double R_021[4]; double R_121[3]; double R_221[2]; double R_321[1]; double R_031[3]; double R_131[2]; double R_231[1]; double R_041[2]; double R_141[1]; double R_051[1]; double R_002[5]; double R_102[4]; double R_202[3]; double R_302[2]; double R_402[1]; double R_012[4]; double R_112[3]; double R_212[2]; double R_312[1]; double R_022[3]; double R_122[2]; double R_222[1]; double R_032[2]; double R_132[1]; double R_042[1]; double R_003[4]; double R_103[3]; double R_203[2]; double R_303[1]; double R_013[3]; double R_113[2]; double R_213[1]; double R_023[2]; double R_123[1]; double R_033[1]; double R_004[3]; double R_104[2]; double R_204[1]; double R_014[2]; double R_114[1]; double R_024[1]; double R_005[2]; double R_105[1]; double R_015[1]; double R_006[1]; for(int i=0;i<6;i++){ R_100[i]=TX*R_000[i+1]; } for(int i=0;i<6;i++){ R_010[i]=TY*R_000[i+1]; } for(int i=0;i<6;i++){ R_001[i]=TZ*R_000[i+1]; } for(int i=0;i<5;i++){ R_200[i]=TX*R_100[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_110[i]=TX*R_010[i+1]; } for(int i=0;i<5;i++){ R_020[i]=TY*R_010[i+1]+R_000[i+1]; } for(int i=0;i<5;i++){ R_101[i]=TX*R_001[i+1]; } for(int i=0;i<5;i++){ R_011[i]=TY*R_001[i+1]; } for(int i=0;i<5;i++){ R_002[i]=TZ*R_001[i+1]+R_000[i+1]; } for(int i=0;i<4;i++){ R_300[i]=TX*R_200[i+1]+2*R_100[i+1]; } for(int i=0;i<4;i++){ R_210[i]=TY*R_200[i+1]; } for(int i=0;i<4;i++){ R_120[i]=TX*R_020[i+1]; } for(int i=0;i<4;i++){ R_030[i]=TY*R_020[i+1]+2*R_010[i+1]; } for(int i=0;i<4;i++){ R_201[i]=TZ*R_200[i+1]; } for(int i=0;i<4;i++){ R_111[i]=TX*R_011[i+1]; } for(int i=0;i<4;i++){ R_021[i]=TZ*R_020[i+1]; } for(int i=0;i<4;i++){ R_102[i]=TX*R_002[i+1]; } for(int i=0;i<4;i++){ R_012[i]=TY*R_002[i+1]; } for(int i=0;i<4;i++){ R_003[i]=TZ*R_002[i+1]+2*R_001[i+1]; } for(int i=0;i<3;i++){ R_400[i]=TX*R_300[i+1]+3*R_200[i+1]; } for(int i=0;i<3;i++){ R_310[i]=TY*R_300[i+1]; } for(int i=0;i<3;i++){ R_220[i]=TX*R_120[i+1]+R_020[i+1]; } for(int i=0;i<3;i++){ R_130[i]=TX*R_030[i+1]; } for(int i=0;i<3;i++){ R_040[i]=TY*R_030[i+1]+3*R_020[i+1]; } for(int i=0;i<3;i++){ R_301[i]=TZ*R_300[i+1]; } for(int i=0;i<3;i++){ R_211[i]=TY*R_201[i+1]; } for(int i=0;i<3;i++){ R_121[i]=TX*R_021[i+1]; } for(int i=0;i<3;i++){ R_031[i]=TZ*R_030[i+1]; } for(int i=0;i<3;i++){ R_202[i]=TX*R_102[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_112[i]=TX*R_012[i+1]; } for(int i=0;i<3;i++){ R_022[i]=TY*R_012[i+1]+R_002[i+1]; } for(int i=0;i<3;i++){ R_103[i]=TX*R_003[i+1]; } for(int i=0;i<3;i++){ R_013[i]=TY*R_003[i+1]; } for(int i=0;i<3;i++){ R_004[i]=TZ*R_003[i+1]+3*R_002[i+1]; } for(int i=0;i<2;i++){ R_500[i]=TX*R_400[i+1]+4*R_300[i+1]; } for(int i=0;i<2;i++){ R_410[i]=TY*R_400[i+1]; } for(int i=0;i<2;i++){ R_320[i]=TX*R_220[i+1]+2*R_120[i+1]; } for(int i=0;i<2;i++){ R_230[i]=TY*R_220[i+1]+2*R_210[i+1]; } for(int i=0;i<2;i++){ R_140[i]=TX*R_040[i+1]; } for(int i=0;i<2;i++){ R_050[i]=TY*R_040[i+1]+4*R_030[i+1]; } for(int i=0;i<2;i++){ R_401[i]=TZ*R_400[i+1]; } for(int i=0;i<2;i++){ R_311[i]=TY*R_301[i+1]; } for(int i=0;i<2;i++){ R_221[i]=TZ*R_220[i+1]; } for(int i=0;i<2;i++){ R_131[i]=TX*R_031[i+1]; } for(int i=0;i<2;i++){ R_041[i]=TZ*R_040[i+1]; } for(int i=0;i<2;i++){ R_302[i]=TX*R_202[i+1]+2*R_102[i+1]; } for(int i=0;i<2;i++){ R_212[i]=TY*R_202[i+1]; } for(int i=0;i<2;i++){ R_122[i]=TX*R_022[i+1]; } for(int i=0;i<2;i++){ R_032[i]=TY*R_022[i+1]+2*R_012[i+1]; } for(int i=0;i<2;i++){ R_203[i]=TZ*R_202[i+1]+2*R_201[i+1]; } for(int i=0;i<2;i++){ R_113[i]=TX*R_013[i+1]; } for(int i=0;i<2;i++){ R_023[i]=TZ*R_022[i+1]+2*R_021[i+1]; } for(int i=0;i<2;i++){ R_104[i]=TX*R_004[i+1]; } for(int i=0;i<2;i++){ R_014[i]=TY*R_004[i+1]; } for(int i=0;i<2;i++){ R_005[i]=TZ*R_004[i+1]+4*R_003[i+1]; } for(int i=0;i<1;i++){ R_600[i]=TX*R_500[i+1]+5*R_400[i+1]; } for(int i=0;i<1;i++){ R_510[i]=TY*R_500[i+1]; } for(int i=0;i<1;i++){ R_420[i]=TX*R_320[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_330[i]=TX*R_230[i+1]+2*R_130[i+1]; } for(int i=0;i<1;i++){ R_240[i]=TY*R_230[i+1]+3*R_220[i+1]; } for(int i=0;i<1;i++){ R_150[i]=TX*R_050[i+1]; } for(int i=0;i<1;i++){ R_060[i]=TY*R_050[i+1]+5*R_040[i+1]; } for(int i=0;i<1;i++){ R_501[i]=TZ*R_500[i+1]; } for(int i=0;i<1;i++){ R_411[i]=TY*R_401[i+1]; } for(int i=0;i<1;i++){ R_321[i]=TZ*R_320[i+1]; } for(int i=0;i<1;i++){ R_231[i]=TZ*R_230[i+1]; } for(int i=0;i<1;i++){ R_141[i]=TX*R_041[i+1]; } for(int i=0;i<1;i++){ R_051[i]=TZ*R_050[i+1]; } for(int i=0;i<1;i++){ R_402[i]=TX*R_302[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_312[i]=TY*R_302[i+1]; } for(int i=0;i<1;i++){ R_222[i]=TX*R_122[i+1]+R_022[i+1]; } for(int i=0;i<1;i++){ R_132[i]=TX*R_032[i+1]; } for(int i=0;i<1;i++){ R_042[i]=TY*R_032[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_303[i]=TX*R_203[i+1]+2*R_103[i+1]; } for(int i=0;i<1;i++){ R_213[i]=TY*R_203[i+1]; } for(int i=0;i<1;i++){ R_123[i]=TX*R_023[i+1]; } for(int i=0;i<1;i++){ R_033[i]=TY*R_023[i+1]+2*R_013[i+1]; } for(int i=0;i<1;i++){ R_204[i]=TZ*R_203[i+1]+3*R_202[i+1]; } for(int i=0;i<1;i++){ R_114[i]=TX*R_014[i+1]; } for(int i=0;i<1;i++){ R_024[i]=TZ*R_023[i+1]+3*R_022[i+1]; } for(int i=0;i<1;i++){ R_105[i]=TX*R_005[i+1]; } for(int i=0;i<1;i++){ R_015[i]=TY*R_005[i+1]; } for(int i=0;i<1;i++){ R_006[i]=TZ*R_005[i+1]+5*R_004[i+1]; } double Qd_101[3]; double Qd_002[3]; double Qd_102[3]; double Qd_202[3]; for(int i=0;i<3;i++){ Qd_101[i]=aQin1; } for(int i=0;i<3;i++){ Qd_002[i]=Qd_101[i]+Qd_001[i]*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_102[i]=Qd_001[i]*Qd_101[i]+aQin1*Qd_001[i]; } for(int i=0;i<3;i++){ Qd_202[i]=aQin1*Qd_101[i]; } double Q_002000000=Qd_002[0]; double Q_102000000=Qd_102[0]; double Q_202000000=Qd_202[0]; double Q_001001000=Qd_001[0]*Qd_001[1]; double Q_001101000=Qd_001[0]*Qd_101[1]; double Q_101001000=Qd_101[0]*Qd_001[1]; double Q_101101000=Qd_101[0]*Qd_101[1]; double Q_000002000=Qd_002[1]; double Q_000102000=Qd_102[1]; double Q_000202000=Qd_202[1]; double Q_001000001=Qd_001[0]*Qd_001[2]; double Q_001000101=Qd_001[0]*Qd_101[2]; double Q_101000001=Qd_101[0]*Qd_001[2]; double Q_101000101=Qd_101[0]*Qd_101[2]; double Q_000001001=Qd_001[1]*Qd_001[2]; double Q_000001101=Qd_001[1]*Qd_101[2]; double Q_000101001=Qd_101[1]*Qd_001[2]; double Q_000101101=Qd_101[1]*Qd_101[2]; double Q_000000002=Qd_002[2]; double Q_000000102=Qd_102[2]; double Q_000000202=Qd_202[2]; double QR_002000000000=Q_002000000*R_000[0]+-1*Q_102000000*R_100[0]+Q_202000000*R_200[0]; double QR_001001000000=Q_001001000*R_000[0]+-1*Q_001101000*R_010[0]+-1*Q_101001000*R_100[0]+Q_101101000*R_110[0]; double QR_000002000000=Q_000002000*R_000[0]+-1*Q_000102000*R_010[0]+Q_000202000*R_020[0]; double QR_001000001000=Q_001000001*R_000[0]+-1*Q_001000101*R_001[0]+-1*Q_101000001*R_100[0]+Q_101000101*R_101[0]; double QR_000001001000=Q_000001001*R_000[0]+-1*Q_000001101*R_001[0]+-1*Q_000101001*R_010[0]+Q_000101101*R_011[0]; double QR_000000002000=Q_000000002*R_000[0]+-1*Q_000000102*R_001[0]+Q_000000202*R_002[0]; double QR_002000000001=Q_002000000*R_001[0]+-1*Q_102000000*R_101[0]+Q_202000000*R_201[0]; double QR_001001000001=Q_001001000*R_001[0]+-1*Q_001101000*R_011[0]+-1*Q_101001000*R_101[0]+Q_101101000*R_111[0]; double QR_000002000001=Q_000002000*R_001[0]+-1*Q_000102000*R_011[0]+Q_000202000*R_021[0]; double QR_001000001001=Q_001000001*R_001[0]+-1*Q_001000101*R_002[0]+-1*Q_101000001*R_101[0]+Q_101000101*R_102[0]; double QR_000001001001=Q_000001001*R_001[0]+-1*Q_000001101*R_002[0]+-1*Q_000101001*R_011[0]+Q_000101101*R_012[0]; double QR_000000002001=Q_000000002*R_001[0]+-1*Q_000000102*R_002[0]+Q_000000202*R_003[0]; double QR_002000000010=Q_002000000*R_010[0]+-1*Q_102000000*R_110[0]+Q_202000000*R_210[0]; double QR_001001000010=Q_001001000*R_010[0]+-1*Q_001101000*R_020[0]+-1*Q_101001000*R_110[0]+Q_101101000*R_120[0]; double QR_000002000010=Q_000002000*R_010[0]+-1*Q_000102000*R_020[0]+Q_000202000*R_030[0]; double QR_001000001010=Q_001000001*R_010[0]+-1*Q_001000101*R_011[0]+-1*Q_101000001*R_110[0]+Q_101000101*R_111[0]; double QR_000001001010=Q_000001001*R_010[0]+-1*Q_000001101*R_011[0]+-1*Q_000101001*R_020[0]+Q_000101101*R_021[0]; double QR_000000002010=Q_000000002*R_010[0]+-1*Q_000000102*R_011[0]+Q_000000202*R_012[0]; double QR_002000000100=Q_002000000*R_100[0]+-1*Q_102000000*R_200[0]+Q_202000000*R_300[0]; double QR_001001000100=Q_001001000*R_100[0]+-1*Q_001101000*R_110[0]+-1*Q_101001000*R_200[0]+Q_101101000*R_210[0]; double QR_000002000100=Q_000002000*R_100[0]+-1*Q_000102000*R_110[0]+Q_000202000*R_120[0]; double QR_001000001100=Q_001000001*R_100[0]+-1*Q_001000101*R_101[0]+-1*Q_101000001*R_200[0]+Q_101000101*R_201[0]; double QR_000001001100=Q_000001001*R_100[0]+-1*Q_000001101*R_101[0]+-1*Q_000101001*R_110[0]+Q_000101101*R_111[0]; double QR_000000002100=Q_000000002*R_100[0]+-1*Q_000000102*R_101[0]+Q_000000202*R_102[0]; double QR_002000000002=Q_002000000*R_002[0]+-1*Q_102000000*R_102[0]+Q_202000000*R_202[0]; double QR_001001000002=Q_001001000*R_002[0]+-1*Q_001101000*R_012[0]+-1*Q_101001000*R_102[0]+Q_101101000*R_112[0]; double QR_000002000002=Q_000002000*R_002[0]+-1*Q_000102000*R_012[0]+Q_000202000*R_022[0]; double QR_001000001002=Q_001000001*R_002[0]+-1*Q_001000101*R_003[0]+-1*Q_101000001*R_102[0]+Q_101000101*R_103[0]; double QR_000001001002=Q_000001001*R_002[0]+-1*Q_000001101*R_003[0]+-1*Q_000101001*R_012[0]+Q_000101101*R_013[0]; double QR_000000002002=Q_000000002*R_002[0]+-1*Q_000000102*R_003[0]+Q_000000202*R_004[0]; double QR_002000000011=Q_002000000*R_011[0]+-1*Q_102000000*R_111[0]+Q_202000000*R_211[0]; double QR_001001000011=Q_001001000*R_011[0]+-1*Q_001101000*R_021[0]+-1*Q_101001000*R_111[0]+Q_101101000*R_121[0]; double QR_000002000011=Q_000002000*R_011[0]+-1*Q_000102000*R_021[0]+Q_000202000*R_031[0]; double QR_001000001011=Q_001000001*R_011[0]+-1*Q_001000101*R_012[0]+-1*Q_101000001*R_111[0]+Q_101000101*R_112[0]; double QR_000001001011=Q_000001001*R_011[0]+-1*Q_000001101*R_012[0]+-1*Q_000101001*R_021[0]+Q_000101101*R_022[0]; double QR_000000002011=Q_000000002*R_011[0]+-1*Q_000000102*R_012[0]+Q_000000202*R_013[0]; double QR_002000000020=Q_002000000*R_020[0]+-1*Q_102000000*R_120[0]+Q_202000000*R_220[0]; double QR_001001000020=Q_001001000*R_020[0]+-1*Q_001101000*R_030[0]+-1*Q_101001000*R_120[0]+Q_101101000*R_130[0]; double QR_000002000020=Q_000002000*R_020[0]+-1*Q_000102000*R_030[0]+Q_000202000*R_040[0]; double QR_001000001020=Q_001000001*R_020[0]+-1*Q_001000101*R_021[0]+-1*Q_101000001*R_120[0]+Q_101000101*R_121[0]; double QR_000001001020=Q_000001001*R_020[0]+-1*Q_000001101*R_021[0]+-1*Q_000101001*R_030[0]+Q_000101101*R_031[0]; double QR_000000002020=Q_000000002*R_020[0]+-1*Q_000000102*R_021[0]+Q_000000202*R_022[0]; double QR_002000000101=Q_002000000*R_101[0]+-1*Q_102000000*R_201[0]+Q_202000000*R_301[0]; double QR_001001000101=Q_001001000*R_101[0]+-1*Q_001101000*R_111[0]+-1*Q_101001000*R_201[0]+Q_101101000*R_211[0]; double QR_000002000101=Q_000002000*R_101[0]+-1*Q_000102000*R_111[0]+Q_000202000*R_121[0]; double QR_001000001101=Q_001000001*R_101[0]+-1*Q_001000101*R_102[0]+-1*Q_101000001*R_201[0]+Q_101000101*R_202[0]; double QR_000001001101=Q_000001001*R_101[0]+-1*Q_000001101*R_102[0]+-1*Q_000101001*R_111[0]+Q_000101101*R_112[0]; double QR_000000002101=Q_000000002*R_101[0]+-1*Q_000000102*R_102[0]+Q_000000202*R_103[0]; double QR_002000000110=Q_002000000*R_110[0]+-1*Q_102000000*R_210[0]+Q_202000000*R_310[0]; double QR_001001000110=Q_001001000*R_110[0]+-1*Q_001101000*R_120[0]+-1*Q_101001000*R_210[0]+Q_101101000*R_220[0]; double QR_000002000110=Q_000002000*R_110[0]+-1*Q_000102000*R_120[0]+Q_000202000*R_130[0]; double QR_001000001110=Q_001000001*R_110[0]+-1*Q_001000101*R_111[0]+-1*Q_101000001*R_210[0]+Q_101000101*R_211[0]; double QR_000001001110=Q_000001001*R_110[0]+-1*Q_000001101*R_111[0]+-1*Q_000101001*R_120[0]+Q_000101101*R_121[0]; double QR_000000002110=Q_000000002*R_110[0]+-1*Q_000000102*R_111[0]+Q_000000202*R_112[0]; double QR_002000000200=Q_002000000*R_200[0]+-1*Q_102000000*R_300[0]+Q_202000000*R_400[0]; double QR_001001000200=Q_001001000*R_200[0]+-1*Q_001101000*R_210[0]+-1*Q_101001000*R_300[0]+Q_101101000*R_310[0]; double QR_000002000200=Q_000002000*R_200[0]+-1*Q_000102000*R_210[0]+Q_000202000*R_220[0]; double QR_001000001200=Q_001000001*R_200[0]+-1*Q_001000101*R_201[0]+-1*Q_101000001*R_300[0]+Q_101000101*R_301[0]; double QR_000001001200=Q_000001001*R_200[0]+-1*Q_000001101*R_201[0]+-1*Q_000101001*R_210[0]+Q_000101101*R_211[0]; double QR_000000002200=Q_000000002*R_200[0]+-1*Q_000000102*R_201[0]+Q_000000202*R_202[0]; double QR_002000000003=Q_002000000*R_003[0]+-1*Q_102000000*R_103[0]+Q_202000000*R_203[0]; double QR_001001000003=Q_001001000*R_003[0]+-1*Q_001101000*R_013[0]+-1*Q_101001000*R_103[0]+Q_101101000*R_113[0]; double QR_000002000003=Q_000002000*R_003[0]+-1*Q_000102000*R_013[0]+Q_000202000*R_023[0]; double QR_001000001003=Q_001000001*R_003[0]+-1*Q_001000101*R_004[0]+-1*Q_101000001*R_103[0]+Q_101000101*R_104[0]; double QR_000001001003=Q_000001001*R_003[0]+-1*Q_000001101*R_004[0]+-1*Q_000101001*R_013[0]+Q_000101101*R_014[0]; double QR_000000002003=Q_000000002*R_003[0]+-1*Q_000000102*R_004[0]+Q_000000202*R_005[0]; double QR_002000000012=Q_002000000*R_012[0]+-1*Q_102000000*R_112[0]+Q_202000000*R_212[0]; double QR_001001000012=Q_001001000*R_012[0]+-1*Q_001101000*R_022[0]+-1*Q_101001000*R_112[0]+Q_101101000*R_122[0]; double QR_000002000012=Q_000002000*R_012[0]+-1*Q_000102000*R_022[0]+Q_000202000*R_032[0]; double QR_001000001012=Q_001000001*R_012[0]+-1*Q_001000101*R_013[0]+-1*Q_101000001*R_112[0]+Q_101000101*R_113[0]; double QR_000001001012=Q_000001001*R_012[0]+-1*Q_000001101*R_013[0]+-1*Q_000101001*R_022[0]+Q_000101101*R_023[0]; double QR_000000002012=Q_000000002*R_012[0]+-1*Q_000000102*R_013[0]+Q_000000202*R_014[0]; double QR_002000000021=Q_002000000*R_021[0]+-1*Q_102000000*R_121[0]+Q_202000000*R_221[0]; double QR_001001000021=Q_001001000*R_021[0]+-1*Q_001101000*R_031[0]+-1*Q_101001000*R_121[0]+Q_101101000*R_131[0]; double QR_000002000021=Q_000002000*R_021[0]+-1*Q_000102000*R_031[0]+Q_000202000*R_041[0]; double QR_001000001021=Q_001000001*R_021[0]+-1*Q_001000101*R_022[0]+-1*Q_101000001*R_121[0]+Q_101000101*R_122[0]; double QR_000001001021=Q_000001001*R_021[0]+-1*Q_000001101*R_022[0]+-1*Q_000101001*R_031[0]+Q_000101101*R_032[0]; double QR_000000002021=Q_000000002*R_021[0]+-1*Q_000000102*R_022[0]+Q_000000202*R_023[0]; double QR_002000000030=Q_002000000*R_030[0]+-1*Q_102000000*R_130[0]+Q_202000000*R_230[0]; double QR_001001000030=Q_001001000*R_030[0]+-1*Q_001101000*R_040[0]+-1*Q_101001000*R_130[0]+Q_101101000*R_140[0]; double QR_000002000030=Q_000002000*R_030[0]+-1*Q_000102000*R_040[0]+Q_000202000*R_050[0]; double QR_001000001030=Q_001000001*R_030[0]+-1*Q_001000101*R_031[0]+-1*Q_101000001*R_130[0]+Q_101000101*R_131[0]; double QR_000001001030=Q_000001001*R_030[0]+-1*Q_000001101*R_031[0]+-1*Q_000101001*R_040[0]+Q_000101101*R_041[0]; double QR_000000002030=Q_000000002*R_030[0]+-1*Q_000000102*R_031[0]+Q_000000202*R_032[0]; double QR_002000000102=Q_002000000*R_102[0]+-1*Q_102000000*R_202[0]+Q_202000000*R_302[0]; double QR_001001000102=Q_001001000*R_102[0]+-1*Q_001101000*R_112[0]+-1*Q_101001000*R_202[0]+Q_101101000*R_212[0]; double QR_000002000102=Q_000002000*R_102[0]+-1*Q_000102000*R_112[0]+Q_000202000*R_122[0]; double QR_001000001102=Q_001000001*R_102[0]+-1*Q_001000101*R_103[0]+-1*Q_101000001*R_202[0]+Q_101000101*R_203[0]; double QR_000001001102=Q_000001001*R_102[0]+-1*Q_000001101*R_103[0]+-1*Q_000101001*R_112[0]+Q_000101101*R_113[0]; double QR_000000002102=Q_000000002*R_102[0]+-1*Q_000000102*R_103[0]+Q_000000202*R_104[0]; double QR_002000000111=Q_002000000*R_111[0]+-1*Q_102000000*R_211[0]+Q_202000000*R_311[0]; double QR_001001000111=Q_001001000*R_111[0]+-1*Q_001101000*R_121[0]+-1*Q_101001000*R_211[0]+Q_101101000*R_221[0]; double QR_000002000111=Q_000002000*R_111[0]+-1*Q_000102000*R_121[0]+Q_000202000*R_131[0]; double QR_001000001111=Q_001000001*R_111[0]+-1*Q_001000101*R_112[0]+-1*Q_101000001*R_211[0]+Q_101000101*R_212[0]; double QR_000001001111=Q_000001001*R_111[0]+-1*Q_000001101*R_112[0]+-1*Q_000101001*R_121[0]+Q_000101101*R_122[0]; double QR_000000002111=Q_000000002*R_111[0]+-1*Q_000000102*R_112[0]+Q_000000202*R_113[0]; double QR_002000000120=Q_002000000*R_120[0]+-1*Q_102000000*R_220[0]+Q_202000000*R_320[0]; double QR_001001000120=Q_001001000*R_120[0]+-1*Q_001101000*R_130[0]+-1*Q_101001000*R_220[0]+Q_101101000*R_230[0]; double QR_000002000120=Q_000002000*R_120[0]+-1*Q_000102000*R_130[0]+Q_000202000*R_140[0]; double QR_001000001120=Q_001000001*R_120[0]+-1*Q_001000101*R_121[0]+-1*Q_101000001*R_220[0]+Q_101000101*R_221[0]; double QR_000001001120=Q_000001001*R_120[0]+-1*Q_000001101*R_121[0]+-1*Q_000101001*R_130[0]+Q_000101101*R_131[0]; double QR_000000002120=Q_000000002*R_120[0]+-1*Q_000000102*R_121[0]+Q_000000202*R_122[0]; double QR_002000000201=Q_002000000*R_201[0]+-1*Q_102000000*R_301[0]+Q_202000000*R_401[0]; double QR_001001000201=Q_001001000*R_201[0]+-1*Q_001101000*R_211[0]+-1*Q_101001000*R_301[0]+Q_101101000*R_311[0]; double QR_000002000201=Q_000002000*R_201[0]+-1*Q_000102000*R_211[0]+Q_000202000*R_221[0]; double QR_001000001201=Q_001000001*R_201[0]+-1*Q_001000101*R_202[0]+-1*Q_101000001*R_301[0]+Q_101000101*R_302[0]; double QR_000001001201=Q_000001001*R_201[0]+-1*Q_000001101*R_202[0]+-1*Q_000101001*R_211[0]+Q_000101101*R_212[0]; double QR_000000002201=Q_000000002*R_201[0]+-1*Q_000000102*R_202[0]+Q_000000202*R_203[0]; double QR_002000000210=Q_002000000*R_210[0]+-1*Q_102000000*R_310[0]+Q_202000000*R_410[0]; double QR_001001000210=Q_001001000*R_210[0]+-1*Q_001101000*R_220[0]+-1*Q_101001000*R_310[0]+Q_101101000*R_320[0]; double QR_000002000210=Q_000002000*R_210[0]+-1*Q_000102000*R_220[0]+Q_000202000*R_230[0]; double QR_001000001210=Q_001000001*R_210[0]+-1*Q_001000101*R_211[0]+-1*Q_101000001*R_310[0]+Q_101000101*R_311[0]; double QR_000001001210=Q_000001001*R_210[0]+-1*Q_000001101*R_211[0]+-1*Q_000101001*R_220[0]+Q_000101101*R_221[0]; double QR_000000002210=Q_000000002*R_210[0]+-1*Q_000000102*R_211[0]+Q_000000202*R_212[0]; double QR_002000000300=Q_002000000*R_300[0]+-1*Q_102000000*R_400[0]+Q_202000000*R_500[0]; double QR_001001000300=Q_001001000*R_300[0]+-1*Q_001101000*R_310[0]+-1*Q_101001000*R_400[0]+Q_101101000*R_410[0]; double QR_000002000300=Q_000002000*R_300[0]+-1*Q_000102000*R_310[0]+Q_000202000*R_320[0]; double QR_001000001300=Q_001000001*R_300[0]+-1*Q_001000101*R_301[0]+-1*Q_101000001*R_400[0]+Q_101000101*R_401[0]; double QR_000001001300=Q_000001001*R_300[0]+-1*Q_000001101*R_301[0]+-1*Q_000101001*R_310[0]+Q_000101101*R_311[0]; double QR_000000002300=Q_000000002*R_300[0]+-1*Q_000000102*R_301[0]+Q_000000202*R_302[0]; double QR_002000000004=Q_002000000*R_004[0]+-1*Q_102000000*R_104[0]+Q_202000000*R_204[0]; double QR_001001000004=Q_001001000*R_004[0]+-1*Q_001101000*R_014[0]+-1*Q_101001000*R_104[0]+Q_101101000*R_114[0]; double QR_000002000004=Q_000002000*R_004[0]+-1*Q_000102000*R_014[0]+Q_000202000*R_024[0]; double QR_001000001004=Q_001000001*R_004[0]+-1*Q_001000101*R_005[0]+-1*Q_101000001*R_104[0]+Q_101000101*R_105[0]; double QR_000001001004=Q_000001001*R_004[0]+-1*Q_000001101*R_005[0]+-1*Q_000101001*R_014[0]+Q_000101101*R_015[0]; double QR_000000002004=Q_000000002*R_004[0]+-1*Q_000000102*R_005[0]+Q_000000202*R_006[0]; double QR_002000000013=Q_002000000*R_013[0]+-1*Q_102000000*R_113[0]+Q_202000000*R_213[0]; double QR_001001000013=Q_001001000*R_013[0]+-1*Q_001101000*R_023[0]+-1*Q_101001000*R_113[0]+Q_101101000*R_123[0]; double QR_000002000013=Q_000002000*R_013[0]+-1*Q_000102000*R_023[0]+Q_000202000*R_033[0]; double QR_001000001013=Q_001000001*R_013[0]+-1*Q_001000101*R_014[0]+-1*Q_101000001*R_113[0]+Q_101000101*R_114[0]; double QR_000001001013=Q_000001001*R_013[0]+-1*Q_000001101*R_014[0]+-1*Q_000101001*R_023[0]+Q_000101101*R_024[0]; double QR_000000002013=Q_000000002*R_013[0]+-1*Q_000000102*R_014[0]+Q_000000202*R_015[0]; double QR_002000000022=Q_002000000*R_022[0]+-1*Q_102000000*R_122[0]+Q_202000000*R_222[0]; double QR_001001000022=Q_001001000*R_022[0]+-1*Q_001101000*R_032[0]+-1*Q_101001000*R_122[0]+Q_101101000*R_132[0]; double QR_000002000022=Q_000002000*R_022[0]+-1*Q_000102000*R_032[0]+Q_000202000*R_042[0]; double QR_001000001022=Q_001000001*R_022[0]+-1*Q_001000101*R_023[0]+-1*Q_101000001*R_122[0]+Q_101000101*R_123[0]; double QR_000001001022=Q_000001001*R_022[0]+-1*Q_000001101*R_023[0]+-1*Q_000101001*R_032[0]+Q_000101101*R_033[0]; double QR_000000002022=Q_000000002*R_022[0]+-1*Q_000000102*R_023[0]+Q_000000202*R_024[0]; double QR_002000000031=Q_002000000*R_031[0]+-1*Q_102000000*R_131[0]+Q_202000000*R_231[0]; double QR_001001000031=Q_001001000*R_031[0]+-1*Q_001101000*R_041[0]+-1*Q_101001000*R_131[0]+Q_101101000*R_141[0]; double QR_000002000031=Q_000002000*R_031[0]+-1*Q_000102000*R_041[0]+Q_000202000*R_051[0]; double QR_001000001031=Q_001000001*R_031[0]+-1*Q_001000101*R_032[0]+-1*Q_101000001*R_131[0]+Q_101000101*R_132[0]; double QR_000001001031=Q_000001001*R_031[0]+-1*Q_000001101*R_032[0]+-1*Q_000101001*R_041[0]+Q_000101101*R_042[0]; double QR_000000002031=Q_000000002*R_031[0]+-1*Q_000000102*R_032[0]+Q_000000202*R_033[0]; double QR_002000000040=Q_002000000*R_040[0]+-1*Q_102000000*R_140[0]+Q_202000000*R_240[0]; double QR_001001000040=Q_001001000*R_040[0]+-1*Q_001101000*R_050[0]+-1*Q_101001000*R_140[0]+Q_101101000*R_150[0]; double QR_000002000040=Q_000002000*R_040[0]+-1*Q_000102000*R_050[0]+Q_000202000*R_060[0]; double QR_001000001040=Q_001000001*R_040[0]+-1*Q_001000101*R_041[0]+-1*Q_101000001*R_140[0]+Q_101000101*R_141[0]; double QR_000001001040=Q_000001001*R_040[0]+-1*Q_000001101*R_041[0]+-1*Q_000101001*R_050[0]+Q_000101101*R_051[0]; double QR_000000002040=Q_000000002*R_040[0]+-1*Q_000000102*R_041[0]+Q_000000202*R_042[0]; double QR_002000000103=Q_002000000*R_103[0]+-1*Q_102000000*R_203[0]+Q_202000000*R_303[0]; double QR_001001000103=Q_001001000*R_103[0]+-1*Q_001101000*R_113[0]+-1*Q_101001000*R_203[0]+Q_101101000*R_213[0]; double QR_000002000103=Q_000002000*R_103[0]+-1*Q_000102000*R_113[0]+Q_000202000*R_123[0]; double QR_001000001103=Q_001000001*R_103[0]+-1*Q_001000101*R_104[0]+-1*Q_101000001*R_203[0]+Q_101000101*R_204[0]; double QR_000001001103=Q_000001001*R_103[0]+-1*Q_000001101*R_104[0]+-1*Q_000101001*R_113[0]+Q_000101101*R_114[0]; double QR_000000002103=Q_000000002*R_103[0]+-1*Q_000000102*R_104[0]+Q_000000202*R_105[0]; double QR_002000000112=Q_002000000*R_112[0]+-1*Q_102000000*R_212[0]+Q_202000000*R_312[0]; double QR_001001000112=Q_001001000*R_112[0]+-1*Q_001101000*R_122[0]+-1*Q_101001000*R_212[0]+Q_101101000*R_222[0]; double QR_000002000112=Q_000002000*R_112[0]+-1*Q_000102000*R_122[0]+Q_000202000*R_132[0]; double QR_001000001112=Q_001000001*R_112[0]+-1*Q_001000101*R_113[0]+-1*Q_101000001*R_212[0]+Q_101000101*R_213[0]; double QR_000001001112=Q_000001001*R_112[0]+-1*Q_000001101*R_113[0]+-1*Q_000101001*R_122[0]+Q_000101101*R_123[0]; double QR_000000002112=Q_000000002*R_112[0]+-1*Q_000000102*R_113[0]+Q_000000202*R_114[0]; double QR_002000000121=Q_002000000*R_121[0]+-1*Q_102000000*R_221[0]+Q_202000000*R_321[0]; double QR_001001000121=Q_001001000*R_121[0]+-1*Q_001101000*R_131[0]+-1*Q_101001000*R_221[0]+Q_101101000*R_231[0]; double QR_000002000121=Q_000002000*R_121[0]+-1*Q_000102000*R_131[0]+Q_000202000*R_141[0]; double QR_001000001121=Q_001000001*R_121[0]+-1*Q_001000101*R_122[0]+-1*Q_101000001*R_221[0]+Q_101000101*R_222[0]; double QR_000001001121=Q_000001001*R_121[0]+-1*Q_000001101*R_122[0]+-1*Q_000101001*R_131[0]+Q_000101101*R_132[0]; double QR_000000002121=Q_000000002*R_121[0]+-1*Q_000000102*R_122[0]+Q_000000202*R_123[0]; double QR_002000000130=Q_002000000*R_130[0]+-1*Q_102000000*R_230[0]+Q_202000000*R_330[0]; double QR_001001000130=Q_001001000*R_130[0]+-1*Q_001101000*R_140[0]+-1*Q_101001000*R_230[0]+Q_101101000*R_240[0]; double QR_000002000130=Q_000002000*R_130[0]+-1*Q_000102000*R_140[0]+Q_000202000*R_150[0]; double QR_001000001130=Q_001000001*R_130[0]+-1*Q_001000101*R_131[0]+-1*Q_101000001*R_230[0]+Q_101000101*R_231[0]; double QR_000001001130=Q_000001001*R_130[0]+-1*Q_000001101*R_131[0]+-1*Q_000101001*R_140[0]+Q_000101101*R_141[0]; double QR_000000002130=Q_000000002*R_130[0]+-1*Q_000000102*R_131[0]+Q_000000202*R_132[0]; double QR_002000000202=Q_002000000*R_202[0]+-1*Q_102000000*R_302[0]+Q_202000000*R_402[0]; double QR_001001000202=Q_001001000*R_202[0]+-1*Q_001101000*R_212[0]+-1*Q_101001000*R_302[0]+Q_101101000*R_312[0]; double QR_000002000202=Q_000002000*R_202[0]+-1*Q_000102000*R_212[0]+Q_000202000*R_222[0]; double QR_001000001202=Q_001000001*R_202[0]+-1*Q_001000101*R_203[0]+-1*Q_101000001*R_302[0]+Q_101000101*R_303[0]; double QR_000001001202=Q_000001001*R_202[0]+-1*Q_000001101*R_203[0]+-1*Q_000101001*R_212[0]+Q_000101101*R_213[0]; double QR_000000002202=Q_000000002*R_202[0]+-1*Q_000000102*R_203[0]+Q_000000202*R_204[0]; double QR_002000000211=Q_002000000*R_211[0]+-1*Q_102000000*R_311[0]+Q_202000000*R_411[0]; double QR_001001000211=Q_001001000*R_211[0]+-1*Q_001101000*R_221[0]+-1*Q_101001000*R_311[0]+Q_101101000*R_321[0]; double QR_000002000211=Q_000002000*R_211[0]+-1*Q_000102000*R_221[0]+Q_000202000*R_231[0]; double QR_001000001211=Q_001000001*R_211[0]+-1*Q_001000101*R_212[0]+-1*Q_101000001*R_311[0]+Q_101000101*R_312[0]; double QR_000001001211=Q_000001001*R_211[0]+-1*Q_000001101*R_212[0]+-1*Q_000101001*R_221[0]+Q_000101101*R_222[0]; double QR_000000002211=Q_000000002*R_211[0]+-1*Q_000000102*R_212[0]+Q_000000202*R_213[0]; double QR_002000000220=Q_002000000*R_220[0]+-1*Q_102000000*R_320[0]+Q_202000000*R_420[0]; double QR_001001000220=Q_001001000*R_220[0]+-1*Q_001101000*R_230[0]+-1*Q_101001000*R_320[0]+Q_101101000*R_330[0]; double QR_000002000220=Q_000002000*R_220[0]+-1*Q_000102000*R_230[0]+Q_000202000*R_240[0]; double QR_001000001220=Q_001000001*R_220[0]+-1*Q_001000101*R_221[0]+-1*Q_101000001*R_320[0]+Q_101000101*R_321[0]; double QR_000001001220=Q_000001001*R_220[0]+-1*Q_000001101*R_221[0]+-1*Q_000101001*R_230[0]+Q_000101101*R_231[0]; double QR_000000002220=Q_000000002*R_220[0]+-1*Q_000000102*R_221[0]+Q_000000202*R_222[0]; double QR_002000000301=Q_002000000*R_301[0]+-1*Q_102000000*R_401[0]+Q_202000000*R_501[0]; double QR_001001000301=Q_001001000*R_301[0]+-1*Q_001101000*R_311[0]+-1*Q_101001000*R_401[0]+Q_101101000*R_411[0]; double QR_000002000301=Q_000002000*R_301[0]+-1*Q_000102000*R_311[0]+Q_000202000*R_321[0]; double QR_001000001301=Q_001000001*R_301[0]+-1*Q_001000101*R_302[0]+-1*Q_101000001*R_401[0]+Q_101000101*R_402[0]; double QR_000001001301=Q_000001001*R_301[0]+-1*Q_000001101*R_302[0]+-1*Q_000101001*R_311[0]+Q_000101101*R_312[0]; double QR_000000002301=Q_000000002*R_301[0]+-1*Q_000000102*R_302[0]+Q_000000202*R_303[0]; double QR_002000000310=Q_002000000*R_310[0]+-1*Q_102000000*R_410[0]+Q_202000000*R_510[0]; double QR_001001000310=Q_001001000*R_310[0]+-1*Q_001101000*R_320[0]+-1*Q_101001000*R_410[0]+Q_101101000*R_420[0]; double QR_000002000310=Q_000002000*R_310[0]+-1*Q_000102000*R_320[0]+Q_000202000*R_330[0]; double QR_001000001310=Q_001000001*R_310[0]+-1*Q_001000101*R_311[0]+-1*Q_101000001*R_410[0]+Q_101000101*R_411[0]; double QR_000001001310=Q_000001001*R_310[0]+-1*Q_000001101*R_311[0]+-1*Q_000101001*R_320[0]+Q_000101101*R_321[0]; double QR_000000002310=Q_000000002*R_310[0]+-1*Q_000000102*R_311[0]+Q_000000202*R_312[0]; double QR_002000000400=Q_002000000*R_400[0]+-1*Q_102000000*R_500[0]+Q_202000000*R_600[0]; double QR_001001000400=Q_001001000*R_400[0]+-1*Q_001101000*R_410[0]+-1*Q_101001000*R_500[0]+Q_101101000*R_510[0]; double QR_000002000400=Q_000002000*R_400[0]+-1*Q_000102000*R_410[0]+Q_000202000*R_420[0]; double QR_001000001400=Q_001000001*R_400[0]+-1*Q_001000101*R_401[0]+-1*Q_101000001*R_500[0]+Q_101000101*R_501[0]; double QR_000001001400=Q_000001001*R_400[0]+-1*Q_000001101*R_401[0]+-1*Q_000101001*R_410[0]+Q_000101101*R_411[0]; double QR_000000002400=Q_000000002*R_400[0]+-1*Q_000000102*R_401[0]+Q_000000202*R_402[0]; double Pd_101[3]; double Pd_002[3]; double Pd_102[3]; double Pd_202[3]; double Pd_110[3]; double Pd_011[3]; double Pd_111[3]; double Pd_211[3]; double Pd_012[3]; double Pd_112[3]; double Pd_212[3]; double Pd_312[3]; double Pd_020[3]; double Pd_120[3]; double Pd_220[3]; double Pd_021[3]; double Pd_121[3]; double Pd_221[3]; double Pd_321[3]; double Pd_022[3]; double Pd_122[3]; double Pd_222[3]; double Pd_322[3]; double Pd_422[3]; for(int i=0;i<3;i++){ Pd_101[i]=aPin1; } for(int i=0;i<3;i++){ Pd_002[i]=Pd_101[i]+Pd_001[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_102[i]=Pd_001[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_202[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_110[i]=aPin1; } for(int i=0;i<3;i++){ Pd_011[i]=Pd_101[i]+Pd_010[i]*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_111[i]=Pd_010[i]*Pd_101[i]+aPin1*Pd_001[i]; } for(int i=0;i<3;i++){ Pd_211[i]=aPin1*Pd_101[i]; } for(int i=0;i<3;i++){ Pd_012[i]=Pd_111[i]+Pd_001[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_112[i]=2*Pd_211[i]+Pd_001[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_212[i]=Pd_001[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_312[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_020[i]=Pd_110[i]+Pd_010[i]*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_120[i]=Pd_010[i]*Pd_110[i]+aPin1*Pd_010[i]; } for(int i=0;i<3;i++){ Pd_220[i]=aPin1*Pd_110[i]; } for(int i=0;i<3;i++){ Pd_021[i]=Pd_111[i]+Pd_010[i]*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_121[i]=2*Pd_211[i]+Pd_010[i]*Pd_111[i]+aPin1*Pd_011[i]; } for(int i=0;i<3;i++){ Pd_221[i]=Pd_010[i]*Pd_211[i]+aPin1*Pd_111[i]; } for(int i=0;i<3;i++){ Pd_321[i]=aPin1*Pd_211[i]; } for(int i=0;i<3;i++){ Pd_022[i]=Pd_112[i]+Pd_010[i]*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_122[i]=2*Pd_212[i]+Pd_010[i]*Pd_112[i]+aPin1*Pd_012[i]; } for(int i=0;i<3;i++){ Pd_222[i]=3*Pd_312[i]+Pd_010[i]*Pd_212[i]+aPin1*Pd_112[i]; } for(int i=0;i<3;i++){ Pd_322[i]=Pd_010[i]*Pd_312[i]+aPin1*Pd_212[i]; } for(int i=0;i<3;i++){ Pd_422[i]=aPin1*Pd_312[i]; } double P_022000000=Pd_022[0]; double P_122000000=Pd_122[0]; double P_222000000=Pd_222[0]; double P_322000000=Pd_322[0]; double P_422000000=Pd_422[0]; double P_021001000=Pd_021[0]*Pd_001[1]; double P_021101000=Pd_021[0]*Pd_101[1]; double P_121001000=Pd_121[0]*Pd_001[1]; double P_121101000=Pd_121[0]*Pd_101[1]; double P_221001000=Pd_221[0]*Pd_001[1]; double P_221101000=Pd_221[0]*Pd_101[1]; double P_321001000=Pd_321[0]*Pd_001[1]; double P_321101000=Pd_321[0]*Pd_101[1]; double P_020002000=Pd_020[0]*Pd_002[1]; double P_020102000=Pd_020[0]*Pd_102[1]; double P_020202000=Pd_020[0]*Pd_202[1]; double P_120002000=Pd_120[0]*Pd_002[1]; double P_120102000=Pd_120[0]*Pd_102[1]; double P_120202000=Pd_120[0]*Pd_202[1]; double P_220002000=Pd_220[0]*Pd_002[1]; double P_220102000=Pd_220[0]*Pd_102[1]; double P_220202000=Pd_220[0]*Pd_202[1]; double P_021000001=Pd_021[0]*Pd_001[2]; double P_021000101=Pd_021[0]*Pd_101[2]; double P_121000001=Pd_121[0]*Pd_001[2]; double P_121000101=Pd_121[0]*Pd_101[2]; double P_221000001=Pd_221[0]*Pd_001[2]; double P_221000101=Pd_221[0]*Pd_101[2]; double P_321000001=Pd_321[0]*Pd_001[2]; double P_321000101=Pd_321[0]*Pd_101[2]; double P_020001001=Pd_020[0]*Pd_001[1]*Pd_001[2]; double P_020001101=Pd_020[0]*Pd_001[1]*Pd_101[2]; double P_020101001=Pd_020[0]*Pd_101[1]*Pd_001[2]; double P_020101101=Pd_020[0]*Pd_101[1]*Pd_101[2]; double P_120001001=Pd_120[0]*Pd_001[1]*Pd_001[2]; double P_120001101=Pd_120[0]*Pd_001[1]*Pd_101[2]; double P_120101001=Pd_120[0]*Pd_101[1]*Pd_001[2]; double P_120101101=Pd_120[0]*Pd_101[1]*Pd_101[2]; double P_220001001=Pd_220[0]*Pd_001[1]*Pd_001[2]; double P_220001101=Pd_220[0]*Pd_001[1]*Pd_101[2]; double P_220101001=Pd_220[0]*Pd_101[1]*Pd_001[2]; double P_220101101=Pd_220[0]*Pd_101[1]*Pd_101[2]; double P_020000002=Pd_020[0]*Pd_002[2]; double P_020000102=Pd_020[0]*Pd_102[2]; double P_020000202=Pd_020[0]*Pd_202[2]; double P_120000002=Pd_120[0]*Pd_002[2]; double P_120000102=Pd_120[0]*Pd_102[2]; double P_120000202=Pd_120[0]*Pd_202[2]; double P_220000002=Pd_220[0]*Pd_002[2]; double P_220000102=Pd_220[0]*Pd_102[2]; double P_220000202=Pd_220[0]*Pd_202[2]; double P_012010000=Pd_012[0]*Pd_010[1]; double P_012110000=Pd_012[0]*Pd_110[1]; double P_112010000=Pd_112[0]*Pd_010[1]; double P_112110000=Pd_112[0]*Pd_110[1]; double P_212010000=Pd_212[0]*Pd_010[1]; double P_212110000=Pd_212[0]*Pd_110[1]; double P_312010000=Pd_312[0]*Pd_010[1]; double P_312110000=Pd_312[0]*Pd_110[1]; double P_011011000=Pd_011[0]*Pd_011[1]; double P_011111000=Pd_011[0]*Pd_111[1]; double P_011211000=Pd_011[0]*Pd_211[1]; double P_111011000=Pd_111[0]*Pd_011[1]; double P_111111000=Pd_111[0]*Pd_111[1]; double P_111211000=Pd_111[0]*Pd_211[1]; double P_211011000=Pd_211[0]*Pd_011[1]; double P_211111000=Pd_211[0]*Pd_111[1]; double P_211211000=Pd_211[0]*Pd_211[1]; double P_010012000=Pd_010[0]*Pd_012[1]; double P_010112000=Pd_010[0]*Pd_112[1]; double P_010212000=Pd_010[0]*Pd_212[1]; double P_010312000=Pd_010[0]*Pd_312[1]; double P_110012000=Pd_110[0]*Pd_012[1]; double P_110112000=Pd_110[0]*Pd_112[1]; double P_110212000=Pd_110[0]*Pd_212[1]; double P_110312000=Pd_110[0]*Pd_312[1]; double P_011010001=Pd_011[0]*Pd_010[1]*Pd_001[2]; double P_011010101=Pd_011[0]*Pd_010[1]*Pd_101[2]; double P_011110001=Pd_011[0]*Pd_110[1]*Pd_001[2]; double P_011110101=Pd_011[0]*Pd_110[1]*Pd_101[2]; double P_111010001=Pd_111[0]*Pd_010[1]*Pd_001[2]; double P_111010101=Pd_111[0]*Pd_010[1]*Pd_101[2]; double P_111110001=Pd_111[0]*Pd_110[1]*Pd_001[2]; double P_111110101=Pd_111[0]*Pd_110[1]*Pd_101[2]; double P_211010001=Pd_211[0]*Pd_010[1]*Pd_001[2]; double P_211010101=Pd_211[0]*Pd_010[1]*Pd_101[2]; double P_211110001=Pd_211[0]*Pd_110[1]*Pd_001[2]; double P_211110101=Pd_211[0]*Pd_110[1]*Pd_101[2]; double P_010011001=Pd_010[0]*Pd_011[1]*Pd_001[2]; double P_010011101=Pd_010[0]*Pd_011[1]*Pd_101[2]; double P_010111001=Pd_010[0]*Pd_111[1]*Pd_001[2]; double P_010111101=Pd_010[0]*Pd_111[1]*Pd_101[2]; double P_010211001=Pd_010[0]*Pd_211[1]*Pd_001[2]; double P_010211101=Pd_010[0]*Pd_211[1]*Pd_101[2]; double P_110011001=Pd_110[0]*Pd_011[1]*Pd_001[2]; double P_110011101=Pd_110[0]*Pd_011[1]*Pd_101[2]; double P_110111001=Pd_110[0]*Pd_111[1]*Pd_001[2]; double P_110111101=Pd_110[0]*Pd_111[1]*Pd_101[2]; double P_110211001=Pd_110[0]*Pd_211[1]*Pd_001[2]; double P_110211101=Pd_110[0]*Pd_211[1]*Pd_101[2]; double P_010010002=Pd_010[0]*Pd_010[1]*Pd_002[2]; double P_010010102=Pd_010[0]*Pd_010[1]*Pd_102[2]; double P_010010202=Pd_010[0]*Pd_010[1]*Pd_202[2]; double P_010110002=Pd_010[0]*Pd_110[1]*Pd_002[2]; double P_010110102=Pd_010[0]*Pd_110[1]*Pd_102[2]; double P_010110202=Pd_010[0]*Pd_110[1]*Pd_202[2]; double P_110010002=Pd_110[0]*Pd_010[1]*Pd_002[2]; double P_110010102=Pd_110[0]*Pd_010[1]*Pd_102[2]; double P_110010202=Pd_110[0]*Pd_010[1]*Pd_202[2]; double P_110110002=Pd_110[0]*Pd_110[1]*Pd_002[2]; double P_110110102=Pd_110[0]*Pd_110[1]*Pd_102[2]; double P_110110202=Pd_110[0]*Pd_110[1]*Pd_202[2]; double P_002020000=Pd_002[0]*Pd_020[1]; double P_002120000=Pd_002[0]*Pd_120[1]; double P_002220000=Pd_002[0]*Pd_220[1]; double P_102020000=Pd_102[0]*Pd_020[1]; double P_102120000=Pd_102[0]*Pd_120[1]; double P_102220000=Pd_102[0]*Pd_220[1]; double P_202020000=Pd_202[0]*Pd_020[1]; double P_202120000=Pd_202[0]*Pd_120[1]; double P_202220000=Pd_202[0]*Pd_220[1]; double P_001021000=Pd_001[0]*Pd_021[1]; double P_001121000=Pd_001[0]*Pd_121[1]; double P_001221000=Pd_001[0]*Pd_221[1]; double P_001321000=Pd_001[0]*Pd_321[1]; double P_101021000=Pd_101[0]*Pd_021[1]; double P_101121000=Pd_101[0]*Pd_121[1]; double P_101221000=Pd_101[0]*Pd_221[1]; double P_101321000=Pd_101[0]*Pd_321[1]; double P_000022000=Pd_022[1]; double P_000122000=Pd_122[1]; double P_000222000=Pd_222[1]; double P_000322000=Pd_322[1]; double P_000422000=Pd_422[1]; double P_001020001=Pd_001[0]*Pd_020[1]*Pd_001[2]; double P_001020101=Pd_001[0]*Pd_020[1]*Pd_101[2]; double P_001120001=Pd_001[0]*Pd_120[1]*Pd_001[2]; double P_001120101=Pd_001[0]*Pd_120[1]*Pd_101[2]; double P_001220001=Pd_001[0]*Pd_220[1]*Pd_001[2]; double P_001220101=Pd_001[0]*Pd_220[1]*Pd_101[2]; double P_101020001=Pd_101[0]*Pd_020[1]*Pd_001[2]; double P_101020101=Pd_101[0]*Pd_020[1]*Pd_101[2]; double P_101120001=Pd_101[0]*Pd_120[1]*Pd_001[2]; double P_101120101=Pd_101[0]*Pd_120[1]*Pd_101[2]; double P_101220001=Pd_101[0]*Pd_220[1]*Pd_001[2]; double P_101220101=Pd_101[0]*Pd_220[1]*Pd_101[2]; double P_000021001=Pd_021[1]*Pd_001[2]; double P_000021101=Pd_021[1]*Pd_101[2]; double P_000121001=Pd_121[1]*Pd_001[2]; double P_000121101=Pd_121[1]*Pd_101[2]; double P_000221001=Pd_221[1]*Pd_001[2]; double P_000221101=Pd_221[1]*Pd_101[2]; double P_000321001=Pd_321[1]*Pd_001[2]; double P_000321101=Pd_321[1]*Pd_101[2]; double P_000020002=Pd_020[1]*Pd_002[2]; double P_000020102=Pd_020[1]*Pd_102[2]; double P_000020202=Pd_020[1]*Pd_202[2]; double P_000120002=Pd_120[1]*Pd_002[2]; double P_000120102=Pd_120[1]*Pd_102[2]; double P_000120202=Pd_120[1]*Pd_202[2]; double P_000220002=Pd_220[1]*Pd_002[2]; double P_000220102=Pd_220[1]*Pd_102[2]; double P_000220202=Pd_220[1]*Pd_202[2]; double P_012000010=Pd_012[0]*Pd_010[2]; double P_012000110=Pd_012[0]*Pd_110[2]; double P_112000010=Pd_112[0]*Pd_010[2]; double P_112000110=Pd_112[0]*Pd_110[2]; double P_212000010=Pd_212[0]*Pd_010[2]; double P_212000110=Pd_212[0]*Pd_110[2]; double P_312000010=Pd_312[0]*Pd_010[2]; double P_312000110=Pd_312[0]*Pd_110[2]; double P_011001010=Pd_011[0]*Pd_001[1]*Pd_010[2]; double P_011001110=Pd_011[0]*Pd_001[1]*Pd_110[2]; double P_011101010=Pd_011[0]*Pd_101[1]*Pd_010[2]; double P_011101110=Pd_011[0]*Pd_101[1]*Pd_110[2]; double P_111001010=Pd_111[0]*Pd_001[1]*Pd_010[2]; double P_111001110=Pd_111[0]*Pd_001[1]*Pd_110[2]; double P_111101010=Pd_111[0]*Pd_101[1]*Pd_010[2]; double P_111101110=Pd_111[0]*Pd_101[1]*Pd_110[2]; double P_211001010=Pd_211[0]*Pd_001[1]*Pd_010[2]; double P_211001110=Pd_211[0]*Pd_001[1]*Pd_110[2]; double P_211101010=Pd_211[0]*Pd_101[1]*Pd_010[2]; double P_211101110=Pd_211[0]*Pd_101[1]*Pd_110[2]; double P_010002010=Pd_010[0]*Pd_002[1]*Pd_010[2]; double P_010002110=Pd_010[0]*Pd_002[1]*Pd_110[2]; double P_010102010=Pd_010[0]*Pd_102[1]*Pd_010[2]; double P_010102110=Pd_010[0]*Pd_102[1]*Pd_110[2]; double P_010202010=Pd_010[0]*Pd_202[1]*Pd_010[2]; double P_010202110=Pd_010[0]*Pd_202[1]*Pd_110[2]; double P_110002010=Pd_110[0]*Pd_002[1]*Pd_010[2]; double P_110002110=Pd_110[0]*Pd_002[1]*Pd_110[2]; double P_110102010=Pd_110[0]*Pd_102[1]*Pd_010[2]; double P_110102110=Pd_110[0]*Pd_102[1]*Pd_110[2]; double P_110202010=Pd_110[0]*Pd_202[1]*Pd_010[2]; double P_110202110=Pd_110[0]*Pd_202[1]*Pd_110[2]; double P_011000011=Pd_011[0]*Pd_011[2]; double P_011000111=Pd_011[0]*Pd_111[2]; double P_011000211=Pd_011[0]*Pd_211[2]; double P_111000011=Pd_111[0]*Pd_011[2]; double P_111000111=Pd_111[0]*Pd_111[2]; double P_111000211=Pd_111[0]*Pd_211[2]; double P_211000011=Pd_211[0]*Pd_011[2]; double P_211000111=Pd_211[0]*Pd_111[2]; double P_211000211=Pd_211[0]*Pd_211[2]; double P_010001011=Pd_010[0]*Pd_001[1]*Pd_011[2]; double P_010001111=Pd_010[0]*Pd_001[1]*Pd_111[2]; double P_010001211=Pd_010[0]*Pd_001[1]*Pd_211[2]; double P_010101011=Pd_010[0]*Pd_101[1]*Pd_011[2]; double P_010101111=Pd_010[0]*Pd_101[1]*Pd_111[2]; double P_010101211=Pd_010[0]*Pd_101[1]*Pd_211[2]; double P_110001011=Pd_110[0]*Pd_001[1]*Pd_011[2]; double P_110001111=Pd_110[0]*Pd_001[1]*Pd_111[2]; double P_110001211=Pd_110[0]*Pd_001[1]*Pd_211[2]; double P_110101011=Pd_110[0]*Pd_101[1]*Pd_011[2]; double P_110101111=Pd_110[0]*Pd_101[1]*Pd_111[2]; double P_110101211=Pd_110[0]*Pd_101[1]*Pd_211[2]; double P_010000012=Pd_010[0]*Pd_012[2]; double P_010000112=Pd_010[0]*Pd_112[2]; double P_010000212=Pd_010[0]*Pd_212[2]; double P_010000312=Pd_010[0]*Pd_312[2]; double P_110000012=Pd_110[0]*Pd_012[2]; double P_110000112=Pd_110[0]*Pd_112[2]; double P_110000212=Pd_110[0]*Pd_212[2]; double P_110000312=Pd_110[0]*Pd_312[2]; double P_002010010=Pd_002[0]*Pd_010[1]*Pd_010[2]; double P_002010110=Pd_002[0]*Pd_010[1]*Pd_110[2]; double P_002110010=Pd_002[0]*Pd_110[1]*Pd_010[2]; double P_002110110=Pd_002[0]*Pd_110[1]*Pd_110[2]; double P_102010010=Pd_102[0]*Pd_010[1]*Pd_010[2]; double P_102010110=Pd_102[0]*Pd_010[1]*Pd_110[2]; double P_102110010=Pd_102[0]*Pd_110[1]*Pd_010[2]; double P_102110110=Pd_102[0]*Pd_110[1]*Pd_110[2]; double P_202010010=Pd_202[0]*Pd_010[1]*Pd_010[2]; double P_202010110=Pd_202[0]*Pd_010[1]*Pd_110[2]; double P_202110010=Pd_202[0]*Pd_110[1]*Pd_010[2]; double P_202110110=Pd_202[0]*Pd_110[1]*Pd_110[2]; double P_001011010=Pd_001[0]*Pd_011[1]*Pd_010[2]; double P_001011110=Pd_001[0]*Pd_011[1]*Pd_110[2]; double P_001111010=Pd_001[0]*Pd_111[1]*Pd_010[2]; double P_001111110=Pd_001[0]*Pd_111[1]*Pd_110[2]; double P_001211010=Pd_001[0]*Pd_211[1]*Pd_010[2]; double P_001211110=Pd_001[0]*Pd_211[1]*Pd_110[2]; double P_101011010=Pd_101[0]*Pd_011[1]*Pd_010[2]; double P_101011110=Pd_101[0]*Pd_011[1]*Pd_110[2]; double P_101111010=Pd_101[0]*Pd_111[1]*Pd_010[2]; double P_101111110=Pd_101[0]*Pd_111[1]*Pd_110[2]; double P_101211010=Pd_101[0]*Pd_211[1]*Pd_010[2]; double P_101211110=Pd_101[0]*Pd_211[1]*Pd_110[2]; double P_000012010=Pd_012[1]*Pd_010[2]; double P_000012110=Pd_012[1]*Pd_110[2]; double P_000112010=Pd_112[1]*Pd_010[2]; double P_000112110=Pd_112[1]*Pd_110[2]; double P_000212010=Pd_212[1]*Pd_010[2]; double P_000212110=Pd_212[1]*Pd_110[2]; double P_000312010=Pd_312[1]*Pd_010[2]; double P_000312110=Pd_312[1]*Pd_110[2]; double P_001010011=Pd_001[0]*Pd_010[1]*Pd_011[2]; double P_001010111=Pd_001[0]*Pd_010[1]*Pd_111[2]; double P_001010211=Pd_001[0]*Pd_010[1]*Pd_211[2]; double P_001110011=Pd_001[0]*Pd_110[1]*Pd_011[2]; double P_001110111=Pd_001[0]*Pd_110[1]*Pd_111[2]; double P_001110211=Pd_001[0]*Pd_110[1]*Pd_211[2]; double P_101010011=Pd_101[0]*Pd_010[1]*Pd_011[2]; double P_101010111=Pd_101[0]*Pd_010[1]*Pd_111[2]; double P_101010211=Pd_101[0]*Pd_010[1]*Pd_211[2]; double P_101110011=Pd_101[0]*Pd_110[1]*Pd_011[2]; double P_101110111=Pd_101[0]*Pd_110[1]*Pd_111[2]; double P_101110211=Pd_101[0]*Pd_110[1]*Pd_211[2]; double P_000011011=Pd_011[1]*Pd_011[2]; double P_000011111=Pd_011[1]*Pd_111[2]; double P_000011211=Pd_011[1]*Pd_211[2]; double P_000111011=Pd_111[1]*Pd_011[2]; double P_000111111=Pd_111[1]*Pd_111[2]; double P_000111211=Pd_111[1]*Pd_211[2]; double P_000211011=Pd_211[1]*Pd_011[2]; double P_000211111=Pd_211[1]*Pd_111[2]; double P_000211211=Pd_211[1]*Pd_211[2]; double P_000010012=Pd_010[1]*Pd_012[2]; double P_000010112=Pd_010[1]*Pd_112[2]; double P_000010212=Pd_010[1]*Pd_212[2]; double P_000010312=Pd_010[1]*Pd_312[2]; double P_000110012=Pd_110[1]*Pd_012[2]; double P_000110112=Pd_110[1]*Pd_112[2]; double P_000110212=Pd_110[1]*Pd_212[2]; double P_000110312=Pd_110[1]*Pd_312[2]; double P_002000020=Pd_002[0]*Pd_020[2]; double P_002000120=Pd_002[0]*Pd_120[2]; double P_002000220=Pd_002[0]*Pd_220[2]; double P_102000020=Pd_102[0]*Pd_020[2]; double P_102000120=Pd_102[0]*Pd_120[2]; double P_102000220=Pd_102[0]*Pd_220[2]; double P_202000020=Pd_202[0]*Pd_020[2]; double P_202000120=Pd_202[0]*Pd_120[2]; double P_202000220=Pd_202[0]*Pd_220[2]; double P_001001020=Pd_001[0]*Pd_001[1]*Pd_020[2]; double P_001001120=Pd_001[0]*Pd_001[1]*Pd_120[2]; double P_001001220=Pd_001[0]*Pd_001[1]*Pd_220[2]; double P_001101020=Pd_001[0]*Pd_101[1]*Pd_020[2]; double P_001101120=Pd_001[0]*Pd_101[1]*Pd_120[2]; double P_001101220=Pd_001[0]*Pd_101[1]*Pd_220[2]; double P_101001020=Pd_101[0]*Pd_001[1]*Pd_020[2]; double P_101001120=Pd_101[0]*Pd_001[1]*Pd_120[2]; double P_101001220=Pd_101[0]*Pd_001[1]*Pd_220[2]; double P_101101020=Pd_101[0]*Pd_101[1]*Pd_020[2]; double P_101101120=Pd_101[0]*Pd_101[1]*Pd_120[2]; double P_101101220=Pd_101[0]*Pd_101[1]*Pd_220[2]; double P_000002020=Pd_002[1]*Pd_020[2]; double P_000002120=Pd_002[1]*Pd_120[2]; double P_000002220=Pd_002[1]*Pd_220[2]; double P_000102020=Pd_102[1]*Pd_020[2]; double P_000102120=Pd_102[1]*Pd_120[2]; double P_000102220=Pd_102[1]*Pd_220[2]; double P_000202020=Pd_202[1]*Pd_020[2]; double P_000202120=Pd_202[1]*Pd_120[2]; double P_000202220=Pd_202[1]*Pd_220[2]; double P_001000021=Pd_001[0]*Pd_021[2]; double P_001000121=Pd_001[0]*Pd_121[2]; double P_001000221=Pd_001[0]*Pd_221[2]; double P_001000321=Pd_001[0]*Pd_321[2]; double P_101000021=Pd_101[0]*Pd_021[2]; double P_101000121=Pd_101[0]*Pd_121[2]; double P_101000221=Pd_101[0]*Pd_221[2]; double P_101000321=Pd_101[0]*Pd_321[2]; double P_000001021=Pd_001[1]*Pd_021[2]; double P_000001121=Pd_001[1]*Pd_121[2]; double P_000001221=Pd_001[1]*Pd_221[2]; double P_000001321=Pd_001[1]*Pd_321[2]; double P_000101021=Pd_101[1]*Pd_021[2]; double P_000101121=Pd_101[1]*Pd_121[2]; double P_000101221=Pd_101[1]*Pd_221[2]; double P_000101321=Pd_101[1]*Pd_321[2]; double P_000000022=Pd_022[2]; double P_000000122=Pd_122[2]; double P_000000222=Pd_222[2]; double P_000000322=Pd_322[2]; double P_000000422=Pd_422[2]; ans_temp[ans_id*6+0]+=Pmtrx[0]*(P_022000000*QR_002000000000+P_122000000*QR_002000000100+P_222000000*QR_002000000200+P_322000000*QR_002000000300+P_422000000*QR_002000000400); ans_temp[ans_id*6+0]+=Pmtrx[1]*(P_022000000*QR_001001000000+P_122000000*QR_001001000100+P_222000000*QR_001001000200+P_322000000*QR_001001000300+P_422000000*QR_001001000400); ans_temp[ans_id*6+0]+=Pmtrx[2]*(P_022000000*QR_000002000000+P_122000000*QR_000002000100+P_222000000*QR_000002000200+P_322000000*QR_000002000300+P_422000000*QR_000002000400); ans_temp[ans_id*6+0]+=Pmtrx[3]*(P_022000000*QR_001000001000+P_122000000*QR_001000001100+P_222000000*QR_001000001200+P_322000000*QR_001000001300+P_422000000*QR_001000001400); ans_temp[ans_id*6+0]+=Pmtrx[4]*(P_022000000*QR_000001001000+P_122000000*QR_000001001100+P_222000000*QR_000001001200+P_322000000*QR_000001001300+P_422000000*QR_000001001400); ans_temp[ans_id*6+0]+=Pmtrx[5]*(P_022000000*QR_000000002000+P_122000000*QR_000000002100+P_222000000*QR_000000002200+P_322000000*QR_000000002300+P_422000000*QR_000000002400); ans_temp[ans_id*6+0]+=Pmtrx[6]*(P_021001000*QR_002000000000+P_021101000*QR_002000000010+P_121001000*QR_002000000100+P_121101000*QR_002000000110+P_221001000*QR_002000000200+P_221101000*QR_002000000210+P_321001000*QR_002000000300+P_321101000*QR_002000000310); ans_temp[ans_id*6+0]+=Pmtrx[7]*(P_021001000*QR_001001000000+P_021101000*QR_001001000010+P_121001000*QR_001001000100+P_121101000*QR_001001000110+P_221001000*QR_001001000200+P_221101000*QR_001001000210+P_321001000*QR_001001000300+P_321101000*QR_001001000310); ans_temp[ans_id*6+0]+=Pmtrx[8]*(P_021001000*QR_000002000000+P_021101000*QR_000002000010+P_121001000*QR_000002000100+P_121101000*QR_000002000110+P_221001000*QR_000002000200+P_221101000*QR_000002000210+P_321001000*QR_000002000300+P_321101000*QR_000002000310); ans_temp[ans_id*6+0]+=Pmtrx[9]*(P_021001000*QR_001000001000+P_021101000*QR_001000001010+P_121001000*QR_001000001100+P_121101000*QR_001000001110+P_221001000*QR_001000001200+P_221101000*QR_001000001210+P_321001000*QR_001000001300+P_321101000*QR_001000001310); ans_temp[ans_id*6+0]+=Pmtrx[10]*(P_021001000*QR_000001001000+P_021101000*QR_000001001010+P_121001000*QR_000001001100+P_121101000*QR_000001001110+P_221001000*QR_000001001200+P_221101000*QR_000001001210+P_321001000*QR_000001001300+P_321101000*QR_000001001310); ans_temp[ans_id*6+0]+=Pmtrx[11]*(P_021001000*QR_000000002000+P_021101000*QR_000000002010+P_121001000*QR_000000002100+P_121101000*QR_000000002110+P_221001000*QR_000000002200+P_221101000*QR_000000002210+P_321001000*QR_000000002300+P_321101000*QR_000000002310); ans_temp[ans_id*6+0]+=Pmtrx[12]*(P_020002000*QR_002000000000+P_020102000*QR_002000000010+P_020202000*QR_002000000020+P_120002000*QR_002000000100+P_120102000*QR_002000000110+P_120202000*QR_002000000120+P_220002000*QR_002000000200+P_220102000*QR_002000000210+P_220202000*QR_002000000220); ans_temp[ans_id*6+0]+=Pmtrx[13]*(P_020002000*QR_001001000000+P_020102000*QR_001001000010+P_020202000*QR_001001000020+P_120002000*QR_001001000100+P_120102000*QR_001001000110+P_120202000*QR_001001000120+P_220002000*QR_001001000200+P_220102000*QR_001001000210+P_220202000*QR_001001000220); ans_temp[ans_id*6+0]+=Pmtrx[14]*(P_020002000*QR_000002000000+P_020102000*QR_000002000010+P_020202000*QR_000002000020+P_120002000*QR_000002000100+P_120102000*QR_000002000110+P_120202000*QR_000002000120+P_220002000*QR_000002000200+P_220102000*QR_000002000210+P_220202000*QR_000002000220); ans_temp[ans_id*6+0]+=Pmtrx[15]*(P_020002000*QR_001000001000+P_020102000*QR_001000001010+P_020202000*QR_001000001020+P_120002000*QR_001000001100+P_120102000*QR_001000001110+P_120202000*QR_001000001120+P_220002000*QR_001000001200+P_220102000*QR_001000001210+P_220202000*QR_001000001220); ans_temp[ans_id*6+0]+=Pmtrx[16]*(P_020002000*QR_000001001000+P_020102000*QR_000001001010+P_020202000*QR_000001001020+P_120002000*QR_000001001100+P_120102000*QR_000001001110+P_120202000*QR_000001001120+P_220002000*QR_000001001200+P_220102000*QR_000001001210+P_220202000*QR_000001001220); ans_temp[ans_id*6+0]+=Pmtrx[17]*(P_020002000*QR_000000002000+P_020102000*QR_000000002010+P_020202000*QR_000000002020+P_120002000*QR_000000002100+P_120102000*QR_000000002110+P_120202000*QR_000000002120+P_220002000*QR_000000002200+P_220102000*QR_000000002210+P_220202000*QR_000000002220); ans_temp[ans_id*6+0]+=Pmtrx[18]*(P_021000001*QR_002000000000+P_021000101*QR_002000000001+P_121000001*QR_002000000100+P_121000101*QR_002000000101+P_221000001*QR_002000000200+P_221000101*QR_002000000201+P_321000001*QR_002000000300+P_321000101*QR_002000000301); ans_temp[ans_id*6+0]+=Pmtrx[19]*(P_021000001*QR_001001000000+P_021000101*QR_001001000001+P_121000001*QR_001001000100+P_121000101*QR_001001000101+P_221000001*QR_001001000200+P_221000101*QR_001001000201+P_321000001*QR_001001000300+P_321000101*QR_001001000301); ans_temp[ans_id*6+0]+=Pmtrx[20]*(P_021000001*QR_000002000000+P_021000101*QR_000002000001+P_121000001*QR_000002000100+P_121000101*QR_000002000101+P_221000001*QR_000002000200+P_221000101*QR_000002000201+P_321000001*QR_000002000300+P_321000101*QR_000002000301); ans_temp[ans_id*6+0]+=Pmtrx[21]*(P_021000001*QR_001000001000+P_021000101*QR_001000001001+P_121000001*QR_001000001100+P_121000101*QR_001000001101+P_221000001*QR_001000001200+P_221000101*QR_001000001201+P_321000001*QR_001000001300+P_321000101*QR_001000001301); ans_temp[ans_id*6+0]+=Pmtrx[22]*(P_021000001*QR_000001001000+P_021000101*QR_000001001001+P_121000001*QR_000001001100+P_121000101*QR_000001001101+P_221000001*QR_000001001200+P_221000101*QR_000001001201+P_321000001*QR_000001001300+P_321000101*QR_000001001301); ans_temp[ans_id*6+0]+=Pmtrx[23]*(P_021000001*QR_000000002000+P_021000101*QR_000000002001+P_121000001*QR_000000002100+P_121000101*QR_000000002101+P_221000001*QR_000000002200+P_221000101*QR_000000002201+P_321000001*QR_000000002300+P_321000101*QR_000000002301); ans_temp[ans_id*6+0]+=Pmtrx[24]*(P_020001001*QR_002000000000+P_020001101*QR_002000000001+P_020101001*QR_002000000010+P_020101101*QR_002000000011+P_120001001*QR_002000000100+P_120001101*QR_002000000101+P_120101001*QR_002000000110+P_120101101*QR_002000000111+P_220001001*QR_002000000200+P_220001101*QR_002000000201+P_220101001*QR_002000000210+P_220101101*QR_002000000211); ans_temp[ans_id*6+0]+=Pmtrx[25]*(P_020001001*QR_001001000000+P_020001101*QR_001001000001+P_020101001*QR_001001000010+P_020101101*QR_001001000011+P_120001001*QR_001001000100+P_120001101*QR_001001000101+P_120101001*QR_001001000110+P_120101101*QR_001001000111+P_220001001*QR_001001000200+P_220001101*QR_001001000201+P_220101001*QR_001001000210+P_220101101*QR_001001000211); ans_temp[ans_id*6+0]+=Pmtrx[26]*(P_020001001*QR_000002000000+P_020001101*QR_000002000001+P_020101001*QR_000002000010+P_020101101*QR_000002000011+P_120001001*QR_000002000100+P_120001101*QR_000002000101+P_120101001*QR_000002000110+P_120101101*QR_000002000111+P_220001001*QR_000002000200+P_220001101*QR_000002000201+P_220101001*QR_000002000210+P_220101101*QR_000002000211); ans_temp[ans_id*6+0]+=Pmtrx[27]*(P_020001001*QR_001000001000+P_020001101*QR_001000001001+P_020101001*QR_001000001010+P_020101101*QR_001000001011+P_120001001*QR_001000001100+P_120001101*QR_001000001101+P_120101001*QR_001000001110+P_120101101*QR_001000001111+P_220001001*QR_001000001200+P_220001101*QR_001000001201+P_220101001*QR_001000001210+P_220101101*QR_001000001211); ans_temp[ans_id*6+0]+=Pmtrx[28]*(P_020001001*QR_000001001000+P_020001101*QR_000001001001+P_020101001*QR_000001001010+P_020101101*QR_000001001011+P_120001001*QR_000001001100+P_120001101*QR_000001001101+P_120101001*QR_000001001110+P_120101101*QR_000001001111+P_220001001*QR_000001001200+P_220001101*QR_000001001201+P_220101001*QR_000001001210+P_220101101*QR_000001001211); ans_temp[ans_id*6+0]+=Pmtrx[29]*(P_020001001*QR_000000002000+P_020001101*QR_000000002001+P_020101001*QR_000000002010+P_020101101*QR_000000002011+P_120001001*QR_000000002100+P_120001101*QR_000000002101+P_120101001*QR_000000002110+P_120101101*QR_000000002111+P_220001001*QR_000000002200+P_220001101*QR_000000002201+P_220101001*QR_000000002210+P_220101101*QR_000000002211); ans_temp[ans_id*6+0]+=Pmtrx[30]*(P_020000002*QR_002000000000+P_020000102*QR_002000000001+P_020000202*QR_002000000002+P_120000002*QR_002000000100+P_120000102*QR_002000000101+P_120000202*QR_002000000102+P_220000002*QR_002000000200+P_220000102*QR_002000000201+P_220000202*QR_002000000202); ans_temp[ans_id*6+0]+=Pmtrx[31]*(P_020000002*QR_001001000000+P_020000102*QR_001001000001+P_020000202*QR_001001000002+P_120000002*QR_001001000100+P_120000102*QR_001001000101+P_120000202*QR_001001000102+P_220000002*QR_001001000200+P_220000102*QR_001001000201+P_220000202*QR_001001000202); ans_temp[ans_id*6+0]+=Pmtrx[32]*(P_020000002*QR_000002000000+P_020000102*QR_000002000001+P_020000202*QR_000002000002+P_120000002*QR_000002000100+P_120000102*QR_000002000101+P_120000202*QR_000002000102+P_220000002*QR_000002000200+P_220000102*QR_000002000201+P_220000202*QR_000002000202); ans_temp[ans_id*6+0]+=Pmtrx[33]*(P_020000002*QR_001000001000+P_020000102*QR_001000001001+P_020000202*QR_001000001002+P_120000002*QR_001000001100+P_120000102*QR_001000001101+P_120000202*QR_001000001102+P_220000002*QR_001000001200+P_220000102*QR_001000001201+P_220000202*QR_001000001202); ans_temp[ans_id*6+0]+=Pmtrx[34]*(P_020000002*QR_000001001000+P_020000102*QR_000001001001+P_020000202*QR_000001001002+P_120000002*QR_000001001100+P_120000102*QR_000001001101+P_120000202*QR_000001001102+P_220000002*QR_000001001200+P_220000102*QR_000001001201+P_220000202*QR_000001001202); ans_temp[ans_id*6+0]+=Pmtrx[35]*(P_020000002*QR_000000002000+P_020000102*QR_000000002001+P_020000202*QR_000000002002+P_120000002*QR_000000002100+P_120000102*QR_000000002101+P_120000202*QR_000000002102+P_220000002*QR_000000002200+P_220000102*QR_000000002201+P_220000202*QR_000000002202); ans_temp[ans_id*6+1]+=Pmtrx[0]*(P_012010000*QR_002000000000+P_012110000*QR_002000000010+P_112010000*QR_002000000100+P_112110000*QR_002000000110+P_212010000*QR_002000000200+P_212110000*QR_002000000210+P_312010000*QR_002000000300+P_312110000*QR_002000000310); ans_temp[ans_id*6+1]+=Pmtrx[1]*(P_012010000*QR_001001000000+P_012110000*QR_001001000010+P_112010000*QR_001001000100+P_112110000*QR_001001000110+P_212010000*QR_001001000200+P_212110000*QR_001001000210+P_312010000*QR_001001000300+P_312110000*QR_001001000310); ans_temp[ans_id*6+1]+=Pmtrx[2]*(P_012010000*QR_000002000000+P_012110000*QR_000002000010+P_112010000*QR_000002000100+P_112110000*QR_000002000110+P_212010000*QR_000002000200+P_212110000*QR_000002000210+P_312010000*QR_000002000300+P_312110000*QR_000002000310); ans_temp[ans_id*6+1]+=Pmtrx[3]*(P_012010000*QR_001000001000+P_012110000*QR_001000001010+P_112010000*QR_001000001100+P_112110000*QR_001000001110+P_212010000*QR_001000001200+P_212110000*QR_001000001210+P_312010000*QR_001000001300+P_312110000*QR_001000001310); ans_temp[ans_id*6+1]+=Pmtrx[4]*(P_012010000*QR_000001001000+P_012110000*QR_000001001010+P_112010000*QR_000001001100+P_112110000*QR_000001001110+P_212010000*QR_000001001200+P_212110000*QR_000001001210+P_312010000*QR_000001001300+P_312110000*QR_000001001310); ans_temp[ans_id*6+1]+=Pmtrx[5]*(P_012010000*QR_000000002000+P_012110000*QR_000000002010+P_112010000*QR_000000002100+P_112110000*QR_000000002110+P_212010000*QR_000000002200+P_212110000*QR_000000002210+P_312010000*QR_000000002300+P_312110000*QR_000000002310); ans_temp[ans_id*6+1]+=Pmtrx[6]*(P_011011000*QR_002000000000+P_011111000*QR_002000000010+P_011211000*QR_002000000020+P_111011000*QR_002000000100+P_111111000*QR_002000000110+P_111211000*QR_002000000120+P_211011000*QR_002000000200+P_211111000*QR_002000000210+P_211211000*QR_002000000220); ans_temp[ans_id*6+1]+=Pmtrx[7]*(P_011011000*QR_001001000000+P_011111000*QR_001001000010+P_011211000*QR_001001000020+P_111011000*QR_001001000100+P_111111000*QR_001001000110+P_111211000*QR_001001000120+P_211011000*QR_001001000200+P_211111000*QR_001001000210+P_211211000*QR_001001000220); ans_temp[ans_id*6+1]+=Pmtrx[8]*(P_011011000*QR_000002000000+P_011111000*QR_000002000010+P_011211000*QR_000002000020+P_111011000*QR_000002000100+P_111111000*QR_000002000110+P_111211000*QR_000002000120+P_211011000*QR_000002000200+P_211111000*QR_000002000210+P_211211000*QR_000002000220); ans_temp[ans_id*6+1]+=Pmtrx[9]*(P_011011000*QR_001000001000+P_011111000*QR_001000001010+P_011211000*QR_001000001020+P_111011000*QR_001000001100+P_111111000*QR_001000001110+P_111211000*QR_001000001120+P_211011000*QR_001000001200+P_211111000*QR_001000001210+P_211211000*QR_001000001220); ans_temp[ans_id*6+1]+=Pmtrx[10]*(P_011011000*QR_000001001000+P_011111000*QR_000001001010+P_011211000*QR_000001001020+P_111011000*QR_000001001100+P_111111000*QR_000001001110+P_111211000*QR_000001001120+P_211011000*QR_000001001200+P_211111000*QR_000001001210+P_211211000*QR_000001001220); ans_temp[ans_id*6+1]+=Pmtrx[11]*(P_011011000*QR_000000002000+P_011111000*QR_000000002010+P_011211000*QR_000000002020+P_111011000*QR_000000002100+P_111111000*QR_000000002110+P_111211000*QR_000000002120+P_211011000*QR_000000002200+P_211111000*QR_000000002210+P_211211000*QR_000000002220); ans_temp[ans_id*6+1]+=Pmtrx[12]*(P_010012000*QR_002000000000+P_010112000*QR_002000000010+P_010212000*QR_002000000020+P_010312000*QR_002000000030+P_110012000*QR_002000000100+P_110112000*QR_002000000110+P_110212000*QR_002000000120+P_110312000*QR_002000000130); ans_temp[ans_id*6+1]+=Pmtrx[13]*(P_010012000*QR_001001000000+P_010112000*QR_001001000010+P_010212000*QR_001001000020+P_010312000*QR_001001000030+P_110012000*QR_001001000100+P_110112000*QR_001001000110+P_110212000*QR_001001000120+P_110312000*QR_001001000130); ans_temp[ans_id*6+1]+=Pmtrx[14]*(P_010012000*QR_000002000000+P_010112000*QR_000002000010+P_010212000*QR_000002000020+P_010312000*QR_000002000030+P_110012000*QR_000002000100+P_110112000*QR_000002000110+P_110212000*QR_000002000120+P_110312000*QR_000002000130); ans_temp[ans_id*6+1]+=Pmtrx[15]*(P_010012000*QR_001000001000+P_010112000*QR_001000001010+P_010212000*QR_001000001020+P_010312000*QR_001000001030+P_110012000*QR_001000001100+P_110112000*QR_001000001110+P_110212000*QR_001000001120+P_110312000*QR_001000001130); ans_temp[ans_id*6+1]+=Pmtrx[16]*(P_010012000*QR_000001001000+P_010112000*QR_000001001010+P_010212000*QR_000001001020+P_010312000*QR_000001001030+P_110012000*QR_000001001100+P_110112000*QR_000001001110+P_110212000*QR_000001001120+P_110312000*QR_000001001130); ans_temp[ans_id*6+1]+=Pmtrx[17]*(P_010012000*QR_000000002000+P_010112000*QR_000000002010+P_010212000*QR_000000002020+P_010312000*QR_000000002030+P_110012000*QR_000000002100+P_110112000*QR_000000002110+P_110212000*QR_000000002120+P_110312000*QR_000000002130); ans_temp[ans_id*6+1]+=Pmtrx[18]*(P_011010001*QR_002000000000+P_011010101*QR_002000000001+P_011110001*QR_002000000010+P_011110101*QR_002000000011+P_111010001*QR_002000000100+P_111010101*QR_002000000101+P_111110001*QR_002000000110+P_111110101*QR_002000000111+P_211010001*QR_002000000200+P_211010101*QR_002000000201+P_211110001*QR_002000000210+P_211110101*QR_002000000211); ans_temp[ans_id*6+1]+=Pmtrx[19]*(P_011010001*QR_001001000000+P_011010101*QR_001001000001+P_011110001*QR_001001000010+P_011110101*QR_001001000011+P_111010001*QR_001001000100+P_111010101*QR_001001000101+P_111110001*QR_001001000110+P_111110101*QR_001001000111+P_211010001*QR_001001000200+P_211010101*QR_001001000201+P_211110001*QR_001001000210+P_211110101*QR_001001000211); ans_temp[ans_id*6+1]+=Pmtrx[20]*(P_011010001*QR_000002000000+P_011010101*QR_000002000001+P_011110001*QR_000002000010+P_011110101*QR_000002000011+P_111010001*QR_000002000100+P_111010101*QR_000002000101+P_111110001*QR_000002000110+P_111110101*QR_000002000111+P_211010001*QR_000002000200+P_211010101*QR_000002000201+P_211110001*QR_000002000210+P_211110101*QR_000002000211); ans_temp[ans_id*6+1]+=Pmtrx[21]*(P_011010001*QR_001000001000+P_011010101*QR_001000001001+P_011110001*QR_001000001010+P_011110101*QR_001000001011+P_111010001*QR_001000001100+P_111010101*QR_001000001101+P_111110001*QR_001000001110+P_111110101*QR_001000001111+P_211010001*QR_001000001200+P_211010101*QR_001000001201+P_211110001*QR_001000001210+P_211110101*QR_001000001211); ans_temp[ans_id*6+1]+=Pmtrx[22]*(P_011010001*QR_000001001000+P_011010101*QR_000001001001+P_011110001*QR_000001001010+P_011110101*QR_000001001011+P_111010001*QR_000001001100+P_111010101*QR_000001001101+P_111110001*QR_000001001110+P_111110101*QR_000001001111+P_211010001*QR_000001001200+P_211010101*QR_000001001201+P_211110001*QR_000001001210+P_211110101*QR_000001001211); ans_temp[ans_id*6+1]+=Pmtrx[23]*(P_011010001*QR_000000002000+P_011010101*QR_000000002001+P_011110001*QR_000000002010+P_011110101*QR_000000002011+P_111010001*QR_000000002100+P_111010101*QR_000000002101+P_111110001*QR_000000002110+P_111110101*QR_000000002111+P_211010001*QR_000000002200+P_211010101*QR_000000002201+P_211110001*QR_000000002210+P_211110101*QR_000000002211); ans_temp[ans_id*6+1]+=Pmtrx[24]*(P_010011001*QR_002000000000+P_010011101*QR_002000000001+P_010111001*QR_002000000010+P_010111101*QR_002000000011+P_010211001*QR_002000000020+P_010211101*QR_002000000021+P_110011001*QR_002000000100+P_110011101*QR_002000000101+P_110111001*QR_002000000110+P_110111101*QR_002000000111+P_110211001*QR_002000000120+P_110211101*QR_002000000121); ans_temp[ans_id*6+1]+=Pmtrx[25]*(P_010011001*QR_001001000000+P_010011101*QR_001001000001+P_010111001*QR_001001000010+P_010111101*QR_001001000011+P_010211001*QR_001001000020+P_010211101*QR_001001000021+P_110011001*QR_001001000100+P_110011101*QR_001001000101+P_110111001*QR_001001000110+P_110111101*QR_001001000111+P_110211001*QR_001001000120+P_110211101*QR_001001000121); ans_temp[ans_id*6+1]+=Pmtrx[26]*(P_010011001*QR_000002000000+P_010011101*QR_000002000001+P_010111001*QR_000002000010+P_010111101*QR_000002000011+P_010211001*QR_000002000020+P_010211101*QR_000002000021+P_110011001*QR_000002000100+P_110011101*QR_000002000101+P_110111001*QR_000002000110+P_110111101*QR_000002000111+P_110211001*QR_000002000120+P_110211101*QR_000002000121); ans_temp[ans_id*6+1]+=Pmtrx[27]*(P_010011001*QR_001000001000+P_010011101*QR_001000001001+P_010111001*QR_001000001010+P_010111101*QR_001000001011+P_010211001*QR_001000001020+P_010211101*QR_001000001021+P_110011001*QR_001000001100+P_110011101*QR_001000001101+P_110111001*QR_001000001110+P_110111101*QR_001000001111+P_110211001*QR_001000001120+P_110211101*QR_001000001121); ans_temp[ans_id*6+1]+=Pmtrx[28]*(P_010011001*QR_000001001000+P_010011101*QR_000001001001+P_010111001*QR_000001001010+P_010111101*QR_000001001011+P_010211001*QR_000001001020+P_010211101*QR_000001001021+P_110011001*QR_000001001100+P_110011101*QR_000001001101+P_110111001*QR_000001001110+P_110111101*QR_000001001111+P_110211001*QR_000001001120+P_110211101*QR_000001001121); ans_temp[ans_id*6+1]+=Pmtrx[29]*(P_010011001*QR_000000002000+P_010011101*QR_000000002001+P_010111001*QR_000000002010+P_010111101*QR_000000002011+P_010211001*QR_000000002020+P_010211101*QR_000000002021+P_110011001*QR_000000002100+P_110011101*QR_000000002101+P_110111001*QR_000000002110+P_110111101*QR_000000002111+P_110211001*QR_000000002120+P_110211101*QR_000000002121); ans_temp[ans_id*6+1]+=Pmtrx[30]*(P_010010002*QR_002000000000+P_010010102*QR_002000000001+P_010010202*QR_002000000002+P_010110002*QR_002000000010+P_010110102*QR_002000000011+P_010110202*QR_002000000012+P_110010002*QR_002000000100+P_110010102*QR_002000000101+P_110010202*QR_002000000102+P_110110002*QR_002000000110+P_110110102*QR_002000000111+P_110110202*QR_002000000112); ans_temp[ans_id*6+1]+=Pmtrx[31]*(P_010010002*QR_001001000000+P_010010102*QR_001001000001+P_010010202*QR_001001000002+P_010110002*QR_001001000010+P_010110102*QR_001001000011+P_010110202*QR_001001000012+P_110010002*QR_001001000100+P_110010102*QR_001001000101+P_110010202*QR_001001000102+P_110110002*QR_001001000110+P_110110102*QR_001001000111+P_110110202*QR_001001000112); ans_temp[ans_id*6+1]+=Pmtrx[32]*(P_010010002*QR_000002000000+P_010010102*QR_000002000001+P_010010202*QR_000002000002+P_010110002*QR_000002000010+P_010110102*QR_000002000011+P_010110202*QR_000002000012+P_110010002*QR_000002000100+P_110010102*QR_000002000101+P_110010202*QR_000002000102+P_110110002*QR_000002000110+P_110110102*QR_000002000111+P_110110202*QR_000002000112); ans_temp[ans_id*6+1]+=Pmtrx[33]*(P_010010002*QR_001000001000+P_010010102*QR_001000001001+P_010010202*QR_001000001002+P_010110002*QR_001000001010+P_010110102*QR_001000001011+P_010110202*QR_001000001012+P_110010002*QR_001000001100+P_110010102*QR_001000001101+P_110010202*QR_001000001102+P_110110002*QR_001000001110+P_110110102*QR_001000001111+P_110110202*QR_001000001112); ans_temp[ans_id*6+1]+=Pmtrx[34]*(P_010010002*QR_000001001000+P_010010102*QR_000001001001+P_010010202*QR_000001001002+P_010110002*QR_000001001010+P_010110102*QR_000001001011+P_010110202*QR_000001001012+P_110010002*QR_000001001100+P_110010102*QR_000001001101+P_110010202*QR_000001001102+P_110110002*QR_000001001110+P_110110102*QR_000001001111+P_110110202*QR_000001001112); ans_temp[ans_id*6+1]+=Pmtrx[35]*(P_010010002*QR_000000002000+P_010010102*QR_000000002001+P_010010202*QR_000000002002+P_010110002*QR_000000002010+P_010110102*QR_000000002011+P_010110202*QR_000000002012+P_110010002*QR_000000002100+P_110010102*QR_000000002101+P_110010202*QR_000000002102+P_110110002*QR_000000002110+P_110110102*QR_000000002111+P_110110202*QR_000000002112); ans_temp[ans_id*6+2]+=Pmtrx[0]*(P_002020000*QR_002000000000+P_002120000*QR_002000000010+P_002220000*QR_002000000020+P_102020000*QR_002000000100+P_102120000*QR_002000000110+P_102220000*QR_002000000120+P_202020000*QR_002000000200+P_202120000*QR_002000000210+P_202220000*QR_002000000220); ans_temp[ans_id*6+2]+=Pmtrx[1]*(P_002020000*QR_001001000000+P_002120000*QR_001001000010+P_002220000*QR_001001000020+P_102020000*QR_001001000100+P_102120000*QR_001001000110+P_102220000*QR_001001000120+P_202020000*QR_001001000200+P_202120000*QR_001001000210+P_202220000*QR_001001000220); ans_temp[ans_id*6+2]+=Pmtrx[2]*(P_002020000*QR_000002000000+P_002120000*QR_000002000010+P_002220000*QR_000002000020+P_102020000*QR_000002000100+P_102120000*QR_000002000110+P_102220000*QR_000002000120+P_202020000*QR_000002000200+P_202120000*QR_000002000210+P_202220000*QR_000002000220); ans_temp[ans_id*6+2]+=Pmtrx[3]*(P_002020000*QR_001000001000+P_002120000*QR_001000001010+P_002220000*QR_001000001020+P_102020000*QR_001000001100+P_102120000*QR_001000001110+P_102220000*QR_001000001120+P_202020000*QR_001000001200+P_202120000*QR_001000001210+P_202220000*QR_001000001220); ans_temp[ans_id*6+2]+=Pmtrx[4]*(P_002020000*QR_000001001000+P_002120000*QR_000001001010+P_002220000*QR_000001001020+P_102020000*QR_000001001100+P_102120000*QR_000001001110+P_102220000*QR_000001001120+P_202020000*QR_000001001200+P_202120000*QR_000001001210+P_202220000*QR_000001001220); ans_temp[ans_id*6+2]+=Pmtrx[5]*(P_002020000*QR_000000002000+P_002120000*QR_000000002010+P_002220000*QR_000000002020+P_102020000*QR_000000002100+P_102120000*QR_000000002110+P_102220000*QR_000000002120+P_202020000*QR_000000002200+P_202120000*QR_000000002210+P_202220000*QR_000000002220); ans_temp[ans_id*6+2]+=Pmtrx[6]*(P_001021000*QR_002000000000+P_001121000*QR_002000000010+P_001221000*QR_002000000020+P_001321000*QR_002000000030+P_101021000*QR_002000000100+P_101121000*QR_002000000110+P_101221000*QR_002000000120+P_101321000*QR_002000000130); ans_temp[ans_id*6+2]+=Pmtrx[7]*(P_001021000*QR_001001000000+P_001121000*QR_001001000010+P_001221000*QR_001001000020+P_001321000*QR_001001000030+P_101021000*QR_001001000100+P_101121000*QR_001001000110+P_101221000*QR_001001000120+P_101321000*QR_001001000130); ans_temp[ans_id*6+2]+=Pmtrx[8]*(P_001021000*QR_000002000000+P_001121000*QR_000002000010+P_001221000*QR_000002000020+P_001321000*QR_000002000030+P_101021000*QR_000002000100+P_101121000*QR_000002000110+P_101221000*QR_000002000120+P_101321000*QR_000002000130); ans_temp[ans_id*6+2]+=Pmtrx[9]*(P_001021000*QR_001000001000+P_001121000*QR_001000001010+P_001221000*QR_001000001020+P_001321000*QR_001000001030+P_101021000*QR_001000001100+P_101121000*QR_001000001110+P_101221000*QR_001000001120+P_101321000*QR_001000001130); ans_temp[ans_id*6+2]+=Pmtrx[10]*(P_001021000*QR_000001001000+P_001121000*QR_000001001010+P_001221000*QR_000001001020+P_001321000*QR_000001001030+P_101021000*QR_000001001100+P_101121000*QR_000001001110+P_101221000*QR_000001001120+P_101321000*QR_000001001130); ans_temp[ans_id*6+2]+=Pmtrx[11]*(P_001021000*QR_000000002000+P_001121000*QR_000000002010+P_001221000*QR_000000002020+P_001321000*QR_000000002030+P_101021000*QR_000000002100+P_101121000*QR_000000002110+P_101221000*QR_000000002120+P_101321000*QR_000000002130); ans_temp[ans_id*6+2]+=Pmtrx[12]*(P_000022000*QR_002000000000+P_000122000*QR_002000000010+P_000222000*QR_002000000020+P_000322000*QR_002000000030+P_000422000*QR_002000000040); ans_temp[ans_id*6+2]+=Pmtrx[13]*(P_000022000*QR_001001000000+P_000122000*QR_001001000010+P_000222000*QR_001001000020+P_000322000*QR_001001000030+P_000422000*QR_001001000040); ans_temp[ans_id*6+2]+=Pmtrx[14]*(P_000022000*QR_000002000000+P_000122000*QR_000002000010+P_000222000*QR_000002000020+P_000322000*QR_000002000030+P_000422000*QR_000002000040); ans_temp[ans_id*6+2]+=Pmtrx[15]*(P_000022000*QR_001000001000+P_000122000*QR_001000001010+P_000222000*QR_001000001020+P_000322000*QR_001000001030+P_000422000*QR_001000001040); ans_temp[ans_id*6+2]+=Pmtrx[16]*(P_000022000*QR_000001001000+P_000122000*QR_000001001010+P_000222000*QR_000001001020+P_000322000*QR_000001001030+P_000422000*QR_000001001040); ans_temp[ans_id*6+2]+=Pmtrx[17]*(P_000022000*QR_000000002000+P_000122000*QR_000000002010+P_000222000*QR_000000002020+P_000322000*QR_000000002030+P_000422000*QR_000000002040); ans_temp[ans_id*6+2]+=Pmtrx[18]*(P_001020001*QR_002000000000+P_001020101*QR_002000000001+P_001120001*QR_002000000010+P_001120101*QR_002000000011+P_001220001*QR_002000000020+P_001220101*QR_002000000021+P_101020001*QR_002000000100+P_101020101*QR_002000000101+P_101120001*QR_002000000110+P_101120101*QR_002000000111+P_101220001*QR_002000000120+P_101220101*QR_002000000121); ans_temp[ans_id*6+2]+=Pmtrx[19]*(P_001020001*QR_001001000000+P_001020101*QR_001001000001+P_001120001*QR_001001000010+P_001120101*QR_001001000011+P_001220001*QR_001001000020+P_001220101*QR_001001000021+P_101020001*QR_001001000100+P_101020101*QR_001001000101+P_101120001*QR_001001000110+P_101120101*QR_001001000111+P_101220001*QR_001001000120+P_101220101*QR_001001000121); ans_temp[ans_id*6+2]+=Pmtrx[20]*(P_001020001*QR_000002000000+P_001020101*QR_000002000001+P_001120001*QR_000002000010+P_001120101*QR_000002000011+P_001220001*QR_000002000020+P_001220101*QR_000002000021+P_101020001*QR_000002000100+P_101020101*QR_000002000101+P_101120001*QR_000002000110+P_101120101*QR_000002000111+P_101220001*QR_000002000120+P_101220101*QR_000002000121); ans_temp[ans_id*6+2]+=Pmtrx[21]*(P_001020001*QR_001000001000+P_001020101*QR_001000001001+P_001120001*QR_001000001010+P_001120101*QR_001000001011+P_001220001*QR_001000001020+P_001220101*QR_001000001021+P_101020001*QR_001000001100+P_101020101*QR_001000001101+P_101120001*QR_001000001110+P_101120101*QR_001000001111+P_101220001*QR_001000001120+P_101220101*QR_001000001121); ans_temp[ans_id*6+2]+=Pmtrx[22]*(P_001020001*QR_000001001000+P_001020101*QR_000001001001+P_001120001*QR_000001001010+P_001120101*QR_000001001011+P_001220001*QR_000001001020+P_001220101*QR_000001001021+P_101020001*QR_000001001100+P_101020101*QR_000001001101+P_101120001*QR_000001001110+P_101120101*QR_000001001111+P_101220001*QR_000001001120+P_101220101*QR_000001001121); ans_temp[ans_id*6+2]+=Pmtrx[23]*(P_001020001*QR_000000002000+P_001020101*QR_000000002001+P_001120001*QR_000000002010+P_001120101*QR_000000002011+P_001220001*QR_000000002020+P_001220101*QR_000000002021+P_101020001*QR_000000002100+P_101020101*QR_000000002101+P_101120001*QR_000000002110+P_101120101*QR_000000002111+P_101220001*QR_000000002120+P_101220101*QR_000000002121); ans_temp[ans_id*6+2]+=Pmtrx[24]*(P_000021001*QR_002000000000+P_000021101*QR_002000000001+P_000121001*QR_002000000010+P_000121101*QR_002000000011+P_000221001*QR_002000000020+P_000221101*QR_002000000021+P_000321001*QR_002000000030+P_000321101*QR_002000000031); ans_temp[ans_id*6+2]+=Pmtrx[25]*(P_000021001*QR_001001000000+P_000021101*QR_001001000001+P_000121001*QR_001001000010+P_000121101*QR_001001000011+P_000221001*QR_001001000020+P_000221101*QR_001001000021+P_000321001*QR_001001000030+P_000321101*QR_001001000031); ans_temp[ans_id*6+2]+=Pmtrx[26]*(P_000021001*QR_000002000000+P_000021101*QR_000002000001+P_000121001*QR_000002000010+P_000121101*QR_000002000011+P_000221001*QR_000002000020+P_000221101*QR_000002000021+P_000321001*QR_000002000030+P_000321101*QR_000002000031); ans_temp[ans_id*6+2]+=Pmtrx[27]*(P_000021001*QR_001000001000+P_000021101*QR_001000001001+P_000121001*QR_001000001010+P_000121101*QR_001000001011+P_000221001*QR_001000001020+P_000221101*QR_001000001021+P_000321001*QR_001000001030+P_000321101*QR_001000001031); ans_temp[ans_id*6+2]+=Pmtrx[28]*(P_000021001*QR_000001001000+P_000021101*QR_000001001001+P_000121001*QR_000001001010+P_000121101*QR_000001001011+P_000221001*QR_000001001020+P_000221101*QR_000001001021+P_000321001*QR_000001001030+P_000321101*QR_000001001031); ans_temp[ans_id*6+2]+=Pmtrx[29]*(P_000021001*QR_000000002000+P_000021101*QR_000000002001+P_000121001*QR_000000002010+P_000121101*QR_000000002011+P_000221001*QR_000000002020+P_000221101*QR_000000002021+P_000321001*QR_000000002030+P_000321101*QR_000000002031); ans_temp[ans_id*6+2]+=Pmtrx[30]*(P_000020002*QR_002000000000+P_000020102*QR_002000000001+P_000020202*QR_002000000002+P_000120002*QR_002000000010+P_000120102*QR_002000000011+P_000120202*QR_002000000012+P_000220002*QR_002000000020+P_000220102*QR_002000000021+P_000220202*QR_002000000022); ans_temp[ans_id*6+2]+=Pmtrx[31]*(P_000020002*QR_001001000000+P_000020102*QR_001001000001+P_000020202*QR_001001000002+P_000120002*QR_001001000010+P_000120102*QR_001001000011+P_000120202*QR_001001000012+P_000220002*QR_001001000020+P_000220102*QR_001001000021+P_000220202*QR_001001000022); ans_temp[ans_id*6+2]+=Pmtrx[32]*(P_000020002*QR_000002000000+P_000020102*QR_000002000001+P_000020202*QR_000002000002+P_000120002*QR_000002000010+P_000120102*QR_000002000011+P_000120202*QR_000002000012+P_000220002*QR_000002000020+P_000220102*QR_000002000021+P_000220202*QR_000002000022); ans_temp[ans_id*6+2]+=Pmtrx[33]*(P_000020002*QR_001000001000+P_000020102*QR_001000001001+P_000020202*QR_001000001002+P_000120002*QR_001000001010+P_000120102*QR_001000001011+P_000120202*QR_001000001012+P_000220002*QR_001000001020+P_000220102*QR_001000001021+P_000220202*QR_001000001022); ans_temp[ans_id*6+2]+=Pmtrx[34]*(P_000020002*QR_000001001000+P_000020102*QR_000001001001+P_000020202*QR_000001001002+P_000120002*QR_000001001010+P_000120102*QR_000001001011+P_000120202*QR_000001001012+P_000220002*QR_000001001020+P_000220102*QR_000001001021+P_000220202*QR_000001001022); ans_temp[ans_id*6+2]+=Pmtrx[35]*(P_000020002*QR_000000002000+P_000020102*QR_000000002001+P_000020202*QR_000000002002+P_000120002*QR_000000002010+P_000120102*QR_000000002011+P_000120202*QR_000000002012+P_000220002*QR_000000002020+P_000220102*QR_000000002021+P_000220202*QR_000000002022); ans_temp[ans_id*6+3]+=Pmtrx[0]*(P_012000010*QR_002000000000+P_012000110*QR_002000000001+P_112000010*QR_002000000100+P_112000110*QR_002000000101+P_212000010*QR_002000000200+P_212000110*QR_002000000201+P_312000010*QR_002000000300+P_312000110*QR_002000000301); ans_temp[ans_id*6+3]+=Pmtrx[1]*(P_012000010*QR_001001000000+P_012000110*QR_001001000001+P_112000010*QR_001001000100+P_112000110*QR_001001000101+P_212000010*QR_001001000200+P_212000110*QR_001001000201+P_312000010*QR_001001000300+P_312000110*QR_001001000301); ans_temp[ans_id*6+3]+=Pmtrx[2]*(P_012000010*QR_000002000000+P_012000110*QR_000002000001+P_112000010*QR_000002000100+P_112000110*QR_000002000101+P_212000010*QR_000002000200+P_212000110*QR_000002000201+P_312000010*QR_000002000300+P_312000110*QR_000002000301); ans_temp[ans_id*6+3]+=Pmtrx[3]*(P_012000010*QR_001000001000+P_012000110*QR_001000001001+P_112000010*QR_001000001100+P_112000110*QR_001000001101+P_212000010*QR_001000001200+P_212000110*QR_001000001201+P_312000010*QR_001000001300+P_312000110*QR_001000001301); ans_temp[ans_id*6+3]+=Pmtrx[4]*(P_012000010*QR_000001001000+P_012000110*QR_000001001001+P_112000010*QR_000001001100+P_112000110*QR_000001001101+P_212000010*QR_000001001200+P_212000110*QR_000001001201+P_312000010*QR_000001001300+P_312000110*QR_000001001301); ans_temp[ans_id*6+3]+=Pmtrx[5]*(P_012000010*QR_000000002000+P_012000110*QR_000000002001+P_112000010*QR_000000002100+P_112000110*QR_000000002101+P_212000010*QR_000000002200+P_212000110*QR_000000002201+P_312000010*QR_000000002300+P_312000110*QR_000000002301); ans_temp[ans_id*6+3]+=Pmtrx[6]*(P_011001010*QR_002000000000+P_011001110*QR_002000000001+P_011101010*QR_002000000010+P_011101110*QR_002000000011+P_111001010*QR_002000000100+P_111001110*QR_002000000101+P_111101010*QR_002000000110+P_111101110*QR_002000000111+P_211001010*QR_002000000200+P_211001110*QR_002000000201+P_211101010*QR_002000000210+P_211101110*QR_002000000211); ans_temp[ans_id*6+3]+=Pmtrx[7]*(P_011001010*QR_001001000000+P_011001110*QR_001001000001+P_011101010*QR_001001000010+P_011101110*QR_001001000011+P_111001010*QR_001001000100+P_111001110*QR_001001000101+P_111101010*QR_001001000110+P_111101110*QR_001001000111+P_211001010*QR_001001000200+P_211001110*QR_001001000201+P_211101010*QR_001001000210+P_211101110*QR_001001000211); ans_temp[ans_id*6+3]+=Pmtrx[8]*(P_011001010*QR_000002000000+P_011001110*QR_000002000001+P_011101010*QR_000002000010+P_011101110*QR_000002000011+P_111001010*QR_000002000100+P_111001110*QR_000002000101+P_111101010*QR_000002000110+P_111101110*QR_000002000111+P_211001010*QR_000002000200+P_211001110*QR_000002000201+P_211101010*QR_000002000210+P_211101110*QR_000002000211); ans_temp[ans_id*6+3]+=Pmtrx[9]*(P_011001010*QR_001000001000+P_011001110*QR_001000001001+P_011101010*QR_001000001010+P_011101110*QR_001000001011+P_111001010*QR_001000001100+P_111001110*QR_001000001101+P_111101010*QR_001000001110+P_111101110*QR_001000001111+P_211001010*QR_001000001200+P_211001110*QR_001000001201+P_211101010*QR_001000001210+P_211101110*QR_001000001211); ans_temp[ans_id*6+3]+=Pmtrx[10]*(P_011001010*QR_000001001000+P_011001110*QR_000001001001+P_011101010*QR_000001001010+P_011101110*QR_000001001011+P_111001010*QR_000001001100+P_111001110*QR_000001001101+P_111101010*QR_000001001110+P_111101110*QR_000001001111+P_211001010*QR_000001001200+P_211001110*QR_000001001201+P_211101010*QR_000001001210+P_211101110*QR_000001001211); ans_temp[ans_id*6+3]+=Pmtrx[11]*(P_011001010*QR_000000002000+P_011001110*QR_000000002001+P_011101010*QR_000000002010+P_011101110*QR_000000002011+P_111001010*QR_000000002100+P_111001110*QR_000000002101+P_111101010*QR_000000002110+P_111101110*QR_000000002111+P_211001010*QR_000000002200+P_211001110*QR_000000002201+P_211101010*QR_000000002210+P_211101110*QR_000000002211); ans_temp[ans_id*6+3]+=Pmtrx[12]*(P_010002010*QR_002000000000+P_010002110*QR_002000000001+P_010102010*QR_002000000010+P_010102110*QR_002000000011+P_010202010*QR_002000000020+P_010202110*QR_002000000021+P_110002010*QR_002000000100+P_110002110*QR_002000000101+P_110102010*QR_002000000110+P_110102110*QR_002000000111+P_110202010*QR_002000000120+P_110202110*QR_002000000121); ans_temp[ans_id*6+3]+=Pmtrx[13]*(P_010002010*QR_001001000000+P_010002110*QR_001001000001+P_010102010*QR_001001000010+P_010102110*QR_001001000011+P_010202010*QR_001001000020+P_010202110*QR_001001000021+P_110002010*QR_001001000100+P_110002110*QR_001001000101+P_110102010*QR_001001000110+P_110102110*QR_001001000111+P_110202010*QR_001001000120+P_110202110*QR_001001000121); ans_temp[ans_id*6+3]+=Pmtrx[14]*(P_010002010*QR_000002000000+P_010002110*QR_000002000001+P_010102010*QR_000002000010+P_010102110*QR_000002000011+P_010202010*QR_000002000020+P_010202110*QR_000002000021+P_110002010*QR_000002000100+P_110002110*QR_000002000101+P_110102010*QR_000002000110+P_110102110*QR_000002000111+P_110202010*QR_000002000120+P_110202110*QR_000002000121); ans_temp[ans_id*6+3]+=Pmtrx[15]*(P_010002010*QR_001000001000+P_010002110*QR_001000001001+P_010102010*QR_001000001010+P_010102110*QR_001000001011+P_010202010*QR_001000001020+P_010202110*QR_001000001021+P_110002010*QR_001000001100+P_110002110*QR_001000001101+P_110102010*QR_001000001110+P_110102110*QR_001000001111+P_110202010*QR_001000001120+P_110202110*QR_001000001121); ans_temp[ans_id*6+3]+=Pmtrx[16]*(P_010002010*QR_000001001000+P_010002110*QR_000001001001+P_010102010*QR_000001001010+P_010102110*QR_000001001011+P_010202010*QR_000001001020+P_010202110*QR_000001001021+P_110002010*QR_000001001100+P_110002110*QR_000001001101+P_110102010*QR_000001001110+P_110102110*QR_000001001111+P_110202010*QR_000001001120+P_110202110*QR_000001001121); ans_temp[ans_id*6+3]+=Pmtrx[17]*(P_010002010*QR_000000002000+P_010002110*QR_000000002001+P_010102010*QR_000000002010+P_010102110*QR_000000002011+P_010202010*QR_000000002020+P_010202110*QR_000000002021+P_110002010*QR_000000002100+P_110002110*QR_000000002101+P_110102010*QR_000000002110+P_110102110*QR_000000002111+P_110202010*QR_000000002120+P_110202110*QR_000000002121); ans_temp[ans_id*6+3]+=Pmtrx[18]*(P_011000011*QR_002000000000+P_011000111*QR_002000000001+P_011000211*QR_002000000002+P_111000011*QR_002000000100+P_111000111*QR_002000000101+P_111000211*QR_002000000102+P_211000011*QR_002000000200+P_211000111*QR_002000000201+P_211000211*QR_002000000202); ans_temp[ans_id*6+3]+=Pmtrx[19]*(P_011000011*QR_001001000000+P_011000111*QR_001001000001+P_011000211*QR_001001000002+P_111000011*QR_001001000100+P_111000111*QR_001001000101+P_111000211*QR_001001000102+P_211000011*QR_001001000200+P_211000111*QR_001001000201+P_211000211*QR_001001000202); ans_temp[ans_id*6+3]+=Pmtrx[20]*(P_011000011*QR_000002000000+P_011000111*QR_000002000001+P_011000211*QR_000002000002+P_111000011*QR_000002000100+P_111000111*QR_000002000101+P_111000211*QR_000002000102+P_211000011*QR_000002000200+P_211000111*QR_000002000201+P_211000211*QR_000002000202); ans_temp[ans_id*6+3]+=Pmtrx[21]*(P_011000011*QR_001000001000+P_011000111*QR_001000001001+P_011000211*QR_001000001002+P_111000011*QR_001000001100+P_111000111*QR_001000001101+P_111000211*QR_001000001102+P_211000011*QR_001000001200+P_211000111*QR_001000001201+P_211000211*QR_001000001202); ans_temp[ans_id*6+3]+=Pmtrx[22]*(P_011000011*QR_000001001000+P_011000111*QR_000001001001+P_011000211*QR_000001001002+P_111000011*QR_000001001100+P_111000111*QR_000001001101+P_111000211*QR_000001001102+P_211000011*QR_000001001200+P_211000111*QR_000001001201+P_211000211*QR_000001001202); ans_temp[ans_id*6+3]+=Pmtrx[23]*(P_011000011*QR_000000002000+P_011000111*QR_000000002001+P_011000211*QR_000000002002+P_111000011*QR_000000002100+P_111000111*QR_000000002101+P_111000211*QR_000000002102+P_211000011*QR_000000002200+P_211000111*QR_000000002201+P_211000211*QR_000000002202); ans_temp[ans_id*6+3]+=Pmtrx[24]*(P_010001011*QR_002000000000+P_010001111*QR_002000000001+P_010001211*QR_002000000002+P_010101011*QR_002000000010+P_010101111*QR_002000000011+P_010101211*QR_002000000012+P_110001011*QR_002000000100+P_110001111*QR_002000000101+P_110001211*QR_002000000102+P_110101011*QR_002000000110+P_110101111*QR_002000000111+P_110101211*QR_002000000112); ans_temp[ans_id*6+3]+=Pmtrx[25]*(P_010001011*QR_001001000000+P_010001111*QR_001001000001+P_010001211*QR_001001000002+P_010101011*QR_001001000010+P_010101111*QR_001001000011+P_010101211*QR_001001000012+P_110001011*QR_001001000100+P_110001111*QR_001001000101+P_110001211*QR_001001000102+P_110101011*QR_001001000110+P_110101111*QR_001001000111+P_110101211*QR_001001000112); ans_temp[ans_id*6+3]+=Pmtrx[26]*(P_010001011*QR_000002000000+P_010001111*QR_000002000001+P_010001211*QR_000002000002+P_010101011*QR_000002000010+P_010101111*QR_000002000011+P_010101211*QR_000002000012+P_110001011*QR_000002000100+P_110001111*QR_000002000101+P_110001211*QR_000002000102+P_110101011*QR_000002000110+P_110101111*QR_000002000111+P_110101211*QR_000002000112); ans_temp[ans_id*6+3]+=Pmtrx[27]*(P_010001011*QR_001000001000+P_010001111*QR_001000001001+P_010001211*QR_001000001002+P_010101011*QR_001000001010+P_010101111*QR_001000001011+P_010101211*QR_001000001012+P_110001011*QR_001000001100+P_110001111*QR_001000001101+P_110001211*QR_001000001102+P_110101011*QR_001000001110+P_110101111*QR_001000001111+P_110101211*QR_001000001112); ans_temp[ans_id*6+3]+=Pmtrx[28]*(P_010001011*QR_000001001000+P_010001111*QR_000001001001+P_010001211*QR_000001001002+P_010101011*QR_000001001010+P_010101111*QR_000001001011+P_010101211*QR_000001001012+P_110001011*QR_000001001100+P_110001111*QR_000001001101+P_110001211*QR_000001001102+P_110101011*QR_000001001110+P_110101111*QR_000001001111+P_110101211*QR_000001001112); ans_temp[ans_id*6+3]+=Pmtrx[29]*(P_010001011*QR_000000002000+P_010001111*QR_000000002001+P_010001211*QR_000000002002+P_010101011*QR_000000002010+P_010101111*QR_000000002011+P_010101211*QR_000000002012+P_110001011*QR_000000002100+P_110001111*QR_000000002101+P_110001211*QR_000000002102+P_110101011*QR_000000002110+P_110101111*QR_000000002111+P_110101211*QR_000000002112); ans_temp[ans_id*6+3]+=Pmtrx[30]*(P_010000012*QR_002000000000+P_010000112*QR_002000000001+P_010000212*QR_002000000002+P_010000312*QR_002000000003+P_110000012*QR_002000000100+P_110000112*QR_002000000101+P_110000212*QR_002000000102+P_110000312*QR_002000000103); ans_temp[ans_id*6+3]+=Pmtrx[31]*(P_010000012*QR_001001000000+P_010000112*QR_001001000001+P_010000212*QR_001001000002+P_010000312*QR_001001000003+P_110000012*QR_001001000100+P_110000112*QR_001001000101+P_110000212*QR_001001000102+P_110000312*QR_001001000103); ans_temp[ans_id*6+3]+=Pmtrx[32]*(P_010000012*QR_000002000000+P_010000112*QR_000002000001+P_010000212*QR_000002000002+P_010000312*QR_000002000003+P_110000012*QR_000002000100+P_110000112*QR_000002000101+P_110000212*QR_000002000102+P_110000312*QR_000002000103); ans_temp[ans_id*6+3]+=Pmtrx[33]*(P_010000012*QR_001000001000+P_010000112*QR_001000001001+P_010000212*QR_001000001002+P_010000312*QR_001000001003+P_110000012*QR_001000001100+P_110000112*QR_001000001101+P_110000212*QR_001000001102+P_110000312*QR_001000001103); ans_temp[ans_id*6+3]+=Pmtrx[34]*(P_010000012*QR_000001001000+P_010000112*QR_000001001001+P_010000212*QR_000001001002+P_010000312*QR_000001001003+P_110000012*QR_000001001100+P_110000112*QR_000001001101+P_110000212*QR_000001001102+P_110000312*QR_000001001103); ans_temp[ans_id*6+3]+=Pmtrx[35]*(P_010000012*QR_000000002000+P_010000112*QR_000000002001+P_010000212*QR_000000002002+P_010000312*QR_000000002003+P_110000012*QR_000000002100+P_110000112*QR_000000002101+P_110000212*QR_000000002102+P_110000312*QR_000000002103); ans_temp[ans_id*6+4]+=Pmtrx[0]*(P_002010010*QR_002000000000+P_002010110*QR_002000000001+P_002110010*QR_002000000010+P_002110110*QR_002000000011+P_102010010*QR_002000000100+P_102010110*QR_002000000101+P_102110010*QR_002000000110+P_102110110*QR_002000000111+P_202010010*QR_002000000200+P_202010110*QR_002000000201+P_202110010*QR_002000000210+P_202110110*QR_002000000211); ans_temp[ans_id*6+4]+=Pmtrx[1]*(P_002010010*QR_001001000000+P_002010110*QR_001001000001+P_002110010*QR_001001000010+P_002110110*QR_001001000011+P_102010010*QR_001001000100+P_102010110*QR_001001000101+P_102110010*QR_001001000110+P_102110110*QR_001001000111+P_202010010*QR_001001000200+P_202010110*QR_001001000201+P_202110010*QR_001001000210+P_202110110*QR_001001000211); ans_temp[ans_id*6+4]+=Pmtrx[2]*(P_002010010*QR_000002000000+P_002010110*QR_000002000001+P_002110010*QR_000002000010+P_002110110*QR_000002000011+P_102010010*QR_000002000100+P_102010110*QR_000002000101+P_102110010*QR_000002000110+P_102110110*QR_000002000111+P_202010010*QR_000002000200+P_202010110*QR_000002000201+P_202110010*QR_000002000210+P_202110110*QR_000002000211); ans_temp[ans_id*6+4]+=Pmtrx[3]*(P_002010010*QR_001000001000+P_002010110*QR_001000001001+P_002110010*QR_001000001010+P_002110110*QR_001000001011+P_102010010*QR_001000001100+P_102010110*QR_001000001101+P_102110010*QR_001000001110+P_102110110*QR_001000001111+P_202010010*QR_001000001200+P_202010110*QR_001000001201+P_202110010*QR_001000001210+P_202110110*QR_001000001211); ans_temp[ans_id*6+4]+=Pmtrx[4]*(P_002010010*QR_000001001000+P_002010110*QR_000001001001+P_002110010*QR_000001001010+P_002110110*QR_000001001011+P_102010010*QR_000001001100+P_102010110*QR_000001001101+P_102110010*QR_000001001110+P_102110110*QR_000001001111+P_202010010*QR_000001001200+P_202010110*QR_000001001201+P_202110010*QR_000001001210+P_202110110*QR_000001001211); ans_temp[ans_id*6+4]+=Pmtrx[5]*(P_002010010*QR_000000002000+P_002010110*QR_000000002001+P_002110010*QR_000000002010+P_002110110*QR_000000002011+P_102010010*QR_000000002100+P_102010110*QR_000000002101+P_102110010*QR_000000002110+P_102110110*QR_000000002111+P_202010010*QR_000000002200+P_202010110*QR_000000002201+P_202110010*QR_000000002210+P_202110110*QR_000000002211); ans_temp[ans_id*6+4]+=Pmtrx[6]*(P_001011010*QR_002000000000+P_001011110*QR_002000000001+P_001111010*QR_002000000010+P_001111110*QR_002000000011+P_001211010*QR_002000000020+P_001211110*QR_002000000021+P_101011010*QR_002000000100+P_101011110*QR_002000000101+P_101111010*QR_002000000110+P_101111110*QR_002000000111+P_101211010*QR_002000000120+P_101211110*QR_002000000121); ans_temp[ans_id*6+4]+=Pmtrx[7]*(P_001011010*QR_001001000000+P_001011110*QR_001001000001+P_001111010*QR_001001000010+P_001111110*QR_001001000011+P_001211010*QR_001001000020+P_001211110*QR_001001000021+P_101011010*QR_001001000100+P_101011110*QR_001001000101+P_101111010*QR_001001000110+P_101111110*QR_001001000111+P_101211010*QR_001001000120+P_101211110*QR_001001000121); ans_temp[ans_id*6+4]+=Pmtrx[8]*(P_001011010*QR_000002000000+P_001011110*QR_000002000001+P_001111010*QR_000002000010+P_001111110*QR_000002000011+P_001211010*QR_000002000020+P_001211110*QR_000002000021+P_101011010*QR_000002000100+P_101011110*QR_000002000101+P_101111010*QR_000002000110+P_101111110*QR_000002000111+P_101211010*QR_000002000120+P_101211110*QR_000002000121); ans_temp[ans_id*6+4]+=Pmtrx[9]*(P_001011010*QR_001000001000+P_001011110*QR_001000001001+P_001111010*QR_001000001010+P_001111110*QR_001000001011+P_001211010*QR_001000001020+P_001211110*QR_001000001021+P_101011010*QR_001000001100+P_101011110*QR_001000001101+P_101111010*QR_001000001110+P_101111110*QR_001000001111+P_101211010*QR_001000001120+P_101211110*QR_001000001121); ans_temp[ans_id*6+4]+=Pmtrx[10]*(P_001011010*QR_000001001000+P_001011110*QR_000001001001+P_001111010*QR_000001001010+P_001111110*QR_000001001011+P_001211010*QR_000001001020+P_001211110*QR_000001001021+P_101011010*QR_000001001100+P_101011110*QR_000001001101+P_101111010*QR_000001001110+P_101111110*QR_000001001111+P_101211010*QR_000001001120+P_101211110*QR_000001001121); ans_temp[ans_id*6+4]+=Pmtrx[11]*(P_001011010*QR_000000002000+P_001011110*QR_000000002001+P_001111010*QR_000000002010+P_001111110*QR_000000002011+P_001211010*QR_000000002020+P_001211110*QR_000000002021+P_101011010*QR_000000002100+P_101011110*QR_000000002101+P_101111010*QR_000000002110+P_101111110*QR_000000002111+P_101211010*QR_000000002120+P_101211110*QR_000000002121); ans_temp[ans_id*6+4]+=Pmtrx[12]*(P_000012010*QR_002000000000+P_000012110*QR_002000000001+P_000112010*QR_002000000010+P_000112110*QR_002000000011+P_000212010*QR_002000000020+P_000212110*QR_002000000021+P_000312010*QR_002000000030+P_000312110*QR_002000000031); ans_temp[ans_id*6+4]+=Pmtrx[13]*(P_000012010*QR_001001000000+P_000012110*QR_001001000001+P_000112010*QR_001001000010+P_000112110*QR_001001000011+P_000212010*QR_001001000020+P_000212110*QR_001001000021+P_000312010*QR_001001000030+P_000312110*QR_001001000031); ans_temp[ans_id*6+4]+=Pmtrx[14]*(P_000012010*QR_000002000000+P_000012110*QR_000002000001+P_000112010*QR_000002000010+P_000112110*QR_000002000011+P_000212010*QR_000002000020+P_000212110*QR_000002000021+P_000312010*QR_000002000030+P_000312110*QR_000002000031); ans_temp[ans_id*6+4]+=Pmtrx[15]*(P_000012010*QR_001000001000+P_000012110*QR_001000001001+P_000112010*QR_001000001010+P_000112110*QR_001000001011+P_000212010*QR_001000001020+P_000212110*QR_001000001021+P_000312010*QR_001000001030+P_000312110*QR_001000001031); ans_temp[ans_id*6+4]+=Pmtrx[16]*(P_000012010*QR_000001001000+P_000012110*QR_000001001001+P_000112010*QR_000001001010+P_000112110*QR_000001001011+P_000212010*QR_000001001020+P_000212110*QR_000001001021+P_000312010*QR_000001001030+P_000312110*QR_000001001031); ans_temp[ans_id*6+4]+=Pmtrx[17]*(P_000012010*QR_000000002000+P_000012110*QR_000000002001+P_000112010*QR_000000002010+P_000112110*QR_000000002011+P_000212010*QR_000000002020+P_000212110*QR_000000002021+P_000312010*QR_000000002030+P_000312110*QR_000000002031); ans_temp[ans_id*6+4]+=Pmtrx[18]*(P_001010011*QR_002000000000+P_001010111*QR_002000000001+P_001010211*QR_002000000002+P_001110011*QR_002000000010+P_001110111*QR_002000000011+P_001110211*QR_002000000012+P_101010011*QR_002000000100+P_101010111*QR_002000000101+P_101010211*QR_002000000102+P_101110011*QR_002000000110+P_101110111*QR_002000000111+P_101110211*QR_002000000112); ans_temp[ans_id*6+4]+=Pmtrx[19]*(P_001010011*QR_001001000000+P_001010111*QR_001001000001+P_001010211*QR_001001000002+P_001110011*QR_001001000010+P_001110111*QR_001001000011+P_001110211*QR_001001000012+P_101010011*QR_001001000100+P_101010111*QR_001001000101+P_101010211*QR_001001000102+P_101110011*QR_001001000110+P_101110111*QR_001001000111+P_101110211*QR_001001000112); ans_temp[ans_id*6+4]+=Pmtrx[20]*(P_001010011*QR_000002000000+P_001010111*QR_000002000001+P_001010211*QR_000002000002+P_001110011*QR_000002000010+P_001110111*QR_000002000011+P_001110211*QR_000002000012+P_101010011*QR_000002000100+P_101010111*QR_000002000101+P_101010211*QR_000002000102+P_101110011*QR_000002000110+P_101110111*QR_000002000111+P_101110211*QR_000002000112); ans_temp[ans_id*6+4]+=Pmtrx[21]*(P_001010011*QR_001000001000+P_001010111*QR_001000001001+P_001010211*QR_001000001002+P_001110011*QR_001000001010+P_001110111*QR_001000001011+P_001110211*QR_001000001012+P_101010011*QR_001000001100+P_101010111*QR_001000001101+P_101010211*QR_001000001102+P_101110011*QR_001000001110+P_101110111*QR_001000001111+P_101110211*QR_001000001112); ans_temp[ans_id*6+4]+=Pmtrx[22]*(P_001010011*QR_000001001000+P_001010111*QR_000001001001+P_001010211*QR_000001001002+P_001110011*QR_000001001010+P_001110111*QR_000001001011+P_001110211*QR_000001001012+P_101010011*QR_000001001100+P_101010111*QR_000001001101+P_101010211*QR_000001001102+P_101110011*QR_000001001110+P_101110111*QR_000001001111+P_101110211*QR_000001001112); ans_temp[ans_id*6+4]+=Pmtrx[23]*(P_001010011*QR_000000002000+P_001010111*QR_000000002001+P_001010211*QR_000000002002+P_001110011*QR_000000002010+P_001110111*QR_000000002011+P_001110211*QR_000000002012+P_101010011*QR_000000002100+P_101010111*QR_000000002101+P_101010211*QR_000000002102+P_101110011*QR_000000002110+P_101110111*QR_000000002111+P_101110211*QR_000000002112); ans_temp[ans_id*6+4]+=Pmtrx[24]*(P_000011011*QR_002000000000+P_000011111*QR_002000000001+P_000011211*QR_002000000002+P_000111011*QR_002000000010+P_000111111*QR_002000000011+P_000111211*QR_002000000012+P_000211011*QR_002000000020+P_000211111*QR_002000000021+P_000211211*QR_002000000022); ans_temp[ans_id*6+4]+=Pmtrx[25]*(P_000011011*QR_001001000000+P_000011111*QR_001001000001+P_000011211*QR_001001000002+P_000111011*QR_001001000010+P_000111111*QR_001001000011+P_000111211*QR_001001000012+P_000211011*QR_001001000020+P_000211111*QR_001001000021+P_000211211*QR_001001000022); ans_temp[ans_id*6+4]+=Pmtrx[26]*(P_000011011*QR_000002000000+P_000011111*QR_000002000001+P_000011211*QR_000002000002+P_000111011*QR_000002000010+P_000111111*QR_000002000011+P_000111211*QR_000002000012+P_000211011*QR_000002000020+P_000211111*QR_000002000021+P_000211211*QR_000002000022); ans_temp[ans_id*6+4]+=Pmtrx[27]*(P_000011011*QR_001000001000+P_000011111*QR_001000001001+P_000011211*QR_001000001002+P_000111011*QR_001000001010+P_000111111*QR_001000001011+P_000111211*QR_001000001012+P_000211011*QR_001000001020+P_000211111*QR_001000001021+P_000211211*QR_001000001022); ans_temp[ans_id*6+4]+=Pmtrx[28]*(P_000011011*QR_000001001000+P_000011111*QR_000001001001+P_000011211*QR_000001001002+P_000111011*QR_000001001010+P_000111111*QR_000001001011+P_000111211*QR_000001001012+P_000211011*QR_000001001020+P_000211111*QR_000001001021+P_000211211*QR_000001001022); ans_temp[ans_id*6+4]+=Pmtrx[29]*(P_000011011*QR_000000002000+P_000011111*QR_000000002001+P_000011211*QR_000000002002+P_000111011*QR_000000002010+P_000111111*QR_000000002011+P_000111211*QR_000000002012+P_000211011*QR_000000002020+P_000211111*QR_000000002021+P_000211211*QR_000000002022); ans_temp[ans_id*6+4]+=Pmtrx[30]*(P_000010012*QR_002000000000+P_000010112*QR_002000000001+P_000010212*QR_002000000002+P_000010312*QR_002000000003+P_000110012*QR_002000000010+P_000110112*QR_002000000011+P_000110212*QR_002000000012+P_000110312*QR_002000000013); ans_temp[ans_id*6+4]+=Pmtrx[31]*(P_000010012*QR_001001000000+P_000010112*QR_001001000001+P_000010212*QR_001001000002+P_000010312*QR_001001000003+P_000110012*QR_001001000010+P_000110112*QR_001001000011+P_000110212*QR_001001000012+P_000110312*QR_001001000013); ans_temp[ans_id*6+4]+=Pmtrx[32]*(P_000010012*QR_000002000000+P_000010112*QR_000002000001+P_000010212*QR_000002000002+P_000010312*QR_000002000003+P_000110012*QR_000002000010+P_000110112*QR_000002000011+P_000110212*QR_000002000012+P_000110312*QR_000002000013); ans_temp[ans_id*6+4]+=Pmtrx[33]*(P_000010012*QR_001000001000+P_000010112*QR_001000001001+P_000010212*QR_001000001002+P_000010312*QR_001000001003+P_000110012*QR_001000001010+P_000110112*QR_001000001011+P_000110212*QR_001000001012+P_000110312*QR_001000001013); ans_temp[ans_id*6+4]+=Pmtrx[34]*(P_000010012*QR_000001001000+P_000010112*QR_000001001001+P_000010212*QR_000001001002+P_000010312*QR_000001001003+P_000110012*QR_000001001010+P_000110112*QR_000001001011+P_000110212*QR_000001001012+P_000110312*QR_000001001013); ans_temp[ans_id*6+4]+=Pmtrx[35]*(P_000010012*QR_000000002000+P_000010112*QR_000000002001+P_000010212*QR_000000002002+P_000010312*QR_000000002003+P_000110012*QR_000000002010+P_000110112*QR_000000002011+P_000110212*QR_000000002012+P_000110312*QR_000000002013); ans_temp[ans_id*6+5]+=Pmtrx[0]*(P_002000020*QR_002000000000+P_002000120*QR_002000000001+P_002000220*QR_002000000002+P_102000020*QR_002000000100+P_102000120*QR_002000000101+P_102000220*QR_002000000102+P_202000020*QR_002000000200+P_202000120*QR_002000000201+P_202000220*QR_002000000202); ans_temp[ans_id*6+5]+=Pmtrx[1]*(P_002000020*QR_001001000000+P_002000120*QR_001001000001+P_002000220*QR_001001000002+P_102000020*QR_001001000100+P_102000120*QR_001001000101+P_102000220*QR_001001000102+P_202000020*QR_001001000200+P_202000120*QR_001001000201+P_202000220*QR_001001000202); ans_temp[ans_id*6+5]+=Pmtrx[2]*(P_002000020*QR_000002000000+P_002000120*QR_000002000001+P_002000220*QR_000002000002+P_102000020*QR_000002000100+P_102000120*QR_000002000101+P_102000220*QR_000002000102+P_202000020*QR_000002000200+P_202000120*QR_000002000201+P_202000220*QR_000002000202); ans_temp[ans_id*6+5]+=Pmtrx[3]*(P_002000020*QR_001000001000+P_002000120*QR_001000001001+P_002000220*QR_001000001002+P_102000020*QR_001000001100+P_102000120*QR_001000001101+P_102000220*QR_001000001102+P_202000020*QR_001000001200+P_202000120*QR_001000001201+P_202000220*QR_001000001202); ans_temp[ans_id*6+5]+=Pmtrx[4]*(P_002000020*QR_000001001000+P_002000120*QR_000001001001+P_002000220*QR_000001001002+P_102000020*QR_000001001100+P_102000120*QR_000001001101+P_102000220*QR_000001001102+P_202000020*QR_000001001200+P_202000120*QR_000001001201+P_202000220*QR_000001001202); ans_temp[ans_id*6+5]+=Pmtrx[5]*(P_002000020*QR_000000002000+P_002000120*QR_000000002001+P_002000220*QR_000000002002+P_102000020*QR_000000002100+P_102000120*QR_000000002101+P_102000220*QR_000000002102+P_202000020*QR_000000002200+P_202000120*QR_000000002201+P_202000220*QR_000000002202); ans_temp[ans_id*6+5]+=Pmtrx[6]*(P_001001020*QR_002000000000+P_001001120*QR_002000000001+P_001001220*QR_002000000002+P_001101020*QR_002000000010+P_001101120*QR_002000000011+P_001101220*QR_002000000012+P_101001020*QR_002000000100+P_101001120*QR_002000000101+P_101001220*QR_002000000102+P_101101020*QR_002000000110+P_101101120*QR_002000000111+P_101101220*QR_002000000112); ans_temp[ans_id*6+5]+=Pmtrx[7]*(P_001001020*QR_001001000000+P_001001120*QR_001001000001+P_001001220*QR_001001000002+P_001101020*QR_001001000010+P_001101120*QR_001001000011+P_001101220*QR_001001000012+P_101001020*QR_001001000100+P_101001120*QR_001001000101+P_101001220*QR_001001000102+P_101101020*QR_001001000110+P_101101120*QR_001001000111+P_101101220*QR_001001000112); ans_temp[ans_id*6+5]+=Pmtrx[8]*(P_001001020*QR_000002000000+P_001001120*QR_000002000001+P_001001220*QR_000002000002+P_001101020*QR_000002000010+P_001101120*QR_000002000011+P_001101220*QR_000002000012+P_101001020*QR_000002000100+P_101001120*QR_000002000101+P_101001220*QR_000002000102+P_101101020*QR_000002000110+P_101101120*QR_000002000111+P_101101220*QR_000002000112); ans_temp[ans_id*6+5]+=Pmtrx[9]*(P_001001020*QR_001000001000+P_001001120*QR_001000001001+P_001001220*QR_001000001002+P_001101020*QR_001000001010+P_001101120*QR_001000001011+P_001101220*QR_001000001012+P_101001020*QR_001000001100+P_101001120*QR_001000001101+P_101001220*QR_001000001102+P_101101020*QR_001000001110+P_101101120*QR_001000001111+P_101101220*QR_001000001112); ans_temp[ans_id*6+5]+=Pmtrx[10]*(P_001001020*QR_000001001000+P_001001120*QR_000001001001+P_001001220*QR_000001001002+P_001101020*QR_000001001010+P_001101120*QR_000001001011+P_001101220*QR_000001001012+P_101001020*QR_000001001100+P_101001120*QR_000001001101+P_101001220*QR_000001001102+P_101101020*QR_000001001110+P_101101120*QR_000001001111+P_101101220*QR_000001001112); ans_temp[ans_id*6+5]+=Pmtrx[11]*(P_001001020*QR_000000002000+P_001001120*QR_000000002001+P_001001220*QR_000000002002+P_001101020*QR_000000002010+P_001101120*QR_000000002011+P_001101220*QR_000000002012+P_101001020*QR_000000002100+P_101001120*QR_000000002101+P_101001220*QR_000000002102+P_101101020*QR_000000002110+P_101101120*QR_000000002111+P_101101220*QR_000000002112); ans_temp[ans_id*6+5]+=Pmtrx[12]*(P_000002020*QR_002000000000+P_000002120*QR_002000000001+P_000002220*QR_002000000002+P_000102020*QR_002000000010+P_000102120*QR_002000000011+P_000102220*QR_002000000012+P_000202020*QR_002000000020+P_000202120*QR_002000000021+P_000202220*QR_002000000022); ans_temp[ans_id*6+5]+=Pmtrx[13]*(P_000002020*QR_001001000000+P_000002120*QR_001001000001+P_000002220*QR_001001000002+P_000102020*QR_001001000010+P_000102120*QR_001001000011+P_000102220*QR_001001000012+P_000202020*QR_001001000020+P_000202120*QR_001001000021+P_000202220*QR_001001000022); ans_temp[ans_id*6+5]+=Pmtrx[14]*(P_000002020*QR_000002000000+P_000002120*QR_000002000001+P_000002220*QR_000002000002+P_000102020*QR_000002000010+P_000102120*QR_000002000011+P_000102220*QR_000002000012+P_000202020*QR_000002000020+P_000202120*QR_000002000021+P_000202220*QR_000002000022); ans_temp[ans_id*6+5]+=Pmtrx[15]*(P_000002020*QR_001000001000+P_000002120*QR_001000001001+P_000002220*QR_001000001002+P_000102020*QR_001000001010+P_000102120*QR_001000001011+P_000102220*QR_001000001012+P_000202020*QR_001000001020+P_000202120*QR_001000001021+P_000202220*QR_001000001022); ans_temp[ans_id*6+5]+=Pmtrx[16]*(P_000002020*QR_000001001000+P_000002120*QR_000001001001+P_000002220*QR_000001001002+P_000102020*QR_000001001010+P_000102120*QR_000001001011+P_000102220*QR_000001001012+P_000202020*QR_000001001020+P_000202120*QR_000001001021+P_000202220*QR_000001001022); ans_temp[ans_id*6+5]+=Pmtrx[17]*(P_000002020*QR_000000002000+P_000002120*QR_000000002001+P_000002220*QR_000000002002+P_000102020*QR_000000002010+P_000102120*QR_000000002011+P_000102220*QR_000000002012+P_000202020*QR_000000002020+P_000202120*QR_000000002021+P_000202220*QR_000000002022); ans_temp[ans_id*6+5]+=Pmtrx[18]*(P_001000021*QR_002000000000+P_001000121*QR_002000000001+P_001000221*QR_002000000002+P_001000321*QR_002000000003+P_101000021*QR_002000000100+P_101000121*QR_002000000101+P_101000221*QR_002000000102+P_101000321*QR_002000000103); ans_temp[ans_id*6+5]+=Pmtrx[19]*(P_001000021*QR_001001000000+P_001000121*QR_001001000001+P_001000221*QR_001001000002+P_001000321*QR_001001000003+P_101000021*QR_001001000100+P_101000121*QR_001001000101+P_101000221*QR_001001000102+P_101000321*QR_001001000103); ans_temp[ans_id*6+5]+=Pmtrx[20]*(P_001000021*QR_000002000000+P_001000121*QR_000002000001+P_001000221*QR_000002000002+P_001000321*QR_000002000003+P_101000021*QR_000002000100+P_101000121*QR_000002000101+P_101000221*QR_000002000102+P_101000321*QR_000002000103); ans_temp[ans_id*6+5]+=Pmtrx[21]*(P_001000021*QR_001000001000+P_001000121*QR_001000001001+P_001000221*QR_001000001002+P_001000321*QR_001000001003+P_101000021*QR_001000001100+P_101000121*QR_001000001101+P_101000221*QR_001000001102+P_101000321*QR_001000001103); ans_temp[ans_id*6+5]+=Pmtrx[22]*(P_001000021*QR_000001001000+P_001000121*QR_000001001001+P_001000221*QR_000001001002+P_001000321*QR_000001001003+P_101000021*QR_000001001100+P_101000121*QR_000001001101+P_101000221*QR_000001001102+P_101000321*QR_000001001103); ans_temp[ans_id*6+5]+=Pmtrx[23]*(P_001000021*QR_000000002000+P_001000121*QR_000000002001+P_001000221*QR_000000002002+P_001000321*QR_000000002003+P_101000021*QR_000000002100+P_101000121*QR_000000002101+P_101000221*QR_000000002102+P_101000321*QR_000000002103); ans_temp[ans_id*6+5]+=Pmtrx[24]*(P_000001021*QR_002000000000+P_000001121*QR_002000000001+P_000001221*QR_002000000002+P_000001321*QR_002000000003+P_000101021*QR_002000000010+P_000101121*QR_002000000011+P_000101221*QR_002000000012+P_000101321*QR_002000000013); ans_temp[ans_id*6+5]+=Pmtrx[25]*(P_000001021*QR_001001000000+P_000001121*QR_001001000001+P_000001221*QR_001001000002+P_000001321*QR_001001000003+P_000101021*QR_001001000010+P_000101121*QR_001001000011+P_000101221*QR_001001000012+P_000101321*QR_001001000013); ans_temp[ans_id*6+5]+=Pmtrx[26]*(P_000001021*QR_000002000000+P_000001121*QR_000002000001+P_000001221*QR_000002000002+P_000001321*QR_000002000003+P_000101021*QR_000002000010+P_000101121*QR_000002000011+P_000101221*QR_000002000012+P_000101321*QR_000002000013); ans_temp[ans_id*6+5]+=Pmtrx[27]*(P_000001021*QR_001000001000+P_000001121*QR_001000001001+P_000001221*QR_001000001002+P_000001321*QR_001000001003+P_000101021*QR_001000001010+P_000101121*QR_001000001011+P_000101221*QR_001000001012+P_000101321*QR_001000001013); ans_temp[ans_id*6+5]+=Pmtrx[28]*(P_000001021*QR_000001001000+P_000001121*QR_000001001001+P_000001221*QR_000001001002+P_000001321*QR_000001001003+P_000101021*QR_000001001010+P_000101121*QR_000001001011+P_000101221*QR_000001001012+P_000101321*QR_000001001013); ans_temp[ans_id*6+5]+=Pmtrx[29]*(P_000001021*QR_000000002000+P_000001121*QR_000000002001+P_000001221*QR_000000002002+P_000001321*QR_000000002003+P_000101021*QR_000000002010+P_000101121*QR_000000002011+P_000101221*QR_000000002012+P_000101321*QR_000000002013); ans_temp[ans_id*6+5]+=Pmtrx[30]*(P_000000022*QR_002000000000+P_000000122*QR_002000000001+P_000000222*QR_002000000002+P_000000322*QR_002000000003+P_000000422*QR_002000000004); ans_temp[ans_id*6+5]+=Pmtrx[31]*(P_000000022*QR_001001000000+P_000000122*QR_001001000001+P_000000222*QR_001001000002+P_000000322*QR_001001000003+P_000000422*QR_001001000004); ans_temp[ans_id*6+5]+=Pmtrx[32]*(P_000000022*QR_000002000000+P_000000122*QR_000002000001+P_000000222*QR_000002000002+P_000000322*QR_000002000003+P_000000422*QR_000002000004); ans_temp[ans_id*6+5]+=Pmtrx[33]*(P_000000022*QR_001000001000+P_000000122*QR_001000001001+P_000000222*QR_001000001002+P_000000322*QR_001000001003+P_000000422*QR_001000001004); ans_temp[ans_id*6+5]+=Pmtrx[34]*(P_000000022*QR_000001001000+P_000000122*QR_000001001001+P_000000222*QR_000001001002+P_000000322*QR_000001001003+P_000000422*QR_000001001004); ans_temp[ans_id*6+5]+=Pmtrx[35]*(P_000000022*QR_000000002000+P_000000122*QR_000000002001+P_000000222*QR_000000002002+P_000000322*QR_000000002003+P_000000422*QR_000000002004); } } __syncthreads(); int num_thread=NTHREAD/2; while (num_thread!=0){ __syncthreads(); if(tId_x<num_thread){ for(int ians=0;ians<6;ians++){ ans_temp[tId_x*6+ians]+=ans_temp[(tId_x+num_thread)*6+ians]; } } num_thread/=2; } if(tId_x==0){ for(int ians=0;ians<6;ians++){ ans[(i_contrc_bra*contrc_ket_num+j_contrc_ket)*6+ians]=ans_temp[(tId_x)*6+ians]; } } } } }
e2d58faf6372220e1144477332002843413ebbe5.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #ifndef THC_GENERIC_FILE #define THC_GENERIC_FILE "generic/VolumetricMaxUnpooling.cu" #else static inline void THNN_(VolumetricMaxUnpooling_shapeCheck)( THCState *state, THCTensor *input, THCTensor *gradOutput, THCIndexTensor *indices, int oT, int oW, int oH, int dT, int dW, int dH, int pT, int pW, int pH) { int inputSlices = 0; THCUNN_check_shape_indices(state, indices, input); THArgCheck(dT > 0 && dW > 0 && dH > 0, 10, "stride should be greater than zero, but got dT: %d dH: %d dW: %d", dT, dH, dW); if (THCTensor_(nDimension)(state, input) == 4) { inputSlices = THCTensor_(size)(state, input, 0); } else if (THCTensor_(nDimension)(state, input) == 5) { inputSlices = THCTensor_(size)(state, input, 1); } else { THArgCheck(false, 2, "4D or 5D tensor expected, got %d", THCTensor_(nDimension)(state, input)); } int dimw = 3; int dimh = 2; int dimt = 1; int dimn = 0; if (input->nDimension == 5) { dimt++; dimw++; dimh++; dimn++; } if (gradOutput != NULL) { if (oT != gradOutput->size[dimt] || oW != gradOutput->size[dimw] || oH != gradOutput->size[dimh]) { THError( "Inconsistent gradOutput size. oT= %d, oH= %d, oW= %d, gradOutput: %dx%dx%d", oT, oH, oW, gradOutput->size[dimt], gradOutput->size[dimh], gradOutput->size[dimw]); } THCUNN_check_dim_size(state, gradOutput, input->nDimension, dimn, inputSlices); } } void THNN_(VolumetricMaxUnpooling_updateOutput)( THCState *state, THCTensor *input, THCTensor *output, THCIndexTensor *indices, int outputTime, int outputWidth, int outputHeight, int dT, int dW, int dH, int padT, int padW, int padH) { int batchSize = 0; int inputSlices = 0; int inputTime = 0; int inputHeight = 0; int inputWidth = 0; THNN_(VolumetricMaxUnpooling_shapeCheck)( state, input, NULL, indices, outputTime, outputWidth, outputHeight, dT, dW, dH, padT, padW, padH); THCUNN_assertSameGPU(state, 3, input, indices, output); int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5; if (THCTensor_(nDimension)(state, input) == 4) { /* sizes */ batchSize = 1; inputSlices = THCTensor_(size)(state, input, 0); inputTime = THCTensor_(size)(state, input, 1); inputHeight = THCTensor_(size)(state, input, 2); inputWidth = THCTensor_(size)(state, input, 3); } else if (fiveDimensionalInput) { /* sizes */ batchSize = THCTensor_(size)(state, input, 0); inputSlices = THCTensor_(size)(state, input, 1); inputTime = THCTensor_(size)(state, input, 2); inputHeight = THCTensor_(size)(state, input, 3); inputWidth = THCTensor_(size)(state, input, 4); } if (!fiveDimensionalInput) /* 4D */ { /* resize output */ THCTensor_(resize4d)(state, output, inputSlices, outputTime, outputHeight, outputWidth); } else { /* 5D */ THCTensor_(resize5d)(state, output, batchSize, inputSlices, outputTime, outputHeight, outputWidth); } input = THCTensor_(newContiguous)(state, input); indices = THCIndexTensor_(newContiguous)(state, indices); THCTensor_(zero)(state, output); if (fiveDimensionalInput) { // Collapse batch and feature dimensions output = THCTensor_(newFoldBatchDim)(state, output); THCTensor *old_input = input; input = THCTensor_(newFoldBatchDim)(state, input); THCTensor_(free)(state, old_input); THCIndexTensor *old_indices = indices; indices = THCIndexTensor_(newFoldBatchDim)(state, indices); THCIndexTensor_(free)(state, old_indices); } else { THCTensor_(retain)(state, output); } THCDeviceTensor<real, 4> cudaInput; THCDeviceTensor<real, 4> cudaOutput; THCDeviceTensor<THCIndex_t, 4> cudaIndices; cudaInput = toDeviceTensor<real, 4>(state, input); cudaOutput = toDeviceTensor<real, 4>(state, output); cudaIndices = toDeviceTensor<THCIndex_t, 4>(state, indices); int totalZ = inputTime * inputSlices * batchSize; int offsetZ = 0; dim3 block(32, 8); while (totalZ > 0) { dim3 grid(THCCeilDiv(inputWidth, static_cast<int>(block.x)), THCCeilDiv(inputHeight, static_cast<int>(block.y)), totalZ > 65535 ? 65535 : totalZ); hipLaunchKernelGGL(( cuda_VolumetricMaxUnpooling_updateOutput), dim3(grid), dim3(block), 0, THCState_getCurrentStream(state), cudaInput, cudaIndices, cudaOutput, dT, dH, dW, padT, padH, padW, offsetZ); THCudaCheck(hipGetLastError()); totalZ -= 65535; offsetZ += 65535; } THCTensor_(free)(state, input); THCTensor_(free)(state, output); THCIndexTensor_(free)(state, indices); } void THNN_(VolumetricMaxUnpooling_updateGradInput)( THCState *state, THCTensor *input, THCTensor *gradOutput, THCTensor *gradInput, THCIndexTensor *indices, int outputTime, int outputWidth, int outputHeight, int dT, int dW, int dH, int padT, int padW, int padH) { int batchSize = 0; int inputSlices = 0; int inputTime = 0; int inputHeight = 0; int inputWidth = 0; THNN_(VolumetricMaxUnpooling_shapeCheck)( state, input, gradOutput, indices, outputTime, outputWidth, outputHeight, dT, dW, dH, padT, padW, padH); THCUNN_assertSameGPU(state, 4, input, indices, gradOutput, gradInput); int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5; if (!fiveDimensionalInput) /* 4D */ { batchSize = 1; inputSlices = THCTensor_(size)(state, input, 0); inputTime = THCTensor_(size)(state, input, 1); inputHeight = THCTensor_(size)(state, input, 2); inputWidth = THCTensor_(size)(state, input, 3); } else { batchSize = THCTensor_(size)(state, input, 0); inputSlices = THCTensor_(size)(state, input, 1); inputTime = THCTensor_(size)(state, input, 2); inputHeight = THCTensor_(size)(state, input, 3); inputWidth = THCTensor_(size)(state, input, 4); } input = THCTensor_(newContiguous)(state, input); THCTensor_(resizeAs)(state, gradInput, input); THCTensor_(zero)(state, gradInput); indices = THCIndexTensor_(newContiguous)(state, indices); gradOutput = THCTensor_(newContiguous)(state, gradOutput); if (fiveDimensionalInput) { gradInput = THCTensor_(newFoldBatchDim)(state, gradInput); THCIndexTensor *old_indices = indices; indices = THCIndexTensor_(newFoldBatchDim)(state, indices); THCIndexTensor_(free)(state, old_indices); THCTensor *old_gradOutput = gradOutput; gradOutput = THCTensor_(newFoldBatchDim)(state, gradOutput); THCTensor_(free)(state, old_gradOutput); } else { THCTensor_(retain)(state, gradInput); } // Collapse batch and feature dimensions THCDeviceTensor<real, 4> cudaGradInput; THCDeviceTensor<real, 4> cudaGradOutput; THCDeviceTensor<THCIndex_t, 4> cudaIndices; cudaGradInput = toDeviceTensor<real, 4>(state, gradInput); cudaGradOutput = toDeviceTensor<real, 4>(state, gradOutput); cudaIndices = toDeviceTensor<THCIndex_t, 4>(state, indices); int totalZ = inputTime * inputSlices * batchSize; int offsetZ = 0; dim3 block(32, 8); while (totalZ > 0) { dim3 grid(THCCeilDiv(inputWidth, static_cast<int>(block.x)), THCCeilDiv(inputHeight, static_cast<int>(block.y)), totalZ > 65535 ? 65535 : totalZ); hipLaunchKernelGGL(( cuda_VolumetricMaxUnpooling_updateGradInput), dim3(grid), dim3(block), 0, THCState_getCurrentStream(state), cudaGradOutput, cudaIndices, cudaGradInput, dT, dH, dW, padT, padH, padW, offsetZ); THCudaCheck(hipGetLastError()); totalZ -= 65535; offsetZ += 65535; } // cleanup THCTensor_(free)(state, gradOutput); THCTensor_(free)(state, gradInput); THCIndexTensor_(free)(state, indices); THCTensor_(free)(state, input); } #endif
e2d58faf6372220e1144477332002843413ebbe5.cu
#ifndef THC_GENERIC_FILE #define THC_GENERIC_FILE "generic/VolumetricMaxUnpooling.cu" #else static inline void THNN_(VolumetricMaxUnpooling_shapeCheck)( THCState *state, THCTensor *input, THCTensor *gradOutput, THCIndexTensor *indices, int oT, int oW, int oH, int dT, int dW, int dH, int pT, int pW, int pH) { int inputSlices = 0; THCUNN_check_shape_indices(state, indices, input); THArgCheck(dT > 0 && dW > 0 && dH > 0, 10, "stride should be greater than zero, but got dT: %d dH: %d dW: %d", dT, dH, dW); if (THCTensor_(nDimension)(state, input) == 4) { inputSlices = THCTensor_(size)(state, input, 0); } else if (THCTensor_(nDimension)(state, input) == 5) { inputSlices = THCTensor_(size)(state, input, 1); } else { THArgCheck(false, 2, "4D or 5D tensor expected, got %d", THCTensor_(nDimension)(state, input)); } int dimw = 3; int dimh = 2; int dimt = 1; int dimn = 0; if (input->nDimension == 5) { dimt++; dimw++; dimh++; dimn++; } if (gradOutput != NULL) { if (oT != gradOutput->size[dimt] || oW != gradOutput->size[dimw] || oH != gradOutput->size[dimh]) { THError( "Inconsistent gradOutput size. oT= %d, oH= %d, oW= %d, gradOutput: %dx%dx%d", oT, oH, oW, gradOutput->size[dimt], gradOutput->size[dimh], gradOutput->size[dimw]); } THCUNN_check_dim_size(state, gradOutput, input->nDimension, dimn, inputSlices); } } void THNN_(VolumetricMaxUnpooling_updateOutput)( THCState *state, THCTensor *input, THCTensor *output, THCIndexTensor *indices, int outputTime, int outputWidth, int outputHeight, int dT, int dW, int dH, int padT, int padW, int padH) { int batchSize = 0; int inputSlices = 0; int inputTime = 0; int inputHeight = 0; int inputWidth = 0; THNN_(VolumetricMaxUnpooling_shapeCheck)( state, input, NULL, indices, outputTime, outputWidth, outputHeight, dT, dW, dH, padT, padW, padH); THCUNN_assertSameGPU(state, 3, input, indices, output); int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5; if (THCTensor_(nDimension)(state, input) == 4) { /* sizes */ batchSize = 1; inputSlices = THCTensor_(size)(state, input, 0); inputTime = THCTensor_(size)(state, input, 1); inputHeight = THCTensor_(size)(state, input, 2); inputWidth = THCTensor_(size)(state, input, 3); } else if (fiveDimensionalInput) { /* sizes */ batchSize = THCTensor_(size)(state, input, 0); inputSlices = THCTensor_(size)(state, input, 1); inputTime = THCTensor_(size)(state, input, 2); inputHeight = THCTensor_(size)(state, input, 3); inputWidth = THCTensor_(size)(state, input, 4); } if (!fiveDimensionalInput) /* 4D */ { /* resize output */ THCTensor_(resize4d)(state, output, inputSlices, outputTime, outputHeight, outputWidth); } else { /* 5D */ THCTensor_(resize5d)(state, output, batchSize, inputSlices, outputTime, outputHeight, outputWidth); } input = THCTensor_(newContiguous)(state, input); indices = THCIndexTensor_(newContiguous)(state, indices); THCTensor_(zero)(state, output); if (fiveDimensionalInput) { // Collapse batch and feature dimensions output = THCTensor_(newFoldBatchDim)(state, output); THCTensor *old_input = input; input = THCTensor_(newFoldBatchDim)(state, input); THCTensor_(free)(state, old_input); THCIndexTensor *old_indices = indices; indices = THCIndexTensor_(newFoldBatchDim)(state, indices); THCIndexTensor_(free)(state, old_indices); } else { THCTensor_(retain)(state, output); } THCDeviceTensor<real, 4> cudaInput; THCDeviceTensor<real, 4> cudaOutput; THCDeviceTensor<THCIndex_t, 4> cudaIndices; cudaInput = toDeviceTensor<real, 4>(state, input); cudaOutput = toDeviceTensor<real, 4>(state, output); cudaIndices = toDeviceTensor<THCIndex_t, 4>(state, indices); int totalZ = inputTime * inputSlices * batchSize; int offsetZ = 0; dim3 block(32, 8); while (totalZ > 0) { dim3 grid(THCCeilDiv(inputWidth, static_cast<int>(block.x)), THCCeilDiv(inputHeight, static_cast<int>(block.y)), totalZ > 65535 ? 65535 : totalZ); cuda_VolumetricMaxUnpooling_updateOutput<<<grid, block, 0, THCState_getCurrentStream(state)>>>( cudaInput, cudaIndices, cudaOutput, dT, dH, dW, padT, padH, padW, offsetZ); THCudaCheck(cudaGetLastError()); totalZ -= 65535; offsetZ += 65535; } THCTensor_(free)(state, input); THCTensor_(free)(state, output); THCIndexTensor_(free)(state, indices); } void THNN_(VolumetricMaxUnpooling_updateGradInput)( THCState *state, THCTensor *input, THCTensor *gradOutput, THCTensor *gradInput, THCIndexTensor *indices, int outputTime, int outputWidth, int outputHeight, int dT, int dW, int dH, int padT, int padW, int padH) { int batchSize = 0; int inputSlices = 0; int inputTime = 0; int inputHeight = 0; int inputWidth = 0; THNN_(VolumetricMaxUnpooling_shapeCheck)( state, input, gradOutput, indices, outputTime, outputWidth, outputHeight, dT, dW, dH, padT, padW, padH); THCUNN_assertSameGPU(state, 4, input, indices, gradOutput, gradInput); int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5; if (!fiveDimensionalInput) /* 4D */ { batchSize = 1; inputSlices = THCTensor_(size)(state, input, 0); inputTime = THCTensor_(size)(state, input, 1); inputHeight = THCTensor_(size)(state, input, 2); inputWidth = THCTensor_(size)(state, input, 3); } else { batchSize = THCTensor_(size)(state, input, 0); inputSlices = THCTensor_(size)(state, input, 1); inputTime = THCTensor_(size)(state, input, 2); inputHeight = THCTensor_(size)(state, input, 3); inputWidth = THCTensor_(size)(state, input, 4); } input = THCTensor_(newContiguous)(state, input); THCTensor_(resizeAs)(state, gradInput, input); THCTensor_(zero)(state, gradInput); indices = THCIndexTensor_(newContiguous)(state, indices); gradOutput = THCTensor_(newContiguous)(state, gradOutput); if (fiveDimensionalInput) { gradInput = THCTensor_(newFoldBatchDim)(state, gradInput); THCIndexTensor *old_indices = indices; indices = THCIndexTensor_(newFoldBatchDim)(state, indices); THCIndexTensor_(free)(state, old_indices); THCTensor *old_gradOutput = gradOutput; gradOutput = THCTensor_(newFoldBatchDim)(state, gradOutput); THCTensor_(free)(state, old_gradOutput); } else { THCTensor_(retain)(state, gradInput); } // Collapse batch and feature dimensions THCDeviceTensor<real, 4> cudaGradInput; THCDeviceTensor<real, 4> cudaGradOutput; THCDeviceTensor<THCIndex_t, 4> cudaIndices; cudaGradInput = toDeviceTensor<real, 4>(state, gradInput); cudaGradOutput = toDeviceTensor<real, 4>(state, gradOutput); cudaIndices = toDeviceTensor<THCIndex_t, 4>(state, indices); int totalZ = inputTime * inputSlices * batchSize; int offsetZ = 0; dim3 block(32, 8); while (totalZ > 0) { dim3 grid(THCCeilDiv(inputWidth, static_cast<int>(block.x)), THCCeilDiv(inputHeight, static_cast<int>(block.y)), totalZ > 65535 ? 65535 : totalZ); cuda_VolumetricMaxUnpooling_updateGradInput<<<grid, block, 0, THCState_getCurrentStream(state)>>>( cudaGradOutput, cudaIndices, cudaGradInput, dT, dH, dW, padT, padH, padW, offsetZ); THCudaCheck(cudaGetLastError()); totalZ -= 65535; offsetZ += 65535; } // cleanup THCTensor_(free)(state, gradOutput); THCTensor_(free)(state, gradInput); THCIndexTensor_(free)(state, indices); THCTensor_(free)(state, input); } #endif
94d0507aef8d8d286cfdba3fb5362d5e973f5e9d.hip
// !!! This is a file automatically generated by hipify!!! #pragma once #include <iostream> #include <hip/hip_runtime.h> #include <hiprand/hiprand.h> #include <hiprand/hiprand_kernel.h> #include <fstream> #include <vector> #include "Math3D.cuh" //Defining CUDA Utility function #define cudaCheckErrors(x) cudaCheckError(x, #x, __FILE__, __LINE__) //Display properties #define WIDTH 1280 #define HEIGHT 720 //Path tracing properties #define MAX_DEPTH 10 #define SAMPLES 1 //World properties #define NUM_OBJECTS 3 #define FOV 90 #define ZNEAR 0.01 #define ZFAR 1000 //Utilities #define PI 3.1415926535897932384626433832795 //CUDA Utility functions void cudaCheckError(hipError_t err, char const *const func, const char *const file, int const line) { if (err != hipSuccess) { std::cerr << "CUDA error has occured! The error: " << hipGetErrorString(err) << ", which has occured at " << file << ", on line " << line << ", in function: " << func << "' \n"; hipDeviceReset(); exit(err); } } class Material { public: __host__ __device__ Material(const Material& m) { this->albedo = m.albedo; this->metallic = m.metallic; this->roughness = m.roughness; this->emittance = m.emittance; } __host__ __device__ Material(Vector3f albedo, float metallic, float roughness, Vector3f emittance) : albedo(albedo), metallic(metallic), roughness(roughness), emittance(emittance) {} __host__ __device__ Material(Vector3f albedo, float metallic, float roughness) : albedo(albedo), metallic(metallic), roughness(roughness), emittance(Vector3f(0)) {} __host__ __device__ Material() {} __host__ __device__ ~Material() {} Vector3f albedo; float metallic; float roughness; Vector3f emittance; }; class Ray { public: __host__ __device__ Ray(Vector3f origin, Vector3f direction) : origin(origin), direction(direction) {} __host__ __device__ Ray() {} __host__ __device__ ~Ray() {} Vector3f origin; Vector3f direction; }; class Sphere { public: __host__ __device__ Sphere(Vector3f center, float radius, Material material) : center(center), radius(radius), material(material) {} __host__ __device__ Sphere() {} __host__ __device__ ~Sphere() {} __host__ __device__ inline Vector3f getNormal(Vector3f pHit) const { return (pHit - center) / radius; } __host__ __device__ float intersect(Ray ray) const { float t0, t1; Vector3f L = center - ray.origin; float tca = L.dot(ray.direction); if (tca < 0) return -1; float d2 = L.dot(L) - tca * tca; if (d2 > radius * radius) return -1; float thc = sqrt(radius * radius - d2); t0 = tca - thc; t1 = tca + thc; if (t0 > t1) swap(t0, t1); if (t0 < 0) { swap(t0, t1); if (t0 < 0) return -1; } return t0; } __host__ __device__ inline void swap(float& a, float& b) const { float ob = b; b = a; a = ob; } Material material; Vector3f center; float radius; }; __device__ Sphere* intersectScene(Sphere* spheres, int size, Ray ray, float& t) { float nearestDistance = 10000; Sphere* ret = nullptr; for (int i = 0; i < size; i++) { float dist = spheres[i].intersect(ray); if (dist > 0) { if (dist < nearestDistance) { nearestDistance = dist; ret = &spheres[i]; } } } t = nearestDistance; return ret; } __host__ __device__ Vector3f max(Vector3f value, Vector3f min) { if (value < min) return min; else return value; } __host__ __device__ Vector3f mix(Vector3f a, Vector3f b, float factor) { return a + (b - a) * factor; } __device__ Vector3f Hemisphere(float u0, float u1) { float r = (float) sqrt(1 - u0 * u1); float phi = (float) (2 * PI * u1); return Vector3f(cos(phi) * r, sin(phi) * r, sqrt(1 - u0 > 0 ? 1 - u0 : 0)); } __device__ float DistributionGGX(Vector3f N, Vector3f H, float roughness) { float a = roughness * roughness; float a2 = a * a; float NdotH = fmax(N.dot(H), 0.0f); float NdotH2 = NdotH * NdotH; float num = a2; float denom = (NdotH2 * (a2 - 1.0) + 1.0); denom = PI * denom * denom; return num / denom; } __device__ float GeometrySchlickGGX(float NdotV, float roughness) { float r = (roughness + 1.0); float k = (r * r) / 8.0; float num = NdotV; float denom = NdotV * (1.0 - k) + k; return num / denom; } __device__ float GeometrySmith(Vector3f N, Vector3f V, Vector3f L, float roughness) { float NdotV = fmax(N.dot(V), 0.0f); float NdotL = fmax(N.dot(L), 0.0f); float ggx2 = GeometrySchlickGGX(NdotV, roughness); float ggx1 = GeometrySchlickGGX(NdotL, roughness); return ggx1 * ggx2; } __device__ Vector3f fresnelSchlickRoughness(float cosTheta, Vector3f F0, float roughness) { return F0 + (max(Vector3f(1 - roughness), F0) - F0) * pow(1.0 - cosTheta, 5.0); } __device__ Vector3f fresnelSchlick(float cosTheta, Vector3f F0) { return F0 + (Vector3f(1) - F0) * pow(1.0 - cosTheta, 5.0); } __device__ void createCoordinateSystem(const Vector3f& N, Vector3f& Nt, Vector3f& Nb) { if (::fabs(N.x) > ::fabs(N.y)) Nt = Vector3f(N.z, 0, -N.x) / sqrtf(N.x * N.x + N.z * N.z); else Nt = Vector3f(0, -N.z, N.y) / sqrtf(N.y * N.y + N.z * N.z); Nb = N.cross(Nt); } __device__ Vector3f trace(Ray ray, Sphere* spheres, hiprandState_t* randState, int size, int depth) { //Depth clamp if (depth >= MAX_DEPTH) return Vector3f(0.0); //Intersection float t; Sphere* sphere = intersectScene(spheres, size, ray, t); if (sphere == nullptr) { return Vector3f(0.0); } //Some properties for the next ray, and it's generation Vector3f hitP = ray.origin + ray.direction * t; Vector3f normal = sphere->getNormal(hitP); Vector3f view = ray.direction; //Properties of the sphere we've hit Material material = sphere->material; Vector3f albedo = material.albedo; float metallicness = material.metallic; float roughness = material.roughness; Vector3f emittance = material.emittance; Vector3f F0(0.04); F0 = mix(F0, albedo, metallicness); //Rendering equation: light out = light emitted + light reflected towards camera Vector3f sample = Hemisphere(hiprand_uniform(randState++), hiprand_uniform(randState++)); Vector3f N = normal; Vector3f V = view; Vector3f R = view.reflect(normal); Vector3f Nt, Nb; createCoordinateSystem(N, Nt, Nb); Vector3f outV = Vector3f( sample.x * Nb.x + sample.y * N.x + sample.z * Nt.x, sample.x * Nb.y + sample.y * N.y + sample.z * Nt.y, sample.x * Nb.z + sample.y * N.z + sample.z * Nt.z ); Vector3f H = (V + outV) / (V + outV).abs(); float NdotV = fmax(N.dot(V), 0.0f); float HdotV = fmax(H.dot(V), 0.0f); float NDF = DistributionGGX(N, H, roughness); Vector3f F = fresnelSchlickRoughness(HdotV, F0, roughness); float G = GeometrySmith(N, V, outV, roughness); Vector3f outRadiance = (F * NDF * G * (PI / 2)) / fmax(N.dot(outV), 0.000001f); Vector3f incoming = trace(Ray(hitP, outV), spheres, randState, size, depth + 1); Vector3f kD = Vector3f(1) - F; kD *= 1 - metallicness; return emittance + (outRadiance + incoming * kD); } __device__ Vector3f renderPixel(Sphere* spheres, hiprandState_t* randState, int size, int x, int y) { Vector3f totalColor; Vector3f position = Vector3f(x - WIDTH / 2, y - HEIGHT / 2, 0); Vector3f direction = Vector3f(0, 0, 1); Ray ray = Ray(position, direction); for (int i = 0; i < SAMPLES; i++) { totalColor += trace(ray, spheres, randState, size, 0); } return totalColor / SAMPLES; } __global__ void render(Vector3f* pixels, Sphere* spheres, hiprandState_t* randState, int size) { int i = threadIdx.x + blockIdx.x * blockDim.x; int j = threadIdx.y + blockIdx.y * blockDim.y; if ((i >= WIDTH) || (j >= HEIGHT)) return; int index = j * WIDTH + i; pixels[index] = renderPixel(spheres, randState, size, i, j); } void createScene(Sphere* spheres) { spheres[0] = Sphere(Vector3f(0, 0, 50), 40, Material(Vector3f(1), .3f, 1)); spheres[1] = Sphere(Vector3f(-100, -100, 30), 100, Material(Vector3f(1, 0, 0), .4f, .1f)); spheres[2] = Sphere(Vector3f(200, 400, -1000), 500, Material(Vector3f(1, 0, 0), .4f, .1f, Vector3f(100))); } __global__ void setupRandom(hiprandState_t* randState) { int idx = threadIdx.x + blockDim.x * blockIdx.x; hiprand_init(1234, idx, 0, &randState[idx]); } int main() { //Create output file std::ofstream file("C:/Users/akmec/Desktop/out.ppm", std::ofstream::out); file << "P3\n" << WIDTH << "\n" << HEIGHT << "\n" << "255" << "\n"; //Variables Vector3f* pixels_d; Sphere* spheres_d; hiprandState_t* randState; cudaCheckErrors(hipMallocManaged((void**) &pixels_d, WIDTH * HEIGHT * sizeof(Vector3f))); cudaCheckErrors(hipMallocManaged((void**) &spheres_d, NUM_OBJECTS * sizeof(Sphere))); cudaCheckErrors(hipMalloc((void**) &randState, sizeof(hiprandState_t))); createScene(spheres_d); hipLaunchKernelGGL(( setupRandom), dim3(1), dim3(1), 0, 0, randState); cudaCheckErrors(hipGetLastError()); cudaCheckErrors(hipDeviceSynchronize()); std::cout << "Started rendering" << std::endl; int tx = 8; int ty = 8; dim3 blocks(WIDTH / tx + 1, HEIGHT / ty + 1, 1); dim3 threads(tx, ty, 1); hipLaunchKernelGGL(( render), dim3(blocks), dim3(threads), 0, 0, pixels_d, spheres_d, randState, NUM_OBJECTS); cudaCheckErrors(hipGetLastError()); cudaCheckErrors(hipDeviceSynchronize()); std::cout << "Started writing file" << std::endl; for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { size_t index = y * WIDTH + x; file << int(pixels_d[index].x * 255.99) << " "; file << int(pixels_d[index].y * 255.99) << " "; file << int(pixels_d[index].z * 255.99) << " "; } file << "\n"; } file.close(); std::cout << "Finished everything" << std::endl; cudaCheckErrors(hipFree(pixels_d)); cudaCheckErrors(hipFree(spheres_d)); cudaCheckErrors(hipFree(randState)); system("PAUSE"); return 0; }
94d0507aef8d8d286cfdba3fb5362d5e973f5e9d.cu
#pragma once #include <iostream> #include <cuda.h> #include <curand.h> #include <curand_kernel.h> #include <fstream> #include <vector> #include "Math3D.cuh" //Defining CUDA Utility function #define cudaCheckErrors(x) cudaCheckError(x, #x, __FILE__, __LINE__) //Display properties #define WIDTH 1280 #define HEIGHT 720 //Path tracing properties #define MAX_DEPTH 10 #define SAMPLES 1 //World properties #define NUM_OBJECTS 3 #define FOV 90 #define ZNEAR 0.01 #define ZFAR 1000 //Utilities #define PI 3.1415926535897932384626433832795 //CUDA Utility functions void cudaCheckError(cudaError err, char const *const func, const char *const file, int const line) { if (err != cudaSuccess) { std::cerr << "CUDA error has occured! The error: " << cudaGetErrorString(err) << ", which has occured at " << file << ", on line " << line << ", in function: " << func << "' \n"; cudaDeviceReset(); exit(err); } } class Material { public: __host__ __device__ Material(const Material& m) { this->albedo = m.albedo; this->metallic = m.metallic; this->roughness = m.roughness; this->emittance = m.emittance; } __host__ __device__ Material(Vector3f albedo, float metallic, float roughness, Vector3f emittance) : albedo(albedo), metallic(metallic), roughness(roughness), emittance(emittance) {} __host__ __device__ Material(Vector3f albedo, float metallic, float roughness) : albedo(albedo), metallic(metallic), roughness(roughness), emittance(Vector3f(0)) {} __host__ __device__ Material() {} __host__ __device__ ~Material() {} Vector3f albedo; float metallic; float roughness; Vector3f emittance; }; class Ray { public: __host__ __device__ Ray(Vector3f origin, Vector3f direction) : origin(origin), direction(direction) {} __host__ __device__ Ray() {} __host__ __device__ ~Ray() {} Vector3f origin; Vector3f direction; }; class Sphere { public: __host__ __device__ Sphere(Vector3f center, float radius, Material material) : center(center), radius(radius), material(material) {} __host__ __device__ Sphere() {} __host__ __device__ ~Sphere() {} __host__ __device__ inline Vector3f getNormal(Vector3f pHit) const { return (pHit - center) / radius; } __host__ __device__ float intersect(Ray ray) const { float t0, t1; Vector3f L = center - ray.origin; float tca = L.dot(ray.direction); if (tca < 0) return -1; float d2 = L.dot(L) - tca * tca; if (d2 > radius * radius) return -1; float thc = sqrt(radius * radius - d2); t0 = tca - thc; t1 = tca + thc; if (t0 > t1) swap(t0, t1); if (t0 < 0) { swap(t0, t1); if (t0 < 0) return -1; } return t0; } __host__ __device__ inline void swap(float& a, float& b) const { float ob = b; b = a; a = ob; } Material material; Vector3f center; float radius; }; __device__ Sphere* intersectScene(Sphere* spheres, int size, Ray ray, float& t) { float nearestDistance = 10000; Sphere* ret = nullptr; for (int i = 0; i < size; i++) { float dist = spheres[i].intersect(ray); if (dist > 0) { if (dist < nearestDistance) { nearestDistance = dist; ret = &spheres[i]; } } } t = nearestDistance; return ret; } __host__ __device__ Vector3f max(Vector3f value, Vector3f min) { if (value < min) return min; else return value; } __host__ __device__ Vector3f mix(Vector3f a, Vector3f b, float factor) { return a + (b - a) * factor; } __device__ Vector3f Hemisphere(float u0, float u1) { float r = (float) sqrt(1 - u0 * u1); float phi = (float) (2 * PI * u1); return Vector3f(cos(phi) * r, sin(phi) * r, sqrt(1 - u0 > 0 ? 1 - u0 : 0)); } __device__ float DistributionGGX(Vector3f N, Vector3f H, float roughness) { float a = roughness * roughness; float a2 = a * a; float NdotH = fmax(N.dot(H), 0.0f); float NdotH2 = NdotH * NdotH; float num = a2; float denom = (NdotH2 * (a2 - 1.0) + 1.0); denom = PI * denom * denom; return num / denom; } __device__ float GeometrySchlickGGX(float NdotV, float roughness) { float r = (roughness + 1.0); float k = (r * r) / 8.0; float num = NdotV; float denom = NdotV * (1.0 - k) + k; return num / denom; } __device__ float GeometrySmith(Vector3f N, Vector3f V, Vector3f L, float roughness) { float NdotV = fmax(N.dot(V), 0.0f); float NdotL = fmax(N.dot(L), 0.0f); float ggx2 = GeometrySchlickGGX(NdotV, roughness); float ggx1 = GeometrySchlickGGX(NdotL, roughness); return ggx1 * ggx2; } __device__ Vector3f fresnelSchlickRoughness(float cosTheta, Vector3f F0, float roughness) { return F0 + (max(Vector3f(1 - roughness), F0) - F0) * pow(1.0 - cosTheta, 5.0); } __device__ Vector3f fresnelSchlick(float cosTheta, Vector3f F0) { return F0 + (Vector3f(1) - F0) * pow(1.0 - cosTheta, 5.0); } __device__ void createCoordinateSystem(const Vector3f& N, Vector3f& Nt, Vector3f& Nb) { if (std::fabs(N.x) > std::fabs(N.y)) Nt = Vector3f(N.z, 0, -N.x) / sqrtf(N.x * N.x + N.z * N.z); else Nt = Vector3f(0, -N.z, N.y) / sqrtf(N.y * N.y + N.z * N.z); Nb = N.cross(Nt); } __device__ Vector3f trace(Ray ray, Sphere* spheres, curandState* randState, int size, int depth) { //Depth clamp if (depth >= MAX_DEPTH) return Vector3f(0.0); //Intersection float t; Sphere* sphere = intersectScene(spheres, size, ray, t); if (sphere == nullptr) { return Vector3f(0.0); } //Some properties for the next ray, and it's generation Vector3f hitP = ray.origin + ray.direction * t; Vector3f normal = sphere->getNormal(hitP); Vector3f view = ray.direction; //Properties of the sphere we've hit Material material = sphere->material; Vector3f albedo = material.albedo; float metallicness = material.metallic; float roughness = material.roughness; Vector3f emittance = material.emittance; Vector3f F0(0.04); F0 = mix(F0, albedo, metallicness); //Rendering equation: light out = light emitted + light reflected towards camera Vector3f sample = Hemisphere(curand_uniform(randState++), curand_uniform(randState++)); Vector3f N = normal; Vector3f V = view; Vector3f R = view.reflect(normal); Vector3f Nt, Nb; createCoordinateSystem(N, Nt, Nb); Vector3f outV = Vector3f( sample.x * Nb.x + sample.y * N.x + sample.z * Nt.x, sample.x * Nb.y + sample.y * N.y + sample.z * Nt.y, sample.x * Nb.z + sample.y * N.z + sample.z * Nt.z ); Vector3f H = (V + outV) / (V + outV).abs(); float NdotV = fmax(N.dot(V), 0.0f); float HdotV = fmax(H.dot(V), 0.0f); float NDF = DistributionGGX(N, H, roughness); Vector3f F = fresnelSchlickRoughness(HdotV, F0, roughness); float G = GeometrySmith(N, V, outV, roughness); Vector3f outRadiance = (F * NDF * G * (PI / 2)) / fmax(N.dot(outV), 0.000001f); Vector3f incoming = trace(Ray(hitP, outV), spheres, randState, size, depth + 1); Vector3f kD = Vector3f(1) - F; kD *= 1 - metallicness; return emittance + (outRadiance + incoming * kD); } __device__ Vector3f renderPixel(Sphere* spheres, curandState* randState, int size, int x, int y) { Vector3f totalColor; Vector3f position = Vector3f(x - WIDTH / 2, y - HEIGHT / 2, 0); Vector3f direction = Vector3f(0, 0, 1); Ray ray = Ray(position, direction); for (int i = 0; i < SAMPLES; i++) { totalColor += trace(ray, spheres, randState, size, 0); } return totalColor / SAMPLES; } __global__ void render(Vector3f* pixels, Sphere* spheres, curandState* randState, int size) { int i = threadIdx.x + blockIdx.x * blockDim.x; int j = threadIdx.y + blockIdx.y * blockDim.y; if ((i >= WIDTH) || (j >= HEIGHT)) return; int index = j * WIDTH + i; pixels[index] = renderPixel(spheres, randState, size, i, j); } void createScene(Sphere* spheres) { spheres[0] = Sphere(Vector3f(0, 0, 50), 40, Material(Vector3f(1), .3f, 1)); spheres[1] = Sphere(Vector3f(-100, -100, 30), 100, Material(Vector3f(1, 0, 0), .4f, .1f)); spheres[2] = Sphere(Vector3f(200, 400, -1000), 500, Material(Vector3f(1, 0, 0), .4f, .1f, Vector3f(100))); } __global__ void setupRandom(curandState* randState) { int idx = threadIdx.x + blockDim.x * blockIdx.x; curand_init(1234, idx, 0, &randState[idx]); } int main() { //Create output file std::ofstream file("C:/Users/akmec/Desktop/out.ppm", std::ofstream::out); file << "P3\n" << WIDTH << "\n" << HEIGHT << "\n" << "255" << "\n"; //Variables Vector3f* pixels_d; Sphere* spheres_d; curandState* randState; cudaCheckErrors(cudaMallocManaged((void**) &pixels_d, WIDTH * HEIGHT * sizeof(Vector3f))); cudaCheckErrors(cudaMallocManaged((void**) &spheres_d, NUM_OBJECTS * sizeof(Sphere))); cudaCheckErrors(cudaMalloc((void**) &randState, sizeof(curandState))); createScene(spheres_d); setupRandom<<<1, 1>>>(randState); cudaCheckErrors(cudaGetLastError()); cudaCheckErrors(cudaDeviceSynchronize()); std::cout << "Started rendering" << std::endl; int tx = 8; int ty = 8; dim3 blocks(WIDTH / tx + 1, HEIGHT / ty + 1, 1); dim3 threads(tx, ty, 1); render<<<blocks, threads>>>(pixels_d, spheres_d, randState, NUM_OBJECTS); cudaCheckErrors(cudaGetLastError()); cudaCheckErrors(cudaDeviceSynchronize()); std::cout << "Started writing file" << std::endl; for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { size_t index = y * WIDTH + x; file << int(pixels_d[index].x * 255.99) << " "; file << int(pixels_d[index].y * 255.99) << " "; file << int(pixels_d[index].z * 255.99) << " "; } file << "\n"; } file.close(); std::cout << "Finished everything" << std::endl; cudaCheckErrors(cudaFree(pixels_d)); cudaCheckErrors(cudaFree(spheres_d)); cudaCheckErrors(cudaFree(randState)); system("PAUSE"); return 0; }
fa7c18a8660716e1bc9ad52ed0441b52dc046a16.hip
// !!! This is a file automatically generated by hipify!!! #include <torch/serialize/tensor.h> #include <ATen/ATen.h> #include <ATen/hip/HIPContext.h> #include <hip/hip_runtime.h> #include <hip/hip_runtime.h> namespace { #define CUDA_1D_KERNEL_LOOP(i, n) \ for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \ i += blockDim.x * gridDim.x) // The number of cuda threads to use. 512 is used for backward compatibility constexpr int ROI_CUDA_NUM_THREADS = 512; // The maximum number of blocks to use in the default kernel call. constexpr int ROI_MAXIMUM_NUM_BLOCKS = 4096; /** * @brief Compute the number of blocks needed to run N threads. */ inline int ROI_GET_BLOCKS(const int N) { return ::max( ::min( (N + ROI_CUDA_NUM_THREADS - 1) / ROI_CUDA_NUM_THREADS, ROI_MAXIMUM_NUM_BLOCKS), // Use at least 1 block, since CUDA does not allow empty block 1); } template <typename T> __device__ T bilinear_interpolate( const T* bottom_data, const int height, const int width, T y, T x, const int index /* index for debug only*/) { // deal with cases that inverse elements are out of feature map boundary if (y < -1.0 || y > height || x < -1.0 || x > width) { // empty return 0; } if (y <= 0) { y = 0; } if (x <= 0) { x = 0; } int y_low = static_cast<int>(y); int x_low = static_cast<int>(x); int y_high; int x_high; if (y_low >= height - 1) { y_high = y_low = height - 1; y = (T)y_low; } else { y_high = y_low + 1; } if (x_low >= width - 1) { x_high = x_low = width - 1; x = (T)x_low; } else { x_high = x_low + 1; } T ly = y - y_low; T lx = x - x_low; T hy = 1. - ly, hx = 1. - lx; // do bilinear interpolation T v1 = bottom_data[y_low * width + x_low]; T v2 = bottom_data[y_low * width + x_high]; T v3 = bottom_data[y_high * width + x_low]; T v4 = bottom_data[y_high * width + x_high]; T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx; T val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4); return val; } template <typename T> __global__ void RoIAlignForwardKernel( const int nthreads, const T* bottom_data, const T spatial_scale, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int sampling_ratio, const T* bottom_rois, T* top_data) { CUDA_1D_KERNEL_LOOP(index, nthreads) { // (n, c, ph, pw) is an element in the pooled output int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; const T* offset_bottom_rois = bottom_rois + n * 5; int roi_batch_ind = offset_bottom_rois[0]; // Do not using rounding; this implementation detail is critical T roi_start_w = offset_bottom_rois[1] * spatial_scale; T roi_start_h = offset_bottom_rois[2] * spatial_scale; T roi_end_w = offset_bottom_rois[3] * spatial_scale; T roi_end_h = offset_bottom_rois[4] * spatial_scale; // Force malformed ROIs to be 1x1 T roi_width = max(roi_end_w - roi_start_w, (T)1.); T roi_height = max(roi_end_h - roi_start_h, (T)1.); T bin_size_h = static_cast<T>(roi_height) / static_cast<T>(pooled_height); T bin_size_w = static_cast<T>(roi_width) / static_cast<T>(pooled_width); const T* offset_bottom_data = bottom_data + (roi_batch_ind * channels + c) * height * width; // We use roi_bin_grid to sample the grid and mimic integral int roi_bin_grid_h = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_height / pooled_height); // e.g., = 2 int roi_bin_grid_w = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_width / pooled_width); // We do average (integral) pooling inside a bin const T count = roi_bin_grid_h * roi_bin_grid_w; // e.g. = 4 T output_val = 0.; for (int iy = 0; iy < roi_bin_grid_h; iy++) { // e.g., iy = 0, 1 const T y = roi_start_h + ph * bin_size_h + static_cast<T>(iy + .5f) * bin_size_h / static_cast<T>(roi_bin_grid_h); // e.g., 0.5, 1.5 for (int ix = 0; ix < roi_bin_grid_w; ix++) { const T x = roi_start_w + pw * bin_size_w + static_cast<T>(ix + .5f) * bin_size_w / static_cast<T>(roi_bin_grid_w); T val = bilinear_interpolate( offset_bottom_data, height, width, y, x, index); output_val += val; } } output_val /= count; top_data[index] = output_val; } } template <typename T> __device__ void bilinear_interpolate_gradient( const int height, const int width, T y, T x, T* w1, T* w2, T* w3, T* w4, int* x_low, int* x_high, int* y_low, int* y_high, const int /*index*/ /* index for debug only*/) { // deal with cases that inverse elements are out of feature map boundary if (y < -1.0 || y > height || x < -1.0 || x > width) { // empty *w1 = *w2 = *w3 = *w4 = 0.; *x_low = *x_high = *y_low = *y_high = -1; return; } if (y <= 0) { y = 0; } if (x <= 0) { x = 0; } *y_low = static_cast<int>(y); *x_low = static_cast<int>(x); if (*y_low >= height - 1) { *y_high = *y_low = height - 1; y = (T)*y_low; } else { *y_high = *y_low + 1; } if (*x_low >= width - 1) { *x_high = *x_low = width - 1; x = (T)*x_low; } else { *x_high = *x_low + 1; } T ly = y - *y_low; T lx = x - *x_low; T hy = 1. - ly, hx = 1. - lx; // reference in forward *w1 = hy * hx, *w2 = hy * lx, *w3 = ly * hx, *w4 = ly * lx; return; } template <typename T> inline __device__ T gpu_atomic_add(const T val, T* address); template <> inline __device__ float gpu_atomic_add(const float val, float* address) { return atomicAdd(address, val); } template <> inline __device__ double gpu_atomic_add(const double val, double* address) { unsigned long long int* address_as_ull = (unsigned long long int*)address; unsigned long long int old = *address_as_ull; unsigned long long int assumed; do { assumed = old; old = atomicCAS(address_as_ull, assumed, __double_as_longlong(val + __longlong_as_double(assumed))); // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN) } while (assumed != old); return val; } template <typename T> __global__ void RoIAlignBackwardKernel( const int nthreads, const T* top_diff, const int num_rois, const T spatial_scale, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int sampling_ratio, T* bottom_diff, const T* bottom_rois) { CUDA_1D_KERNEL_LOOP(index, nthreads) { // (n, c, ph, pw) is an element in the pooled output int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; const T* offset_bottom_rois = bottom_rois + n * 5; int roi_batch_ind = offset_bottom_rois[0]; // Do not using rounding; this implementation detail is critical T roi_start_w = offset_bottom_rois[1] * spatial_scale; T roi_start_h = offset_bottom_rois[2] * spatial_scale; T roi_end_w = offset_bottom_rois[3] * spatial_scale; T roi_end_h = offset_bottom_rois[4] * spatial_scale; // Force malformed ROIs to be 1x1 T roi_width = max(roi_end_w - roi_start_w, (T)1.); T roi_height = max(roi_end_h - roi_start_h, (T)1.); T bin_size_h = static_cast<T>(roi_height) / static_cast<T>(pooled_height); T bin_size_w = static_cast<T>(roi_width) / static_cast<T>(pooled_width); T* offset_bottom_diff = bottom_diff + (roi_batch_ind * channels + c) * height * width; int top_offset = (n * channels + c) * pooled_height * pooled_width; const T* offset_top_diff = top_diff + top_offset; const T top_diff_this_bin = offset_top_diff[ph * pooled_width + pw]; // We use roi_bin_grid to sample the grid and mimic integral int roi_bin_grid_h = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_height / pooled_height); // e.g., = 2 int roi_bin_grid_w = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_width / pooled_width); // We do average (integral) pooling inside a bin const T count = roi_bin_grid_h * roi_bin_grid_w; // e.g. = 4 for (int iy = 0; iy < roi_bin_grid_h; iy++) { // e.g., iy = 0, 1 const T y = roi_start_h + ph * bin_size_h + static_cast<T>(iy + .5f) * bin_size_h / static_cast<T>(roi_bin_grid_h); // e.g., 0.5, 1.5 for (int ix = 0; ix < roi_bin_grid_w; ix++) { const T x = roi_start_w + pw * bin_size_w + static_cast<T>(ix + .5f) * bin_size_w / static_cast<T>(roi_bin_grid_w); T w1, w2, w3, w4; int x_low, x_high, y_low, y_high; bilinear_interpolate_gradient( height, width, y, x, &w1, &w2, &w3, &w4, &x_low, &x_high, &y_low, &y_high, index); T g1 = top_diff_this_bin * w1 / count; T g2 = top_diff_this_bin * w2 / count; T g3 = top_diff_this_bin * w3 / count; T g4 = top_diff_this_bin * w4 / count; if (x_low >= 0 && x_high >= 0 && y_low >= 0 && y_high >= 0) { /* atomicAdd( offset_bottom_diff + y_low * width + x_low, static_cast<T>(g1)); atomicAdd( offset_bottom_diff + y_low * width + x_high, static_cast<T>(g2)); atomicAdd( offset_bottom_diff + y_high * width + x_low, static_cast<T>(g3)); atomicAdd( offset_bottom_diff + y_high * width + x_high, static_cast<T>(g4)); */ gpu_atomic_add( static_cast<T>(g1), offset_bottom_diff + y_low * width + x_low); gpu_atomic_add( static_cast<T>(g2), offset_bottom_diff + y_low * width + x_high); gpu_atomic_add( static_cast<T>(g3), offset_bottom_diff + y_high * width + x_low); gpu_atomic_add( static_cast<T>(g4), offset_bottom_diff + y_high * width + x_high); } // if } // ix } // iy } // CUDA_1D_KERNEL_LOOP } // RoIAlignBackward } // namespace at::Tensor ROIAlign_Forward_CUDA( const at::Tensor input, const at::Tensor rois, int64_t pooled_height, int64_t pooled_width, double spatial_scale, int64_t sampling_ratio) { AT_ASSERT(input.is_contiguous()); AT_ASSERT(rois.is_contiguous()); AT_ASSERT(input.ndimension() == 4); AT_ASSERT(rois.ndimension() == 2); AT_ASSERT(rois.size(1) == 5); auto proposals = rois.size(0); auto channels = input.size(1); auto height = input.size(2); auto width = input.size(3); // Output Tensor is (num_rois, C, pooled_height, pooled_width) auto output = torch::zeros({proposals, channels, pooled_height, pooled_width}, input.options()); auto count = output.numel(); AT_DISPATCH_FLOATING_TYPES(input.type(), "ROIAlign_Forward_CUDA", ([&] { hipLaunchKernelGGL(( RoIAlignForwardKernel<scalar_t>) , dim3(ROI_GET_BLOCKS(count)), dim3(ROI_CUDA_NUM_THREADS), 0, at::hip::getCurrentHIPStreamMasqueradingAsCUDA(), count, input.data<scalar_t>(), static_cast<scalar_t>(spatial_scale), channels, height, width, pooled_height, pooled_width, sampling_ratio, rois.data<scalar_t>(), output.data<scalar_t>()); })); AT_ASSERT(hipGetLastError() == hipSuccess); return output; } at::Tensor ROIAlign_Backward_CUDA( const at::Tensor rois, const at::Tensor grad_output, int64_t b_size, int64_t channels, int64_t height, int64_t width, int64_t pooled_height, int64_t pooled_width, double spatial_scale, int64_t sampling_ratio) { AT_ASSERT(rois.is_contiguous()); AT_ASSERT(rois.ndimension() == 2); AT_ASSERT(rois.size(1) == 5); auto roi_cols = rois.size(1); AT_ASSERT(roi_cols == 4 || roi_cols == 5); // Output Tensor is (num_rois, C, pooled_height, pooled_width) // gradient wrt input features auto grad_in = torch::zeros({b_size, channels, height, width}, rois.options()); auto num_rois = rois.size(0); auto count = grad_output.numel(); AT_DISPATCH_FLOATING_TYPES(rois.type(), "ROIAlign_Backward_CUDA", ([&] { hipLaunchKernelGGL(( RoIAlignBackwardKernel<scalar_t>) , dim3(ROI_GET_BLOCKS(count)), dim3(ROI_CUDA_NUM_THREADS), 0, at::hip::getCurrentHIPStreamMasqueradingAsCUDA(), count, grad_output.data<scalar_t>(), num_rois, static_cast<scalar_t>(spatial_scale), channels, height, width, pooled_height, pooled_width, sampling_ratio, grad_in.data<scalar_t>(), rois.data<scalar_t>()); })); AT_ASSERT(hipGetLastError() == hipSuccess); return grad_in; }
fa7c18a8660716e1bc9ad52ed0441b52dc046a16.cu
#include <torch/serialize/tensor.h> #include <ATen/ATen.h> #include <ATen/cuda/CUDAContext.h> #include <cuda.h> #include <cuda_runtime.h> namespace { #define CUDA_1D_KERNEL_LOOP(i, n) \ for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \ i += blockDim.x * gridDim.x) // The number of cuda threads to use. 512 is used for backward compatibility constexpr int ROI_CUDA_NUM_THREADS = 512; // The maximum number of blocks to use in the default kernel call. constexpr int ROI_MAXIMUM_NUM_BLOCKS = 4096; /** * @brief Compute the number of blocks needed to run N threads. */ inline int ROI_GET_BLOCKS(const int N) { return std::max( std::min( (N + ROI_CUDA_NUM_THREADS - 1) / ROI_CUDA_NUM_THREADS, ROI_MAXIMUM_NUM_BLOCKS), // Use at least 1 block, since CUDA does not allow empty block 1); } template <typename T> __device__ T bilinear_interpolate( const T* bottom_data, const int height, const int width, T y, T x, const int index /* index for debug only*/) { // deal with cases that inverse elements are out of feature map boundary if (y < -1.0 || y > height || x < -1.0 || x > width) { // empty return 0; } if (y <= 0) { y = 0; } if (x <= 0) { x = 0; } int y_low = static_cast<int>(y); int x_low = static_cast<int>(x); int y_high; int x_high; if (y_low >= height - 1) { y_high = y_low = height - 1; y = (T)y_low; } else { y_high = y_low + 1; } if (x_low >= width - 1) { x_high = x_low = width - 1; x = (T)x_low; } else { x_high = x_low + 1; } T ly = y - y_low; T lx = x - x_low; T hy = 1. - ly, hx = 1. - lx; // do bilinear interpolation T v1 = bottom_data[y_low * width + x_low]; T v2 = bottom_data[y_low * width + x_high]; T v3 = bottom_data[y_high * width + x_low]; T v4 = bottom_data[y_high * width + x_high]; T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx; T val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4); return val; } template <typename T> __global__ void RoIAlignForwardKernel( const int nthreads, const T* bottom_data, const T spatial_scale, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int sampling_ratio, const T* bottom_rois, T* top_data) { CUDA_1D_KERNEL_LOOP(index, nthreads) { // (n, c, ph, pw) is an element in the pooled output int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; const T* offset_bottom_rois = bottom_rois + n * 5; int roi_batch_ind = offset_bottom_rois[0]; // Do not using rounding; this implementation detail is critical T roi_start_w = offset_bottom_rois[1] * spatial_scale; T roi_start_h = offset_bottom_rois[2] * spatial_scale; T roi_end_w = offset_bottom_rois[3] * spatial_scale; T roi_end_h = offset_bottom_rois[4] * spatial_scale; // Force malformed ROIs to be 1x1 T roi_width = max(roi_end_w - roi_start_w, (T)1.); T roi_height = max(roi_end_h - roi_start_h, (T)1.); T bin_size_h = static_cast<T>(roi_height) / static_cast<T>(pooled_height); T bin_size_w = static_cast<T>(roi_width) / static_cast<T>(pooled_width); const T* offset_bottom_data = bottom_data + (roi_batch_ind * channels + c) * height * width; // We use roi_bin_grid to sample the grid and mimic integral int roi_bin_grid_h = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_height / pooled_height); // e.g., = 2 int roi_bin_grid_w = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_width / pooled_width); // We do average (integral) pooling inside a bin const T count = roi_bin_grid_h * roi_bin_grid_w; // e.g. = 4 T output_val = 0.; for (int iy = 0; iy < roi_bin_grid_h; iy++) { // e.g., iy = 0, 1 const T y = roi_start_h + ph * bin_size_h + static_cast<T>(iy + .5f) * bin_size_h / static_cast<T>(roi_bin_grid_h); // e.g., 0.5, 1.5 for (int ix = 0; ix < roi_bin_grid_w; ix++) { const T x = roi_start_w + pw * bin_size_w + static_cast<T>(ix + .5f) * bin_size_w / static_cast<T>(roi_bin_grid_w); T val = bilinear_interpolate( offset_bottom_data, height, width, y, x, index); output_val += val; } } output_val /= count; top_data[index] = output_val; } } template <typename T> __device__ void bilinear_interpolate_gradient( const int height, const int width, T y, T x, T* w1, T* w2, T* w3, T* w4, int* x_low, int* x_high, int* y_low, int* y_high, const int /*index*/ /* index for debug only*/) { // deal with cases that inverse elements are out of feature map boundary if (y < -1.0 || y > height || x < -1.0 || x > width) { // empty *w1 = *w2 = *w3 = *w4 = 0.; *x_low = *x_high = *y_low = *y_high = -1; return; } if (y <= 0) { y = 0; } if (x <= 0) { x = 0; } *y_low = static_cast<int>(y); *x_low = static_cast<int>(x); if (*y_low >= height - 1) { *y_high = *y_low = height - 1; y = (T)*y_low; } else { *y_high = *y_low + 1; } if (*x_low >= width - 1) { *x_high = *x_low = width - 1; x = (T)*x_low; } else { *x_high = *x_low + 1; } T ly = y - *y_low; T lx = x - *x_low; T hy = 1. - ly, hx = 1. - lx; // reference in forward *w1 = hy * hx, *w2 = hy * lx, *w3 = ly * hx, *w4 = ly * lx; return; } template <typename T> inline __device__ T gpu_atomic_add(const T val, T* address); template <> inline __device__ float gpu_atomic_add(const float val, float* address) { return atomicAdd(address, val); } template <> inline __device__ double gpu_atomic_add(const double val, double* address) { unsigned long long int* address_as_ull = (unsigned long long int*)address; unsigned long long int old = *address_as_ull; unsigned long long int assumed; do { assumed = old; old = atomicCAS(address_as_ull, assumed, __double_as_longlong(val + __longlong_as_double(assumed))); // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN) } while (assumed != old); return val; } template <typename T> __global__ void RoIAlignBackwardKernel( const int nthreads, const T* top_diff, const int num_rois, const T spatial_scale, const int channels, const int height, const int width, const int pooled_height, const int pooled_width, const int sampling_ratio, T* bottom_diff, const T* bottom_rois) { CUDA_1D_KERNEL_LOOP(index, nthreads) { // (n, c, ph, pw) is an element in the pooled output int pw = index % pooled_width; int ph = (index / pooled_width) % pooled_height; int c = (index / pooled_width / pooled_height) % channels; int n = index / pooled_width / pooled_height / channels; const T* offset_bottom_rois = bottom_rois + n * 5; int roi_batch_ind = offset_bottom_rois[0]; // Do not using rounding; this implementation detail is critical T roi_start_w = offset_bottom_rois[1] * spatial_scale; T roi_start_h = offset_bottom_rois[2] * spatial_scale; T roi_end_w = offset_bottom_rois[3] * spatial_scale; T roi_end_h = offset_bottom_rois[4] * spatial_scale; // Force malformed ROIs to be 1x1 T roi_width = max(roi_end_w - roi_start_w, (T)1.); T roi_height = max(roi_end_h - roi_start_h, (T)1.); T bin_size_h = static_cast<T>(roi_height) / static_cast<T>(pooled_height); T bin_size_w = static_cast<T>(roi_width) / static_cast<T>(pooled_width); T* offset_bottom_diff = bottom_diff + (roi_batch_ind * channels + c) * height * width; int top_offset = (n * channels + c) * pooled_height * pooled_width; const T* offset_top_diff = top_diff + top_offset; const T top_diff_this_bin = offset_top_diff[ph * pooled_width + pw]; // We use roi_bin_grid to sample the grid and mimic integral int roi_bin_grid_h = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_height / pooled_height); // e.g., = 2 int roi_bin_grid_w = (sampling_ratio > 0) ? sampling_ratio : ceil(roi_width / pooled_width); // We do average (integral) pooling inside a bin const T count = roi_bin_grid_h * roi_bin_grid_w; // e.g. = 4 for (int iy = 0; iy < roi_bin_grid_h; iy++) { // e.g., iy = 0, 1 const T y = roi_start_h + ph * bin_size_h + static_cast<T>(iy + .5f) * bin_size_h / static_cast<T>(roi_bin_grid_h); // e.g., 0.5, 1.5 for (int ix = 0; ix < roi_bin_grid_w; ix++) { const T x = roi_start_w + pw * bin_size_w + static_cast<T>(ix + .5f) * bin_size_w / static_cast<T>(roi_bin_grid_w); T w1, w2, w3, w4; int x_low, x_high, y_low, y_high; bilinear_interpolate_gradient( height, width, y, x, &w1, &w2, &w3, &w4, &x_low, &x_high, &y_low, &y_high, index); T g1 = top_diff_this_bin * w1 / count; T g2 = top_diff_this_bin * w2 / count; T g3 = top_diff_this_bin * w3 / count; T g4 = top_diff_this_bin * w4 / count; if (x_low >= 0 && x_high >= 0 && y_low >= 0 && y_high >= 0) { /* atomicAdd( offset_bottom_diff + y_low * width + x_low, static_cast<T>(g1)); atomicAdd( offset_bottom_diff + y_low * width + x_high, static_cast<T>(g2)); atomicAdd( offset_bottom_diff + y_high * width + x_low, static_cast<T>(g3)); atomicAdd( offset_bottom_diff + y_high * width + x_high, static_cast<T>(g4)); */ gpu_atomic_add( static_cast<T>(g1), offset_bottom_diff + y_low * width + x_low); gpu_atomic_add( static_cast<T>(g2), offset_bottom_diff + y_low * width + x_high); gpu_atomic_add( static_cast<T>(g3), offset_bottom_diff + y_high * width + x_low); gpu_atomic_add( static_cast<T>(g4), offset_bottom_diff + y_high * width + x_high); } // if } // ix } // iy } // CUDA_1D_KERNEL_LOOP } // RoIAlignBackward } // namespace at::Tensor ROIAlign_Forward_CUDA( const at::Tensor input, const at::Tensor rois, int64_t pooled_height, int64_t pooled_width, double spatial_scale, int64_t sampling_ratio) { AT_ASSERT(input.is_contiguous()); AT_ASSERT(rois.is_contiguous()); AT_ASSERT(input.ndimension() == 4); AT_ASSERT(rois.ndimension() == 2); AT_ASSERT(rois.size(1) == 5); auto proposals = rois.size(0); auto channels = input.size(1); auto height = input.size(2); auto width = input.size(3); // Output Tensor is (num_rois, C, pooled_height, pooled_width) auto output = torch::zeros({proposals, channels, pooled_height, pooled_width}, input.options()); auto count = output.numel(); AT_DISPATCH_FLOATING_TYPES(input.type(), "ROIAlign_Forward_CUDA", ([&] { RoIAlignForwardKernel<scalar_t> <<<ROI_GET_BLOCKS(count), ROI_CUDA_NUM_THREADS, 0, at::cuda::getCurrentCUDAStream()>>>( count, input.data<scalar_t>(), static_cast<scalar_t>(spatial_scale), channels, height, width, pooled_height, pooled_width, sampling_ratio, rois.data<scalar_t>(), output.data<scalar_t>()); })); AT_ASSERT(cudaGetLastError() == cudaSuccess); return output; } at::Tensor ROIAlign_Backward_CUDA( const at::Tensor rois, const at::Tensor grad_output, int64_t b_size, int64_t channels, int64_t height, int64_t width, int64_t pooled_height, int64_t pooled_width, double spatial_scale, int64_t sampling_ratio) { AT_ASSERT(rois.is_contiguous()); AT_ASSERT(rois.ndimension() == 2); AT_ASSERT(rois.size(1) == 5); auto roi_cols = rois.size(1); AT_ASSERT(roi_cols == 4 || roi_cols == 5); // Output Tensor is (num_rois, C, pooled_height, pooled_width) // gradient wrt input features auto grad_in = torch::zeros({b_size, channels, height, width}, rois.options()); auto num_rois = rois.size(0); auto count = grad_output.numel(); AT_DISPATCH_FLOATING_TYPES(rois.type(), "ROIAlign_Backward_CUDA", ([&] { RoIAlignBackwardKernel<scalar_t> <<<ROI_GET_BLOCKS(count), ROI_CUDA_NUM_THREADS, 0, at::cuda::getCurrentCUDAStream()>>>( count, grad_output.data<scalar_t>(), num_rois, static_cast<scalar_t>(spatial_scale), channels, height, width, pooled_height, pooled_width, sampling_ratio, grad_in.data<scalar_t>(), rois.data<scalar_t>()); })); AT_ASSERT(cudaGetLastError() == cudaSuccess); return grad_in; }
37c6fe413b48db668e64f2885b1645a3c96b0301.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include "DataComputerPressure.h" #include "cutils_func.h" #include "boost_for_export.h" #include "State.h" namespace py = boost::python; using namespace MD_ENGINE; const std::string computer_type_ = "pressure"; DataComputerPressure::DataComputerPressure(State *state_, std::string computeMode_) : DataComputer(state_, computeMode_, true,computer_type_), tempComputer(state_, computeMode_) { usingExternalTemperature = false; if (computeMode == "vector") { requiresPerAtomVirials = true; } } void DataComputerPressure::computeScalar_GPU(bool transferToCPU, uint32_t groupTag) { mdAssert(groupTag == 1, "Trying to compute pressure for group other than 'all'"); if (!usingExternalTemperature) { tempComputer.computeScalar_GPU(transferToCPU, groupTag); } GPUData &gpd = state->gpd; gpuBuffer.d_data.memset(0); lastGroupTag = groupTag; int nAtoms = state->atoms.size(); if (groupTag == 1) { hipLaunchKernelGGL(( accumulate_gpu<real, Virial, SumVirialToScalar, N_DATA_PER_THREAD>) , dim3(NBLOCK(nAtoms / (double) N_DATA_PER_THREAD)), dim3(PERBLOCK), N_DATA_PER_THREAD*PERBLOCK*sizeof(real), 0, gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirialToScalar()); } else { hipLaunchKernelGGL(( accumulate_gpu_if<real, Virial, SumVirialToScalarIf, N_DATA_PER_THREAD>) , dim3(NBLOCK(nAtoms / (double) N_DATA_PER_THREAD)), dim3(PERBLOCK), N_DATA_PER_THREAD*PERBLOCK*sizeof(real), 0, gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirialToScalarIf(gpd.fs.getDevData(), groupTag)); } if (transferToCPU) { //does NOT sync gpuBuffer.dataToHost(); } } void DataComputerPressure::computeVector_GPU(bool transferToCPU, uint32_t groupTag) { if (transferToCPU) { state->gpd.virials.dataToHost(); } } void DataComputerPressure::computeTensor_GPU(bool transferToCPU, uint32_t groupTag) { mdAssert(groupTag == 1, "Trying to compute pressure for group other than 'all'"); if (!usingExternalTemperature) { tempComputer.computeTensor_GPU(transferToCPU, groupTag); } GPUData &gpd = state->gpd; gpuBuffer.d_data.memset(0); lastGroupTag = groupTag; int nAtoms = state->atoms.size(); if (groupTag == 1) { hipLaunchKernelGGL(( accumulate_gpu<Virial, Virial, SumVirial, N_DATA_PER_THREAD>) , dim3(NBLOCK(nAtoms / (double) N_DATA_PER_THREAD)), dim3(PERBLOCK), N_DATA_PER_THREAD*PERBLOCK*sizeof(Virial), 0, (Virial *) gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirial()); } else { hipLaunchKernelGGL(( accumulate_gpu_if<Virial, Virial, SumVirialIf, N_DATA_PER_THREAD>) , dim3(NBLOCK(nAtoms / (double) N_DATA_PER_THREAD)), dim3(PERBLOCK), N_DATA_PER_THREAD*PERBLOCK*sizeof(Virial), 0, (Virial *) gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirialIf(gpd.fs.getDevData(), groupTag)); } if (transferToCPU) { //does NOT sync gpuBuffer.dataToHost(); } } void DataComputerPressure::computeScalar_CPU() { //we are assuming that z component of virial is zero if sim is 2D real boltz = state->units.boltz; double tempScalar_loc, ndf_loc; if (usingExternalTemperature) { tempScalar_loc = tempScalar; ndf_loc = tempNDF; } else { tempComputer.computeScalar_CPU(); tempScalar_loc = tempComputer.tempScalar; ndf_loc = tempComputer.ndf; } double sumVirial = gpuBuffer.h_data[0]; double dim = state->is2d ? 2 : 3; double volume = state->boundsGPU.volume(); pressureScalar = (tempScalar_loc * ndf_loc * boltz + sumVirial) / (dim * volume) * state->units.nktv_to_press; } void DataComputerPressure::computeVector_CPU() { //not implemented assert(false); } void DataComputerPressure::computeTensor_CPU() { Virial tempTensor_loc; if (usingExternalTemperature) { tempTensor_loc = tempTensor; } else { tempComputer.computeTensor_CPU(); tempTensor_loc = tempComputer.tempTensor; } pressureTensor = Virial(0, 0, 0, 0, 0, 0); Virial sumVirial = * (Virial *) gpuBuffer.h_data.data(); raw_virial_data = sumVirial; // do we need this though..? // if we just do += (constraint_virial / volume * state->units.nktv_to_press) it should be equivalent // XXX we do tempTensor_loc + sumVirial... gmx does -sumVirial... ??? // ---- should we switch signs on constraint virial? TBD. double volume = state->boundsGPU.volume(); for (int i=0; i<6; i++) { pressureTensor[i] = (tempTensor_loc[i] + sumVirial[i]) / volume * state->units.nktv_to_press; } if (state->is2d) { pressureTensor[2] = 0; pressureTensor[4] = 0; pressureTensor[5] = 0; } } void DataComputerPressure::appendScalar(boost::python::list &vals) { vals.append(pressureScalar); } void DataComputerPressure::appendVector(boost::python::list &vals) { //not implemented assert(false); } void DataComputerPressure::appendTensor(boost::python::list &vals) { vals.append(pressureTensor); } void DataComputerPressure::prepareForRun() { tempComputer = DataComputerTemperature(state, computeMode); tempComputer.prepareForRun(); DataComputer::prepareForRun(); }
37c6fe413b48db668e64f2885b1645a3c96b0301.cu
#include "DataComputerPressure.h" #include "cutils_func.h" #include "boost_for_export.h" #include "State.h" namespace py = boost::python; using namespace MD_ENGINE; const std::string computer_type_ = "pressure"; DataComputerPressure::DataComputerPressure(State *state_, std::string computeMode_) : DataComputer(state_, computeMode_, true,computer_type_), tempComputer(state_, computeMode_) { usingExternalTemperature = false; if (computeMode == "vector") { requiresPerAtomVirials = true; } } void DataComputerPressure::computeScalar_GPU(bool transferToCPU, uint32_t groupTag) { mdAssert(groupTag == 1, "Trying to compute pressure for group other than 'all'"); if (!usingExternalTemperature) { tempComputer.computeScalar_GPU(transferToCPU, groupTag); } GPUData &gpd = state->gpd; gpuBuffer.d_data.memset(0); lastGroupTag = groupTag; int nAtoms = state->atoms.size(); if (groupTag == 1) { accumulate_gpu<real, Virial, SumVirialToScalar, N_DATA_PER_THREAD> <<<NBLOCK(nAtoms / (double) N_DATA_PER_THREAD), PERBLOCK, N_DATA_PER_THREAD*PERBLOCK*sizeof(real)>>> (gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirialToScalar()); } else { accumulate_gpu_if<real, Virial, SumVirialToScalarIf, N_DATA_PER_THREAD> <<<NBLOCK(nAtoms / (double) N_DATA_PER_THREAD), PERBLOCK, N_DATA_PER_THREAD*PERBLOCK*sizeof(real)>>> (gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirialToScalarIf(gpd.fs.getDevData(), groupTag)); } if (transferToCPU) { //does NOT sync gpuBuffer.dataToHost(); } } void DataComputerPressure::computeVector_GPU(bool transferToCPU, uint32_t groupTag) { if (transferToCPU) { state->gpd.virials.dataToHost(); } } void DataComputerPressure::computeTensor_GPU(bool transferToCPU, uint32_t groupTag) { mdAssert(groupTag == 1, "Trying to compute pressure for group other than 'all'"); if (!usingExternalTemperature) { tempComputer.computeTensor_GPU(transferToCPU, groupTag); } GPUData &gpd = state->gpd; gpuBuffer.d_data.memset(0); lastGroupTag = groupTag; int nAtoms = state->atoms.size(); if (groupTag == 1) { accumulate_gpu<Virial, Virial, SumVirial, N_DATA_PER_THREAD> <<<NBLOCK(nAtoms / (double) N_DATA_PER_THREAD), PERBLOCK, N_DATA_PER_THREAD*PERBLOCK*sizeof(Virial)>>> ((Virial *) gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirial()); } else { accumulate_gpu_if<Virial, Virial, SumVirialIf, N_DATA_PER_THREAD> <<<NBLOCK(nAtoms / (double) N_DATA_PER_THREAD), PERBLOCK, N_DATA_PER_THREAD*PERBLOCK*sizeof(Virial)>>> ((Virial *) gpuBuffer.getDevData(), gpd.virials.getDevData(), nAtoms, state->devManager.prop.warpSize, SumVirialIf(gpd.fs.getDevData(), groupTag)); } if (transferToCPU) { //does NOT sync gpuBuffer.dataToHost(); } } void DataComputerPressure::computeScalar_CPU() { //we are assuming that z component of virial is zero if sim is 2D real boltz = state->units.boltz; double tempScalar_loc, ndf_loc; if (usingExternalTemperature) { tempScalar_loc = tempScalar; ndf_loc = tempNDF; } else { tempComputer.computeScalar_CPU(); tempScalar_loc = tempComputer.tempScalar; ndf_loc = tempComputer.ndf; } double sumVirial = gpuBuffer.h_data[0]; double dim = state->is2d ? 2 : 3; double volume = state->boundsGPU.volume(); pressureScalar = (tempScalar_loc * ndf_loc * boltz + sumVirial) / (dim * volume) * state->units.nktv_to_press; } void DataComputerPressure::computeVector_CPU() { //not implemented assert(false); } void DataComputerPressure::computeTensor_CPU() { Virial tempTensor_loc; if (usingExternalTemperature) { tempTensor_loc = tempTensor; } else { tempComputer.computeTensor_CPU(); tempTensor_loc = tempComputer.tempTensor; } pressureTensor = Virial(0, 0, 0, 0, 0, 0); Virial sumVirial = * (Virial *) gpuBuffer.h_data.data(); raw_virial_data = sumVirial; // do we need this though..? // if we just do += (constraint_virial / volume * state->units.nktv_to_press) it should be equivalent // XXX we do tempTensor_loc + sumVirial... gmx does -sumVirial... ??? // ---- should we switch signs on constraint virial? TBD. double volume = state->boundsGPU.volume(); for (int i=0; i<6; i++) { pressureTensor[i] = (tempTensor_loc[i] + sumVirial[i]) / volume * state->units.nktv_to_press; } if (state->is2d) { pressureTensor[2] = 0; pressureTensor[4] = 0; pressureTensor[5] = 0; } } void DataComputerPressure::appendScalar(boost::python::list &vals) { vals.append(pressureScalar); } void DataComputerPressure::appendVector(boost::python::list &vals) { //not implemented assert(false); } void DataComputerPressure::appendTensor(boost::python::list &vals) { vals.append(pressureTensor); } void DataComputerPressure::prepareForRun() { tempComputer = DataComputerTemperature(state, computeMode); tempComputer.prepareForRun(); DataComputer::prepareForRun(); }
3f8a5d6096fc1de70a00fba75eddc0725f227c5b.hip
// !!! This is a file automatically generated by hipify!!! // Copyright (c) 2020 Michael Koesel and respective contributors // SPDX-License-Identifier: MIT // See accompanying LICENSE file for detailed information #include "dogm/common.h" #include "dogm/cuda_utils.h" #include "dogm/dogm.h" #include "dogm/dogm_types.h" #include "dogm/kernel/ego_motion_compensation.h" #include "dogm/kernel/init.h" #include "dogm/kernel/init_new_particles.h" #include "dogm/kernel/mass_update.h" #include "dogm/kernel/particle_to_grid.h" #include "dogm/kernel/predict.h" #include "dogm/kernel/resampling.h" #include "dogm/kernel/statistical_moments.h" #include "dogm/kernel/update_persistent_particles.h" #include <thrust/sort.h> #include <thrust/transform.h> #include <hip/hip_runtime.h> #include <vector> namespace dogm { constexpr int BLOCK_SIZE = 256; DOGM::DOGM(const Params& params) : params(params), grid_size(static_cast<int>(params.size / params.resolution)), particle_count(params.particle_count), grid_cell_count(grid_size * grid_size), new_born_particle_count(params.new_born_particle_count), block_dim(BLOCK_SIZE), first_pose_received(false), first_measurement_received(false), position_x(0.0f), position_y(0.0f) { int device; CHECK_ERROR(hipGetDevice(&device)); hipDeviceProp_t device_prop; CHECK_ERROR(hipGetDeviceProperties(&device_prop, device)); int blocks_per_sm = device_prop.maxThreadsPerMultiProcessor / block_dim.x; dim3 dim(device_prop.multiProcessorCount * blocks_per_sm); particles_grid = birth_particles_grid = grid_map_grid = dim; particle_array.init(particle_count, true); particle_array_next.init(particle_count, true); birth_particle_array.init(new_born_particle_count, true); CHECK_ERROR(hipMalloc(&grid_cell_array, grid_cell_count * sizeof(GridCell))); CHECK_ERROR(hipMalloc(&meas_cell_array, grid_cell_count * sizeof(MeasurementCell))); CHECK_ERROR(hipMalloc(&weight_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&birth_weight_array, new_born_particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&born_masses_array, grid_cell_count * sizeof(float))); CHECK_ERROR(hipMalloc(&vel_x_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&vel_y_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&vel_x_squared_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&vel_y_squared_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&vel_xy_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&rand_array, particle_count * sizeof(float))); CHECK_ERROR(hipMalloc(&rng_states, particles_grid.x * block_dim.x * sizeof(hiprandState_t))); initialize(); } DOGM::~DOGM() { particle_array.free(); particle_array_next.free(); birth_particle_array.free(); CHECK_ERROR(hipFree(grid_cell_array)); CHECK_ERROR(hipFree(meas_cell_array)); CHECK_ERROR(hipFree(weight_array)); CHECK_ERROR(hipFree(birth_weight_array)); CHECK_ERROR(hipFree(born_masses_array)); CHECK_ERROR(hipFree(vel_x_array)); CHECK_ERROR(hipFree(vel_y_array)); CHECK_ERROR(hipFree(vel_x_squared_array)); CHECK_ERROR(hipFree(vel_y_squared_array)); CHECK_ERROR(hipFree(vel_xy_array)); CHECK_ERROR(hipFree(rng_states)); } void DOGM::initialize() { hipStream_t particles_stream, grid_stream; CHECK_ERROR(hipStreamCreate(&particles_stream)); CHECK_ERROR(hipStreamCreate(&grid_stream)); hipLaunchKernelGGL(( setupRandomStatesKernel), dim3(particles_grid), dim3(block_dim), 0, 0, rng_states, 123456, particles_grid.x * block_dim.x); CHECK_ERROR(hipGetLastError()); CHECK_ERROR(hipDeviceSynchronize()); hipLaunchKernelGGL(( initGridCellsKernel), dim3(grid_map_grid), dim3(block_dim), 0, grid_stream, grid_cell_array, meas_cell_array, grid_size, grid_cell_count); CHECK_ERROR(hipGetLastError()); CHECK_ERROR(hipStreamDestroy(particles_stream)); CHECK_ERROR(hipStreamDestroy(grid_stream)); } void DOGM::updateGrid(MeasurementCell* measurement_grid, float new_x, float new_y, float new_yaw, float dt, bool device) { updateMeasurementGrid(measurement_grid, device); updatePose(new_x, new_y, new_yaw); particlePrediction(dt); particleAssignment(); gridCellOccupancyUpdate(); updatePersistentParticles(); initializeNewParticles(); statisticalMoments(); resampling(); particle_array = particle_array_next; CHECK_ERROR(hipDeviceSynchronize()); } std::vector<GridCell> DOGM::getGridCells() const { std::vector<GridCell> grid_cells(static_cast<std::vector<GridCell>::size_type>(grid_cell_count)); CHECK_ERROR( hipMemcpy(grid_cells.data(), grid_cell_array, grid_cell_count * sizeof(GridCell), hipMemcpyDeviceToHost)); return grid_cells; } std::vector<MeasurementCell> DOGM::getMeasurementCells() const { std::vector<MeasurementCell> meas_cells(static_cast<std::vector<GridCell>::size_type>(grid_cell_count)); CHECK_ERROR(hipMemcpy(meas_cells.data(), meas_cell_array, grid_cell_count * sizeof(MeasurementCell), hipMemcpyDeviceToHost)); return meas_cells; } ParticlesSoA DOGM::getParticles() const { ParticlesSoA particles(particle_count, false); particles.copy(particle_array, hipMemcpyDeviceToHost); return particles; } void DOGM::updatePose(float new_x, float new_y, float new_yaw) { if (!first_pose_received) { position_x = new_x; position_y = new_y; yaw = new_yaw; first_pose_received = true; } else { const float x_diff = new_x - position_x; const float y_diff = new_y - position_y; if (fabsf(x_diff) > params.resolution || fabsf(y_diff) > params.resolution) { const int x_move = -static_cast<int>(x_diff / params.resolution); const int y_move = -static_cast<int>(y_diff / params.resolution); GridCell* old_grid_cell_array; CHECK_ERROR(hipMalloc(&old_grid_cell_array, grid_cell_count * sizeof(GridCell))); CHECK_ERROR(hipMemcpy(old_grid_cell_array, grid_cell_array, grid_cell_count * sizeof(GridCell), hipMemcpyDeviceToDevice)); CHECK_ERROR(hipMemset(grid_cell_array, 0, grid_cell_count * sizeof(GridCell))); dim3 dim_block(32, 32); dim3 grid_dim(divUp(grid_size, dim_block.x), divUp(grid_size, dim_block.y)); hipLaunchKernelGGL(( moveParticlesKernel), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, x_move, y_move, particle_count); CHECK_ERROR(hipGetLastError()); hipLaunchKernelGGL(( moveMapKernel), dim3(grid_dim), dim3(dim_block), 0, 0, grid_cell_array, old_grid_cell_array, x_move, y_move, grid_size); CHECK_ERROR(hipGetLastError()); CHECK_ERROR(hipFree(old_grid_cell_array)); position_x = new_x; position_y = new_y; yaw = new_yaw; } } } void DOGM::updateMeasurementGrid(MeasurementCell* measurement_grid, bool device) { hipMemcpyKind kind = device ? hipMemcpyDeviceToDevice : hipMemcpyHostToDevice; CHECK_ERROR(hipMemcpy(meas_cell_array, measurement_grid, grid_cell_count * sizeof(MeasurementCell), kind)); if (!first_measurement_received) { initializeParticles(); first_measurement_received = true; } } void DOGM::initializeParticles() { hipLaunchKernelGGL(( copyMassesKernel), dim3(grid_map_grid), dim3(block_dim), 0, 0, meas_cell_array, born_masses_array, grid_cell_count); CHECK_ERROR(hipGetLastError()); CHECK_ERROR(hipDeviceSynchronize()); thrust::device_vector<float> particle_orders_accum(grid_cell_count); accumulate(born_masses_array, particle_orders_accum); float* particle_orders_array_accum = thrust::raw_pointer_cast(particle_orders_accum.data()); float new_weight = 1.0f / particle_count; normalize_particle_orders(particle_orders_array_accum, grid_cell_count, particle_count); hipLaunchKernelGGL(( initParticlesKernel1), dim3(grid_map_grid), dim3(block_dim), 0, 0, grid_cell_array, meas_cell_array, particle_array, particle_orders_array_accum, grid_cell_count); CHECK_ERROR(hipGetLastError()); hipLaunchKernelGGL(( initParticlesKernel2), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, grid_cell_array, rng_states, params.init_max_velocity, grid_size, new_weight, particle_count); CHECK_ERROR(hipGetLastError()); } void DOGM::particlePrediction(float dt) { // clang-format off glm::mat4x4 transition_matrix(1, 0, dt, 0, 0, 1, 0, dt, 0, 0, 1, 0, 0, 0, 0, 1); // clang-format on // FIXME: glm uses column major, we need row major transition_matrix = glm::transpose(transition_matrix); hipLaunchKernelGGL(( predictKernel), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, rng_states, params.stddev_velocity, grid_size, params.persistence_prob, transition_matrix, params.stddev_process_noise_position, params.stddev_process_noise_velocity, particle_count); CHECK_ERROR(hipGetLastError()); } void DOGM::particleAssignment() { hipLaunchKernelGGL(( reinitGridParticleIndices), dim3(grid_map_grid), dim3(block_dim), 0, 0, grid_cell_array, grid_cell_count); CHECK_ERROR(hipGetLastError()); // CHECK_ERROR(hipDeviceSynchronize()); // sort particles thrust::device_ptr<int> grid_index_ptr(particle_array.grid_cell_idx); thrust::device_ptr<float> weight_ptr(particle_array.weight); thrust::device_ptr<bool> associated_ptr(particle_array.associated); thrust::device_ptr<glm::vec4> state_ptr(particle_array.state); auto it = thrust::make_zip_iterator(thrust::make_tuple(weight_ptr, associated_ptr, state_ptr)); thrust::sort_by_key(grid_index_ptr, grid_index_ptr + particle_count, it); hipLaunchKernelGGL(( particleToGridKernel), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, grid_cell_array, weight_array, particle_count); CHECK_ERROR(hipGetLastError()); } void DOGM::gridCellOccupancyUpdate() { // CHECK_ERROR(hipDeviceSynchronize()); thrust::device_vector<float> weights_accum(particle_count); accumulate(weight_array, weights_accum); float* weight_array_accum = thrust::raw_pointer_cast(weights_accum.data()); hipLaunchKernelGGL(( gridCellPredictionUpdateKernel), dim3(grid_map_grid), dim3(block_dim), 0, 0, grid_cell_array, particle_array, weight_array, weight_array_accum, meas_cell_array, born_masses_array, params.birth_prob, grid_cell_count); CHECK_ERROR(hipGetLastError()); } void DOGM::updatePersistentParticles() { hipLaunchKernelGGL(( updatePersistentParticlesKernel1), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, meas_cell_array, weight_array, particle_count); CHECK_ERROR(hipGetLastError()); // CHECK_ERROR(hipDeviceSynchronize()); thrust::device_vector<float> weights_accum(particle_count); accumulate(weight_array, weights_accum); float* weight_array_accum = thrust::raw_pointer_cast(weights_accum.data()); hipLaunchKernelGGL(( updatePersistentParticlesKernel2), dim3(divUp(grid_cell_count, BLOCK_SIZE)), dim3(BLOCK_SIZE), 0, 0, grid_cell_array, weight_array_accum, grid_cell_count); CHECK_ERROR(hipGetLastError()); hipLaunchKernelGGL(( updatePersistentParticlesKernel3), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, meas_cell_array, grid_cell_array, weight_array, particle_count); CHECK_ERROR(hipGetLastError()); } void DOGM::initializeNewParticles() { hipLaunchKernelGGL(( initBirthParticlesKernel), dim3(birth_particles_grid), dim3(block_dim), 0, 0, birth_particle_array, rng_states, params.stddev_velocity, grid_size, new_born_particle_count); CHECK_ERROR(hipGetLastError()); // CHECK_ERROR(hipDeviceSynchronize()); thrust::device_vector<float> particle_orders_accum(grid_cell_count); accumulate(born_masses_array, particle_orders_accum); float* particle_orders_array_accum = thrust::raw_pointer_cast(particle_orders_accum.data()); normalize_particle_orders(particle_orders_array_accum, grid_cell_count, new_born_particle_count); hipLaunchKernelGGL(( initNewParticlesKernel1), dim3(grid_map_grid), dim3(block_dim), 0, 0, grid_cell_array, meas_cell_array, weight_array, born_masses_array, birth_particle_array, particle_orders_array_accum, grid_cell_count); CHECK_ERROR(hipGetLastError()); hipLaunchKernelGGL(( initNewParticlesKernel2), dim3(birth_particles_grid), dim3(block_dim), 0, 0, birth_particle_array, grid_cell_array, rng_states, params.stddev_velocity, params.init_max_velocity, grid_size, new_born_particle_count); CHECK_ERROR(hipGetLastError()); } void DOGM::statisticalMoments() { hipLaunchKernelGGL(( statisticalMomentsKernel1), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, weight_array, vel_x_array, vel_y_array, vel_x_squared_array, vel_y_squared_array, vel_xy_array, particle_count); CHECK_ERROR(hipGetLastError()); // CHECK_ERROR(hipDeviceSynchronize()); thrust::device_vector<float> vel_x_accum(particle_count); accumulate(vel_x_array, vel_x_accum); float* vel_x_array_accum = thrust::raw_pointer_cast(vel_x_accum.data()); thrust::device_vector<float> vel_y_accum(particle_count); accumulate(vel_y_array, vel_y_accum); float* vel_y_array_accum = thrust::raw_pointer_cast(vel_y_accum.data()); thrust::device_vector<float> vel_x_squared_accum(particle_count); accumulate(vel_x_squared_array, vel_x_squared_accum); float* vel_x_squared_array_accum = thrust::raw_pointer_cast(vel_x_squared_accum.data()); thrust::device_vector<float> vel_y_squared_accum(particle_count); accumulate(vel_y_squared_array, vel_y_squared_accum); float* vel_y_squared_array_accum = thrust::raw_pointer_cast(vel_y_squared_accum.data()); thrust::device_vector<float> vel_xy_accum(particle_count); accumulate(vel_xy_array, vel_xy_accum); float* vel_xy_array_accum = thrust::raw_pointer_cast(vel_xy_accum.data()); hipLaunchKernelGGL(( statisticalMomentsKernel2), dim3(grid_map_grid), dim3(block_dim), 0, 0, grid_cell_array, vel_x_array_accum, vel_y_array_accum, vel_x_squared_array_accum, vel_y_squared_array_accum, vel_xy_array_accum, grid_cell_count); CHECK_ERROR(hipGetLastError()); } void DOGM::resampling() { // CHECK_ERROR(hipDeviceSynchronize()); thrust::device_ptr<float> persistent_weights(weight_array); thrust::device_ptr<float> new_born_weights(birth_particle_array.weight); thrust::device_vector<float> joint_weight_array; joint_weight_array.insert(joint_weight_array.end(), persistent_weights, persistent_weights + particle_count); joint_weight_array.insert(joint_weight_array.end(), new_born_weights, new_born_weights + new_born_particle_count); thrust::device_vector<float> joint_weight_accum(joint_weight_array.size()); accumulate(joint_weight_array, joint_weight_accum); float joint_max = joint_weight_accum.back(); hipLaunchKernelGGL(( resamplingGenerateRandomNumbersKernel), dim3(particles_grid), dim3(block_dim), 0, 0, rand_array, rng_states, joint_max, particle_count); CHECK_ERROR(hipGetLastError()); // CHECK_ERROR(hipDeviceSynchronize()); thrust::device_ptr<float> rand_ptr(rand_array); thrust::device_vector<float> rand_vector(rand_ptr, rand_ptr + particle_count); thrust::sort(rand_vector.begin(), rand_vector.end()); thrust::device_vector<int> idx_resampled(particle_count); calc_resampled_indices(joint_weight_accum, rand_vector, idx_resampled, joint_max); int* idx_array_resampled = thrust::raw_pointer_cast(idx_resampled.data()); float new_weight = joint_max / particle_count; hipLaunchKernelGGL(( resamplingKernel), dim3(particles_grid), dim3(block_dim), 0, 0, particle_array, particle_array_next, birth_particle_array, idx_array_resampled, new_weight, particle_count); CHECK_ERROR(hipGetLastError()); } } /* namespace dogm */
3f8a5d6096fc1de70a00fba75eddc0725f227c5b.cu
// Copyright (c) 2020 Michael Koesel and respective contributors // SPDX-License-Identifier: MIT // See accompanying LICENSE file for detailed information #include "dogm/common.h" #include "dogm/cuda_utils.h" #include "dogm/dogm.h" #include "dogm/dogm_types.h" #include "dogm/kernel/ego_motion_compensation.h" #include "dogm/kernel/init.h" #include "dogm/kernel/init_new_particles.h" #include "dogm/kernel/mass_update.h" #include "dogm/kernel/particle_to_grid.h" #include "dogm/kernel/predict.h" #include "dogm/kernel/resampling.h" #include "dogm/kernel/statistical_moments.h" #include "dogm/kernel/update_persistent_particles.h" #include <thrust/sort.h> #include <thrust/transform.h> #include <cuda_runtime.h> #include <vector> namespace dogm { constexpr int BLOCK_SIZE = 256; DOGM::DOGM(const Params& params) : params(params), grid_size(static_cast<int>(params.size / params.resolution)), particle_count(params.particle_count), grid_cell_count(grid_size * grid_size), new_born_particle_count(params.new_born_particle_count), block_dim(BLOCK_SIZE), first_pose_received(false), first_measurement_received(false), position_x(0.0f), position_y(0.0f) { int device; CHECK_ERROR(cudaGetDevice(&device)); cudaDeviceProp device_prop; CHECK_ERROR(cudaGetDeviceProperties(&device_prop, device)); int blocks_per_sm = device_prop.maxThreadsPerMultiProcessor / block_dim.x; dim3 dim(device_prop.multiProcessorCount * blocks_per_sm); particles_grid = birth_particles_grid = grid_map_grid = dim; particle_array.init(particle_count, true); particle_array_next.init(particle_count, true); birth_particle_array.init(new_born_particle_count, true); CHECK_ERROR(cudaMalloc(&grid_cell_array, grid_cell_count * sizeof(GridCell))); CHECK_ERROR(cudaMalloc(&meas_cell_array, grid_cell_count * sizeof(MeasurementCell))); CHECK_ERROR(cudaMalloc(&weight_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&birth_weight_array, new_born_particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&born_masses_array, grid_cell_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&vel_x_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&vel_y_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&vel_x_squared_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&vel_y_squared_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&vel_xy_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&rand_array, particle_count * sizeof(float))); CHECK_ERROR(cudaMalloc(&rng_states, particles_grid.x * block_dim.x * sizeof(curandState))); initialize(); } DOGM::~DOGM() { particle_array.free(); particle_array_next.free(); birth_particle_array.free(); CHECK_ERROR(cudaFree(grid_cell_array)); CHECK_ERROR(cudaFree(meas_cell_array)); CHECK_ERROR(cudaFree(weight_array)); CHECK_ERROR(cudaFree(birth_weight_array)); CHECK_ERROR(cudaFree(born_masses_array)); CHECK_ERROR(cudaFree(vel_x_array)); CHECK_ERROR(cudaFree(vel_y_array)); CHECK_ERROR(cudaFree(vel_x_squared_array)); CHECK_ERROR(cudaFree(vel_y_squared_array)); CHECK_ERROR(cudaFree(vel_xy_array)); CHECK_ERROR(cudaFree(rng_states)); } void DOGM::initialize() { cudaStream_t particles_stream, grid_stream; CHECK_ERROR(cudaStreamCreate(&particles_stream)); CHECK_ERROR(cudaStreamCreate(&grid_stream)); setupRandomStatesKernel<<<particles_grid, block_dim>>>(rng_states, 123456, particles_grid.x * block_dim.x); CHECK_ERROR(cudaGetLastError()); CHECK_ERROR(cudaDeviceSynchronize()); initGridCellsKernel<<<grid_map_grid, block_dim, 0, grid_stream>>>(grid_cell_array, meas_cell_array, grid_size, grid_cell_count); CHECK_ERROR(cudaGetLastError()); CHECK_ERROR(cudaStreamDestroy(particles_stream)); CHECK_ERROR(cudaStreamDestroy(grid_stream)); } void DOGM::updateGrid(MeasurementCell* measurement_grid, float new_x, float new_y, float new_yaw, float dt, bool device) { updateMeasurementGrid(measurement_grid, device); updatePose(new_x, new_y, new_yaw); particlePrediction(dt); particleAssignment(); gridCellOccupancyUpdate(); updatePersistentParticles(); initializeNewParticles(); statisticalMoments(); resampling(); particle_array = particle_array_next; CHECK_ERROR(cudaDeviceSynchronize()); } std::vector<GridCell> DOGM::getGridCells() const { std::vector<GridCell> grid_cells(static_cast<std::vector<GridCell>::size_type>(grid_cell_count)); CHECK_ERROR( cudaMemcpy(grid_cells.data(), grid_cell_array, grid_cell_count * sizeof(GridCell), cudaMemcpyDeviceToHost)); return grid_cells; } std::vector<MeasurementCell> DOGM::getMeasurementCells() const { std::vector<MeasurementCell> meas_cells(static_cast<std::vector<GridCell>::size_type>(grid_cell_count)); CHECK_ERROR(cudaMemcpy(meas_cells.data(), meas_cell_array, grid_cell_count * sizeof(MeasurementCell), cudaMemcpyDeviceToHost)); return meas_cells; } ParticlesSoA DOGM::getParticles() const { ParticlesSoA particles(particle_count, false); particles.copy(particle_array, cudaMemcpyDeviceToHost); return particles; } void DOGM::updatePose(float new_x, float new_y, float new_yaw) { if (!first_pose_received) { position_x = new_x; position_y = new_y; yaw = new_yaw; first_pose_received = true; } else { const float x_diff = new_x - position_x; const float y_diff = new_y - position_y; if (fabsf(x_diff) > params.resolution || fabsf(y_diff) > params.resolution) { const int x_move = -static_cast<int>(x_diff / params.resolution); const int y_move = -static_cast<int>(y_diff / params.resolution); GridCell* old_grid_cell_array; CHECK_ERROR(cudaMalloc(&old_grid_cell_array, grid_cell_count * sizeof(GridCell))); CHECK_ERROR(cudaMemcpy(old_grid_cell_array, grid_cell_array, grid_cell_count * sizeof(GridCell), cudaMemcpyDeviceToDevice)); CHECK_ERROR(cudaMemset(grid_cell_array, 0, grid_cell_count * sizeof(GridCell))); dim3 dim_block(32, 32); dim3 grid_dim(divUp(grid_size, dim_block.x), divUp(grid_size, dim_block.y)); moveParticlesKernel<<<particles_grid, block_dim>>>(particle_array, x_move, y_move, particle_count); CHECK_ERROR(cudaGetLastError()); moveMapKernel<<<grid_dim, dim_block>>>(grid_cell_array, old_grid_cell_array, x_move, y_move, grid_size); CHECK_ERROR(cudaGetLastError()); CHECK_ERROR(cudaFree(old_grid_cell_array)); position_x = new_x; position_y = new_y; yaw = new_yaw; } } } void DOGM::updateMeasurementGrid(MeasurementCell* measurement_grid, bool device) { cudaMemcpyKind kind = device ? cudaMemcpyDeviceToDevice : cudaMemcpyHostToDevice; CHECK_ERROR(cudaMemcpy(meas_cell_array, measurement_grid, grid_cell_count * sizeof(MeasurementCell), kind)); if (!first_measurement_received) { initializeParticles(); first_measurement_received = true; } } void DOGM::initializeParticles() { copyMassesKernel<<<grid_map_grid, block_dim>>>(meas_cell_array, born_masses_array, grid_cell_count); CHECK_ERROR(cudaGetLastError()); CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_vector<float> particle_orders_accum(grid_cell_count); accumulate(born_masses_array, particle_orders_accum); float* particle_orders_array_accum = thrust::raw_pointer_cast(particle_orders_accum.data()); float new_weight = 1.0f / particle_count; normalize_particle_orders(particle_orders_array_accum, grid_cell_count, particle_count); initParticlesKernel1<<<grid_map_grid, block_dim>>>(grid_cell_array, meas_cell_array, particle_array, particle_orders_array_accum, grid_cell_count); CHECK_ERROR(cudaGetLastError()); initParticlesKernel2<<<particles_grid, block_dim>>>( particle_array, grid_cell_array, rng_states, params.init_max_velocity, grid_size, new_weight, particle_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::particlePrediction(float dt) { // clang-format off glm::mat4x4 transition_matrix(1, 0, dt, 0, 0, 1, 0, dt, 0, 0, 1, 0, 0, 0, 0, 1); // clang-format on // FIXME: glm uses column major, we need row major transition_matrix = glm::transpose(transition_matrix); predictKernel<<<particles_grid, block_dim>>>( particle_array, rng_states, params.stddev_velocity, grid_size, params.persistence_prob, transition_matrix, params.stddev_process_noise_position, params.stddev_process_noise_velocity, particle_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::particleAssignment() { reinitGridParticleIndices<<<grid_map_grid, block_dim>>>(grid_cell_array, grid_cell_count); CHECK_ERROR(cudaGetLastError()); // CHECK_ERROR(cudaDeviceSynchronize()); // sort particles thrust::device_ptr<int> grid_index_ptr(particle_array.grid_cell_idx); thrust::device_ptr<float> weight_ptr(particle_array.weight); thrust::device_ptr<bool> associated_ptr(particle_array.associated); thrust::device_ptr<glm::vec4> state_ptr(particle_array.state); auto it = thrust::make_zip_iterator(thrust::make_tuple(weight_ptr, associated_ptr, state_ptr)); thrust::sort_by_key(grid_index_ptr, grid_index_ptr + particle_count, it); particleToGridKernel<<<particles_grid, block_dim>>>(particle_array, grid_cell_array, weight_array, particle_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::gridCellOccupancyUpdate() { // CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_vector<float> weights_accum(particle_count); accumulate(weight_array, weights_accum); float* weight_array_accum = thrust::raw_pointer_cast(weights_accum.data()); gridCellPredictionUpdateKernel<<<grid_map_grid, block_dim>>>(grid_cell_array, particle_array, weight_array, weight_array_accum, meas_cell_array, born_masses_array, params.birth_prob, grid_cell_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::updatePersistentParticles() { updatePersistentParticlesKernel1<<<particles_grid, block_dim>>>(particle_array, meas_cell_array, weight_array, particle_count); CHECK_ERROR(cudaGetLastError()); // CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_vector<float> weights_accum(particle_count); accumulate(weight_array, weights_accum); float* weight_array_accum = thrust::raw_pointer_cast(weights_accum.data()); updatePersistentParticlesKernel2<<<divUp(grid_cell_count, BLOCK_SIZE), BLOCK_SIZE>>>( grid_cell_array, weight_array_accum, grid_cell_count); CHECK_ERROR(cudaGetLastError()); updatePersistentParticlesKernel3<<<particles_grid, block_dim>>>(particle_array, meas_cell_array, grid_cell_array, weight_array, particle_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::initializeNewParticles() { initBirthParticlesKernel<<<birth_particles_grid, block_dim>>>( birth_particle_array, rng_states, params.stddev_velocity, grid_size, new_born_particle_count); CHECK_ERROR(cudaGetLastError()); // CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_vector<float> particle_orders_accum(grid_cell_count); accumulate(born_masses_array, particle_orders_accum); float* particle_orders_array_accum = thrust::raw_pointer_cast(particle_orders_accum.data()); normalize_particle_orders(particle_orders_array_accum, grid_cell_count, new_born_particle_count); initNewParticlesKernel1<<<grid_map_grid, block_dim>>>(grid_cell_array, meas_cell_array, weight_array, born_masses_array, birth_particle_array, particle_orders_array_accum, grid_cell_count); CHECK_ERROR(cudaGetLastError()); initNewParticlesKernel2<<<birth_particles_grid, block_dim>>>(birth_particle_array, grid_cell_array, rng_states, params.stddev_velocity, params.init_max_velocity, grid_size, new_born_particle_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::statisticalMoments() { statisticalMomentsKernel1<<<particles_grid, block_dim>>>(particle_array, weight_array, vel_x_array, vel_y_array, vel_x_squared_array, vel_y_squared_array, vel_xy_array, particle_count); CHECK_ERROR(cudaGetLastError()); // CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_vector<float> vel_x_accum(particle_count); accumulate(vel_x_array, vel_x_accum); float* vel_x_array_accum = thrust::raw_pointer_cast(vel_x_accum.data()); thrust::device_vector<float> vel_y_accum(particle_count); accumulate(vel_y_array, vel_y_accum); float* vel_y_array_accum = thrust::raw_pointer_cast(vel_y_accum.data()); thrust::device_vector<float> vel_x_squared_accum(particle_count); accumulate(vel_x_squared_array, vel_x_squared_accum); float* vel_x_squared_array_accum = thrust::raw_pointer_cast(vel_x_squared_accum.data()); thrust::device_vector<float> vel_y_squared_accum(particle_count); accumulate(vel_y_squared_array, vel_y_squared_accum); float* vel_y_squared_array_accum = thrust::raw_pointer_cast(vel_y_squared_accum.data()); thrust::device_vector<float> vel_xy_accum(particle_count); accumulate(vel_xy_array, vel_xy_accum); float* vel_xy_array_accum = thrust::raw_pointer_cast(vel_xy_accum.data()); statisticalMomentsKernel2<<<grid_map_grid, block_dim>>>(grid_cell_array, vel_x_array_accum, vel_y_array_accum, vel_x_squared_array_accum, vel_y_squared_array_accum, vel_xy_array_accum, grid_cell_count); CHECK_ERROR(cudaGetLastError()); } void DOGM::resampling() { // CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_ptr<float> persistent_weights(weight_array); thrust::device_ptr<float> new_born_weights(birth_particle_array.weight); thrust::device_vector<float> joint_weight_array; joint_weight_array.insert(joint_weight_array.end(), persistent_weights, persistent_weights + particle_count); joint_weight_array.insert(joint_weight_array.end(), new_born_weights, new_born_weights + new_born_particle_count); thrust::device_vector<float> joint_weight_accum(joint_weight_array.size()); accumulate(joint_weight_array, joint_weight_accum); float joint_max = joint_weight_accum.back(); resamplingGenerateRandomNumbersKernel<<<particles_grid, block_dim>>>(rand_array, rng_states, joint_max, particle_count); CHECK_ERROR(cudaGetLastError()); // CHECK_ERROR(cudaDeviceSynchronize()); thrust::device_ptr<float> rand_ptr(rand_array); thrust::device_vector<float> rand_vector(rand_ptr, rand_ptr + particle_count); thrust::sort(rand_vector.begin(), rand_vector.end()); thrust::device_vector<int> idx_resampled(particle_count); calc_resampled_indices(joint_weight_accum, rand_vector, idx_resampled, joint_max); int* idx_array_resampled = thrust::raw_pointer_cast(idx_resampled.data()); float new_weight = joint_max / particle_count; resamplingKernel<<<particles_grid, block_dim>>>(particle_array, particle_array_next, birth_particle_array, idx_array_resampled, new_weight, particle_count); CHECK_ERROR(cudaGetLastError()); } } /* namespace dogm */
c5f748b5de867d9d46cfa3b525d08b4f47e3eef0.hip
// !!! This is a file automatically generated by hipify!!! #include"svd.h" #include<cusolverDn.h> void svd(int m,int n,hipComplex* T,hipComplex* U,hipComplex* V,float* S){ hipsolverDnHandle_t handle; hipsolverGesvdjInfo_t params=NULL; int* info=NULL; int echo=1; int lda=0; lda=m; int ldu=0; ldu=m; int ldv=0; ldv=n; int lwork=0; hipComplex* work=NULL; float* s=NULL; hipComplex* u=NULL; hipComplex* v=NULL; hipComplex* t=NULL; cusolverStatus_t status=CUSOLVER_STATUS_SUCCESS; status=hipsolverDnCreate(&handle); assert(status==CUSOLVER_STATUS_SUCCESS); status=hipsolverDnCreateGesvdjInfo(&params); assert(status==CUSOLVER_STATUS_SUCCESS); hipError_t stat1=hipSuccess; hipError_t stat2=hipSuccess; hipError_t stat3=hipSuccess; hipError_t stat4=hipSuccess; hipError_t stat5=hipSuccess; hipError_t stat6=hipSuccess; stat1=hipMalloc((void**)&info,sizeof(int)); int* inf=(int*)malloc(sizeof(int)); stat2=hipMalloc((void**)&u,sizeof(hipComplex)*m*((m<n)?m:n)); stat3=hipMalloc((void**)&v,sizeof(hipComplex)*n*((m<n)?m:n)); stat4=hipMalloc((void**)&s,sizeof(float)*((m<n)?m:n)); stat5=hipMalloc((void**)&t,sizeof(hipComplex)*m*n); stat6=hipMemcpy(t,T,sizeof(hipComplex)*m*n,hipMemcpyHostToDevice); if( stat1!=hipSuccess|| stat2!=hipSuccess|| stat3!=hipSuccess|| stat4!=hipSuccess|| stat5!=hipSuccess|| stat6!=hipSuccess){ printf("cuda malloc error\n"); exit(-1); } if(hipsolverDnCgesvdj_bufferSize( handle, HIPSOLVER_EIG_MODE_VECTOR, echo, m, n, t, m, s, u, ldu, v, ldv, &lwork, params)!=CUSOLVER_STATUS_SUCCESS){ printf("hipsolverDnCgesvdj_bufferSize failed\n"); exit(-1); } if(hipDeviceSynchronize()!=hipSuccess){ printf("synchronize failed"); exit(-1); } stat1=hipMalloc((void**)&work,sizeof(hipComplex)*lwork); assert(stat1==hipSuccess); if(hipsolverDnCgesvdj( handle, HIPSOLVER_EIG_MODE_VECTOR, echo, m, n, t, lda, s, u, ldu, v, ldv, work, lwork, info, params)!=CUSOLVER_STATUS_SUCCESS){ printf("hipsolverDnCgesvdj err\n"); return; } if(hipDeviceSynchronize()!=hipSuccess){ printf("cuda synchronize err\n"); return; } stat1=hipMemcpy(U,u,sizeof(hipComplex)*m*((m<n)?m:n),hipMemcpyDeviceToHost); assert(stat1==hipSuccess); stat1=hipMemcpy(V,v,sizeof(hipComplex)*n*((m<n)?m:n),hipMemcpyDeviceToHost); assert(stat1==hipSuccess); stat1=hipMemcpy(S,s,sizeof(float)*((m<n)?m:n),hipMemcpyDeviceToHost); assert(stat1==hipSuccess); hipMemcpy(inf,info,sizeof(int),hipMemcpyDeviceToHost); printf("info %d\n",*inf); free(inf); stat1=hipFree(u); assert(stat1==hipSuccess); stat1=hipFree(v); assert(stat1==hipSuccess); stat1=hipFree(s); assert(stat1==hipSuccess); hipFree(info); hipFree(work); status=hipsolverDnDestroy(handle); assert(status==CUSOLVER_STATUS_SUCCESS); status=hipsolverDnDestroyGesvdjInfo(params); assert(status==CUSOLVER_STATUS_SUCCESS); printf("svd success\n"); } void svd_getv(int m,int n,hipComplex *A,hipComplex *VT,hipComplex* U,float* S){ hipsolverDnHandle_t cusolverH = NULL; hipblasHandle_t cublasH = NULL; hipblasStatus_t cublas_status = HIPBLAS_STATUS_SUCCESS; cusolverStatus_t cusolver_status = CUSOLVER_STATUS_SUCCESS; hipError_t cudaStat1 = hipSuccess; hipError_t cudaStat2 = hipSuccess; hipError_t cudaStat3 = hipSuccess; hipError_t cudaStat4 = hipSuccess; hipError_t cudaStat5 = hipSuccess; int lda=m,ldu=m,ldvt=n; hipComplex *d_A; hipComplex *d_U; float *d_S; hipComplex *d_VT; int *devInfo=NULL; hipComplex *d_work = NULL; float *r_work = NULL; int lwork =0; int info_gpu = 0; cusolver_status = hipsolverDnCreate(&cusolverH); assert(CUSOLVER_STATUS_SUCCESS == cusolver_status); cublas_status = hipblasCreate(&cublasH); assert(HIPBLAS_STATUS_SUCCESS == cublas_status); //copy A U S and VT to device cudaStat1 = hipMalloc((void**)&d_A, sizeof(hipComplex)*lda*n); cudaStat2 = hipMalloc((void**)&d_U, sizeof(hipComplex)*ldu*m); cudaStat3 = hipMalloc((void**)&d_S, sizeof(float)*n); cudaStat4 = hipMalloc((void**)&d_VT, sizeof(hipComplex)*ldvt*n); cudaStat5 = hipMalloc((void**)&devInfo, sizeof(int)); assert(hipSuccess == cudaStat1); assert(hipSuccess == cudaStat2); assert(hipSuccess == cudaStat3); assert(hipSuccess == cudaStat4); assert(hipSuccess == cudaStat5); cudaStat1 = hipMemcpy(d_A, A, sizeof(hipComplex)*lda*n, hipMemcpyHostToDevice); /* cudaStat2 = hipMemcpy(d_U, U, sizeof(hipComplex)*ldu*m, hipMemcpyHostToDevice); cudaStat3 = hipMemcpy(d_S, S, sizeof(float)*n, hipMemcpyHostToDevice); cudaStat4 = hipMemcpy(d_VT, VT, sizeof(hipComplex)*ldvt*n, hipMemcpyHostToDevice); assert(hipSuccess == cudaStat1); assert(hipSuccess == cudaStat2); assert(hipSuccess == cudaStat3); assert(hipSuccess == cudaStat4);*/ cusolver_status = hipsolverDnCgesvd_bufferSize( cusolverH, m, n, &lwork); assert(hipSuccess == cudaStat1); cudaStat1 = hipMalloc((void**)&d_work, sizeof(hipComplex)*lwork); cudaStat3 = hipMalloc((void**)&r_work, sizeof(float)*lwork); assert(hipSuccess == cudaStat1); assert(hipSuccess == cudaStat3); //compute SVD cusolver_status = hipsolverDnCgesvd( cusolverH, 'A', 'A', m, n, d_A, lda, d_S, d_U, ldu, d_VT, ldvt, d_work, lwork, r_work, devInfo ); cudaStat1 = hipDeviceSynchronize(); assert(CUSOLVER_STATUS_SUCCESS == cusolver_status); assert(hipSuccess == cudaStat1); //check if SVD is good or not cudaStat1 = hipMemcpy(&info_gpu, devInfo, sizeof(int), hipMemcpyDeviceToHost); assert(hipSuccess == cudaStat1); assert(0 == info_gpu); //copy U S and VT to host cudaStat1 = hipMemcpy(U, d_U, sizeof(hipComplex)*ldu*m, hipMemcpyDeviceToHost); cudaStat2 = hipMemcpy(S, d_S, sizeof(float)*n, hipMemcpyDeviceToHost); cudaStat3 = hipMemcpy(VT, d_VT, sizeof(hipComplex)*ldvt*n, hipMemcpyDeviceToHost); assert(hipSuccess == cudaStat1); assert(hipSuccess == cudaStat2); assert(hipSuccess == cudaStat3); if (d_A) hipFree(d_A); if (devInfo) hipFree(devInfo); if (d_work) hipFree(d_work); if (r_work) hipFree(r_work); if (cublasH) hipblasDestroy(cublasH); if (cusolverH) hipsolverDnDestroy(cusolverH); hipDeviceReset(); }
c5f748b5de867d9d46cfa3b525d08b4f47e3eef0.cu
#include"svd.h" #include<cusolverDn.h> void svd(int m,int n,cuComplex* T,cuComplex* U,cuComplex* V,float* S){ cusolverDnHandle_t handle; gesvdjInfo_t params=NULL; int* info=NULL; int echo=1; int lda=0; lda=m; int ldu=0; ldu=m; int ldv=0; ldv=n; int lwork=0; cuComplex* work=NULL; float* s=NULL; cuComplex* u=NULL; cuComplex* v=NULL; cuComplex* t=NULL; cusolverStatus_t status=CUSOLVER_STATUS_SUCCESS; status=cusolverDnCreate(&handle); assert(status==CUSOLVER_STATUS_SUCCESS); status=cusolverDnCreateGesvdjInfo(&params); assert(status==CUSOLVER_STATUS_SUCCESS); cudaError_t stat1=cudaSuccess; cudaError_t stat2=cudaSuccess; cudaError_t stat3=cudaSuccess; cudaError_t stat4=cudaSuccess; cudaError_t stat5=cudaSuccess; cudaError_t stat6=cudaSuccess; stat1=cudaMalloc((void**)&info,sizeof(int)); int* inf=(int*)malloc(sizeof(int)); stat2=cudaMalloc((void**)&u,sizeof(cuComplex)*m*((m<n)?m:n)); stat3=cudaMalloc((void**)&v,sizeof(cuComplex)*n*((m<n)?m:n)); stat4=cudaMalloc((void**)&s,sizeof(float)*((m<n)?m:n)); stat5=cudaMalloc((void**)&t,sizeof(cuComplex)*m*n); stat6=cudaMemcpy(t,T,sizeof(cuComplex)*m*n,cudaMemcpyHostToDevice); if( stat1!=cudaSuccess|| stat2!=cudaSuccess|| stat3!=cudaSuccess|| stat4!=cudaSuccess|| stat5!=cudaSuccess|| stat6!=cudaSuccess){ printf("cuda malloc error\n"); exit(-1); } if(cusolverDnCgesvdj_bufferSize( handle, CUSOLVER_EIG_MODE_VECTOR, echo, m, n, t, m, s, u, ldu, v, ldv, &lwork, params)!=CUSOLVER_STATUS_SUCCESS){ printf("cusolverDnCgesvdj_bufferSize failed\n"); exit(-1); } if(cudaDeviceSynchronize()!=cudaSuccess){ printf("synchronize failed"); exit(-1); } stat1=cudaMalloc((void**)&work,sizeof(cuComplex)*lwork); assert(stat1==cudaSuccess); if(cusolverDnCgesvdj( handle, CUSOLVER_EIG_MODE_VECTOR, echo, m, n, t, lda, s, u, ldu, v, ldv, work, lwork, info, params)!=CUSOLVER_STATUS_SUCCESS){ printf("cusolverDnCgesvdj err\n"); return; } if(cudaDeviceSynchronize()!=cudaSuccess){ printf("cuda synchronize err\n"); return; } stat1=cudaMemcpy(U,u,sizeof(cuComplex)*m*((m<n)?m:n),cudaMemcpyDeviceToHost); assert(stat1==cudaSuccess); stat1=cudaMemcpy(V,v,sizeof(cuComplex)*n*((m<n)?m:n),cudaMemcpyDeviceToHost); assert(stat1==cudaSuccess); stat1=cudaMemcpy(S,s,sizeof(float)*((m<n)?m:n),cudaMemcpyDeviceToHost); assert(stat1==cudaSuccess); cudaMemcpy(inf,info,sizeof(int),cudaMemcpyDeviceToHost); printf("info %d\n",*inf); free(inf); stat1=cudaFree(u); assert(stat1==cudaSuccess); stat1=cudaFree(v); assert(stat1==cudaSuccess); stat1=cudaFree(s); assert(stat1==cudaSuccess); cudaFree(info); cudaFree(work); status=cusolverDnDestroy(handle); assert(status==CUSOLVER_STATUS_SUCCESS); status=cusolverDnDestroyGesvdjInfo(params); assert(status==CUSOLVER_STATUS_SUCCESS); printf("svd success\n"); } void svd_getv(int m,int n,cuComplex *A,cuComplex *VT,cuComplex* U,float* S){ cusolverDnHandle_t cusolverH = NULL; cublasHandle_t cublasH = NULL; cublasStatus_t cublas_status = CUBLAS_STATUS_SUCCESS; cusolverStatus_t cusolver_status = CUSOLVER_STATUS_SUCCESS; cudaError_t cudaStat1 = cudaSuccess; cudaError_t cudaStat2 = cudaSuccess; cudaError_t cudaStat3 = cudaSuccess; cudaError_t cudaStat4 = cudaSuccess; cudaError_t cudaStat5 = cudaSuccess; int lda=m,ldu=m,ldvt=n; cuComplex *d_A; cuComplex *d_U; float *d_S; cuComplex *d_VT; int *devInfo=NULL; cuComplex *d_work = NULL; float *r_work = NULL; int lwork =0; int info_gpu = 0; cusolver_status = cusolverDnCreate(&cusolverH); assert(CUSOLVER_STATUS_SUCCESS == cusolver_status); cublas_status = cublasCreate(&cublasH); assert(CUBLAS_STATUS_SUCCESS == cublas_status); //copy A U S and VT to device cudaStat1 = cudaMalloc((void**)&d_A, sizeof(cuComplex)*lda*n); cudaStat2 = cudaMalloc((void**)&d_U, sizeof(cuComplex)*ldu*m); cudaStat3 = cudaMalloc((void**)&d_S, sizeof(float)*n); cudaStat4 = cudaMalloc((void**)&d_VT, sizeof(cuComplex)*ldvt*n); cudaStat5 = cudaMalloc((void**)&devInfo, sizeof(int)); assert(cudaSuccess == cudaStat1); assert(cudaSuccess == cudaStat2); assert(cudaSuccess == cudaStat3); assert(cudaSuccess == cudaStat4); assert(cudaSuccess == cudaStat5); cudaStat1 = cudaMemcpy(d_A, A, sizeof(cuComplex)*lda*n, cudaMemcpyHostToDevice); /* cudaStat2 = cudaMemcpy(d_U, U, sizeof(cuComplex)*ldu*m, cudaMemcpyHostToDevice); cudaStat3 = cudaMemcpy(d_S, S, sizeof(float)*n, cudaMemcpyHostToDevice); cudaStat4 = cudaMemcpy(d_VT, VT, sizeof(cuComplex)*ldvt*n, cudaMemcpyHostToDevice); assert(cudaSuccess == cudaStat1); assert(cudaSuccess == cudaStat2); assert(cudaSuccess == cudaStat3); assert(cudaSuccess == cudaStat4);*/ cusolver_status = cusolverDnCgesvd_bufferSize( cusolverH, m, n, &lwork); assert(cudaSuccess == cudaStat1); cudaStat1 = cudaMalloc((void**)&d_work, sizeof(cuComplex)*lwork); cudaStat3 = cudaMalloc((void**)&r_work, sizeof(float)*lwork); assert(cudaSuccess == cudaStat1); assert(cudaSuccess == cudaStat3); //compute SVD cusolver_status = cusolverDnCgesvd( cusolverH, 'A', 'A', m, n, d_A, lda, d_S, d_U, ldu, d_VT, ldvt, d_work, lwork, r_work, devInfo ); cudaStat1 = cudaDeviceSynchronize(); assert(CUSOLVER_STATUS_SUCCESS == cusolver_status); assert(cudaSuccess == cudaStat1); //check if SVD is good or not cudaStat1 = cudaMemcpy(&info_gpu, devInfo, sizeof(int), cudaMemcpyDeviceToHost); assert(cudaSuccess == cudaStat1); assert(0 == info_gpu); //copy U S and VT to host cudaStat1 = cudaMemcpy(U, d_U, sizeof(cuComplex)*ldu*m, cudaMemcpyDeviceToHost); cudaStat2 = cudaMemcpy(S, d_S, sizeof(float)*n, cudaMemcpyDeviceToHost); cudaStat3 = cudaMemcpy(VT, d_VT, sizeof(cuComplex)*ldvt*n, cudaMemcpyDeviceToHost); assert(cudaSuccess == cudaStat1); assert(cudaSuccess == cudaStat2); assert(cudaSuccess == cudaStat3); if (d_A) cudaFree(d_A); if (devInfo) cudaFree(devInfo); if (d_work) cudaFree(d_work); if (r_work) cudaFree(r_work); if (cublasH) cublasDestroy(cublasH); if (cusolverH) cusolverDnDestroy(cusolverH); cudaDeviceReset(); }
74702ac4b258ecd4d87a2e968b94c7d9f9577103.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" float h_A[]= { 0.714899237705168, 0.6593403492402452, 0.8200921920091004, 0.9453533202697284, 0.5522293587559314, 0.911488355650966, 0.6084328273759245, 0.6081693680408298, 0.9852630400453032, 0.9552435162269735, 0.7714556537320874, 0.893125822994568, 0.860835407553673, 0.6580368874559931, 0.8842885921552708, 0.7190619784527801, 0.7882722698016977, 0.6656379333000253, 0.9357561249147422, 0.8418344620101638, 0.7478591689725955, 0.8936638455948593, 0.8199023289899353, 0.6480275860499387, 0.6918710181332538, 0.957257686694877, 0.9896533928031612, 0.899464805150862, 0.933628059174556, 0.7802194673808598, 0.7180120788579141, 0.7001088917695673, 0.9846389829627131, 0.9335261679414126, 0.7891089669509961, 0.6920323513219662, 0.8372538068068409, 0.8262784216167918, 0.7594003365297706, 0.5817232948572205, 0.5308636056915322, 0.9717268845480704, 0.6168177198192328, 0.5261255208422979, 0.8354996870027067, 0.6313485414343811, 0.9396454597205113, 0.7709578667049983, 0.6280897526294885, 0.7903603343603801, 0.925954504579481, 0.7083630830802359, 0.8357339796535525, 0.534019588432888, 0.5119797738072018, 0.7728667479286768, 0.5281954764378672, 0.806846906243039, 0.9040260184287333, 0.6166848743159523, 0.5938194282251794, 0.8093009396743354, 0.8384950487844927, 0.9284209351465467, 0.8514262607452571, 0.8999592091442796, 0.8138785652259415, 0.7531482108463914, 0.9724259136764555, 0.9743884191877106, 0.8284500571666802, 0.7231979108297033, 0.9980085370102973, 0.7045189187157035, 0.8088190919993448, 0.5312498364247776, 0.8975216995204517, 0.8606825088359613, 0.869643791334979, 0.8921488183940844, 0.730567423848039, 0.5376339784064466, 0.5883775141302325, 0.5880074795492454, 0.5509298664515099, 0.5147798807327489, 0.5235937180516632, 0.6340500872623072, 0.8017164132600838, 0.8662938756188187, 0.5486619474749198, 0.6690091055092129, 0.626551384362175, 0.6938121021798289, 0.7561657803978581, 0.9725588029300659, 0.5134272225385352, 0.5024475806457671, 0.7327618801734921, 0.5481755412990239, 0.6730427146305296, 0.5869097722812583, 0.8751373530713528, 0.6884968207872837, 0.5295233343609733, 0.9092635863689065, 0.8569948983478097, 0.5748866839943527, 0.5301264666632447, 0.5330860289336155, 0.8221794288222217, 0.5911432769932973, 0.8945968988339126, 0.8888067368388528, 0.6483249846943383, 0.5334111450646923, 0.7708760786820803, 0.6673642269103457, 0.8692980696066652, 0.7234282380731747, 0.7878217535872505, 0.7684477288619345, 0.8781166045056072, 0.5409864805361351, 0.8710436103457933, 0.6049513096387487, 0.6692985137889889, 0.5866150606619355, 0.7064072480515011, 0.6174344251953949, 0.6030389504185693, 0.9955888433744646, 0.988088541152838, 0.6944172619618818, 0.8194627533999113, 0.678678424976046, 0.5769443485533892, 0.9605395043675642, 0.5243279077421619, 0.7969404186593336, 0.9826790121352584, 0.6734684994846307, 0.6915359726040123, 0.5599063898812213, 0.6310519258067021, 0.9544227292039527, 0.8161394483017943, 0.5119326555245434, 0.9155175004524563, 0.8795559567537004, 0.5247951787159624, 0.8881153608508414, 0.5833758509829722, 0.8316443186230482, 0.8113989227924175, 0.9244000317141048, 0.971306233799871, 0.922421602068382, 0.6815139490287734, 0.6702404325650719, 0.8998808360074478, 0.977975702477339, 0.5822350239302843, 0.8182573720323139, 0.552203577287874, 0.967072188226312, 0.5975915485116701, 0.5833555345238668, 0.6898032838835463, 0.6373851261192228, 0.9038056564126419, 0.9412314051502731, 0.7536901561836067, 0.7255196618410964, 0.6503097276041911, 0.9070690220972784, 0.9239482815877914, 0.9537711969022059, 0.9705096297935893, 0.5242138341317024, 0.7722253181124641, 0.9240838129722668, 0.5428206151729358, 0.5226365258208607, 0.9788553844760457, 0.9599167011294389, 0.6985366050744497, 0.9006775307634758, 0.7024064399731178, 0.6447591530613797, 0.9935124962772821, 0.5772732979308175, 0.7287128935286853, 0.6262950178391171, 0.5611406486932214, 0.8916997901780892, 0.8392771443256062, 0.5488533269123854, 0.7127002529099173, 0.9356370987977973, 0.6183737175811865, 0.7355702587931011, 0.5905247847392963, 0.9174645802674555, 0.9318144489330222, 0.8555117351291632, 0.7778563783139687, 0.5627287532732232, 0.7154658759167272, 0.7210336333762667, 0.829893316042666, 0.6907817904306046, 0.6054482014847642, 0.5108529961784989, 0.8849135061450457, 0.7830740983422007, 0.501019029088817, 0.7753900564249839, 0.6436817602797851, 0.5806123996160818, 0.8806068062082901, 0.7600965474257322, 0.5410228837313282, 0.9776069493730013, 0.5459742938064962, 0.7888928434340872, 0.8130793601278763, 0.6087725536384629, 0.6225128240226405, 0.5690274446605674, 0.8277102988016317, 0.5769291340186056, 0.7923307710897269, 0.7642161960151865, 0.9369003639291422, 0.9726811171798574, 0.5939810371503956, 0.809206226889726, 0.8790077654474049, 0.8280801100030807, 0.6592710417704546, 0.6307658958207889, 0.8205785690876383, 0.5470605695938913, 0.6346800029584398, 0.76365395465, 0.7735219009778354, 0.6575503924657982, 0.5899439753600312, 0.6259707599859079, 0.5373807945545741, 0.733226313004866, 0.5968375279635458, 0.9576227551070152, 0.5460084793065373, 0.7318161350184554, 0.8468201436796416, 0.532904438691469, 0.8978193631922068, 0.9413535540535227, 0.8738075664049618, 0.7233782483979383, 0.7639849426313691, 0.6544524590705578, 0.9955110638688266, 0.9736225235581263, 0.5772298983657422, 0.7054965414366082, 0.5243903070203558, 0.6548075381060549, 0.7788180879154296, 0.8750093825201364, 0.8269702207734297, 0.9235011877312231, 0.9229763075137061, 0.6237624121476577, 0.5607706532736418, 0.9909798308039458, 0.6909799098551241, 0.7263990659891462, 0.8980803954921195, 0.7158142164087887, 0.7196871862962309, 0.6001387982282831, 0.5884897876613996, 0.543629638658152, 0.5759723211632939, 0.7307176652842963, 0.6127889951549005, 0.7200889355665543, 0.884520185870739, 0.9024484847320571, 0.6908844075940646, 0.8989288011214942, 0.5592638945267838, 0.7412159771991746, 0.6373339574870374, 0.8968420503429578, 0.718054399322513, 0.9028092284240686, 0.9562782289356355, 0.6543610263094788, 0.6027857147699588, 0.8114401375992001, 0.8851495548678878, 0.6497652613438074, 0.5060997118118272, 0.8412658625963879, 0.8403133856877005, 0.8600068769682263, 0.9032229513983538, 0.9541919007733819, 0.6909085525910921, 0.6341321380378007, 0.5688976291010526, 0.8186433280578973, 0.7727738809620817, 0.6867237678369895, 0.7873490570434726, 0.5423021279749756, 0.8556063503832361, 0.8285718137056327, 0.5633738652013544, 0.679216260183704, 0.7156197319516586, 0.8789325826480099, 0.6253535009118849, 0.8007703751311753, 0.6046952411629385, 0.6683611825600259, 0.7330645040912231, 0.8715188373168332, 0.7409504877200004, 0.5452631776333947, 0.9762881605137783, 0.7805276151202554, 0.6379783392576039, 0.6664836641469523, 0.8834197345013293, 0.5037707311951245, 0.958120119115252, 0.5715436775365879, 0.7652424769249608, 0.8596961560884122, 0.8737891894094488, 0.765269146568656, 0.7015752280605909, 0.5371062863302415, 0.8635776837800367, 0.6645048189287229, 0.7125012956816683, 0.7796393953098166, 0.8854705863060834, 0.8951929174646185, 0.9653909502196992, 0.5923169214307802, 0.8319387232147146, 0.514282434393756, 0.7093383924316943, 0.5644075977510082, 0.9007494796339282, 0.837330400867986, 0.8731099133856018, 0.787336129822629, 0.805222386947527, 0.5469156350556642, 0.5063813677423787, 0.8127489540553137, 0.8445148521063401, 0.5587181361825462, 0.7935152965990917, 0.9099370330192247, 0.6685294280035562, 0.5325658993910349, 0.5682328719765312, 0.6565344334138221, 0.875891991693905, 0.8434818323219142, 0.8131837013762584, 0.957040505535927, 0.5525338150777055, 0.6707510797538001, 0.8161081114325226, 0.7904721719727048, 0.6065342222197316, 0.8926907984274886, 0.7454221202689533, 0.551696987170168, 0.702449812101543, 0.7262913157253231, 0.6902993315094016, 0.5948188987161848, 0.5371305178182884, 0.7061519379368606, 0.605212101259724, 0.7115243043985338, 0.8317604472154732, 0.8868120877945399, 0.6891562880683273, 0.6650398989424056, 0.5434370945999463, 0.8576784475156061, 0.8167072978783985, 0.6205046545781528, 0.509535139706685, 0.5207317539185301, 0.7047105406619496, 0.8734501759355237, 0.8720570004131034, 0.7717490705287743, 0.9436550978797907, 0.6363301883244781, 0.5733934335161759, 0.73562891556836, 0.791415464345583, 0.9542255175565082, 0.9940546721130015, 0.6277241852897406, 0.6015777977893709, 0.9399795451772848, 0.6782116652330491, 0.756403886735032, 0.859357450769002, 0.9228915187886892, 0.5240994350652488, 0.6065635208403455, 0.7848130303441482, 0.9171852030307641, 0.8372517283880556, 0.708514912461105, 0.6104357060356547, 0.5187071027257043, 0.5699203863024365, 0.7694991667229549, 0.6911137200990399, 0.5328375998963639, 0.7440280003973498, 0.8765018807983846, 0.9694893251727895, 0.9801356837198116, 0.5556311584142242, 0.5562671402413085, 0.9587691253954582, 0.9533140880295825, 0.8599154249907996, 0.891113291026211, 0.7007975525972432, 0.581037305287438, 0.7665215069007576, 0.5319217782622715, 0.8733949876893567, 0.7639977671754576, 0.6460226390857654, 0.8483497795114068, 0.9540304010031364, 0.8234342758162617, 0.7467893058034248, 0.9285455683371808, 0.6354960850498901, 0.5641110887937977, 0.9074485995130179, 0.9610537114644087, 0.7057832692714137, 0.7480798299941793, 0.7883978912494558, 0.8142421344059176, 0.6470122628936739, 0.8545119022834793, 0.8263256318998567, 0.8937010840643602, 0.6534751789705421, 0.8538826009990672, 0.8649203307780579, 0.7916708344214441, 0.7112655502722134, 0.7834104491842493, 0.8174932111663565, 0.5443012667665321, 0.5704987489722957, 0.562300002328861, 0.5727093255741714, 0.5264743165816661, 0.5681939334307726, 0.5890514638977031, 0.5478511128226372, 0.588484537454118, 0.8170449834131122, 0.5473490133405773, 0.5195769488205446, 0.5969588466893039, 0.6737300386527724, 0.5939499395266987, 0.5195431464231615, 0.5221974588155305, 0.5023152187484748, 0.5633983418211452, 0.8562291455603068, 0.9615220028453428, 0.6770915410678773, 0.74807601246659, 0.7345277540949653, 0.7262586947960892, 0.5287536691109124, 0.6284111831609029, 0.5165642568031459, 0.7592624345057368, 0.5897646758058119, 0.5118738438949171, 0.7692533580794759, 0.7268894457514261, 0.660279178520743, 0.7368153216256048, 0.6561940485543389, 0.961486700915714, 0.8544052467073004, 0.7062922066816391, 0.7379369012878284, 0.6195471938599522, 0.557123138446443, 0.939774629193501, 0.9248740688188111, 0.7805974139681242, 0.8203103312051463, 0.543107391251388, 0.7077911046205337, 0.7341558960876031, 0.6911913632341786, 0.9697654273470726, 0.5711365456293559, 0.5601831084086717, 0.9329817150218498, 0.6780957900252718, 0.6498983099187599, 0.7780138378245673, 0.7905590731360672, 0.9636347599207729, 0.9056962563478959, 0.9661958007857407, 0.87226526899735, 0.759138888566067, 0.6252901250784715, 0.739460715269842, 0.6239798826029042, 0.6000789350390112, 0.5109548603908938, 0.9942072599048892, 0.9081822701063889, 0.6923123816744274, 0.6646708931346561, 0.8162379521685574, 0.6380722150337172, 0.5402536592228362, 0.8104229198641574, 0.8343087274143453, 0.523424288606466, 0.840834850590032, 0.9892858561188753, 0.8752759493189619, 0.5349731267253144, 0.6195475549900122, 0.5863531763324414, 0.9723893551625384, 0.673143454585218, 0.8155358029154018, 0.7313237350635464, 0.9011691945442573, 0.7044788363627335, 0.5449437873103457, 0.7319459353512335, 0.8140903880580115, 0.9230198505857923, 0.8436510894389959, 0.7387585707544, 0.8111836155198344, 0.5642054342332932, 0.6314426774041031, 0.6200473089643925, 0.5568967373718732, 0.991361974741475, 0.5106011591595954, 0.8854292762217122, 0.530595836498597, 0.7648749697572337, 0.7775096723984414, 0.538250808402597, 0.7280276212111796, 0.5412664427602116, 0.6523585242131706, 0.8912183290802129, 0.93477350277957, 0.8032222348025597, 0.813829336281096, 0.7850414386623239, 0.6935285656162133, 0.692220524762279, 0.7051676293064348, 0.784187987218747, 0.9298892282935483, 0.974698890630403, 0.7589030582342469, 0.6332820677811835, 0.5798125228358056, 0.7834821774956127, 0.5824733641736499, 0.6550281033777312, 0.9919900196023808, 0.6789412432856097, 0.9219106317941272, 0.8798391382129259, 0.862603745948723, 0.7729761467393494, 0.6508402448099077, 0.7532891009760978, 0.9190128479175159, 0.7927144234522598, 0.9971939705620985, 0.6050598707256221, 0.8317531082861415, 0.9098724789911716, 0.5389072911949471, 0.9918197892117087, 0.9386103137044246, 0.7117883833745796, 0.5450774013244541, 0.7337293524824782, 0.6265777040844696, 0.8781068478718252, 0.8716937277115359, 0.5330652289927765, 0.909832921002632, 0.6495011256950489, 0.8566561978490865, 0.5525675468907973, 0.5668606939478587, 0.578998732846387, 0.8907421164824845, 0.8018467276858705, 0.9062221336179044, 0.6012489173395115, 0.5726381753560553, 0.8909258704822918, 0.830828997772137, 0.9348819842742015, 0.9488940158871031, 0.6030904469361291, 0.6891939822276341, 0.7598049046404163, 0.9566278257051317, 0.5724432427357613, 0.54445557843665, 0.5869346477763179, 0.703563913038736, 0.765422625655241, 0.7899135625518942, 0.9710139792958583, 0.8139838169190722, 0.5465981092096999, 0.764382878581779, 0.66943327241418, 0.7481682386909418, 0.6952402822921546, 0.9219502415152925, 0.9529747761499425, 0.6425030572238957, 0.66444885563488, 0.564837605073613, 0.5613855238963441, 0.6807413025073243, 0.5955944858575498, 0.9134393358886727, 0.5911961624810709, 0.693548262237623, 0.8367084534211953, 0.9411943413993216, 0.9186756239888745, 0.8650442190945391, 0.8096903019044159, 0.9607325671629684, 0.8942198042555074, 0.807980724626685, 0.6903194740630678, 0.6767510519602763, 0.807815540638659, 0.9078717616399886, 0.782910644242343, 0.9235349134709511, 0.5827662586169281, 0.9470239953491264, 0.8590842676132524, 0.5939794577015103, 0.9876294260130768, 0.5871791766326104, 0.8411338986016971, 0.5726129762095099, 0.5789282343090258, 0.5795202592716739, 0.5427223785377429, 0.848716796802117, 0.7044566072982174, 0.8368948416016131, 0.6392099290566813, 0.9889960018990578, 0.5695200896349948, 0.6235788508806733, 0.9657585705976398, 0.5664392054725076, 0.5255184677399329, 0.6888541450993906, 0.7720755972747267, 0.9724955128047557, 0.780176422235112, 0.7579203274133928, 0.5505057797746056, 0.9251359151826174, 0.8919755655618635, 0.6276698636196372, 0.9617549714725198, 0.7413785793021042, 0.8572009817696027, 0.5303609377672771, 0.7455581493809011, 0.682905979771071, 0.8864139741175463, 0.7675721171259957, 0.5134694086153817, 0.5104560611066875, 0.7534929727414377, 0.8811267814699522, 0.9549519511632123, 0.9125111870324931, 0.8402406819222059, 0.9686418706160229, 0.6721637457934657, 0.8900863348423029, 0.5809693611978983, 0.7706564243018375, 0.7535196657143783, 0.5655518417133625, 0.8632540477794817, 0.6237253907538303, 0.5235537906020364, 0.9079835428848342, 0.5161664290898977, 0.7827906088548443, 0.8457223981764126, 0.7561076309830225, 0.7200744157533459, 0.55264061667909, 0.798063132885956, 0.5307963059082288, 0.8048928349247049, 0.6178979807612097, 0.5685293417227364, 0.614024763742988, 0.9319050643931062, 0.6472319169525976, 0.549893894860735, 0.8334537922246017, 0.5301294859779133, 0.7036475590597746, 0.9381193195427198, 0.7033594020467008, 0.7185124231051246, 0.5087656899224133, 0.7515433439643251, 0.6335023934066711, 0.9502628604083856, 0.8383348007202307, 0.5501444946181654, 0.7159386398506163, 0.9296945028972425, 0.500483162623564, 0.5665402709425185, 0.6354245403594752, 0.7366110460178619, 0.8950806044581257, 0.6841529995003168, 0.798177034885608, 0.502837044774656, 0.5671804848769749, 0.5358571931904212, 0.6772239244428808, 0.868503602696236, 0.5230202083443396, 0.6598911952145408, 0.8624209028876779, 0.5766540627820045, 0.8097515142206597, 0.7822304611890237, 0.7692478750370071, 0.9473958742825763, 0.9584906659810823, 0.8495720619881241, 0.5914480554617187, 0.7568183262472455, 0.582183441975346, 0.6148975449624521, 0.5141616415768713, 0.7203114647529774, 0.5081514661893591, 0.8570889855115246, 0.8739931094696456, 0.5501752338486747, 0.9495004804805334, 0.7810733713034409, 0.6427883486470105, 0.8109524715383036, 0.6744951578424425, 0.9099481299748415, 0.5657323986245227, 0.8472508171022414, 0.6813595457804296, 0.8928115156495428, 0.8448648731692978, 0.7828337778894238, 0.9935621965112946, 0.6408110758702255, 0.7520084803803828, 0.644906930628748, 0.6023292539503734, 0.9222396281707188, 0.7718165123241905, 0.7013252689420441, 0.5941630112345334, 0.6957966073899595, 0.663304159675309, 0.7184497707585031, 0.9846575025305783, 0.9190990017206796, 0.8523543925425037, 0.7890317150842945, 0.8694198137231677, 0.63802512359019, 0.7225992566676784, 0.8242260866115121, 0.7938159984291284, 0.7117550091989064, 0.6608500609186267, 0.9860693720637639, 0.7325119634293508, 0.5696608005770114, 0.8604168361181119, 0.8556759568825537, 0.8479435160812658, 0.9896704019898664, 0.9841054791164934, 0.6391377994071699, 0.7858689686512776, 0.7204037565239714, 0.9958312374347824, 0.8852621340542617, 0.856883440148613, 0.6717923352678983, 0.5834614132464107, 0.7329109727195666, 0.8274537398464987, 0.6358844853028955, 0.5365783081388151, 0.8808955299964034, 0.9692693154709209, 0.943340140000064, 0.903785170180891, 0.5091186025335808, 0.9995488680986733, 0.7326760443444389, 0.6738152273744389, 0.5813275434490868, 0.8503018909001137, 0.5818015738209762, 0.6419578390385787, 0.8601416126932446, 0.9125592332170969, 0.9710800993580571, 0.963518881509527, 0.8099505089885708, 0.7079732766594808, 0.7580140312570196, 0.8229125339508798, 0.9935080354380305, 0.5316645912203887, 0.7364563786531991, 0.6948311352333294, 0.8115931563991554, 0.7169197721341644, 0.648658248093623, 0.8881480776717243, 0.8136200877355975, 0.8417608044057672, 0.6889934629160765, 0.8803212583260943, 0.9949030265274985, 0.7710474552277471, 0.6190169203857003, 0.6662557927603043, 0.7417858635295068, 0.6095648336885604, 0.8678463207101997, 0.6419893888079617, 0.6905344882021733, 0.9503518675280267, 0.8719467726767451, 0.8293262797257053, 0.8050915383501369, 0.6317715548525393, 0.5368675894542442, 0.514869111192901, 0.8459887105925784, 0.9085545158262233, 0.9344696380855234, 0.5641573773207562, 0.992083601027581, 0.5009808830822562, 0.8155845460965072, 0.5623545146251705, 0.7534304839758319, 0.6449293148723123, 0.9033773631967212, 0.9233719295038589, 0.9722245887680585, 0.9053553288579095, 0.5180050259293034, 0.5243396083312116, 0.8373455819595571, 0.9766203116656029, 0.7707451726107644, 0.7612857119103602, 0.5295396938042125, 0.6065449178331757, 0.5434819670507746, 0.5064813685900201, 0.5095551673723235, 0.8542107122806689, 0.5468194901986756, 0.693421100319, 0.6589585544053436, 0.5207416983374547, 0.9101756411911455, 0.9985937277017491, 0.7542832173759411, 0.9168668968117992, 0.8800996584039971, 0.9509279006760335, 0.8384263417753841, 0.9792537626994404, 0.7204342789272195, 0.8123164392834459, 0.5935647423695357, 0.8640389314644732, 0.8604348018605885, 0.9843255565643754, 0.5418681933225657, 0.9128127832684534, 0.8386906644148151, 0.622569676016947, 0.6866383474631415, 0.7956549706673264, 0.8028055988535624, 0.5792058358310403, 0.5238470473003998, 0.6705974426602423, 0.9183361303494078, 0.9273858354645896, 0.814532814899667, 0.7043712728744094, 0.736043961261097, 0.9281961695762453, 0.9671066754591742, 0.8971867030592273, 0.9173427396161358, 0.6911738119575463, 0.9557114065595523, 0.959550268588818, 0.6092420394088642, 0.9181791424116117, 0.6963463190121704, 0.7608386323626333, 0.5438589523701818, 0.7774548101880385, 0.9358002968117705, 0.6415612339733465, 0.6288123992555512, 0.9850116354988165, 0.7983252202635746, 0.7788506729759543, 0.7892876526053931, 0.6410306156126776, 0.9888888991487497, 0.5687850402815366, 0.9340789379279841, 0.570482324642928, 0.6237570455946669, 0.9408174328244927, 0.7603764259604455, 0.8363761846284126, 0.5638257367037884, 0.5990168554691719, 0.6910825224590166, 0.8966386715906715, 0.5296444671815281, 0.9835969306041905, 0.8026963059044673, 0.9965008472598088, 0.5280685388596, 0.9958978376504297, 0.6120676991252856, 0.9925640405026861, 0.5743030840877522, 0.5787614774796664, 0.7839437845127978, 0.6577305713639505, 0.8759129899379714, 0.8442670700452837, 0.9977143584609288, 0.5630843774352265, 0.8824618172972648, 0.7353117677158472, 0.5549342971016928, 0.8625127818377221, 0.6287908447188745, 0.8603241101590813, 0.8019860751931377, 0.9519817327215931, 0.929150234953081, 0.5233695910619802, 0.6092652323361047, 0.6268761674186525, 0.6013146909835099, 0.5343859354849183, 0.550035128040716, 0.5186477881473128, 0.8447116193226469, 0.6744573406745742, 0.8341525026583503, 0.6002990275827004, 0.8159316160174189, 0.5047789342121152, 0.9582888281251125, 0.889215943020931, 0.5092441443413397, 0.522753284148985, 0.8585905597087496, 0.9851500099620788, 0.6780260121755493, 0.5347311611180499, 0.7864154673693363, 0.6908097478716986, 0.6260923409143679, 0.7439043954621911, 0.5418376147557895, 0.8495002019989841, 0.7211542724202722, 0.720067855664281, 0.711392080678194, 0.5729288454367311, 0.9314208398493533, 0.642001703877979, 0.5996095617981001, 0.5546545226700801, 0.9181718216259855, 0.7388686621607143, 0.9985736608089562, 0.938139825226662, 0.9278300791764933, 0.9302073444311413, 0.6242953718185338, 0.8493128752089243, 0.9731123703482502, 0.6703890534103323, 0.681432428423985, 0.8269486520062607, 0.7781590666878319, 0.6892780696821976, 0.8996840406353698, 0.6711511718388123, 0.8962841844109882, 0.6337230333548112, 0.9162195762723906, 0.9896044983779647, 0.7773066409266807, 0.6704520185595724, 0.7249714368703324, 0.9194587932164057, 0.5023898378154985, 0.6817130584848259, 0.6445306337899981, 0.6223165144277016, 0.8963534716863251, 0.8120159764275696, 0.8089046140403061, 0.8119470242556391, 0.885104455521087, 0.664963571463228, 0.5137450324664875, 0.5409572760196888, 0.779186547352593, 0.6762678670182992, 0.6686918499459775, 0.5114896437127547, 0.943799645391411, 0.5517218527393111, 0.776565186013888, 0.7225434352247616, 0.9610915747088796, 0.6151661475115529, 0.6566178828585421, 0.6636161615603928, 0.6616715075411022, 0.7673040771537916, 0.6251741948916316, 0.6353733585567626, 0.628527323895766, 0.8882156113604631, 0.6890686350094752, 0.6351766863817383, 0.6504643458096129, 0.9768584842858181, 0.7747129320005545, 0.5606099869717298, 0.8314439512235265, 0.6806617676660154, 0.7293552489596655, 0.8839844966137886, 0.7676463915674501, 0.7008335159857427, 0.5104238327691768, 0.8907526891071789, 0.5643686777342827, 0.8490682497162244, 0.9532995317734415, 0.5616124473583757, 0.808186986345413, 0.5347020017714006, 0.6786425551486783, 0.6195953722597805, 0.9479173187959913, 0.5809844502623445, 0.7061942711615923, 0.6576957502509282, 0.9577727879364519, 0.609228753606329, 0.9524956649537517, 0.9693179426086884, 0.5407479062226326, 0.829911760006663, 0.7506590403484825, 0.5963550129977244, 0.7098068835790232, 0.8689724847350547, 0.8534287549858192, 0.6165148755921164, 0.6401174262569596, 0.6957297999880339, 0.8846311647286043, 0.6030721755322559, 0.923238727000741, 0.7797086248141958, 0.8250850096700526, 0.8712542455502057, 0.8499084837356693, 0.5596005251095597, 0.5129036158294911, 0.805512391043429, 0.5544282363239954, 0.7293694447134789, 0.5335436495748754, 0.9642443631737312, 0.7538963368894713, 0.852047287083169, 0.687029104604705, 0.6177032453578496, 0.7931769735532612, 0.5782279068650884, 0.9010304986235913, 0.943508606858491, 0.5414785481671529, 0.9871336449843631, 0.8462238063349006, 0.7685017910763983, 0.8063302826347762, 0.7061826253298751, 0.7504277167339306, 0.817124032162607, 0.8416104179494577, 0.7489064691716957, 0.8337231973657941, 0.9161079897919806, 0.9014897266348425, 0.5912596516017559, 0.7809145446490497, 0.6131195035555357, 0.9685944279325427, 0.7211009123678227, 0.8836902484094888, 0.6940018560059542, 0.5867401299547919, 0.7904873610250666, 0.6867066867668838, 0.8815015498170606, 0.5014305250815501, 0.6389132289693962, 0.9546271395197699, 0.5191573498937255, 0.9715317094246325, 0.8504999191564628, 0.5489283507570941, 0.584729944188646, 0.9650568704745224, 0.9052786689654229, 0.8180460027485775, 0.7357220576684493, 0.8370012805201876, 0.6681277208325579, 0.9505437469774821, 0.6913354968959724, 0.5200083252548511, 0.9557017047892293, 0.7377705023371827, 0.9289826453581649, 0.8659492932730093, 0.6584336346862094, 0.9274016881525187, 0.6094148898044727, 0.7946455865244986, 0.8582962033373591, 0.7442612705729019, 0.7696381136846151, 0.7270895573123024, 0.5876391344045486, 0.9188503133546491, 0.6345438565824195, 0.5768528766085457, 0.9244329583578382, 0.9164677181201757, 0.8198664873252317, 0.7711903431628182, 0.6917565422472931, 0.7021516791659326, 0.8862179867996558, 0.7926089741012183, 0.628801874035622, 0.897529734023969, 0.7631220994515182, 0.5752065651424352, 0.8019544010813758, 0.7498519435719622, 0.878677807116468, 0.9099686412136768, 0.6495254449827226, 0.8317706057459928, 0.8467642374092617, 0.5124050582066125, 0.5186282639666125, 0.5162247890269265, 0.9088736167926997, 0.762797116514896, 0.8427167178642287, 0.5160962317016575, 0.6112543258605174, 0.9628410927240112, 0.597509847122122, 0.5920668830105134, 0.5999893775097818, 0.8031463253022338, 0.7489008915946778, 0.9576366952570203, 0.7330627846965758, 0.8213955799460855, 0.6095091088337574, 0.5168302150084956, 0.5820597976355589, 0.630051078110865, 0.7208606611397412, 0.9106371674557223, 0.8583519382477898, 0.8116920375659458, 0.9374527801414856, 0.956083353135863, 0.72998120185333, 0.6489496518442797, 0.7701633664691347, 0.7437282258952518, 0.8684160240872199, 0.6782327425657124, 0.5609015265329469, 0.7840570725191227, 0.7401726673550745, 0.9939679402406059, 0.8428050597653057, 0.6285391288449356, 0.9525523550249577, 0.7245200142579794, 0.8427555078558537, 0.9292276507484976, 0.806770733806882, 0.6675726232899162, 0.6369665345614437, 0.5109058634108643, 0.6447965531257694, 0.5396864467457, 0.5244341748339532, 0.69697885960669, 0.8015139580253169, 0.6625834751865, 0.5296664619535063, 0.7480719310015472, 0.7583603357347073, 0.8902408106172301, 0.6264941805609019, 0.8750471258719341, 0.9860714471043267, 0.6622214606383849, 0.74077104617175, 0.9392160341422497, 0.52123905659883, 0.799803777207009, 0.7542227224634717, 0.7015991923341619, 0.6745916455846831, 0.6090065971247887, 0.643455107728671, 0.8238403526156649, 0.5729818044183512, 0.8667251293410434, 0.8064405388888514, 0.8672955544535296, 0.929190801975835, 0.9577870147515954, 0.5966201911276999, 0.7113779860165379, 0.7893430203092717, 0.6487794671215054, 0.7264422218586388, 0.7705471058365967, 0.6042753828117917, 0.5488067488308401, 0.8787982192929122, 0.9313015390736115, 0.720808400627172, 0.9717199183209253, 0.688212024819574, 0.5516452935407065, 0.938095637560733, 0.9992472993034112, 0.6874819380931972, 0.7028171548838387, 0.9943468859867792, 0.9259098723793173, 0.5834754263238795, 0.5941462984060523, 0.8914090414830754, 0.6003124424414688, 0.9923591274464963, 0.6533175249704222, 0.7350077984127374, 0.7379595794015494, 0.575755852353445, 0.7897328533514314, 0.6151332713320594, 0.9272165155202021, 0.7991995960756262, 0.9716674947822159, 0.5546038296844353, 0.792908613673254, 0.935710263660041, 0.7349848374022494, 0.5596725039122803, 0.9361611827547351, 0.6634999735065663, 0.7861468418237738, 0.7888882042940063, 0.8430562618096415, 0.9036731783931962, 0.6472213041678718, 0.5244572172299351, 0.5920373125046093, 0.5073957476323756, 0.5280367089418969, 0.9961337428785373, 0.9536468839520664, 0.6636269798552731, 0.8365366461939714, 0.7868881194147763, 0.8593804582581533, 0.8072796801492867, 0.8278788695197186, 0.8192336213892765, 0.5876548625470283, 0.5742056815307378, 0.8660512454503853, 0.9754952712545373, 0.5188648803554188, 0.9110024614941881, 0.916456465018583, 0.8094395118376327, 0.7144298239964624, 0.8725437640354723, 0.7710582072881818, 0.6999662412155296, 0.5855077245976706, 0.7602793160811296, 0.9693940357590372, 0.9973307305499417, 0.8864938929639585, 0.6156993999010254, 0.6265524355891801, 0.7309322563268539, 0.6348231023408648, 0.9723856632835176, 0.6854418491295867, 0.9911315982800987, 0.6119848536492785, 0.5682034441053972, 0.5625732595378967, 0.8805467671398628, 0.8396580020783695, 0.5468118580641375, 0.7675240976154468, 0.5867008348509065, 0.5272339365430552, 0.9170970867119437, 0.7225833240488155, 0.9330899948606803, 0.5135873433399177, 0.5939122396553949, 0.6906957476095247, 0.9607317807488538, 0.6919965510327051, 0.769545510549152, 0.7286416336207603, 0.7726159681456106, 0.8573848332057681, 0.7936834193228975, 0.8937796822230413, 0.963401924514188, 0.8120226735569775, 0.9560630617163759, 0.6654914154380734, 0.9002419693951857, 0.6244109068604935, 0.7082182411360969, 0.5916262926112537, 0.7217034518612253, 0.9454356491933378, 0.725640878965292, 0.8577928864712285, 0.6684209091711908, 0.8130957845675291, 0.8512776362772251, 0.8828078866629909, 0.9879298679279742, 0.8259812818360481, 0.6669431024388734, 0.7241581854512833, 0.7629956383688419, 0.6738888410012275, 0.7969342003028033, 0.845608748341045, 0.9257573045070762, 0.8158095302513027, 0.8274920225204306, 0.6171749213338311, 0.5380250079784248, 0.5651703266744204, 0.5956938686163428, 0.7716476787179425, 0.5393274807297179, 0.6904593591498682, 0.7293030362607404, 0.5388881249837228, 0.9163724608366519, 0.8949668271344372, 0.6347818418808666, 0.9937358556584308, 0.6325918236146499, 0.6260613572803713, 0.6975344573257027, 0.748857158919509, 0.9962503917560738, 0.8488907191438116, 0.9229532253096726, 0.539779848226567, 0.511252249289505, 0.5904138628804119, 0.7214165786443512, 0.5910124723110795, 0.6660936865126847, 0.5448169456244305, 0.9446774020638611, 0.9676012860751123, 0.6429014773253252, 0.8497101542057285, 0.9935408900407224, 0.6062008922111344, 0.7751845422903718, 0.9283645345462738, 0.8185597929361603, 0.5677214927454797, 0.595606820034031, 0.9924306478217967, 0.88344471358283, 0.7370333303593328, 0.6938448657440432, 0.8944284618243459, 0.6563129764014879, 0.9132957964920958, 0.6715824332919551, 0.7131609632199478, 0.5675186165542083, 0.6993878090153459, 0.913103962334672, 0.8407653253003909, 0.69912646732845, 0.7770139705164247, 0.9552839157835878, 0.8691885467803161, 0.6293413920923085, 0.8388835072633141, 0.5998594464991525, 0.968134564846439, 0.8951548267731255, 0.8252174201121384, 0.6160138261439643, 0.7773566309062205, 0.6916507573864236, 0.5807535339925275, 0.9258010798652971, 0.7656931994811436, 0.5658469372350063, 0.9737903878229381, 0.668481586638406, 0.8397331840170059, 0.7666673533290878, 0.7887155294125846, 0.9018985231749859, 0.7251929644112718, 0.8844965089821621, 0.9677352083136705, 0.8710441261396651, 0.9592715738667945, 0.5645512000219833, 0.9559603690274174, 0.9271176725368131, 0.7165294968376588, 0.8282268778104105, 0.9510795559565053, 0.9280273696488505, 0.5240366536440538, 0.9312836512556888, 0.6676942944227335, 0.8551110159945744, 0.7448348582834579, 0.5627373997430986, 0.8038616595583132, 0.6831127679353685, 0.518689376266759, 0.7901056764893017, 0.5419234593002746, 0.5833245401875009, 0.8628305046701199, 0.8950565446225467, 0.5646754445481028, 0.690547275065224, 0.6979013844372711, 0.5184591468427029, 0.9079318892014414, 0.7514291772834929, 0.7335120297246609, 0.9951521308907829, 0.8719688428797499, 0.827071226089379, 0.7314254549385253, 0.7989991039558348, 0.5736868021273909, 0.5491986801214117, 0.6951736878194997, 0.7522730143223768, 0.634091807681439, 0.5210741463665289, 0.6762470284625738, 0.6314464641096101, 0.624268118223448, 0.7431250889619572, 0.7835422500190575, 0.7720536900523256, 0.9197980736324693, 0.9162472884597045, 0.710764055830452, 0.5964460739888114, 0.8597451236010295, 0.6485790544970058, 0.909987786106981, 0.5195793031016005, 0.6117422473988958, 0.5598955408832629, 0.7143063412222272, 0.5491761579054409, 0.6592463719554792, 0.6523369223179598, 0.5644039126230951, 0.5439254682853882, 0.6533854347394867, 0.8950114173398549, 0.9859915273208884, 0.9862603576061675, 0.7330059525563113, 0.5271303938884919, 0.7650068385973852, 0.7636638174446216, 0.8680512598160818, 0.6816302939950526, 0.8681961391731801, 0.6245189862363439, 0.6382248408832374, 0.6274854214410394, 0.78615844499789, 0.780574810551214, 0.605991182970578, 0.5105979840959562, 0.8647857587211185, 0.8152559692858743, 0.5441492722302703, 0.7762142325947545, 0.9203133024412911, 0.8378921348235446, 0.7466556345359071, 0.8387258673109992, 0.8568752996853403, 0.6842565684588178, 0.5010589179774196, 0.6634973600175997, 0.8909461889099035, 0.5366930437973239, 0.7618991050109198, 0.8174327957797372, 0.5777383748275439, 0.7094991869184164, 0.9161757219595784, 0.5972083815411466, 0.7151215173266364, 0.7994611119894826, 0.8661468048663687, 0.7921168535260954, 0.5619309355069639, 0.7276565536786832, 0.8027146078944928, 0.781992636316762, 0.7731199871646506, 0.9870157543487068, 0.8046192780732702, 0.6331394081004307, 0.792922232254657, 0.7697219337384742, 0.6772491532308755, 0.6063599009204489, 0.7609239121321716, 0.8094969240876992, 0.5390215336686695, 0.7171950888997553, 0.7102948440067917, 0.8544718279458058, 0.9804506285550212, 0.6698904118689688, 0.6022714707530368, 0.8635078379555414, 0.6241611902190143, 0.6919556734996597, 0.8491585269314992, 0.5865279184800638, 0.5584486087942411, 0.6512344649706066, 0.7297302995938235, 0.6534304755627002, 0.7797223889336591, 0.7634034450186772, 0.8513610822686428, 0.7297851016008245, 0.7025524667942226, 0.599181650395693, 0.6940331802178015, 0.8396675413858863, 0.7883884923200379, 0.8216871485067592, 0.6413258037109926, 0.7561548101705087, 0.6718339885069906, 0.9373217011787645, 0.6808350944882655, 0.8100742998634867, 0.6399486342874809, 0.583894631927244, 0.5491353628200171, 0.5645172429813654, 0.7651287805791276, 0.599873169242636, 0.7793522940548414, 0.599227328589752, 0.8363575545129591, 0.9099783695789619, 0.7275689653749082, 0.5035551349228342, 0.9188533975872, 0.8121769156113592, 0.5807966889476599, 0.8698657600446373, 0.8285221190982968, 0.5167710114517965, 0.5722406442775426, 0.7978612047943068, 0.7238620944038044, 0.6878837342263064, 0.6657158424168258, 0.9752079055888137, 0.6815722122679624, 0.8792810197304497, 0.812590227950509, 0.5343778876577465, 0.576072119955493, 0.8199214985156, 0.710135493272805, 0.5944500182827686, 0.9661489836577549, 0.8306285831984093, 0.6408892685572585, 0.7806776581804702, 0.5022406931343896, 0.5867553902553148, 0.9162617679355611, 0.9054606334547478, 0.8480994114910061, 0.6970790797767902, 0.5362439368907911, 0.8151813898936953, 0.7208735837919895, 0.965688973302806, 0.7066157954362717, 0.6217488671327662, 0.7052368289008206, 0.9475683950886093, 0.696150642070306, 0.6471552545772581, 0.979271893829913, 0.8004788743363986, 0.9561731785882099, 0.7627981044899421, 0.525527729127792, 0.8347980109562567, 0.9534845210388199, 0.6328148620025589, 0.8586345406606539, 0.9741790957973862, 0.8703705688273324, 0.7486300937175524, 0.8488304503748101, 0.882694291208169, 0.8013309391463747, 0.536014600121361, 0.7933767251708894, 0.8217323265829959, 0.8917495680719559, 0.8890198518173259, 0.5544551545154196, 0.6662193638078293, 0.7174125042075009, 0.7509755197603167, 0.9469480646316966, 0.5483721394554564, 0.6450425612716715, 0.8656043422024355, 0.9911092095731524, 0.8292057560877847, 0.8046713431308617, 0.8734708011996093, 0.8821888892105814, 0.5591682329189653, 0.800103009134664, 0.9824063752245151, 0.6984982846654548, 0.6210390335416234, 0.6005450303119111, 0.6393416192242121, 0.805819212843185, 0.8561172842015047, 0.8319970492474826, 0.9142957382616751, 0.8890855947038026, 0.6844006914822596, 0.5277545405650137, 0.5828764149464394, 0.7501584886038796, 0.697685180979452, 0.6475494378005215, 0.7039498860127644, 0.5100601747671356, 0.9463853153099666, 0.9980262451186144, 0.5841671521385732, 0.5824926165292171, 0.6866209932292969, 0.9823483056291338, 0.6667654175423576, 0.621980746034845, 0.9308541052336489, 0.7786547955097396, 0.6959226398135991, 0.6009460925277543, 0.827822422688217, 0.5844957613935857, 0.5445535496653859, 0.892250777390059, 0.7379171019731167, 0.672077928925549, 0.585902552786487, 0.5727757334038889, 0.9225604906491849, 0.899739241297127, 0.9012872283308504, 0.9139765023306405, 0.5038156166150285, 0.8881666679321782, 0.9126412699731634, 0.6588666404464645, 0.8469337466857467, 0.6819149423369797, 0.7933798097891029, 0.738740318526969, 0.8591372549355019, 0.9153760116160707, 0.8069490867632234, 0.8669894156959632, 0.9848904401012876, 0.958384733735133, 0.8475479758198083, 0.5873024134506228, 0.7498062005713406, 0.7629134105553143, 0.883038241925704, 0.5379952744607848, 0.5746128535446261, 0.6815854606121777, 0.9718081814278505, 0.5413317160948099, 0.8890661576030743, 0.5416714721577306, 0.9342827673368999, 0.6652018849769676, 0.7000785269601936, 0.7968261362664956, 0.9013034701887248, 0.8867064950721346, 0.8245795262404698, 0.8181170660935687, 0.8992156277816795, 0.6068532013169106, 0.8654253419311326, 0.7263403064576557, 0.9261714168778701, 0.9354698036133385, 0.686120933646905, 0.5410127489263319, 0.8013971732248947, 0.5113269701351297, 0.5036765402981079, 0.69158930658379, 0.5646962566455807, 0.734544031087879, 0.983982443244964, 0.9687735514862008, 0.7189298866677871, 0.9146733949144694, 0.7598272680038082, 0.7352929287871568, 0.8659886369486876, 0.7655569234245532, 0.9264135167868114, 0.669261743553765, 0.8789837421036524, 0.9107632654885331, 0.7412533636843797, 0.59161173342153, 0.6433152116150997, 0.8236058635859249, 0.6127666218137064, 0.6113564925151926, 0.8885098251085701, 0.6462442790416637, 0.5586198962238589, 0.9146460246062129, 0.8230612255798349, 0.7859020293935984, 0.524158930477432, 0.8834447772214882, 0.6776328686367403, 0.6438217000084501, 0.6372556400831897, 0.5063916525353342, 0.5218079863721143, 0.684314185281739, 0.8495854896209114, 0.7248363806723359, 0.9551268404370494, 0.6277288493469356, 0.7108246431032929, 0.741444276323528, 0.6963162263415921, 0.765080349437703, 0.8532156407484648, 0.6490237028397381, 0.589732217157203, 0.5890536655432388, 0.6923185261962007, 0.6049675032025355, 0.9912989307909565, 0.9630326581840074, 0.8537260202581473, 0.7256419090332591, 0.8426710836077372, 0.7379853307185479, 0.9457681789150698, 0.7868882785559809, 0.5515442811027342, 0.5041598503130009, 0.5636070426560051, 0.7872064641131906, 0.9941983995638923, 0.7832937533438307, 0.8547809575565962, 0.8916517892111142, 0.7994855974688095, 0.7694804318674131, 0.7472978264793337, 0.7399587569443988, 0.9807496193576837, 0.557441324762673, 0.9883988774474683, 0.5720895412284299, 0.5287639871916541, 0.8742894188222776, 0.6393191440175279, 0.9150961063210259, 0.8786276739925787, 0.524747526014729, 0.9509211611429347, 0.8435754875545265, 0.830702124405222, 0.9752423384995711, 0.6337106865090429, 0.7851354447833482, 0.5378513301783441, 0.6406872825277243, 0.701504909373259, 0.5228107025518314, 0.9873122003965533, 0.747169744522536, 0.5218591463250417, 0.8837166306110202, 0.98068840855556, 0.780367018430127, 0.735294690214955, 0.6020030504710734, 0.7363824900457989, 0.5580448658148587, 0.9436350510792096, 0.6470262963760548, 0.5866496283310255, 0.5910663739423464, 0.9111078845262204, 0.6696957923926545, 0.696926111654643, 0.8195097740389266, 0.5775860058368936, 0.928827208361773, 0.8967795153158356, 0.5656443498470952, 0.918988076697139, 0.8783283078295515, 0.6295252825108626, 0.5564140719360701, 0.9471230598171835, 0.6849586240459107, 0.9203802826105298, 0.733680782740266, 0.5954511175063653, 0.9592349751654639, 0.9010096457436851, 0.7905261511540287, 0.6811085477201726, 0.9702399728038549, 0.6476800362193159, 0.9619374125253238, 0.9138961076550465, 0.5763522314919106, 0.8216997423352113, 0.852762475756635, 0.7852397612288513, 0.7467508018554389, 0.9104389912111968, 0.6344296416832325, 0.9226655541754629, 0.7210154082138017, 0.748661153886045, 0.9980212037231943, 0.713065301590434, 0.6297720842568433, 0.9511566407955294, 0.5733221804690487, 0.7014302476922936, 0.9393524375453988, 0.9203337424909167, 0.6867581193575709, 0.7528867049829062, 0.5689517879551561, 0.7176554997350377, 0.638033140704825, 0.7115833139332732, 0.6460929360875789, 0.6088108933552638, 0.7048994845991923, 0.6789072571710413, 0.5977635026269235, 0.9739893965874362, 0.8153549701137928, 0.8784119704857629, 0.7593543274581349, 0.6313162235572616, 0.6511023264455079, 0.7282276156852976, 0.8815558372005745, 0.8005134697172771, 0.5067303262009564, 0.6091119811872098, 0.8040154331066245, 0.5538746941493178, 0.7229848051471328, 0.5836850023274596, 0.8062474425244877, 0.7835763070942219, 0.5388816234239331, 0.679457724389811, 0.5177085150421354, 0.7992351318160109, 0.8229242431624626, 0.6908803722955692, 0.7186629875945816, 0.9268983875438999, 0.9531332947297668, 0.8938210476604475, 0.8846663159628229, 0.6825436635490417, 0.8442234946857172, 0.584359576095792, 0.8113389924442447, 0.6366909311395518, 0.8913562942004112, 0.9599647269422442, 0.5742050554835412, 0.6806866250599198, 0.8654236893185705, 0.9063158062472891, 0.5162403155221338, 0.927712182208625, 0.7938843952410307, 0.8875481346405394, 0.6178973330217754, 0.670922798541411, 0.7607924954393745, 0.5256851731375056, 0.9008661300516236, 0.8275958234596146, 0.7784366118579691, 0.6250417167080273, 0.8917096632718988, 0.6947292320856919, 0.7081489758808067, 0.7330143762027135, 0.7608212169277919, 0.5427262823177115, 0.8041027177507576, 0.769978367128541, 0.939838423278582, 0.8128140082973834, 0.7290631316824194, 0.6497558409141396, 0.9218306908963319, 0.9978425824689737, 0.7386217657692493, 0.6318648720932452, 0.5385334328026687, 0.7197792847812345, 0.9688677556160177, 0.5497000285590489, 0.7704709391355744, 0.822052043528751, 0.7954420093661044, 0.8405026747128113, 0.5351163985541214, 0.6690102459834013, 0.5345359365328872, 0.9182831614943414, 0.6845258717492946, 0.9904541975133518, 0.8461512636038855, 0.91548675256085, 0.7910404033743956, 0.8907474796644095, 0.9098836631994618, 0.5558472069151348, 0.9008354569073646, 0.9452125177793486, 0.9187960298241067, 0.8522936881717069, 0.7786860438827113, 0.5073534704503333, 0.8536968388995947, 0.7681878208857315, 0.8193044389189108, 0.8529926936022905, 0.6994638504463768, 0.5990544573329741, 0.5961757250631056, 0.8259396854930081, 0.881326043862166, 0.8299511193751532, 0.9551196920458729, 0.9226769799030088, 0.6665214228252053, 0.627349301886063, 0.7254811837968725, 0.5755597208291244, 0.554926804036437, 0.7395218773051495, 0.8522633922426657, 0.9558032000340895, 0.5898847350875367, 0.8292412508995635, 0.5435907908156128, 0.6478661993511781, 0.9489959869708298, 0.599882735864635, 0.8916595194832422, 0.6280878290653957, 0.6019058839819713, 0.9149893973229243, 0.6432209845470267, 0.9585413323982771, 0.8464904188763751, 0.5066667154517372, 0.8979258684350054, 0.9287696728491261, 0.565886699497347, 0.8644032607566445, 0.9485545120242145, 0.8556560520373797, 0.5248329546683002, 0.5762217171024484, 0.8259761127928213, 0.6488941185220416, 0.6372334758583061, 0.5547965343938261, 0.927640672113132, 0.7190692689019063, 0.9197098830327819, 0.5395225754101882, 0.6903886613159107, 0.5925180432367259, 0.9993491823138161, 0.7995875900373022, 0.5275843633581634, 0.6946983924815886, 0.9553410641242313, 0.5011284735817164, 0.9550209622796704, 0.9346498380843731, 0.5059807174719009, 0.8508585258916732, 0.8751417909198644, 0.7715239013652754, 0.5619418418004074, 0.5637029976508683, 0.5052752859591054, 0.6689067738809151, 0.6556311238671824, 0.7943011928223319, 0.5380219916642031, 0.9332454916280256, 0.7666524277270956, 0.9891861887488069, 0.8357686930605959, 0.6840063654849842, 0.5601908815813466, 0.7226595568746665, 0.8401891807158768, 0.7657890700890996, 0.6716663081090741, 0.8751746503160951, 0.8456085100727943, 0.6854966228123682, 0.707280819090531, 0.871014530893987, 0.7361309387018821, 0.8741370783871225, 0.9041871488407436, 0.5830980634489187, 0.7685920838283593, 0.6966438328169506, 0.8517129917489175, 0.8974518767301445, 0.7827143966899989, 0.7812481186708073, 0.5555260042685757, 0.6953617976239921, 0.8689727506245951, 0.9855873456435942, 0.6554254444932807, 0.798974880545683, 0.7588316447630388, 0.8176525484580602, 0.8726925490593653, 0.5262852847764714, 0.6695980583225866, 0.8082154587061458, 0.7554297030355437, 0.604949654338603, 0.8760550215513803, 0.5111815309709001, 0.919430966363503, 0.8616328975250706, 0.8667505915666758, 0.8002990180499006, 0.6583156180211166, 0.9322373932646413, 0.6436662147709803, 0.9253065381216303, 0.698513003275815, 0.9156344240880248, 0.7721024971501115, 0.5411224947938844, 0.5627213370773958, 0.9887625190769178, 0.6559904821349188, 0.7422016772424557, 0.7512806616379337, 0.9313646230850063, 0.8746960484159758, 0.5958383186232634, 0.581215143414767, 0.6917575221480203, 0.8776757557808577, 0.7348509050495693, 0.723168102044947, 0.5648229009991051, 0.587251985627134, 0.9867462683499941, 0.5538139193283795, 0.884453555560877, 0.9111555152569993, 0.6585905114705954, 0.9323578235937253, 0.5137883210693897, 0.5656354624380188, 0.7994766482625246, 0.6262005670567451, 0.6895549533024236, 0.5904418233605073, 0.8982432376507175, 0.5380621619944117, 0.527772956078285, 0.8020671155128132, 0.9511480163937567, 0.8133899110778885, 0.5106019760094698, 0.8780756737304969, 0.593955378689644, 0.6696315403862183, 0.9673412189784524, 0.8132960655611392, 0.6868168064473854, 0.9988186007620561, 0.7151338760016147, 0.6165333985653724, 0.6148781767463899, 0.6437457303662573, 0.8554934549808595, 0.9517785513067553, 0.7465905513897415, 0.7033633568715387, 0.6365967169807905, 0.6877065634759141, 0.7049592218530623, 0.5141475746297162, 0.5212469942141784, 0.7793184464435123, 0.9943063007267385, 0.875718063685583, 0.7189501427969563, 0.6176835357793071, 0.8864124282735939, 0.5621384463966554, 0.6919699017224918, 0.6444630147105765, 0.6270938268957917, 0.8684460302664628, 0.8950577266646542, 0.6698876093286189, 0.7241676556745147, 0.5552939251615925, 0.9527816480765305, 0.5459400869142912, 0.7777547257605391, 0.9991399125248879, 0.96761618442605, 0.9636701985577933, 0.6601120487210879, 0.9641683561771175, 0.9474414322443836, 0.7662264121311466, 0.7752901126308185, 0.8893150406586601, 0.7312565805406572, 0.8111569935683272, 0.610149496180503, 0.6200661752583492, 0.6512884942787263, 0.8540537276860847, 0.8106756705660079, 0.5270456869176618, 0.9785004716236918, 0.6913159128310506, 0.8584909499471982, 0.879275822826052, 0.8785803594957815, 0.556815572032472, 0.8178872502910135, 0.6888646738988835, 0.722267382428827, 0.6554097206934727, 0.7728855177388922, 0.5661329705914648, 0.7811304124395257, 0.8144818399051921, 0.5039717301929464, 0.7591233998797704, 0.8622813865712369, 0.5216782882506699, 0.5319725653846628, 0.7829623094238922, 0.8628133207007391, 0.7188664926304662, 0.8820333014367008, 0.5482346999735859, 0.5573981290849068, 0.6133698374950027, 0.8483009243941059, 0.5238228625351367, 0.6490094841470233, 0.8943001205346285, 0.8160471508280497, 0.6619636108117993, 0.6874409796652305, 0.8754114975402945, 0.5876133075640722, 0.8853791065248688, 0.7307921226525265, 0.9497606791919082, 0.8711571824895598, 0.5236113951148316, 0.8947615538045877, 0.8902729366443208, 0.9657963578760187, 0.6869213412332742, 0.7901795993985996, 0.6278771226027099, 0.8069828328691495, 0.9895802739750197, 0.6851446355237567, 0.5454486652253747, 0.9345583936589845, 0.7221988968092966, 0.7215086271273896, 0.8343161851859152, 0.9234891762333359, 0.7988944104477524, 0.8817088644304385, 0.8952187894479038, 0.9424338543777471, 0.9604589928223863, 0.8921477594919998, 0.8147050632905937, 0.9988841966877864, 0.9561763765306532, 0.508492135519786, 0.5419297331527839, 0.8860458367483572, 0.8290439245702965, 0.7246188459550802, 0.5852222152397111, 0.9718472361334805, 0.5390255337682119, 0.8852675915299573, 0.6706710187102645, 0.5019000113903732, 0.8195062729988496, 0.8447413376019977, 0.909361262945422, 0.5139376892279612, 0.6536941060002501, 0.7632780797563077, 0.5080341568240043, 0.5016627834171469, 0.5096089285401327, 0.5550204525561513, 0.6436710568088067, 0.8272992241335898, 0.5143401462783689, 0.7739445820543964, 0.6332784506915129, 0.7280386899216216, 0.9515639145291733, 0.8347130420015796, 0.9069266378275059, 0.8842376017226574, 0.7611739266330171, 0.559976155389079, 0.636241425795268, 0.6975186511067719, 0.7094404943528261, 0.5370255440338947, 0.9879444395847193, 0.8301927577727868, 0.529230760720075, 0.5819874124541112, 0.7399436856263516, 0.9224045350498673, 0.6901549768312083, 0.884722702977976, 0.9516571565058183, 0.5752280682510047, 0.945115163986789, 0.8454456741141931, 0.6045332151954925, 0.5563106837922907, 0.8339268008012518, 0.6567959820813983, 0.5989808805741025, 0.8298199895532633, 0.6945232414890519, 0.9745908042982309, 0.8044605450397344, 0.7292027959782907, 0.6890546486636399, 0.5335646862987389, 0.5871944533800393, 0.7017368614725614, 0.8981045028748407, 0.9158334804254631, 0.6701782098094577, 0.6226562096516907, 0.8005316013661254, 0.5864715220692576, 0.8628244626985004, 0.8039286092623148, 0.7834414924892783, 0.5282976885529496, 0.5814073303593847, 0.8530094578929455, 0.8269169420464495, 0.956943116325168, 0.6903770609433271, 0.5692072714653094, 0.7862637184908701, 0.7745671951549695, 0.9551422956858756, 0.6353726699863571, 0.729719253775927, 0.8559987712159576, 0.8852722710621066, 0.5923658558757079, 0.8061356710509275, 0.8440822112990651, 0.6756907669585133, 0.6301220830340963, 0.9255779158279811, 0.8065802773273343, 0.7046619703210135, 0.8033709257365678, 0.7207733316988026, 0.9325078236241573, 0.8696303924653097, 0.7403608043429368, 0.6651964736698754, 0.9193025114275808, 0.6122882337366218, 0.6935015202655055, 0.6271978270493801, 0.7009517826769918, 0.9150674203349293, 0.559919519594272, 0.6556909726319895, 0.8856726775898626, 0.8593579240328877, 0.8514548130266462, 0.9259929269099144, 0.7022997019677053, 0.7637971309083929, 0.9608295548797241, 0.9463980310228786, 0.8007299022900745, 0.8572390913310195, 0.6924144896420531, 0.8070635084040423, 0.8395391357033006, 0.9638228158292851, 0.7646937580411437, 0.8170459041895657, 0.5768812097892213, 0.8890259000535177, 0.8115255865139972, 0.5726542148249195, 0.509760569016529, 0.5519721178654218, 0.8686837429984288, 0.7342187453266119, 0.5843671649086764, 0.8314750549308491, 0.7529781242250905, 0.8110604083926539, 0.6868666520075044, 0.5691548494910323, 0.9675583488376265, 0.8245484162885695, 0.8465865691900349, 0.6534046488565599, 0.5718209165693013, 0.8714951764168033, 0.6586432062810961, 0.7715071525074633, 0.6777276178360487, 0.6379979296517032, 0.9628505379900527, 0.5085892138167374, 0.7517712963491092, 0.9266814525422873, 0.5789181436961777, 0.5704187263247533, 0.7531053875400363, 0.651901829295329, 0.9383761828412741, 0.647349967123846, 0.878514273776501, 0.6431143491684492, 0.7878570024530865, 0.682510605795264, 0.7569295267143272, 0.9785666634047852, 0.6232460532106359, 0.798380732648091, 0.9523851893014157, 0.8771994584136081, 0.5311242762291697, 0.822048241211139, 0.6794105787086646, 0.6592402673111599, 0.6335799904508399, 0.8299922754108702, 0.6817115459493158, 0.6288997329286911, 0.5283390129221898, 0.7834367549245793, 0.554798476079644, 0.8276483286640942, 0.8567431846461706, 0.911077725004223, 0.8104666239786208, 0.8116615957499572, 0.9369946233714097, 0.5581005032929625, 0.6650042116470867, 0.6790526832294619, 0.6944824326369016, 0.8939656179690653, 0.6834801274293028, 0.8521478817121368, 0.6725173185021984, 0.5657526631827575, 0.9486016947159601, 0.5850315387331078, 0.6642266330793724, 0.5988748614076544, 0.862279247695971, 0.5389125264582335, 0.7089270062848948, 0.8520567817109088, 0.6102021541829146, 0.7233704298752504, 0.5486177387491498, 0.8450543033001401, 0.6683740957870818, 0.832565319043659, 0.6718491152148609, 0.6288220001123641, 0.6581573134878773, 0.7222412527562023, 0.9860498553264808, 0.847905765688942, 0.6398282116543879, 0.7055681725175833, 0.5657493952933211, 0.6924986386381363, 0.5188629058600711, 0.7886677856305165, 0.6883754394442632, 0.8261969855979385, 0.6864846145226358, 0.8852374286551025, 0.6836286118557866, 0.8240398407624024, 0.9686616037674447, 0.6290599591786452, 0.7064232062455634, 0.5997119553635993, 0.9850676754396017, 0.8966347071551272, 0.6174090200522672, 0.7912621509252105, 0.9006154810245859, 0.5441137913703333, 0.7740507063728765, 0.6515815345816421, 0.9795841297484837, 0.6393675401780996, 0.5464878191492984, 0.7404016496554302, 0.8458097358413831, 0.5406456105900412, 0.9845772327323519, 0.5342142954434275, 0.6857309671365868, 0.9037945393443209, 0.5279158274247269, 0.7997498112178214, 0.7305221170850222, 0.616952121602838, 0.663351106869364, 0.5458894406278841, 0.6491190398122905, 0.5721395799120923, 0.6490383555475341, 0.5520248604269463, 0.998075966781298, 0.856379386083638, 0.6752470248782052, 0.6166630140863523, 0.5993824873605494, 0.9271287105167694, 0.5061309270889605, 0.9323968782332008, 0.9489420415414042, 0.546099954814831, 0.5506645273184474, 0.60810075308103, 0.5581525347884699, 0.9755319670262546, 0.8306369794249525, 0.5905231920733909, 0.669178867376903, 0.9266701251216571, 0.9513102392358672, 0.9242030613633743, 0.9723142341115906, 0.864954868228141, 0.9274635881987505, 0.7876242035165437, 0.7724355247244437, 0.9679318819436273, 0.6607922205487333, 0.5043489756321935, 0.7054767068327341, 0.7993534872775875, 0.7303337185752314, 0.9686718060868105, 0.8226600861440976, 0.5940703458536198, 0.5550648127311428, 0.5913457801615225, 0.7052852189990526, 0.9973346252000339, 0.9731494821225994, 0.5809645028730465, 0.9981030813560232, 0.7447961551428338, 0.6196460535380237, 0.7389732842768741, 0.6690180032758544, 0.8833483096997412, 0.8994920742973032, 0.5761037043434152, 0.9778271335106139, 0.9454650467587675, 0.7773524474453942, 0.7864315277557645, 0.5837658755550487, 0.818209003004159, 0.5210644891551547, 0.7641649260066902, 0.6303090775006857, 0.7661067427744945, 0.7532947768726248, 0.891593108810594, 0.69394756684432, 0.6427082747129632, 0.912272931055324, 0.5360444736674863, 0.6368617113144304, 0.5337682668636958, 0.9033613424109327, 0.7269463465797934, 0.8490718986680277, 0.9022130873690355, 0.9373800196915142, 0.9851660166613465, 0.5483296785309326, 0.9109883320462633, 0.5576041684682771, 0.748239440480885, 0.5601662523181742, 0.7530709499435718, 0.9203169125256743, 0.8158179647552257, 0.9008044258442673, 0.9840788190244475, 0.8684858717683521, 0.560639324605837, 0.9114514721125768, 0.975828009810511, 0.8550062748408775, 0.8840880176834274, 0.9401227960807157, 0.5330505598519917, 0.923176108949981, 0.8686688032469669, 0.5051590028951186, 0.7775256176017014, 0.753868367136303, 0.5996937518792607, 0.6701294725260777, 0.6077691799880649, 0.6489642177703833, 0.8859033395443516, 0.5701466910608932, 0.5556115187453821, 0.56654881018169, 0.5189004876304872, 0.8999519818889956, 0.8712325959979794, 0.5544883667859892, 0.7913077151363805, 0.7609436553807664, 0.9228327986791957, 0.9198686502252624, 0.6429878929055679, 0.8529790835306297, 0.82227625175666, 0.5319553087977986, 0.9826722759390725, 0.5704975108915608, 0.7484765292580977, 0.5297703321295155, 0.9241277799775247, 0.8349818055147449, 0.560406473355895, 0.5154896507714026, 0.7873297371493972, 0.7796783540254791, 0.965229471595484, 0.5605857057494208, 0.5913683219264723, 0.7516048862451451, 0.5384895093647807, 0.8633209679039823, 0.7845450691301357, 0.7526948198465305, 0.8777672562184504, 0.5215995049081372, 0.6195114549447283, 0.6387073538558246, 0.8117801283966903, 0.9740289584065264, 0.7117752709247569, 0.5377388415719064, 0.769682697490365, 0.7637234438012286, 0.7155527929302705, 0.8484309190544188, 0.9952329994930759, 0.6130226451549751, 0.8175788201978433, 0.9853411973960007, 0.6989657051201134, 0.8587949644189437, 0.520441553044229, 0.5461468348649265, 0.9019045688204912, 0.6006188284080347, 0.6677208940268552, 0.7744141613502249, 0.8979636232395767, 0.7499909178189454, 0.7482435127861806, 0.5095606295464572, 0.8817343821329704, 0.7656561151481023, 0.924194894367997, 0.8170294509667368, 0.5875320966520463, 0.9758328937666145, 0.6941785936079121, 0.7452599806286011, 0.793523720782376, 0.9449590179079714, 0.7818910472299812, 0.9665242347510117, 0.7297771987434389, 0.5933534936949066, 0.5279756424482174, 0.832038462111307, 0.9574877593942056, 0.5837804405257377, 0.925687638681553, 0.8256324456480921, 0.5637878956339515, 0.6722544367749437, 0.8222123773904101, 0.6865297562974935, 0.7034203655168553, 0.8831951958592801, 0.8993673935652859, 0.7745568512844601, 0.5658775199316903, 0.8128429138806059, 0.9496867822292928, 0.9056945554214225, 0.7536505031954764, 0.7794676632190385, 0.5994270365980213, 0.834761813417077, 0.6497304237947086, 0.9355455719024095, 0.5981473858790562, 0.82670487793305, 0.9847343741016659, 0.8874090759768803, 0.7302835148685576, 0.8747581849204571, 0.9317838494990986, 0.9345501825756233, 0.5100352794010032, 0.8453245026956415, 0.8135485376292224, 0.6622197523572549, 0.5694087644774355, 0.6639676392264233, 0.9398636275476308, 0.7927823345592738, 0.7555326188911755, 0.5175488766908378, 0.7928710402103021, 0.5542657942625997, 0.7044169012305095, 0.673541599286023, 0.9685376620795068, 0.5798675667320858, 0.9531168752441297, 0.5404180581045651, 0.9906592789926909, 0.6755264380741195, 0.9375972057162008, 0.7641184140233664, 0.7108719167977762, 0.5715781102012256, 0.9424958204278631, 0.5154697805649899, 0.9042410220327675, 0.6345370807499439, 0.5031038005644697, 0.5826350109311278, 0.5990859919722579, 0.600366319698991, 0.976272553977257, 0.8835297742166435, 0.9560986353646075, 0.5511874315843093, 0.6503458303158559, 0.9129826612840517, 0.8754279833261807, 0.7510545471995456, 0.8775220727777293, 0.8753633833981764, 0.8839761041877362, 0.9649401191570197, 0.7852487955966825, 0.8921778815138381, 0.9463191987999844, 0.6488077196420212, 0.6443862811664766, 0.8084260213117536, 0.7679191962071905, 0.8236249278006709, 0.9817962173204126, 0.5975606331862418, 0.6008368603478962, 0.8138363189251467, 0.7233233900563066, 0.7353919053475703, 0.869332051159609, 0.7581305468741071, 0.6060587258509598, 0.7379928840989556, 0.6331653176583493, 0.913288389964592, 0.5020325745861691, 0.5952578557073789, 0.7083807855963361, 0.6029341020024335, 0.8913288181521697, 0.970710783012459, 0.691303703875128, 0.5339964597182413, 0.9257044661839762, 0.8818531421666058, 0.9170506093264437, 0.9542480621077407, 0.8426311915038207, 0.7579988888293772, 0.5716832142024207, 0.5348854696446717, 0.7536976930485931, 0.6652321110551535, 0.7675412715486223, 0.7956083013217798, 0.9688902732943552, 0.8255345878984273, 0.7321994961813205, 0.5978065534046699, 0.8895026484427127, 0.6816343131634011, 0.6602252523096136, 0.5725837878868212, 0.831430143645447, 0.5135029967183113, 0.5252364958033102, 0.7085703202698219, 0.6869304036314889, 0.5810960744556017, 0.9145153644655009, 0.9360453776488684, 0.778373473736486, 0.7580624711952287, 0.747036599468881, 0.9294976825686925, 0.9557677535738869, 0.8152458223112344, 0.9295840582591395, 0.5729462828875949, 0.6514688072164417, 0.6638555929586848, 0.7692485279197838, 0.9317412899663131, 0.5374209254539761, 0.7195050327921074, 0.8978815888193361, 0.9847411378784738, 0.9139605396620434, 0.5110783628037149, 0.6027358331712018, 0.7480360869872134, 0.6054558117991902, 0.7780270123566342, 0.5276892191232967, 0.9270135264125493, 0.5124741644370572, 0.7995169637930378, 0.9843469503293043, 0.7198012149029505, 0.5584843008195225, 0.7441378240775898, 0.6139993915210542, 0.5721464938836287, 0.7639972691619843, 0.7330278729543239, 0.5423962513240936, 0.673505879605496, 0.824708226688142, 0.9648435078801919, 0.6033214066193526, 0.9849156087977295, 0.8042145020753237, 0.6950919504592001, 0.938794570297276, 0.745136663495036, 0.8954818298437073, 0.983828057847121, 0.6779555128110781, 0.7164803948040415, 0.5328699997949621, 0.5004370193342161, 0.5344031978313286, 0.6129685905369864, 0.6834200584266079, 0.7316571803364245, 0.7468277636273281, 0.8286871620679506, 0.872630766150687, 0.8983540531747716, 0.9362847554956988, 0.9444999901492601, 0.8161305243031034, 0.9815809890693303, 0.5905912676728949, 0.9188518622395281, 0.9178960567827265, 0.7043634923717179, 0.6529575971109047, 0.7230784349232169, 0.7520441537905506, 0.9572518514162839, 0.7005283360936327, 0.8414767721687901, 0.904526434179546, 0.6351770479215483, 0.6517834175473565, 0.940655797125999, 0.8092032455542801, 0.6751216767047956, 0.8188867036898124, 0.7101708344753399, 0.8416267146340799, 0.8336018250679198, 0.747140112598854, 0.9832142567506252, 0.8862486546386923, 0.5682968644289447, 0.8520367769427892, 0.8371346141242875, 0.9490652139402893, 0.9611267117545785, 0.6620622312410167, 0.8519385067367697, 0.8810216521201433, 0.5474183120989145, 0.5254790771810849, 0.6635410269961696, 0.5710819144283839, 0.6430167493020873, 0.7716914837581712, 0.5107189366977285, 0.7969690228686757, 0.6324667328415825, 0.8095553526654666, 0.6817667465452782, 0.6033366625145742, 0.5160765822714144, 0.9852784023861291, 0.7873190326599171, 0.7586214439214225, 0.5472846294386365, 0.6513630131259305, 0.9553044073551993, 0.8859454613274835, 0.8029632846422625, 0.8692246723098846, 0.7234677591504683, 0.7849991360192445, 0.6082081912349202, 0.8816931819676047, 0.8309198060181453, 0.5889696288139146, 0.8793354798411956, 0.9346750731440692, 0.8253114887665259, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0}; int h_B[]= { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332, 334, 336, 338, 340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362, 364, 366, 368, 370, 372, 375, 377, 379, 381, 383, 385, 387, 389, 391, 393, 396, 398, 400, 402, 404, 406, 410, 412, 414, 416, 418, 420, 422, 424, 426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446, 448, 450, 452, 454, 456, 458, 460, 462, 464, 466, 468, 470, 472, 474, 476, 478, 480, 482, 484, 486, 488, 490, 492, 495, 497, 499, 501, 503, 505, 507, 509, 511, 513, 515, 517, 519, 521, 524, 526, 528, 530, 532, 534, 536, 538, 540, 542, 544, 546, 548, 550, 552, 554, 556, 558, 560, 562, 564, 566, 568, 570, 573, 575, 577, 579, 581, 583, 585, 587, 589, 591, 593, 595, 597, 599, 601, 603, 605, 607, 609, 611, 613, 615, 617, 619, 621, 623, 625, 627, 629, 631, 633, 635, 637, 639, 641, 643, 645, 647, 649, 651, 654, 656, 659, 661, 663, 665, 667, 669, 671, 673, 675, 677, 679, 681, 683, 685, 687, 689, 691, 693, 695, 697, 699, 701, 703, 705, 707, 709, 711, 713, 715, 717, 719, 721, 723, 725, 727, 729, 731, 733, 735, 737, 739, 741, 743, 745, 747, 749, 752, 754, 756, 758, 761, 763, 765, 767, 771, 773, 775, 777, 779, 781, 783, 785, 787, 789, 791, 793, 795, 797, 799, 801, 803, 805, 807, 809, 811, 813, 815, 817, 819, 821, 823, 825, 827, 829, 831, 833, 836, 838, 840, 842, 846, 848, 850, 852, 854, 856, 859, 861, 863, 865, 868, 870, 873, 875, 880, 882, 884, 886, 888, 890, 893, 895, 897, 899, 901, 903, 905, 907, 909, 911, 913, 915, 918, 920, 923, 925, 928, 930, 933, 935, 938, 940, 942, 944, 947, 949, 952, 954, 959, 961, 963, 965, 967, 969, 971, 973, 975, 977, 979, 981, 983, 985, 987, 989, 991, 993, 995, 997, 999, 1001, 1003, 1005, 1007, 1009, 1011, 1013, 1015, 1017, 1019, 1021, 1023, 1025, 1027, 1029, 1031, 1033, 1035, 1037, 1040, 1042, 1044, 1046, 1049, 1051, 1053, 1055, 1057, 1059, 1061, 1063, 1065, 1067, 1069, 1071, 1073, 1075, 1077, 1079, 1082, 1084, 1086, 1088, 1092, 1094, 1096, 1098, 1100, 1102, 1104, 1106, 1108, 1110, 1112, 1114, 1116, 1118, 1120, 1122, 1125, 1127, 1129, 1131, 1133, 1135, 1137, 1139, 1142, 1144, 1147, 1149, 1152, 1154, 1157, 1159, 1165, 1167, 1170, 1172, 1175, 1177, 1180, 1182, 1185, 1187, 1189, 1191, 1193, 1195, 1198, 1200, 1203, 1205, 1207, 1209, 1211, 1213, 1215, 1217, 1219, 1221, 1223, 1225, 1227, 1229, 1231, 1233, 1236, 1238, 1240, 1242, 1244, 1246, 1248, 1250, 1252, 1254, 1257, 1259, 1261, 1263, 1265, 1267, 1269, 1271, 1273, 1275, 1277, 1279, 1281, 1283, 1285, 1287, 1289, 1291, 1293, 1295, 1297, 1299, 1301, 1303, 1306, 1308, 1310, 1312, 1314, 1316, 1318, 1320, 1322, 1324, 1326, 1328, 1330, 1332, 1334, 1336, 1338, 1340, 1342, 1344, 1346, 1348, 1350, 1352, 1354, 1356, 1358, 1360, 1362, 1364, 1366, 1368, 1370, 1372, 1374, 1376, 1379, 1381, 1383, 1385, 1387, 1389, 1391, 1393, 1396, 1398, 1400, 1402, 1405, 1407, 1409, 1411, 1413, 1415, 1417, 1419, 1421, 1423, 1425, 1427, 1430, 1432, 1434, 1436, 1438, 1440, 1443, 1445, 1448, 1450, 1456, 1458, 1461, 1463, 1467, 1469, 1471, 1473, 1475, 1477, 1480, 1482, 1485, 1487, 1490, 1492, 1495, 1497, 1500, 1502, 1505, 1507, 1510, 1512, 1514, 1516, 1518, 1520, 1523, 1525, 1527, 1529, 1531, 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1547, 1549, 1551, 1553, 1555, 1557, 1560, 1562, 1564, 1566, 1569, 1571, 1574, 1576, 1581, 1583, 1586, 1588, 1591, 1593, 1596, 1598, 1601, 1603, 1606, 1608, 1611, 1613, 1616, 1618, 1621, 1623, 1626, 1628, 1631, 1633, 1156, 1156, 1164, 1162, 1161, 1164, 1162, 1161, 1590, 1455, 1453, 409, 408, 1590, 1455, 1453, 1455, 1453, 1578, 1573, 1585, 1504, 1509, 1455, 1453, 1455, 1453, 409, 408, 1455, 1453, 1455, 1453, 409, 408, 1453, 1455, 1455, 1453, 1504, 1509, 1479, 1489, 1479, 1489, 1635, 1630, 1585, 1455, 1453, 1455, 1453, 1465, 1460, 1455, 1453, 1455, 1453, 1465, 1460, 1455, 1453, 1455, 1453, 409, 408, 1455, 1453, 1455, 1453, 1455, 1453, 1455, 1453, 1378, 1504, 1509, 1504, 1509, 1378, 1504, 1509, 1504, 1509, 1578, 1573, 1585, 1590, 1578, 1573, 1585, 1590, 1630, 1635, 1635, 1630, 1578, 1573, 1585, 1590, 946, 958, 1455, 1453, 1489, 1489, 1504, 1509, 1504, 1509, 1578, 1573, 1578, 1573, 1585, 1590, 1578, 1573, 1578, 1573, 1585, 1590, 1580, 1305, 858, 858, 845, 845, 879, 879, 958, 946, 946, 958, 1141, 1141, 1091, 1091, 1164, 1162, 1164, 1162, 1164, 1162, 1164, 1162, 1455, 1453, 1479, 1479, 1479, 1489, 1479, 1489, 1455, 1453, 1305, 1509, 1509, 1504, 1504, 1455, 1453, 1455, 1453, 1455, 1453, 1455, 1453, 1635, 1630, 1635, 1630, 1580, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3104, 3106, 3108, 3110, 3112, 3114, 3116, 3118, 3120, 3122, 3124, 3126, 3128, 3130, 3132, 3134, 3136, 3138, 3140, 3142, 3144, 3146, 3148, 3150, 3152, 3154, 3156, 3158, 3160, 3162, 3164, 3166, 3168, 3170, 3172, 3174, 3176, 3178, 3180, 3182, 3184, 3186, 3188, 3190, 3192, 3194, 3196, 3198, 3200, 3202, 3204, 3206, 3208, 3210, 3212, 3214, 3216, 3218, 3220, 3222, 3224, 3226, 3228, 3230, 3232, 3234, 3236, 3238, 3240, 3242, 3244, 3246, 3248, 3250, 3252, 3254, 3256, 3258, 3260, 3262, 3264, 3266, 3268, 3270, 3272, 3274, 3276, 3278, 3280, 3282, 3284, 3286, 3288, 3290, 3292, 3294, 3296, 3298, 3300, 3302, 3304, 3306, 3308, 3310, 3312, 3314, 3316, 3318, 3320, 3322, 3324, 3326, 3328, 3330, 3332, 3334, 3336, 3338, 3340, 3342, 3344, 3346, 3348, 3350, 3352, 3354, 3356, 3358, 3360, 3362, 3364, 3366, 3368, 3370, 3372, 3374, 3376, 3378, 3380, 3382, 3384, 3386, 3388, 3390, 3392, 3394, 3396, 3398, 3400, 3402, 3404, 3406, 3408, 3410, 3412, 3414, 3416, 3418, 3420, 3422, 3424, 3426, 3428, 3430, 3432, 3434, 3436, 3438, 3440, 3442, 3444, 3446, 3448, 3450, 3452, 3454, 3456, 3458, 3460, 3462, 3464, 3466, 3468, 3470, 3472, 3474, 3476, 3478, 3480, 3482, 3484, 3486, 3488, 3490, 3492, 3494, 3496, 3498, 3500, 3502, 3504, 3506, 3508, 3510, 3512, 3514, 3516, 3518, 3520, 3522, 3524, 3526, 3528, 3530, 3532, 3534, 3536, 3538, 3540, 3542, 3544, 3546, 3548, 3550, 3552, 3554, 3556, 3558, 3560, 3562, 3564, 3566, 3568, 3570, 3572, 3574, 3576, 3578, 3580, 3582, 3584, 3586, 3588, 3590, 3592, 3594, 3596, 3598, 3600, 3602, 3604, 3606, 3608, 3610, 3612, 3614, 3616, 3618, 3620, 3622, 3624, 3626, 3628, 3630, 3632, 3634, 3636, 3638, 3640, 3642, 3644, 3646, 3648, 3650, 3652, 3654, 3656, 3658, 3660, 3662, 3664, 3666, 3668, 3670, 3672, 3674, 3676, 3678, 3680, 3682, 3684, 3686, 3688, 3690, 3692, 3694, 3696, 3698, 3700, 3702, 3704, 3706, 3708, 3710, 3712, 3714, 3716, 3718, 3720, 3722, 3724, 3726, 3728, 3730, 3732, 3734, 3736, 3738, 3740, 3742, 3744, 3746, 3748, 3750, 3752, 3754, 3756, 3758, 3760, 3762, 3764, 3766, 3768, 3770, 3772, 3774, 3776, 3778, 3780, 3782, 3784, 3786, 3788, 3790, 3792, 3794, 3796, 3798, 3800, 3802, 3804, 3806, 3808, 3810, 3812, 3814, 3816, 3818, 3820, 3822, 3824, 3826, 3828, 3830, 3832, 3834, 3836, 3838, 3840, 3842, 3844, 3846, 3848, 3850, 3852, 3854, 3856, 3858, 3860, 3862, 3864, 3866, 3868, 3870, 3872, 3874, 3875, 3876, 3877, 3878, 3879, 3880, 3881, 3882, 3883, 3884, 3885, 3886, 3887, 3888, 3889, 3890, 3891, 3892, 3893, 3894, 3895, 3896, 3897, 3898, 3899, 3900, 3901, 3902, 3903, 3904, 3905, 3906, 3907, 3908, 3909, 3910, 3911, 3912, 3913, 3914, 3915, 3916, 3917, 3918, 3919, 3920, 3921, 3922, 3923, 3924, 3925, 3926, 3927, 3928, 3929, 3930, 3931, 3932, 3933, 3934, 3935, 3936, 3937, 3938, 3939, 3940, 3941, 3942, 3943, 3944, 3945, 3946, 3947, 3948, 3949, 3950, 3951, 3952, 3953, 3954, 3955, 3956, 3957, 3958, 3959, 3960, 3961, 3962, 3963, 3964, 3965, 3966, 3967, 3968, 3969, 3970, 3971, 3972, 3973, 3974, 3975, 3976, 3977, 3978, 3979, 3980, 3981, 3982, 3983, 3984, 3985, 3986, 3987, 3988, 3989, 3990, 3991, 3992, 3993, 3994, 3995, 3996, 3997, 3998, 3999, 4000, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008, 4009, 4010, 4011, 4012, 4013, 4014, 4015, 4016, 4017, 4018, 4019, 4020, 4021, 4022, 4023, 4024, 4025, 4026, 4027, 4028, 4029, 4030, 4031, 4032, 4033, 4034, 4035, 4036, 4037, 4038, 4039, 4040, 4041, 4042, 4043, 4044, 4045, 4046, 4047, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 877, 872, 4067, 858, 927, 922, 956, 951, 653, 653, 892, 1146, 1151, 1151, 1146, 1162, 1179, 1174, 658, 658, 927, 922, 4085, 956, 951, 927, 922, 1151, 1146, 1146, 1151, 658, 653, 1202, 1197, 4451, 4454, 1202, 1197, 1465, 1460, 1484, 1479, 1452, 1447, 4458, 4460, 1484, 1499, 1494, 4109, 4463, 1452, 1447, 4465, 1465, 1460, 1484, 1509, 4114, 4467, 1452, 1447, 1453, 1455, 1465, 1460, 4192, 1479, 1489, 1499, 1494, 4470, 1452, 1447, 4472, 1452, 1447, 4474, 1460, 4476, 1452, 1447, 4478, 1452, 1447, 4480, 1465, 4482, 1452, 1447, 1452, 1447, 1484, 1484, 1499, 1494, 1484, 1484, 1499, 1494, 1484, 1484, 1499, 1494, 1504, 4486, 1479, 1499, 1494, 1447, 1452, 1455, 1453, 1465, 1460, 4192, 1479, 1489, 1494, 1499, 4488, 1455, 1453, 1465, 1460, 4192, 4490, 4492, 4151, 4152, 4494, 4426, 1452, 1447, 4497, 1452, 1447, 4499, 4501, 4157, 1447, 1452, 4503, 1452, 1447, 4505, 4507, 4162, 1452, 1447, 4509, 1452, 1447, 4511, 1465, 1460, 4513, 1484, 1484, 1499, 1494, 1499, 1494, 1499, 1494, 1504, 1452, 1447, 4515, 1452, 1447, 4517, 1465, 1460, 4174, 1452, 1447, 4519, 1452, 1447, 4521, 1465, 1460, 4413, 1484, 1479, 1499, 1494, 1504, 1499, 1494, 1499, 1494, 1452, 1447, 1455, 1453, 1465, 1460, 4185, 1479, 1489, 1499, 1494, 4524, 1499, 1494, 4526, 1447, 1452, 1453, 1455, 1465, 1460, 4192, 1479, 1489, 1499, 1494, 4529, 1494, 1499, 4531, 4533, 4535, 1600, 4537, 4539, 1595, 1610, 1605, 1615, 1625, 1620, 1625, 1620, 1625, 1620, 4543, 4545, 4547, 927, 922, 4208, 956, 951, 927, 922, 4214, 956, 951, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 658, 653, 1202, 1197, 4551, 1479, 1484, 1484, 1499, 1494, 1509, 1499, 1494, 1504, 1447, 1452, 1453, 1455, 1465, 1460, 4244, 1378, 1479, 1489, 1499, 1494, 4555, 1499, 1494, 4557, 4559, 4561, 4563, 1600, 1595, 4565, 4567, 4569, 1600, 1595, 4426, 4373, 1590, 1585, 1595, 1600, 1605, 1610, 1559, 1620, 1625, 1635, 1630, 877, 872, 877, 872, 877, 872, 877, 872, 4268, 4270, 877, 872, 858, 877, 872, 877, 872, 4279, 892, 927, 922, 4284, 956, 951, 4579, 927, 922, 937, 932, 956, 951, 956, 951, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 1162, 1169, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 1164, 1169, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 1162, 1164, 1161, 1179, 1174, 1179, 1174, 4348, 1202, 1197, 1151, 1146, 1141, 1151, 1146, 1156, 4587, 1169, 4589, 1161, 1151, 1146, 1141, 1151, 1146, 1156, 4591, 1161, 4593, 1169, 1179, 1174, 1184, 4348, 1202, 1197, 1573, 1590, 1585, 4358, 4595, 1484, 1484, 1578, 1590, 1585, 1452, 1447, 1455, 1453, 1465, 1460, 4358, 1378, 4599, 1499, 1494, 1494, 1499, 1452, 1447, 1455, 1453, 1465, 1460, 4381, 1378, 4601, 1499, 1494, 1494, 1499, 1452, 1447, 1452, 1447, 4603, 1484, 1479, 1509, 1504, 4373, 4375, 1452, 1447, 1453, 1455, 1460, 1465, 4381, 1378, 1489, 1479, 1499, 1494, 1499, 1494, 1447, 1452, 1455, 1453, 1465, 1460, 4392, 1378, 1479, 1489, 1499, 1494, 1499, 1494, 1452, 1447, 4610, 1452, 1447, 4612, 1465, 1460, 4406, 1452, 1447, 4614, 1452, 1447, 4616, 1465, 1460, 4413, 1484, 1479, 1484, 1489, 1499, 1494, 1509, 1504, 4426, 1590, 1585, 1600, 1595, 1605, 1610, 1559, 1625, 1620, 4618, 4426, 1585, 1590, 1595, 1600, 1605, 1610, 1559, 1625, 1620, 4620, 1578, 1573, 1590, 1585, 1600, 1595, 1610, 1605, 1615, 1625, 1620, 1635, 1630, 30, 31, 4640, 4641, 4642, 4643, 4644, 4645, 4646, 4647, 4648, 4649, 4650, 4651, 4652, 4653, 4654, 4655, 4656, 4657, 4658, 4659, 4660, 4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668, 4669, 4670, 4671, 4672, 4673, 4674, 4677, 4678, 4679, 4680, 4681, 4682, 4683, 4684, 4687, 4688, 4689, 4690, 4692, 4693, 4695, 4696, 4697, 4698, 4699, 4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4713, 4714, 4716, 4717, 4719, 4721, 4722, 4724, 4725, 4727, 4729, 4730, 4731, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739, 4740, 4741, 4742, 4743, 4744, 4745, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4756, 4757, 4758, 4759, 4760, 4762, 4763, 4764, 4765, 4766, 4769, 4770, 4772, 4773, 4774, 4776, 4777, 4780, 4781, 4782, 4784, 4785, 4788, 4789, 4790, 4792, 4793, 4795, 4796, 4798, 4799, 4800, 4801, 4802, 4803, 4804, 4805, 4806, 4807, 4808, 4810, 4811, 4813, 4814, 4815, 4816, 4817, 4819, 4820, 4822, 4823, 4824, 4825, 4826, 4827, 4828, 4829, 4830, 4831, 4832, 4833, 4834, 4835, 4836, 4837, 4838, 4839, 4840, 4841, 4842, 4843, 4844, 4846, 4847, 4849, 4850, 4851, 4852, 4853, 4854, 4855, 4856, 4857, 4858, 4859, 4861, 4862, 4866, 4869, 4870, 4871, 4872, 4873, 4874, 4875, 4876, 4877, 4878, 4882, 4883, 4884, 4885, 4886, 4887, 4888, 4889, 4890, 4891, 4892, 4893, 4894, 4895, 4896, 4897, 4898, 4899, 4900, 4901, 4902, 4904, 4905, 4906, 4907, 4908, 4909, 4910, 4911, 4912, 4913, 4914, 4915, 4916, 4917, 4918, 4919, 4920, 4921, 4922, 4923, 4924, 4926, 4927, 4932, 4933, 4937, 4938, 4939, 4940, 4941, 4942, 4943, 4944, 4945, 4946, 4947, 4948, 4949, 4950, 4951, 4952, 4953, 4954, 4955, 4956, 4957, 4958, 4959, 4960, 4961, 4962, 4963, 4964, 4965, 4966, 4967, 4968, 4969, 4970, 4971, 4972, 4973, 4974, 4975, 4977, 4978, 4979, 4980, 4981, 4982, 4983, 4984, 4985, 4986, 4987, 4988, 4989, 4990, 4991, 4992, 4993, 4994, 4995, 4996, 4997, 4998, 4999, 5000, 5001, 5002, 5003, 5004, 5005, 5006, 5007, 5008, 5009, 5010, 5011, 5012, 5013, 5014, 5015, 5016, 5017, 5018, 5019, 5020, 5021, 5022, 5023, 5024, 5025, 5027, 5029, 5030, 5031, 5032, 5033, 5034, 5035, 5037, 5039, 5040, 5041, 5042, 5043, 5044, 5045, 5046, 5047, 5048, 5049, 5051, 5052, 5053, 5054, 5055, 5056, 5057, 5058, 5059, 5060, 5061, 5062, 5063, 5065, 5066, 5067, 5068, 5069, 5070, 5071, 5072, 5073, 5074, 5075, 5076, 5078, 5079, 5080, 5081, 5082, 5083, 5084, 5085, 5087, 5088, 5089, 5090, 5091, 5092, 5093, 5094, 5095, 5096, 5097, 5098, 5099, 5100, 5101, 5102, 5103, 5104, 5105, 5106, 5107, 5108, 5109, 5110, 5111, 5112, 5113, 5114, 5115, 5116, 5117, 5118, 5119, 5120, 5121, 5122, 5124, 5125, 5127, 5128, 5129, 5130, 5131, 5133, 5134, 5136, 5137, 5138, 5139, 5140, 5141, 5142, 5143, 5144, 5145, 5146, 5147, 5148, 5149, 5150, 5151, 5152, 5153, 5154, 5155, 5156, 5158, 5159, 5160, 5161, 5162, 5163, 5164, 5165, 5166, 5167, 5169, 5170, 5171, 5172, 5173, 5174, 5175, 5176, 5177, 5178, 5179, 5180, 5181, 4456, 4453, 4572, 4572, 4768, 4767, 4572, 4572, 4768, 4767, 4572, 4572, 4572, 4572, 4571, 4572, 4571, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5184, 5188, 5190, 5195, 5197, 5200, 5204, 5207, 5209, 5211, 5213, 5215, 5217, 5219, 5221, 5225, 5228, 5231, 5233, 5238, 5240, 5242, 5245, 5247, 5249, 5251, 5254, 5256, 5259, 5261, 5265, 5269, 5273, 5277, 5279, 5281, 5283, 5286, 5288, 5290, 5292, 5298, 5300, 5303, 5305, 5308, 5310, 5312, 5316, 5318, 5320, 5323, 5325, 5327, 5330, 5332, 5334, 5339, 5342, 5344, 5346, 5348, 5350, 5353, 5355, 5357, 5359, 5361, 5363, 5366, 5368, 5370, 5374, 5377, 5379, 5381, 5383, 5386, 5388, 5391, 5393, 5395, 5397, 5400, 5402, 5407, 5410, 5413, 5415, 5417, 5421, 5423, 5425, 5427, 5429, 5433, 5435, 5437, 5440, 5442, 5444, 5446, 5448, 5450, 5454, 5457, 5459, 5463, 5466, 5468, 5470, 5472, 5474, 5476, 5478, 5480, 5485, 5487, 5489, 5494, 5496, 5498, 5501, 5504, 5506, 5509, 5511, 5514, 5519, 5522, 5527, 5531, 5534, 5540, 5542, 5544, 5546, 5550, 5552, 5554, 5556, 5558, 5562, 5564, 5566, 5568, 5572, 5576, 5578, 5580, 5584, 5586, 5588, 5590, 5592, 5594, 5598, 5600, 5602, 5604, 5606, 5608, 5611, 5613, 5615, 5622, 5624, 5627, 5629, 5631, 5634, 5637, 5639, 5641, 5644, 5646, 5648, 5650, 5652, 5655, 5657, 4576, 4575, 5194, 5484, 5493, 5518, 5517, 5526, 5525, 4576, 4575, 5194, 5484, 5493, 5518, 5517, 5526, 5525, 4576, 4575, 5462, 5484, 5493, 5518, 5517, 5526, 5659, 5660, 5404, 4554, 4553, 5661, 4571, 5662, 4571, 5663, 5664, 5224, 4686, 4728, 5302, 5307, 4554, 4553, 4554, 4553, 4554, 4553, 4571, 5372, 4571, 5373, 4571, 4554, 4553, 5404, 5665, 4571, 5666, 4571, 4720, 4728, 5302, 5307, 4554, 4553, 4554, 4553, 4554, 4553, 5276, 5667, 5668, 4571, 5372, 4571, 5373, 4571, 5302, 5307, 4554, 4553, 5338, 5669, 5372, 5670, 5373, 5671, 4554, 4553, 5404, 5672, 5673, 5674, 5675, 4572, 4571, 4576, 4575, 5462, 5484, 5493, 5518, 5517, 5526, 5525, 5571, 5621, 4605, 5621, 4598, 4597, 4605, 5064, 5077, 5571, 5621, 4605, 4605, 5621, 5619, 4622, 4622, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 4578, 4577, 4574, 4573, 5877, 5878, 5187, 4578, 4577, 5879, 5385, 4549, 5390, 4550, 5806, 4581, 4582, 4583, 4449, 5880, 4449, 4583, 5881, 4449, 4583, 5503, 4586, 4585, 5708, 4449, 4583, 5882, 5883, 4449, 4583, 5884, 5885, 5529, 5709, 4578, 4577, 4574, 4573, 5886, 5887, 4578, 4577, 5456, 5888, 5385, 4549, 5390, 4550, 5806, 4582, 4581, 4583, 4449, 5889, 4449, 4583, 5890, 4449, 4583, 5503, 4586, 4585, 5708, 4449, 4583, 5891, 5892, 4449, 4583, 5893, 5894, 5529, 5709, 4578, 4577, 4574, 4573, 5895, 5896, 4578, 4577, 5456, 5897, 5206, 4549, 5390, 4550, 5806, 4582, 4581, 4450, 4584, 5898, 4450, 4584, 5899, 4450, 4584, 5503, 4586, 4585, 5708, 4450, 4584, 5900, 5901, 4450, 4584, 5902, 5529, 5709, 4694, 4691, 5610, 5135, 5132, 5617, 5905, 5906, 5907, 5412, 5236, 5784, 5419, 5786, 4928, 4925, 5909, 5873, 5654, 5876, 5911, 5792, 5439, 5795, 5735, 5294, 5912, 4848, 4845, 4812, 4746, 5329, 4821, 4818, 5336, 5914, 5341, 4607, 4606, 5731, 5285, 5733, 4761, 4718, 4685, 5915, 4726, 4723, 5916, 4778, 4775, 5917, 4786, 4485, 4484, 5918, 5919, 5920, 4606, 5921, 5922, 4607, 5923, 5924, 5275, 5716, 5244, 5718, 4712, 5925, 5926, 5927, 5928, 5376, 4542, 4541, 4771, 5929, 5792, 5439, 5795, 4694, 4691, 5610, 5135, 5132, 5617, 5930, 5931, 5932, 5412, 5236, 5784, 5419, 5786, 4928, 4925, 5934, 5873, 5654, 5876, 5936, 5792, 5439, 5795, 5716, 5244, 5718, 4712, 4718, 4715, 5937, 4726, 4723, 5938, 4778, 4775, 5939, 4786, 4485, 4484, 5940, 5941, 5942, 4606, 5943, 5944, 4607, 5945, 5946, 5275, 4812, 4746, 5329, 4821, 4818, 5336, 5947, 5341, 4607, 4606, 5731, 5285, 5733, 4761, 5735, 5294, 5948, 4848, 4845, 5950, 5951, 5952, 5953, 5376, 4542, 4541, 4771, 5954, 5792, 5439, 5795, 4778, 4775, 5955, 4786, 4783, 5956, 4794, 4791, 4797, 5957, 5958, 5322, 4607, 4606, 4812, 4809, 5329, 4821, 4818, 5336, 5959, 4607, 4606, 5341, 5757, 5352, 5759, 4848, 4845, 5763, 5365, 5765, 4863, 4860, 5961, 5963, 5376, 4879, 4542, 4541, 5792, 5439, 5795, 5385, 4549, 5390, 4550, 5399, 4584, 4583, 5780, 5123, 4903, 5610, 5135, 5132, 5617, 5965, 5966, 5967, 5412, 5409, 5784, 5419, 5786, 4928, 4925, 5968, 5789, 5970, 5790, 5654, 5876, 5972, 5973, 5792, 5439, 5795, 4578, 4577, 4574, 4573, 5974, 5975, 4578, 4577, 5456, 5976, 5465, 4976, 5806, 4582, 4581, 5482, 4584, 4583, 5977, 5491, 4584, 4583, 5978, 5500, 4584, 4583, 5503, 4586, 4585, 5821, 5516, 5513, 5979, 5980, 5524, 5521, 5981, 5982, 5529, 5827, 5844, 5582, 5846, 4607, 4606, 5850, 5536, 5852, 4609, 4608, 5123, 5126, 5610, 5132, 5086, 5617, 5983, 5984, 5842, 5985, 5873, 5654, 5876, 5844, 5582, 5846, 4607, 4606, 5850, 5536, 5852, 4609, 4608, 5123, 5050, 5610, 5132, 5086, 5617, 5986, 5987, 5988, 5862, 5989, 5873, 5654, 5876, 5831, 5548, 5990, 4609, 4608, 5836, 5560, 5991, 4609, 4608, 5844, 5582, 5846, 4607, 4606, 5123, 5126, 5610, 5132, 5086, 5617, 5992, 5993, 5842, 5994, 5864, 5633, 5157, 5995, 5868, 5643, 5168, 5844, 5582, 5846, 4607, 4606, 5850, 5596, 5852, 4609, 4608, 5126, 5123, 5610, 5135, 5132, 5617, 5996, 5997, 5862, 5998, 5864, 5633, 5157, 5999, 5868, 5643, 5168, 4622, 5873, 5654, 5876, 26, 27, 28, 29, 30, 31, 6016, 6017, 6018, 6019, 6020, 6022, 6023, 6024, 6026, 6027, 6028, 6029, 6030, 6031, 6032, 6033, 6034, 6036, 6037, 6039, 6040, 6041, 6042, 6043, 6044, 6045, 6046, 6047, 6049, 6050, 6051, 6053, 6054, 6055, 6056, 6057, 6058, 6059, 6061, 6062, 6063, 6065, 6066, 6067, 6068, 6069, 6070, 6071, 6072, 6073, 6075, 6076, 6078, 6079, 6080, 6081, 6082, 6083, 6084, 6085, 6086, 6088, 6089, 6090, 6092, 6093, 6094, 6095, 6096, 6097, 6098, 6100, 6101, 6102, 6104, 6105, 6106, 6107, 6108, 6109, 6110, 6111, 6112, 6114, 6115, 6117, 6118, 6119, 6120, 6121, 6122, 6123, 6124, 6125, 6127, 6128, 6129, 6130, 6131, 6132, 6133, 6134, 6135, 6136, 6137, 6138, 6141, 6142, 6143, 6144, 6145, 6146, 6147, 5908, 6149, 6150, 6151, 5910, 6153, 6154, 6155, 6156, 6157, 6159, 6160, 6161, 6162, 6163, 6164, 6165, 6166, 6168, 6169, 6170, 6171, 6172, 6173, 6174, 6175, 6176, 6178, 6179, 6181, 6182, 6184, 6185, 6186, 6188, 6190, 6191, 6193, 6194, 6196, 6197, 6198, 6199, 6200, 6205, 6206, 6207, 6208, 6210, 6211, 6212, 6213, 6214, 6215, 6216, 6217, 6218, 6219, 6222, 6223, 6224, 6225, 6226, 6227, 6228, 5933, 6230, 6231, 6232, 5935, 6234, 6235, 6236, 6237, 6238, 6239, 6240, 6241, 6242, 6244, 6245, 6247, 6248, 6250, 6251, 6252, 6254, 6256, 6257, 6259, 6260, 6262, 6263, 6264, 6265, 6266, 6267, 6268, 6270, 6271, 6272, 6273, 6274, 6275, 6276, 6277, 6278, 6280, 6281, 6286, 6287, 6288, 6289, 6291, 6292, 6293, 6294, 6295, 6297, 6298, 6300, 6301, 6302, 6303, 6305, 6306, 6307, 6308, 6309, 6310, 6311, 6312, 6313, 6315, 6316, 6317, 6318, 6319, 6320, 6321, 6322, 6323, 6324, 6325, 6326, 6327, 6330, 6331, 6332, 6333, 6334, 6335, 6336, 6337, 6338, 6339, 6340, 6341, 6342, 6343, 6344, 6345, 6346, 6347, 6348, 6349, 6350, 6351, 6354, 6355, 6356, 6357, 6358, 6359, 6360, 6362, 6364, 6365, 6366, 6367, 6369, 6370, 6371, 6372, 6373, 6374, 6375, 6376, 6378, 6379, 6380, 6382, 6383, 6384, 6385, 6386, 6387, 6388, 6389, 6391, 6392, 6393, 6395, 6396, 6397, 6398, 6399, 6400, 6401, 6402, 6403, 6404, 6406, 6407, 6408, 6410, 6411, 6412, 6413, 6414, 6415, 6416, 6417, 6418, 6419, 6420, 6421, 6422, 6423, 6424, 6425, 6426, 6427, 6428, 6430, 6432, 6433, 6434, 6435, 6436, 6437, 6438, 6439, 6440, 6441, 6442, 6443, 6444, 6445, 6446, 6447, 6448, 6449, 6450, 6451, 6454, 6456, 6457, 6458, 6459, 6460, 6462, 6463, 6464, 6465, 6467, 6468, 6469, 6470, 6471, 6472, 6473, 6474, 6475, 6476, 6477, 6478, 6479, 6480, 6482, 6484, 6485, 6486, 6488, 6489, 6490, 6491, 6492, 6493, 6494, 6495, 6496, 6497, 6498, 6499, 6500, 6501, 6502, 6503, 6504, 6505, 6506, 6507, 6509, 6511, 6512, 6513, 6515, 6516, 6517, 6518, 6519, 6520, 6521, 6204, 6202, 6285, 6283, 6329, 6328, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 6528, 6530, 6533, 6541, 6543, 6545, 6547, 6550, 6553, 6556, 6561, 6563, 6566, 6574, 6576, 6578, 6580, 6583, 6586, 6589, 6594, 6596, 6599, 6607, 6609, 6611, 6613, 6616, 6619, 6622, 6624, 6627, 6630, 6633, 6634, 6639, 6651, 6653, 6656, 6659, 6666, 6668, 6670, 6672, 6686, 6692, 6695, 6698, 6699, 6704, 6718, 6720, 6722, 6724, 6733, 6736, 6739, 6748, 6751, 6757, 6759, 6761, 6765, 6768, 6771, 6774, 6780, 6785, 6788, 6798, 6802, 6805, 6808, 6809, 6814, 6824, 6826, 6829, 6835, 6837, 6840, 6843, 6847, 6850, 6853, 6861, 6866, 6868, 6871, 6882, 6887, 6889, 6892, 6895, 6902, 6906, 6911, 6913, 6916, 6930, 6935, 6937, 6940, 6539, 6537, 6560, 6572, 6570, 6593, 6605, 6603, 6626, 6637, 6642, 6644, 6646, 6648, 6650, 6663, 6665, 6680, 6678, 6676, 6682, 6684, 6955, 6956, 6689, 6691, 6702, 6707, 6709, 6711, 6713, 6715, 6717, 6732, 6730, 6728, 6743, 6745, 6747, 6957, 6958, 6754, 6756, 6778, 6783, 6959, 6960, 6791, 6793, 6797, 6795, 6857, 6812, 6817, 6816, 6819, 6821, 6823, 6833, 6857, 6859, 6864, 6875, 6876, 6878, 6880, 6885, 6897, 6899, 6901, 6905, 6909, 6920, 6921, 6923, 6924, 6926, 6928, 6933, 6944, 6945, 6947, 6948, 6950, 6952, 6954, 29, 30, 31, 6976, 6978, 6986, 6988, 6996, 6998, 7015, 7019, 7020, 7029, 7032, 7034, 7038, 7041, 7044, 7045, 7051, 7053, 7055, 7056, 7057, 6979, 7079, 7080, 6549, 6038, 6035, 6552, 6558, 6555, 7081, 6989, 7082, 7083, 6582, 6077, 6074, 6585, 6591, 6588, 7084, 6999, 7085, 7086, 6615, 6116, 6113, 6618, 7006, 6621, 7087, 6632, 6629, 7010, 7088, 7011, 7089, 7090, 7091, 7092, 7093, 7012, 6658, 6655, 7094, 7095, 6183, 6180, 6177, 7096, 7097, 7098, 7099, 7100, 7101, 7103, 7104, 6697, 6694, 7024, 7105, 7025, 7106, 7107, 7108, 7109, 7110, 7111, 6249, 6246, 6243, 7112, 7113, 7114, 6738, 6735, 7115, 7116, 7117, 7033, 7118, 7120, 7121, 6763, 6299, 6296, 6773, 6770, 7122, 7042, 7123, 7043, 7124, 7126, 7127, 7054, 7128, 7129, 6801, 6855, 6852, 7130, 6807, 6804, 7049, 7131, 7050, 7132, 7133, 7134, 7135, 7136, 7054, 7137, 6849, 6855, 6852, 7138, 7139, 7061, 7140, 7062, 6873, 6870, 7141, 7142, 7143, 7144, 7065, 7145, 7066, 6894, 6891, 6896, 7146, 7147, 7148, 7070, 7149, 7071, 7150, 7072, 6918, 6915, 7151, 7152, 7153, 7154, 7155, 7156, 7075, 7157, 7076, 6942, 6939, 7158, 7159, 7160, 7161, 7162, 7163, 7164, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 6025, 6532, 7189, 7192, 7193, 7194, 7195, 7196, 7197, 6064, 6565, 7199, 7202, 7203, 7204, 7205, 7206, 7207, 6103, 6598, 7209, 7212, 7213, 7214, 7215, 7216, 7217, 7219, 7220, 7221, 7223, 7229, 7230, 7231, 7174, 6187, 7234, 7235, 7236, 7237, 7176, 7245, 7246, 7247, 7249, 6253, 7256, 7257, 7258, 7259, 7262, 7263, 7178, 7267, 7179, 7271, 7272, 7273, 7180, 7274, 7275, 7181, 7277, 7279, 7182, 6381, 6828, 7283, 6846, 6394, 6390, 7286, 7287, 7288, 7290, 7291, 7292, 7294, 7295, 6381, 6828, 7300, 6846, 6394, 6390, 7302, 7303, 7304, 7307, 7309, 7310, 7311, 7316, 7318, 7319, 7320, 7321, 7325, 7327, 7329, 7330, 7331, 7338, 7340, 7341, 7342, 7227, 7225, 7241, 7233, 7244, 7253, 7251, 7255, 7265, 7270, 7282, 7299, 7314, 7323, 7336, 7334, 7349, 7347, 7345, 29, 30, 31, 7360, 7361, 7362, 7363, 7367, 7369, 7370, 7371, 7372, 7376, 7378, 7379, 7380, 7381, 7385, 7387, 7392, 7394, 7395, 7397, 7399, 7400, 7401, 7405, 7407, 7409, 7410, 7412, 7414, 7415, 7418, 7419, 7421, 7424, 7425, 7426, 7427, 7428, 7429, 7430, 7432, 7434, 7439, 7440, 7441, 7442, 7443, 7444, 7446, 7450, 7454, 7460, 7464, 7390, 7466, 7467, 7468, 7469, 7391, 7470, 7404, 7471, 7472, 7473, 7413, 7474, 7475, 7423, 7422, 7476, 7437, 7477, 7297, 7449, 7448, 7478, 7453, 7452, 7479, 7459, 7458, 7457, 7480, 7481, 7463, 7462, 7482, 7483, 7484, 25, 26, 27, 28, 29, 30, 31, 7488, 7490, 7491, 7493, 7495, 7496, 7498, 7500, 7501, 7506, 7511, 7517, 7522, 7524, 7525, 7530, 7533, 7198, 7208, 7218, 7541, 7389, 7542, 7505, 7546, 7509, 7548, 7403, 7549, 7515, 7552, 7516, 7555, 7556, 7520, 7521, 7289, 7558, 7436, 7560, 7305, 7312, 7561, 7562, 7456, 7564, 7565, 7332, 7567, 7568, 7569, 7570, 7343, 7572, 7573, 7574, 24, 25, 26, 27, 28, 29, 30, 31, 7593, 7594, 7598, 7600, 7585, 7601, 7366, 7588, 7602, 7375, 7591, 7603, 7384, 7605, 7607, 7609, 7611, 7613, 7551, 7615, 7618, 7518, 7616, 7619, 7597, 7620, 7622, 7559, 7532, 7624, 7625, 7628, 7631, 7633, 7636, 7639, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7652, 7654, 7655, 7657, 7658, 7660, 7604, 7508, 7544, 7547, 7610, 7513, 7554, 7669, 7557, 7672, 7431, 7621, 7676, 7445, 7678, 7679, 7680, 7682, 24, 25, 26, 27, 28, 29, 30, 31, 7653, 7656, 7659, 7719, 7723, 7668, 7728, 7731, 7732, 7733, 7734, 7735, 7612, 7606, 7675, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7747, 7748, 7670, 7673, 7677, 7746, 7745, 7744, 7756, 7757, 7758, 7683, 7635, 7566, 7563, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7720, 7777, 7781, 7782, 7783, 7726, 7779, 7780, 7787, 7788, 7789, 7790, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7808, 7809, 7810, 7813, 7814, 7815, 7816, 7818, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7842, 7724, 7721, 7846, 7786, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7873, 7874, 7875, 7876, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7784, 7843, 7906, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7936, 7938, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7968, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 8000, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 8032, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7969, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; int h_C[]= { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, 353, 355, 357, 359, 361, 363, 365, 367, 369, 371, 373, 376, 378, 380, 382, 384, 386, 388, 390, 392, 394, 397, 399, 401, 403, 405, 407, 411, 413, 415, 417, 419, 421, 423, 425, 427, 429, 431, 433, 435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457, 459, 461, 463, 465, 467, 469, 471, 473, 475, 477, 479, 481, 483, 485, 487, 489, 491, 493, 496, 498, 500, 502, 504, 506, 508, 510, 512, 514, 516, 518, 520, 522, 525, 527, 529, 531, 533, 535, 537, 539, 541, 543, 545, 547, 549, 551, 553, 555, 557, 559, 561, 563, 565, 567, 569, 571, 574, 576, 578, 580, 582, 584, 586, 588, 590, 592, 594, 596, 598, 600, 602, 604, 606, 608, 610, 612, 614, 616, 618, 620, 622, 624, 626, 628, 630, 632, 634, 636, 638, 640, 642, 644, 646, 648, 650, 652, 655, 657, 660, 662, 664, 666, 668, 670, 672, 674, 676, 678, 680, 682, 684, 686, 688, 690, 692, 694, 696, 698, 700, 702, 704, 706, 708, 710, 712, 714, 716, 718, 720, 722, 724, 726, 728, 730, 732, 734, 736, 738, 740, 742, 744, 746, 748, 750, 753, 755, 757, 759, 762, 764, 766, 768, 772, 774, 776, 778, 780, 782, 784, 786, 788, 790, 792, 794, 796, 798, 800, 802, 804, 806, 808, 810, 812, 814, 816, 818, 820, 822, 824, 826, 828, 830, 832, 834, 837, 839, 841, 843, 847, 849, 851, 853, 855, 857, 860, 862, 864, 866, 869, 871, 874, 876, 881, 883, 885, 887, 889, 891, 894, 896, 898, 900, 902, 904, 906, 908, 910, 912, 914, 916, 919, 921, 924, 926, 929, 931, 934, 936, 939, 941, 943, 945, 948, 950, 953, 955, 960, 962, 964, 966, 968, 970, 972, 974, 976, 978, 980, 982, 984, 986, 988, 990, 992, 994, 996, 998, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018, 1020, 1022, 1024, 1026, 1028, 1030, 1032, 1034, 1036, 1038, 1041, 1043, 1045, 1047, 1050, 1052, 1054, 1056, 1058, 1060, 1062, 1064, 1066, 1068, 1070, 1072, 1074, 1076, 1078, 1080, 1083, 1085, 1087, 1089, 1093, 1095, 1097, 1099, 1101, 1103, 1105, 1107, 1109, 1111, 1113, 1115, 1117, 1119, 1121, 1123, 1126, 1128, 1130, 1132, 1134, 1136, 1138, 1140, 1143, 1145, 1148, 1150, 1153, 1155, 1158, 1160, 1166, 1168, 1171, 1173, 1176, 1178, 1181, 1183, 1186, 1188, 1190, 1192, 1194, 1196, 1199, 1201, 1204, 1206, 1208, 1210, 1212, 1214, 1216, 1218, 1220, 1222, 1224, 1226, 1228, 1230, 1232, 1234, 1237, 1239, 1241, 1243, 1245, 1247, 1249, 1251, 1253, 1255, 1258, 1260, 1262, 1264, 1266, 1268, 1270, 1272, 1274, 1276, 1278, 1280, 1282, 1284, 1286, 1288, 1290, 1292, 1294, 1296, 1298, 1300, 1302, 1304, 1307, 1309, 1311, 1313, 1315, 1317, 1319, 1321, 1323, 1325, 1327, 1329, 1331, 1333, 1335, 1337, 1339, 1341, 1343, 1345, 1347, 1349, 1351, 1353, 1355, 1357, 1359, 1361, 1363, 1365, 1367, 1369, 1371, 1373, 1375, 1377, 1380, 1382, 1384, 1386, 1388, 1390, 1392, 1394, 1397, 1399, 1401, 1403, 1406, 1408, 1410, 1412, 1414, 1416, 1418, 1420, 1422, 1424, 1426, 1428, 1431, 1433, 1435, 1437, 1439, 1441, 1444, 1446, 1449, 1451, 1457, 1459, 1462, 1464, 1468, 1470, 1472, 1474, 1476, 1478, 1481, 1483, 1486, 1488, 1491, 1493, 1496, 1498, 1501, 1503, 1506, 1508, 1511, 1513, 1515, 1517, 1519, 1521, 1524, 1526, 1528, 1530, 1532, 1534, 1536, 1538, 1540, 1542, 1544, 1546, 1548, 1550, 1552, 1554, 1556, 1558, 1561, 1563, 1565, 1567, 1570, 1572, 1575, 1577, 1582, 1584, 1587, 1589, 1592, 1594, 1597, 1599, 1602, 1604, 1607, 1609, 1612, 1614, 1617, 1619, 1622, 1624, 1627, 1629, 1632, 1634, 1039, 1048, 1163, 1163, 136, 1163, 1163, 137, 760, 1442, 1442, 1429, 1429, 751, 1454, 1454, 1442, 1442, 572, 572, 760, 1395, 1395, 1442, 1442, 1454, 1454, 1429, 1429, 1442, 1442, 1454, 1454, 1429, 1429, 1454, 1454, 1442, 1442, 1404, 1404, 1235, 1235, 1256, 1256, 1568, 1568, 751, 1454, 1454, 1442, 1442, 374, 374, 1454, 1454, 1442, 1442, 395, 395, 1442, 1442, 1454, 1454, 1429, 1429, 1442, 1442, 1454, 1454, 1454, 1454, 1442, 1442, 494, 1395, 1395, 1404, 1404, 523, 1395, 1395, 1404, 1404, 572, 572, 751, 751, 572, 572, 751, 751, 1522, 1522, 1568, 1568, 572, 572, 751, 751, 917, 917, 1442, 1442, 1235, 1256, 1395, 1395, 1404, 1404, 769, 769, 769, 769, 751, 751, 769, 769, 769, 769, 760, 760, 770, 770, 867, 878, 835, 844, 867, 878, 917, 917, 957, 957, 1039, 1048, 1081, 1090, 1124, 1124, 1124, 1124, 1163, 1163, 1163, 1163, 1442, 1442, 1235, 1256, 1235, 1235, 1256, 1256, 1454, 1454, 1579, 1395, 1404, 1395, 1404, 1454, 1454, 1442, 1442, 1442, 1442, 1454, 1454, 1522, 1522, 1568, 1568, 1579, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3105, 3107, 3109, 3111, 3113, 3115, 3117, 3119, 3121, 3123, 3125, 3127, 3129, 3131, 3133, 3135, 3137, 3139, 3141, 3143, 3145, 3147, 3149, 3151, 3153, 3155, 3157, 3159, 3161, 3163, 3165, 3167, 3169, 3171, 3173, 3175, 3177, 3179, 3181, 3183, 3185, 3187, 3189, 3191, 3193, 3195, 3197, 3199, 3201, 3203, 3205, 3207, 3209, 3211, 3213, 3215, 3217, 3219, 3221, 3223, 3225, 3227, 3229, 3231, 3233, 3235, 3237, 3239, 3241, 3243, 3245, 3247, 3249, 3251, 3253, 3255, 3257, 3259, 3261, 3263, 3265, 3267, 3269, 3271, 3273, 3275, 3277, 3279, 3281, 3283, 3285, 3287, 3289, 3291, 3293, 3295, 3297, 3299, 3301, 3303, 3305, 3307, 3309, 3311, 3313, 3315, 3317, 3319, 3321, 3323, 3325, 3327, 3329, 3331, 3333, 3335, 3337, 3339, 3341, 3343, 3345, 3347, 3349, 3351, 3353, 3355, 3357, 3359, 3361, 3363, 3365, 3367, 3369, 3371, 3373, 3375, 3377, 3379, 3381, 3383, 3385, 3387, 3389, 3391, 3393, 3395, 3397, 3399, 3401, 3403, 3405, 3407, 3409, 3411, 3413, 3415, 3417, 3419, 3421, 3423, 3425, 3427, 3429, 3431, 3433, 3435, 3437, 3439, 3441, 3443, 3445, 3447, 3449, 3451, 3453, 3455, 3457, 3459, 3461, 3463, 3465, 3467, 3469, 3471, 3473, 3475, 3477, 3479, 3481, 3483, 3485, 3487, 3489, 3491, 3493, 3495, 3497, 3499, 3501, 3503, 3505, 3507, 3509, 3511, 3513, 3515, 3517, 3519, 3521, 3523, 3525, 3527, 3529, 3531, 3533, 3535, 3537, 3539, 3541, 3543, 3545, 3547, 3549, 3551, 3553, 3555, 3557, 3559, 3561, 3563, 3565, 3567, 3569, 3571, 3573, 3575, 3577, 3579, 3581, 3583, 3585, 3587, 3589, 3591, 3593, 3595, 3597, 3599, 3601, 3603, 3605, 3607, 3609, 3611, 3613, 3615, 3617, 3619, 3621, 3623, 3625, 3627, 3629, 3631, 3633, 3635, 3637, 3639, 3641, 3643, 3645, 3647, 3649, 3651, 3653, 3655, 3657, 3659, 3661, 3663, 3665, 3667, 3669, 3671, 3673, 3675, 3677, 3679, 3681, 3683, 3685, 3687, 3689, 3691, 3693, 3695, 3697, 3699, 3701, 3703, 3705, 3707, 3709, 3711, 3713, 3715, 3717, 3719, 3721, 3723, 3725, 3727, 3729, 3731, 3733, 3735, 3737, 3739, 3741, 3743, 3745, 3747, 3749, 3751, 3753, 3755, 3757, 3759, 3761, 3763, 3765, 3767, 3769, 3771, 3773, 3775, 3777, 3779, 3781, 3783, 3785, 3787, 3789, 3791, 3793, 3795, 3797, 3799, 3801, 3803, 3805, 3807, 3809, 3811, 3813, 3815, 3817, 3819, 3821, 3823, 3825, 3827, 3829, 3831, 3833, 3835, 3837, 3839, 3841, 3843, 3845, 3847, 3849, 3851, 3853, 3855, 3857, 3859, 3861, 3863, 3865, 3867, 3869, 3871, 3873, 1655, 1672, 1673, 1674, 1675, 1676, 1677, 1678, 1681, 1688, 1689, 1690, 1691, 1696, 1697, 1698, 1701, 1702, 1708, 1709, 1710, 1722, 1723, 1726, 1727, 1730, 1731, 1733, 1734, 1737, 1738, 1741, 1742, 1744, 1745, 1748, 1751, 1765, 1766, 1781, 1782, 1788, 1789, 1790, 1791, 1794, 1795, 1797, 1800, 1801, 1804, 1805, 1806, 1807, 1811, 1812, 1815, 1816, 1817, 1818, 1822, 1823, 1826, 1827, 1830, 1831, 1843, 1844, 1847, 1848, 1854, 1855, 1858, 1859, 1879, 1884, 1885, 1888, 1889, 1897, 1902, 1903, 1906, 1907, 1908, 1909, 1910, 1911, 1913, 1914, 1915, 1916, 1923, 1926, 1929, 1930, 1931, 1932, 1933, 1934, 1940, 1946, 1958, 1959, 1962, 1964, 1983, 1984, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002, 2006, 2008, 2022, 2025, 2031, 2033, 2039, 2042, 2050, 2051, 2058, 2061, 2082, 2085, 2094, 2097, 2107, 2108, 2110, 2111, 2119, 2120, 2122, 2123, 2135, 2136, 2138, 2140, 2152, 2153, 2166, 2167, 2176, 2177, 2184, 2197, 2200, 2213, 2216, 2219, 2220, 2223, 2224, 2230, 2231, 2234, 2235, 2257, 2258, 2269, 2270, 2273, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 4065, 4064, 4066, 4068, 4070, 4069, 4072, 4071, 4073, 4347, 4074, 4076, 4075, 4078, 4077, 4079, 4081, 4080, 4095, 4347, 4083, 4082, 4084, 4087, 4086, 4089, 4088, 4091, 4090, 4093, 4092, 4095, 4094, 4097, 4096, 4452, 4455, 4099, 4098, 4101, 4100, 4102, 4103, 4105, 4104, 4459, 4461, 4106, 4108, 4107, 572, 4464, 4171, 4170, 4466, 4111, 4110, 4112, 4113, 572, 4468, 4237, 4115, 4189, 4239, 4242, 4190, 4150, 4247, 4193, 4397, 4396, 4471, 4117, 4116, 4473, 4119, 4118, 4475, 4120, 4477, 4122, 4121, 4479, 4124, 4123, 4481, 4125, 4483, 4127, 4126, 4128, 4127, 4129, 4130, 4132, 4131, 4133, 4134, 4136, 4135, 4137, 4138, 4140, 4139, 4141, 4487, 4142, 4235, 4234, 4188, 4237, 4239, 4143, 4242, 4144, 4150, 4247, 4145, 4398, 4364, 4489, 4147, 4146, 4149, 4148, 4150, 4491, 4493, 572, 572, 4495, 572, 4154, 4153, 4498, 4156, 4155, 4500, 4502, 1466, 4159, 4158, 4504, 4161, 4160, 4506, 4508, 1466, 4171, 4163, 4510, 4368, 4367, 4512, 4173, 4164, 4514, 4165, 4166, 4179, 4167, 4181, 4180, 4235, 4168, 4169, 4171, 4170, 4516, 4368, 4367, 4518, 4173, 4172, 1429, 4410, 4409, 4520, 4408, 4407, 4522, 4412, 4411, 1466, 4175, 4228, 4177, 4176, 4236, 4179, 4178, 4181, 4180, 4182, 4188, 4183, 4189, 4242, 4190, 4184, 4247, 4186, 4397, 4187, 4525, 4248, 4384, 4527, 4188, 4237, 4189, 4239, 4242, 4190, 4191, 4247, 4193, 4397, 4396, 4530, 4398, 4364, 4532, 4534, 4536, 4194, 4538, 4540, 4195, 4197, 4196, 4198, 4200, 4199, 4202, 4201, 4204, 4203, 4544, 4546, 4548, 4206, 4205, 4207, 4210, 4209, 4212, 4211, 4213, 4216, 4215, 4218, 4217, 4220, 4219, 4222, 4221, 4223, 4225, 4224, 4227, 4226, 4552, 4228, 4229, 4230, 4232, 4231, 4233, 4235, 4234, 4236, 4238, 4237, 4240, 4239, 4242, 4241, 4243, 4245, 4247, 4246, 4397, 4396, 4556, 4248, 4384, 4558, 4560, 4562, 4564, 4250, 4249, 4566, 4568, 4570, 4252, 4251, 769, 769, 4427, 4422, 4253, 4441, 4432, 4254, 4255, 4434, 4256, 4258, 4257, 4260, 4259, 4262, 4261, 4264, 4263, 4266, 4265, 4267, 4269, 4272, 4271, 4273, 4275, 4274, 4277, 4276, 4278, 4280, 4282, 4281, 4283, 4286, 4285, 4580, 4288, 4287, 4290, 4289, 4292, 4291, 4294, 4293, 4296, 4295, 4298, 4297, 4300, 4299, 4301, 4302, 4303, 4305, 4304, 4307, 4306, 4309, 4308, 4310, 4311, 4312, 4314, 4313, 4316, 4315, 4318, 4317, 4319, 4321, 4320, 4322, 4324, 4323, 4326, 4325, 4347, 4350, 4327, 4329, 4328, 4330, 4332, 4331, 4333, 4588, 4334, 4590, 4335, 4337, 4336, 4338, 4340, 4339, 4341, 4592, 4342, 4594, 4343, 4345, 4344, 4346, 4347, 4350, 4349, 4351, 4427, 4352, 4362, 4596, 4353, 4354, 4355, 4357, 4356, 4359, 4386, 4361, 4360, 4390, 4389, 4362, 4363, 4600, 4397, 4396, 4398, 4364, 4359, 4386, 4361, 4360, 4390, 4389, 4362, 4363, 4602, 4397, 4396, 4398, 4364, 4366, 4365, 4368, 4367, 4604, 4369, 4370, 4372, 4371, 4374, 4374, 4377, 4376, 4387, 4378, 4389, 4379, 4380, 4382, 4394, 4383, 4397, 4396, 4399, 4384, 4386, 4385, 4388, 4387, 4390, 4389, 4391, 4393, 4395, 4394, 4397, 4396, 4399, 4398, 4401, 4400, 4611, 4403, 4402, 4613, 4405, 4404, 1429, 4408, 4407, 4615, 4410, 4409, 4617, 4412, 4411, 1466, 4414, 4415, 4416, 4417, 4419, 4418, 4421, 4420, 4425, 4427, 4422, 4429, 4423, 4432, 4431, 4424, 4435, 4434, 4619, 4425, 4428, 4427, 4430, 4429, 4432, 4431, 4433, 4435, 4434, 4621, 4437, 4436, 4439, 4438, 4441, 4440, 4443, 4442, 4444, 4446, 4445, 4448, 4447, 30, 31, 1636, 1637, 1638, 1639, 1640, 1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654, 1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666, 1667, 1668, 1669, 1670, 1671, 1679, 1680, 1682, 1683, 1684, 1685, 1686, 1687, 1692, 1693, 1694, 1695, 1699, 1700, 1703, 1704, 1705, 1706, 1707, 1711, 1712, 1713, 1714, 1715, 1716, 1717, 1718, 1719, 1720, 1721, 1724, 1725, 1728, 1729, 1732, 1735, 1736, 1739, 1740, 1743, 1746, 1747, 1749, 1750, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1783, 1784, 1785, 1786, 1787, 1792, 1793, 1796, 1798, 1799, 1802, 1803, 1808, 1809, 1810, 1813, 1814, 1819, 1820, 1821, 1824, 1825, 1828, 1829, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1845, 1846, 1849, 1850, 1851, 1852, 1853, 1856, 1857, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1880, 1881, 1882, 1883, 1886, 1887, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1898, 1899, 1900, 1901, 1904, 1905, 1912, 1917, 1918, 1919, 1920, 1921, 1922, 1924, 1925, 1927, 1928, 1935, 1936, 1937, 1938, 1939, 1941, 1942, 1943, 1944, 1945, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1960, 1961, 1963, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1985, 1986, 1995, 1996, 2003, 2004, 2005, 2007, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2023, 2024, 2026, 2027, 2028, 2029, 2030, 2032, 2034, 2035, 2036, 2037, 2038, 2040, 2041, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2052, 2053, 2054, 2055, 2056, 2057, 2059, 2060, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2083, 2084, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2095, 2096, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105, 2106, 2109, 2112, 2113, 2114, 2115, 2116, 2117, 2118, 2121, 2124, 2125, 2126, 2127, 2128, 2129, 2130, 2131, 2132, 2133, 2134, 2137, 2139, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149, 2150, 2151, 2154, 2155, 2156, 2157, 2158, 2159, 2160, 2161, 2162, 2163, 2164, 2165, 2168, 2169, 2170, 2171, 2172, 2173, 2174, 2175, 2178, 2179, 2180, 2181, 2182, 2183, 2185, 2186, 2187, 2188, 2189, 2190, 2191, 2192, 2193, 2194, 2195, 2196, 2198, 2199, 2201, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2214, 2215, 2217, 2218, 2221, 2222, 2225, 2226, 2227, 2228, 2229, 2232, 2233, 2236, 2237, 2238, 2239, 2240, 2241, 2242, 2243, 2244, 2245, 2246, 2247, 2248, 2249, 2250, 2251, 2252, 2253, 2254, 2255, 2256, 2259, 2260, 2261, 2262, 2263, 2264, 2265, 2266, 2267, 2268, 2271, 2272, 2274, 2275, 2276, 2277, 2278, 2279, 2280, 2281, 2282, 2283, 2284, 4676, 4675, 4700, 4880, 4523, 4523, 4700, 4880, 4523, 4523, 4864, 4867, 4880, 4930, 4929, 4935, 4934, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5185, 5189, 5191, 5196, 5198, 5201, 5205, 5208, 5210, 5212, 5214, 5216, 5218, 5220, 5222, 5226, 5229, 5232, 5234, 5239, 5241, 5243, 5246, 5248, 5250, 5252, 5255, 5257, 5260, 5262, 5266, 5270, 5274, 5278, 5280, 5282, 5284, 5287, 5289, 5291, 5293, 5299, 5301, 5304, 5306, 5309, 5311, 5313, 5317, 5319, 5321, 5324, 5326, 5328, 5331, 5333, 5335, 5340, 5343, 5345, 5347, 5349, 5351, 5354, 5356, 5358, 5360, 5362, 5364, 5367, 5369, 5371, 5375, 5378, 5380, 5382, 5384, 5387, 5389, 5392, 5394, 5396, 5398, 5401, 5403, 5408, 5411, 5414, 5416, 5418, 5422, 5424, 5426, 5428, 5430, 5434, 5436, 5438, 5441, 5443, 5445, 5447, 5449, 5451, 5455, 5458, 5460, 5464, 5467, 5469, 5471, 5473, 5475, 5477, 5479, 5481, 5486, 5488, 5490, 5495, 5497, 5499, 5502, 5505, 5507, 5510, 5512, 5515, 5520, 5523, 5528, 5532, 5535, 5541, 5543, 5545, 5547, 5551, 5553, 5555, 5557, 5559, 5563, 5565, 5567, 5569, 5573, 5577, 5579, 5581, 5585, 5587, 5589, 5591, 5593, 5595, 5599, 5601, 5603, 5605, 5607, 5609, 5612, 5614, 5616, 5623, 5625, 5628, 5630, 5632, 5635, 5638, 5640, 5642, 5645, 5647, 5649, 5651, 5653, 5656, 5658, 5453, 5186, 5461, 5199, 5492, 5028, 5026, 5038, 5036, 5453, 5452, 5461, 5199, 5492, 5028, 5026, 5038, 5036, 5453, 5452, 5461, 5483, 5492, 5028, 5026, 5038, 2399, 2400, 5570, 5406, 5235, 2419, 5237, 2424, 5297, 2431, 2432, 5223, 5253, 5258, 4779, 4787, 5264, 5227, 5268, 5267, 5272, 5271, 5230, 4462, 5296, 4462, 5297, 5406, 5235, 5618, 2503, 5237, 2508, 5297, 5253, 5258, 4779, 4787, 5264, 5263, 5268, 5267, 5272, 5271, 5337, 2555, 2556, 5295, 4496, 5296, 4496, 5297, 4779, 4787, 5315, 5314, 5337, 2605, 4865, 2607, 4868, 2613, 5406, 5405, 5618, 2641, 2642, 2644, 2645, 5432, 5431, 5453, 5452, 5461, 5483, 5492, 5028, 5026, 5038, 5036, 5570, 5620, 5533, 5620, 5538, 5537, 5539, 5549, 5561, 5570, 5620, 5574, 5575, 5620, 5618, 5626, 5636, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5799, 5798, 5797, 5696, 2289, 2290, 5800, 5802, 5801, 2294, 5697, 5703, 5774, 5775, 5805, 5807, 5698, 5700, 5699, 2304, 5814, 5812, 2307, 5817, 5815, 5818, 5820, 5701, 5192, 5823, 5822, 2316, 2317, 5825, 5824, 2320, 2321, 5826, 5193, 5799, 5798, 5797, 5796, 2328, 2329, 5802, 5801, 5800, 2333, 5772, 5703, 5774, 5775, 5805, 5808, 5807, 5700, 5699, 2343, 5814, 5812, 2346, 5817, 5815, 5818, 5820, 5701, 5202, 5823, 5822, 2355, 2356, 5825, 5824, 2359, 2360, 5826, 5203, 5799, 5798, 5797, 5796, 2367, 2368, 5802, 5801, 5800, 2372, 5702, 5703, 5704, 5775, 5805, 5808, 5807, 5706, 5705, 2382, 5814, 5813, 2385, 5817, 5816, 5818, 5820, 5819, 5707, 5823, 5822, 2394, 2395, 5825, 5824, 2398, 5826, 5530, 5713, 5855, 5714, 5859, 5858, 5860, 2409, 2410, 2411, 5782, 5781, 5783, 5785, 5420, 5788, 5787, 2420, 4457, 5874, 5875, 2425, 4457, 5793, 5794, 5756, 5736, 5913, 5761, 5760, 5748, 5747, 5710, 5751, 5750, 5752, 2441, 5729, 5755, 5754, 5730, 5732, 4528, 5734, 5721, 5711, 2451, 5723, 5722, 2454, 5738, 5737, 2457, 5740, 5725, 5724, 2461, 2462, 2463, 5712, 2465, 2466, 5727, 2468, 2469, 5728, 5715, 5717, 4528, 5719, 2475, 2476, 2477, 2478, 5768, 5770, 5769, 5771, 2483, 4462, 5793, 5794, 5713, 5841, 5714, 5859, 5858, 5860, 2493, 2494, 2495, 5782, 5781, 5783, 5785, 5420, 5788, 5787, 2504, 4469, 5874, 5875, 2509, 4469, 5793, 5794, 5715, 5717, 4528, 5719, 5721, 5720, 2519, 5723, 5722, 2522, 5738, 5737, 2525, 5740, 5725, 5724, 2529, 2530, 2531, 5726, 2533, 2534, 5727, 2536, 2537, 5728, 5748, 5747, 5749, 5751, 5750, 5752, 2545, 5729, 5755, 5754, 5730, 5732, 4528, 5734, 5756, 5736, 5949, 5761, 5760, 2559, 2560, 2561, 2562, 5768, 5770, 5769, 5771, 2567, 4496, 5793, 5794, 5738, 5737, 2573, 5740, 5739, 2576, 5742, 5741, 5743, 2580, 2581, 5746, 5745, 5744, 5748, 5747, 5749, 5751, 5750, 5752, 2591, 5755, 5754, 5753, 5756, 5758, 4523, 5761, 5760, 5762, 5764, 4528, 5767, 5766, 2606, 2608, 5768, 5771, 5770, 5769, 4881, 5793, 5794, 5772, 5773, 5774, 5775, 5778, 5777, 5776, 5779, 5855, 5840, 5857, 5859, 5858, 5860, 2631, 2632, 2633, 5782, 5781, 5783, 5785, 5420, 5788, 5787, 5969, 4931, 5971, 4936, 5874, 5875, 2649, 2650, 5791, 5793, 5794, 5799, 5798, 5797, 5796, 2658, 2659, 5802, 5801, 5800, 2663, 5803, 5804, 5805, 5808, 5807, 5811, 5810, 5809, 2672, 5814, 5813, 5812, 2676, 5817, 5816, 5815, 5818, 5820, 5819, 5508, 5823, 5822, 2686, 2687, 5825, 5824, 2690, 2691, 5826, 5530, 5843, 5845, 5583, 5848, 5847, 5849, 5851, 5597, 5854, 5853, 5841, 5856, 5857, 5858, 5859, 5860, 2710, 2711, 5861, 2713, 5828, 5874, 5875, 5843, 5845, 5583, 5848, 5847, 5849, 5851, 5597, 5854, 5853, 5841, 5856, 5857, 5858, 5859, 5860, 2733, 2734, 2735, 5861, 2737, 5829, 5874, 5875, 5830, 5832, 2743, 5834, 5833, 5835, 5837, 2748, 5839, 5838, 5843, 5845, 5583, 5848, 5847, 5841, 5840, 5857, 5858, 5859, 5860, 2762, 2763, 5861, 2765, 5863, 5865, 5866, 2769, 5867, 5869, 5870, 5843, 5845, 5583, 5848, 5847, 5849, 5851, 5597, 5854, 5853, 5856, 5855, 5857, 5859, 5858, 5860, 2789, 2790, 5861, 2792, 5863, 5865, 5866, 2796, 5867, 5869, 5870, 5871, 5872, 5874, 5875, 26, 27, 28, 29, 30, 31, 2285, 2286, 2287, 2288, 6021, 2291, 2292, 2293, 2295, 2296, 2297, 2298, 2299, 2300, 2301, 2302, 2303, 2305, 2306, 2308, 2309, 2310, 2311, 2312, 2313, 2314, 2315, 6048, 2318, 2319, 6052, 2322, 2323, 2324, 2325, 2326, 2327, 6060, 2330, 2331, 2332, 2334, 2335, 2336, 2337, 2338, 2339, 2340, 2341, 2342, 2344, 2345, 2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 6087, 2357, 2358, 6091, 2361, 2362, 2363, 2364, 2365, 2366, 6099, 2369, 2370, 2371, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380, 2381, 2383, 2384, 2386, 2387, 2388, 2389, 2390, 2391, 2392, 2393, 6126, 2396, 2397, 5903, 2401, 2402, 2403, 2404, 2405, 2406, 2407, 2408, 6139, 2412, 2413, 2414, 2415, 2416, 2417, 2418, 6148, 2421, 2422, 2423, 6152, 2426, 2427, 2428, 2429, 2430, 2433, 2434, 2435, 2436, 2437, 2438, 2439, 2440, 2442, 2443, 2444, 2445, 2446, 2447, 2448, 2449, 2450, 2452, 2453, 2455, 2456, 2458, 2459, 2460, 6189, 2464, 6192, 2467, 6195, 2470, 2471, 2472, 2473, 2474, 2479, 2480, 2481, 2482, 2484, 2485, 2486, 2487, 2488, 2489, 2490, 2491, 2492, 6220, 2496, 2497, 2498, 2499, 2500, 2501, 2502, 6229, 2505, 2506, 2507, 6233, 2510, 2511, 2512, 2513, 2514, 2515, 2516, 2517, 2518, 2520, 2521, 2523, 2524, 2526, 2527, 2528, 6255, 2532, 6258, 2535, 6261, 2538, 2539, 2540, 2541, 2542, 2543, 2544, 2546, 2547, 2548, 2549, 2550, 2551, 2552, 2553, 2554, 2557, 2558, 2563, 2564, 2565, 2566, 2568, 2569, 2570, 2571, 2572, 2574, 2575, 2577, 2578, 2579, 6304, 2582, 2583, 2584, 2585, 2586, 2587, 2588, 2589, 2590, 2592, 2593, 2594, 2595, 2596, 2597, 2598, 2599, 2600, 2601, 2602, 2603, 2604, 2609, 2610, 2611, 2612, 2614, 2615, 2616, 2617, 2618, 2619, 2620, 2621, 2622, 2623, 2624, 2625, 2626, 2627, 2628, 2629, 2630, 6352, 2634, 2635, 2636, 2637, 2638, 2639, 2640, 2643, 2646, 2647, 2648, 6368, 2651, 2652, 2653, 2654, 2655, 2656, 2657, 6377, 2660, 2661, 2662, 2664, 2665, 2666, 2667, 2668, 2669, 2670, 2671, 2673, 2674, 2675, 2677, 2678, 2679, 2680, 2681, 2682, 2683, 2684, 2685, 6405, 2688, 2689, 6409, 2692, 2693, 2694, 2695, 2696, 2697, 2698, 2699, 2700, 2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 6429, 2712, 2714, 2715, 2716, 2717, 2718, 2719, 2720, 2721, 2722, 2723, 2724, 2725, 2726, 2727, 2728, 2729, 2730, 2731, 2732, 6452, 2736, 2738, 2739, 2740, 2741, 2742, 2744, 2745, 2746, 2747, 2749, 2750, 2751, 2752, 2753, 2754, 2755, 2756, 2757, 2758, 2759, 2760, 2761, 6481, 2764, 2766, 2767, 2768, 2770, 2771, 2772, 2773, 2774, 2775, 2776, 2777, 2778, 2779, 2780, 2781, 2782, 2783, 2784, 2785, 2786, 2787, 2788, 6508, 2791, 2793, 2794, 2795, 2797, 2798, 2799, 2800, 2801, 2802, 2803, 6203, 6201, 6284, 6282, 5962, 5960, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 6529, 6531, 6534, 6542, 6544, 6546, 6548, 6551, 6554, 6557, 6562, 6564, 6567, 6575, 6577, 6579, 6581, 6584, 6587, 6590, 6595, 6597, 6600, 6608, 6610, 6612, 6614, 6617, 6620, 6623, 5904, 6628, 6631, 6140, 6635, 6640, 6652, 6654, 6657, 6660, 6667, 6669, 6671, 6673, 6687, 6693, 6696, 6221, 6700, 6705, 6719, 6721, 6723, 6725, 6734, 6737, 6740, 6749, 6752, 6758, 6760, 6762, 6766, 6769, 6772, 6775, 6781, 6786, 6789, 6799, 6803, 6806, 6353, 6810, 6815, 6825, 6827, 6830, 6836, 6838, 6841, 6844, 6848, 6851, 6854, 6862, 6867, 6869, 6872, 6883, 6888, 6890, 6893, 6453, 6903, 6907, 6912, 6914, 6917, 6931, 6936, 6938, 6941, 6538, 6536, 6559, 6571, 6569, 6592, 6604, 6602, 6625, 6636, 6641, 6643, 6645, 6647, 6649, 6662, 6664, 6679, 6677, 6675, 6681, 6683, 2865, 2866, 6209, 6690, 6701, 6706, 6708, 6710, 6712, 6714, 6716, 6731, 6729, 6727, 6742, 6744, 6746, 2895, 2896, 6290, 6755, 6777, 6782, 2911, 2912, 5964, 6792, 6796, 6794, 6856, 6811, 6363, 6361, 6818, 6820, 6822, 6832, 6856, 6858, 6863, 6874, 6431, 6877, 6879, 6884, 6455, 6898, 6900, 6904, 6908, 6919, 6483, 6922, 6487, 6925, 6927, 6932, 6943, 6510, 6946, 6514, 6949, 6951, 6953, 29, 30, 31, 6977, 6535, 6987, 6568, 6997, 6601, 6661, 6674, 6688, 6726, 6741, 6753, 6767, 6776, 6790, 6800, 7052, 6831, 6839, 6842, 6845, 6540, 2807, 2808, 6982, 6981, 6980, 6983, 6985, 6984, 2815, 6573, 2819, 2820, 6992, 6991, 6990, 6993, 6995, 6994, 2827, 6606, 2831, 2832, 7002, 7001, 7000, 7003, 7005, 7004, 2839, 7008, 7007, 7009, 2843, 6638, 2845, 2846, 2847, 2848, 2849, 6158, 7014, 7013, 2854, 2855, 7018, 7017, 7016, 2860, 2861, 2862, 2863, 2864, 7102, 2868, 2869, 7022, 7021, 7023, 2873, 6703, 2875, 2876, 2877, 2878, 2879, 2880, 7028, 7027, 7026, 2885, 2886, 2887, 7031, 7030, 2891, 2892, 2893, 6279, 7119, 2898, 2899, 7037, 7036, 7035, 7040, 7039, 2907, 6779, 2909, 6784, 7125, 2914, 2915, 6834, 2919, 2920, 7058, 7060, 7059, 2927, 7047, 7046, 7048, 2931, 6813, 2933, 2934, 2935, 2936, 2937, 6834, 2941, 7058, 7060, 7059, 2948, 2949, 6860, 2951, 6865, 7064, 7063, 2955, 2956, 2957, 2958, 6881, 2960, 6886, 7068, 7067, 7069, 2965, 2966, 2967, 6461, 2969, 6466, 2971, 6910, 7074, 7073, 2975, 2976, 2977, 2978, 2979, 2980, 6929, 2982, 6934, 7078, 7077, 2986, 2987, 2988, 2989, 2990, 2991, 2992, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7169, 7168, 2806, 2809, 2810, 2811, 2812, 2813, 2814, 7171, 7170, 2818, 2821, 2822, 2823, 2824, 2825, 2826, 7173, 7172, 2830, 2833, 2834, 2835, 2836, 2837, 2838, 2840, 2841, 2842, 2844, 2850, 2851, 2852, 6167, 7175, 2857, 2858, 2859, 7238, 6685, 2870, 2871, 2872, 2874, 7177, 2882, 2883, 2884, 7260, 2888, 2889, 6269, 2894, 6750, 2900, 2901, 2902, 6764, 2904, 2905, 6314, 2908, 2910, 6787, 7185, 7184, 2918, 7188, 7187, 7183, 2924, 2925, 2926, 2928, 2929, 2930, 2932, 7296, 7185, 7184, 2940, 7188, 7187, 7186, 2945, 2946, 2947, 2950, 2952, 2953, 2954, 2959, 2961, 2962, 2963, 2964, 2968, 2970, 2972, 2973, 2974, 2981, 2983, 2984, 2985, 7226, 7224, 7240, 7232, 7243, 7252, 7250, 7254, 7264, 7269, 7281, 7298, 7313, 7322, 7335, 7333, 7348, 7346, 7344, 29, 30, 31, 2804, 2805, 7190, 7364, 7368, 2816, 2817, 7200, 7373, 7377, 2828, 2829, 7210, 7382, 7386, 7388, 7393, 2853, 2856, 7398, 7239, 2867, 7402, 2881, 7408, 7261, 7411, 2890, 2897, 7416, 2903, 7420, 2906, 2913, 2916, 2917, 7284, 2921, 2922, 2923, 7433, 7435, 2938, 2939, 7301, 2942, 2943, 2944, 7447, 7451, 7455, 7461, 7465, 7222, 3004, 3005, 3006, 3009, 7228, 3011, 7248, 3015, 3016, 3019, 7266, 3021, 3022, 7278, 7276, 3028, 7293, 3035, 7438, 7308, 7306, 3043, 7317, 7315, 3047, 7328, 7326, 7324, 3052, 3053, 7339, 7337, 3057, 3058, 3059, 25, 26, 27, 28, 29, 30, 31, 7489, 7191, 7365, 7494, 7201, 7374, 7499, 7211, 7383, 7396, 7406, 7417, 7523, 7285, 7526, 7531, 7534, 7492, 7497, 7502, 3002, 7503, 7543, 7504, 3010, 7242, 3013, 7510, 7550, 7514, 3020, 7268, 3024, 3025, 7519, 7280, 7528, 3033, 7529, 3036, 7536, 7537, 3041, 3042, 7538, 3045, 3046, 7539, 3049, 3050, 3051, 7571, 7540, 3055, 3056, 7575, 24, 25, 26, 27, 28, 29, 30, 31, 7507, 7512, 7527, 7535, 7584, 2994, 7586, 7587, 2997, 7589, 7590, 3000, 7592, 3003, 3007, 3012, 3014, 3018, 7614, 3023, 3026, 7595, 7617, 3029, 7596, 3031, 3034, 7623, 7599, 3038, 3040, 3044, 3048, 7634, 3054, 7576, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 2993, 2995, 2996, 2998, 2999, 3001, 7661, 7648, 7662, 7663, 7664, 7649, 7667, 3027, 7671, 3030, 7650, 7674, 3037, 7651, 7626, 7629, 7632, 7637, 24, 25, 26, 27, 28, 29, 30, 31, 7713, 7715, 7717, 3008, 3017, 7725, 3032, 3039, 7627, 7630, 7681, 7638, 7722, 7718, 7729, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7545, 7665, 7749, 7750, 7751, 7716, 7714, 7712, 3063, 3067, 3069, 7755, 7754, 7753, 7752, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7776, 7666, 3060, 3061, 3062, 7778, 7727, 7730, 3071, 3072, 3073, 3074, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7608, 7553, 7811, 3065, 3068, 3070, 7817, 7819, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7812, 7841, 7840, 7847, 7844, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3064, 3066, 7845, 3076, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7904, 7905, 3075, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7937, 7907, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7785, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7872, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3077, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 8064, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; bool h_Op[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; #define THREADS_PER_BLOCK 32 #define BLOCKS_PER_GRID 1 #define SIZE_OF_IN 3104 #define SIZE_OF_AC 5024 __device__ void ac(float *A, const int *B, const int *C, const bool *Op, int n_iter) { int i= blockDim.x * blockIdx.x + threadIdx.x; __shared__ float R[254*THREADS_PER_BLOCK]; const int t= THREADS_PER_BLOCK; __shared__ float final; final=0; R[i + 0*t] = A[i + 0*t]; R[i + 1*t] = A[i + 1*t]; R[i + 2*t] = A[i + 2*t]; R[i + 3*t] = A[i + 3*t]; R[i + 4*t] = A[i + 4*t]; R[i + 5*t] = A[i + 5*t]; R[i + 6*t] = A[i + 6*t]; R[i + 7*t] = A[i + 7*t]; R[i + 8*t] = A[i + 8*t]; R[i + 9*t] = A[i + 9*t]; R[i + 10*t] = A[i + 10*t]; R[i + 11*t] = A[i + 11*t]; R[i + 12*t] = A[i + 12*t]; R[i + 13*t] = A[i + 13*t]; R[i + 14*t] = A[i + 14*t]; R[i + 15*t] = A[i + 15*t]; R[i + 16*t] = A[i + 16*t]; R[i + 17*t] = A[i + 17*t]; R[i + 18*t] = A[i + 18*t]; R[i + 19*t] = A[i + 19*t]; R[i + 20*t] = A[i + 20*t]; R[i + 21*t] = A[i + 21*t]; R[i + 22*t] = A[i + 22*t]; R[i + 23*t] = A[i + 23*t]; R[i + 24*t] = A[i + 24*t]; R[i + 25*t] = A[i + 25*t]; R[i + 26*t] = A[i + 26*t]; R[i + 27*t] = A[i + 27*t]; R[i + 28*t] = A[i + 28*t]; R[i + 29*t] = A[i + 29*t]; R[i + 30*t] = A[i + 30*t]; R[i + 31*t] = A[i + 31*t]; R[i + 32*t] = A[i + 32*t]; R[i + 33*t] = A[i + 33*t]; R[i + 34*t] = A[i + 34*t]; R[i + 35*t] = A[i + 35*t]; R[i + 36*t] = A[i + 36*t]; R[i + 37*t] = A[i + 37*t]; R[i + 38*t] = A[i + 38*t]; R[i + 39*t] = A[i + 39*t]; R[i + 40*t] = A[i + 40*t]; R[i + 41*t] = A[i + 41*t]; R[i + 42*t] = A[i + 42*t]; R[i + 43*t] = A[i + 43*t]; R[i + 44*t] = A[i + 44*t]; R[i + 45*t] = A[i + 45*t]; R[i + 46*t] = A[i + 46*t]; R[i + 47*t] = A[i + 47*t]; R[i + 48*t] = A[i + 48*t]; R[i + 49*t] = A[i + 49*t]; R[i + 50*t] = A[i + 50*t]; R[i + 51*t] = A[i + 51*t]; R[i + 52*t] = A[i + 52*t]; R[i + 53*t] = A[i + 53*t]; R[i + 54*t] = A[i + 54*t]; R[i + 55*t] = A[i + 55*t]; R[i + 56*t] = A[i + 56*t]; R[i + 57*t] = A[i + 57*t]; R[i + 58*t] = A[i + 58*t]; R[i + 59*t] = A[i + 59*t]; R[i + 60*t] = A[i + 60*t]; R[i + 61*t] = A[i + 61*t]; R[i + 62*t] = A[i + 62*t]; R[i + 63*t] = A[i + 63*t]; R[i + 64*t] = A[i + 64*t]; R[i + 65*t] = A[i + 65*t]; R[i + 66*t] = A[i + 66*t]; R[i + 67*t] = A[i + 67*t]; R[i + 68*t] = A[i + 68*t]; R[i + 69*t] = A[i + 69*t]; R[i + 70*t] = A[i + 70*t]; R[i + 71*t] = A[i + 71*t]; R[i + 72*t] = A[i + 72*t]; R[i + 73*t] = A[i + 73*t]; R[i + 74*t] = A[i + 74*t]; R[i + 75*t] = A[i + 75*t]; R[i + 76*t] = A[i + 76*t]; R[i + 77*t] = A[i + 77*t]; R[i + 78*t] = A[i + 78*t]; R[i + 79*t] = A[i + 79*t]; R[i + 80*t] = A[i + 80*t]; R[i + 81*t] = A[i + 81*t]; R[i + 82*t] = A[i + 82*t]; R[i + 83*t] = A[i + 83*t]; R[i + 84*t] = A[i + 84*t]; R[i + 85*t] = A[i + 85*t]; R[i + 86*t] = A[i + 86*t]; R[i + 87*t] = A[i + 87*t]; R[i + 88*t] = A[i + 88*t]; R[i + 89*t] = A[i + 89*t]; R[i + 90*t] = A[i + 90*t]; R[i + 91*t] = A[i + 91*t]; R[i + 92*t] = A[i + 92*t]; R[i + 93*t] = A[i + 93*t]; R[i + 94*t] = A[i + 94*t]; R[i + 95*t] = A[i + 95*t]; R[i + 96*t] = A[i + 96*t]; __syncthreads(); for (int iter=0; iter< n_iter; iter++) { R[i + 97*t] = Op[i + 0*t] ? R[B[i + 0*t]] * R[C[i + 0*t]] : R[B[i + 0*t]] + R[C[i + 0*t]]; R[i + 98*t] = Op[i + 1*t] ? R[B[i + 1*t]] * R[C[i + 1*t]] : R[B[i + 1*t]] + R[C[i + 1*t]]; R[i + 99*t] = Op[i + 2*t] ? R[B[i + 2*t]] * R[C[i + 2*t]] : R[B[i + 2*t]] + R[C[i + 2*t]]; R[i + 100*t] = Op[i + 3*t] ? R[B[i + 3*t]] * R[C[i + 3*t]] : R[B[i + 3*t]] + R[C[i + 3*t]]; R[i + 101*t] = Op[i + 4*t] ? R[B[i + 4*t]] * R[C[i + 4*t]] : R[B[i + 4*t]] + R[C[i + 4*t]]; R[i + 102*t] = Op[i + 5*t] ? R[B[i + 5*t]] * R[C[i + 5*t]] : R[B[i + 5*t]] + R[C[i + 5*t]]; R[i + 103*t] = Op[i + 6*t] ? R[B[i + 6*t]] * R[C[i + 6*t]] : R[B[i + 6*t]] + R[C[i + 6*t]]; R[i + 104*t] = Op[i + 7*t] ? R[B[i + 7*t]] * R[C[i + 7*t]] : R[B[i + 7*t]] + R[C[i + 7*t]]; R[i + 105*t] = Op[i + 8*t] ? R[B[i + 8*t]] * R[C[i + 8*t]] : R[B[i + 8*t]] + R[C[i + 8*t]]; R[i + 106*t] = Op[i + 9*t] ? R[B[i + 9*t]] * R[C[i + 9*t]] : R[B[i + 9*t]] + R[C[i + 9*t]]; R[i + 107*t] = Op[i + 10*t] ? R[B[i + 10*t]] * R[C[i + 10*t]] : R[B[i + 10*t]] + R[C[i + 10*t]]; R[i + 108*t] = Op[i + 11*t] ? R[B[i + 11*t]] * R[C[i + 11*t]] : R[B[i + 11*t]] + R[C[i + 11*t]]; R[i + 109*t] = Op[i + 12*t] ? R[B[i + 12*t]] * R[C[i + 12*t]] : R[B[i + 12*t]] + R[C[i + 12*t]]; R[i + 110*t] = Op[i + 13*t] ? R[B[i + 13*t]] * R[C[i + 13*t]] : R[B[i + 13*t]] + R[C[i + 13*t]]; R[i + 111*t] = Op[i + 14*t] ? R[B[i + 14*t]] * R[C[i + 14*t]] : R[B[i + 14*t]] + R[C[i + 14*t]]; R[i + 112*t] = Op[i + 15*t] ? R[B[i + 15*t]] * R[C[i + 15*t]] : R[B[i + 15*t]] + R[C[i + 15*t]]; R[i + 113*t] = Op[i + 16*t] ? R[B[i + 16*t]] * R[C[i + 16*t]] : R[B[i + 16*t]] + R[C[i + 16*t]]; R[i + 114*t] = Op[i + 17*t] ? R[B[i + 17*t]] * R[C[i + 17*t]] : R[B[i + 17*t]] + R[C[i + 17*t]]; R[i + 115*t] = Op[i + 18*t] ? R[B[i + 18*t]] * R[C[i + 18*t]] : R[B[i + 18*t]] + R[C[i + 18*t]]; R[i + 116*t] = Op[i + 19*t] ? R[B[i + 19*t]] * R[C[i + 19*t]] : R[B[i + 19*t]] + R[C[i + 19*t]]; R[i + 117*t] = Op[i + 20*t] ? R[B[i + 20*t]] * R[C[i + 20*t]] : R[B[i + 20*t]] + R[C[i + 20*t]]; R[i + 118*t] = Op[i + 21*t] ? R[B[i + 21*t]] * R[C[i + 21*t]] : R[B[i + 21*t]] + R[C[i + 21*t]]; R[i + 119*t] = Op[i + 22*t] ? R[B[i + 22*t]] * R[C[i + 22*t]] : R[B[i + 22*t]] + R[C[i + 22*t]]; R[i + 120*t] = Op[i + 23*t] ? R[B[i + 23*t]] * R[C[i + 23*t]] : R[B[i + 23*t]] + R[C[i + 23*t]]; R[i + 121*t] = Op[i + 24*t] ? R[B[i + 24*t]] * R[C[i + 24*t]] : R[B[i + 24*t]] + R[C[i + 24*t]]; R[i + 122*t] = Op[i + 25*t] ? R[B[i + 25*t]] * R[C[i + 25*t]] : R[B[i + 25*t]] + R[C[i + 25*t]]; R[i + 123*t] = Op[i + 26*t] ? R[B[i + 26*t]] * R[C[i + 26*t]] : R[B[i + 26*t]] + R[C[i + 26*t]]; R[i + 124*t] = Op[i + 27*t] ? R[B[i + 27*t]] * R[C[i + 27*t]] : R[B[i + 27*t]] + R[C[i + 27*t]]; R[i + 125*t] = Op[i + 28*t] ? R[B[i + 28*t]] * R[C[i + 28*t]] : R[B[i + 28*t]] + R[C[i + 28*t]]; R[i + 126*t] = Op[i + 29*t] ? R[B[i + 29*t]] * R[C[i + 29*t]] : R[B[i + 29*t]] + R[C[i + 29*t]]; __syncthreads(); R[i + 127*t] = Op[i + 30*t] ? R[B[i + 30*t]] * R[C[i + 30*t]] : R[B[i + 30*t]] + R[C[i + 30*t]]; R[i + 128*t] = Op[i + 31*t] ? R[B[i + 31*t]] * R[C[i + 31*t]] : R[B[i + 31*t]] + R[C[i + 31*t]]; R[i + 129*t] = Op[i + 32*t] ? R[B[i + 32*t]] * R[C[i + 32*t]] : R[B[i + 32*t]] + R[C[i + 32*t]]; R[i + 130*t] = Op[i + 33*t] ? R[B[i + 33*t]] * R[C[i + 33*t]] : R[B[i + 33*t]] + R[C[i + 33*t]]; R[i + 131*t] = Op[i + 34*t] ? R[B[i + 34*t]] * R[C[i + 34*t]] : R[B[i + 34*t]] + R[C[i + 34*t]]; R[i + 132*t] = Op[i + 35*t] ? R[B[i + 35*t]] * R[C[i + 35*t]] : R[B[i + 35*t]] + R[C[i + 35*t]]; R[i + 133*t] = Op[i + 36*t] ? R[B[i + 36*t]] * R[C[i + 36*t]] : R[B[i + 36*t]] + R[C[i + 36*t]]; R[i + 134*t] = Op[i + 37*t] ? R[B[i + 37*t]] * R[C[i + 37*t]] : R[B[i + 37*t]] + R[C[i + 37*t]]; R[i + 135*t] = Op[i + 38*t] ? R[B[i + 38*t]] * R[C[i + 38*t]] : R[B[i + 38*t]] + R[C[i + 38*t]]; R[i + 136*t] = Op[i + 39*t] ? R[B[i + 39*t]] * R[C[i + 39*t]] : R[B[i + 39*t]] + R[C[i + 39*t]]; R[i + 137*t] = Op[i + 40*t] ? R[B[i + 40*t]] * R[C[i + 40*t]] : R[B[i + 40*t]] + R[C[i + 40*t]]; R[i + 138*t] = Op[i + 41*t] ? R[B[i + 41*t]] * R[C[i + 41*t]] : R[B[i + 41*t]] + R[C[i + 41*t]]; R[i + 139*t] = Op[i + 42*t] ? R[B[i + 42*t]] * R[C[i + 42*t]] : R[B[i + 42*t]] + R[C[i + 42*t]]; R[i + 140*t] = Op[i + 43*t] ? R[B[i + 43*t]] * R[C[i + 43*t]] : R[B[i + 43*t]] + R[C[i + 43*t]]; R[i + 141*t] = Op[i + 44*t] ? R[B[i + 44*t]] * R[C[i + 44*t]] : R[B[i + 44*t]] + R[C[i + 44*t]]; R[i + 142*t] = Op[i + 45*t] ? R[B[i + 45*t]] * R[C[i + 45*t]] : R[B[i + 45*t]] + R[C[i + 45*t]]; R[i + 143*t] = Op[i + 46*t] ? R[B[i + 46*t]] * R[C[i + 46*t]] : R[B[i + 46*t]] + R[C[i + 46*t]]; R[i + 144*t] = Op[i + 47*t] ? R[B[i + 47*t]] * R[C[i + 47*t]] : R[B[i + 47*t]] + R[C[i + 47*t]]; __syncthreads(); R[i + 145*t] = Op[i + 48*t] ? R[B[i + 48*t]] * R[C[i + 48*t]] : R[B[i + 48*t]] + R[C[i + 48*t]]; R[i + 146*t] = Op[i + 49*t] ? R[B[i + 49*t]] * R[C[i + 49*t]] : R[B[i + 49*t]] + R[C[i + 49*t]]; R[i + 147*t] = Op[i + 50*t] ? R[B[i + 50*t]] * R[C[i + 50*t]] : R[B[i + 50*t]] + R[C[i + 50*t]]; R[i + 148*t] = Op[i + 51*t] ? R[B[i + 51*t]] * R[C[i + 51*t]] : R[B[i + 51*t]] + R[C[i + 51*t]]; R[i + 149*t] = Op[i + 52*t] ? R[B[i + 52*t]] * R[C[i + 52*t]] : R[B[i + 52*t]] + R[C[i + 52*t]]; R[i + 150*t] = Op[i + 53*t] ? R[B[i + 53*t]] * R[C[i + 53*t]] : R[B[i + 53*t]] + R[C[i + 53*t]]; R[i + 151*t] = Op[i + 54*t] ? R[B[i + 54*t]] * R[C[i + 54*t]] : R[B[i + 54*t]] + R[C[i + 54*t]]; R[i + 152*t] = Op[i + 55*t] ? R[B[i + 55*t]] * R[C[i + 55*t]] : R[B[i + 55*t]] + R[C[i + 55*t]]; R[i + 153*t] = Op[i + 56*t] ? R[B[i + 56*t]] * R[C[i + 56*t]] : R[B[i + 56*t]] + R[C[i + 56*t]]; R[i + 154*t] = Op[i + 57*t] ? R[B[i + 57*t]] * R[C[i + 57*t]] : R[B[i + 57*t]] + R[C[i + 57*t]]; R[i + 155*t] = Op[i + 58*t] ? R[B[i + 58*t]] * R[C[i + 58*t]] : R[B[i + 58*t]] + R[C[i + 58*t]]; R[i + 156*t] = Op[i + 59*t] ? R[B[i + 59*t]] * R[C[i + 59*t]] : R[B[i + 59*t]] + R[C[i + 59*t]]; R[i + 157*t] = Op[i + 60*t] ? R[B[i + 60*t]] * R[C[i + 60*t]] : R[B[i + 60*t]] + R[C[i + 60*t]]; R[i + 158*t] = Op[i + 61*t] ? R[B[i + 61*t]] * R[C[i + 61*t]] : R[B[i + 61*t]] + R[C[i + 61*t]]; R[i + 159*t] = Op[i + 62*t] ? R[B[i + 62*t]] * R[C[i + 62*t]] : R[B[i + 62*t]] + R[C[i + 62*t]]; R[i + 160*t] = Op[i + 63*t] ? R[B[i + 63*t]] * R[C[i + 63*t]] : R[B[i + 63*t]] + R[C[i + 63*t]]; R[i + 161*t] = Op[i + 64*t] ? R[B[i + 64*t]] * R[C[i + 64*t]] : R[B[i + 64*t]] + R[C[i + 64*t]]; __syncthreads(); R[i + 162*t] = Op[i + 65*t] ? R[B[i + 65*t]] * R[C[i + 65*t]] : R[B[i + 65*t]] + R[C[i + 65*t]]; R[i + 163*t] = Op[i + 66*t] ? R[B[i + 66*t]] * R[C[i + 66*t]] : R[B[i + 66*t]] + R[C[i + 66*t]]; R[i + 164*t] = Op[i + 67*t] ? R[B[i + 67*t]] * R[C[i + 67*t]] : R[B[i + 67*t]] + R[C[i + 67*t]]; R[i + 165*t] = Op[i + 68*t] ? R[B[i + 68*t]] * R[C[i + 68*t]] : R[B[i + 68*t]] + R[C[i + 68*t]]; R[i + 166*t] = Op[i + 69*t] ? R[B[i + 69*t]] * R[C[i + 69*t]] : R[B[i + 69*t]] + R[C[i + 69*t]]; R[i + 167*t] = Op[i + 70*t] ? R[B[i + 70*t]] * R[C[i + 70*t]] : R[B[i + 70*t]] + R[C[i + 70*t]]; R[i + 168*t] = Op[i + 71*t] ? R[B[i + 71*t]] * R[C[i + 71*t]] : R[B[i + 71*t]] + R[C[i + 71*t]]; R[i + 169*t] = Op[i + 72*t] ? R[B[i + 72*t]] * R[C[i + 72*t]] : R[B[i + 72*t]] + R[C[i + 72*t]]; R[i + 170*t] = Op[i + 73*t] ? R[B[i + 73*t]] * R[C[i + 73*t]] : R[B[i + 73*t]] + R[C[i + 73*t]]; R[i + 171*t] = Op[i + 74*t] ? R[B[i + 74*t]] * R[C[i + 74*t]] : R[B[i + 74*t]] + R[C[i + 74*t]]; R[i + 172*t] = Op[i + 75*t] ? R[B[i + 75*t]] * R[C[i + 75*t]] : R[B[i + 75*t]] + R[C[i + 75*t]]; R[i + 173*t] = Op[i + 76*t] ? R[B[i + 76*t]] * R[C[i + 76*t]] : R[B[i + 76*t]] + R[C[i + 76*t]]; R[i + 174*t] = Op[i + 77*t] ? R[B[i + 77*t]] * R[C[i + 77*t]] : R[B[i + 77*t]] + R[C[i + 77*t]]; R[i + 175*t] = Op[i + 78*t] ? R[B[i + 78*t]] * R[C[i + 78*t]] : R[B[i + 78*t]] + R[C[i + 78*t]]; R[i + 176*t] = Op[i + 79*t] ? R[B[i + 79*t]] * R[C[i + 79*t]] : R[B[i + 79*t]] + R[C[i + 79*t]]; R[i + 177*t] = Op[i + 80*t] ? R[B[i + 80*t]] * R[C[i + 80*t]] : R[B[i + 80*t]] + R[C[i + 80*t]]; __syncthreads(); R[i + 178*t] = Op[i + 81*t] ? R[B[i + 81*t]] * R[C[i + 81*t]] : R[B[i + 81*t]] + R[C[i + 81*t]]; R[i + 179*t] = Op[i + 82*t] ? R[B[i + 82*t]] * R[C[i + 82*t]] : R[B[i + 82*t]] + R[C[i + 82*t]]; R[i + 180*t] = Op[i + 83*t] ? R[B[i + 83*t]] * R[C[i + 83*t]] : R[B[i + 83*t]] + R[C[i + 83*t]]; R[i + 181*t] = Op[i + 84*t] ? R[B[i + 84*t]] * R[C[i + 84*t]] : R[B[i + 84*t]] + R[C[i + 84*t]]; R[i + 182*t] = Op[i + 85*t] ? R[B[i + 85*t]] * R[C[i + 85*t]] : R[B[i + 85*t]] + R[C[i + 85*t]]; R[i + 183*t] = Op[i + 86*t] ? R[B[i + 86*t]] * R[C[i + 86*t]] : R[B[i + 86*t]] + R[C[i + 86*t]]; R[i + 184*t] = Op[i + 87*t] ? R[B[i + 87*t]] * R[C[i + 87*t]] : R[B[i + 87*t]] + R[C[i + 87*t]]; R[i + 185*t] = Op[i + 88*t] ? R[B[i + 88*t]] * R[C[i + 88*t]] : R[B[i + 88*t]] + R[C[i + 88*t]]; R[i + 186*t] = Op[i + 89*t] ? R[B[i + 89*t]] * R[C[i + 89*t]] : R[B[i + 89*t]] + R[C[i + 89*t]]; R[i + 187*t] = Op[i + 90*t] ? R[B[i + 90*t]] * R[C[i + 90*t]] : R[B[i + 90*t]] + R[C[i + 90*t]]; __syncthreads(); R[i + 188*t] = Op[i + 91*t] ? R[B[i + 91*t]] * R[C[i + 91*t]] : R[B[i + 91*t]] + R[C[i + 91*t]]; R[i + 189*t] = Op[i + 92*t] ? R[B[i + 92*t]] * R[C[i + 92*t]] : R[B[i + 92*t]] + R[C[i + 92*t]]; R[i + 190*t] = Op[i + 93*t] ? R[B[i + 93*t]] * R[C[i + 93*t]] : R[B[i + 93*t]] + R[C[i + 93*t]]; R[i + 191*t] = Op[i + 94*t] ? R[B[i + 94*t]] * R[C[i + 94*t]] : R[B[i + 94*t]] + R[C[i + 94*t]]; R[i + 192*t] = Op[i + 95*t] ? R[B[i + 95*t]] * R[C[i + 95*t]] : R[B[i + 95*t]] + R[C[i + 95*t]]; R[i + 193*t] = Op[i + 96*t] ? R[B[i + 96*t]] * R[C[i + 96*t]] : R[B[i + 96*t]] + R[C[i + 96*t]]; R[i + 194*t] = Op[i + 97*t] ? R[B[i + 97*t]] * R[C[i + 97*t]] : R[B[i + 97*t]] + R[C[i + 97*t]]; R[i + 195*t] = Op[i + 98*t] ? R[B[i + 98*t]] * R[C[i + 98*t]] : R[B[i + 98*t]] + R[C[i + 98*t]]; R[i + 196*t] = Op[i + 99*t] ? R[B[i + 99*t]] * R[C[i + 99*t]] : R[B[i + 99*t]] + R[C[i + 99*t]]; R[i + 197*t] = Op[i + 100*t] ? R[B[i + 100*t]] * R[C[i + 100*t]] : R[B[i + 100*t]] + R[C[i + 100*t]]; R[i + 198*t] = Op[i + 101*t] ? R[B[i + 101*t]] * R[C[i + 101*t]] : R[B[i + 101*t]] + R[C[i + 101*t]]; R[i + 199*t] = Op[i + 102*t] ? R[B[i + 102*t]] * R[C[i + 102*t]] : R[B[i + 102*t]] + R[C[i + 102*t]]; R[i + 200*t] = Op[i + 103*t] ? R[B[i + 103*t]] * R[C[i + 103*t]] : R[B[i + 103*t]] + R[C[i + 103*t]]; R[i + 201*t] = Op[i + 104*t] ? R[B[i + 104*t]] * R[C[i + 104*t]] : R[B[i + 104*t]] + R[C[i + 104*t]]; R[i + 202*t] = Op[i + 105*t] ? R[B[i + 105*t]] * R[C[i + 105*t]] : R[B[i + 105*t]] + R[C[i + 105*t]]; R[i + 203*t] = Op[i + 106*t] ? R[B[i + 106*t]] * R[C[i + 106*t]] : R[B[i + 106*t]] + R[C[i + 106*t]]; __syncthreads(); R[i + 204*t] = Op[i + 107*t] ? R[B[i + 107*t]] * R[C[i + 107*t]] : R[B[i + 107*t]] + R[C[i + 107*t]]; R[i + 205*t] = Op[i + 108*t] ? R[B[i + 108*t]] * R[C[i + 108*t]] : R[B[i + 108*t]] + R[C[i + 108*t]]; R[i + 206*t] = Op[i + 109*t] ? R[B[i + 109*t]] * R[C[i + 109*t]] : R[B[i + 109*t]] + R[C[i + 109*t]]; R[i + 207*t] = Op[i + 110*t] ? R[B[i + 110*t]] * R[C[i + 110*t]] : R[B[i + 110*t]] + R[C[i + 110*t]]; R[i + 208*t] = Op[i + 111*t] ? R[B[i + 111*t]] * R[C[i + 111*t]] : R[B[i + 111*t]] + R[C[i + 111*t]]; R[i + 209*t] = Op[i + 112*t] ? R[B[i + 112*t]] * R[C[i + 112*t]] : R[B[i + 112*t]] + R[C[i + 112*t]]; R[i + 210*t] = Op[i + 113*t] ? R[B[i + 113*t]] * R[C[i + 113*t]] : R[B[i + 113*t]] + R[C[i + 113*t]]; R[i + 211*t] = Op[i + 114*t] ? R[B[i + 114*t]] * R[C[i + 114*t]] : R[B[i + 114*t]] + R[C[i + 114*t]]; R[i + 212*t] = Op[i + 115*t] ? R[B[i + 115*t]] * R[C[i + 115*t]] : R[B[i + 115*t]] + R[C[i + 115*t]]; R[i + 213*t] = Op[i + 116*t] ? R[B[i + 116*t]] * R[C[i + 116*t]] : R[B[i + 116*t]] + R[C[i + 116*t]]; R[i + 214*t] = Op[i + 117*t] ? R[B[i + 117*t]] * R[C[i + 117*t]] : R[B[i + 117*t]] + R[C[i + 117*t]]; R[i + 215*t] = Op[i + 118*t] ? R[B[i + 118*t]] * R[C[i + 118*t]] : R[B[i + 118*t]] + R[C[i + 118*t]]; R[i + 216*t] = Op[i + 119*t] ? R[B[i + 119*t]] * R[C[i + 119*t]] : R[B[i + 119*t]] + R[C[i + 119*t]]; R[i + 217*t] = Op[i + 120*t] ? R[B[i + 120*t]] * R[C[i + 120*t]] : R[B[i + 120*t]] + R[C[i + 120*t]]; __syncthreads(); R[i + 218*t] = Op[i + 121*t] ? R[B[i + 121*t]] * R[C[i + 121*t]] : R[B[i + 121*t]] + R[C[i + 121*t]]; R[i + 219*t] = Op[i + 122*t] ? R[B[i + 122*t]] * R[C[i + 122*t]] : R[B[i + 122*t]] + R[C[i + 122*t]]; R[i + 220*t] = Op[i + 123*t] ? R[B[i + 123*t]] * R[C[i + 123*t]] : R[B[i + 123*t]] + R[C[i + 123*t]]; R[i + 221*t] = Op[i + 124*t] ? R[B[i + 124*t]] * R[C[i + 124*t]] : R[B[i + 124*t]] + R[C[i + 124*t]]; R[i + 222*t] = Op[i + 125*t] ? R[B[i + 125*t]] * R[C[i + 125*t]] : R[B[i + 125*t]] + R[C[i + 125*t]]; R[i + 223*t] = Op[i + 126*t] ? R[B[i + 126*t]] * R[C[i + 126*t]] : R[B[i + 126*t]] + R[C[i + 126*t]]; __syncthreads(); R[i + 224*t] = Op[i + 127*t] ? R[B[i + 127*t]] * R[C[i + 127*t]] : R[B[i + 127*t]] + R[C[i + 127*t]]; R[i + 225*t] = Op[i + 128*t] ? R[B[i + 128*t]] * R[C[i + 128*t]] : R[B[i + 128*t]] + R[C[i + 128*t]]; R[i + 226*t] = Op[i + 129*t] ? R[B[i + 129*t]] * R[C[i + 129*t]] : R[B[i + 129*t]] + R[C[i + 129*t]]; R[i + 227*t] = Op[i + 130*t] ? R[B[i + 130*t]] * R[C[i + 130*t]] : R[B[i + 130*t]] + R[C[i + 130*t]]; R[i + 228*t] = Op[i + 131*t] ? R[B[i + 131*t]] * R[C[i + 131*t]] : R[B[i + 131*t]] + R[C[i + 131*t]]; R[i + 229*t] = Op[i + 132*t] ? R[B[i + 132*t]] * R[C[i + 132*t]] : R[B[i + 132*t]] + R[C[i + 132*t]]; __syncthreads(); R[i + 230*t] = Op[i + 133*t] ? R[B[i + 133*t]] * R[C[i + 133*t]] : R[B[i + 133*t]] + R[C[i + 133*t]]; R[i + 231*t] = Op[i + 134*t] ? R[B[i + 134*t]] * R[C[i + 134*t]] : R[B[i + 134*t]] + R[C[i + 134*t]]; R[i + 232*t] = Op[i + 135*t] ? R[B[i + 135*t]] * R[C[i + 135*t]] : R[B[i + 135*t]] + R[C[i + 135*t]]; R[i + 233*t] = Op[i + 136*t] ? R[B[i + 136*t]] * R[C[i + 136*t]] : R[B[i + 136*t]] + R[C[i + 136*t]]; __syncthreads(); R[i + 234*t] = Op[i + 137*t] ? R[B[i + 137*t]] * R[C[i + 137*t]] : R[B[i + 137*t]] + R[C[i + 137*t]]; R[i + 235*t] = Op[i + 138*t] ? R[B[i + 138*t]] * R[C[i + 138*t]] : R[B[i + 138*t]] + R[C[i + 138*t]]; R[i + 236*t] = Op[i + 139*t] ? R[B[i + 139*t]] * R[C[i + 139*t]] : R[B[i + 139*t]] + R[C[i + 139*t]]; __syncthreads(); R[i + 237*t] = Op[i + 140*t] ? R[B[i + 140*t]] * R[C[i + 140*t]] : R[B[i + 140*t]] + R[C[i + 140*t]]; R[i + 238*t] = Op[i + 141*t] ? R[B[i + 141*t]] * R[C[i + 141*t]] : R[B[i + 141*t]] + R[C[i + 141*t]]; __syncthreads(); R[i + 239*t] = Op[i + 142*t] ? R[B[i + 142*t]] * R[C[i + 142*t]] : R[B[i + 142*t]] + R[C[i + 142*t]]; R[i + 240*t] = Op[i + 143*t] ? R[B[i + 143*t]] * R[C[i + 143*t]] : R[B[i + 143*t]] + R[C[i + 143*t]]; __syncthreads(); R[i + 241*t] = Op[i + 144*t] ? R[B[i + 144*t]] * R[C[i + 144*t]] : R[B[i + 144*t]] + R[C[i + 144*t]]; __syncthreads(); R[i + 242*t] = Op[i + 145*t] ? R[B[i + 145*t]] * R[C[i + 145*t]] : R[B[i + 145*t]] + R[C[i + 145*t]]; __syncthreads(); R[i + 243*t] = Op[i + 146*t] ? R[B[i + 146*t]] * R[C[i + 146*t]] : R[B[i + 146*t]] + R[C[i + 146*t]]; __syncthreads(); R[i + 244*t] = Op[i + 147*t] ? R[B[i + 147*t]] * R[C[i + 147*t]] : R[B[i + 147*t]] + R[C[i + 147*t]]; __syncthreads(); R[i + 245*t] = Op[i + 148*t] ? R[B[i + 148*t]] * R[C[i + 148*t]] : R[B[i + 148*t]] + R[C[i + 148*t]]; __syncthreads(); R[i + 246*t] = Op[i + 149*t] ? R[B[i + 149*t]] * R[C[i + 149*t]] : R[B[i + 149*t]] + R[C[i + 149*t]]; __syncthreads(); R[i + 247*t] = Op[i + 150*t] ? R[B[i + 150*t]] * R[C[i + 150*t]] : R[B[i + 150*t]] + R[C[i + 150*t]]; __syncthreads(); R[i + 248*t] = Op[i + 151*t] ? R[B[i + 151*t]] * R[C[i + 151*t]] : R[B[i + 151*t]] + R[C[i + 151*t]]; __syncthreads(); R[i + 249*t] = Op[i + 152*t] ? R[B[i + 152*t]] * R[C[i + 152*t]] : R[B[i + 152*t]] + R[C[i + 152*t]]; __syncthreads(); R[i + 250*t] = Op[i + 153*t] ? R[B[i + 153*t]] * R[C[i + 153*t]] : R[B[i + 153*t]] + R[C[i + 153*t]]; __syncthreads(); R[i + 251*t] = Op[i + 154*t] ? R[B[i + 154*t]] * R[C[i + 154*t]] : R[B[i + 154*t]] + R[C[i + 154*t]]; __syncthreads(); R[i + 252*t] = Op[i + 155*t] ? R[B[i + 155*t]] * R[C[i + 155*t]] : R[B[i + 155*t]] + R[C[i + 155*t]]; __syncthreads(); R[i + 253*t] = Op[i + 156*t] ? R[B[i + 156*t]] * R[C[i + 156*t]] : R[B[i + 156*t]] + R[C[i + 156*t]]; if (i==0) { final += R[253*t]; } __syncthreads(); } if (i==0) { A[0]= final;} }
74702ac4b258ecd4d87a2e968b94c7d9f9577103.cu
float h_A[]= { 0.714899237705168, 0.6593403492402452, 0.8200921920091004, 0.9453533202697284, 0.5522293587559314, 0.911488355650966, 0.6084328273759245, 0.6081693680408298, 0.9852630400453032, 0.9552435162269735, 0.7714556537320874, 0.893125822994568, 0.860835407553673, 0.6580368874559931, 0.8842885921552708, 0.7190619784527801, 0.7882722698016977, 0.6656379333000253, 0.9357561249147422, 0.8418344620101638, 0.7478591689725955, 0.8936638455948593, 0.8199023289899353, 0.6480275860499387, 0.6918710181332538, 0.957257686694877, 0.9896533928031612, 0.899464805150862, 0.933628059174556, 0.7802194673808598, 0.7180120788579141, 0.7001088917695673, 0.9846389829627131, 0.9335261679414126, 0.7891089669509961, 0.6920323513219662, 0.8372538068068409, 0.8262784216167918, 0.7594003365297706, 0.5817232948572205, 0.5308636056915322, 0.9717268845480704, 0.6168177198192328, 0.5261255208422979, 0.8354996870027067, 0.6313485414343811, 0.9396454597205113, 0.7709578667049983, 0.6280897526294885, 0.7903603343603801, 0.925954504579481, 0.7083630830802359, 0.8357339796535525, 0.534019588432888, 0.5119797738072018, 0.7728667479286768, 0.5281954764378672, 0.806846906243039, 0.9040260184287333, 0.6166848743159523, 0.5938194282251794, 0.8093009396743354, 0.8384950487844927, 0.9284209351465467, 0.8514262607452571, 0.8999592091442796, 0.8138785652259415, 0.7531482108463914, 0.9724259136764555, 0.9743884191877106, 0.8284500571666802, 0.7231979108297033, 0.9980085370102973, 0.7045189187157035, 0.8088190919993448, 0.5312498364247776, 0.8975216995204517, 0.8606825088359613, 0.869643791334979, 0.8921488183940844, 0.730567423848039, 0.5376339784064466, 0.5883775141302325, 0.5880074795492454, 0.5509298664515099, 0.5147798807327489, 0.5235937180516632, 0.6340500872623072, 0.8017164132600838, 0.8662938756188187, 0.5486619474749198, 0.6690091055092129, 0.626551384362175, 0.6938121021798289, 0.7561657803978581, 0.9725588029300659, 0.5134272225385352, 0.5024475806457671, 0.7327618801734921, 0.5481755412990239, 0.6730427146305296, 0.5869097722812583, 0.8751373530713528, 0.6884968207872837, 0.5295233343609733, 0.9092635863689065, 0.8569948983478097, 0.5748866839943527, 0.5301264666632447, 0.5330860289336155, 0.8221794288222217, 0.5911432769932973, 0.8945968988339126, 0.8888067368388528, 0.6483249846943383, 0.5334111450646923, 0.7708760786820803, 0.6673642269103457, 0.8692980696066652, 0.7234282380731747, 0.7878217535872505, 0.7684477288619345, 0.8781166045056072, 0.5409864805361351, 0.8710436103457933, 0.6049513096387487, 0.6692985137889889, 0.5866150606619355, 0.7064072480515011, 0.6174344251953949, 0.6030389504185693, 0.9955888433744646, 0.988088541152838, 0.6944172619618818, 0.8194627533999113, 0.678678424976046, 0.5769443485533892, 0.9605395043675642, 0.5243279077421619, 0.7969404186593336, 0.9826790121352584, 0.6734684994846307, 0.6915359726040123, 0.5599063898812213, 0.6310519258067021, 0.9544227292039527, 0.8161394483017943, 0.5119326555245434, 0.9155175004524563, 0.8795559567537004, 0.5247951787159624, 0.8881153608508414, 0.5833758509829722, 0.8316443186230482, 0.8113989227924175, 0.9244000317141048, 0.971306233799871, 0.922421602068382, 0.6815139490287734, 0.6702404325650719, 0.8998808360074478, 0.977975702477339, 0.5822350239302843, 0.8182573720323139, 0.552203577287874, 0.967072188226312, 0.5975915485116701, 0.5833555345238668, 0.6898032838835463, 0.6373851261192228, 0.9038056564126419, 0.9412314051502731, 0.7536901561836067, 0.7255196618410964, 0.6503097276041911, 0.9070690220972784, 0.9239482815877914, 0.9537711969022059, 0.9705096297935893, 0.5242138341317024, 0.7722253181124641, 0.9240838129722668, 0.5428206151729358, 0.5226365258208607, 0.9788553844760457, 0.9599167011294389, 0.6985366050744497, 0.9006775307634758, 0.7024064399731178, 0.6447591530613797, 0.9935124962772821, 0.5772732979308175, 0.7287128935286853, 0.6262950178391171, 0.5611406486932214, 0.8916997901780892, 0.8392771443256062, 0.5488533269123854, 0.7127002529099173, 0.9356370987977973, 0.6183737175811865, 0.7355702587931011, 0.5905247847392963, 0.9174645802674555, 0.9318144489330222, 0.8555117351291632, 0.7778563783139687, 0.5627287532732232, 0.7154658759167272, 0.7210336333762667, 0.829893316042666, 0.6907817904306046, 0.6054482014847642, 0.5108529961784989, 0.8849135061450457, 0.7830740983422007, 0.501019029088817, 0.7753900564249839, 0.6436817602797851, 0.5806123996160818, 0.8806068062082901, 0.7600965474257322, 0.5410228837313282, 0.9776069493730013, 0.5459742938064962, 0.7888928434340872, 0.8130793601278763, 0.6087725536384629, 0.6225128240226405, 0.5690274446605674, 0.8277102988016317, 0.5769291340186056, 0.7923307710897269, 0.7642161960151865, 0.9369003639291422, 0.9726811171798574, 0.5939810371503956, 0.809206226889726, 0.8790077654474049, 0.8280801100030807, 0.6592710417704546, 0.6307658958207889, 0.8205785690876383, 0.5470605695938913, 0.6346800029584398, 0.76365395465, 0.7735219009778354, 0.6575503924657982, 0.5899439753600312, 0.6259707599859079, 0.5373807945545741, 0.733226313004866, 0.5968375279635458, 0.9576227551070152, 0.5460084793065373, 0.7318161350184554, 0.8468201436796416, 0.532904438691469, 0.8978193631922068, 0.9413535540535227, 0.8738075664049618, 0.7233782483979383, 0.7639849426313691, 0.6544524590705578, 0.9955110638688266, 0.9736225235581263, 0.5772298983657422, 0.7054965414366082, 0.5243903070203558, 0.6548075381060549, 0.7788180879154296, 0.8750093825201364, 0.8269702207734297, 0.9235011877312231, 0.9229763075137061, 0.6237624121476577, 0.5607706532736418, 0.9909798308039458, 0.6909799098551241, 0.7263990659891462, 0.8980803954921195, 0.7158142164087887, 0.7196871862962309, 0.6001387982282831, 0.5884897876613996, 0.543629638658152, 0.5759723211632939, 0.7307176652842963, 0.6127889951549005, 0.7200889355665543, 0.884520185870739, 0.9024484847320571, 0.6908844075940646, 0.8989288011214942, 0.5592638945267838, 0.7412159771991746, 0.6373339574870374, 0.8968420503429578, 0.718054399322513, 0.9028092284240686, 0.9562782289356355, 0.6543610263094788, 0.6027857147699588, 0.8114401375992001, 0.8851495548678878, 0.6497652613438074, 0.5060997118118272, 0.8412658625963879, 0.8403133856877005, 0.8600068769682263, 0.9032229513983538, 0.9541919007733819, 0.6909085525910921, 0.6341321380378007, 0.5688976291010526, 0.8186433280578973, 0.7727738809620817, 0.6867237678369895, 0.7873490570434726, 0.5423021279749756, 0.8556063503832361, 0.8285718137056327, 0.5633738652013544, 0.679216260183704, 0.7156197319516586, 0.8789325826480099, 0.6253535009118849, 0.8007703751311753, 0.6046952411629385, 0.6683611825600259, 0.7330645040912231, 0.8715188373168332, 0.7409504877200004, 0.5452631776333947, 0.9762881605137783, 0.7805276151202554, 0.6379783392576039, 0.6664836641469523, 0.8834197345013293, 0.5037707311951245, 0.958120119115252, 0.5715436775365879, 0.7652424769249608, 0.8596961560884122, 0.8737891894094488, 0.765269146568656, 0.7015752280605909, 0.5371062863302415, 0.8635776837800367, 0.6645048189287229, 0.7125012956816683, 0.7796393953098166, 0.8854705863060834, 0.8951929174646185, 0.9653909502196992, 0.5923169214307802, 0.8319387232147146, 0.514282434393756, 0.7093383924316943, 0.5644075977510082, 0.9007494796339282, 0.837330400867986, 0.8731099133856018, 0.787336129822629, 0.805222386947527, 0.5469156350556642, 0.5063813677423787, 0.8127489540553137, 0.8445148521063401, 0.5587181361825462, 0.7935152965990917, 0.9099370330192247, 0.6685294280035562, 0.5325658993910349, 0.5682328719765312, 0.6565344334138221, 0.875891991693905, 0.8434818323219142, 0.8131837013762584, 0.957040505535927, 0.5525338150777055, 0.6707510797538001, 0.8161081114325226, 0.7904721719727048, 0.6065342222197316, 0.8926907984274886, 0.7454221202689533, 0.551696987170168, 0.702449812101543, 0.7262913157253231, 0.6902993315094016, 0.5948188987161848, 0.5371305178182884, 0.7061519379368606, 0.605212101259724, 0.7115243043985338, 0.8317604472154732, 0.8868120877945399, 0.6891562880683273, 0.6650398989424056, 0.5434370945999463, 0.8576784475156061, 0.8167072978783985, 0.6205046545781528, 0.509535139706685, 0.5207317539185301, 0.7047105406619496, 0.8734501759355237, 0.8720570004131034, 0.7717490705287743, 0.9436550978797907, 0.6363301883244781, 0.5733934335161759, 0.73562891556836, 0.791415464345583, 0.9542255175565082, 0.9940546721130015, 0.6277241852897406, 0.6015777977893709, 0.9399795451772848, 0.6782116652330491, 0.756403886735032, 0.859357450769002, 0.9228915187886892, 0.5240994350652488, 0.6065635208403455, 0.7848130303441482, 0.9171852030307641, 0.8372517283880556, 0.708514912461105, 0.6104357060356547, 0.5187071027257043, 0.5699203863024365, 0.7694991667229549, 0.6911137200990399, 0.5328375998963639, 0.7440280003973498, 0.8765018807983846, 0.9694893251727895, 0.9801356837198116, 0.5556311584142242, 0.5562671402413085, 0.9587691253954582, 0.9533140880295825, 0.8599154249907996, 0.891113291026211, 0.7007975525972432, 0.581037305287438, 0.7665215069007576, 0.5319217782622715, 0.8733949876893567, 0.7639977671754576, 0.6460226390857654, 0.8483497795114068, 0.9540304010031364, 0.8234342758162617, 0.7467893058034248, 0.9285455683371808, 0.6354960850498901, 0.5641110887937977, 0.9074485995130179, 0.9610537114644087, 0.7057832692714137, 0.7480798299941793, 0.7883978912494558, 0.8142421344059176, 0.6470122628936739, 0.8545119022834793, 0.8263256318998567, 0.8937010840643602, 0.6534751789705421, 0.8538826009990672, 0.8649203307780579, 0.7916708344214441, 0.7112655502722134, 0.7834104491842493, 0.8174932111663565, 0.5443012667665321, 0.5704987489722957, 0.562300002328861, 0.5727093255741714, 0.5264743165816661, 0.5681939334307726, 0.5890514638977031, 0.5478511128226372, 0.588484537454118, 0.8170449834131122, 0.5473490133405773, 0.5195769488205446, 0.5969588466893039, 0.6737300386527724, 0.5939499395266987, 0.5195431464231615, 0.5221974588155305, 0.5023152187484748, 0.5633983418211452, 0.8562291455603068, 0.9615220028453428, 0.6770915410678773, 0.74807601246659, 0.7345277540949653, 0.7262586947960892, 0.5287536691109124, 0.6284111831609029, 0.5165642568031459, 0.7592624345057368, 0.5897646758058119, 0.5118738438949171, 0.7692533580794759, 0.7268894457514261, 0.660279178520743, 0.7368153216256048, 0.6561940485543389, 0.961486700915714, 0.8544052467073004, 0.7062922066816391, 0.7379369012878284, 0.6195471938599522, 0.557123138446443, 0.939774629193501, 0.9248740688188111, 0.7805974139681242, 0.8203103312051463, 0.543107391251388, 0.7077911046205337, 0.7341558960876031, 0.6911913632341786, 0.9697654273470726, 0.5711365456293559, 0.5601831084086717, 0.9329817150218498, 0.6780957900252718, 0.6498983099187599, 0.7780138378245673, 0.7905590731360672, 0.9636347599207729, 0.9056962563478959, 0.9661958007857407, 0.87226526899735, 0.759138888566067, 0.6252901250784715, 0.739460715269842, 0.6239798826029042, 0.6000789350390112, 0.5109548603908938, 0.9942072599048892, 0.9081822701063889, 0.6923123816744274, 0.6646708931346561, 0.8162379521685574, 0.6380722150337172, 0.5402536592228362, 0.8104229198641574, 0.8343087274143453, 0.523424288606466, 0.840834850590032, 0.9892858561188753, 0.8752759493189619, 0.5349731267253144, 0.6195475549900122, 0.5863531763324414, 0.9723893551625384, 0.673143454585218, 0.8155358029154018, 0.7313237350635464, 0.9011691945442573, 0.7044788363627335, 0.5449437873103457, 0.7319459353512335, 0.8140903880580115, 0.9230198505857923, 0.8436510894389959, 0.7387585707544, 0.8111836155198344, 0.5642054342332932, 0.6314426774041031, 0.6200473089643925, 0.5568967373718732, 0.991361974741475, 0.5106011591595954, 0.8854292762217122, 0.530595836498597, 0.7648749697572337, 0.7775096723984414, 0.538250808402597, 0.7280276212111796, 0.5412664427602116, 0.6523585242131706, 0.8912183290802129, 0.93477350277957, 0.8032222348025597, 0.813829336281096, 0.7850414386623239, 0.6935285656162133, 0.692220524762279, 0.7051676293064348, 0.784187987218747, 0.9298892282935483, 0.974698890630403, 0.7589030582342469, 0.6332820677811835, 0.5798125228358056, 0.7834821774956127, 0.5824733641736499, 0.6550281033777312, 0.9919900196023808, 0.6789412432856097, 0.9219106317941272, 0.8798391382129259, 0.862603745948723, 0.7729761467393494, 0.6508402448099077, 0.7532891009760978, 0.9190128479175159, 0.7927144234522598, 0.9971939705620985, 0.6050598707256221, 0.8317531082861415, 0.9098724789911716, 0.5389072911949471, 0.9918197892117087, 0.9386103137044246, 0.7117883833745796, 0.5450774013244541, 0.7337293524824782, 0.6265777040844696, 0.8781068478718252, 0.8716937277115359, 0.5330652289927765, 0.909832921002632, 0.6495011256950489, 0.8566561978490865, 0.5525675468907973, 0.5668606939478587, 0.578998732846387, 0.8907421164824845, 0.8018467276858705, 0.9062221336179044, 0.6012489173395115, 0.5726381753560553, 0.8909258704822918, 0.830828997772137, 0.9348819842742015, 0.9488940158871031, 0.6030904469361291, 0.6891939822276341, 0.7598049046404163, 0.9566278257051317, 0.5724432427357613, 0.54445557843665, 0.5869346477763179, 0.703563913038736, 0.765422625655241, 0.7899135625518942, 0.9710139792958583, 0.8139838169190722, 0.5465981092096999, 0.764382878581779, 0.66943327241418, 0.7481682386909418, 0.6952402822921546, 0.9219502415152925, 0.9529747761499425, 0.6425030572238957, 0.66444885563488, 0.564837605073613, 0.5613855238963441, 0.6807413025073243, 0.5955944858575498, 0.9134393358886727, 0.5911961624810709, 0.693548262237623, 0.8367084534211953, 0.9411943413993216, 0.9186756239888745, 0.8650442190945391, 0.8096903019044159, 0.9607325671629684, 0.8942198042555074, 0.807980724626685, 0.6903194740630678, 0.6767510519602763, 0.807815540638659, 0.9078717616399886, 0.782910644242343, 0.9235349134709511, 0.5827662586169281, 0.9470239953491264, 0.8590842676132524, 0.5939794577015103, 0.9876294260130768, 0.5871791766326104, 0.8411338986016971, 0.5726129762095099, 0.5789282343090258, 0.5795202592716739, 0.5427223785377429, 0.848716796802117, 0.7044566072982174, 0.8368948416016131, 0.6392099290566813, 0.9889960018990578, 0.5695200896349948, 0.6235788508806733, 0.9657585705976398, 0.5664392054725076, 0.5255184677399329, 0.6888541450993906, 0.7720755972747267, 0.9724955128047557, 0.780176422235112, 0.7579203274133928, 0.5505057797746056, 0.9251359151826174, 0.8919755655618635, 0.6276698636196372, 0.9617549714725198, 0.7413785793021042, 0.8572009817696027, 0.5303609377672771, 0.7455581493809011, 0.682905979771071, 0.8864139741175463, 0.7675721171259957, 0.5134694086153817, 0.5104560611066875, 0.7534929727414377, 0.8811267814699522, 0.9549519511632123, 0.9125111870324931, 0.8402406819222059, 0.9686418706160229, 0.6721637457934657, 0.8900863348423029, 0.5809693611978983, 0.7706564243018375, 0.7535196657143783, 0.5655518417133625, 0.8632540477794817, 0.6237253907538303, 0.5235537906020364, 0.9079835428848342, 0.5161664290898977, 0.7827906088548443, 0.8457223981764126, 0.7561076309830225, 0.7200744157533459, 0.55264061667909, 0.798063132885956, 0.5307963059082288, 0.8048928349247049, 0.6178979807612097, 0.5685293417227364, 0.614024763742988, 0.9319050643931062, 0.6472319169525976, 0.549893894860735, 0.8334537922246017, 0.5301294859779133, 0.7036475590597746, 0.9381193195427198, 0.7033594020467008, 0.7185124231051246, 0.5087656899224133, 0.7515433439643251, 0.6335023934066711, 0.9502628604083856, 0.8383348007202307, 0.5501444946181654, 0.7159386398506163, 0.9296945028972425, 0.500483162623564, 0.5665402709425185, 0.6354245403594752, 0.7366110460178619, 0.8950806044581257, 0.6841529995003168, 0.798177034885608, 0.502837044774656, 0.5671804848769749, 0.5358571931904212, 0.6772239244428808, 0.868503602696236, 0.5230202083443396, 0.6598911952145408, 0.8624209028876779, 0.5766540627820045, 0.8097515142206597, 0.7822304611890237, 0.7692478750370071, 0.9473958742825763, 0.9584906659810823, 0.8495720619881241, 0.5914480554617187, 0.7568183262472455, 0.582183441975346, 0.6148975449624521, 0.5141616415768713, 0.7203114647529774, 0.5081514661893591, 0.8570889855115246, 0.8739931094696456, 0.5501752338486747, 0.9495004804805334, 0.7810733713034409, 0.6427883486470105, 0.8109524715383036, 0.6744951578424425, 0.9099481299748415, 0.5657323986245227, 0.8472508171022414, 0.6813595457804296, 0.8928115156495428, 0.8448648731692978, 0.7828337778894238, 0.9935621965112946, 0.6408110758702255, 0.7520084803803828, 0.644906930628748, 0.6023292539503734, 0.9222396281707188, 0.7718165123241905, 0.7013252689420441, 0.5941630112345334, 0.6957966073899595, 0.663304159675309, 0.7184497707585031, 0.9846575025305783, 0.9190990017206796, 0.8523543925425037, 0.7890317150842945, 0.8694198137231677, 0.63802512359019, 0.7225992566676784, 0.8242260866115121, 0.7938159984291284, 0.7117550091989064, 0.6608500609186267, 0.9860693720637639, 0.7325119634293508, 0.5696608005770114, 0.8604168361181119, 0.8556759568825537, 0.8479435160812658, 0.9896704019898664, 0.9841054791164934, 0.6391377994071699, 0.7858689686512776, 0.7204037565239714, 0.9958312374347824, 0.8852621340542617, 0.856883440148613, 0.6717923352678983, 0.5834614132464107, 0.7329109727195666, 0.8274537398464987, 0.6358844853028955, 0.5365783081388151, 0.8808955299964034, 0.9692693154709209, 0.943340140000064, 0.903785170180891, 0.5091186025335808, 0.9995488680986733, 0.7326760443444389, 0.6738152273744389, 0.5813275434490868, 0.8503018909001137, 0.5818015738209762, 0.6419578390385787, 0.8601416126932446, 0.9125592332170969, 0.9710800993580571, 0.963518881509527, 0.8099505089885708, 0.7079732766594808, 0.7580140312570196, 0.8229125339508798, 0.9935080354380305, 0.5316645912203887, 0.7364563786531991, 0.6948311352333294, 0.8115931563991554, 0.7169197721341644, 0.648658248093623, 0.8881480776717243, 0.8136200877355975, 0.8417608044057672, 0.6889934629160765, 0.8803212583260943, 0.9949030265274985, 0.7710474552277471, 0.6190169203857003, 0.6662557927603043, 0.7417858635295068, 0.6095648336885604, 0.8678463207101997, 0.6419893888079617, 0.6905344882021733, 0.9503518675280267, 0.8719467726767451, 0.8293262797257053, 0.8050915383501369, 0.6317715548525393, 0.5368675894542442, 0.514869111192901, 0.8459887105925784, 0.9085545158262233, 0.9344696380855234, 0.5641573773207562, 0.992083601027581, 0.5009808830822562, 0.8155845460965072, 0.5623545146251705, 0.7534304839758319, 0.6449293148723123, 0.9033773631967212, 0.9233719295038589, 0.9722245887680585, 0.9053553288579095, 0.5180050259293034, 0.5243396083312116, 0.8373455819595571, 0.9766203116656029, 0.7707451726107644, 0.7612857119103602, 0.5295396938042125, 0.6065449178331757, 0.5434819670507746, 0.5064813685900201, 0.5095551673723235, 0.8542107122806689, 0.5468194901986756, 0.693421100319, 0.6589585544053436, 0.5207416983374547, 0.9101756411911455, 0.9985937277017491, 0.7542832173759411, 0.9168668968117992, 0.8800996584039971, 0.9509279006760335, 0.8384263417753841, 0.9792537626994404, 0.7204342789272195, 0.8123164392834459, 0.5935647423695357, 0.8640389314644732, 0.8604348018605885, 0.9843255565643754, 0.5418681933225657, 0.9128127832684534, 0.8386906644148151, 0.622569676016947, 0.6866383474631415, 0.7956549706673264, 0.8028055988535624, 0.5792058358310403, 0.5238470473003998, 0.6705974426602423, 0.9183361303494078, 0.9273858354645896, 0.814532814899667, 0.7043712728744094, 0.736043961261097, 0.9281961695762453, 0.9671066754591742, 0.8971867030592273, 0.9173427396161358, 0.6911738119575463, 0.9557114065595523, 0.959550268588818, 0.6092420394088642, 0.9181791424116117, 0.6963463190121704, 0.7608386323626333, 0.5438589523701818, 0.7774548101880385, 0.9358002968117705, 0.6415612339733465, 0.6288123992555512, 0.9850116354988165, 0.7983252202635746, 0.7788506729759543, 0.7892876526053931, 0.6410306156126776, 0.9888888991487497, 0.5687850402815366, 0.9340789379279841, 0.570482324642928, 0.6237570455946669, 0.9408174328244927, 0.7603764259604455, 0.8363761846284126, 0.5638257367037884, 0.5990168554691719, 0.6910825224590166, 0.8966386715906715, 0.5296444671815281, 0.9835969306041905, 0.8026963059044673, 0.9965008472598088, 0.5280685388596, 0.9958978376504297, 0.6120676991252856, 0.9925640405026861, 0.5743030840877522, 0.5787614774796664, 0.7839437845127978, 0.6577305713639505, 0.8759129899379714, 0.8442670700452837, 0.9977143584609288, 0.5630843774352265, 0.8824618172972648, 0.7353117677158472, 0.5549342971016928, 0.8625127818377221, 0.6287908447188745, 0.8603241101590813, 0.8019860751931377, 0.9519817327215931, 0.929150234953081, 0.5233695910619802, 0.6092652323361047, 0.6268761674186525, 0.6013146909835099, 0.5343859354849183, 0.550035128040716, 0.5186477881473128, 0.8447116193226469, 0.6744573406745742, 0.8341525026583503, 0.6002990275827004, 0.8159316160174189, 0.5047789342121152, 0.9582888281251125, 0.889215943020931, 0.5092441443413397, 0.522753284148985, 0.8585905597087496, 0.9851500099620788, 0.6780260121755493, 0.5347311611180499, 0.7864154673693363, 0.6908097478716986, 0.6260923409143679, 0.7439043954621911, 0.5418376147557895, 0.8495002019989841, 0.7211542724202722, 0.720067855664281, 0.711392080678194, 0.5729288454367311, 0.9314208398493533, 0.642001703877979, 0.5996095617981001, 0.5546545226700801, 0.9181718216259855, 0.7388686621607143, 0.9985736608089562, 0.938139825226662, 0.9278300791764933, 0.9302073444311413, 0.6242953718185338, 0.8493128752089243, 0.9731123703482502, 0.6703890534103323, 0.681432428423985, 0.8269486520062607, 0.7781590666878319, 0.6892780696821976, 0.8996840406353698, 0.6711511718388123, 0.8962841844109882, 0.6337230333548112, 0.9162195762723906, 0.9896044983779647, 0.7773066409266807, 0.6704520185595724, 0.7249714368703324, 0.9194587932164057, 0.5023898378154985, 0.6817130584848259, 0.6445306337899981, 0.6223165144277016, 0.8963534716863251, 0.8120159764275696, 0.8089046140403061, 0.8119470242556391, 0.885104455521087, 0.664963571463228, 0.5137450324664875, 0.5409572760196888, 0.779186547352593, 0.6762678670182992, 0.6686918499459775, 0.5114896437127547, 0.943799645391411, 0.5517218527393111, 0.776565186013888, 0.7225434352247616, 0.9610915747088796, 0.6151661475115529, 0.6566178828585421, 0.6636161615603928, 0.6616715075411022, 0.7673040771537916, 0.6251741948916316, 0.6353733585567626, 0.628527323895766, 0.8882156113604631, 0.6890686350094752, 0.6351766863817383, 0.6504643458096129, 0.9768584842858181, 0.7747129320005545, 0.5606099869717298, 0.8314439512235265, 0.6806617676660154, 0.7293552489596655, 0.8839844966137886, 0.7676463915674501, 0.7008335159857427, 0.5104238327691768, 0.8907526891071789, 0.5643686777342827, 0.8490682497162244, 0.9532995317734415, 0.5616124473583757, 0.808186986345413, 0.5347020017714006, 0.6786425551486783, 0.6195953722597805, 0.9479173187959913, 0.5809844502623445, 0.7061942711615923, 0.6576957502509282, 0.9577727879364519, 0.609228753606329, 0.9524956649537517, 0.9693179426086884, 0.5407479062226326, 0.829911760006663, 0.7506590403484825, 0.5963550129977244, 0.7098068835790232, 0.8689724847350547, 0.8534287549858192, 0.6165148755921164, 0.6401174262569596, 0.6957297999880339, 0.8846311647286043, 0.6030721755322559, 0.923238727000741, 0.7797086248141958, 0.8250850096700526, 0.8712542455502057, 0.8499084837356693, 0.5596005251095597, 0.5129036158294911, 0.805512391043429, 0.5544282363239954, 0.7293694447134789, 0.5335436495748754, 0.9642443631737312, 0.7538963368894713, 0.852047287083169, 0.687029104604705, 0.6177032453578496, 0.7931769735532612, 0.5782279068650884, 0.9010304986235913, 0.943508606858491, 0.5414785481671529, 0.9871336449843631, 0.8462238063349006, 0.7685017910763983, 0.8063302826347762, 0.7061826253298751, 0.7504277167339306, 0.817124032162607, 0.8416104179494577, 0.7489064691716957, 0.8337231973657941, 0.9161079897919806, 0.9014897266348425, 0.5912596516017559, 0.7809145446490497, 0.6131195035555357, 0.9685944279325427, 0.7211009123678227, 0.8836902484094888, 0.6940018560059542, 0.5867401299547919, 0.7904873610250666, 0.6867066867668838, 0.8815015498170606, 0.5014305250815501, 0.6389132289693962, 0.9546271395197699, 0.5191573498937255, 0.9715317094246325, 0.8504999191564628, 0.5489283507570941, 0.584729944188646, 0.9650568704745224, 0.9052786689654229, 0.8180460027485775, 0.7357220576684493, 0.8370012805201876, 0.6681277208325579, 0.9505437469774821, 0.6913354968959724, 0.5200083252548511, 0.9557017047892293, 0.7377705023371827, 0.9289826453581649, 0.8659492932730093, 0.6584336346862094, 0.9274016881525187, 0.6094148898044727, 0.7946455865244986, 0.8582962033373591, 0.7442612705729019, 0.7696381136846151, 0.7270895573123024, 0.5876391344045486, 0.9188503133546491, 0.6345438565824195, 0.5768528766085457, 0.9244329583578382, 0.9164677181201757, 0.8198664873252317, 0.7711903431628182, 0.6917565422472931, 0.7021516791659326, 0.8862179867996558, 0.7926089741012183, 0.628801874035622, 0.897529734023969, 0.7631220994515182, 0.5752065651424352, 0.8019544010813758, 0.7498519435719622, 0.878677807116468, 0.9099686412136768, 0.6495254449827226, 0.8317706057459928, 0.8467642374092617, 0.5124050582066125, 0.5186282639666125, 0.5162247890269265, 0.9088736167926997, 0.762797116514896, 0.8427167178642287, 0.5160962317016575, 0.6112543258605174, 0.9628410927240112, 0.597509847122122, 0.5920668830105134, 0.5999893775097818, 0.8031463253022338, 0.7489008915946778, 0.9576366952570203, 0.7330627846965758, 0.8213955799460855, 0.6095091088337574, 0.5168302150084956, 0.5820597976355589, 0.630051078110865, 0.7208606611397412, 0.9106371674557223, 0.8583519382477898, 0.8116920375659458, 0.9374527801414856, 0.956083353135863, 0.72998120185333, 0.6489496518442797, 0.7701633664691347, 0.7437282258952518, 0.8684160240872199, 0.6782327425657124, 0.5609015265329469, 0.7840570725191227, 0.7401726673550745, 0.9939679402406059, 0.8428050597653057, 0.6285391288449356, 0.9525523550249577, 0.7245200142579794, 0.8427555078558537, 0.9292276507484976, 0.806770733806882, 0.6675726232899162, 0.6369665345614437, 0.5109058634108643, 0.6447965531257694, 0.5396864467457, 0.5244341748339532, 0.69697885960669, 0.8015139580253169, 0.6625834751865, 0.5296664619535063, 0.7480719310015472, 0.7583603357347073, 0.8902408106172301, 0.6264941805609019, 0.8750471258719341, 0.9860714471043267, 0.6622214606383849, 0.74077104617175, 0.9392160341422497, 0.52123905659883, 0.799803777207009, 0.7542227224634717, 0.7015991923341619, 0.6745916455846831, 0.6090065971247887, 0.643455107728671, 0.8238403526156649, 0.5729818044183512, 0.8667251293410434, 0.8064405388888514, 0.8672955544535296, 0.929190801975835, 0.9577870147515954, 0.5966201911276999, 0.7113779860165379, 0.7893430203092717, 0.6487794671215054, 0.7264422218586388, 0.7705471058365967, 0.6042753828117917, 0.5488067488308401, 0.8787982192929122, 0.9313015390736115, 0.720808400627172, 0.9717199183209253, 0.688212024819574, 0.5516452935407065, 0.938095637560733, 0.9992472993034112, 0.6874819380931972, 0.7028171548838387, 0.9943468859867792, 0.9259098723793173, 0.5834754263238795, 0.5941462984060523, 0.8914090414830754, 0.6003124424414688, 0.9923591274464963, 0.6533175249704222, 0.7350077984127374, 0.7379595794015494, 0.575755852353445, 0.7897328533514314, 0.6151332713320594, 0.9272165155202021, 0.7991995960756262, 0.9716674947822159, 0.5546038296844353, 0.792908613673254, 0.935710263660041, 0.7349848374022494, 0.5596725039122803, 0.9361611827547351, 0.6634999735065663, 0.7861468418237738, 0.7888882042940063, 0.8430562618096415, 0.9036731783931962, 0.6472213041678718, 0.5244572172299351, 0.5920373125046093, 0.5073957476323756, 0.5280367089418969, 0.9961337428785373, 0.9536468839520664, 0.6636269798552731, 0.8365366461939714, 0.7868881194147763, 0.8593804582581533, 0.8072796801492867, 0.8278788695197186, 0.8192336213892765, 0.5876548625470283, 0.5742056815307378, 0.8660512454503853, 0.9754952712545373, 0.5188648803554188, 0.9110024614941881, 0.916456465018583, 0.8094395118376327, 0.7144298239964624, 0.8725437640354723, 0.7710582072881818, 0.6999662412155296, 0.5855077245976706, 0.7602793160811296, 0.9693940357590372, 0.9973307305499417, 0.8864938929639585, 0.6156993999010254, 0.6265524355891801, 0.7309322563268539, 0.6348231023408648, 0.9723856632835176, 0.6854418491295867, 0.9911315982800987, 0.6119848536492785, 0.5682034441053972, 0.5625732595378967, 0.8805467671398628, 0.8396580020783695, 0.5468118580641375, 0.7675240976154468, 0.5867008348509065, 0.5272339365430552, 0.9170970867119437, 0.7225833240488155, 0.9330899948606803, 0.5135873433399177, 0.5939122396553949, 0.6906957476095247, 0.9607317807488538, 0.6919965510327051, 0.769545510549152, 0.7286416336207603, 0.7726159681456106, 0.8573848332057681, 0.7936834193228975, 0.8937796822230413, 0.963401924514188, 0.8120226735569775, 0.9560630617163759, 0.6654914154380734, 0.9002419693951857, 0.6244109068604935, 0.7082182411360969, 0.5916262926112537, 0.7217034518612253, 0.9454356491933378, 0.725640878965292, 0.8577928864712285, 0.6684209091711908, 0.8130957845675291, 0.8512776362772251, 0.8828078866629909, 0.9879298679279742, 0.8259812818360481, 0.6669431024388734, 0.7241581854512833, 0.7629956383688419, 0.6738888410012275, 0.7969342003028033, 0.845608748341045, 0.9257573045070762, 0.8158095302513027, 0.8274920225204306, 0.6171749213338311, 0.5380250079784248, 0.5651703266744204, 0.5956938686163428, 0.7716476787179425, 0.5393274807297179, 0.6904593591498682, 0.7293030362607404, 0.5388881249837228, 0.9163724608366519, 0.8949668271344372, 0.6347818418808666, 0.9937358556584308, 0.6325918236146499, 0.6260613572803713, 0.6975344573257027, 0.748857158919509, 0.9962503917560738, 0.8488907191438116, 0.9229532253096726, 0.539779848226567, 0.511252249289505, 0.5904138628804119, 0.7214165786443512, 0.5910124723110795, 0.6660936865126847, 0.5448169456244305, 0.9446774020638611, 0.9676012860751123, 0.6429014773253252, 0.8497101542057285, 0.9935408900407224, 0.6062008922111344, 0.7751845422903718, 0.9283645345462738, 0.8185597929361603, 0.5677214927454797, 0.595606820034031, 0.9924306478217967, 0.88344471358283, 0.7370333303593328, 0.6938448657440432, 0.8944284618243459, 0.6563129764014879, 0.9132957964920958, 0.6715824332919551, 0.7131609632199478, 0.5675186165542083, 0.6993878090153459, 0.913103962334672, 0.8407653253003909, 0.69912646732845, 0.7770139705164247, 0.9552839157835878, 0.8691885467803161, 0.6293413920923085, 0.8388835072633141, 0.5998594464991525, 0.968134564846439, 0.8951548267731255, 0.8252174201121384, 0.6160138261439643, 0.7773566309062205, 0.6916507573864236, 0.5807535339925275, 0.9258010798652971, 0.7656931994811436, 0.5658469372350063, 0.9737903878229381, 0.668481586638406, 0.8397331840170059, 0.7666673533290878, 0.7887155294125846, 0.9018985231749859, 0.7251929644112718, 0.8844965089821621, 0.9677352083136705, 0.8710441261396651, 0.9592715738667945, 0.5645512000219833, 0.9559603690274174, 0.9271176725368131, 0.7165294968376588, 0.8282268778104105, 0.9510795559565053, 0.9280273696488505, 0.5240366536440538, 0.9312836512556888, 0.6676942944227335, 0.8551110159945744, 0.7448348582834579, 0.5627373997430986, 0.8038616595583132, 0.6831127679353685, 0.518689376266759, 0.7901056764893017, 0.5419234593002746, 0.5833245401875009, 0.8628305046701199, 0.8950565446225467, 0.5646754445481028, 0.690547275065224, 0.6979013844372711, 0.5184591468427029, 0.9079318892014414, 0.7514291772834929, 0.7335120297246609, 0.9951521308907829, 0.8719688428797499, 0.827071226089379, 0.7314254549385253, 0.7989991039558348, 0.5736868021273909, 0.5491986801214117, 0.6951736878194997, 0.7522730143223768, 0.634091807681439, 0.5210741463665289, 0.6762470284625738, 0.6314464641096101, 0.624268118223448, 0.7431250889619572, 0.7835422500190575, 0.7720536900523256, 0.9197980736324693, 0.9162472884597045, 0.710764055830452, 0.5964460739888114, 0.8597451236010295, 0.6485790544970058, 0.909987786106981, 0.5195793031016005, 0.6117422473988958, 0.5598955408832629, 0.7143063412222272, 0.5491761579054409, 0.6592463719554792, 0.6523369223179598, 0.5644039126230951, 0.5439254682853882, 0.6533854347394867, 0.8950114173398549, 0.9859915273208884, 0.9862603576061675, 0.7330059525563113, 0.5271303938884919, 0.7650068385973852, 0.7636638174446216, 0.8680512598160818, 0.6816302939950526, 0.8681961391731801, 0.6245189862363439, 0.6382248408832374, 0.6274854214410394, 0.78615844499789, 0.780574810551214, 0.605991182970578, 0.5105979840959562, 0.8647857587211185, 0.8152559692858743, 0.5441492722302703, 0.7762142325947545, 0.9203133024412911, 0.8378921348235446, 0.7466556345359071, 0.8387258673109992, 0.8568752996853403, 0.6842565684588178, 0.5010589179774196, 0.6634973600175997, 0.8909461889099035, 0.5366930437973239, 0.7618991050109198, 0.8174327957797372, 0.5777383748275439, 0.7094991869184164, 0.9161757219595784, 0.5972083815411466, 0.7151215173266364, 0.7994611119894826, 0.8661468048663687, 0.7921168535260954, 0.5619309355069639, 0.7276565536786832, 0.8027146078944928, 0.781992636316762, 0.7731199871646506, 0.9870157543487068, 0.8046192780732702, 0.6331394081004307, 0.792922232254657, 0.7697219337384742, 0.6772491532308755, 0.6063599009204489, 0.7609239121321716, 0.8094969240876992, 0.5390215336686695, 0.7171950888997553, 0.7102948440067917, 0.8544718279458058, 0.9804506285550212, 0.6698904118689688, 0.6022714707530368, 0.8635078379555414, 0.6241611902190143, 0.6919556734996597, 0.8491585269314992, 0.5865279184800638, 0.5584486087942411, 0.6512344649706066, 0.7297302995938235, 0.6534304755627002, 0.7797223889336591, 0.7634034450186772, 0.8513610822686428, 0.7297851016008245, 0.7025524667942226, 0.599181650395693, 0.6940331802178015, 0.8396675413858863, 0.7883884923200379, 0.8216871485067592, 0.6413258037109926, 0.7561548101705087, 0.6718339885069906, 0.9373217011787645, 0.6808350944882655, 0.8100742998634867, 0.6399486342874809, 0.583894631927244, 0.5491353628200171, 0.5645172429813654, 0.7651287805791276, 0.599873169242636, 0.7793522940548414, 0.599227328589752, 0.8363575545129591, 0.9099783695789619, 0.7275689653749082, 0.5035551349228342, 0.9188533975872, 0.8121769156113592, 0.5807966889476599, 0.8698657600446373, 0.8285221190982968, 0.5167710114517965, 0.5722406442775426, 0.7978612047943068, 0.7238620944038044, 0.6878837342263064, 0.6657158424168258, 0.9752079055888137, 0.6815722122679624, 0.8792810197304497, 0.812590227950509, 0.5343778876577465, 0.576072119955493, 0.8199214985156, 0.710135493272805, 0.5944500182827686, 0.9661489836577549, 0.8306285831984093, 0.6408892685572585, 0.7806776581804702, 0.5022406931343896, 0.5867553902553148, 0.9162617679355611, 0.9054606334547478, 0.8480994114910061, 0.6970790797767902, 0.5362439368907911, 0.8151813898936953, 0.7208735837919895, 0.965688973302806, 0.7066157954362717, 0.6217488671327662, 0.7052368289008206, 0.9475683950886093, 0.696150642070306, 0.6471552545772581, 0.979271893829913, 0.8004788743363986, 0.9561731785882099, 0.7627981044899421, 0.525527729127792, 0.8347980109562567, 0.9534845210388199, 0.6328148620025589, 0.8586345406606539, 0.9741790957973862, 0.8703705688273324, 0.7486300937175524, 0.8488304503748101, 0.882694291208169, 0.8013309391463747, 0.536014600121361, 0.7933767251708894, 0.8217323265829959, 0.8917495680719559, 0.8890198518173259, 0.5544551545154196, 0.6662193638078293, 0.7174125042075009, 0.7509755197603167, 0.9469480646316966, 0.5483721394554564, 0.6450425612716715, 0.8656043422024355, 0.9911092095731524, 0.8292057560877847, 0.8046713431308617, 0.8734708011996093, 0.8821888892105814, 0.5591682329189653, 0.800103009134664, 0.9824063752245151, 0.6984982846654548, 0.6210390335416234, 0.6005450303119111, 0.6393416192242121, 0.805819212843185, 0.8561172842015047, 0.8319970492474826, 0.9142957382616751, 0.8890855947038026, 0.6844006914822596, 0.5277545405650137, 0.5828764149464394, 0.7501584886038796, 0.697685180979452, 0.6475494378005215, 0.7039498860127644, 0.5100601747671356, 0.9463853153099666, 0.9980262451186144, 0.5841671521385732, 0.5824926165292171, 0.6866209932292969, 0.9823483056291338, 0.6667654175423576, 0.621980746034845, 0.9308541052336489, 0.7786547955097396, 0.6959226398135991, 0.6009460925277543, 0.827822422688217, 0.5844957613935857, 0.5445535496653859, 0.892250777390059, 0.7379171019731167, 0.672077928925549, 0.585902552786487, 0.5727757334038889, 0.9225604906491849, 0.899739241297127, 0.9012872283308504, 0.9139765023306405, 0.5038156166150285, 0.8881666679321782, 0.9126412699731634, 0.6588666404464645, 0.8469337466857467, 0.6819149423369797, 0.7933798097891029, 0.738740318526969, 0.8591372549355019, 0.9153760116160707, 0.8069490867632234, 0.8669894156959632, 0.9848904401012876, 0.958384733735133, 0.8475479758198083, 0.5873024134506228, 0.7498062005713406, 0.7629134105553143, 0.883038241925704, 0.5379952744607848, 0.5746128535446261, 0.6815854606121777, 0.9718081814278505, 0.5413317160948099, 0.8890661576030743, 0.5416714721577306, 0.9342827673368999, 0.6652018849769676, 0.7000785269601936, 0.7968261362664956, 0.9013034701887248, 0.8867064950721346, 0.8245795262404698, 0.8181170660935687, 0.8992156277816795, 0.6068532013169106, 0.8654253419311326, 0.7263403064576557, 0.9261714168778701, 0.9354698036133385, 0.686120933646905, 0.5410127489263319, 0.8013971732248947, 0.5113269701351297, 0.5036765402981079, 0.69158930658379, 0.5646962566455807, 0.734544031087879, 0.983982443244964, 0.9687735514862008, 0.7189298866677871, 0.9146733949144694, 0.7598272680038082, 0.7352929287871568, 0.8659886369486876, 0.7655569234245532, 0.9264135167868114, 0.669261743553765, 0.8789837421036524, 0.9107632654885331, 0.7412533636843797, 0.59161173342153, 0.6433152116150997, 0.8236058635859249, 0.6127666218137064, 0.6113564925151926, 0.8885098251085701, 0.6462442790416637, 0.5586198962238589, 0.9146460246062129, 0.8230612255798349, 0.7859020293935984, 0.524158930477432, 0.8834447772214882, 0.6776328686367403, 0.6438217000084501, 0.6372556400831897, 0.5063916525353342, 0.5218079863721143, 0.684314185281739, 0.8495854896209114, 0.7248363806723359, 0.9551268404370494, 0.6277288493469356, 0.7108246431032929, 0.741444276323528, 0.6963162263415921, 0.765080349437703, 0.8532156407484648, 0.6490237028397381, 0.589732217157203, 0.5890536655432388, 0.6923185261962007, 0.6049675032025355, 0.9912989307909565, 0.9630326581840074, 0.8537260202581473, 0.7256419090332591, 0.8426710836077372, 0.7379853307185479, 0.9457681789150698, 0.7868882785559809, 0.5515442811027342, 0.5041598503130009, 0.5636070426560051, 0.7872064641131906, 0.9941983995638923, 0.7832937533438307, 0.8547809575565962, 0.8916517892111142, 0.7994855974688095, 0.7694804318674131, 0.7472978264793337, 0.7399587569443988, 0.9807496193576837, 0.557441324762673, 0.9883988774474683, 0.5720895412284299, 0.5287639871916541, 0.8742894188222776, 0.6393191440175279, 0.9150961063210259, 0.8786276739925787, 0.524747526014729, 0.9509211611429347, 0.8435754875545265, 0.830702124405222, 0.9752423384995711, 0.6337106865090429, 0.7851354447833482, 0.5378513301783441, 0.6406872825277243, 0.701504909373259, 0.5228107025518314, 0.9873122003965533, 0.747169744522536, 0.5218591463250417, 0.8837166306110202, 0.98068840855556, 0.780367018430127, 0.735294690214955, 0.6020030504710734, 0.7363824900457989, 0.5580448658148587, 0.9436350510792096, 0.6470262963760548, 0.5866496283310255, 0.5910663739423464, 0.9111078845262204, 0.6696957923926545, 0.696926111654643, 0.8195097740389266, 0.5775860058368936, 0.928827208361773, 0.8967795153158356, 0.5656443498470952, 0.918988076697139, 0.8783283078295515, 0.6295252825108626, 0.5564140719360701, 0.9471230598171835, 0.6849586240459107, 0.9203802826105298, 0.733680782740266, 0.5954511175063653, 0.9592349751654639, 0.9010096457436851, 0.7905261511540287, 0.6811085477201726, 0.9702399728038549, 0.6476800362193159, 0.9619374125253238, 0.9138961076550465, 0.5763522314919106, 0.8216997423352113, 0.852762475756635, 0.7852397612288513, 0.7467508018554389, 0.9104389912111968, 0.6344296416832325, 0.9226655541754629, 0.7210154082138017, 0.748661153886045, 0.9980212037231943, 0.713065301590434, 0.6297720842568433, 0.9511566407955294, 0.5733221804690487, 0.7014302476922936, 0.9393524375453988, 0.9203337424909167, 0.6867581193575709, 0.7528867049829062, 0.5689517879551561, 0.7176554997350377, 0.638033140704825, 0.7115833139332732, 0.6460929360875789, 0.6088108933552638, 0.7048994845991923, 0.6789072571710413, 0.5977635026269235, 0.9739893965874362, 0.8153549701137928, 0.8784119704857629, 0.7593543274581349, 0.6313162235572616, 0.6511023264455079, 0.7282276156852976, 0.8815558372005745, 0.8005134697172771, 0.5067303262009564, 0.6091119811872098, 0.8040154331066245, 0.5538746941493178, 0.7229848051471328, 0.5836850023274596, 0.8062474425244877, 0.7835763070942219, 0.5388816234239331, 0.679457724389811, 0.5177085150421354, 0.7992351318160109, 0.8229242431624626, 0.6908803722955692, 0.7186629875945816, 0.9268983875438999, 0.9531332947297668, 0.8938210476604475, 0.8846663159628229, 0.6825436635490417, 0.8442234946857172, 0.584359576095792, 0.8113389924442447, 0.6366909311395518, 0.8913562942004112, 0.9599647269422442, 0.5742050554835412, 0.6806866250599198, 0.8654236893185705, 0.9063158062472891, 0.5162403155221338, 0.927712182208625, 0.7938843952410307, 0.8875481346405394, 0.6178973330217754, 0.670922798541411, 0.7607924954393745, 0.5256851731375056, 0.9008661300516236, 0.8275958234596146, 0.7784366118579691, 0.6250417167080273, 0.8917096632718988, 0.6947292320856919, 0.7081489758808067, 0.7330143762027135, 0.7608212169277919, 0.5427262823177115, 0.8041027177507576, 0.769978367128541, 0.939838423278582, 0.8128140082973834, 0.7290631316824194, 0.6497558409141396, 0.9218306908963319, 0.9978425824689737, 0.7386217657692493, 0.6318648720932452, 0.5385334328026687, 0.7197792847812345, 0.9688677556160177, 0.5497000285590489, 0.7704709391355744, 0.822052043528751, 0.7954420093661044, 0.8405026747128113, 0.5351163985541214, 0.6690102459834013, 0.5345359365328872, 0.9182831614943414, 0.6845258717492946, 0.9904541975133518, 0.8461512636038855, 0.91548675256085, 0.7910404033743956, 0.8907474796644095, 0.9098836631994618, 0.5558472069151348, 0.9008354569073646, 0.9452125177793486, 0.9187960298241067, 0.8522936881717069, 0.7786860438827113, 0.5073534704503333, 0.8536968388995947, 0.7681878208857315, 0.8193044389189108, 0.8529926936022905, 0.6994638504463768, 0.5990544573329741, 0.5961757250631056, 0.8259396854930081, 0.881326043862166, 0.8299511193751532, 0.9551196920458729, 0.9226769799030088, 0.6665214228252053, 0.627349301886063, 0.7254811837968725, 0.5755597208291244, 0.554926804036437, 0.7395218773051495, 0.8522633922426657, 0.9558032000340895, 0.5898847350875367, 0.8292412508995635, 0.5435907908156128, 0.6478661993511781, 0.9489959869708298, 0.599882735864635, 0.8916595194832422, 0.6280878290653957, 0.6019058839819713, 0.9149893973229243, 0.6432209845470267, 0.9585413323982771, 0.8464904188763751, 0.5066667154517372, 0.8979258684350054, 0.9287696728491261, 0.565886699497347, 0.8644032607566445, 0.9485545120242145, 0.8556560520373797, 0.5248329546683002, 0.5762217171024484, 0.8259761127928213, 0.6488941185220416, 0.6372334758583061, 0.5547965343938261, 0.927640672113132, 0.7190692689019063, 0.9197098830327819, 0.5395225754101882, 0.6903886613159107, 0.5925180432367259, 0.9993491823138161, 0.7995875900373022, 0.5275843633581634, 0.6946983924815886, 0.9553410641242313, 0.5011284735817164, 0.9550209622796704, 0.9346498380843731, 0.5059807174719009, 0.8508585258916732, 0.8751417909198644, 0.7715239013652754, 0.5619418418004074, 0.5637029976508683, 0.5052752859591054, 0.6689067738809151, 0.6556311238671824, 0.7943011928223319, 0.5380219916642031, 0.9332454916280256, 0.7666524277270956, 0.9891861887488069, 0.8357686930605959, 0.6840063654849842, 0.5601908815813466, 0.7226595568746665, 0.8401891807158768, 0.7657890700890996, 0.6716663081090741, 0.8751746503160951, 0.8456085100727943, 0.6854966228123682, 0.707280819090531, 0.871014530893987, 0.7361309387018821, 0.8741370783871225, 0.9041871488407436, 0.5830980634489187, 0.7685920838283593, 0.6966438328169506, 0.8517129917489175, 0.8974518767301445, 0.7827143966899989, 0.7812481186708073, 0.5555260042685757, 0.6953617976239921, 0.8689727506245951, 0.9855873456435942, 0.6554254444932807, 0.798974880545683, 0.7588316447630388, 0.8176525484580602, 0.8726925490593653, 0.5262852847764714, 0.6695980583225866, 0.8082154587061458, 0.7554297030355437, 0.604949654338603, 0.8760550215513803, 0.5111815309709001, 0.919430966363503, 0.8616328975250706, 0.8667505915666758, 0.8002990180499006, 0.6583156180211166, 0.9322373932646413, 0.6436662147709803, 0.9253065381216303, 0.698513003275815, 0.9156344240880248, 0.7721024971501115, 0.5411224947938844, 0.5627213370773958, 0.9887625190769178, 0.6559904821349188, 0.7422016772424557, 0.7512806616379337, 0.9313646230850063, 0.8746960484159758, 0.5958383186232634, 0.581215143414767, 0.6917575221480203, 0.8776757557808577, 0.7348509050495693, 0.723168102044947, 0.5648229009991051, 0.587251985627134, 0.9867462683499941, 0.5538139193283795, 0.884453555560877, 0.9111555152569993, 0.6585905114705954, 0.9323578235937253, 0.5137883210693897, 0.5656354624380188, 0.7994766482625246, 0.6262005670567451, 0.6895549533024236, 0.5904418233605073, 0.8982432376507175, 0.5380621619944117, 0.527772956078285, 0.8020671155128132, 0.9511480163937567, 0.8133899110778885, 0.5106019760094698, 0.8780756737304969, 0.593955378689644, 0.6696315403862183, 0.9673412189784524, 0.8132960655611392, 0.6868168064473854, 0.9988186007620561, 0.7151338760016147, 0.6165333985653724, 0.6148781767463899, 0.6437457303662573, 0.8554934549808595, 0.9517785513067553, 0.7465905513897415, 0.7033633568715387, 0.6365967169807905, 0.6877065634759141, 0.7049592218530623, 0.5141475746297162, 0.5212469942141784, 0.7793184464435123, 0.9943063007267385, 0.875718063685583, 0.7189501427969563, 0.6176835357793071, 0.8864124282735939, 0.5621384463966554, 0.6919699017224918, 0.6444630147105765, 0.6270938268957917, 0.8684460302664628, 0.8950577266646542, 0.6698876093286189, 0.7241676556745147, 0.5552939251615925, 0.9527816480765305, 0.5459400869142912, 0.7777547257605391, 0.9991399125248879, 0.96761618442605, 0.9636701985577933, 0.6601120487210879, 0.9641683561771175, 0.9474414322443836, 0.7662264121311466, 0.7752901126308185, 0.8893150406586601, 0.7312565805406572, 0.8111569935683272, 0.610149496180503, 0.6200661752583492, 0.6512884942787263, 0.8540537276860847, 0.8106756705660079, 0.5270456869176618, 0.9785004716236918, 0.6913159128310506, 0.8584909499471982, 0.879275822826052, 0.8785803594957815, 0.556815572032472, 0.8178872502910135, 0.6888646738988835, 0.722267382428827, 0.6554097206934727, 0.7728855177388922, 0.5661329705914648, 0.7811304124395257, 0.8144818399051921, 0.5039717301929464, 0.7591233998797704, 0.8622813865712369, 0.5216782882506699, 0.5319725653846628, 0.7829623094238922, 0.8628133207007391, 0.7188664926304662, 0.8820333014367008, 0.5482346999735859, 0.5573981290849068, 0.6133698374950027, 0.8483009243941059, 0.5238228625351367, 0.6490094841470233, 0.8943001205346285, 0.8160471508280497, 0.6619636108117993, 0.6874409796652305, 0.8754114975402945, 0.5876133075640722, 0.8853791065248688, 0.7307921226525265, 0.9497606791919082, 0.8711571824895598, 0.5236113951148316, 0.8947615538045877, 0.8902729366443208, 0.9657963578760187, 0.6869213412332742, 0.7901795993985996, 0.6278771226027099, 0.8069828328691495, 0.9895802739750197, 0.6851446355237567, 0.5454486652253747, 0.9345583936589845, 0.7221988968092966, 0.7215086271273896, 0.8343161851859152, 0.9234891762333359, 0.7988944104477524, 0.8817088644304385, 0.8952187894479038, 0.9424338543777471, 0.9604589928223863, 0.8921477594919998, 0.8147050632905937, 0.9988841966877864, 0.9561763765306532, 0.508492135519786, 0.5419297331527839, 0.8860458367483572, 0.8290439245702965, 0.7246188459550802, 0.5852222152397111, 0.9718472361334805, 0.5390255337682119, 0.8852675915299573, 0.6706710187102645, 0.5019000113903732, 0.8195062729988496, 0.8447413376019977, 0.909361262945422, 0.5139376892279612, 0.6536941060002501, 0.7632780797563077, 0.5080341568240043, 0.5016627834171469, 0.5096089285401327, 0.5550204525561513, 0.6436710568088067, 0.8272992241335898, 0.5143401462783689, 0.7739445820543964, 0.6332784506915129, 0.7280386899216216, 0.9515639145291733, 0.8347130420015796, 0.9069266378275059, 0.8842376017226574, 0.7611739266330171, 0.559976155389079, 0.636241425795268, 0.6975186511067719, 0.7094404943528261, 0.5370255440338947, 0.9879444395847193, 0.8301927577727868, 0.529230760720075, 0.5819874124541112, 0.7399436856263516, 0.9224045350498673, 0.6901549768312083, 0.884722702977976, 0.9516571565058183, 0.5752280682510047, 0.945115163986789, 0.8454456741141931, 0.6045332151954925, 0.5563106837922907, 0.8339268008012518, 0.6567959820813983, 0.5989808805741025, 0.8298199895532633, 0.6945232414890519, 0.9745908042982309, 0.8044605450397344, 0.7292027959782907, 0.6890546486636399, 0.5335646862987389, 0.5871944533800393, 0.7017368614725614, 0.8981045028748407, 0.9158334804254631, 0.6701782098094577, 0.6226562096516907, 0.8005316013661254, 0.5864715220692576, 0.8628244626985004, 0.8039286092623148, 0.7834414924892783, 0.5282976885529496, 0.5814073303593847, 0.8530094578929455, 0.8269169420464495, 0.956943116325168, 0.6903770609433271, 0.5692072714653094, 0.7862637184908701, 0.7745671951549695, 0.9551422956858756, 0.6353726699863571, 0.729719253775927, 0.8559987712159576, 0.8852722710621066, 0.5923658558757079, 0.8061356710509275, 0.8440822112990651, 0.6756907669585133, 0.6301220830340963, 0.9255779158279811, 0.8065802773273343, 0.7046619703210135, 0.8033709257365678, 0.7207733316988026, 0.9325078236241573, 0.8696303924653097, 0.7403608043429368, 0.6651964736698754, 0.9193025114275808, 0.6122882337366218, 0.6935015202655055, 0.6271978270493801, 0.7009517826769918, 0.9150674203349293, 0.559919519594272, 0.6556909726319895, 0.8856726775898626, 0.8593579240328877, 0.8514548130266462, 0.9259929269099144, 0.7022997019677053, 0.7637971309083929, 0.9608295548797241, 0.9463980310228786, 0.8007299022900745, 0.8572390913310195, 0.6924144896420531, 0.8070635084040423, 0.8395391357033006, 0.9638228158292851, 0.7646937580411437, 0.8170459041895657, 0.5768812097892213, 0.8890259000535177, 0.8115255865139972, 0.5726542148249195, 0.509760569016529, 0.5519721178654218, 0.8686837429984288, 0.7342187453266119, 0.5843671649086764, 0.8314750549308491, 0.7529781242250905, 0.8110604083926539, 0.6868666520075044, 0.5691548494910323, 0.9675583488376265, 0.8245484162885695, 0.8465865691900349, 0.6534046488565599, 0.5718209165693013, 0.8714951764168033, 0.6586432062810961, 0.7715071525074633, 0.6777276178360487, 0.6379979296517032, 0.9628505379900527, 0.5085892138167374, 0.7517712963491092, 0.9266814525422873, 0.5789181436961777, 0.5704187263247533, 0.7531053875400363, 0.651901829295329, 0.9383761828412741, 0.647349967123846, 0.878514273776501, 0.6431143491684492, 0.7878570024530865, 0.682510605795264, 0.7569295267143272, 0.9785666634047852, 0.6232460532106359, 0.798380732648091, 0.9523851893014157, 0.8771994584136081, 0.5311242762291697, 0.822048241211139, 0.6794105787086646, 0.6592402673111599, 0.6335799904508399, 0.8299922754108702, 0.6817115459493158, 0.6288997329286911, 0.5283390129221898, 0.7834367549245793, 0.554798476079644, 0.8276483286640942, 0.8567431846461706, 0.911077725004223, 0.8104666239786208, 0.8116615957499572, 0.9369946233714097, 0.5581005032929625, 0.6650042116470867, 0.6790526832294619, 0.6944824326369016, 0.8939656179690653, 0.6834801274293028, 0.8521478817121368, 0.6725173185021984, 0.5657526631827575, 0.9486016947159601, 0.5850315387331078, 0.6642266330793724, 0.5988748614076544, 0.862279247695971, 0.5389125264582335, 0.7089270062848948, 0.8520567817109088, 0.6102021541829146, 0.7233704298752504, 0.5486177387491498, 0.8450543033001401, 0.6683740957870818, 0.832565319043659, 0.6718491152148609, 0.6288220001123641, 0.6581573134878773, 0.7222412527562023, 0.9860498553264808, 0.847905765688942, 0.6398282116543879, 0.7055681725175833, 0.5657493952933211, 0.6924986386381363, 0.5188629058600711, 0.7886677856305165, 0.6883754394442632, 0.8261969855979385, 0.6864846145226358, 0.8852374286551025, 0.6836286118557866, 0.8240398407624024, 0.9686616037674447, 0.6290599591786452, 0.7064232062455634, 0.5997119553635993, 0.9850676754396017, 0.8966347071551272, 0.6174090200522672, 0.7912621509252105, 0.9006154810245859, 0.5441137913703333, 0.7740507063728765, 0.6515815345816421, 0.9795841297484837, 0.6393675401780996, 0.5464878191492984, 0.7404016496554302, 0.8458097358413831, 0.5406456105900412, 0.9845772327323519, 0.5342142954434275, 0.6857309671365868, 0.9037945393443209, 0.5279158274247269, 0.7997498112178214, 0.7305221170850222, 0.616952121602838, 0.663351106869364, 0.5458894406278841, 0.6491190398122905, 0.5721395799120923, 0.6490383555475341, 0.5520248604269463, 0.998075966781298, 0.856379386083638, 0.6752470248782052, 0.6166630140863523, 0.5993824873605494, 0.9271287105167694, 0.5061309270889605, 0.9323968782332008, 0.9489420415414042, 0.546099954814831, 0.5506645273184474, 0.60810075308103, 0.5581525347884699, 0.9755319670262546, 0.8306369794249525, 0.5905231920733909, 0.669178867376903, 0.9266701251216571, 0.9513102392358672, 0.9242030613633743, 0.9723142341115906, 0.864954868228141, 0.9274635881987505, 0.7876242035165437, 0.7724355247244437, 0.9679318819436273, 0.6607922205487333, 0.5043489756321935, 0.7054767068327341, 0.7993534872775875, 0.7303337185752314, 0.9686718060868105, 0.8226600861440976, 0.5940703458536198, 0.5550648127311428, 0.5913457801615225, 0.7052852189990526, 0.9973346252000339, 0.9731494821225994, 0.5809645028730465, 0.9981030813560232, 0.7447961551428338, 0.6196460535380237, 0.7389732842768741, 0.6690180032758544, 0.8833483096997412, 0.8994920742973032, 0.5761037043434152, 0.9778271335106139, 0.9454650467587675, 0.7773524474453942, 0.7864315277557645, 0.5837658755550487, 0.818209003004159, 0.5210644891551547, 0.7641649260066902, 0.6303090775006857, 0.7661067427744945, 0.7532947768726248, 0.891593108810594, 0.69394756684432, 0.6427082747129632, 0.912272931055324, 0.5360444736674863, 0.6368617113144304, 0.5337682668636958, 0.9033613424109327, 0.7269463465797934, 0.8490718986680277, 0.9022130873690355, 0.9373800196915142, 0.9851660166613465, 0.5483296785309326, 0.9109883320462633, 0.5576041684682771, 0.748239440480885, 0.5601662523181742, 0.7530709499435718, 0.9203169125256743, 0.8158179647552257, 0.9008044258442673, 0.9840788190244475, 0.8684858717683521, 0.560639324605837, 0.9114514721125768, 0.975828009810511, 0.8550062748408775, 0.8840880176834274, 0.9401227960807157, 0.5330505598519917, 0.923176108949981, 0.8686688032469669, 0.5051590028951186, 0.7775256176017014, 0.753868367136303, 0.5996937518792607, 0.6701294725260777, 0.6077691799880649, 0.6489642177703833, 0.8859033395443516, 0.5701466910608932, 0.5556115187453821, 0.56654881018169, 0.5189004876304872, 0.8999519818889956, 0.8712325959979794, 0.5544883667859892, 0.7913077151363805, 0.7609436553807664, 0.9228327986791957, 0.9198686502252624, 0.6429878929055679, 0.8529790835306297, 0.82227625175666, 0.5319553087977986, 0.9826722759390725, 0.5704975108915608, 0.7484765292580977, 0.5297703321295155, 0.9241277799775247, 0.8349818055147449, 0.560406473355895, 0.5154896507714026, 0.7873297371493972, 0.7796783540254791, 0.965229471595484, 0.5605857057494208, 0.5913683219264723, 0.7516048862451451, 0.5384895093647807, 0.8633209679039823, 0.7845450691301357, 0.7526948198465305, 0.8777672562184504, 0.5215995049081372, 0.6195114549447283, 0.6387073538558246, 0.8117801283966903, 0.9740289584065264, 0.7117752709247569, 0.5377388415719064, 0.769682697490365, 0.7637234438012286, 0.7155527929302705, 0.8484309190544188, 0.9952329994930759, 0.6130226451549751, 0.8175788201978433, 0.9853411973960007, 0.6989657051201134, 0.8587949644189437, 0.520441553044229, 0.5461468348649265, 0.9019045688204912, 0.6006188284080347, 0.6677208940268552, 0.7744141613502249, 0.8979636232395767, 0.7499909178189454, 0.7482435127861806, 0.5095606295464572, 0.8817343821329704, 0.7656561151481023, 0.924194894367997, 0.8170294509667368, 0.5875320966520463, 0.9758328937666145, 0.6941785936079121, 0.7452599806286011, 0.793523720782376, 0.9449590179079714, 0.7818910472299812, 0.9665242347510117, 0.7297771987434389, 0.5933534936949066, 0.5279756424482174, 0.832038462111307, 0.9574877593942056, 0.5837804405257377, 0.925687638681553, 0.8256324456480921, 0.5637878956339515, 0.6722544367749437, 0.8222123773904101, 0.6865297562974935, 0.7034203655168553, 0.8831951958592801, 0.8993673935652859, 0.7745568512844601, 0.5658775199316903, 0.8128429138806059, 0.9496867822292928, 0.9056945554214225, 0.7536505031954764, 0.7794676632190385, 0.5994270365980213, 0.834761813417077, 0.6497304237947086, 0.9355455719024095, 0.5981473858790562, 0.82670487793305, 0.9847343741016659, 0.8874090759768803, 0.7302835148685576, 0.8747581849204571, 0.9317838494990986, 0.9345501825756233, 0.5100352794010032, 0.8453245026956415, 0.8135485376292224, 0.6622197523572549, 0.5694087644774355, 0.6639676392264233, 0.9398636275476308, 0.7927823345592738, 0.7555326188911755, 0.5175488766908378, 0.7928710402103021, 0.5542657942625997, 0.7044169012305095, 0.673541599286023, 0.9685376620795068, 0.5798675667320858, 0.9531168752441297, 0.5404180581045651, 0.9906592789926909, 0.6755264380741195, 0.9375972057162008, 0.7641184140233664, 0.7108719167977762, 0.5715781102012256, 0.9424958204278631, 0.5154697805649899, 0.9042410220327675, 0.6345370807499439, 0.5031038005644697, 0.5826350109311278, 0.5990859919722579, 0.600366319698991, 0.976272553977257, 0.8835297742166435, 0.9560986353646075, 0.5511874315843093, 0.6503458303158559, 0.9129826612840517, 0.8754279833261807, 0.7510545471995456, 0.8775220727777293, 0.8753633833981764, 0.8839761041877362, 0.9649401191570197, 0.7852487955966825, 0.8921778815138381, 0.9463191987999844, 0.6488077196420212, 0.6443862811664766, 0.8084260213117536, 0.7679191962071905, 0.8236249278006709, 0.9817962173204126, 0.5975606331862418, 0.6008368603478962, 0.8138363189251467, 0.7233233900563066, 0.7353919053475703, 0.869332051159609, 0.7581305468741071, 0.6060587258509598, 0.7379928840989556, 0.6331653176583493, 0.913288389964592, 0.5020325745861691, 0.5952578557073789, 0.7083807855963361, 0.6029341020024335, 0.8913288181521697, 0.970710783012459, 0.691303703875128, 0.5339964597182413, 0.9257044661839762, 0.8818531421666058, 0.9170506093264437, 0.9542480621077407, 0.8426311915038207, 0.7579988888293772, 0.5716832142024207, 0.5348854696446717, 0.7536976930485931, 0.6652321110551535, 0.7675412715486223, 0.7956083013217798, 0.9688902732943552, 0.8255345878984273, 0.7321994961813205, 0.5978065534046699, 0.8895026484427127, 0.6816343131634011, 0.6602252523096136, 0.5725837878868212, 0.831430143645447, 0.5135029967183113, 0.5252364958033102, 0.7085703202698219, 0.6869304036314889, 0.5810960744556017, 0.9145153644655009, 0.9360453776488684, 0.778373473736486, 0.7580624711952287, 0.747036599468881, 0.9294976825686925, 0.9557677535738869, 0.8152458223112344, 0.9295840582591395, 0.5729462828875949, 0.6514688072164417, 0.6638555929586848, 0.7692485279197838, 0.9317412899663131, 0.5374209254539761, 0.7195050327921074, 0.8978815888193361, 0.9847411378784738, 0.9139605396620434, 0.5110783628037149, 0.6027358331712018, 0.7480360869872134, 0.6054558117991902, 0.7780270123566342, 0.5276892191232967, 0.9270135264125493, 0.5124741644370572, 0.7995169637930378, 0.9843469503293043, 0.7198012149029505, 0.5584843008195225, 0.7441378240775898, 0.6139993915210542, 0.5721464938836287, 0.7639972691619843, 0.7330278729543239, 0.5423962513240936, 0.673505879605496, 0.824708226688142, 0.9648435078801919, 0.6033214066193526, 0.9849156087977295, 0.8042145020753237, 0.6950919504592001, 0.938794570297276, 0.745136663495036, 0.8954818298437073, 0.983828057847121, 0.6779555128110781, 0.7164803948040415, 0.5328699997949621, 0.5004370193342161, 0.5344031978313286, 0.6129685905369864, 0.6834200584266079, 0.7316571803364245, 0.7468277636273281, 0.8286871620679506, 0.872630766150687, 0.8983540531747716, 0.9362847554956988, 0.9444999901492601, 0.8161305243031034, 0.9815809890693303, 0.5905912676728949, 0.9188518622395281, 0.9178960567827265, 0.7043634923717179, 0.6529575971109047, 0.7230784349232169, 0.7520441537905506, 0.9572518514162839, 0.7005283360936327, 0.8414767721687901, 0.904526434179546, 0.6351770479215483, 0.6517834175473565, 0.940655797125999, 0.8092032455542801, 0.6751216767047956, 0.8188867036898124, 0.7101708344753399, 0.8416267146340799, 0.8336018250679198, 0.747140112598854, 0.9832142567506252, 0.8862486546386923, 0.5682968644289447, 0.8520367769427892, 0.8371346141242875, 0.9490652139402893, 0.9611267117545785, 0.6620622312410167, 0.8519385067367697, 0.8810216521201433, 0.5474183120989145, 0.5254790771810849, 0.6635410269961696, 0.5710819144283839, 0.6430167493020873, 0.7716914837581712, 0.5107189366977285, 0.7969690228686757, 0.6324667328415825, 0.8095553526654666, 0.6817667465452782, 0.6033366625145742, 0.5160765822714144, 0.9852784023861291, 0.7873190326599171, 0.7586214439214225, 0.5472846294386365, 0.6513630131259305, 0.9553044073551993, 0.8859454613274835, 0.8029632846422625, 0.8692246723098846, 0.7234677591504683, 0.7849991360192445, 0.6082081912349202, 0.8816931819676047, 0.8309198060181453, 0.5889696288139146, 0.8793354798411956, 0.9346750731440692, 0.8253114887665259, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0, 50000.0}; int h_B[]= { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332, 334, 336, 338, 340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362, 364, 366, 368, 370, 372, 375, 377, 379, 381, 383, 385, 387, 389, 391, 393, 396, 398, 400, 402, 404, 406, 410, 412, 414, 416, 418, 420, 422, 424, 426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446, 448, 450, 452, 454, 456, 458, 460, 462, 464, 466, 468, 470, 472, 474, 476, 478, 480, 482, 484, 486, 488, 490, 492, 495, 497, 499, 501, 503, 505, 507, 509, 511, 513, 515, 517, 519, 521, 524, 526, 528, 530, 532, 534, 536, 538, 540, 542, 544, 546, 548, 550, 552, 554, 556, 558, 560, 562, 564, 566, 568, 570, 573, 575, 577, 579, 581, 583, 585, 587, 589, 591, 593, 595, 597, 599, 601, 603, 605, 607, 609, 611, 613, 615, 617, 619, 621, 623, 625, 627, 629, 631, 633, 635, 637, 639, 641, 643, 645, 647, 649, 651, 654, 656, 659, 661, 663, 665, 667, 669, 671, 673, 675, 677, 679, 681, 683, 685, 687, 689, 691, 693, 695, 697, 699, 701, 703, 705, 707, 709, 711, 713, 715, 717, 719, 721, 723, 725, 727, 729, 731, 733, 735, 737, 739, 741, 743, 745, 747, 749, 752, 754, 756, 758, 761, 763, 765, 767, 771, 773, 775, 777, 779, 781, 783, 785, 787, 789, 791, 793, 795, 797, 799, 801, 803, 805, 807, 809, 811, 813, 815, 817, 819, 821, 823, 825, 827, 829, 831, 833, 836, 838, 840, 842, 846, 848, 850, 852, 854, 856, 859, 861, 863, 865, 868, 870, 873, 875, 880, 882, 884, 886, 888, 890, 893, 895, 897, 899, 901, 903, 905, 907, 909, 911, 913, 915, 918, 920, 923, 925, 928, 930, 933, 935, 938, 940, 942, 944, 947, 949, 952, 954, 959, 961, 963, 965, 967, 969, 971, 973, 975, 977, 979, 981, 983, 985, 987, 989, 991, 993, 995, 997, 999, 1001, 1003, 1005, 1007, 1009, 1011, 1013, 1015, 1017, 1019, 1021, 1023, 1025, 1027, 1029, 1031, 1033, 1035, 1037, 1040, 1042, 1044, 1046, 1049, 1051, 1053, 1055, 1057, 1059, 1061, 1063, 1065, 1067, 1069, 1071, 1073, 1075, 1077, 1079, 1082, 1084, 1086, 1088, 1092, 1094, 1096, 1098, 1100, 1102, 1104, 1106, 1108, 1110, 1112, 1114, 1116, 1118, 1120, 1122, 1125, 1127, 1129, 1131, 1133, 1135, 1137, 1139, 1142, 1144, 1147, 1149, 1152, 1154, 1157, 1159, 1165, 1167, 1170, 1172, 1175, 1177, 1180, 1182, 1185, 1187, 1189, 1191, 1193, 1195, 1198, 1200, 1203, 1205, 1207, 1209, 1211, 1213, 1215, 1217, 1219, 1221, 1223, 1225, 1227, 1229, 1231, 1233, 1236, 1238, 1240, 1242, 1244, 1246, 1248, 1250, 1252, 1254, 1257, 1259, 1261, 1263, 1265, 1267, 1269, 1271, 1273, 1275, 1277, 1279, 1281, 1283, 1285, 1287, 1289, 1291, 1293, 1295, 1297, 1299, 1301, 1303, 1306, 1308, 1310, 1312, 1314, 1316, 1318, 1320, 1322, 1324, 1326, 1328, 1330, 1332, 1334, 1336, 1338, 1340, 1342, 1344, 1346, 1348, 1350, 1352, 1354, 1356, 1358, 1360, 1362, 1364, 1366, 1368, 1370, 1372, 1374, 1376, 1379, 1381, 1383, 1385, 1387, 1389, 1391, 1393, 1396, 1398, 1400, 1402, 1405, 1407, 1409, 1411, 1413, 1415, 1417, 1419, 1421, 1423, 1425, 1427, 1430, 1432, 1434, 1436, 1438, 1440, 1443, 1445, 1448, 1450, 1456, 1458, 1461, 1463, 1467, 1469, 1471, 1473, 1475, 1477, 1480, 1482, 1485, 1487, 1490, 1492, 1495, 1497, 1500, 1502, 1505, 1507, 1510, 1512, 1514, 1516, 1518, 1520, 1523, 1525, 1527, 1529, 1531, 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1547, 1549, 1551, 1553, 1555, 1557, 1560, 1562, 1564, 1566, 1569, 1571, 1574, 1576, 1581, 1583, 1586, 1588, 1591, 1593, 1596, 1598, 1601, 1603, 1606, 1608, 1611, 1613, 1616, 1618, 1621, 1623, 1626, 1628, 1631, 1633, 1156, 1156, 1164, 1162, 1161, 1164, 1162, 1161, 1590, 1455, 1453, 409, 408, 1590, 1455, 1453, 1455, 1453, 1578, 1573, 1585, 1504, 1509, 1455, 1453, 1455, 1453, 409, 408, 1455, 1453, 1455, 1453, 409, 408, 1453, 1455, 1455, 1453, 1504, 1509, 1479, 1489, 1479, 1489, 1635, 1630, 1585, 1455, 1453, 1455, 1453, 1465, 1460, 1455, 1453, 1455, 1453, 1465, 1460, 1455, 1453, 1455, 1453, 409, 408, 1455, 1453, 1455, 1453, 1455, 1453, 1455, 1453, 1378, 1504, 1509, 1504, 1509, 1378, 1504, 1509, 1504, 1509, 1578, 1573, 1585, 1590, 1578, 1573, 1585, 1590, 1630, 1635, 1635, 1630, 1578, 1573, 1585, 1590, 946, 958, 1455, 1453, 1489, 1489, 1504, 1509, 1504, 1509, 1578, 1573, 1578, 1573, 1585, 1590, 1578, 1573, 1578, 1573, 1585, 1590, 1580, 1305, 858, 858, 845, 845, 879, 879, 958, 946, 946, 958, 1141, 1141, 1091, 1091, 1164, 1162, 1164, 1162, 1164, 1162, 1164, 1162, 1455, 1453, 1479, 1479, 1479, 1489, 1479, 1489, 1455, 1453, 1305, 1509, 1509, 1504, 1504, 1455, 1453, 1455, 1453, 1455, 1453, 1455, 1453, 1635, 1630, 1635, 1630, 1580, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3104, 3106, 3108, 3110, 3112, 3114, 3116, 3118, 3120, 3122, 3124, 3126, 3128, 3130, 3132, 3134, 3136, 3138, 3140, 3142, 3144, 3146, 3148, 3150, 3152, 3154, 3156, 3158, 3160, 3162, 3164, 3166, 3168, 3170, 3172, 3174, 3176, 3178, 3180, 3182, 3184, 3186, 3188, 3190, 3192, 3194, 3196, 3198, 3200, 3202, 3204, 3206, 3208, 3210, 3212, 3214, 3216, 3218, 3220, 3222, 3224, 3226, 3228, 3230, 3232, 3234, 3236, 3238, 3240, 3242, 3244, 3246, 3248, 3250, 3252, 3254, 3256, 3258, 3260, 3262, 3264, 3266, 3268, 3270, 3272, 3274, 3276, 3278, 3280, 3282, 3284, 3286, 3288, 3290, 3292, 3294, 3296, 3298, 3300, 3302, 3304, 3306, 3308, 3310, 3312, 3314, 3316, 3318, 3320, 3322, 3324, 3326, 3328, 3330, 3332, 3334, 3336, 3338, 3340, 3342, 3344, 3346, 3348, 3350, 3352, 3354, 3356, 3358, 3360, 3362, 3364, 3366, 3368, 3370, 3372, 3374, 3376, 3378, 3380, 3382, 3384, 3386, 3388, 3390, 3392, 3394, 3396, 3398, 3400, 3402, 3404, 3406, 3408, 3410, 3412, 3414, 3416, 3418, 3420, 3422, 3424, 3426, 3428, 3430, 3432, 3434, 3436, 3438, 3440, 3442, 3444, 3446, 3448, 3450, 3452, 3454, 3456, 3458, 3460, 3462, 3464, 3466, 3468, 3470, 3472, 3474, 3476, 3478, 3480, 3482, 3484, 3486, 3488, 3490, 3492, 3494, 3496, 3498, 3500, 3502, 3504, 3506, 3508, 3510, 3512, 3514, 3516, 3518, 3520, 3522, 3524, 3526, 3528, 3530, 3532, 3534, 3536, 3538, 3540, 3542, 3544, 3546, 3548, 3550, 3552, 3554, 3556, 3558, 3560, 3562, 3564, 3566, 3568, 3570, 3572, 3574, 3576, 3578, 3580, 3582, 3584, 3586, 3588, 3590, 3592, 3594, 3596, 3598, 3600, 3602, 3604, 3606, 3608, 3610, 3612, 3614, 3616, 3618, 3620, 3622, 3624, 3626, 3628, 3630, 3632, 3634, 3636, 3638, 3640, 3642, 3644, 3646, 3648, 3650, 3652, 3654, 3656, 3658, 3660, 3662, 3664, 3666, 3668, 3670, 3672, 3674, 3676, 3678, 3680, 3682, 3684, 3686, 3688, 3690, 3692, 3694, 3696, 3698, 3700, 3702, 3704, 3706, 3708, 3710, 3712, 3714, 3716, 3718, 3720, 3722, 3724, 3726, 3728, 3730, 3732, 3734, 3736, 3738, 3740, 3742, 3744, 3746, 3748, 3750, 3752, 3754, 3756, 3758, 3760, 3762, 3764, 3766, 3768, 3770, 3772, 3774, 3776, 3778, 3780, 3782, 3784, 3786, 3788, 3790, 3792, 3794, 3796, 3798, 3800, 3802, 3804, 3806, 3808, 3810, 3812, 3814, 3816, 3818, 3820, 3822, 3824, 3826, 3828, 3830, 3832, 3834, 3836, 3838, 3840, 3842, 3844, 3846, 3848, 3850, 3852, 3854, 3856, 3858, 3860, 3862, 3864, 3866, 3868, 3870, 3872, 3874, 3875, 3876, 3877, 3878, 3879, 3880, 3881, 3882, 3883, 3884, 3885, 3886, 3887, 3888, 3889, 3890, 3891, 3892, 3893, 3894, 3895, 3896, 3897, 3898, 3899, 3900, 3901, 3902, 3903, 3904, 3905, 3906, 3907, 3908, 3909, 3910, 3911, 3912, 3913, 3914, 3915, 3916, 3917, 3918, 3919, 3920, 3921, 3922, 3923, 3924, 3925, 3926, 3927, 3928, 3929, 3930, 3931, 3932, 3933, 3934, 3935, 3936, 3937, 3938, 3939, 3940, 3941, 3942, 3943, 3944, 3945, 3946, 3947, 3948, 3949, 3950, 3951, 3952, 3953, 3954, 3955, 3956, 3957, 3958, 3959, 3960, 3961, 3962, 3963, 3964, 3965, 3966, 3967, 3968, 3969, 3970, 3971, 3972, 3973, 3974, 3975, 3976, 3977, 3978, 3979, 3980, 3981, 3982, 3983, 3984, 3985, 3986, 3987, 3988, 3989, 3990, 3991, 3992, 3993, 3994, 3995, 3996, 3997, 3998, 3999, 4000, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008, 4009, 4010, 4011, 4012, 4013, 4014, 4015, 4016, 4017, 4018, 4019, 4020, 4021, 4022, 4023, 4024, 4025, 4026, 4027, 4028, 4029, 4030, 4031, 4032, 4033, 4034, 4035, 4036, 4037, 4038, 4039, 4040, 4041, 4042, 4043, 4044, 4045, 4046, 4047, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 877, 872, 4067, 858, 927, 922, 956, 951, 653, 653, 892, 1146, 1151, 1151, 1146, 1162, 1179, 1174, 658, 658, 927, 922, 4085, 956, 951, 927, 922, 1151, 1146, 1146, 1151, 658, 653, 1202, 1197, 4451, 4454, 1202, 1197, 1465, 1460, 1484, 1479, 1452, 1447, 4458, 4460, 1484, 1499, 1494, 4109, 4463, 1452, 1447, 4465, 1465, 1460, 1484, 1509, 4114, 4467, 1452, 1447, 1453, 1455, 1465, 1460, 4192, 1479, 1489, 1499, 1494, 4470, 1452, 1447, 4472, 1452, 1447, 4474, 1460, 4476, 1452, 1447, 4478, 1452, 1447, 4480, 1465, 4482, 1452, 1447, 1452, 1447, 1484, 1484, 1499, 1494, 1484, 1484, 1499, 1494, 1484, 1484, 1499, 1494, 1504, 4486, 1479, 1499, 1494, 1447, 1452, 1455, 1453, 1465, 1460, 4192, 1479, 1489, 1494, 1499, 4488, 1455, 1453, 1465, 1460, 4192, 4490, 4492, 4151, 4152, 4494, 4426, 1452, 1447, 4497, 1452, 1447, 4499, 4501, 4157, 1447, 1452, 4503, 1452, 1447, 4505, 4507, 4162, 1452, 1447, 4509, 1452, 1447, 4511, 1465, 1460, 4513, 1484, 1484, 1499, 1494, 1499, 1494, 1499, 1494, 1504, 1452, 1447, 4515, 1452, 1447, 4517, 1465, 1460, 4174, 1452, 1447, 4519, 1452, 1447, 4521, 1465, 1460, 4413, 1484, 1479, 1499, 1494, 1504, 1499, 1494, 1499, 1494, 1452, 1447, 1455, 1453, 1465, 1460, 4185, 1479, 1489, 1499, 1494, 4524, 1499, 1494, 4526, 1447, 1452, 1453, 1455, 1465, 1460, 4192, 1479, 1489, 1499, 1494, 4529, 1494, 1499, 4531, 4533, 4535, 1600, 4537, 4539, 1595, 1610, 1605, 1615, 1625, 1620, 1625, 1620, 1625, 1620, 4543, 4545, 4547, 927, 922, 4208, 956, 951, 927, 922, 4214, 956, 951, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 658, 653, 1202, 1197, 4551, 1479, 1484, 1484, 1499, 1494, 1509, 1499, 1494, 1504, 1447, 1452, 1453, 1455, 1465, 1460, 4244, 1378, 1479, 1489, 1499, 1494, 4555, 1499, 1494, 4557, 4559, 4561, 4563, 1600, 1595, 4565, 4567, 4569, 1600, 1595, 4426, 4373, 1590, 1585, 1595, 1600, 1605, 1610, 1559, 1620, 1625, 1635, 1630, 877, 872, 877, 872, 877, 872, 877, 872, 4268, 4270, 877, 872, 858, 877, 872, 877, 872, 4279, 892, 927, 922, 4284, 956, 951, 4579, 927, 922, 937, 932, 956, 951, 956, 951, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 1162, 1169, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 1164, 1169, 1151, 1146, 1151, 1146, 1146, 1151, 1156, 1162, 1164, 1161, 1179, 1174, 1179, 1174, 4348, 1202, 1197, 1151, 1146, 1141, 1151, 1146, 1156, 4587, 1169, 4589, 1161, 1151, 1146, 1141, 1151, 1146, 1156, 4591, 1161, 4593, 1169, 1179, 1174, 1184, 4348, 1202, 1197, 1573, 1590, 1585, 4358, 4595, 1484, 1484, 1578, 1590, 1585, 1452, 1447, 1455, 1453, 1465, 1460, 4358, 1378, 4599, 1499, 1494, 1494, 1499, 1452, 1447, 1455, 1453, 1465, 1460, 4381, 1378, 4601, 1499, 1494, 1494, 1499, 1452, 1447, 1452, 1447, 4603, 1484, 1479, 1509, 1504, 4373, 4375, 1452, 1447, 1453, 1455, 1460, 1465, 4381, 1378, 1489, 1479, 1499, 1494, 1499, 1494, 1447, 1452, 1455, 1453, 1465, 1460, 4392, 1378, 1479, 1489, 1499, 1494, 1499, 1494, 1452, 1447, 4610, 1452, 1447, 4612, 1465, 1460, 4406, 1452, 1447, 4614, 1452, 1447, 4616, 1465, 1460, 4413, 1484, 1479, 1484, 1489, 1499, 1494, 1509, 1504, 4426, 1590, 1585, 1600, 1595, 1605, 1610, 1559, 1625, 1620, 4618, 4426, 1585, 1590, 1595, 1600, 1605, 1610, 1559, 1625, 1620, 4620, 1578, 1573, 1590, 1585, 1600, 1595, 1610, 1605, 1615, 1625, 1620, 1635, 1630, 30, 31, 4640, 4641, 4642, 4643, 4644, 4645, 4646, 4647, 4648, 4649, 4650, 4651, 4652, 4653, 4654, 4655, 4656, 4657, 4658, 4659, 4660, 4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668, 4669, 4670, 4671, 4672, 4673, 4674, 4677, 4678, 4679, 4680, 4681, 4682, 4683, 4684, 4687, 4688, 4689, 4690, 4692, 4693, 4695, 4696, 4697, 4698, 4699, 4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4713, 4714, 4716, 4717, 4719, 4721, 4722, 4724, 4725, 4727, 4729, 4730, 4731, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739, 4740, 4741, 4742, 4743, 4744, 4745, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4756, 4757, 4758, 4759, 4760, 4762, 4763, 4764, 4765, 4766, 4769, 4770, 4772, 4773, 4774, 4776, 4777, 4780, 4781, 4782, 4784, 4785, 4788, 4789, 4790, 4792, 4793, 4795, 4796, 4798, 4799, 4800, 4801, 4802, 4803, 4804, 4805, 4806, 4807, 4808, 4810, 4811, 4813, 4814, 4815, 4816, 4817, 4819, 4820, 4822, 4823, 4824, 4825, 4826, 4827, 4828, 4829, 4830, 4831, 4832, 4833, 4834, 4835, 4836, 4837, 4838, 4839, 4840, 4841, 4842, 4843, 4844, 4846, 4847, 4849, 4850, 4851, 4852, 4853, 4854, 4855, 4856, 4857, 4858, 4859, 4861, 4862, 4866, 4869, 4870, 4871, 4872, 4873, 4874, 4875, 4876, 4877, 4878, 4882, 4883, 4884, 4885, 4886, 4887, 4888, 4889, 4890, 4891, 4892, 4893, 4894, 4895, 4896, 4897, 4898, 4899, 4900, 4901, 4902, 4904, 4905, 4906, 4907, 4908, 4909, 4910, 4911, 4912, 4913, 4914, 4915, 4916, 4917, 4918, 4919, 4920, 4921, 4922, 4923, 4924, 4926, 4927, 4932, 4933, 4937, 4938, 4939, 4940, 4941, 4942, 4943, 4944, 4945, 4946, 4947, 4948, 4949, 4950, 4951, 4952, 4953, 4954, 4955, 4956, 4957, 4958, 4959, 4960, 4961, 4962, 4963, 4964, 4965, 4966, 4967, 4968, 4969, 4970, 4971, 4972, 4973, 4974, 4975, 4977, 4978, 4979, 4980, 4981, 4982, 4983, 4984, 4985, 4986, 4987, 4988, 4989, 4990, 4991, 4992, 4993, 4994, 4995, 4996, 4997, 4998, 4999, 5000, 5001, 5002, 5003, 5004, 5005, 5006, 5007, 5008, 5009, 5010, 5011, 5012, 5013, 5014, 5015, 5016, 5017, 5018, 5019, 5020, 5021, 5022, 5023, 5024, 5025, 5027, 5029, 5030, 5031, 5032, 5033, 5034, 5035, 5037, 5039, 5040, 5041, 5042, 5043, 5044, 5045, 5046, 5047, 5048, 5049, 5051, 5052, 5053, 5054, 5055, 5056, 5057, 5058, 5059, 5060, 5061, 5062, 5063, 5065, 5066, 5067, 5068, 5069, 5070, 5071, 5072, 5073, 5074, 5075, 5076, 5078, 5079, 5080, 5081, 5082, 5083, 5084, 5085, 5087, 5088, 5089, 5090, 5091, 5092, 5093, 5094, 5095, 5096, 5097, 5098, 5099, 5100, 5101, 5102, 5103, 5104, 5105, 5106, 5107, 5108, 5109, 5110, 5111, 5112, 5113, 5114, 5115, 5116, 5117, 5118, 5119, 5120, 5121, 5122, 5124, 5125, 5127, 5128, 5129, 5130, 5131, 5133, 5134, 5136, 5137, 5138, 5139, 5140, 5141, 5142, 5143, 5144, 5145, 5146, 5147, 5148, 5149, 5150, 5151, 5152, 5153, 5154, 5155, 5156, 5158, 5159, 5160, 5161, 5162, 5163, 5164, 5165, 5166, 5167, 5169, 5170, 5171, 5172, 5173, 5174, 5175, 5176, 5177, 5178, 5179, 5180, 5181, 4456, 4453, 4572, 4572, 4768, 4767, 4572, 4572, 4768, 4767, 4572, 4572, 4572, 4572, 4571, 4572, 4571, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5184, 5188, 5190, 5195, 5197, 5200, 5204, 5207, 5209, 5211, 5213, 5215, 5217, 5219, 5221, 5225, 5228, 5231, 5233, 5238, 5240, 5242, 5245, 5247, 5249, 5251, 5254, 5256, 5259, 5261, 5265, 5269, 5273, 5277, 5279, 5281, 5283, 5286, 5288, 5290, 5292, 5298, 5300, 5303, 5305, 5308, 5310, 5312, 5316, 5318, 5320, 5323, 5325, 5327, 5330, 5332, 5334, 5339, 5342, 5344, 5346, 5348, 5350, 5353, 5355, 5357, 5359, 5361, 5363, 5366, 5368, 5370, 5374, 5377, 5379, 5381, 5383, 5386, 5388, 5391, 5393, 5395, 5397, 5400, 5402, 5407, 5410, 5413, 5415, 5417, 5421, 5423, 5425, 5427, 5429, 5433, 5435, 5437, 5440, 5442, 5444, 5446, 5448, 5450, 5454, 5457, 5459, 5463, 5466, 5468, 5470, 5472, 5474, 5476, 5478, 5480, 5485, 5487, 5489, 5494, 5496, 5498, 5501, 5504, 5506, 5509, 5511, 5514, 5519, 5522, 5527, 5531, 5534, 5540, 5542, 5544, 5546, 5550, 5552, 5554, 5556, 5558, 5562, 5564, 5566, 5568, 5572, 5576, 5578, 5580, 5584, 5586, 5588, 5590, 5592, 5594, 5598, 5600, 5602, 5604, 5606, 5608, 5611, 5613, 5615, 5622, 5624, 5627, 5629, 5631, 5634, 5637, 5639, 5641, 5644, 5646, 5648, 5650, 5652, 5655, 5657, 4576, 4575, 5194, 5484, 5493, 5518, 5517, 5526, 5525, 4576, 4575, 5194, 5484, 5493, 5518, 5517, 5526, 5525, 4576, 4575, 5462, 5484, 5493, 5518, 5517, 5526, 5659, 5660, 5404, 4554, 4553, 5661, 4571, 5662, 4571, 5663, 5664, 5224, 4686, 4728, 5302, 5307, 4554, 4553, 4554, 4553, 4554, 4553, 4571, 5372, 4571, 5373, 4571, 4554, 4553, 5404, 5665, 4571, 5666, 4571, 4720, 4728, 5302, 5307, 4554, 4553, 4554, 4553, 4554, 4553, 5276, 5667, 5668, 4571, 5372, 4571, 5373, 4571, 5302, 5307, 4554, 4553, 5338, 5669, 5372, 5670, 5373, 5671, 4554, 4553, 5404, 5672, 5673, 5674, 5675, 4572, 4571, 4576, 4575, 5462, 5484, 5493, 5518, 5517, 5526, 5525, 5571, 5621, 4605, 5621, 4598, 4597, 4605, 5064, 5077, 5571, 5621, 4605, 4605, 5621, 5619, 4622, 4622, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 4578, 4577, 4574, 4573, 5877, 5878, 5187, 4578, 4577, 5879, 5385, 4549, 5390, 4550, 5806, 4581, 4582, 4583, 4449, 5880, 4449, 4583, 5881, 4449, 4583, 5503, 4586, 4585, 5708, 4449, 4583, 5882, 5883, 4449, 4583, 5884, 5885, 5529, 5709, 4578, 4577, 4574, 4573, 5886, 5887, 4578, 4577, 5456, 5888, 5385, 4549, 5390, 4550, 5806, 4582, 4581, 4583, 4449, 5889, 4449, 4583, 5890, 4449, 4583, 5503, 4586, 4585, 5708, 4449, 4583, 5891, 5892, 4449, 4583, 5893, 5894, 5529, 5709, 4578, 4577, 4574, 4573, 5895, 5896, 4578, 4577, 5456, 5897, 5206, 4549, 5390, 4550, 5806, 4582, 4581, 4450, 4584, 5898, 4450, 4584, 5899, 4450, 4584, 5503, 4586, 4585, 5708, 4450, 4584, 5900, 5901, 4450, 4584, 5902, 5529, 5709, 4694, 4691, 5610, 5135, 5132, 5617, 5905, 5906, 5907, 5412, 5236, 5784, 5419, 5786, 4928, 4925, 5909, 5873, 5654, 5876, 5911, 5792, 5439, 5795, 5735, 5294, 5912, 4848, 4845, 4812, 4746, 5329, 4821, 4818, 5336, 5914, 5341, 4607, 4606, 5731, 5285, 5733, 4761, 4718, 4685, 5915, 4726, 4723, 5916, 4778, 4775, 5917, 4786, 4485, 4484, 5918, 5919, 5920, 4606, 5921, 5922, 4607, 5923, 5924, 5275, 5716, 5244, 5718, 4712, 5925, 5926, 5927, 5928, 5376, 4542, 4541, 4771, 5929, 5792, 5439, 5795, 4694, 4691, 5610, 5135, 5132, 5617, 5930, 5931, 5932, 5412, 5236, 5784, 5419, 5786, 4928, 4925, 5934, 5873, 5654, 5876, 5936, 5792, 5439, 5795, 5716, 5244, 5718, 4712, 4718, 4715, 5937, 4726, 4723, 5938, 4778, 4775, 5939, 4786, 4485, 4484, 5940, 5941, 5942, 4606, 5943, 5944, 4607, 5945, 5946, 5275, 4812, 4746, 5329, 4821, 4818, 5336, 5947, 5341, 4607, 4606, 5731, 5285, 5733, 4761, 5735, 5294, 5948, 4848, 4845, 5950, 5951, 5952, 5953, 5376, 4542, 4541, 4771, 5954, 5792, 5439, 5795, 4778, 4775, 5955, 4786, 4783, 5956, 4794, 4791, 4797, 5957, 5958, 5322, 4607, 4606, 4812, 4809, 5329, 4821, 4818, 5336, 5959, 4607, 4606, 5341, 5757, 5352, 5759, 4848, 4845, 5763, 5365, 5765, 4863, 4860, 5961, 5963, 5376, 4879, 4542, 4541, 5792, 5439, 5795, 5385, 4549, 5390, 4550, 5399, 4584, 4583, 5780, 5123, 4903, 5610, 5135, 5132, 5617, 5965, 5966, 5967, 5412, 5409, 5784, 5419, 5786, 4928, 4925, 5968, 5789, 5970, 5790, 5654, 5876, 5972, 5973, 5792, 5439, 5795, 4578, 4577, 4574, 4573, 5974, 5975, 4578, 4577, 5456, 5976, 5465, 4976, 5806, 4582, 4581, 5482, 4584, 4583, 5977, 5491, 4584, 4583, 5978, 5500, 4584, 4583, 5503, 4586, 4585, 5821, 5516, 5513, 5979, 5980, 5524, 5521, 5981, 5982, 5529, 5827, 5844, 5582, 5846, 4607, 4606, 5850, 5536, 5852, 4609, 4608, 5123, 5126, 5610, 5132, 5086, 5617, 5983, 5984, 5842, 5985, 5873, 5654, 5876, 5844, 5582, 5846, 4607, 4606, 5850, 5536, 5852, 4609, 4608, 5123, 5050, 5610, 5132, 5086, 5617, 5986, 5987, 5988, 5862, 5989, 5873, 5654, 5876, 5831, 5548, 5990, 4609, 4608, 5836, 5560, 5991, 4609, 4608, 5844, 5582, 5846, 4607, 4606, 5123, 5126, 5610, 5132, 5086, 5617, 5992, 5993, 5842, 5994, 5864, 5633, 5157, 5995, 5868, 5643, 5168, 5844, 5582, 5846, 4607, 4606, 5850, 5596, 5852, 4609, 4608, 5126, 5123, 5610, 5135, 5132, 5617, 5996, 5997, 5862, 5998, 5864, 5633, 5157, 5999, 5868, 5643, 5168, 4622, 5873, 5654, 5876, 26, 27, 28, 29, 30, 31, 6016, 6017, 6018, 6019, 6020, 6022, 6023, 6024, 6026, 6027, 6028, 6029, 6030, 6031, 6032, 6033, 6034, 6036, 6037, 6039, 6040, 6041, 6042, 6043, 6044, 6045, 6046, 6047, 6049, 6050, 6051, 6053, 6054, 6055, 6056, 6057, 6058, 6059, 6061, 6062, 6063, 6065, 6066, 6067, 6068, 6069, 6070, 6071, 6072, 6073, 6075, 6076, 6078, 6079, 6080, 6081, 6082, 6083, 6084, 6085, 6086, 6088, 6089, 6090, 6092, 6093, 6094, 6095, 6096, 6097, 6098, 6100, 6101, 6102, 6104, 6105, 6106, 6107, 6108, 6109, 6110, 6111, 6112, 6114, 6115, 6117, 6118, 6119, 6120, 6121, 6122, 6123, 6124, 6125, 6127, 6128, 6129, 6130, 6131, 6132, 6133, 6134, 6135, 6136, 6137, 6138, 6141, 6142, 6143, 6144, 6145, 6146, 6147, 5908, 6149, 6150, 6151, 5910, 6153, 6154, 6155, 6156, 6157, 6159, 6160, 6161, 6162, 6163, 6164, 6165, 6166, 6168, 6169, 6170, 6171, 6172, 6173, 6174, 6175, 6176, 6178, 6179, 6181, 6182, 6184, 6185, 6186, 6188, 6190, 6191, 6193, 6194, 6196, 6197, 6198, 6199, 6200, 6205, 6206, 6207, 6208, 6210, 6211, 6212, 6213, 6214, 6215, 6216, 6217, 6218, 6219, 6222, 6223, 6224, 6225, 6226, 6227, 6228, 5933, 6230, 6231, 6232, 5935, 6234, 6235, 6236, 6237, 6238, 6239, 6240, 6241, 6242, 6244, 6245, 6247, 6248, 6250, 6251, 6252, 6254, 6256, 6257, 6259, 6260, 6262, 6263, 6264, 6265, 6266, 6267, 6268, 6270, 6271, 6272, 6273, 6274, 6275, 6276, 6277, 6278, 6280, 6281, 6286, 6287, 6288, 6289, 6291, 6292, 6293, 6294, 6295, 6297, 6298, 6300, 6301, 6302, 6303, 6305, 6306, 6307, 6308, 6309, 6310, 6311, 6312, 6313, 6315, 6316, 6317, 6318, 6319, 6320, 6321, 6322, 6323, 6324, 6325, 6326, 6327, 6330, 6331, 6332, 6333, 6334, 6335, 6336, 6337, 6338, 6339, 6340, 6341, 6342, 6343, 6344, 6345, 6346, 6347, 6348, 6349, 6350, 6351, 6354, 6355, 6356, 6357, 6358, 6359, 6360, 6362, 6364, 6365, 6366, 6367, 6369, 6370, 6371, 6372, 6373, 6374, 6375, 6376, 6378, 6379, 6380, 6382, 6383, 6384, 6385, 6386, 6387, 6388, 6389, 6391, 6392, 6393, 6395, 6396, 6397, 6398, 6399, 6400, 6401, 6402, 6403, 6404, 6406, 6407, 6408, 6410, 6411, 6412, 6413, 6414, 6415, 6416, 6417, 6418, 6419, 6420, 6421, 6422, 6423, 6424, 6425, 6426, 6427, 6428, 6430, 6432, 6433, 6434, 6435, 6436, 6437, 6438, 6439, 6440, 6441, 6442, 6443, 6444, 6445, 6446, 6447, 6448, 6449, 6450, 6451, 6454, 6456, 6457, 6458, 6459, 6460, 6462, 6463, 6464, 6465, 6467, 6468, 6469, 6470, 6471, 6472, 6473, 6474, 6475, 6476, 6477, 6478, 6479, 6480, 6482, 6484, 6485, 6486, 6488, 6489, 6490, 6491, 6492, 6493, 6494, 6495, 6496, 6497, 6498, 6499, 6500, 6501, 6502, 6503, 6504, 6505, 6506, 6507, 6509, 6511, 6512, 6513, 6515, 6516, 6517, 6518, 6519, 6520, 6521, 6204, 6202, 6285, 6283, 6329, 6328, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 6528, 6530, 6533, 6541, 6543, 6545, 6547, 6550, 6553, 6556, 6561, 6563, 6566, 6574, 6576, 6578, 6580, 6583, 6586, 6589, 6594, 6596, 6599, 6607, 6609, 6611, 6613, 6616, 6619, 6622, 6624, 6627, 6630, 6633, 6634, 6639, 6651, 6653, 6656, 6659, 6666, 6668, 6670, 6672, 6686, 6692, 6695, 6698, 6699, 6704, 6718, 6720, 6722, 6724, 6733, 6736, 6739, 6748, 6751, 6757, 6759, 6761, 6765, 6768, 6771, 6774, 6780, 6785, 6788, 6798, 6802, 6805, 6808, 6809, 6814, 6824, 6826, 6829, 6835, 6837, 6840, 6843, 6847, 6850, 6853, 6861, 6866, 6868, 6871, 6882, 6887, 6889, 6892, 6895, 6902, 6906, 6911, 6913, 6916, 6930, 6935, 6937, 6940, 6539, 6537, 6560, 6572, 6570, 6593, 6605, 6603, 6626, 6637, 6642, 6644, 6646, 6648, 6650, 6663, 6665, 6680, 6678, 6676, 6682, 6684, 6955, 6956, 6689, 6691, 6702, 6707, 6709, 6711, 6713, 6715, 6717, 6732, 6730, 6728, 6743, 6745, 6747, 6957, 6958, 6754, 6756, 6778, 6783, 6959, 6960, 6791, 6793, 6797, 6795, 6857, 6812, 6817, 6816, 6819, 6821, 6823, 6833, 6857, 6859, 6864, 6875, 6876, 6878, 6880, 6885, 6897, 6899, 6901, 6905, 6909, 6920, 6921, 6923, 6924, 6926, 6928, 6933, 6944, 6945, 6947, 6948, 6950, 6952, 6954, 29, 30, 31, 6976, 6978, 6986, 6988, 6996, 6998, 7015, 7019, 7020, 7029, 7032, 7034, 7038, 7041, 7044, 7045, 7051, 7053, 7055, 7056, 7057, 6979, 7079, 7080, 6549, 6038, 6035, 6552, 6558, 6555, 7081, 6989, 7082, 7083, 6582, 6077, 6074, 6585, 6591, 6588, 7084, 6999, 7085, 7086, 6615, 6116, 6113, 6618, 7006, 6621, 7087, 6632, 6629, 7010, 7088, 7011, 7089, 7090, 7091, 7092, 7093, 7012, 6658, 6655, 7094, 7095, 6183, 6180, 6177, 7096, 7097, 7098, 7099, 7100, 7101, 7103, 7104, 6697, 6694, 7024, 7105, 7025, 7106, 7107, 7108, 7109, 7110, 7111, 6249, 6246, 6243, 7112, 7113, 7114, 6738, 6735, 7115, 7116, 7117, 7033, 7118, 7120, 7121, 6763, 6299, 6296, 6773, 6770, 7122, 7042, 7123, 7043, 7124, 7126, 7127, 7054, 7128, 7129, 6801, 6855, 6852, 7130, 6807, 6804, 7049, 7131, 7050, 7132, 7133, 7134, 7135, 7136, 7054, 7137, 6849, 6855, 6852, 7138, 7139, 7061, 7140, 7062, 6873, 6870, 7141, 7142, 7143, 7144, 7065, 7145, 7066, 6894, 6891, 6896, 7146, 7147, 7148, 7070, 7149, 7071, 7150, 7072, 6918, 6915, 7151, 7152, 7153, 7154, 7155, 7156, 7075, 7157, 7076, 6942, 6939, 7158, 7159, 7160, 7161, 7162, 7163, 7164, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 6025, 6532, 7189, 7192, 7193, 7194, 7195, 7196, 7197, 6064, 6565, 7199, 7202, 7203, 7204, 7205, 7206, 7207, 6103, 6598, 7209, 7212, 7213, 7214, 7215, 7216, 7217, 7219, 7220, 7221, 7223, 7229, 7230, 7231, 7174, 6187, 7234, 7235, 7236, 7237, 7176, 7245, 7246, 7247, 7249, 6253, 7256, 7257, 7258, 7259, 7262, 7263, 7178, 7267, 7179, 7271, 7272, 7273, 7180, 7274, 7275, 7181, 7277, 7279, 7182, 6381, 6828, 7283, 6846, 6394, 6390, 7286, 7287, 7288, 7290, 7291, 7292, 7294, 7295, 6381, 6828, 7300, 6846, 6394, 6390, 7302, 7303, 7304, 7307, 7309, 7310, 7311, 7316, 7318, 7319, 7320, 7321, 7325, 7327, 7329, 7330, 7331, 7338, 7340, 7341, 7342, 7227, 7225, 7241, 7233, 7244, 7253, 7251, 7255, 7265, 7270, 7282, 7299, 7314, 7323, 7336, 7334, 7349, 7347, 7345, 29, 30, 31, 7360, 7361, 7362, 7363, 7367, 7369, 7370, 7371, 7372, 7376, 7378, 7379, 7380, 7381, 7385, 7387, 7392, 7394, 7395, 7397, 7399, 7400, 7401, 7405, 7407, 7409, 7410, 7412, 7414, 7415, 7418, 7419, 7421, 7424, 7425, 7426, 7427, 7428, 7429, 7430, 7432, 7434, 7439, 7440, 7441, 7442, 7443, 7444, 7446, 7450, 7454, 7460, 7464, 7390, 7466, 7467, 7468, 7469, 7391, 7470, 7404, 7471, 7472, 7473, 7413, 7474, 7475, 7423, 7422, 7476, 7437, 7477, 7297, 7449, 7448, 7478, 7453, 7452, 7479, 7459, 7458, 7457, 7480, 7481, 7463, 7462, 7482, 7483, 7484, 25, 26, 27, 28, 29, 30, 31, 7488, 7490, 7491, 7493, 7495, 7496, 7498, 7500, 7501, 7506, 7511, 7517, 7522, 7524, 7525, 7530, 7533, 7198, 7208, 7218, 7541, 7389, 7542, 7505, 7546, 7509, 7548, 7403, 7549, 7515, 7552, 7516, 7555, 7556, 7520, 7521, 7289, 7558, 7436, 7560, 7305, 7312, 7561, 7562, 7456, 7564, 7565, 7332, 7567, 7568, 7569, 7570, 7343, 7572, 7573, 7574, 24, 25, 26, 27, 28, 29, 30, 31, 7593, 7594, 7598, 7600, 7585, 7601, 7366, 7588, 7602, 7375, 7591, 7603, 7384, 7605, 7607, 7609, 7611, 7613, 7551, 7615, 7618, 7518, 7616, 7619, 7597, 7620, 7622, 7559, 7532, 7624, 7625, 7628, 7631, 7633, 7636, 7639, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7652, 7654, 7655, 7657, 7658, 7660, 7604, 7508, 7544, 7547, 7610, 7513, 7554, 7669, 7557, 7672, 7431, 7621, 7676, 7445, 7678, 7679, 7680, 7682, 24, 25, 26, 27, 28, 29, 30, 31, 7653, 7656, 7659, 7719, 7723, 7668, 7728, 7731, 7732, 7733, 7734, 7735, 7612, 7606, 7675, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7747, 7748, 7670, 7673, 7677, 7746, 7745, 7744, 7756, 7757, 7758, 7683, 7635, 7566, 7563, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7720, 7777, 7781, 7782, 7783, 7726, 7779, 7780, 7787, 7788, 7789, 7790, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7808, 7809, 7810, 7813, 7814, 7815, 7816, 7818, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7842, 7724, 7721, 7846, 7786, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7873, 7874, 7875, 7876, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7784, 7843, 7906, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7936, 7938, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7968, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 8000, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 8032, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7969, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; int h_C[]= { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, 353, 355, 357, 359, 361, 363, 365, 367, 369, 371, 373, 376, 378, 380, 382, 384, 386, 388, 390, 392, 394, 397, 399, 401, 403, 405, 407, 411, 413, 415, 417, 419, 421, 423, 425, 427, 429, 431, 433, 435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457, 459, 461, 463, 465, 467, 469, 471, 473, 475, 477, 479, 481, 483, 485, 487, 489, 491, 493, 496, 498, 500, 502, 504, 506, 508, 510, 512, 514, 516, 518, 520, 522, 525, 527, 529, 531, 533, 535, 537, 539, 541, 543, 545, 547, 549, 551, 553, 555, 557, 559, 561, 563, 565, 567, 569, 571, 574, 576, 578, 580, 582, 584, 586, 588, 590, 592, 594, 596, 598, 600, 602, 604, 606, 608, 610, 612, 614, 616, 618, 620, 622, 624, 626, 628, 630, 632, 634, 636, 638, 640, 642, 644, 646, 648, 650, 652, 655, 657, 660, 662, 664, 666, 668, 670, 672, 674, 676, 678, 680, 682, 684, 686, 688, 690, 692, 694, 696, 698, 700, 702, 704, 706, 708, 710, 712, 714, 716, 718, 720, 722, 724, 726, 728, 730, 732, 734, 736, 738, 740, 742, 744, 746, 748, 750, 753, 755, 757, 759, 762, 764, 766, 768, 772, 774, 776, 778, 780, 782, 784, 786, 788, 790, 792, 794, 796, 798, 800, 802, 804, 806, 808, 810, 812, 814, 816, 818, 820, 822, 824, 826, 828, 830, 832, 834, 837, 839, 841, 843, 847, 849, 851, 853, 855, 857, 860, 862, 864, 866, 869, 871, 874, 876, 881, 883, 885, 887, 889, 891, 894, 896, 898, 900, 902, 904, 906, 908, 910, 912, 914, 916, 919, 921, 924, 926, 929, 931, 934, 936, 939, 941, 943, 945, 948, 950, 953, 955, 960, 962, 964, 966, 968, 970, 972, 974, 976, 978, 980, 982, 984, 986, 988, 990, 992, 994, 996, 998, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018, 1020, 1022, 1024, 1026, 1028, 1030, 1032, 1034, 1036, 1038, 1041, 1043, 1045, 1047, 1050, 1052, 1054, 1056, 1058, 1060, 1062, 1064, 1066, 1068, 1070, 1072, 1074, 1076, 1078, 1080, 1083, 1085, 1087, 1089, 1093, 1095, 1097, 1099, 1101, 1103, 1105, 1107, 1109, 1111, 1113, 1115, 1117, 1119, 1121, 1123, 1126, 1128, 1130, 1132, 1134, 1136, 1138, 1140, 1143, 1145, 1148, 1150, 1153, 1155, 1158, 1160, 1166, 1168, 1171, 1173, 1176, 1178, 1181, 1183, 1186, 1188, 1190, 1192, 1194, 1196, 1199, 1201, 1204, 1206, 1208, 1210, 1212, 1214, 1216, 1218, 1220, 1222, 1224, 1226, 1228, 1230, 1232, 1234, 1237, 1239, 1241, 1243, 1245, 1247, 1249, 1251, 1253, 1255, 1258, 1260, 1262, 1264, 1266, 1268, 1270, 1272, 1274, 1276, 1278, 1280, 1282, 1284, 1286, 1288, 1290, 1292, 1294, 1296, 1298, 1300, 1302, 1304, 1307, 1309, 1311, 1313, 1315, 1317, 1319, 1321, 1323, 1325, 1327, 1329, 1331, 1333, 1335, 1337, 1339, 1341, 1343, 1345, 1347, 1349, 1351, 1353, 1355, 1357, 1359, 1361, 1363, 1365, 1367, 1369, 1371, 1373, 1375, 1377, 1380, 1382, 1384, 1386, 1388, 1390, 1392, 1394, 1397, 1399, 1401, 1403, 1406, 1408, 1410, 1412, 1414, 1416, 1418, 1420, 1422, 1424, 1426, 1428, 1431, 1433, 1435, 1437, 1439, 1441, 1444, 1446, 1449, 1451, 1457, 1459, 1462, 1464, 1468, 1470, 1472, 1474, 1476, 1478, 1481, 1483, 1486, 1488, 1491, 1493, 1496, 1498, 1501, 1503, 1506, 1508, 1511, 1513, 1515, 1517, 1519, 1521, 1524, 1526, 1528, 1530, 1532, 1534, 1536, 1538, 1540, 1542, 1544, 1546, 1548, 1550, 1552, 1554, 1556, 1558, 1561, 1563, 1565, 1567, 1570, 1572, 1575, 1577, 1582, 1584, 1587, 1589, 1592, 1594, 1597, 1599, 1602, 1604, 1607, 1609, 1612, 1614, 1617, 1619, 1622, 1624, 1627, 1629, 1632, 1634, 1039, 1048, 1163, 1163, 136, 1163, 1163, 137, 760, 1442, 1442, 1429, 1429, 751, 1454, 1454, 1442, 1442, 572, 572, 760, 1395, 1395, 1442, 1442, 1454, 1454, 1429, 1429, 1442, 1442, 1454, 1454, 1429, 1429, 1454, 1454, 1442, 1442, 1404, 1404, 1235, 1235, 1256, 1256, 1568, 1568, 751, 1454, 1454, 1442, 1442, 374, 374, 1454, 1454, 1442, 1442, 395, 395, 1442, 1442, 1454, 1454, 1429, 1429, 1442, 1442, 1454, 1454, 1454, 1454, 1442, 1442, 494, 1395, 1395, 1404, 1404, 523, 1395, 1395, 1404, 1404, 572, 572, 751, 751, 572, 572, 751, 751, 1522, 1522, 1568, 1568, 572, 572, 751, 751, 917, 917, 1442, 1442, 1235, 1256, 1395, 1395, 1404, 1404, 769, 769, 769, 769, 751, 751, 769, 769, 769, 769, 760, 760, 770, 770, 867, 878, 835, 844, 867, 878, 917, 917, 957, 957, 1039, 1048, 1081, 1090, 1124, 1124, 1124, 1124, 1163, 1163, 1163, 1163, 1442, 1442, 1235, 1256, 1235, 1235, 1256, 1256, 1454, 1454, 1579, 1395, 1404, 1395, 1404, 1454, 1454, 1442, 1442, 1442, 1442, 1454, 1454, 1522, 1522, 1568, 1568, 1579, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3105, 3107, 3109, 3111, 3113, 3115, 3117, 3119, 3121, 3123, 3125, 3127, 3129, 3131, 3133, 3135, 3137, 3139, 3141, 3143, 3145, 3147, 3149, 3151, 3153, 3155, 3157, 3159, 3161, 3163, 3165, 3167, 3169, 3171, 3173, 3175, 3177, 3179, 3181, 3183, 3185, 3187, 3189, 3191, 3193, 3195, 3197, 3199, 3201, 3203, 3205, 3207, 3209, 3211, 3213, 3215, 3217, 3219, 3221, 3223, 3225, 3227, 3229, 3231, 3233, 3235, 3237, 3239, 3241, 3243, 3245, 3247, 3249, 3251, 3253, 3255, 3257, 3259, 3261, 3263, 3265, 3267, 3269, 3271, 3273, 3275, 3277, 3279, 3281, 3283, 3285, 3287, 3289, 3291, 3293, 3295, 3297, 3299, 3301, 3303, 3305, 3307, 3309, 3311, 3313, 3315, 3317, 3319, 3321, 3323, 3325, 3327, 3329, 3331, 3333, 3335, 3337, 3339, 3341, 3343, 3345, 3347, 3349, 3351, 3353, 3355, 3357, 3359, 3361, 3363, 3365, 3367, 3369, 3371, 3373, 3375, 3377, 3379, 3381, 3383, 3385, 3387, 3389, 3391, 3393, 3395, 3397, 3399, 3401, 3403, 3405, 3407, 3409, 3411, 3413, 3415, 3417, 3419, 3421, 3423, 3425, 3427, 3429, 3431, 3433, 3435, 3437, 3439, 3441, 3443, 3445, 3447, 3449, 3451, 3453, 3455, 3457, 3459, 3461, 3463, 3465, 3467, 3469, 3471, 3473, 3475, 3477, 3479, 3481, 3483, 3485, 3487, 3489, 3491, 3493, 3495, 3497, 3499, 3501, 3503, 3505, 3507, 3509, 3511, 3513, 3515, 3517, 3519, 3521, 3523, 3525, 3527, 3529, 3531, 3533, 3535, 3537, 3539, 3541, 3543, 3545, 3547, 3549, 3551, 3553, 3555, 3557, 3559, 3561, 3563, 3565, 3567, 3569, 3571, 3573, 3575, 3577, 3579, 3581, 3583, 3585, 3587, 3589, 3591, 3593, 3595, 3597, 3599, 3601, 3603, 3605, 3607, 3609, 3611, 3613, 3615, 3617, 3619, 3621, 3623, 3625, 3627, 3629, 3631, 3633, 3635, 3637, 3639, 3641, 3643, 3645, 3647, 3649, 3651, 3653, 3655, 3657, 3659, 3661, 3663, 3665, 3667, 3669, 3671, 3673, 3675, 3677, 3679, 3681, 3683, 3685, 3687, 3689, 3691, 3693, 3695, 3697, 3699, 3701, 3703, 3705, 3707, 3709, 3711, 3713, 3715, 3717, 3719, 3721, 3723, 3725, 3727, 3729, 3731, 3733, 3735, 3737, 3739, 3741, 3743, 3745, 3747, 3749, 3751, 3753, 3755, 3757, 3759, 3761, 3763, 3765, 3767, 3769, 3771, 3773, 3775, 3777, 3779, 3781, 3783, 3785, 3787, 3789, 3791, 3793, 3795, 3797, 3799, 3801, 3803, 3805, 3807, 3809, 3811, 3813, 3815, 3817, 3819, 3821, 3823, 3825, 3827, 3829, 3831, 3833, 3835, 3837, 3839, 3841, 3843, 3845, 3847, 3849, 3851, 3853, 3855, 3857, 3859, 3861, 3863, 3865, 3867, 3869, 3871, 3873, 1655, 1672, 1673, 1674, 1675, 1676, 1677, 1678, 1681, 1688, 1689, 1690, 1691, 1696, 1697, 1698, 1701, 1702, 1708, 1709, 1710, 1722, 1723, 1726, 1727, 1730, 1731, 1733, 1734, 1737, 1738, 1741, 1742, 1744, 1745, 1748, 1751, 1765, 1766, 1781, 1782, 1788, 1789, 1790, 1791, 1794, 1795, 1797, 1800, 1801, 1804, 1805, 1806, 1807, 1811, 1812, 1815, 1816, 1817, 1818, 1822, 1823, 1826, 1827, 1830, 1831, 1843, 1844, 1847, 1848, 1854, 1855, 1858, 1859, 1879, 1884, 1885, 1888, 1889, 1897, 1902, 1903, 1906, 1907, 1908, 1909, 1910, 1911, 1913, 1914, 1915, 1916, 1923, 1926, 1929, 1930, 1931, 1932, 1933, 1934, 1940, 1946, 1958, 1959, 1962, 1964, 1983, 1984, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002, 2006, 2008, 2022, 2025, 2031, 2033, 2039, 2042, 2050, 2051, 2058, 2061, 2082, 2085, 2094, 2097, 2107, 2108, 2110, 2111, 2119, 2120, 2122, 2123, 2135, 2136, 2138, 2140, 2152, 2153, 2166, 2167, 2176, 2177, 2184, 2197, 2200, 2213, 2216, 2219, 2220, 2223, 2224, 2230, 2231, 2234, 2235, 2257, 2258, 2269, 2270, 2273, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 4065, 4064, 4066, 4068, 4070, 4069, 4072, 4071, 4073, 4347, 4074, 4076, 4075, 4078, 4077, 4079, 4081, 4080, 4095, 4347, 4083, 4082, 4084, 4087, 4086, 4089, 4088, 4091, 4090, 4093, 4092, 4095, 4094, 4097, 4096, 4452, 4455, 4099, 4098, 4101, 4100, 4102, 4103, 4105, 4104, 4459, 4461, 4106, 4108, 4107, 572, 4464, 4171, 4170, 4466, 4111, 4110, 4112, 4113, 572, 4468, 4237, 4115, 4189, 4239, 4242, 4190, 4150, 4247, 4193, 4397, 4396, 4471, 4117, 4116, 4473, 4119, 4118, 4475, 4120, 4477, 4122, 4121, 4479, 4124, 4123, 4481, 4125, 4483, 4127, 4126, 4128, 4127, 4129, 4130, 4132, 4131, 4133, 4134, 4136, 4135, 4137, 4138, 4140, 4139, 4141, 4487, 4142, 4235, 4234, 4188, 4237, 4239, 4143, 4242, 4144, 4150, 4247, 4145, 4398, 4364, 4489, 4147, 4146, 4149, 4148, 4150, 4491, 4493, 572, 572, 4495, 572, 4154, 4153, 4498, 4156, 4155, 4500, 4502, 1466, 4159, 4158, 4504, 4161, 4160, 4506, 4508, 1466, 4171, 4163, 4510, 4368, 4367, 4512, 4173, 4164, 4514, 4165, 4166, 4179, 4167, 4181, 4180, 4235, 4168, 4169, 4171, 4170, 4516, 4368, 4367, 4518, 4173, 4172, 1429, 4410, 4409, 4520, 4408, 4407, 4522, 4412, 4411, 1466, 4175, 4228, 4177, 4176, 4236, 4179, 4178, 4181, 4180, 4182, 4188, 4183, 4189, 4242, 4190, 4184, 4247, 4186, 4397, 4187, 4525, 4248, 4384, 4527, 4188, 4237, 4189, 4239, 4242, 4190, 4191, 4247, 4193, 4397, 4396, 4530, 4398, 4364, 4532, 4534, 4536, 4194, 4538, 4540, 4195, 4197, 4196, 4198, 4200, 4199, 4202, 4201, 4204, 4203, 4544, 4546, 4548, 4206, 4205, 4207, 4210, 4209, 4212, 4211, 4213, 4216, 4215, 4218, 4217, 4220, 4219, 4222, 4221, 4223, 4225, 4224, 4227, 4226, 4552, 4228, 4229, 4230, 4232, 4231, 4233, 4235, 4234, 4236, 4238, 4237, 4240, 4239, 4242, 4241, 4243, 4245, 4247, 4246, 4397, 4396, 4556, 4248, 4384, 4558, 4560, 4562, 4564, 4250, 4249, 4566, 4568, 4570, 4252, 4251, 769, 769, 4427, 4422, 4253, 4441, 4432, 4254, 4255, 4434, 4256, 4258, 4257, 4260, 4259, 4262, 4261, 4264, 4263, 4266, 4265, 4267, 4269, 4272, 4271, 4273, 4275, 4274, 4277, 4276, 4278, 4280, 4282, 4281, 4283, 4286, 4285, 4580, 4288, 4287, 4290, 4289, 4292, 4291, 4294, 4293, 4296, 4295, 4298, 4297, 4300, 4299, 4301, 4302, 4303, 4305, 4304, 4307, 4306, 4309, 4308, 4310, 4311, 4312, 4314, 4313, 4316, 4315, 4318, 4317, 4319, 4321, 4320, 4322, 4324, 4323, 4326, 4325, 4347, 4350, 4327, 4329, 4328, 4330, 4332, 4331, 4333, 4588, 4334, 4590, 4335, 4337, 4336, 4338, 4340, 4339, 4341, 4592, 4342, 4594, 4343, 4345, 4344, 4346, 4347, 4350, 4349, 4351, 4427, 4352, 4362, 4596, 4353, 4354, 4355, 4357, 4356, 4359, 4386, 4361, 4360, 4390, 4389, 4362, 4363, 4600, 4397, 4396, 4398, 4364, 4359, 4386, 4361, 4360, 4390, 4389, 4362, 4363, 4602, 4397, 4396, 4398, 4364, 4366, 4365, 4368, 4367, 4604, 4369, 4370, 4372, 4371, 4374, 4374, 4377, 4376, 4387, 4378, 4389, 4379, 4380, 4382, 4394, 4383, 4397, 4396, 4399, 4384, 4386, 4385, 4388, 4387, 4390, 4389, 4391, 4393, 4395, 4394, 4397, 4396, 4399, 4398, 4401, 4400, 4611, 4403, 4402, 4613, 4405, 4404, 1429, 4408, 4407, 4615, 4410, 4409, 4617, 4412, 4411, 1466, 4414, 4415, 4416, 4417, 4419, 4418, 4421, 4420, 4425, 4427, 4422, 4429, 4423, 4432, 4431, 4424, 4435, 4434, 4619, 4425, 4428, 4427, 4430, 4429, 4432, 4431, 4433, 4435, 4434, 4621, 4437, 4436, 4439, 4438, 4441, 4440, 4443, 4442, 4444, 4446, 4445, 4448, 4447, 30, 31, 1636, 1637, 1638, 1639, 1640, 1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654, 1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666, 1667, 1668, 1669, 1670, 1671, 1679, 1680, 1682, 1683, 1684, 1685, 1686, 1687, 1692, 1693, 1694, 1695, 1699, 1700, 1703, 1704, 1705, 1706, 1707, 1711, 1712, 1713, 1714, 1715, 1716, 1717, 1718, 1719, 1720, 1721, 1724, 1725, 1728, 1729, 1732, 1735, 1736, 1739, 1740, 1743, 1746, 1747, 1749, 1750, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1783, 1784, 1785, 1786, 1787, 1792, 1793, 1796, 1798, 1799, 1802, 1803, 1808, 1809, 1810, 1813, 1814, 1819, 1820, 1821, 1824, 1825, 1828, 1829, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1845, 1846, 1849, 1850, 1851, 1852, 1853, 1856, 1857, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1880, 1881, 1882, 1883, 1886, 1887, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1898, 1899, 1900, 1901, 1904, 1905, 1912, 1917, 1918, 1919, 1920, 1921, 1922, 1924, 1925, 1927, 1928, 1935, 1936, 1937, 1938, 1939, 1941, 1942, 1943, 1944, 1945, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1960, 1961, 1963, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1985, 1986, 1995, 1996, 2003, 2004, 2005, 2007, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2023, 2024, 2026, 2027, 2028, 2029, 2030, 2032, 2034, 2035, 2036, 2037, 2038, 2040, 2041, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2052, 2053, 2054, 2055, 2056, 2057, 2059, 2060, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2083, 2084, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2095, 2096, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105, 2106, 2109, 2112, 2113, 2114, 2115, 2116, 2117, 2118, 2121, 2124, 2125, 2126, 2127, 2128, 2129, 2130, 2131, 2132, 2133, 2134, 2137, 2139, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149, 2150, 2151, 2154, 2155, 2156, 2157, 2158, 2159, 2160, 2161, 2162, 2163, 2164, 2165, 2168, 2169, 2170, 2171, 2172, 2173, 2174, 2175, 2178, 2179, 2180, 2181, 2182, 2183, 2185, 2186, 2187, 2188, 2189, 2190, 2191, 2192, 2193, 2194, 2195, 2196, 2198, 2199, 2201, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2214, 2215, 2217, 2218, 2221, 2222, 2225, 2226, 2227, 2228, 2229, 2232, 2233, 2236, 2237, 2238, 2239, 2240, 2241, 2242, 2243, 2244, 2245, 2246, 2247, 2248, 2249, 2250, 2251, 2252, 2253, 2254, 2255, 2256, 2259, 2260, 2261, 2262, 2263, 2264, 2265, 2266, 2267, 2268, 2271, 2272, 2274, 2275, 2276, 2277, 2278, 2279, 2280, 2281, 2282, 2283, 2284, 4676, 4675, 4700, 4880, 4523, 4523, 4700, 4880, 4523, 4523, 4864, 4867, 4880, 4930, 4929, 4935, 4934, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5185, 5189, 5191, 5196, 5198, 5201, 5205, 5208, 5210, 5212, 5214, 5216, 5218, 5220, 5222, 5226, 5229, 5232, 5234, 5239, 5241, 5243, 5246, 5248, 5250, 5252, 5255, 5257, 5260, 5262, 5266, 5270, 5274, 5278, 5280, 5282, 5284, 5287, 5289, 5291, 5293, 5299, 5301, 5304, 5306, 5309, 5311, 5313, 5317, 5319, 5321, 5324, 5326, 5328, 5331, 5333, 5335, 5340, 5343, 5345, 5347, 5349, 5351, 5354, 5356, 5358, 5360, 5362, 5364, 5367, 5369, 5371, 5375, 5378, 5380, 5382, 5384, 5387, 5389, 5392, 5394, 5396, 5398, 5401, 5403, 5408, 5411, 5414, 5416, 5418, 5422, 5424, 5426, 5428, 5430, 5434, 5436, 5438, 5441, 5443, 5445, 5447, 5449, 5451, 5455, 5458, 5460, 5464, 5467, 5469, 5471, 5473, 5475, 5477, 5479, 5481, 5486, 5488, 5490, 5495, 5497, 5499, 5502, 5505, 5507, 5510, 5512, 5515, 5520, 5523, 5528, 5532, 5535, 5541, 5543, 5545, 5547, 5551, 5553, 5555, 5557, 5559, 5563, 5565, 5567, 5569, 5573, 5577, 5579, 5581, 5585, 5587, 5589, 5591, 5593, 5595, 5599, 5601, 5603, 5605, 5607, 5609, 5612, 5614, 5616, 5623, 5625, 5628, 5630, 5632, 5635, 5638, 5640, 5642, 5645, 5647, 5649, 5651, 5653, 5656, 5658, 5453, 5186, 5461, 5199, 5492, 5028, 5026, 5038, 5036, 5453, 5452, 5461, 5199, 5492, 5028, 5026, 5038, 5036, 5453, 5452, 5461, 5483, 5492, 5028, 5026, 5038, 2399, 2400, 5570, 5406, 5235, 2419, 5237, 2424, 5297, 2431, 2432, 5223, 5253, 5258, 4779, 4787, 5264, 5227, 5268, 5267, 5272, 5271, 5230, 4462, 5296, 4462, 5297, 5406, 5235, 5618, 2503, 5237, 2508, 5297, 5253, 5258, 4779, 4787, 5264, 5263, 5268, 5267, 5272, 5271, 5337, 2555, 2556, 5295, 4496, 5296, 4496, 5297, 4779, 4787, 5315, 5314, 5337, 2605, 4865, 2607, 4868, 2613, 5406, 5405, 5618, 2641, 2642, 2644, 2645, 5432, 5431, 5453, 5452, 5461, 5483, 5492, 5028, 5026, 5038, 5036, 5570, 5620, 5533, 5620, 5538, 5537, 5539, 5549, 5561, 5570, 5620, 5574, 5575, 5620, 5618, 5626, 5636, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5799, 5798, 5797, 5696, 2289, 2290, 5800, 5802, 5801, 2294, 5697, 5703, 5774, 5775, 5805, 5807, 5698, 5700, 5699, 2304, 5814, 5812, 2307, 5817, 5815, 5818, 5820, 5701, 5192, 5823, 5822, 2316, 2317, 5825, 5824, 2320, 2321, 5826, 5193, 5799, 5798, 5797, 5796, 2328, 2329, 5802, 5801, 5800, 2333, 5772, 5703, 5774, 5775, 5805, 5808, 5807, 5700, 5699, 2343, 5814, 5812, 2346, 5817, 5815, 5818, 5820, 5701, 5202, 5823, 5822, 2355, 2356, 5825, 5824, 2359, 2360, 5826, 5203, 5799, 5798, 5797, 5796, 2367, 2368, 5802, 5801, 5800, 2372, 5702, 5703, 5704, 5775, 5805, 5808, 5807, 5706, 5705, 2382, 5814, 5813, 2385, 5817, 5816, 5818, 5820, 5819, 5707, 5823, 5822, 2394, 2395, 5825, 5824, 2398, 5826, 5530, 5713, 5855, 5714, 5859, 5858, 5860, 2409, 2410, 2411, 5782, 5781, 5783, 5785, 5420, 5788, 5787, 2420, 4457, 5874, 5875, 2425, 4457, 5793, 5794, 5756, 5736, 5913, 5761, 5760, 5748, 5747, 5710, 5751, 5750, 5752, 2441, 5729, 5755, 5754, 5730, 5732, 4528, 5734, 5721, 5711, 2451, 5723, 5722, 2454, 5738, 5737, 2457, 5740, 5725, 5724, 2461, 2462, 2463, 5712, 2465, 2466, 5727, 2468, 2469, 5728, 5715, 5717, 4528, 5719, 2475, 2476, 2477, 2478, 5768, 5770, 5769, 5771, 2483, 4462, 5793, 5794, 5713, 5841, 5714, 5859, 5858, 5860, 2493, 2494, 2495, 5782, 5781, 5783, 5785, 5420, 5788, 5787, 2504, 4469, 5874, 5875, 2509, 4469, 5793, 5794, 5715, 5717, 4528, 5719, 5721, 5720, 2519, 5723, 5722, 2522, 5738, 5737, 2525, 5740, 5725, 5724, 2529, 2530, 2531, 5726, 2533, 2534, 5727, 2536, 2537, 5728, 5748, 5747, 5749, 5751, 5750, 5752, 2545, 5729, 5755, 5754, 5730, 5732, 4528, 5734, 5756, 5736, 5949, 5761, 5760, 2559, 2560, 2561, 2562, 5768, 5770, 5769, 5771, 2567, 4496, 5793, 5794, 5738, 5737, 2573, 5740, 5739, 2576, 5742, 5741, 5743, 2580, 2581, 5746, 5745, 5744, 5748, 5747, 5749, 5751, 5750, 5752, 2591, 5755, 5754, 5753, 5756, 5758, 4523, 5761, 5760, 5762, 5764, 4528, 5767, 5766, 2606, 2608, 5768, 5771, 5770, 5769, 4881, 5793, 5794, 5772, 5773, 5774, 5775, 5778, 5777, 5776, 5779, 5855, 5840, 5857, 5859, 5858, 5860, 2631, 2632, 2633, 5782, 5781, 5783, 5785, 5420, 5788, 5787, 5969, 4931, 5971, 4936, 5874, 5875, 2649, 2650, 5791, 5793, 5794, 5799, 5798, 5797, 5796, 2658, 2659, 5802, 5801, 5800, 2663, 5803, 5804, 5805, 5808, 5807, 5811, 5810, 5809, 2672, 5814, 5813, 5812, 2676, 5817, 5816, 5815, 5818, 5820, 5819, 5508, 5823, 5822, 2686, 2687, 5825, 5824, 2690, 2691, 5826, 5530, 5843, 5845, 5583, 5848, 5847, 5849, 5851, 5597, 5854, 5853, 5841, 5856, 5857, 5858, 5859, 5860, 2710, 2711, 5861, 2713, 5828, 5874, 5875, 5843, 5845, 5583, 5848, 5847, 5849, 5851, 5597, 5854, 5853, 5841, 5856, 5857, 5858, 5859, 5860, 2733, 2734, 2735, 5861, 2737, 5829, 5874, 5875, 5830, 5832, 2743, 5834, 5833, 5835, 5837, 2748, 5839, 5838, 5843, 5845, 5583, 5848, 5847, 5841, 5840, 5857, 5858, 5859, 5860, 2762, 2763, 5861, 2765, 5863, 5865, 5866, 2769, 5867, 5869, 5870, 5843, 5845, 5583, 5848, 5847, 5849, 5851, 5597, 5854, 5853, 5856, 5855, 5857, 5859, 5858, 5860, 2789, 2790, 5861, 2792, 5863, 5865, 5866, 2796, 5867, 5869, 5870, 5871, 5872, 5874, 5875, 26, 27, 28, 29, 30, 31, 2285, 2286, 2287, 2288, 6021, 2291, 2292, 2293, 2295, 2296, 2297, 2298, 2299, 2300, 2301, 2302, 2303, 2305, 2306, 2308, 2309, 2310, 2311, 2312, 2313, 2314, 2315, 6048, 2318, 2319, 6052, 2322, 2323, 2324, 2325, 2326, 2327, 6060, 2330, 2331, 2332, 2334, 2335, 2336, 2337, 2338, 2339, 2340, 2341, 2342, 2344, 2345, 2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 6087, 2357, 2358, 6091, 2361, 2362, 2363, 2364, 2365, 2366, 6099, 2369, 2370, 2371, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380, 2381, 2383, 2384, 2386, 2387, 2388, 2389, 2390, 2391, 2392, 2393, 6126, 2396, 2397, 5903, 2401, 2402, 2403, 2404, 2405, 2406, 2407, 2408, 6139, 2412, 2413, 2414, 2415, 2416, 2417, 2418, 6148, 2421, 2422, 2423, 6152, 2426, 2427, 2428, 2429, 2430, 2433, 2434, 2435, 2436, 2437, 2438, 2439, 2440, 2442, 2443, 2444, 2445, 2446, 2447, 2448, 2449, 2450, 2452, 2453, 2455, 2456, 2458, 2459, 2460, 6189, 2464, 6192, 2467, 6195, 2470, 2471, 2472, 2473, 2474, 2479, 2480, 2481, 2482, 2484, 2485, 2486, 2487, 2488, 2489, 2490, 2491, 2492, 6220, 2496, 2497, 2498, 2499, 2500, 2501, 2502, 6229, 2505, 2506, 2507, 6233, 2510, 2511, 2512, 2513, 2514, 2515, 2516, 2517, 2518, 2520, 2521, 2523, 2524, 2526, 2527, 2528, 6255, 2532, 6258, 2535, 6261, 2538, 2539, 2540, 2541, 2542, 2543, 2544, 2546, 2547, 2548, 2549, 2550, 2551, 2552, 2553, 2554, 2557, 2558, 2563, 2564, 2565, 2566, 2568, 2569, 2570, 2571, 2572, 2574, 2575, 2577, 2578, 2579, 6304, 2582, 2583, 2584, 2585, 2586, 2587, 2588, 2589, 2590, 2592, 2593, 2594, 2595, 2596, 2597, 2598, 2599, 2600, 2601, 2602, 2603, 2604, 2609, 2610, 2611, 2612, 2614, 2615, 2616, 2617, 2618, 2619, 2620, 2621, 2622, 2623, 2624, 2625, 2626, 2627, 2628, 2629, 2630, 6352, 2634, 2635, 2636, 2637, 2638, 2639, 2640, 2643, 2646, 2647, 2648, 6368, 2651, 2652, 2653, 2654, 2655, 2656, 2657, 6377, 2660, 2661, 2662, 2664, 2665, 2666, 2667, 2668, 2669, 2670, 2671, 2673, 2674, 2675, 2677, 2678, 2679, 2680, 2681, 2682, 2683, 2684, 2685, 6405, 2688, 2689, 6409, 2692, 2693, 2694, 2695, 2696, 2697, 2698, 2699, 2700, 2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 6429, 2712, 2714, 2715, 2716, 2717, 2718, 2719, 2720, 2721, 2722, 2723, 2724, 2725, 2726, 2727, 2728, 2729, 2730, 2731, 2732, 6452, 2736, 2738, 2739, 2740, 2741, 2742, 2744, 2745, 2746, 2747, 2749, 2750, 2751, 2752, 2753, 2754, 2755, 2756, 2757, 2758, 2759, 2760, 2761, 6481, 2764, 2766, 2767, 2768, 2770, 2771, 2772, 2773, 2774, 2775, 2776, 2777, 2778, 2779, 2780, 2781, 2782, 2783, 2784, 2785, 2786, 2787, 2788, 6508, 2791, 2793, 2794, 2795, 2797, 2798, 2799, 2800, 2801, 2802, 2803, 6203, 6201, 6284, 6282, 5962, 5960, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 6529, 6531, 6534, 6542, 6544, 6546, 6548, 6551, 6554, 6557, 6562, 6564, 6567, 6575, 6577, 6579, 6581, 6584, 6587, 6590, 6595, 6597, 6600, 6608, 6610, 6612, 6614, 6617, 6620, 6623, 5904, 6628, 6631, 6140, 6635, 6640, 6652, 6654, 6657, 6660, 6667, 6669, 6671, 6673, 6687, 6693, 6696, 6221, 6700, 6705, 6719, 6721, 6723, 6725, 6734, 6737, 6740, 6749, 6752, 6758, 6760, 6762, 6766, 6769, 6772, 6775, 6781, 6786, 6789, 6799, 6803, 6806, 6353, 6810, 6815, 6825, 6827, 6830, 6836, 6838, 6841, 6844, 6848, 6851, 6854, 6862, 6867, 6869, 6872, 6883, 6888, 6890, 6893, 6453, 6903, 6907, 6912, 6914, 6917, 6931, 6936, 6938, 6941, 6538, 6536, 6559, 6571, 6569, 6592, 6604, 6602, 6625, 6636, 6641, 6643, 6645, 6647, 6649, 6662, 6664, 6679, 6677, 6675, 6681, 6683, 2865, 2866, 6209, 6690, 6701, 6706, 6708, 6710, 6712, 6714, 6716, 6731, 6729, 6727, 6742, 6744, 6746, 2895, 2896, 6290, 6755, 6777, 6782, 2911, 2912, 5964, 6792, 6796, 6794, 6856, 6811, 6363, 6361, 6818, 6820, 6822, 6832, 6856, 6858, 6863, 6874, 6431, 6877, 6879, 6884, 6455, 6898, 6900, 6904, 6908, 6919, 6483, 6922, 6487, 6925, 6927, 6932, 6943, 6510, 6946, 6514, 6949, 6951, 6953, 29, 30, 31, 6977, 6535, 6987, 6568, 6997, 6601, 6661, 6674, 6688, 6726, 6741, 6753, 6767, 6776, 6790, 6800, 7052, 6831, 6839, 6842, 6845, 6540, 2807, 2808, 6982, 6981, 6980, 6983, 6985, 6984, 2815, 6573, 2819, 2820, 6992, 6991, 6990, 6993, 6995, 6994, 2827, 6606, 2831, 2832, 7002, 7001, 7000, 7003, 7005, 7004, 2839, 7008, 7007, 7009, 2843, 6638, 2845, 2846, 2847, 2848, 2849, 6158, 7014, 7013, 2854, 2855, 7018, 7017, 7016, 2860, 2861, 2862, 2863, 2864, 7102, 2868, 2869, 7022, 7021, 7023, 2873, 6703, 2875, 2876, 2877, 2878, 2879, 2880, 7028, 7027, 7026, 2885, 2886, 2887, 7031, 7030, 2891, 2892, 2893, 6279, 7119, 2898, 2899, 7037, 7036, 7035, 7040, 7039, 2907, 6779, 2909, 6784, 7125, 2914, 2915, 6834, 2919, 2920, 7058, 7060, 7059, 2927, 7047, 7046, 7048, 2931, 6813, 2933, 2934, 2935, 2936, 2937, 6834, 2941, 7058, 7060, 7059, 2948, 2949, 6860, 2951, 6865, 7064, 7063, 2955, 2956, 2957, 2958, 6881, 2960, 6886, 7068, 7067, 7069, 2965, 2966, 2967, 6461, 2969, 6466, 2971, 6910, 7074, 7073, 2975, 2976, 2977, 2978, 2979, 2980, 6929, 2982, 6934, 7078, 7077, 2986, 2987, 2988, 2989, 2990, 2991, 2992, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7169, 7168, 2806, 2809, 2810, 2811, 2812, 2813, 2814, 7171, 7170, 2818, 2821, 2822, 2823, 2824, 2825, 2826, 7173, 7172, 2830, 2833, 2834, 2835, 2836, 2837, 2838, 2840, 2841, 2842, 2844, 2850, 2851, 2852, 6167, 7175, 2857, 2858, 2859, 7238, 6685, 2870, 2871, 2872, 2874, 7177, 2882, 2883, 2884, 7260, 2888, 2889, 6269, 2894, 6750, 2900, 2901, 2902, 6764, 2904, 2905, 6314, 2908, 2910, 6787, 7185, 7184, 2918, 7188, 7187, 7183, 2924, 2925, 2926, 2928, 2929, 2930, 2932, 7296, 7185, 7184, 2940, 7188, 7187, 7186, 2945, 2946, 2947, 2950, 2952, 2953, 2954, 2959, 2961, 2962, 2963, 2964, 2968, 2970, 2972, 2973, 2974, 2981, 2983, 2984, 2985, 7226, 7224, 7240, 7232, 7243, 7252, 7250, 7254, 7264, 7269, 7281, 7298, 7313, 7322, 7335, 7333, 7348, 7346, 7344, 29, 30, 31, 2804, 2805, 7190, 7364, 7368, 2816, 2817, 7200, 7373, 7377, 2828, 2829, 7210, 7382, 7386, 7388, 7393, 2853, 2856, 7398, 7239, 2867, 7402, 2881, 7408, 7261, 7411, 2890, 2897, 7416, 2903, 7420, 2906, 2913, 2916, 2917, 7284, 2921, 2922, 2923, 7433, 7435, 2938, 2939, 7301, 2942, 2943, 2944, 7447, 7451, 7455, 7461, 7465, 7222, 3004, 3005, 3006, 3009, 7228, 3011, 7248, 3015, 3016, 3019, 7266, 3021, 3022, 7278, 7276, 3028, 7293, 3035, 7438, 7308, 7306, 3043, 7317, 7315, 3047, 7328, 7326, 7324, 3052, 3053, 7339, 7337, 3057, 3058, 3059, 25, 26, 27, 28, 29, 30, 31, 7489, 7191, 7365, 7494, 7201, 7374, 7499, 7211, 7383, 7396, 7406, 7417, 7523, 7285, 7526, 7531, 7534, 7492, 7497, 7502, 3002, 7503, 7543, 7504, 3010, 7242, 3013, 7510, 7550, 7514, 3020, 7268, 3024, 3025, 7519, 7280, 7528, 3033, 7529, 3036, 7536, 7537, 3041, 3042, 7538, 3045, 3046, 7539, 3049, 3050, 3051, 7571, 7540, 3055, 3056, 7575, 24, 25, 26, 27, 28, 29, 30, 31, 7507, 7512, 7527, 7535, 7584, 2994, 7586, 7587, 2997, 7589, 7590, 3000, 7592, 3003, 3007, 3012, 3014, 3018, 7614, 3023, 3026, 7595, 7617, 3029, 7596, 3031, 3034, 7623, 7599, 3038, 3040, 3044, 3048, 7634, 3054, 7576, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 2993, 2995, 2996, 2998, 2999, 3001, 7661, 7648, 7662, 7663, 7664, 7649, 7667, 3027, 7671, 3030, 7650, 7674, 3037, 7651, 7626, 7629, 7632, 7637, 24, 25, 26, 27, 28, 29, 30, 31, 7713, 7715, 7717, 3008, 3017, 7725, 3032, 3039, 7627, 7630, 7681, 7638, 7722, 7718, 7729, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7545, 7665, 7749, 7750, 7751, 7716, 7714, 7712, 3063, 3067, 3069, 7755, 7754, 7753, 7752, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7776, 7666, 3060, 3061, 3062, 7778, 7727, 7730, 3071, 3072, 3073, 3074, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7608, 7553, 7811, 3065, 3068, 3070, 7817, 7819, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7812, 7841, 7840, 7847, 7844, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3064, 3066, 7845, 3076, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7904, 7905, 3075, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7937, 7907, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7785, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 7872, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3077, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 8064, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; bool h_Op[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; #define THREADS_PER_BLOCK 32 #define BLOCKS_PER_GRID 1 #define SIZE_OF_IN 3104 #define SIZE_OF_AC 5024 __device__ void ac(float *A, const int *B, const int *C, const bool *Op, int n_iter) { int i= blockDim.x * blockIdx.x + threadIdx.x; __shared__ float R[254*THREADS_PER_BLOCK]; const int t= THREADS_PER_BLOCK; __shared__ float final; final=0; R[i + 0*t] = A[i + 0*t]; R[i + 1*t] = A[i + 1*t]; R[i + 2*t] = A[i + 2*t]; R[i + 3*t] = A[i + 3*t]; R[i + 4*t] = A[i + 4*t]; R[i + 5*t] = A[i + 5*t]; R[i + 6*t] = A[i + 6*t]; R[i + 7*t] = A[i + 7*t]; R[i + 8*t] = A[i + 8*t]; R[i + 9*t] = A[i + 9*t]; R[i + 10*t] = A[i + 10*t]; R[i + 11*t] = A[i + 11*t]; R[i + 12*t] = A[i + 12*t]; R[i + 13*t] = A[i + 13*t]; R[i + 14*t] = A[i + 14*t]; R[i + 15*t] = A[i + 15*t]; R[i + 16*t] = A[i + 16*t]; R[i + 17*t] = A[i + 17*t]; R[i + 18*t] = A[i + 18*t]; R[i + 19*t] = A[i + 19*t]; R[i + 20*t] = A[i + 20*t]; R[i + 21*t] = A[i + 21*t]; R[i + 22*t] = A[i + 22*t]; R[i + 23*t] = A[i + 23*t]; R[i + 24*t] = A[i + 24*t]; R[i + 25*t] = A[i + 25*t]; R[i + 26*t] = A[i + 26*t]; R[i + 27*t] = A[i + 27*t]; R[i + 28*t] = A[i + 28*t]; R[i + 29*t] = A[i + 29*t]; R[i + 30*t] = A[i + 30*t]; R[i + 31*t] = A[i + 31*t]; R[i + 32*t] = A[i + 32*t]; R[i + 33*t] = A[i + 33*t]; R[i + 34*t] = A[i + 34*t]; R[i + 35*t] = A[i + 35*t]; R[i + 36*t] = A[i + 36*t]; R[i + 37*t] = A[i + 37*t]; R[i + 38*t] = A[i + 38*t]; R[i + 39*t] = A[i + 39*t]; R[i + 40*t] = A[i + 40*t]; R[i + 41*t] = A[i + 41*t]; R[i + 42*t] = A[i + 42*t]; R[i + 43*t] = A[i + 43*t]; R[i + 44*t] = A[i + 44*t]; R[i + 45*t] = A[i + 45*t]; R[i + 46*t] = A[i + 46*t]; R[i + 47*t] = A[i + 47*t]; R[i + 48*t] = A[i + 48*t]; R[i + 49*t] = A[i + 49*t]; R[i + 50*t] = A[i + 50*t]; R[i + 51*t] = A[i + 51*t]; R[i + 52*t] = A[i + 52*t]; R[i + 53*t] = A[i + 53*t]; R[i + 54*t] = A[i + 54*t]; R[i + 55*t] = A[i + 55*t]; R[i + 56*t] = A[i + 56*t]; R[i + 57*t] = A[i + 57*t]; R[i + 58*t] = A[i + 58*t]; R[i + 59*t] = A[i + 59*t]; R[i + 60*t] = A[i + 60*t]; R[i + 61*t] = A[i + 61*t]; R[i + 62*t] = A[i + 62*t]; R[i + 63*t] = A[i + 63*t]; R[i + 64*t] = A[i + 64*t]; R[i + 65*t] = A[i + 65*t]; R[i + 66*t] = A[i + 66*t]; R[i + 67*t] = A[i + 67*t]; R[i + 68*t] = A[i + 68*t]; R[i + 69*t] = A[i + 69*t]; R[i + 70*t] = A[i + 70*t]; R[i + 71*t] = A[i + 71*t]; R[i + 72*t] = A[i + 72*t]; R[i + 73*t] = A[i + 73*t]; R[i + 74*t] = A[i + 74*t]; R[i + 75*t] = A[i + 75*t]; R[i + 76*t] = A[i + 76*t]; R[i + 77*t] = A[i + 77*t]; R[i + 78*t] = A[i + 78*t]; R[i + 79*t] = A[i + 79*t]; R[i + 80*t] = A[i + 80*t]; R[i + 81*t] = A[i + 81*t]; R[i + 82*t] = A[i + 82*t]; R[i + 83*t] = A[i + 83*t]; R[i + 84*t] = A[i + 84*t]; R[i + 85*t] = A[i + 85*t]; R[i + 86*t] = A[i + 86*t]; R[i + 87*t] = A[i + 87*t]; R[i + 88*t] = A[i + 88*t]; R[i + 89*t] = A[i + 89*t]; R[i + 90*t] = A[i + 90*t]; R[i + 91*t] = A[i + 91*t]; R[i + 92*t] = A[i + 92*t]; R[i + 93*t] = A[i + 93*t]; R[i + 94*t] = A[i + 94*t]; R[i + 95*t] = A[i + 95*t]; R[i + 96*t] = A[i + 96*t]; __syncthreads(); for (int iter=0; iter< n_iter; iter++) { R[i + 97*t] = Op[i + 0*t] ? R[B[i + 0*t]] * R[C[i + 0*t]] : R[B[i + 0*t]] + R[C[i + 0*t]]; R[i + 98*t] = Op[i + 1*t] ? R[B[i + 1*t]] * R[C[i + 1*t]] : R[B[i + 1*t]] + R[C[i + 1*t]]; R[i + 99*t] = Op[i + 2*t] ? R[B[i + 2*t]] * R[C[i + 2*t]] : R[B[i + 2*t]] + R[C[i + 2*t]]; R[i + 100*t] = Op[i + 3*t] ? R[B[i + 3*t]] * R[C[i + 3*t]] : R[B[i + 3*t]] + R[C[i + 3*t]]; R[i + 101*t] = Op[i + 4*t] ? R[B[i + 4*t]] * R[C[i + 4*t]] : R[B[i + 4*t]] + R[C[i + 4*t]]; R[i + 102*t] = Op[i + 5*t] ? R[B[i + 5*t]] * R[C[i + 5*t]] : R[B[i + 5*t]] + R[C[i + 5*t]]; R[i + 103*t] = Op[i + 6*t] ? R[B[i + 6*t]] * R[C[i + 6*t]] : R[B[i + 6*t]] + R[C[i + 6*t]]; R[i + 104*t] = Op[i + 7*t] ? R[B[i + 7*t]] * R[C[i + 7*t]] : R[B[i + 7*t]] + R[C[i + 7*t]]; R[i + 105*t] = Op[i + 8*t] ? R[B[i + 8*t]] * R[C[i + 8*t]] : R[B[i + 8*t]] + R[C[i + 8*t]]; R[i + 106*t] = Op[i + 9*t] ? R[B[i + 9*t]] * R[C[i + 9*t]] : R[B[i + 9*t]] + R[C[i + 9*t]]; R[i + 107*t] = Op[i + 10*t] ? R[B[i + 10*t]] * R[C[i + 10*t]] : R[B[i + 10*t]] + R[C[i + 10*t]]; R[i + 108*t] = Op[i + 11*t] ? R[B[i + 11*t]] * R[C[i + 11*t]] : R[B[i + 11*t]] + R[C[i + 11*t]]; R[i + 109*t] = Op[i + 12*t] ? R[B[i + 12*t]] * R[C[i + 12*t]] : R[B[i + 12*t]] + R[C[i + 12*t]]; R[i + 110*t] = Op[i + 13*t] ? R[B[i + 13*t]] * R[C[i + 13*t]] : R[B[i + 13*t]] + R[C[i + 13*t]]; R[i + 111*t] = Op[i + 14*t] ? R[B[i + 14*t]] * R[C[i + 14*t]] : R[B[i + 14*t]] + R[C[i + 14*t]]; R[i + 112*t] = Op[i + 15*t] ? R[B[i + 15*t]] * R[C[i + 15*t]] : R[B[i + 15*t]] + R[C[i + 15*t]]; R[i + 113*t] = Op[i + 16*t] ? R[B[i + 16*t]] * R[C[i + 16*t]] : R[B[i + 16*t]] + R[C[i + 16*t]]; R[i + 114*t] = Op[i + 17*t] ? R[B[i + 17*t]] * R[C[i + 17*t]] : R[B[i + 17*t]] + R[C[i + 17*t]]; R[i + 115*t] = Op[i + 18*t] ? R[B[i + 18*t]] * R[C[i + 18*t]] : R[B[i + 18*t]] + R[C[i + 18*t]]; R[i + 116*t] = Op[i + 19*t] ? R[B[i + 19*t]] * R[C[i + 19*t]] : R[B[i + 19*t]] + R[C[i + 19*t]]; R[i + 117*t] = Op[i + 20*t] ? R[B[i + 20*t]] * R[C[i + 20*t]] : R[B[i + 20*t]] + R[C[i + 20*t]]; R[i + 118*t] = Op[i + 21*t] ? R[B[i + 21*t]] * R[C[i + 21*t]] : R[B[i + 21*t]] + R[C[i + 21*t]]; R[i + 119*t] = Op[i + 22*t] ? R[B[i + 22*t]] * R[C[i + 22*t]] : R[B[i + 22*t]] + R[C[i + 22*t]]; R[i + 120*t] = Op[i + 23*t] ? R[B[i + 23*t]] * R[C[i + 23*t]] : R[B[i + 23*t]] + R[C[i + 23*t]]; R[i + 121*t] = Op[i + 24*t] ? R[B[i + 24*t]] * R[C[i + 24*t]] : R[B[i + 24*t]] + R[C[i + 24*t]]; R[i + 122*t] = Op[i + 25*t] ? R[B[i + 25*t]] * R[C[i + 25*t]] : R[B[i + 25*t]] + R[C[i + 25*t]]; R[i + 123*t] = Op[i + 26*t] ? R[B[i + 26*t]] * R[C[i + 26*t]] : R[B[i + 26*t]] + R[C[i + 26*t]]; R[i + 124*t] = Op[i + 27*t] ? R[B[i + 27*t]] * R[C[i + 27*t]] : R[B[i + 27*t]] + R[C[i + 27*t]]; R[i + 125*t] = Op[i + 28*t] ? R[B[i + 28*t]] * R[C[i + 28*t]] : R[B[i + 28*t]] + R[C[i + 28*t]]; R[i + 126*t] = Op[i + 29*t] ? R[B[i + 29*t]] * R[C[i + 29*t]] : R[B[i + 29*t]] + R[C[i + 29*t]]; __syncthreads(); R[i + 127*t] = Op[i + 30*t] ? R[B[i + 30*t]] * R[C[i + 30*t]] : R[B[i + 30*t]] + R[C[i + 30*t]]; R[i + 128*t] = Op[i + 31*t] ? R[B[i + 31*t]] * R[C[i + 31*t]] : R[B[i + 31*t]] + R[C[i + 31*t]]; R[i + 129*t] = Op[i + 32*t] ? R[B[i + 32*t]] * R[C[i + 32*t]] : R[B[i + 32*t]] + R[C[i + 32*t]]; R[i + 130*t] = Op[i + 33*t] ? R[B[i + 33*t]] * R[C[i + 33*t]] : R[B[i + 33*t]] + R[C[i + 33*t]]; R[i + 131*t] = Op[i + 34*t] ? R[B[i + 34*t]] * R[C[i + 34*t]] : R[B[i + 34*t]] + R[C[i + 34*t]]; R[i + 132*t] = Op[i + 35*t] ? R[B[i + 35*t]] * R[C[i + 35*t]] : R[B[i + 35*t]] + R[C[i + 35*t]]; R[i + 133*t] = Op[i + 36*t] ? R[B[i + 36*t]] * R[C[i + 36*t]] : R[B[i + 36*t]] + R[C[i + 36*t]]; R[i + 134*t] = Op[i + 37*t] ? R[B[i + 37*t]] * R[C[i + 37*t]] : R[B[i + 37*t]] + R[C[i + 37*t]]; R[i + 135*t] = Op[i + 38*t] ? R[B[i + 38*t]] * R[C[i + 38*t]] : R[B[i + 38*t]] + R[C[i + 38*t]]; R[i + 136*t] = Op[i + 39*t] ? R[B[i + 39*t]] * R[C[i + 39*t]] : R[B[i + 39*t]] + R[C[i + 39*t]]; R[i + 137*t] = Op[i + 40*t] ? R[B[i + 40*t]] * R[C[i + 40*t]] : R[B[i + 40*t]] + R[C[i + 40*t]]; R[i + 138*t] = Op[i + 41*t] ? R[B[i + 41*t]] * R[C[i + 41*t]] : R[B[i + 41*t]] + R[C[i + 41*t]]; R[i + 139*t] = Op[i + 42*t] ? R[B[i + 42*t]] * R[C[i + 42*t]] : R[B[i + 42*t]] + R[C[i + 42*t]]; R[i + 140*t] = Op[i + 43*t] ? R[B[i + 43*t]] * R[C[i + 43*t]] : R[B[i + 43*t]] + R[C[i + 43*t]]; R[i + 141*t] = Op[i + 44*t] ? R[B[i + 44*t]] * R[C[i + 44*t]] : R[B[i + 44*t]] + R[C[i + 44*t]]; R[i + 142*t] = Op[i + 45*t] ? R[B[i + 45*t]] * R[C[i + 45*t]] : R[B[i + 45*t]] + R[C[i + 45*t]]; R[i + 143*t] = Op[i + 46*t] ? R[B[i + 46*t]] * R[C[i + 46*t]] : R[B[i + 46*t]] + R[C[i + 46*t]]; R[i + 144*t] = Op[i + 47*t] ? R[B[i + 47*t]] * R[C[i + 47*t]] : R[B[i + 47*t]] + R[C[i + 47*t]]; __syncthreads(); R[i + 145*t] = Op[i + 48*t] ? R[B[i + 48*t]] * R[C[i + 48*t]] : R[B[i + 48*t]] + R[C[i + 48*t]]; R[i + 146*t] = Op[i + 49*t] ? R[B[i + 49*t]] * R[C[i + 49*t]] : R[B[i + 49*t]] + R[C[i + 49*t]]; R[i + 147*t] = Op[i + 50*t] ? R[B[i + 50*t]] * R[C[i + 50*t]] : R[B[i + 50*t]] + R[C[i + 50*t]]; R[i + 148*t] = Op[i + 51*t] ? R[B[i + 51*t]] * R[C[i + 51*t]] : R[B[i + 51*t]] + R[C[i + 51*t]]; R[i + 149*t] = Op[i + 52*t] ? R[B[i + 52*t]] * R[C[i + 52*t]] : R[B[i + 52*t]] + R[C[i + 52*t]]; R[i + 150*t] = Op[i + 53*t] ? R[B[i + 53*t]] * R[C[i + 53*t]] : R[B[i + 53*t]] + R[C[i + 53*t]]; R[i + 151*t] = Op[i + 54*t] ? R[B[i + 54*t]] * R[C[i + 54*t]] : R[B[i + 54*t]] + R[C[i + 54*t]]; R[i + 152*t] = Op[i + 55*t] ? R[B[i + 55*t]] * R[C[i + 55*t]] : R[B[i + 55*t]] + R[C[i + 55*t]]; R[i + 153*t] = Op[i + 56*t] ? R[B[i + 56*t]] * R[C[i + 56*t]] : R[B[i + 56*t]] + R[C[i + 56*t]]; R[i + 154*t] = Op[i + 57*t] ? R[B[i + 57*t]] * R[C[i + 57*t]] : R[B[i + 57*t]] + R[C[i + 57*t]]; R[i + 155*t] = Op[i + 58*t] ? R[B[i + 58*t]] * R[C[i + 58*t]] : R[B[i + 58*t]] + R[C[i + 58*t]]; R[i + 156*t] = Op[i + 59*t] ? R[B[i + 59*t]] * R[C[i + 59*t]] : R[B[i + 59*t]] + R[C[i + 59*t]]; R[i + 157*t] = Op[i + 60*t] ? R[B[i + 60*t]] * R[C[i + 60*t]] : R[B[i + 60*t]] + R[C[i + 60*t]]; R[i + 158*t] = Op[i + 61*t] ? R[B[i + 61*t]] * R[C[i + 61*t]] : R[B[i + 61*t]] + R[C[i + 61*t]]; R[i + 159*t] = Op[i + 62*t] ? R[B[i + 62*t]] * R[C[i + 62*t]] : R[B[i + 62*t]] + R[C[i + 62*t]]; R[i + 160*t] = Op[i + 63*t] ? R[B[i + 63*t]] * R[C[i + 63*t]] : R[B[i + 63*t]] + R[C[i + 63*t]]; R[i + 161*t] = Op[i + 64*t] ? R[B[i + 64*t]] * R[C[i + 64*t]] : R[B[i + 64*t]] + R[C[i + 64*t]]; __syncthreads(); R[i + 162*t] = Op[i + 65*t] ? R[B[i + 65*t]] * R[C[i + 65*t]] : R[B[i + 65*t]] + R[C[i + 65*t]]; R[i + 163*t] = Op[i + 66*t] ? R[B[i + 66*t]] * R[C[i + 66*t]] : R[B[i + 66*t]] + R[C[i + 66*t]]; R[i + 164*t] = Op[i + 67*t] ? R[B[i + 67*t]] * R[C[i + 67*t]] : R[B[i + 67*t]] + R[C[i + 67*t]]; R[i + 165*t] = Op[i + 68*t] ? R[B[i + 68*t]] * R[C[i + 68*t]] : R[B[i + 68*t]] + R[C[i + 68*t]]; R[i + 166*t] = Op[i + 69*t] ? R[B[i + 69*t]] * R[C[i + 69*t]] : R[B[i + 69*t]] + R[C[i + 69*t]]; R[i + 167*t] = Op[i + 70*t] ? R[B[i + 70*t]] * R[C[i + 70*t]] : R[B[i + 70*t]] + R[C[i + 70*t]]; R[i + 168*t] = Op[i + 71*t] ? R[B[i + 71*t]] * R[C[i + 71*t]] : R[B[i + 71*t]] + R[C[i + 71*t]]; R[i + 169*t] = Op[i + 72*t] ? R[B[i + 72*t]] * R[C[i + 72*t]] : R[B[i + 72*t]] + R[C[i + 72*t]]; R[i + 170*t] = Op[i + 73*t] ? R[B[i + 73*t]] * R[C[i + 73*t]] : R[B[i + 73*t]] + R[C[i + 73*t]]; R[i + 171*t] = Op[i + 74*t] ? R[B[i + 74*t]] * R[C[i + 74*t]] : R[B[i + 74*t]] + R[C[i + 74*t]]; R[i + 172*t] = Op[i + 75*t] ? R[B[i + 75*t]] * R[C[i + 75*t]] : R[B[i + 75*t]] + R[C[i + 75*t]]; R[i + 173*t] = Op[i + 76*t] ? R[B[i + 76*t]] * R[C[i + 76*t]] : R[B[i + 76*t]] + R[C[i + 76*t]]; R[i + 174*t] = Op[i + 77*t] ? R[B[i + 77*t]] * R[C[i + 77*t]] : R[B[i + 77*t]] + R[C[i + 77*t]]; R[i + 175*t] = Op[i + 78*t] ? R[B[i + 78*t]] * R[C[i + 78*t]] : R[B[i + 78*t]] + R[C[i + 78*t]]; R[i + 176*t] = Op[i + 79*t] ? R[B[i + 79*t]] * R[C[i + 79*t]] : R[B[i + 79*t]] + R[C[i + 79*t]]; R[i + 177*t] = Op[i + 80*t] ? R[B[i + 80*t]] * R[C[i + 80*t]] : R[B[i + 80*t]] + R[C[i + 80*t]]; __syncthreads(); R[i + 178*t] = Op[i + 81*t] ? R[B[i + 81*t]] * R[C[i + 81*t]] : R[B[i + 81*t]] + R[C[i + 81*t]]; R[i + 179*t] = Op[i + 82*t] ? R[B[i + 82*t]] * R[C[i + 82*t]] : R[B[i + 82*t]] + R[C[i + 82*t]]; R[i + 180*t] = Op[i + 83*t] ? R[B[i + 83*t]] * R[C[i + 83*t]] : R[B[i + 83*t]] + R[C[i + 83*t]]; R[i + 181*t] = Op[i + 84*t] ? R[B[i + 84*t]] * R[C[i + 84*t]] : R[B[i + 84*t]] + R[C[i + 84*t]]; R[i + 182*t] = Op[i + 85*t] ? R[B[i + 85*t]] * R[C[i + 85*t]] : R[B[i + 85*t]] + R[C[i + 85*t]]; R[i + 183*t] = Op[i + 86*t] ? R[B[i + 86*t]] * R[C[i + 86*t]] : R[B[i + 86*t]] + R[C[i + 86*t]]; R[i + 184*t] = Op[i + 87*t] ? R[B[i + 87*t]] * R[C[i + 87*t]] : R[B[i + 87*t]] + R[C[i + 87*t]]; R[i + 185*t] = Op[i + 88*t] ? R[B[i + 88*t]] * R[C[i + 88*t]] : R[B[i + 88*t]] + R[C[i + 88*t]]; R[i + 186*t] = Op[i + 89*t] ? R[B[i + 89*t]] * R[C[i + 89*t]] : R[B[i + 89*t]] + R[C[i + 89*t]]; R[i + 187*t] = Op[i + 90*t] ? R[B[i + 90*t]] * R[C[i + 90*t]] : R[B[i + 90*t]] + R[C[i + 90*t]]; __syncthreads(); R[i + 188*t] = Op[i + 91*t] ? R[B[i + 91*t]] * R[C[i + 91*t]] : R[B[i + 91*t]] + R[C[i + 91*t]]; R[i + 189*t] = Op[i + 92*t] ? R[B[i + 92*t]] * R[C[i + 92*t]] : R[B[i + 92*t]] + R[C[i + 92*t]]; R[i + 190*t] = Op[i + 93*t] ? R[B[i + 93*t]] * R[C[i + 93*t]] : R[B[i + 93*t]] + R[C[i + 93*t]]; R[i + 191*t] = Op[i + 94*t] ? R[B[i + 94*t]] * R[C[i + 94*t]] : R[B[i + 94*t]] + R[C[i + 94*t]]; R[i + 192*t] = Op[i + 95*t] ? R[B[i + 95*t]] * R[C[i + 95*t]] : R[B[i + 95*t]] + R[C[i + 95*t]]; R[i + 193*t] = Op[i + 96*t] ? R[B[i + 96*t]] * R[C[i + 96*t]] : R[B[i + 96*t]] + R[C[i + 96*t]]; R[i + 194*t] = Op[i + 97*t] ? R[B[i + 97*t]] * R[C[i + 97*t]] : R[B[i + 97*t]] + R[C[i + 97*t]]; R[i + 195*t] = Op[i + 98*t] ? R[B[i + 98*t]] * R[C[i + 98*t]] : R[B[i + 98*t]] + R[C[i + 98*t]]; R[i + 196*t] = Op[i + 99*t] ? R[B[i + 99*t]] * R[C[i + 99*t]] : R[B[i + 99*t]] + R[C[i + 99*t]]; R[i + 197*t] = Op[i + 100*t] ? R[B[i + 100*t]] * R[C[i + 100*t]] : R[B[i + 100*t]] + R[C[i + 100*t]]; R[i + 198*t] = Op[i + 101*t] ? R[B[i + 101*t]] * R[C[i + 101*t]] : R[B[i + 101*t]] + R[C[i + 101*t]]; R[i + 199*t] = Op[i + 102*t] ? R[B[i + 102*t]] * R[C[i + 102*t]] : R[B[i + 102*t]] + R[C[i + 102*t]]; R[i + 200*t] = Op[i + 103*t] ? R[B[i + 103*t]] * R[C[i + 103*t]] : R[B[i + 103*t]] + R[C[i + 103*t]]; R[i + 201*t] = Op[i + 104*t] ? R[B[i + 104*t]] * R[C[i + 104*t]] : R[B[i + 104*t]] + R[C[i + 104*t]]; R[i + 202*t] = Op[i + 105*t] ? R[B[i + 105*t]] * R[C[i + 105*t]] : R[B[i + 105*t]] + R[C[i + 105*t]]; R[i + 203*t] = Op[i + 106*t] ? R[B[i + 106*t]] * R[C[i + 106*t]] : R[B[i + 106*t]] + R[C[i + 106*t]]; __syncthreads(); R[i + 204*t] = Op[i + 107*t] ? R[B[i + 107*t]] * R[C[i + 107*t]] : R[B[i + 107*t]] + R[C[i + 107*t]]; R[i + 205*t] = Op[i + 108*t] ? R[B[i + 108*t]] * R[C[i + 108*t]] : R[B[i + 108*t]] + R[C[i + 108*t]]; R[i + 206*t] = Op[i + 109*t] ? R[B[i + 109*t]] * R[C[i + 109*t]] : R[B[i + 109*t]] + R[C[i + 109*t]]; R[i + 207*t] = Op[i + 110*t] ? R[B[i + 110*t]] * R[C[i + 110*t]] : R[B[i + 110*t]] + R[C[i + 110*t]]; R[i + 208*t] = Op[i + 111*t] ? R[B[i + 111*t]] * R[C[i + 111*t]] : R[B[i + 111*t]] + R[C[i + 111*t]]; R[i + 209*t] = Op[i + 112*t] ? R[B[i + 112*t]] * R[C[i + 112*t]] : R[B[i + 112*t]] + R[C[i + 112*t]]; R[i + 210*t] = Op[i + 113*t] ? R[B[i + 113*t]] * R[C[i + 113*t]] : R[B[i + 113*t]] + R[C[i + 113*t]]; R[i + 211*t] = Op[i + 114*t] ? R[B[i + 114*t]] * R[C[i + 114*t]] : R[B[i + 114*t]] + R[C[i + 114*t]]; R[i + 212*t] = Op[i + 115*t] ? R[B[i + 115*t]] * R[C[i + 115*t]] : R[B[i + 115*t]] + R[C[i + 115*t]]; R[i + 213*t] = Op[i + 116*t] ? R[B[i + 116*t]] * R[C[i + 116*t]] : R[B[i + 116*t]] + R[C[i + 116*t]]; R[i + 214*t] = Op[i + 117*t] ? R[B[i + 117*t]] * R[C[i + 117*t]] : R[B[i + 117*t]] + R[C[i + 117*t]]; R[i + 215*t] = Op[i + 118*t] ? R[B[i + 118*t]] * R[C[i + 118*t]] : R[B[i + 118*t]] + R[C[i + 118*t]]; R[i + 216*t] = Op[i + 119*t] ? R[B[i + 119*t]] * R[C[i + 119*t]] : R[B[i + 119*t]] + R[C[i + 119*t]]; R[i + 217*t] = Op[i + 120*t] ? R[B[i + 120*t]] * R[C[i + 120*t]] : R[B[i + 120*t]] + R[C[i + 120*t]]; __syncthreads(); R[i + 218*t] = Op[i + 121*t] ? R[B[i + 121*t]] * R[C[i + 121*t]] : R[B[i + 121*t]] + R[C[i + 121*t]]; R[i + 219*t] = Op[i + 122*t] ? R[B[i + 122*t]] * R[C[i + 122*t]] : R[B[i + 122*t]] + R[C[i + 122*t]]; R[i + 220*t] = Op[i + 123*t] ? R[B[i + 123*t]] * R[C[i + 123*t]] : R[B[i + 123*t]] + R[C[i + 123*t]]; R[i + 221*t] = Op[i + 124*t] ? R[B[i + 124*t]] * R[C[i + 124*t]] : R[B[i + 124*t]] + R[C[i + 124*t]]; R[i + 222*t] = Op[i + 125*t] ? R[B[i + 125*t]] * R[C[i + 125*t]] : R[B[i + 125*t]] + R[C[i + 125*t]]; R[i + 223*t] = Op[i + 126*t] ? R[B[i + 126*t]] * R[C[i + 126*t]] : R[B[i + 126*t]] + R[C[i + 126*t]]; __syncthreads(); R[i + 224*t] = Op[i + 127*t] ? R[B[i + 127*t]] * R[C[i + 127*t]] : R[B[i + 127*t]] + R[C[i + 127*t]]; R[i + 225*t] = Op[i + 128*t] ? R[B[i + 128*t]] * R[C[i + 128*t]] : R[B[i + 128*t]] + R[C[i + 128*t]]; R[i + 226*t] = Op[i + 129*t] ? R[B[i + 129*t]] * R[C[i + 129*t]] : R[B[i + 129*t]] + R[C[i + 129*t]]; R[i + 227*t] = Op[i + 130*t] ? R[B[i + 130*t]] * R[C[i + 130*t]] : R[B[i + 130*t]] + R[C[i + 130*t]]; R[i + 228*t] = Op[i + 131*t] ? R[B[i + 131*t]] * R[C[i + 131*t]] : R[B[i + 131*t]] + R[C[i + 131*t]]; R[i + 229*t] = Op[i + 132*t] ? R[B[i + 132*t]] * R[C[i + 132*t]] : R[B[i + 132*t]] + R[C[i + 132*t]]; __syncthreads(); R[i + 230*t] = Op[i + 133*t] ? R[B[i + 133*t]] * R[C[i + 133*t]] : R[B[i + 133*t]] + R[C[i + 133*t]]; R[i + 231*t] = Op[i + 134*t] ? R[B[i + 134*t]] * R[C[i + 134*t]] : R[B[i + 134*t]] + R[C[i + 134*t]]; R[i + 232*t] = Op[i + 135*t] ? R[B[i + 135*t]] * R[C[i + 135*t]] : R[B[i + 135*t]] + R[C[i + 135*t]]; R[i + 233*t] = Op[i + 136*t] ? R[B[i + 136*t]] * R[C[i + 136*t]] : R[B[i + 136*t]] + R[C[i + 136*t]]; __syncthreads(); R[i + 234*t] = Op[i + 137*t] ? R[B[i + 137*t]] * R[C[i + 137*t]] : R[B[i + 137*t]] + R[C[i + 137*t]]; R[i + 235*t] = Op[i + 138*t] ? R[B[i + 138*t]] * R[C[i + 138*t]] : R[B[i + 138*t]] + R[C[i + 138*t]]; R[i + 236*t] = Op[i + 139*t] ? R[B[i + 139*t]] * R[C[i + 139*t]] : R[B[i + 139*t]] + R[C[i + 139*t]]; __syncthreads(); R[i + 237*t] = Op[i + 140*t] ? R[B[i + 140*t]] * R[C[i + 140*t]] : R[B[i + 140*t]] + R[C[i + 140*t]]; R[i + 238*t] = Op[i + 141*t] ? R[B[i + 141*t]] * R[C[i + 141*t]] : R[B[i + 141*t]] + R[C[i + 141*t]]; __syncthreads(); R[i + 239*t] = Op[i + 142*t] ? R[B[i + 142*t]] * R[C[i + 142*t]] : R[B[i + 142*t]] + R[C[i + 142*t]]; R[i + 240*t] = Op[i + 143*t] ? R[B[i + 143*t]] * R[C[i + 143*t]] : R[B[i + 143*t]] + R[C[i + 143*t]]; __syncthreads(); R[i + 241*t] = Op[i + 144*t] ? R[B[i + 144*t]] * R[C[i + 144*t]] : R[B[i + 144*t]] + R[C[i + 144*t]]; __syncthreads(); R[i + 242*t] = Op[i + 145*t] ? R[B[i + 145*t]] * R[C[i + 145*t]] : R[B[i + 145*t]] + R[C[i + 145*t]]; __syncthreads(); R[i + 243*t] = Op[i + 146*t] ? R[B[i + 146*t]] * R[C[i + 146*t]] : R[B[i + 146*t]] + R[C[i + 146*t]]; __syncthreads(); R[i + 244*t] = Op[i + 147*t] ? R[B[i + 147*t]] * R[C[i + 147*t]] : R[B[i + 147*t]] + R[C[i + 147*t]]; __syncthreads(); R[i + 245*t] = Op[i + 148*t] ? R[B[i + 148*t]] * R[C[i + 148*t]] : R[B[i + 148*t]] + R[C[i + 148*t]]; __syncthreads(); R[i + 246*t] = Op[i + 149*t] ? R[B[i + 149*t]] * R[C[i + 149*t]] : R[B[i + 149*t]] + R[C[i + 149*t]]; __syncthreads(); R[i + 247*t] = Op[i + 150*t] ? R[B[i + 150*t]] * R[C[i + 150*t]] : R[B[i + 150*t]] + R[C[i + 150*t]]; __syncthreads(); R[i + 248*t] = Op[i + 151*t] ? R[B[i + 151*t]] * R[C[i + 151*t]] : R[B[i + 151*t]] + R[C[i + 151*t]]; __syncthreads(); R[i + 249*t] = Op[i + 152*t] ? R[B[i + 152*t]] * R[C[i + 152*t]] : R[B[i + 152*t]] + R[C[i + 152*t]]; __syncthreads(); R[i + 250*t] = Op[i + 153*t] ? R[B[i + 153*t]] * R[C[i + 153*t]] : R[B[i + 153*t]] + R[C[i + 153*t]]; __syncthreads(); R[i + 251*t] = Op[i + 154*t] ? R[B[i + 154*t]] * R[C[i + 154*t]] : R[B[i + 154*t]] + R[C[i + 154*t]]; __syncthreads(); R[i + 252*t] = Op[i + 155*t] ? R[B[i + 155*t]] * R[C[i + 155*t]] : R[B[i + 155*t]] + R[C[i + 155*t]]; __syncthreads(); R[i + 253*t] = Op[i + 156*t] ? R[B[i + 156*t]] * R[C[i + 156*t]] : R[B[i + 156*t]] + R[C[i + 156*t]]; if (i==0) { final += R[253*t]; } __syncthreads(); } if (i==0) { A[0]= final;} }
0d13e455db157b7f529cbe9b85ef6f6ec5035de2.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" #include <string> #include "caffe/util/gpu_math_functions.cuh" #include "caffe/util/math_functions.hpp" #pragma clang diagnostic push #pragma ide diagnostic ignored "CannotResolve" namespace caffe { template<typename Gtype, typename Wtype> __global__ void AdamRegUpdateAllAndClear(int N, Gtype* g, Wtype *w, Wtype* m, Wtype* v, float beta1, float beta2, float eps_hat, float local_rate, float local_decay, bool reg_L2, bool clear_grads) { CUDA_KERNEL_LOOP(i, N) { Wtype reg = reg_L2 ? w[i] : Wtype((Wtype(0) < w[i]) - (w[i] < Wtype(0))); Wtype gr = Wtype(g[i]) + reg * local_decay; Wtype mi = m[i] = m[i] * beta1 + gr * (Wtype(1.) - beta1); Wtype vi = v[i] = v[i] * beta2 + gr * gr * (Wtype(1.) - beta2); gr = local_rate * mi / (sqrt(vi) + eps_hat); w[i] -= gr; g[i] = clear_grads ? Gtype(0) : Gtype(gr); } } #pragma clang diagnostic pop template<> __global__ void AdamRegUpdateAllAndClear<half, half>(int N, half* g, half *w, half* m, half* v, float beta1, float beta2, float eps_hat, float local_rate, float local_decay, bool reg_L2, bool clear_grads) { half hz; CUDA_KERNEL_LOOP(i, N) { float wf = __half2float(w[i]); float gf = __half2float(g[i]); float mf = __half2float(m[i]); float vf = __half2float(v[i]); float reg = reg_L2 ? wf : float((0.F < wf)-(wf < 0.F)); gf += reg * local_decay; mf = beta1 * mf + (1.F - beta1)*gf; vf = beta2 * vf + (1.F - beta2)*gf*gf; gf = local_rate * mf / sqrt(vf + eps_hat); wf -= gf; w[i] = float2half_clip(wf); m[i] = float2half_clip(mf); v[i] = float2half_clip(vf); g[i] = clear_grads ? hz : float2half_clip(gf); } } template<typename Gtype, typename Wtype> void adam_reg_update_and_clear_gpu(int N, Gtype* g, Wtype *w, Wtype* m, Wtype* v, float beta1, float beta2, float eps_hat, float local_rate, const std::string& reg_type, float local_decay, void *handle, bool clear_grads) { hipblasHandle_t cublas_handle = handle == nullptr ? Caffe::cublas_handle(0) : reinterpret_cast<hipblasHandle_t>(handle); hipStream_t stream; CUBLAS_CHECK(hipblasGetStream(cublas_handle, &stream)); AdamRegUpdateAllAndClear // NOLINT_NEXT_LINE(whitespacehipLaunchKernelGGL((/operators)) , dim3(CAFFE_GET_BLOCKS(N)), dim3(CAFFE_CUDA_NUM_THREADS), 0, stream, N, g, w, m, v, beta1, beta2, eps_hat, local_rate, local_decay, reg_type == "L2", clear_grads); CUDA_POST_KERNEL_CHECK; CUDA_CHECK(hipStreamSynchronize(stream)); } template<> void adam_reg_update_and_clear_gpu<float16, float16>(int N, float16 *g, float16 *w, float16 *m, float16 *v, float beta1, float beta2, float eps_hat, float local_rate, const std::string& reg_type, float local_decay, void *handle, bool clear_grads) { hipblasHandle_t cublas_handle = handle == nullptr ? Caffe::cublas_handle(0) : reinterpret_cast<hipblasHandle_t>(handle); hipStream_t stream; CUBLAS_CHECK(hipblasGetStream(cublas_handle, &stream)); AdamRegUpdateAllAndClear // NOLINT_NEXT_LINE(whitespacehipLaunchKernelGGL((/operators)) , dim3(CAFFE_GET_BLOCKS(N)), dim3(CAFFE_CUDA_NUM_THREADS), 0, stream, N, reinterpret_cast<half*>(g), reinterpret_cast<half*>(w), reinterpret_cast<half*>(m), reinterpret_cast<half*>(v), beta1, beta2, eps_hat, local_rate, local_decay, reg_type == "L2", clear_grads); CUDA_POST_KERNEL_CHECK; CUDA_CHECK(hipStreamSynchronize(stream)); } template void adam_reg_update_and_clear_gpu<float16, float>(int, float16*, float*, float*, float*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float16, double>(int, float16*, double*, double*, double*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float, float>(int, float*, float*, float*, float*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float, double>(int, float*, double*, double*, double*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float, float16>(int, float*, float16*, float16*, float16*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<double, float>(int, double*, float*, float*, float*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<double, double>(int, double*, double*, double*, double*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<double, float16>(int, double*, float16*, float16*, float16*, float, float, float, float, const std::string&, float, void*, bool); } // namespace caffe
0d13e455db157b7f529cbe9b85ef6f6ec5035de2.cu
#include <string> #include "caffe/util/gpu_math_functions.cuh" #include "caffe/util/math_functions.hpp" #pragma clang diagnostic push #pragma ide diagnostic ignored "CannotResolve" namespace caffe { template<typename Gtype, typename Wtype> __global__ void AdamRegUpdateAllAndClear(int N, Gtype* g, Wtype *w, Wtype* m, Wtype* v, float beta1, float beta2, float eps_hat, float local_rate, float local_decay, bool reg_L2, bool clear_grads) { CUDA_KERNEL_LOOP(i, N) { Wtype reg = reg_L2 ? w[i] : Wtype((Wtype(0) < w[i]) - (w[i] < Wtype(0))); Wtype gr = Wtype(g[i]) + reg * local_decay; Wtype mi = m[i] = m[i] * beta1 + gr * (Wtype(1.) - beta1); Wtype vi = v[i] = v[i] * beta2 + gr * gr * (Wtype(1.) - beta2); gr = local_rate * mi / (sqrt(vi) + eps_hat); w[i] -= gr; g[i] = clear_grads ? Gtype(0) : Gtype(gr); } } #pragma clang diagnostic pop template<> __global__ void AdamRegUpdateAllAndClear<half, half>(int N, half* g, half *w, half* m, half* v, float beta1, float beta2, float eps_hat, float local_rate, float local_decay, bool reg_L2, bool clear_grads) { half hz; CUDA_KERNEL_LOOP(i, N) { float wf = __half2float(w[i]); float gf = __half2float(g[i]); float mf = __half2float(m[i]); float vf = __half2float(v[i]); float reg = reg_L2 ? wf : float((0.F < wf)-(wf < 0.F)); gf += reg * local_decay; mf = beta1 * mf + (1.F - beta1)*gf; vf = beta2 * vf + (1.F - beta2)*gf*gf; gf = local_rate * mf / sqrt(vf + eps_hat); wf -= gf; w[i] = float2half_clip(wf); m[i] = float2half_clip(mf); v[i] = float2half_clip(vf); g[i] = clear_grads ? hz : float2half_clip(gf); } } template<typename Gtype, typename Wtype> void adam_reg_update_and_clear_gpu(int N, Gtype* g, Wtype *w, Wtype* m, Wtype* v, float beta1, float beta2, float eps_hat, float local_rate, const std::string& reg_type, float local_decay, void *handle, bool clear_grads) { cublasHandle_t cublas_handle = handle == nullptr ? Caffe::cublas_handle(0) : reinterpret_cast<cublasHandle_t>(handle); cudaStream_t stream; CUBLAS_CHECK(cublasGetStream(cublas_handle, &stream)); AdamRegUpdateAllAndClear // NOLINT_NEXT_LINE(whitespace/operators) <<<CAFFE_GET_BLOCKS(N), CAFFE_CUDA_NUM_THREADS, 0, stream>>>(N, g, w, m, v, beta1, beta2, eps_hat, local_rate, local_decay, reg_type == "L2", clear_grads); CUDA_POST_KERNEL_CHECK; CUDA_CHECK(cudaStreamSynchronize(stream)); } template<> void adam_reg_update_and_clear_gpu<float16, float16>(int N, float16 *g, float16 *w, float16 *m, float16 *v, float beta1, float beta2, float eps_hat, float local_rate, const std::string& reg_type, float local_decay, void *handle, bool clear_grads) { cublasHandle_t cublas_handle = handle == nullptr ? Caffe::cublas_handle(0) : reinterpret_cast<cublasHandle_t>(handle); cudaStream_t stream; CUBLAS_CHECK(cublasGetStream(cublas_handle, &stream)); AdamRegUpdateAllAndClear // NOLINT_NEXT_LINE(whitespace/operators) <<<CAFFE_GET_BLOCKS(N), CAFFE_CUDA_NUM_THREADS, 0, stream>>>(N, reinterpret_cast<half*>(g), reinterpret_cast<half*>(w), reinterpret_cast<half*>(m), reinterpret_cast<half*>(v), beta1, beta2, eps_hat, local_rate, local_decay, reg_type == "L2", clear_grads); CUDA_POST_KERNEL_CHECK; CUDA_CHECK(cudaStreamSynchronize(stream)); } template void adam_reg_update_and_clear_gpu<float16, float>(int, float16*, float*, float*, float*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float16, double>(int, float16*, double*, double*, double*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float, float>(int, float*, float*, float*, float*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float, double>(int, float*, double*, double*, double*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<float, float16>(int, float*, float16*, float16*, float16*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<double, float>(int, double*, float*, float*, float*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<double, double>(int, double*, double*, double*, double*, float, float, float, float, const std::string&, float, void*, bool); template void adam_reg_update_and_clear_gpu<double, float16>(int, double*, float16*, float16*, float16*, float, float, float, float, const std::string&, float, void*, bool); } // namespace caffe
a7f645a4f741357967c9eb90d0b4b9eee4e62a96.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" // MP 4 Reduction // Given a list (lst) of length n // Output its sum = lst[0] + lst[1] + ... + lst[n-1]; #include <wb.h> #define BLOCK_SIZE 1024 //@@ You can change this #define wbCheck(stmt) do { \ hipError_t err = stmt; \ if (err != hipSuccess) { \ wbLog(ERROR, "Failed to run stmt ", #stmt); \ return -1; \ } \ } while(0) __global__ void total(float * input, float * output, int len) { //@@ Load a segment of the input vector into shared memory //@@ Traverse the reduction tree //@@ Write the computed sum of the block to the output vector at the //@@ correct index __shared__ float partialSum[ 2 * BLOCK_SIZE ]; unsigned int t = threadIdx.x; unsigned int start = 2 * blockIdx.x*blockDim.x; partialSum[t] = .0; partialSum[blockDim.x+t] = .0; if( t+start < len ) { partialSum[t] = input[start+t]; if( t+start+blockDim.x < len ) partialSum[blockDim.x+t] = input[start + blockDim.x + t]; } for( unsigned int stride = blockDim.x; stride > 0; stride /= 2 ) { __syncthreads(); if( t < stride ) partialSum[t] += partialSum[t+stride]; } if( 0 == t ) output[ blockIdx.x ] = partialSum[0]; } int main(int argc, char ** argv) { int ii; wbArg_t args; float * hostInput; // The input 1D list float * hostOutput; // The output list float * deviceInput; float * deviceOutput; int numInputElements; // number of elements in the input list int numOutputElements; // number of elements in the output list args = wbArg_read(argc, argv); wbTime_start(Generic, "Importing data and creating memory on host"); hostInput = (float *) wbImport(wbArg_getInputFile(args, 0), &numInputElements); numOutputElements = numInputElements / (BLOCK_SIZE<<1); if (numInputElements % (BLOCK_SIZE<<1)) { numOutputElements++; } hostOutput = (float*) malloc(numOutputElements * sizeof(float)); wbTime_stop(Generic, "Importing data and creating memory on host"); wbLog(TRACE, "The number of input elements in the input is ", numInputElements); wbLog(TRACE, "The number of output elements in the input is ", numOutputElements); wbTime_start(GPU, "Allocating GPU memory."); //@@ Allocate GPU memory here wbCheck(hipMalloc( (void**)&deviceInput, numInputElements * sizeof(float) )); wbCheck(hipMalloc( (void**)&deviceOutput, numOutputElements * sizeof(float) )); wbTime_stop(GPU, "Allocating GPU memory."); wbTime_start(GPU, "Copying input memory to the GPU."); //@@ Copy memory to the GPU here wbCheck(hipMemcpy(deviceInput,hostInput,numInputElements*sizeof(float) ,hipMemcpyHostToDevice )); wbTime_stop(GPU, "Copying input memory to the GPU."); //@@ Initialize the grid and block dimensions here dim3 dimGrid( numOutputElements, 1, 1 ); dim3 dimBlock( BLOCK_SIZE, 1, 1 ); wbTime_start(Compute, "Performing CUDA computation"); //@@ Launch the GPU Kernel here hipLaunchKernelGGL(( total), dim3(dimGrid), dim3(dimBlock) , 0, 0, deviceInput , deviceOutput , numInputElements ); hipDeviceSynchronize(); wbTime_stop(Compute, "Performing CUDA computation"); wbTime_start(Copy, "Copying output memory to the CPU"); //@@ Copy the GPU memory back to the CPU here wbCheck(hipMemcpy(hostOutput,deviceOutput,numOutputElements*sizeof(float) , hipMemcpyDeviceToHost )); wbTime_stop(Copy, "Copying output memory to the CPU"); /******************************************************************** * Reduce output vector on the host * NOTE: One could also perform the reduction of the output vector * recursively and support any size input. For simplicity, we do not * require that for this lab. ********************************************************************/ for (ii = 1; ii < numOutputElements; ii++) hostOutput[0] += hostOutput[ii]; wbTime_start(GPU, "Freeing GPU Memory"); //@@ Free the GPU memory here wbCheck(hipFree( deviceInput )); wbCheck(hipFree( deviceOutput )); wbTime_stop(GPU, "Freeing GPU Memory"); wbSolution(args, hostOutput, 1); free(hostInput); free(hostOutput); return 0; }
a7f645a4f741357967c9eb90d0b4b9eee4e62a96.cu
// MP 4 Reduction // Given a list (lst) of length n // Output its sum = lst[0] + lst[1] + ... + lst[n-1]; #include <wb.h> #define BLOCK_SIZE 1024 //@@ You can change this #define wbCheck(stmt) do { \ cudaError_t err = stmt; \ if (err != cudaSuccess) { \ wbLog(ERROR, "Failed to run stmt ", #stmt); \ return -1; \ } \ } while(0) __global__ void total(float * input, float * output, int len) { //@@ Load a segment of the input vector into shared memory //@@ Traverse the reduction tree //@@ Write the computed sum of the block to the output vector at the //@@ correct index __shared__ float partialSum[ 2 * BLOCK_SIZE ]; unsigned int t = threadIdx.x; unsigned int start = 2 * blockIdx.x*blockDim.x; partialSum[t] = .0; partialSum[blockDim.x+t] = .0; if( t+start < len ) { partialSum[t] = input[start+t]; if( t+start+blockDim.x < len ) partialSum[blockDim.x+t] = input[start + blockDim.x + t]; } for( unsigned int stride = blockDim.x; stride > 0; stride /= 2 ) { __syncthreads(); if( t < stride ) partialSum[t] += partialSum[t+stride]; } if( 0 == t ) output[ blockIdx.x ] = partialSum[0]; } int main(int argc, char ** argv) { int ii; wbArg_t args; float * hostInput; // The input 1D list float * hostOutput; // The output list float * deviceInput; float * deviceOutput; int numInputElements; // number of elements in the input list int numOutputElements; // number of elements in the output list args = wbArg_read(argc, argv); wbTime_start(Generic, "Importing data and creating memory on host"); hostInput = (float *) wbImport(wbArg_getInputFile(args, 0), &numInputElements); numOutputElements = numInputElements / (BLOCK_SIZE<<1); if (numInputElements % (BLOCK_SIZE<<1)) { numOutputElements++; } hostOutput = (float*) malloc(numOutputElements * sizeof(float)); wbTime_stop(Generic, "Importing data and creating memory on host"); wbLog(TRACE, "The number of input elements in the input is ", numInputElements); wbLog(TRACE, "The number of output elements in the input is ", numOutputElements); wbTime_start(GPU, "Allocating GPU memory."); //@@ Allocate GPU memory here wbCheck(cudaMalloc( (void**)&deviceInput, numInputElements * sizeof(float) )); wbCheck(cudaMalloc( (void**)&deviceOutput, numOutputElements * sizeof(float) )); wbTime_stop(GPU, "Allocating GPU memory."); wbTime_start(GPU, "Copying input memory to the GPU."); //@@ Copy memory to the GPU here wbCheck(cudaMemcpy(deviceInput,hostInput,numInputElements*sizeof(float) ,cudaMemcpyHostToDevice )); wbTime_stop(GPU, "Copying input memory to the GPU."); //@@ Initialize the grid and block dimensions here dim3 dimGrid( numOutputElements, 1, 1 ); dim3 dimBlock( BLOCK_SIZE, 1, 1 ); wbTime_start(Compute, "Performing CUDA computation"); //@@ Launch the GPU Kernel here total<<< dimGrid, dimBlock >>>( deviceInput , deviceOutput , numInputElements ); cudaDeviceSynchronize(); wbTime_stop(Compute, "Performing CUDA computation"); wbTime_start(Copy, "Copying output memory to the CPU"); //@@ Copy the GPU memory back to the CPU here wbCheck(cudaMemcpy(hostOutput,deviceOutput,numOutputElements*sizeof(float) , cudaMemcpyDeviceToHost )); wbTime_stop(Copy, "Copying output memory to the CPU"); /******************************************************************** * Reduce output vector on the host * NOTE: One could also perform the reduction of the output vector * recursively and support any size input. For simplicity, we do not * require that for this lab. ********************************************************************/ for (ii = 1; ii < numOutputElements; ii++) hostOutput[0] += hostOutput[ii]; wbTime_start(GPU, "Freeing GPU Memory"); //@@ Free the GPU memory here wbCheck(cudaFree( deviceInput )); wbCheck(cudaFree( deviceOutput )); wbTime_stop(GPU, "Freeing GPU Memory"); wbSolution(args, hostOutput, 1); free(hostInput); free(hostOutput); return 0; }
0e6bb6805bcd3279b5d43722751f7efb48c4ae92.hip
// !!! This is a file automatically generated by hipify!!! #include "hip/hip_runtime.h" /* ****************************************************************************** * * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author [email protected] // @author [email protected] // #include <ops/declarable/helpers/axis.h> #include <helpers/PointersManager.h> #include <helpers/TAD.h> #include <array> #include <helpers/ConstantTadHelper.h> namespace sd { namespace ops { namespace helpers { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // extract patches kernel // - theSame - SAME or VALID - output format // - batchCount - batches - the first dimension of input // - sizeRow, sizeCol - rows and cols sizes for batch // - rowDim, colDim - rows and cols dimensions for input patches // - outRowDim, outColDim - rows and cols dimensions for output patches // - strideRow, strideCol - step between input elements with patches // - rateRow, rateCol - counts for input patches // - rowCast, colCast - shifts for output placement (1 or 0) // - lastDim - last dimension of input/output // - input - input tensor buffer // - patchShape - input patch TAD shape // - inputOffsets - input TAD offsets // - output - output tensor buffer // - outTadShape - output TAD shape // - outputOffsets - output TAD offsets // template <typename T> static __global__ void globalExtractPatchesKernel(bool theSame, int batchCount, int sizeRow, int sizeCol, int rowDim, int colDim, int outRowDim, int outColDim, int strideRow, int strideCol, int rateRow, int rateCol, int rowCast, int colCast, int lastDim, const T* input, const Nd4jLong* patchShape, const Nd4jLong* inputOffsets, T* output, const Nd4jLong* outTadShape, const Nd4jLong* outputOffsets) { auto start = threadIdx.x + blockIdx.x * blockDim.x; auto step = blockDim.x * gridDim.x; // batch input by 3 last dims and extrapole input onto output with outColDim/outRowDim for (Nd4jLong batch = start; batch < batchCount; batch += step) { auto patch = input + inputOffsets[batch];// listOfMatricies->at(batch); auto outMatrix = output + outputOffsets[batch]; //listOfOutputs->at(batch); for (Nd4jLong i = 0; i < outRowDim; i++) { for (Nd4jLong j = 0; j < outColDim; j++) { Nd4jLong pos = 0; auto rowStart = i * strideRow - (theSame?rowCast:0); auto colStart = j * strideCol - (theSame?colCast:0); auto rowEnd = rowStart + sizeRow * rateRow; auto colEnd = colStart + sizeCol * rateCol; if (!theSame) { rowEnd = math::nd4j_min(rowStart + sizeRow * rateRow, Nd4jLong (rowDim)); colEnd = math::nd4j_min(colStart + sizeCol * rateCol, Nd4jLong (colDim)); } for (auto row = rowStart; row < rowEnd; row += rateRow) { for (auto col = colStart; col < colEnd; col += rateCol) { for (auto pixel = 0; pixel < lastDim; pixel++) { Nd4jLong zPos[] = {i, j, pos}; Nd4jLong xPos[] = {row, col, pixel}; bool setUp = (theSame && row >= 0 && col >= 0 && row < rowDim && col < colDim) || (!theSame); if (setUp) { // VALID or SAME cases outMatrix[shape::getOffset(outTadShape, zPos)] = patch[shape::getOffset(patchShape, xPos)]; } pos++; } } } } } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template <typename T> static void _extractPatches(sd::LaunchContext * context, NDArray* images, NDArray* output, int sizeRow, int sizeCol, int strideRow, int strideCol, int rateRow, int rateCol, bool theSame){ NDArray::prepareSpecialUse({output}, {images}); std::vector<int> restDims({1, 2, 3}); // the first and the last dims // 3D matricies - 2D matricies of vectors (if last dim is greater than 1) //int e = 0; const int ksizeRowsEffective = sizeRow + (sizeRow - 1) * (rateRow - 1); const int ksizeColsEffective = sizeCol + (sizeCol - 1) * (rateCol - 1); const int ksize = ksizeRowsEffective * ksizeColsEffective; Nd4jLong lastDim = images->sizeAt(3); Nd4jLong outLastDim = output->sizeAt(3); Nd4jLong rowDim = images->sizeAt(1); Nd4jLong colDim = images->sizeAt(2); Nd4jLong outRowDim = output->sizeAt(1); Nd4jLong outColDim = output->sizeAt(2); auto rowCast = 1; auto colCast = 1; // validate shifts if (sizeRow * rateRow < 3) rowCast = 0; if (sizeCol * rateCol < 3) colCast = 0; auto packX = sd::ConstantTadHelper::getInstance().tadForDimensions(images->shapeInfo(), restDims.data(), restDims.size()); auto packZ = sd::ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), restDims.data(), restDims.size()); int batchCount = packX.numberOfTads(); PointersManager manager(context, "helpers::extractPatches"); auto stream = context->getCudaStream(); auto imagesBuffer = reinterpret_cast<T*>(images->specialBuffer()); auto outputBuffer = reinterpret_cast<T*>(output->specialBuffer()); hipLaunchKernelGGL(( globalExtractPatchesKernel<T>), dim3(128), dim3(128), 1024, *stream, theSame, batchCount, sizeRow, sizeCol, rowDim, colDim, outRowDim, outColDim, strideRow, strideCol, rateRow, rateCol, rowCast, colCast, lastDim, imagesBuffer, packX.specialShapeInfo(), packX.specialOffsets(), outputBuffer, packZ.specialShapeInfo(), packZ.specialOffsets()); manager.synchronize(); NDArray::registerSpecialUse({output}, {images}); } BUILD_SINGLE_TEMPLATE(template void _extractPatches, (sd::LaunchContext * context, NDArray* input, NDArray* output, int sizeRow, int sizeCol, int stradeRow, int stradeCol, int rateRow, int rateCol, bool theSame), LIBND4J_TYPES); ND4J_LOCAL void extractPatches(sd::LaunchContext * context, NDArray* images, NDArray* output, int sizeRow, int sizeCol, int stradeRow, int stradeCol, int rateRow, int rateCol, bool theSame){ auto xType = images->dataType(); BUILD_SINGLE_SELECTOR(xType, _extractPatches, (context, images, output, sizeRow, sizeCol, stradeRow, stradeCol, rateRow, rateCol, theSame), LIBND4J_TYPES); } } } }
0e6bb6805bcd3279b5d43722751f7efb48c4ae92.cu
/* ****************************************************************************** * * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author [email protected] // @author [email protected] // #include <ops/declarable/helpers/axis.h> #include <helpers/PointersManager.h> #include <helpers/TAD.h> #include <array> #include <helpers/ConstantTadHelper.h> namespace sd { namespace ops { namespace helpers { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // extract patches kernel // - theSame - SAME or VALID - output format // - batchCount - batches - the first dimension of input // - sizeRow, sizeCol - rows and cols sizes for batch // - rowDim, colDim - rows and cols dimensions for input patches // - outRowDim, outColDim - rows and cols dimensions for output patches // - strideRow, strideCol - step between input elements with patches // - rateRow, rateCol - counts for input patches // - rowCast, colCast - shifts for output placement (1 or 0) // - lastDim - last dimension of input/output // - input - input tensor buffer // - patchShape - input patch TAD shape // - inputOffsets - input TAD offsets // - output - output tensor buffer // - outTadShape - output TAD shape // - outputOffsets - output TAD offsets // template <typename T> static __global__ void globalExtractPatchesKernel(bool theSame, int batchCount, int sizeRow, int sizeCol, int rowDim, int colDim, int outRowDim, int outColDim, int strideRow, int strideCol, int rateRow, int rateCol, int rowCast, int colCast, int lastDim, const T* input, const Nd4jLong* patchShape, const Nd4jLong* inputOffsets, T* output, const Nd4jLong* outTadShape, const Nd4jLong* outputOffsets) { auto start = threadIdx.x + blockIdx.x * blockDim.x; auto step = blockDim.x * gridDim.x; // batch input by 3 last dims and extrapole input onto output with outColDim/outRowDim for (Nd4jLong batch = start; batch < batchCount; batch += step) { auto patch = input + inputOffsets[batch];// listOfMatricies->at(batch); auto outMatrix = output + outputOffsets[batch]; //listOfOutputs->at(batch); for (Nd4jLong i = 0; i < outRowDim; i++) { for (Nd4jLong j = 0; j < outColDim; j++) { Nd4jLong pos = 0; auto rowStart = i * strideRow - (theSame?rowCast:0); auto colStart = j * strideCol - (theSame?colCast:0); auto rowEnd = rowStart + sizeRow * rateRow; auto colEnd = colStart + sizeCol * rateCol; if (!theSame) { rowEnd = math::nd4j_min(rowStart + sizeRow * rateRow, Nd4jLong (rowDim)); colEnd = math::nd4j_min(colStart + sizeCol * rateCol, Nd4jLong (colDim)); } for (auto row = rowStart; row < rowEnd; row += rateRow) { for (auto col = colStart; col < colEnd; col += rateCol) { for (auto pixel = 0; pixel < lastDim; pixel++) { Nd4jLong zPos[] = {i, j, pos}; Nd4jLong xPos[] = {row, col, pixel}; bool setUp = (theSame && row >= 0 && col >= 0 && row < rowDim && col < colDim) || (!theSame); if (setUp) { // VALID or SAME cases outMatrix[shape::getOffset(outTadShape, zPos)] = patch[shape::getOffset(patchShape, xPos)]; } pos++; } } } } } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template <typename T> static void _extractPatches(sd::LaunchContext * context, NDArray* images, NDArray* output, int sizeRow, int sizeCol, int strideRow, int strideCol, int rateRow, int rateCol, bool theSame){ NDArray::prepareSpecialUse({output}, {images}); std::vector<int> restDims({1, 2, 3}); // the first and the last dims // 3D matricies - 2D matricies of vectors (if last dim is greater than 1) //int e = 0; const int ksizeRowsEffective = sizeRow + (sizeRow - 1) * (rateRow - 1); const int ksizeColsEffective = sizeCol + (sizeCol - 1) * (rateCol - 1); const int ksize = ksizeRowsEffective * ksizeColsEffective; Nd4jLong lastDim = images->sizeAt(3); Nd4jLong outLastDim = output->sizeAt(3); Nd4jLong rowDim = images->sizeAt(1); Nd4jLong colDim = images->sizeAt(2); Nd4jLong outRowDim = output->sizeAt(1); Nd4jLong outColDim = output->sizeAt(2); auto rowCast = 1; auto colCast = 1; // validate shifts if (sizeRow * rateRow < 3) rowCast = 0; if (sizeCol * rateCol < 3) colCast = 0; auto packX = sd::ConstantTadHelper::getInstance().tadForDimensions(images->shapeInfo(), restDims.data(), restDims.size()); auto packZ = sd::ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), restDims.data(), restDims.size()); int batchCount = packX.numberOfTads(); PointersManager manager(context, "helpers::extractPatches"); auto stream = context->getCudaStream(); auto imagesBuffer = reinterpret_cast<T*>(images->specialBuffer()); auto outputBuffer = reinterpret_cast<T*>(output->specialBuffer()); globalExtractPatchesKernel<T><<<128, 128, 1024, *stream>>>(theSame, batchCount, sizeRow, sizeCol, rowDim, colDim, outRowDim, outColDim, strideRow, strideCol, rateRow, rateCol, rowCast, colCast, lastDim, imagesBuffer, packX.specialShapeInfo(), packX.specialOffsets(), outputBuffer, packZ.specialShapeInfo(), packZ.specialOffsets()); manager.synchronize(); NDArray::registerSpecialUse({output}, {images}); } BUILD_SINGLE_TEMPLATE(template void _extractPatches, (sd::LaunchContext * context, NDArray* input, NDArray* output, int sizeRow, int sizeCol, int stradeRow, int stradeCol, int rateRow, int rateCol, bool theSame), LIBND4J_TYPES); ND4J_LOCAL void extractPatches(sd::LaunchContext * context, NDArray* images, NDArray* output, int sizeRow, int sizeCol, int stradeRow, int stradeCol, int rateRow, int rateCol, bool theSame){ auto xType = images->dataType(); BUILD_SINGLE_SELECTOR(xType, _extractPatches, (context, images, output, sizeRow, sizeCol, stradeRow, stradeCol, rateRow, rateCol, theSame), LIBND4J_TYPES); } } } }
6e6206ee081fa6d69d9c16546cfaf6875c6cc7a7.hip
// !!! This is a file automatically generated by hipify!!! #include <hip/hip_runtime.h> #include <cv.h> #include <highgui.h> #include "functions.h" #include "common/cuda/BufferManager.h" #include "common/cuda/Types.h" #include "common/cuda/Util.cuh" #include "common/cuda/Memory.h" #define PITCH2D_TEST /** * \brief: pitch2D */ __global__ void transparentKernel(void* out, hipTextureObject_t in, int width, int height){ int ix = IMAD(blockDim.x, blockIdx.x, threadIdx.x); int iy = IMAD(blockDim.y, blockIdx.y, threadIdx.y); if (ix >= width || iy >= height) return; uchar val = read2D<uchar>(in, ix, iy); val = (uchar)(val * 0.5); write2D<uchar>(out, val, ix, iy, width); } /** * \brief: cudaSurfaceObject_t */ __global__ void transparentKernel(hipSurfaceObject_t out, hipTextureObject_t in, int width, int height){ int ix = IMAD(blockDim.x, blockIdx.x, threadIdx.x); int iy = IMAD(blockDim.y, blockIdx.y, threadIdx.y); if (ix >= width || iy >= height) return; uchar val = read2D<uchar>(in, ix, iy); val = (uchar)(val * 0.5); write2D<uchar>(out, val, ix, iy); } void transparent(BufferManager &out, BufferManager &in){ int width = in.width(); int height = in.height(); dim3 blocks(16, 8); dim3 grids(DIVUP(width, blocks.x), DIVUP(height, blocks.y)); #ifdef PITCH2D_TEST transparentKernel << <grids, blocks >> >(out.ptr(), in.cu_tex_obj(), width, height); #else transparentKernel << <grids, blocks >> >(out.cu_surf_obj(), in.cu_tex_obj(), width, height); #endif } void texture_test(){ std::string filename = "image.bmp"; cv::Mat image = cv::imread(filename, 0); cv::Mat out(image.rows, image.cols, CV_8UC1); BufferManager d_in, d_out; #ifdef PITCH2D_TEST d_in.create(image.cols, image.rows, UCHAR, PITCH2D, RD_ELEMENT_TYPE); d_out.create(image.cols, image.rows, UCHAR, PITCH2D, RD_ELEMENT_TYPE); #else d_in.create(image.cols, image.rows, UCHAR, BLOCK_LINEAR, RD_ELEMENT_TYPE); d_out.create(image.cols, image.rows, UCHAR, BLOCK_LINEAR, RD_ELEMENT_TYPE); #endif d_in.upload(image.data, image.step, 0, 0, image.cols, image.rows); transparent(d_out, d_in); d_out.download(out.data, image.step, 0, 0, image.cols, image.rows); cv::imshow("image", image); cv::imshow("out", out); cv::waitKey(0); }
6e6206ee081fa6d69d9c16546cfaf6875c6cc7a7.cu
#include <cuda_runtime.h> #include <cv.h> #include <highgui.h> #include "functions.h" #include "common/cuda/BufferManager.h" #include "common/cuda/Types.h" #include "common/cuda/Util.cuh" #include "common/cuda/Memory.h" #define PITCH2D_TEST /** * \brief: 通过pitch2D 写数据 */ __global__ void transparentKernel(void* out, cudaTextureObject_t in, int width, int height){ int ix = IMAD(blockDim.x, blockIdx.x, threadIdx.x); int iy = IMAD(blockDim.y, blockIdx.y, threadIdx.y); if (ix >= width || iy >= height) return; uchar val = read2D<uchar>(in, ix, iy); val = (uchar)(val * 0.5); write2D<uchar>(out, val, ix, iy, width); } /** * \brief: 通过cudaSurfaceObject_t 写数据 */ __global__ void transparentKernel(cudaSurfaceObject_t out, cudaTextureObject_t in, int width, int height){ int ix = IMAD(blockDim.x, blockIdx.x, threadIdx.x); int iy = IMAD(blockDim.y, blockIdx.y, threadIdx.y); if (ix >= width || iy >= height) return; uchar val = read2D<uchar>(in, ix, iy); val = (uchar)(val * 0.5); write2D<uchar>(out, val, ix, iy); } void transparent(BufferManager &out, BufferManager &in){ int width = in.width(); int height = in.height(); dim3 blocks(16, 8); dim3 grids(DIVUP(width, blocks.x), DIVUP(height, blocks.y)); #ifdef PITCH2D_TEST transparentKernel << <grids, blocks >> >(out.ptr(), in.cu_tex_obj(), width, height); #else transparentKernel << <grids, blocks >> >(out.cu_surf_obj(), in.cu_tex_obj(), width, height); #endif } void texture_test(){ std::string filename = "image.bmp"; cv::Mat image = cv::imread(filename, 0); cv::Mat out(image.rows, image.cols, CV_8UC1); BufferManager d_in, d_out; #ifdef PITCH2D_TEST d_in.create(image.cols, image.rows, UCHAR, PITCH2D, RD_ELEMENT_TYPE); d_out.create(image.cols, image.rows, UCHAR, PITCH2D, RD_ELEMENT_TYPE); #else d_in.create(image.cols, image.rows, UCHAR, BLOCK_LINEAR, RD_ELEMENT_TYPE); d_out.create(image.cols, image.rows, UCHAR, BLOCK_LINEAR, RD_ELEMENT_TYPE); #endif d_in.upload(image.data, image.step, 0, 0, image.cols, image.rows); transparent(d_out, d_in); d_out.download(out.data, image.step, 0, 0, image.cols, image.rows); cv::imshow("image", image); cv::imshow("out", out); cv::waitKey(0); }