serial_no
int64
1
24.2k
cuda_source
stringlengths
11
9.01M
21,501
/** * 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. * */ /***********************************************************************************\ * Standard Includes \***********************************************************************************/ #include <iostream> #include <assert.h> /* For the CUDA runtime routines (prefixed with "cuda_") */ #include <cuda_runtime.h> #include <time.h> /***********************************************************************************\ * # Defines \***********************************************************************************/ #define TRUE (1) #define FALSE (0) /* Number of rows and columns of the global memory */ #define NUM_OF_GLOBAL_ROWS (16000) #define NUM_OF_GLOBAL_COLS (16000) /* Number of threads in each block */ #define BLOCK_SIZE (32) /* Convolution Kernel size */ #define KERNEL_SIZE (2) /* Number of row and columns of the local memory */ #define NUM_OF_LOCAL_ROWS (BLOCK_SIZE + (2 * KERNEL_SIZE)) #define NUM_OF_LOCAL_COLS (BLOCK_SIZE + (2 * KERNEL_SIZE)) /***********************************************************************************\ * Enums \***********************************************************************************/ typedef enum Status_Tag { PASSED, FAILED }Status_T; typedef enum Cuda_Event_Tag { ALLOCATE_DEVICE_MATRIX_A, ALLOCATE_DEVICE_MATRIX_B, COPY_MATRIX_A_FROM_HOST_TO_DEVICE, LAUNCH_KERNEL_CONV2DDEVICE, DEVICE_SYNCHRONIZATION, COPY_MATRIX_B_FROM_DEVICE_TO_HOST, FREE_DEVICE_MATRIX_A, FREE_DEVICE_MATRIX_B }Cuda_Event_T; typedef enum Corner_Cell_Name_Tag { LEFT_TOP_PADDING_CORNER, RIGHT_TOP_PADDING_CORNER, LEFT_BOTTOM_PADDING_CORNER, RIGHT_BOTTOM_PADDING_CORNER, NUM_OF_CORNERS }Corner_Cell_Name_T; typedef enum Ver_Side_Cell_Name_Tag { LEFT_PADDING_CELL, RIGHT_PADDING_CELL, NUM_OF_VER_SIDES }Ver_Side_Cell_Name_T; typedef enum Hor_Side_Cell_Name_Tag { TOP_PADDING_CELL, BOTTOM_PADDING_CELL, NUM_OF_HOR_SIDES }Hor_Side_Cell_Name_T; /***********************************************************************************\ * Structures \***********************************************************************************/ typedef struct Result_Tag { Status_T status; int index; }Result_T; typedef struct Cell_Tag { int r_idx; /* Row index */ int c_idx; /* Column index */ }Cell_T; /***********************************************************************************\ * Function Macros \***********************************************************************************/ /* Convert matrix index to array index */ #define MATRIX_TO_ARRAY_INDEX(r_idx, c_idx, num_cols) ((r_idx*num_cols) + c_idx) /***********************************************************************************\ * CUDA Kernel Device code for 2D Convolution \***********************************************************************************/ __global__ void conv2DDevice(const int *in, int *out) { int g_col_idx = blockDim.x * blockIdx.x + threadIdx.x; int g_row_idx = blockDim.y * blockIdx.y + threadIdx.y; int l_col_idx = threadIdx.x + KERNEL_SIZE; int l_row_idx = threadIdx.y + KERNEL_SIZE; __shared__ int local[NUM_OF_LOCAL_ROWS*NUM_OF_LOCAL_COLS]; /* Convert from matrix indexing to array indexing */ int g_idx = MATRIX_TO_ARRAY_INDEX(g_row_idx, g_col_idx, NUM_OF_GLOBAL_COLS); int l_idx = MATRIX_TO_ARRAY_INDEX(l_row_idx, l_col_idx, NUM_OF_LOCAL_COLS); if ((g_row_idx < NUM_OF_GLOBAL_ROWS) && (g_col_idx < NUM_OF_GLOBAL_COLS)) { /* Read input elements into shared memory */ /* Fill the internal (BLOCK_SIZE*BLOCK_SIZE) matrix */ local[l_idx] = in[g_idx]; /* Fill the left and right padding columns of local memory */ if (threadIdx.x < KERNEL_SIZE) { Cell_T l_ver_side[NUM_OF_VER_SIDES]; Cell_T g_ver_side[NUM_OF_VER_SIDES]; /* Find left and right padding column indices of local memory */ l_ver_side[LEFT_PADDING_CELL].r_idx = l_row_idx; l_ver_side[LEFT_PADDING_CELL].c_idx = l_col_idx - KERNEL_SIZE; l_ver_side[RIGHT_PADDING_CELL].r_idx = l_row_idx; l_ver_side[RIGHT_PADDING_CELL].c_idx = l_col_idx + BLOCK_SIZE; /* Find indices of global memory whose data needs to be filled into the left and right padding columns of local memory */ g_ver_side[LEFT_PADDING_CELL].r_idx = g_row_idx; g_ver_side[LEFT_PADDING_CELL].c_idx = g_col_idx - KERNEL_SIZE; g_ver_side[RIGHT_PADDING_CELL].r_idx = g_row_idx; g_ver_side[RIGHT_PADDING_CELL].c_idx = g_col_idx + BLOCK_SIZE; for (int cell = LEFT_PADDING_CELL; cell < NUM_OF_VER_SIDES; ++cell) { bool within_bounds = FALSE; /* Check if the cell is within bounds of global matrix */ if (LEFT_PADDING_CELL == cell) { within_bounds = (g_ver_side[cell].c_idx >= 0); } if (RIGHT_PADDING_CELL == cell) { within_bounds = (g_ver_side[cell].c_idx < NUM_OF_GLOBAL_COLS); } /* Copy corner into local memory if it is within the bounds of global matrix */ /* Convert from matrix indexing to array indexing */ int pad_l_idx = MATRIX_TO_ARRAY_INDEX(l_ver_side[cell].r_idx, l_ver_side[cell].c_idx, NUM_OF_LOCAL_COLS); int pad_g_idx = MATRIX_TO_ARRAY_INDEX(g_ver_side[cell].r_idx, g_ver_side[cell].c_idx, NUM_OF_GLOBAL_COLS); if (TRUE == within_bounds) { local[pad_l_idx] = in[pad_g_idx]; } else { local[pad_l_idx] = 0; } } } /* Fill the top and bottom padding rows */ if (threadIdx.y < KERNEL_SIZE) { Cell_T l_hor_side[NUM_OF_HOR_SIDES]; Cell_T g_hor_side[NUM_OF_HOR_SIDES]; /* Find top and bottom padding row indices of local memory */ l_hor_side[TOP_PADDING_CELL].r_idx = l_row_idx - KERNEL_SIZE; l_hor_side[TOP_PADDING_CELL].c_idx = l_col_idx; l_hor_side[BOTTOM_PADDING_CELL].r_idx = l_row_idx + BLOCK_SIZE; l_hor_side[BOTTOM_PADDING_CELL].c_idx = l_col_idx; /* Find indices of global memory whose data needs to be filled into the top and bottom padding rows of local memory */ g_hor_side[TOP_PADDING_CELL].r_idx = g_row_idx - KERNEL_SIZE; g_hor_side[TOP_PADDING_CELL].c_idx = g_col_idx; g_hor_side[BOTTOM_PADDING_CELL].r_idx = g_row_idx + BLOCK_SIZE; g_hor_side[BOTTOM_PADDING_CELL].c_idx = g_col_idx; for (int cell = TOP_PADDING_CELL; cell < NUM_OF_HOR_SIDES; ++cell) { bool within_bounds = FALSE; /* Check if the cell is within bounds of global matrix */ if (TOP_PADDING_CELL == cell) { within_bounds = (g_hor_side[cell].r_idx >= 0); } if (BOTTOM_PADDING_CELL == cell) { within_bounds = (g_hor_side[cell].r_idx < NUM_OF_GLOBAL_ROWS); } /* Copy corner into local memory if it is within the bounds of global matrix */ /* Convert from matrix indexing to array indexing */ int pad_l_idx = MATRIX_TO_ARRAY_INDEX(l_hor_side[cell].r_idx, l_hor_side[cell].c_idx, NUM_OF_LOCAL_COLS); int pad_g_idx = MATRIX_TO_ARRAY_INDEX(g_hor_side[cell].r_idx, g_hor_side[cell].c_idx, NUM_OF_GLOBAL_COLS); if (TRUE == within_bounds) { local[pad_l_idx] = in[pad_g_idx]; } else { local[pad_l_idx] = 0; } } } /* Fill the corners */ if ((threadIdx.x) < KERNEL_SIZE && (threadIdx.y < KERNEL_SIZE)) { Cell_T l_corner[NUM_OF_CORNERS]; Cell_T g_corner[NUM_OF_CORNERS]; /* Find left top, right top, left bottom and right bottom padding corner indices of local memory */ l_corner[LEFT_TOP_PADDING_CORNER].r_idx = l_row_idx - KERNEL_SIZE; l_corner[LEFT_TOP_PADDING_CORNER].c_idx = l_col_idx - KERNEL_SIZE; l_corner[RIGHT_TOP_PADDING_CORNER].r_idx = l_row_idx - KERNEL_SIZE; l_corner[RIGHT_TOP_PADDING_CORNER].c_idx = l_col_idx + BLOCK_SIZE; l_corner[LEFT_BOTTOM_PADDING_CORNER].r_idx = l_row_idx + BLOCK_SIZE; l_corner[LEFT_BOTTOM_PADDING_CORNER].c_idx = l_col_idx - KERNEL_SIZE; l_corner[RIGHT_BOTTOM_PADDING_CORNER].r_idx = l_row_idx + BLOCK_SIZE; l_corner[RIGHT_BOTTOM_PADDING_CORNER].c_idx = l_col_idx + BLOCK_SIZE; /* Find indices of global memory whose data needs to be filled into the left top, right top, left bottom and right bottom padding corners of local memory */ g_corner[LEFT_TOP_PADDING_CORNER].r_idx = g_row_idx - KERNEL_SIZE; g_corner[LEFT_TOP_PADDING_CORNER].c_idx = g_col_idx - KERNEL_SIZE; g_corner[RIGHT_TOP_PADDING_CORNER].r_idx = g_row_idx - KERNEL_SIZE; g_corner[RIGHT_TOP_PADDING_CORNER].c_idx = g_col_idx + BLOCK_SIZE; g_corner[LEFT_BOTTOM_PADDING_CORNER].r_idx = g_row_idx + BLOCK_SIZE; g_corner[LEFT_BOTTOM_PADDING_CORNER].c_idx = g_col_idx - KERNEL_SIZE; g_corner[RIGHT_BOTTOM_PADDING_CORNER].r_idx = g_row_idx + BLOCK_SIZE; g_corner[RIGHT_BOTTOM_PADDING_CORNER].c_idx = g_col_idx + BLOCK_SIZE; for (int corner = LEFT_TOP_PADDING_CORNER; corner < NUM_OF_CORNERS; ++corner) { bool within_bounds = FALSE; /* Check if the corner is within bounds of global matrix */ if (LEFT_TOP_PADDING_CORNER == corner){ within_bounds = ((g_corner[corner].r_idx >= 0) && (g_corner[corner].c_idx >= 0)); } if (RIGHT_TOP_PADDING_CORNER == corner){ within_bounds = ((g_corner[corner].r_idx >= 0) && (g_corner[corner].c_idx < NUM_OF_GLOBAL_COLS)); } if (LEFT_BOTTOM_PADDING_CORNER == corner){ within_bounds = ((g_corner[corner].r_idx < NUM_OF_GLOBAL_ROWS) && (g_corner[corner].c_idx >= 0)); } if (RIGHT_BOTTOM_PADDING_CORNER == corner){ within_bounds = ((g_corner[corner].r_idx < NUM_OF_GLOBAL_ROWS) && (g_corner[corner].c_idx < NUM_OF_GLOBAL_COLS)); } /* Copy corner into local memory if it is within the bounds of global matrix */ /* Convert from matrix indexing to array indexing */ int pad_l_idx = MATRIX_TO_ARRAY_INDEX(l_corner[corner].r_idx, l_corner[corner].c_idx, NUM_OF_LOCAL_COLS); int pad_g_idx = MATRIX_TO_ARRAY_INDEX(g_corner[corner].r_idx, g_corner[corner].c_idx, NUM_OF_GLOBAL_COLS); if (TRUE == within_bounds){ local[pad_l_idx] = in[pad_g_idx]; } else { local[pad_l_idx] = 0; } } } __syncthreads(); /* Apply convolution */ int result = 0; for (int row_offset = -KERNEL_SIZE; row_offset <= KERNEL_SIZE; ++row_offset) { for (int col_offset = -KERNEL_SIZE; col_offset <= KERNEL_SIZE; ++col_offset) { /* Convert local matrix row and column to local element index */ int l_ele_idx = MATRIX_TO_ARRAY_INDEX((l_row_idx + row_offset), (l_col_idx + col_offset), NUM_OF_LOCAL_COLS); result += local[l_ele_idx]; } } /* Store the result */ out[g_idx] = result; } } /***********************************************************************************\ * Host code for 2D Convolution and comparing the result with device 2D Convolution \***********************************************************************************/ Result_T checkResult(int* h_A, int* h_B) { Result_T result; result.status = PASSED; result.index = -1; int mat_size = NUM_OF_GLOBAL_ROWS * NUM_OF_GLOBAL_COLS; for (int ele_idx = 0; ele_idx < mat_size; ++ele_idx) { /* Convert input element index to input matrix row and column */ int mat_row_num = ele_idx / NUM_OF_GLOBAL_COLS; int mat_col_num = ele_idx % NUM_OF_GLOBAL_COLS; int sum = 0; for (int row_offset = -KERNEL_SIZE; row_offset <= KERNEL_SIZE; ++row_offset) { for (int col_offset = -KERNEL_SIZE; col_offset <= KERNEL_SIZE; ++col_offset) { /* Get kernel matrix row and column */ int mat_ker_row_num = mat_row_num + row_offset; int mat_ker_col_num = mat_col_num + col_offset; if ((mat_ker_row_num >= 0) && (mat_ker_row_num < NUM_OF_GLOBAL_ROWS) && (mat_ker_col_num >= 0) && (mat_ker_col_num < NUM_OF_GLOBAL_COLS)) { /* Convert kernel matrix row and column to kernel element index */ int ker_ele_idx = MATRIX_TO_ARRAY_INDEX(mat_ker_row_num, mat_ker_col_num, NUM_OF_GLOBAL_COLS); if (ker_ele_idx >= 0) { sum += h_A[ker_ele_idx]; } } } } if (h_B[ele_idx] != sum) { result.status = FAILED; result.index = ele_idx; return result; } } return result; } /***********************************************************************************\ * Host code to initialize input matrix \***********************************************************************************/ void initHostInputMatrix(int* h_A) { for (int idx = 0; idx < (NUM_OF_GLOBAL_ROWS*NUM_OF_GLOBAL_COLS); ++idx) { h_A[idx] = (idx / NUM_OF_GLOBAL_COLS) + 1; } } /***********************************************************************************\ * Function to check CUDA error \***********************************************************************************/ inline cudaError_t checkCuda(cudaError_t result, Cuda_Event_T cuda_event) { char error_string[100]; switch (cuda_event) { case ALLOCATE_DEVICE_MATRIX_A: strcpy(error_string, "Failed to allocate device matrix A"); break; case ALLOCATE_DEVICE_MATRIX_B: strcpy(error_string, "Failed to allocate device matrix B"); break; case COPY_MATRIX_A_FROM_HOST_TO_DEVICE: strcpy(error_string, "Failed to copy matrix A from host to device"); break; case LAUNCH_KERNEL_CONV2DDEVICE: strcpy(error_string, "Failed to launch conv2DDevice kernel"); break; case DEVICE_SYNCHRONIZATION: strcpy(error_string, "Failed to synchronize"); break; case COPY_MATRIX_B_FROM_DEVICE_TO_HOST: strcpy(error_string, "Failed to copy matrix B from device to host"); break; case FREE_DEVICE_MATRIX_A: strcpy(error_string, "Failed to free device matrix A"); break; case FREE_DEVICE_MATRIX_B: strcpy(error_string, "Failed to free device matrix B"); break; default: strcpy(error_string, "NOT DUE TO ONE OF THE CUDA EVENTS"); break; } if (result != cudaSuccess) { fprintf(stderr, "CUDA Runtime Error: %s (error code: %s)\n", error_string, cudaGetErrorString(result)); assert(result == cudaSuccess); exit(EXIT_FAILURE); } return result; } /***********************************************************************************\ * Host main routine \***********************************************************************************/ int main(void) { /* Print the matrix dimension to be used, and compute its size */ int numElements = NUM_OF_GLOBAL_ROWS * NUM_OF_GLOBAL_COLS; size_t size = numElements * sizeof(int); printf("[Convolution of matrix of (%d, %d) elements]\n", NUM_OF_GLOBAL_ROWS, NUM_OF_GLOBAL_COLS); /* Allocate the host input matrix A */ int *h_A = (int *)malloc(size); /* Allocate the host output matrix B */ int *h_B = (int *)malloc(size); /* Verify that allocations succeeded */ if (h_A == NULL || h_B == NULL) { fprintf(stderr, "Failed to allocate host matrix!\n"); exit(EXIT_FAILURE); } /* Initialize the host input matrix */ initHostInputMatrix(h_A); /* Allocate the device input matrix A */ int *d_A = NULL; checkCuda(cudaMalloc((void **)&d_A, size), ALLOCATE_DEVICE_MATRIX_A); /* Allocate the device output matrix B */ int *d_B = NULL; checkCuda(cudaMalloc((void **)&d_B, size), ALLOCATE_DEVICE_MATRIX_B); /* Copy the host input matrix A in host memory to the device input matrix in device memory */ printf("Copy input data from the host memory to the CUDA device\n"); checkCuda(cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice), COPY_MATRIX_A_FROM_HOST_TO_DEVICE); clock_t tic = clock(); /* Launch the 2D Convolution CUDA Kernel */ dim3 block_dim(BLOCK_SIZE, BLOCK_SIZE); dim3 grid_dim(((NUM_OF_GLOBAL_COLS + BLOCK_SIZE - 1) / BLOCK_SIZE), ((NUM_OF_GLOBAL_ROWS + BLOCK_SIZE - 1) / BLOCK_SIZE)); printf("CUDA kernel launch with (%d,%d) blocks of (%d,%d) threads\n", grid_dim.x, grid_dim.y, block_dim.x, block_dim.y); conv2DDevice<<<grid_dim, block_dim>>>(d_A, d_B); checkCuda(cudaGetLastError(), LAUNCH_KERNEL_CONV2DDEVICE); /* Wait for device to finish running the 2D Convolution */ checkCuda(cudaDeviceSynchronize(), DEVICE_SYNCHRONIZATION); clock_t toc = clock(); printf("2D Convolution on GPU time: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); /* Copy the device result vector in device memory to the host result vector in host memory */ printf("Copy output data from the CUDA device to the host memory\n"); checkCuda(cudaMemcpy(h_B, d_B, size, cudaMemcpyDeviceToHost), COPY_MATRIX_B_FROM_DEVICE_TO_HOST); /* Verify that the result vector is correct */ tic = clock(); Result_T result = checkResult(h_A, h_B); if(FAILED == result.status) { fprintf(stderr, "Result verification failed at element %d!\n", result.index); exit(EXIT_FAILURE); } toc = clock(); printf("2D Convolution on CPU time: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); printf("Test PASSED\n"); /* Free device global memory */ checkCuda(cudaFree(d_A), FREE_DEVICE_MATRIX_A); checkCuda(cudaFree(d_B), FREE_DEVICE_MATRIX_B); /* Free host memory */ free(h_A); free(h_B); printf("Done\n"); return 0; }
21,502
// Use grid strided loops, described here: // https://devblogs.nvidia.com/cuda-pro-tip-write-flexible-kernels-grid-stride-loops/ // This pattern ensures that all of the loop values are visited once, no matter // what grid parameters are used for the function. // We cannot include CUDA header for mathematical constants, since it requires // that the development headers of the CUDA toolkit are installed. template <typename T> struct Constants {}; template <> struct Constants<double> { static constexpr double INV_SQRT_2 = 0.7071067811865475; static constexpr double INV_SQRT_2PI = 0.3989422804014327; }; template <> struct Constants<float> { static constexpr float INV_SQRT_2 = 0.70710677; static constexpr float INV_SQRT_2PI = 0.3989423; }; template <typename U> __global__ void gather_add(U* out_bo, const U* table_to, const int* indices_bk, int T, int O, int B, int K) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int b = _loop_start; b < B; b += _loop_stride) { for (int k = 0; k < K; ++k) { int idx = indices_bk[b * K + k]; const U* table = table_to + idx * O; U* out = out_bo + b * O; for (int o = 0; o < O; ++o) { out[o] += table[o]; } } } } template <typename T> __global__ void seq2col(T* output, const T* X, const int* lengths, int nW, int B, int I, int nL) { // Let's say nW is 1 (it usually is). Then we want to take: // 1a 1b 1c // 2a 2b 2c // 3a 3b 3c // And make // __ __ __ 1a 1b 1c 2a 2b 2c // 1a 1b 1c 2a 2b 2c 3a 3b 3c // 2a 2b 2c 3a 3b 3c __ __ __ // Where __ is padding. // Now let's say nW is 2. Then we want to take: // 1a 1b 1c // 2a 2b 2c // 3a 3b 3c // And make // __ __ __ __ __ __ 1a 1b 1c 2a 2b 2c 3a 3b 3c // __ __ __ 1a 1b 1c 2a 2b 2c 3a 3b 3c __ __ __ // 1a 1b 1c 2a 2b 2c 3a 3b 3c __ __ __ __ __ __ // * x_start=-6, x_end=9 : (0-2) * 3, (0+2+1) * 3 // * x_start=-3, x_end=13 : (1-2) * 3, (1+2+1) * 3 // * x_start=0, x_end=16 : (2-2) * 3, (2+2+1) * 3 // // If lengths > 1, then the sequence lengths dictate // the boundaries/padding rather than the begin/end // of X. int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; int nF = nW * 2 + 1; int seq = 0; int seq_start = 0; for (int b = _loop_start; b < B; b += _loop_stride) { // Find sequence sequence in which b lies. for (; seq < nL; ++seq) { if (b < seq_start + lengths[seq]) { break; } seq_start += lengths[seq]; } // Calculate the bounds of the sequence wherein b lies. int seq_end = seq_start + lengths[seq]; // Find the unconstrained window around b, which // may be out of the sequence bounds. int window_start = b - nW; int window_end = b + nW + 1; // Find the sequence-constrained window around b. int x_start = max(seq_start, window_start); int x_end = min(seq_end, window_end); int n_elems = x_end - x_start; // If the left window is cut short, we want to start by // the same amount in the output. int out_offset = x_start - window_start; for (int i = 0; i < n_elems * I; i++) { output[(b * I * nF) + (out_offset * I) + i] = X[(x_start * I) + i]; } } } template <typename T> __global__ void pad(T* out, T const **seqs, int const *lengths, int stride, int N, int L) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < L * stride; i += _loop_stride) { for (int j = 0; j < N; ++j) { T const *seq = seqs[j]; if (i < lengths[j] * stride) { out[j * L * stride + i] = seq[i]; } else { out[j * L * stride + i] = T(); } } } } template <typename T> __global__ void maxout(T* best, int* which, const T* cands, int B, int O, int P) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int bo = _loop_start; bo < B * O; bo += _loop_stride) { // Go to the candidates at the output we're working on const T* cands_bo = &cands[bo * P]; int best_idx = 0; T best_val = cands_bo[0]; for (int p = 1; p < P; ++p) { if (cands_bo[p] > best_val) { best_idx = p; best_val = cands_bo[p]; } } which[bo] = best_idx; best[bo] = best_val; } } template <typename T> __global__ void clipped_linear(T* Y, const T* X, double slope, double offset, double min_val, double max_val, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T y = X[i] * slope + offset; Y[i] = min(max(y, min_val), max_val); } } template <typename T> __global__ void dish(T* Y, const T* X, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; Y[i] = 0.5 * x * (x / sqrt(1 + x * x) + 1); } } template <typename T> __global__ void gelu(T* Y, const T* X, double threshold, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; if (x >= threshold) { Y[i] = x; } else if (x <= -threshold) { Y[i] = 0.0; } else { T cdf = 0.5 * (1.0 + erf(Constants<T>::INV_SQRT_2 * x)); Y[i] = x * cdf; } } } template <typename T> __global__ void mish(T* Y, const T* X, double threshold, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; T one = 1.; for (int i = _loop_start; i < N; i += _loop_stride) { if (X[i] >= threshold) Y[i] = X[i]; else Y[i] = X[i] * tanh(log(one + exp(X[i]))); } } template <typename T> __global__ void swish(T* Y, const T* X, double threshold, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { if (X[i] >= threshold) { Y[i] = X[i]; } else if (X[i] <= -threshold) { Y[i] = 0.0; } else { T logistic_cdf = 1.0 / (1.0 + exp(-X[i])); Y[i] = X[i] * logistic_cdf; } } } template <typename U> __global__ void reduce_sum(U* output, const U* X, const int* lengths, int B, int T, int O) { // Compute sums of a batch of concatenated sequences int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int b = _loop_start; b < B; b += _loop_stride) { // Go to the regions we're working on U* output_b = &output[b*O]; // Find the sequence item we're working on int t = 0; for (int i=0; i < b; ++i) { t += lengths[i]; } int length = lengths[b]; // Each invocation of the kernel sums one batch. for (int i=0; i < length; ++i) // Iterate over rows { const U* X_t = &X[(t+i)*O]; for (int j=0; j < O; ++j) { output_b[j] += X_t[j]; } } } } template <typename U> __global__ void reduce_max(U* maxes, int* which, const U* X, const int* lengths, int B, int T, int O) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int b = _loop_start; b < B; b += _loop_stride) { // Go to the regions we're working on U* maxes_b = &maxes[b*O]; int* which_b = &which[b*O]; // Find the sequence item we're working on const U* X_t = X; for (int i=0; i < b; ++i) { X_t += lengths[i] * O; } // Each invocation of the kernel maxes one sequence. // Start by assuming maxes are the first element. for (int i=0; i < O; ++i) { maxes_b[i] = X_t[i]; which_b[i] = 0; } int length = lengths[b]; for (int i=1; i < length; ++i) // Iterate over rows { X_t += O; for (int j=0; j < O; ++j) { if (X_t[j] > maxes_b[j]) { maxes_b[j] = X_t[j]; which_b[j] = i; } } } } } template <typename T> __global__ void backprop_seq2col(T* d_seqs, const T* d_cols, const int* lengths, int nW, int B, int I, int nL) { // Here's what we're doing, if we had 2d indexing. //for i in range(B): // d_seq[i] += d_cols[i-2, 4] // d_seq[i] += d_cols[i-1, 3] // d_seq[i] += d_cols[i, 2] // d_seq[i] += d_cols[i+1, 1] // d_seq[i] += d_cols[i+2, 0] int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; int nF = nW * 2 + 1; int seq = 0; int seq_start = 0; for (int b = _loop_start; b < B; b += _loop_stride) { // Find sequence offset in which b lies. // Fixme: do not restart offset search for every b. for (; seq < nL; ++seq) { if (b < seq_start + lengths[seq]) { break; } seq_start += lengths[seq]; } // Calculate the bounds of the sequence wherein b lies. int seq_end = seq_start + lengths[seq]; // Find the unconstrained window around b, which // may be out of the sequence bounds. int window_start = b - nW; int window_end = b + nW + 1; // Find the sequence-constrained window around b. int d_seqs_start = max(seq_start, window_start); int d_seqs_end = min(seq_end, window_end); // The here update proceeds differently than the other seq2col // implementations. We have to do all the updates for the b in this loop // iteration, otherwise we get data races due to parallelism in CUDA. // // A batch item b occurs, given nw=1, in: // // position 0 in b - 1 (if present) <- window_start // position 1 in b // position 2 in b + 1 (if present) <- window_end // // The following loop sums the gradients for those occurrences. // b_w loops over [b - 1, b, b + 1] and computes the position // of b within the column gradients of [b - 1 ... b + 1]. for (int b_w = d_seqs_start; b_w < d_seqs_end; ++b_w) { int position = (2 * nW) - (b_w - window_start); int start = (b_w * I * nF) + (position * I); for (int i = 0; i < I; ++i) { d_seqs[(b*I + i)] += d_cols[start + i]; } } } } template <typename T> __global__ void backprop_clipped_linear(T* dX, const T* dY, const T* X, double slope, double offset, double min_val, double max_val, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; T low = (min_val - offset) / slope; T high = (max_val - offset) / slope; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; if (low < x && x < high) { dX[i] = dY[i] * slope; } else { dX[i] = 0; } } } template <typename T> __global__ void backprop_hard_swish(T* dX, const T* dY, const T* X, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { if (X[i] > 2.5) { dX[i] = dY[i]; } else if (X[i] < -2.5) { dX[i] = 0; } else { dX[i] = dY[i] * (X[i] * 0.4 + 0.5); } } } template <typename T> __global__ void backprop_hard_swish_mobilenet(T* dX, const T* dY, const T* X, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { if (X[i] > 3.0) { dX[i] = dY[i]; } else if (X[i] < -3.0) { dX[i] = 0; } else { dX[i] = dY[i] * ((X[i] * 2.0 + 3.0) / 6.0); } } } template <typename T> __global__ void backprop_dish(T* dX, const T* dY, const T* X, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; T x_sq = x * x; T x_sq_plus_one = x_sq + 1.0; dX[i] = dY[i] * (x/sqrt(x_sq_plus_one) - (0.5 * x * x_sq) / pow(x_sq_plus_one, static_cast<T>(1.5)) + 0.5); } } template <typename T> __global__ void backprop_gelu(T* dX, const T* dY, const T* X, double threshold, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; if (x >= threshold) { dX[i] = dY[i]; } else if (x <= -threshold) { dX[i] = 0.0; } else { T cdf = 0.5 * (1.0 + erf(Constants<T>::INV_SQRT_2 * x)); T pdf = Constants<T>::INV_SQRT_2PI * exp(-0.5 * x * x); dX[i] = dY[i] * (cdf + x * pdf); } } } template <typename T> __global__ void backprop_maxout(T* dX, const T* dY, const int* which, int B, int O, int P) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int b = _loop_start; b < B; b += _loop_stride) { // Go to the regions we're working on T* dX_b = &dX[b*O*P]; const T* dY_b = &dY[b*O]; const int* which_b = &which[b*O]; for (int i=0; i < O; ++i) dX_b[(i*P)+which_b[i]] = dY_b[i]; } } template <typename T> __global__ void backprop_mish(T* dX, const T* dY, const T* X, double threshold, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; if (x >= threshold) { dX[i] = dY[i]; } else { T exp_x = exp(x); T exp_2x = exp(2*x); T exp_3x = exp(3*x); T omega = (4. * (x+1)) + (4 * exp_2x) + exp_3x + exp_x * (4.*x+6); T delta = 2 * exp_x + exp_2x + 2; dX[i] = dY[i] * ((exp_x * omega) / (delta * delta)); } } } template <typename T> __global__ void backprop_swish(T* dX, const T* dY, const T* X, const T* Y, double threshold, int N) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; for (int i = _loop_start; i < N; i += _loop_stride) { T x = X[i]; T y = Y[i]; if (x >= threshold) { dX[i] = dY[i]; } else if (x <= -threshold) { dX[i] = 0.0; } else { T cdf = 1.0 / (1 + exp(-x)); T d = y + cdf * (1 - y); dX[i] = dY[i] * d; } } } template <typename U> __global__ void backprop_reduce_sum(U* dX, const U* d_sum, const int* lengths, int B, int T, int O) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; int seq_start = 0; int b = 0; for (int t = _loop_start; t < T; t += _loop_stride) { // Find the sequence item we're working on while ((b < B) && (seq_start+lengths[b]) <= t) { seq_start += lengths[b]; b += 1; } if (lengths[b] == 0) continue; for (int i=0; i < O; ++i) { dX[t * O + i] = d_sum[b * O + i]; } } } template <typename U> __global__ void backprop_reduce_mean(U* dX, const U* d_mean, const int* lengths, int B, int T, int O) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; int seq_start = 0; int b = 0; for (int t = _loop_start; t < T; t += _loop_stride) { // Find the sequence item we're working on while ((b < B) && (seq_start+lengths[b]) <= t) { seq_start += lengths[b]; b += 1; } if (lengths[b] == 0) continue; U* dX_t = &dX[t * O]; const U* d_mean_b = &d_mean[b * O]; int lengths_b = lengths[b]; for (int i=0; i < O; ++i) { dX_t[i] = d_mean_b[i] / lengths_b; } } } template <typename U> __global__ void backprop_reduce_max(U* dX, const U* d_maxes, const int* which, const int* lengths, int B, int T, int O) { int _loop_start = blockIdx.x * blockDim.x + threadIdx.x; int _loop_stride = blockDim.x * gridDim.x; int seq_start = 0; int b = 0; for (int t = _loop_start; t < T; t += _loop_stride) { // We're calculating the gradient of the unpooled sequences, from // the gradient of the maxes. In this loop, we're getting the gradient // of a single sequence item, t. We need to know the sequence index, // b. while ((b < B) && (seq_start+lengths[b]) <= t) { seq_start += lengths[b]; b += 1; } if (lengths[b] == 0) continue; // The "which" array tells us which rows were selected as the max. // So we need to find the index of our t in the sequence. int index_of_t = t-seq_start; // Get the rows we're dealing with, to avoid cluttering the loop // with the index math. U* dX_t = &dX[t*O]; const U* d_maxes_b = &d_maxes[b*O]; const int* which_b = &which[b*O]; // Now loop over our row. for (int i=0; i < O; ++i) { // If we used the value for this cell, // pass the gradient if (which_b[i] == index_of_t) dX_t[i] = d_maxes_b[i]; } } }
21,503
#include "includes.h" __global__ void variance_delta_kernel(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta) { int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; if (i >= filters) return; int j,k; variance_delta[i] = 0; for(j = 0; j < batch; ++j){ for(k = 0; k < spatial; ++k){ int index = j*filters*spatial + i*spatial + k; variance_delta[i] += delta[index]*(x[index] - mean[i]); } } variance_delta[i] *= -.5 * pow(variance[i] + .000001f, (float)(-3./2.)); }
21,504
#include "includes.h" __global__ void devFindUniqueTriangleAffected(int maxIndex, int *pTriangleAffected, int *pTriangleAffectedIndex, int *pUniqueFlag) { unsigned int i = blockIdx.x*blockDim.x + threadIdx.x + 1; while (i < maxIndex) { if (pTriangleAffected[i-1] == pTriangleAffected[i] && pTriangleAffected[i] != -1) { int j = pTriangleAffectedIndex[i]; pUniqueFlag[j] = 0; } i += gridDim.x*blockDim.x; } }
21,505
#include <math.h> #include <cuComplex.h> #include "quaternions.cu" #define SIGN(x) (x>=0.0?1.0:-1.0) template <typename T> __device__ void actfunc_real(T *a, const int N) { int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N) a[i] = SIGN(a[i]); } __device__ void actfunc_comp(cuFloatComplex *a, const int N) { int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N) a[i] = make_cuFloatComplex(SIGN(cuCrealf(a[i])),SIGN(cuCimagf(a[i]))); } __device__ void actfunc_comp(cuDoubleComplex *a, const int N) { int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N) a[i] = make_cuDoubleComplex(SIGN(cuCreal(a[i])),SIGN(cuCimag(a[i]))); } __device__ void actfunc_quat(Quaternionf *a, const int N) { int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N) a[i] = make_float4(SIGN(a[i].x),SIGN(a[i].y),SIGN(a[i].z),SIGN(a[i].w)); } __device__ void actfunc_quat(Quaternion *a, const int N) { int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N) a[i] = make_double4(SIGN(a[i].x),SIGN(a[i].y),SIGN(a[i].z),SIGN(a[i].w)); } __device__ void actfunc_comp_multi(cuFloatComplex *a, const int K, const int N) { cuFloatComplex t; float hphi, phi, theta, w; int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N){ hphi = M_PI/K; phi = 2.0f * hphi; t = a[i]; theta = atan2f(cuCimagf(t),cuCrealf(t))+hphi; w = floorf(theta / phi) * phi; a[i] = make_cuFloatComplex(cosf(w),sinf(w)); } } __device__ void actfunc_comp_multi(cuDoubleComplex *a, const int K, const int N) { cuDoubleComplex t; double hphi, phi, theta, w; int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N){ hphi = M_PI/K; phi = 2.0 * hphi; t = a[i]; theta = atan2(cuCimag(t),cuCreal(t))+hphi; w = floor(theta / phi) * phi; a[i] = make_cuDoubleComplex(cos(w),sin(w)); } } __device__ float qsign(float phase, float coef, const int K) { float dphase, phase0, k, p; dphase = 2*M_PI/K*coef; phase0 = M_PI*coef; k = roundf((phase-0.5f*dphase + phase0)/dphase); p = k*dphase - phase0; return p+0.5f*dphase; } __device__ void actfunc_quat_multi(Quaternionf *a, const int A, const int B, const int C, const int N) { float phi, theta, psi; int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N){ QuaternionArgf q = quaternionarg(a[i]); phi = qsign(q.y, 1.0f, A); theta = qsign(q.z, 0.5f, B); psi = qsign(q.w, 0.25f, C); a[i] = quaternion(quaternionarg(1.0f, phi, theta, psi)); } } __device__ double qsign(double phase, double coef, int K) { double dphase, phase0, k, p; dphase = 2*M_PI/K*coef; phase0 = M_PI*coef; k = round((phase-0.5*dphase + phase0)/dphase); p = k*dphase - phase0; return p+0.5*dphase; } __device__ void actfunc_quat_multi(Quaternion *a, const int A, const int B, const int C, const int N) { double phi, theta, psi; int i = threadIdx.x + blockIdx.x * blockDim.x; if(i<N){ QuaternionArg q = quaternionarg(a[i]); phi = qsign(q.y, 1.0, A); theta = qsign(q.z, 0.5, B); psi = qsign(q.w, 0.25, C); a[i] = quaternion(quaternionarg(1.0, phi, theta, psi)); } } extern "C" { void __global__ actfunc_real_float(float *a, const int N) { actfunc_real(a, N);} void __global__ actfunc_real_double(double *a, const int N) { actfunc_real(a, N);} void __global__ actfunc_comp_float(cuFloatComplex *a, const int N) { actfunc_comp(a, N);} void __global__ actfunc_comp_double(cuDoubleComplex *a, const int N) { actfunc_comp(a, N);} void __global__ actfunc_quat_float(Quaternionf *a, const int N) { actfunc_quat(a, N);} void __global__ actfunc_quat_double(Quaternion *a, const int N) { actfunc_quat(a, N);} void __global__ actfunc_comp_multi_float(cuFloatComplex *a, const int K, const int N) { actfunc_comp_multi(a, K, N);} void __global__ actfunc_comp_multi_double(cuDoubleComplex *a, const int K, const int N) { actfunc_comp_multi(a, K, N);} void __global__ actfunc_quat_multi_float(Quaternionf *a, const int A, const int B, const int C, const int N) { actfunc_quat_multi(a, A, B, C, N);} void __global__ actfunc_quat_multi_double(Quaternion *a, const int A, const int B, const int C, const int N) { actfunc_quat_multi(a, A, B, C, N);} }
21,506
#include <stdio.h> class A { public: int num; __host__ __device__ A(int n) { num = n; } __host__ __device__ int get_num() { return num; } }; __global__ void kernel(A* a) { printf("%d\n", a->get_num()); } int main() { A* a = new A(3); printf("%d\n", a->get_num()); A* d_a; cudaMalloc(&d_a, sizeof(A)); cudaMemcpy(d_a, a, sizeof(A), cudaMemcpyHostToDevice); kernel<<< 1, 1 >>>(d_a); cudaDeviceSynchronize(); return 0; }
21,507
// // stencil main program // using CUDA to do parallel computing of stencil // do t repeated run // do it for n = 500, 1000, 20000 and t = 10 // Created by Ethan Zhang on 11/8/18. // Copyright © 2018 Ethan Zhang. All rights reserved. // #include <iostream> #include <algorithm> #include "stdio.h" #include "cmath" #include <float.h> #include "cuda.h" #include <ctime> #define THREADS_PER_DIM 25 // #define TASKS_PER_THREADS 50 // #define BLOCKS 32 // #define N 1000*1000 // #define RADIUS 1001 // #define TASKS using namespace std; __device__ double get2ndMin(double *candidates){ double first, second; first = second = DBL_MAX; for(int k =0; k<4; k++){ if(candidates[k]<=first){ second = first; first = candidates[k]; } else if (candidates[k] <= second && candidates[k] >= first){ second = candidates[k];} } return second; } __global__ void serial_calc(int n, double *A, double *prev_A){ for(int i = 0; i<n; i++){ for(int j=0; j<n; j++){ if(i==0 || i==n-1 || j==0 || j==n-1){ A[i*n+j] = prev_A[i*n+ j]; } double candidates[] ={A[(i+1)*n+ (j+1)], A[(i+1)*n +(j-1)], A[(i-1)*n + j-1], A[(i-1)*n + j+1]}; A[i*n+j] = prev_A[i*n+j] + get2ndMin(candidates); } // printf("i %d\n", i); } } // int j = threadIdx.y + blockIdx.y * blockDim.y; // int i = threadIdx.x + blockIdx.x * blockDim.x; // if(i ==0 || i ==n-1 || j ==0 || j ==n-1){ // dA[i*n+j] = prev_dA[i*n+j]; // }else{ // // tmp[lindex_x-1][lindex_y-1] = A[i-1][j-1] // double candidates[] = {prev_dA[(i+1)*n+(j+1)], prev_dA[(i+1)*n+(j-1)],prev_dA[(i-1)*n+(j-1)],prev_dA[(i-1)*n+(j+1)]}; // dA[i*n+j] = prev_dA[i*n+j] + get2ndMin(candidates); // } // __syncthreads(); // printf("exec. in block%d, threads%d, i%d, j%d, \n", blockIdx.x, threadIdx.x, i, j); // } //parent node // __global__ void stencil(double *dA,int n){ // calc<<<BLOCKS, THREADS_PER_DIM>>>(n, dA); // __syncthreads(); // printf("exec. in parent node\n"); // } __global__ void verification(double *A, int n){ double v1, v2,v3; v1 = 0.0; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ v1 += A[i*n+j]; } } int fl = floor((double)n/2); v2 = A[fl*n+fl]; v3 = A[37*n+47]; // __syncthreads(); A[0] = v1; A[1] = v2; A[2] = v3; } double verisum_all(int n, double *A){ double sum=0.0; for(int i = 0; i<n; i++){ for(int j=0; j<n; j++){ sum += A[i*n+j]; } } return sum; } double value_half(int n, double *A){ int fl = floor((double)n/2); double result = A[fl*n+ fl]; return result; } double value_37_47(int n, double *A){ double result =A[37*n+47]; return result; } int main(int argc, char** argv) { // initialize below int n = atoi(argv[1]); int N = n*n; printf("size N%d\n",N); // initialize below // 1d stencil double *array; int size = (N) * sizeof(double); array =(double *)malloc(size); for(int i =0; i<n;i++){ for(int j =0; j<n; j++){ array[i*n+j] = pow(1+cos(2*i)+sin(j),2); } } //verify initialization results double verisum_1 = verisum_all(n, array); double half_value_1 = value_half(n, array); double spec_1 = value_37_47(n, array); //print result printf("init verisum all %f\n", verisum_1); printf("init verification n/2 %f\n", half_value_1); printf("init verification A[37][47] %f\n", spec_1); double *dA; double *prev_dA; // allocate memory on device cudaMalloc((void **)&dA, size); cudaMalloc((void **)&prev_dA, size); // Copy inputs to device cudaMemcpy(dA, array, size, cudaMemcpyHostToDevice); cudaMemcpy(prev_dA, array, size, cudaMemcpyHostToDevice); //launch kernal on device int t = 10; // dim3 dimBlock(THREADS_PER_DIM, THREADS_PER_DIM); // dim3 dimGrid(n/THREADS_PER_DIM, n/ THREADS_PER_DIM); cudaEvent_t start, stop; float time; cudaEventCreate(&start); cudaEventCreate(&stop); // double v1 =0.0; cudaEventRecord(start, 0); for(int episode =0; episode<t; episode++){ // printf("loop %d\n", episode ); serial_calc<<<1, 1>>>(n, dA, prev_dA); cudaDeviceSynchronize(); double *tem_a = dA; dA = prev_dA; prev_dA = tem_a; } verification<<<1,1>>>(prev_dA,n); cudaEventRecord(stop, 0); cudaMemcpy(array,prev_dA, size, cudaMemcpyDeviceToHost); cudaEventElapsedTime(&time, start, stop); //print result printf ("Time for the kernel: %f ms\n", time); printf("verisum all %f\n", array[0]); printf("verification n/2 %f\n", array[1]); printf("verification A[37][47] %f\n", array[2]); //free memory free(array); // free(tem_a); cudaFree(dA); cudaFree(prev_dA); return 0; }
21,508
#include<bits/stdc++.h> using namespace std; __global__ void matMul(int R,int M,int C,int *A, int *B, int *D){ int i = blockIdx.x, j = threadIdx.x, block_size = blockDim.x; assert(i<R && j<C && block_size == C); D[i*C + j] = 0; for(int k=0;k<M;++k) D[i*C + j] += A[i*M + k] * B[k*C + j]; return; } int main(int argc, char *argv[]){ srand(0); int R = 256, M = 256, C = 256; if(argc > 1) R = stoi(argv[1]); if(argc > 2) M = stoi(argv[2]); if(argc > 3) C = stoi(argv[3]); int **A = new int* [R], **B = new int* [M], **D = new int* [R]; A[0] = new int [R*M]; for(int i=1;i<R;++i) A[i] = A[i-1] + M; for(int i=0;i<R;++i) for(int j=0;j<M;++j) A[i][j] = rand()%10; B[0] = new int [M*C]; for(int i=1;i<M;++i) B[i] = B[i-1] + C; for(int i=0;i<M;++i) for(int j=0;j<C;++j) B[i][j] = rand()%10; D[0] = new int [R*C]; for(int i=1;i<R;++i) D[i] = D[i-1] + C; clock_t start_time,end_time; start_time = clock(); //Record the starting time int *dA, *dB, *dD; cudaMalloc((void **)&dA, R*M*sizeof(int)); cudaMalloc((void **)&dB, M*C*sizeof(int)); cudaMalloc((void **)&dD, R*C*sizeof(int)); // Copy data to divice cudaMemcpy(dA, A[0], R*M*sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(dB, B[0], M*C*sizeof(int), cudaMemcpyHostToDevice); // Running code on GPUs int block_size = C, n_block = R; matMul<<<n_block, block_size>>>(R, M, C, dA, dB, dD); cudaMemcpy(D[0], dD, R*C*sizeof(int), cudaMemcpyDeviceToHost); cudaFree(dA); cudaFree(dB); cudaFree(dD); end_time = clock(); // Record the ending time double dt = double(end_time - start_time)/CLOCKS_PER_SEC; cout<<"Time Usage: "<<dt<<"s\nResults:\n"; int stride = R*C/10; for(int i=0;i<R*C;i+=stride) cout<<D[i/C][i%C]<<' '; cout<<endl; delete [] A[0]; delete [] B[0]; delete [] D[0]; delete [] A; delete [] B; delete [] D; return 0; }
21,509
#include "includes.h" __global__ void VecAddInt32(int32_t* in0, int32_t* in1, int32_t* out, int cnt) { int tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < cnt) { out[tid] = in0[tid] + in1[tid]; } }
21,510
#include <stdio.h> #include <stdlib.h> // Variables float* h_A; // host vectors float* h_B; float* h_C; float* h_D; float* d_A; // device vectors float* d_B; float* d_C; // Functions void RandomInit(float*, int); __global__ void MatAdd(const float*, const float*, float*, int, int); // Host code int main(){ // Settings // gid -> GPU device id (0, 1, ...) // err -> error message get from CUDA calls // N, M -> Matrix size N x M // mem -> to calculate maximum memory size // // dimBlock, dimGrid -> Launching the kernel // temp -> For temperory store the data // // start, stop -> CUDA event timer // Intime -> Calculate the input time, allocate and move data in device memory // gputime -> Time spent for doing the calculation // Outime -> Copy result from device to host memory // gputime_tot -> Time total spent // // cputime -> Time spent for only using CPU // // sum, diff -> For checking the result from GPU int gid; cudaError_t err; int N, M; int mem = 1024 * 1024 * 1024; int block_size; cudaEvent_t start, stop; float Intime, gputime, Outime, gputime_tot; float cputime; double sum = 0, diff; // Select GPU device printf("Select the GPU with device ID: "); scanf("%d", &gid); err = cudaSetDevice(gid); if (err != cudaSuccess) { printf("!!! Cannot select GPU with device ID = %d\n", gid); exit(1); } printf("Set GPU with device ID = %d\n", gid); // Input the size of Matrix A, B, size = N x M printf("Matrix Addition of N x M Matrix: C = A + B\n"); printf("Enter the width (col) of the matrix: "); scanf("%d", &M); printf("Matrix width = %d\n", M); printf("Enter the height (row) of the matrix: "); scanf("%d", &N); printf("Matrix height = %d\n", N); // Check if the memory can fit the A,B,C matrix if(3 * N * M > mem){ printf("The size of these 3 matrices cannot be fitted into 4 Gbyte\n"); exit(2); } long size = N * M * sizeof(float); // Allocate matrix A, B, C on host memory h_A = (float*) malloc(size); h_B = (float*) malloc(size); h_C = (float*) malloc(size); // Initialize the matrices RandomInit(h_A, N * M); RandomInit(h_B, N * M); // Input the block size, and compute the grid size // Enter the block size (threadsPerBlock) printf("Enter the number of square block size: "); scanf("%d", &block_size); printf("Block size = %d x %d\n", block_size, block_size); if(block_size * block_size > 1024){ printf("The number of threads per block must be less than 1024 ! \n"); exit(1); } dim3 dimBlock(block_size, block_size); // Calculate the grid size, so that every threads calculate one element in Matrix C dim3 dimGrid(M / dimBlock.x, N / dimBlock.y); printf("Grid size = %d x %d\n", dimGrid.y, dimGrid.x); if((dimGrid.x > 2147483647) || (dimGrid.y > 65535)){ printf("The number of blocks per grid is too much ! \n"); exit(1); } // Create the timer cudaEventCreate(&start); cudaEventCreate(&stop); // Start the timer: Record allocate memory and move data, from host to device cudaEventRecord(start, 0); // Allocate the matrices in device memory cudaMalloc((void**)&d_A, size); cudaMalloc((void**)&d_B, size); cudaMalloc((void**)&d_C, size); // Copy matrices from host to device memory cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice); cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice); // Stop the timer: Record allocate memory and move data, from host to device cudaEventRecord(stop, 0); cudaEventSynchronize(stop); // Calculate spend time: Record allocate memory and move data, from host to device cudaEventElapsedTime(&Intime, start, stop); printf("=================================\n"); printf("Allocate memory and move data from host to device time spent for GPU: %f (ms) \n", Intime); // Start the timer: Matrix Addition calculation time cudaEventRecord(start, 0); // Call the kernel MatAdd MatAdd<<<dimGrid, dimBlock>>>(d_A, d_B, d_C, N, M); // Stop the timer: Matrix Addition calculation time cudaEventRecord(stop, 0); cudaEventSynchronize(stop); // Calculate spend time: Matrix Addition calculation time cudaEventElapsedTime(&gputime, start, stop); printf("Matrix Addition calculation time for GPU: %f (ms) \n", gputime); printf("GPU Gflops: %f\n", 3 * N / (1000000.0 * gputime)); // Start the timer: Copy result from device memory to host memory cudaEventRecord(start, 0); // Copy result from device memory to host memory, and free the device memory cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost); cudaFree(d_A); cudaFree(d_B); cudaFree(d_C); // Stop the timer: Copy result from device memory to host memory cudaEventRecord(stop, 0); cudaEventSynchronize(stop); // Calculate spend time: Copy result from device memory to host memory cudaEventElapsedTime( &Outime, start, stop); printf("Copy result from device memory to host memory time spent for GPU: %f (ms) \n", Outime); // Total time spent for GPU gputime_tot = Intime + gputime + Outime; printf("Total time for GPU: %f (ms) \n", gputime_tot); // Start the timer: Time spent for just using CPU cudaEventRecord(start, 0); h_D = (float*) malloc(size); for(int i = 0; i < N * M; i = i+1){ h_D[i] = h_A[i] + h_B[i]; } // Stop the timer: Time spent for just using CPU cudaEventRecord(stop,0); cudaEventSynchronize(stop); // Calculate spend time: Time spent for just using CPU cudaEventElapsedTime(&cputime, start, stop); printf("---------------------------------\n"); printf("Time spent for just using CPU: %f (ms) \n", cputime); printf("CPU Gflops: %f\n", 3 * N / (1000000.0 * cputime)); printf("=================================\n"); printf("Speed up of GPU = %f\n", cputime/(gputime_tot)); // Destroy the timer cudaEventDestroy(start); cudaEventDestroy(stop); // Check the result printf("=================================\n"); printf("Check the result:\n"); for(int i = 0; i < N * M; i = i+1){ diff = abs(h_D[i] - h_C[i]); sum = sum + diff * diff; if(diff > 1.0e-15){ printf("row=%d, col=%d, h_D=%15.10e, h_C=%15.10e \n", i / M, i % M, h_D[i], h_C[i]); } } sum = sqrt(sum); printf("norm(h_C - h_D)=%20.15e\n\n", sum); cudaDeviceReset(); return 0; } // Create a random array of size n, with elements between 0~1 void RandomInit(float* data, int n){ for(int i = 0; i < n; i = i+1){ data[i] = rand() / (float)RAND_MAX; } } // Device code // Add matrix A and B, with size N x M together. __global__ void MatAdd(const float* A, const float* B, float* C, int N, int M){ int row = blockDim.y * blockIdx.y + threadIdx.y; int col = blockDim.x * blockIdx.x + threadIdx.x; int index = row * M + col; if ((index < N * N) && (col < M) && (row < N)){ C[index] = A[index] + B[index]; } __syncthreads(); }
21,511
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define MAX_SIZE 1024 #define GET_TIME(now){ \ struct timeval t; \ gettimeofday(&t, NULL); \ now = t.tv_sec * 1000 + t.tv_usec / 1000.0; \ } int arr[MAX_SIZE * MAX_SIZE]; int brr[MAX_SIZE * MAX_SIZE]; int crr[MAX_SIZE * MAX_SIZE]; __global__ void mp(int *a, int *b, int *c) { int i, sum; int id_0, id_1, id_2; int col = blockIdx.x * blockDim.x + threadIdx.x; int row = blockIdx.y * blockDim.y + threadIdx.y; sum = 0; for(i = 0; i < MAX_SIZE; i++){ id_0 = row * MAX_SIZE + i; id_1 = i * MAX_SIZE + col; sum += a[id_0] * b[id_1]; } id_2 = row * MAX_SIZE + col; c[id_2] = sum; } int main(void) { int *d_a, *d_b, *d_c; int block_size; int size = sizeof(int) * MAX_SIZE * MAX_SIZE; int i, j; double start_time, end_time; FILE *fp = fopen("matrix.txt","r"); printf("Input the Block Size: "); scanf("%d",&block_size); for(i = 0; i < MAX_SIZE; i++){ for(j = 0; j < MAX_SIZE; j++){ fscanf(fp,"%d",&arr[i * MAX_SIZE + j]); } } for(i = 0; i < MAX_SIZE; i++){ for(j = 0; j < MAX_SIZE; j++){ fscanf(fp,"%d",&brr[i * MAX_SIZE + j]); } } fclose(fp); cudaMalloc((void **)&d_a, size); cudaMalloc((void **)&d_b, size); cudaMalloc((void **)&d_c, size); cudaMemcpy(d_a, arr, size, cudaMemcpyHostToDevice); cudaMemcpy(d_b, brr, size, cudaMemcpyHostToDevice); GET_TIME(start_time); dim3 dimBlock (block_size, block_size); dim3 dimGrid(MAX_SIZE / dimBlock.x, MAX_SIZE/dimBlock.y); mp<<<dimGrid, dimBlock>>>(d_a, d_b, d_c); GET_TIME(end_time); cudaMemcpy(crr, d_c, size, cudaMemcpyDeviceToHost); fp = fopen("matrix_cu.txt","w"); for(i = 0; i < MAX_SIZE; i++){ for(j = 0; j < MAX_SIZE; j++){ fprintf(fp,"%d ",crr[i * MAX_SIZE + j]); } fprintf(fp,"\n"); } fclose(fp); printf("Matrix Mulitplication Cuda VER: Elapsed time is %e (msec)\n",end_time - start_time); cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); return 0; }
21,512
#include "includes.h" __global__ void AddTo32( int *sum, int *out, const int *pIn ) { (void) atomicAdd( &out[threadIdx.x], *pIn ); }
21,513
extern "C" { typedef struct { double e0; double e1; double e2; } struct_vec_9645; typedef struct { double e0; struct_vec_9645 e1; struct_vec_9645 e2; int e3; } struct_Isect_9647; __device__ inline int threadIdx_x() { return threadIdx.x; } __device__ inline int threadIdx_y() { return threadIdx.y; } __device__ inline int threadIdx_z() { return threadIdx.z; } __device__ inline int blockIdx_x() { return blockIdx.x; } __device__ inline int blockIdx_y() { return blockIdx.y; } __device__ inline int blockIdx_z() { return blockIdx.z; } __device__ inline int blockDim_x() { return blockDim.x; } __device__ inline int blockDim_y() { return blockDim.y; } __device__ inline int blockDim_z() { return blockDim.z; } __device__ inline int gridDim_x() { return gridDim.x; } __device__ inline int gridDim_y() { return gridDim.y; } __device__ inline int gridDim_z() { return gridDim.z; } __global__ void lambda_71814(unsigned long*, unsigned char*); __global__ __launch_bounds__ (32 * 4 * 1) void lambda_71814(unsigned long* _71817_104835, unsigned char* _71818_104836) { int threadIdx_y_104842; int pthreadIdx_y_104842; int blockDim_y_104848; int pblockDim_y_104848; int blockIdx_y_104854; int pblockIdx_y_104854; int threadIdx_x_104860; int pthreadIdx_x_104860; int blockDim_x_104866; int pblockDim_x_104866; int blockIdx_x_104872; int pblockIdx_x_104872; int threadIdx_x_104886; int pthreadIdx_x_104886; int blockDim_x_104889; int pblockDim_x_104889; int blockIdx_x_104892; int pblockIdx_x_104892; int threadIdx_y_104895; int pthreadIdx_y_104895; int blockDim_y_104898; int pblockDim_y_104898; int blockIdx_y_104901; int pblockIdx_y_104901; int lower_104904; int plower_104904; int upper_104905; int pupper_104905; int step_104906; int pstep_104906; double r_104907; double pr_104907; double g_104908; double pg_104908; double b_104909; double pb_104909; unsigned long state_104910; unsigned long pstate_104910; int _107577; int p_107577; int _107582; int p_107582; int _107589; int p_107589; int _107593; int p_107593; int _107600; int p_107600; int _107604; int p_107604; int threadIdx_y_107607; int pthreadIdx_y_107607; int blockDim_y_107610; int pblockDim_y_107610; int blockIdx_y_107613; int pblockIdx_y_107613; int threadIdx_x_107616; int pthreadIdx_x_107616; int blockDim_x_107619; int pblockDim_x_107619; int blockIdx_x_107622; int pblockIdx_x_107622; int threadIdx_y_107636; int pthreadIdx_y_107636; int blockDim_y_107639; int pblockDim_y_107639; int blockIdx_y_107642; int pblockIdx_y_107642; int threadIdx_x_107645; int pthreadIdx_x_107645; int blockDim_x_107648; int pblockDim_x_107648; int blockIdx_x_107651; int pblockIdx_x_107651; int threadIdx_y_107665; int pthreadIdx_y_107665; int blockDim_y_107668; int pblockDim_y_107668; int blockIdx_y_107671; int pblockIdx_y_107671; int threadIdx_x_107674; int pthreadIdx_x_107674; int blockDim_x_107677; int pblockDim_x_107677; int blockIdx_x_107680; int pblockIdx_x_107680; int lower_104915; int plower_104915; int upper_104916; int pupper_104916; int step_104917; int pstep_104917; double r_104918; double pr_104918; double g_104919; double pg_104919; double b_104920; double pb_104920; unsigned long state_104921; unsigned long pstate_104921; double length_104955; double plength_104955; double _104962; double p_104962; struct_vec_9645 vnormalize_104968; struct_vec_9645 pvnormalize_104968; double _104988; double p_104988; double length_105014; double plength_105014; double _105017; double p_105017; struct_vec_9645 vnormalize_105022; struct_vec_9645 pvnormalize_105022; struct_Isect_9647 ray_sphere_intersect_105025; struct_Isect_9647 pray_sphere_intersect_105025; double _105039; double p_105039; double length_105065; double plength_105065; double _105068; double p_105068; struct_vec_9645 vnormalize_105073; struct_vec_9645 pvnormalize_105073; struct_Isect_9647 ray_sphere_intersect_105076; struct_Isect_9647 pray_sphere_intersect_105076; double _105090; double p_105090; double length_105115; double plength_105115; double _105118; double p_105118; struct_vec_9645 vnormalize_105123; struct_vec_9645 pvnormalize_105123; struct_Isect_9647 ray_sphere_intersect_105126; struct_Isect_9647 pray_sphere_intersect_105126; double _105134; double p_105134; struct_Isect_9647 ray_plane_intersect_105145; struct_Isect_9647 pray_plane_intersect_105145; double _105160; double p_105160; double _105161; double p_105161; double _105162; double p_105162; double length_105181; double plength_105181; double _105184; double p_105184; struct_vec_9645 vnormalize_105189; struct_vec_9645 pvnormalize_105189; double length_105209; double plength_105209; double _105212; double p_105212; struct_vec_9645 vnormalize_105217; struct_vec_9645 pvnormalize_105217; int lower_105220; int plower_105220; int upper_105221; int pupper_105221; int step_105222; int pstep_105222; double occlusion_105223; double pocclusion_105223; unsigned long state_105224; unsigned long pstate_105224; double r_107461; double pr_107461; double g_107462; double pg_107462; double b_107463; double pb_107463; unsigned long state_107464; unsigned long pstate_107464; double theta_105239; double ptheta_105239; double _105258; double p_105258; double _105265; double p_105265; double z_105270; double pz_105270; double _105323; double p_105323; double length_105346; double plength_105346; double _105349; double p_105349; struct_vec_9645 vnormalize_105354; struct_vec_9645 pvnormalize_105354; struct_Isect_9647 ray_sphere_intersect_105357; struct_Isect_9647 pray_sphere_intersect_105357; double _105375; double p_105375; double length_105399; double plength_105399; double _105402; double p_105402; struct_vec_9645 vnormalize_105407; struct_vec_9645 pvnormalize_105407; struct_Isect_9647 ray_sphere_intersect_105410; struct_Isect_9647 pray_sphere_intersect_105410; double _105428; double p_105428; double length_105452; double plength_105452; double _105455; double p_105455; struct_vec_9645 vnormalize_105460; struct_vec_9645 pvnormalize_105460; struct_Isect_9647 ray_sphere_intersect_105463; struct_Isect_9647 pray_sphere_intersect_105463; double _105471; double p_105471; struct_Isect_9647 ray_plane_intersect_105489; struct_Isect_9647 pray_plane_intersect_105489; double occlusion_105495; double pocclusion_105495; double theta_105507; double ptheta_105507; double _105520; double p_105520; double _105523; double p_105523; double z_105528; double pz_105528; double _105557; double p_105557; double length_105580; double plength_105580; double _105583; double p_105583; struct_vec_9645 vnormalize_105588; struct_vec_9645 pvnormalize_105588; struct_Isect_9647 ray_sphere_intersect_105591; struct_Isect_9647 pray_sphere_intersect_105591; double _105602; double p_105602; double length_105626; double plength_105626; double _105629; double p_105629; struct_vec_9645 vnormalize_105634; struct_vec_9645 pvnormalize_105634; struct_Isect_9647 ray_sphere_intersect_105637; struct_Isect_9647 pray_sphere_intersect_105637; double _105648; double p_105648; double length_105672; double plength_105672; double _105675; double p_105675; struct_vec_9645 vnormalize_105680; struct_vec_9645 pvnormalize_105680; struct_Isect_9647 ray_sphere_intersect_105683; struct_Isect_9647 pray_sphere_intersect_105683; double _105691; double p_105691; struct_Isect_9647 ray_plane_intersect_105702; struct_Isect_9647 pray_plane_intersect_105702; double occlusion_105708; double pocclusion_105708; double theta_105720; double ptheta_105720; double _105733; double p_105733; double _105736; double p_105736; double z_105741; double pz_105741; double _105770; double p_105770; double length_105793; double plength_105793; double _105796; double p_105796; struct_vec_9645 vnormalize_105801; struct_vec_9645 pvnormalize_105801; struct_Isect_9647 ray_sphere_intersect_105804; struct_Isect_9647 pray_sphere_intersect_105804; double _105815; double p_105815; double length_105839; double plength_105839; double _105842; double p_105842; struct_vec_9645 vnormalize_105847; struct_vec_9645 pvnormalize_105847; struct_Isect_9647 ray_sphere_intersect_105850; struct_Isect_9647 pray_sphere_intersect_105850; double _105861; double p_105861; double length_105885; double plength_105885; double _105888; double p_105888; struct_vec_9645 vnormalize_105893; struct_vec_9645 pvnormalize_105893; struct_Isect_9647 ray_sphere_intersect_105896; struct_Isect_9647 pray_sphere_intersect_105896; double _105904; double p_105904; struct_Isect_9647 ray_plane_intersect_105915; struct_Isect_9647 pray_plane_intersect_105915; double occlusion_105921; double pocclusion_105921; double theta_105933; double ptheta_105933; double _105946; double p_105946; double _105949; double p_105949; double z_105954; double pz_105954; double _105983; double p_105983; double length_106006; double plength_106006; double _106009; double p_106009; struct_vec_9645 vnormalize_106014; struct_vec_9645 pvnormalize_106014; struct_Isect_9647 ray_sphere_intersect_106017; struct_Isect_9647 pray_sphere_intersect_106017; double _106028; double p_106028; double length_106052; double plength_106052; double _106055; double p_106055; struct_vec_9645 vnormalize_106060; struct_vec_9645 pvnormalize_106060; struct_Isect_9647 ray_sphere_intersect_106063; struct_Isect_9647 pray_sphere_intersect_106063; double _106074; double p_106074; double length_106098; double plength_106098; double _106101; double p_106101; struct_vec_9645 vnormalize_106106; struct_vec_9645 pvnormalize_106106; struct_Isect_9647 ray_sphere_intersect_106109; struct_Isect_9647 pray_sphere_intersect_106109; double _106117; double p_106117; struct_Isect_9647 ray_plane_intersect_106128; struct_Isect_9647 pray_plane_intersect_106128; double occlusion_106134; double pocclusion_106134; double theta_106146; double ptheta_106146; double _106159; double p_106159; double _106162; double p_106162; double z_106167; double pz_106167; double _106196; double p_106196; double length_106219; double plength_106219; double _106222; double p_106222; struct_vec_9645 vnormalize_106227; struct_vec_9645 pvnormalize_106227; struct_Isect_9647 ray_sphere_intersect_106230; struct_Isect_9647 pray_sphere_intersect_106230; double _106241; double p_106241; double length_106265; double plength_106265; double _106268; double p_106268; struct_vec_9645 vnormalize_106273; struct_vec_9645 pvnormalize_106273; struct_Isect_9647 ray_sphere_intersect_106276; struct_Isect_9647 pray_sphere_intersect_106276; double _106287; double p_106287; double length_106311; double plength_106311; double _106314; double p_106314; struct_vec_9645 vnormalize_106319; struct_vec_9645 pvnormalize_106319; struct_Isect_9647 ray_sphere_intersect_106322; struct_Isect_9647 pray_sphere_intersect_106322; double _106330; double p_106330; struct_Isect_9647 ray_plane_intersect_106341; struct_Isect_9647 pray_plane_intersect_106341; double occlusion_106347; double pocclusion_106347; double theta_106359; double ptheta_106359; double _106372; double p_106372; double _106375; double p_106375; double z_106380; double pz_106380; double _106409; double p_106409; double length_106432; double plength_106432; double _106435; double p_106435; struct_vec_9645 vnormalize_106440; struct_vec_9645 pvnormalize_106440; struct_Isect_9647 ray_sphere_intersect_106443; struct_Isect_9647 pray_sphere_intersect_106443; double _106454; double p_106454; double length_106478; double plength_106478; double _106481; double p_106481; struct_vec_9645 vnormalize_106486; struct_vec_9645 pvnormalize_106486; struct_Isect_9647 ray_sphere_intersect_106489; struct_Isect_9647 pray_sphere_intersect_106489; double _106500; double p_106500; double length_106524; double plength_106524; double _106527; double p_106527; struct_vec_9645 vnormalize_106532; struct_vec_9645 pvnormalize_106532; struct_Isect_9647 ray_sphere_intersect_106535; struct_Isect_9647 pray_sphere_intersect_106535; double _106543; double p_106543; struct_Isect_9647 ray_plane_intersect_106554; struct_Isect_9647 pray_plane_intersect_106554; double occlusion_106560; double pocclusion_106560; double theta_106572; double ptheta_106572; double _106585; double p_106585; double _106588; double p_106588; double z_106593; double pz_106593; double _106622; double p_106622; double length_106645; double plength_106645; double _106648; double p_106648; struct_vec_9645 vnormalize_106653; struct_vec_9645 pvnormalize_106653; struct_Isect_9647 ray_sphere_intersect_106656; struct_Isect_9647 pray_sphere_intersect_106656; double _106667; double p_106667; double length_106691; double plength_106691; double _106694; double p_106694; struct_vec_9645 vnormalize_106699; struct_vec_9645 pvnormalize_106699; struct_Isect_9647 ray_sphere_intersect_106702; struct_Isect_9647 pray_sphere_intersect_106702; double _106713; double p_106713; double length_106737; double plength_106737; double _106740; double p_106740; struct_vec_9645 vnormalize_106745; struct_vec_9645 pvnormalize_106745; struct_Isect_9647 ray_sphere_intersect_106748; struct_Isect_9647 pray_sphere_intersect_106748; double _106756; double p_106756; struct_Isect_9647 ray_plane_intersect_106767; struct_Isect_9647 pray_plane_intersect_106767; double occlusion_106773; double pocclusion_106773; double theta_106785; double ptheta_106785; double _106798; double p_106798; double _106801; double p_106801; double z_106806; double pz_106806; double _106835; double p_106835; double length_106858; double plength_106858; double _106861; double p_106861; struct_vec_9645 vnormalize_106866; struct_vec_9645 pvnormalize_106866; struct_Isect_9647 ray_sphere_intersect_106869; struct_Isect_9647 pray_sphere_intersect_106869; double _106880; double p_106880; double length_106904; double plength_106904; double _106907; double p_106907; struct_vec_9645 vnormalize_106912; struct_vec_9645 pvnormalize_106912; struct_Isect_9647 ray_sphere_intersect_106915; struct_Isect_9647 pray_sphere_intersect_106915; double _106926; double p_106926; double length_106950; double plength_106950; double _106953; double p_106953; struct_vec_9645 vnormalize_106958; struct_vec_9645 pvnormalize_106958; struct_Isect_9647 ray_sphere_intersect_106961; struct_Isect_9647 pray_sphere_intersect_106961; double _106969; double p_106969; struct_Isect_9647 ray_plane_intersect_106980; struct_Isect_9647 pray_plane_intersect_106980; double occlusion_106986; double pocclusion_106986; threadIdx_y_104842 = threadIdx_y(); pthreadIdx_y_104842 = threadIdx_y_104842; l104840: ; threadIdx_y_104842 = pthreadIdx_y_104842; blockDim_y_104848 = blockDim_y(); pblockDim_y_104848 = blockDim_y_104848; l104846: ; blockDim_y_104848 = pblockDim_y_104848; blockIdx_y_104854 = blockIdx_y(); pblockIdx_y_104854 = blockIdx_y_104854; l104852: ; blockIdx_y_104854 = pblockIdx_y_104854; threadIdx_x_104860 = threadIdx_x(); pthreadIdx_x_104860 = threadIdx_x_104860; l104858: ; threadIdx_x_104860 = pthreadIdx_x_104860; blockDim_x_104866 = blockDim_x(); pblockDim_x_104866 = blockDim_x_104866; l104864: ; blockDim_x_104866 = pblockDim_x_104866; blockIdx_x_104872 = blockIdx_x(); pblockIdx_x_104872 = blockIdx_x_104872; l104870: ; blockIdx_x_104872 = pblockIdx_x_104872; int _104877; _104877 = blockDim_x_104866 * blockIdx_x_104872; int _104874; _104874 = blockDim_y_104848 * blockIdx_y_104854; int _104875; _104875 = threadIdx_y_104842 + _104874; int _104878; _104878 = threadIdx_x_104860 + _104877; int _104876; _104876 = 256 * _104875; int _104879; _104879 = _104876 + _104878; unsigned long* _104880; _104880 = _71817_104835 + _104879; unsigned long _104881; _104881 = *_104880; threadIdx_x_104886 = threadIdx_x(); pthreadIdx_x_104886 = threadIdx_x_104886; l104884: ; threadIdx_x_104886 = pthreadIdx_x_104886; blockDim_x_104889 = blockDim_x(); pblockDim_x_104889 = blockDim_x_104889; l104887: ; blockDim_x_104889 = pblockDim_x_104889; blockIdx_x_104892 = blockIdx_x(); pblockIdx_x_104892 = blockIdx_x_104892; l104890: ; blockIdx_x_104892 = pblockIdx_x_104892; threadIdx_y_104895 = threadIdx_y(); pthreadIdx_y_104895 = threadIdx_y_104895; l104893: ; threadIdx_y_104895 = pthreadIdx_y_104895; blockDim_y_104898 = blockDim_y(); pblockDim_y_104898 = blockDim_y_104898; l104896: ; blockDim_y_104898 = pblockDim_y_104898; blockIdx_y_104901 = blockIdx_y(); pblockIdx_y_104901 = blockIdx_y_104901; l104899: ; blockIdx_y_104901 = pblockIdx_y_104901; int _104929; _104929 = blockDim_x_104889 * blockIdx_x_104892; unsigned long state_107698; state_107698 = _104881; int _104930; _104930 = threadIdx_x_104886 + _104929; int _104941; _104941 = blockDim_y_104898 * blockIdx_y_104901; double _104931; _104931 = (double)_104930; int _104942; _104942 = threadIdx_y_104895 + _104941; double _104943; _104943 = (double)_104942; plower_104904 = 0; pupper_104905 = 2; pstep_104906 = 1; pr_104907 = 0.000000e+00; pg_104908 = 0.000000e+00; pb_104909 = 0.000000e+00; pstate_104910 = state_107698; goto l104902; l104902: ; lower_104904 = plower_104904; upper_104905 = pupper_104905; step_104906 = pstep_104906; r_104907 = pr_104907; g_104908 = pg_104908; b_104909 = pb_104909; state_104910 = pstate_104910; bool _104911; _104911 = lower_104904 < upper_104905; if (_104911) goto l104912; else goto l107568; l107568: ; double _107571; _107571 = r_104907 / 4.000000e+00; double _107572; _107572 = 2.555000e+02 * _107571; int i_107573; i_107573 = (int)_107572; bool _107574; _107574 = i_107573 < 0; if (_107574) goto l107575; else goto l107697; l107697: ; p_107577 = i_107573; goto l107576; l107575: ; p_107577 = 0; goto l107576; l107576: ; _107577 = p_107577; bool _107579; _107579 = 255 < _107577; if (_107579) goto l107580; else goto l107696; l107696: ; p_107582 = _107577; goto l107581; l107580: ; p_107582 = 255; goto l107581; l107581: ; _107582 = p_107582; double _107583; _107583 = g_104908 / 4.000000e+00; double _107584; _107584 = 2.555000e+02 * _107583; int i_107585; i_107585 = (int)_107584; bool _107586; _107586 = i_107585 < 0; if (_107586) goto l107587; else goto l107695; l107695: ; p_107589 = i_107585; goto l107588; l107587: ; p_107589 = 0; goto l107588; l107588: ; _107589 = p_107589; bool _107590; _107590 = 255 < _107589; if (_107590) goto l107591; else goto l107694; l107694: ; p_107593 = _107589; goto l107592; l107591: ; p_107593 = 255; goto l107592; l107592: ; _107593 = p_107593; double _107594; _107594 = b_104909 / 4.000000e+00; double _107595; _107595 = 2.555000e+02 * _107594; int i_107596; i_107596 = (int)_107595; bool _107597; _107597 = i_107596 < 0; if (_107597) goto l107598; else goto l107693; l107693: ; p_107600 = i_107596; goto l107599; l107598: ; p_107600 = 0; goto l107599; l107599: ; _107600 = p_107600; bool _107601; _107601 = 255 < _107600; if (_107601) goto l107602; else goto l107692; l107692: ; p_107604 = _107600; goto l107603; l107602: ; p_107604 = 255; goto l107603; l107603: ; _107604 = p_107604; threadIdx_y_107607 = threadIdx_y(); pthreadIdx_y_107607 = threadIdx_y_107607; l107605: ; threadIdx_y_107607 = pthreadIdx_y_107607; blockDim_y_107610 = blockDim_y(); pblockDim_y_107610 = blockDim_y_107610; l107608: ; blockDim_y_107610 = pblockDim_y_107610; blockIdx_y_107613 = blockIdx_y(); pblockIdx_y_107613 = blockIdx_y_107613; l107611: ; blockIdx_y_107613 = pblockIdx_y_107613; threadIdx_x_107616 = threadIdx_x(); pthreadIdx_x_107616 = threadIdx_x_107616; l107614: ; threadIdx_x_107616 = pthreadIdx_x_107616; blockDim_x_107619 = blockDim_x(); pblockDim_x_107619 = blockDim_x_107619; l107617: ; blockDim_x_107619 = pblockDim_x_107619; blockIdx_x_107622 = blockIdx_x(); pblockIdx_x_107622 = blockIdx_x_107622; l107620: ; blockIdx_x_107622 = pblockIdx_x_107622; int _107624; _107624 = blockDim_y_107610 * blockIdx_y_107613; int _107627; _107627 = blockDim_x_107619 * blockIdx_x_107622; int _107628; _107628 = threadIdx_x_107616 + _107627; unsigned char _107632; _107632 = (unsigned char)_107582; int _107625; _107625 = threadIdx_y_107607 + _107624; int _107626; _107626 = 256 * _107625; int _107629; _107629 = _107626 + _107628; int _107630; _107630 = 3 * _107629; unsigned char* _107631; _107631 = _71818_104836 + _107630; *_107631 = _107632; threadIdx_y_107636 = threadIdx_y(); pthreadIdx_y_107636 = threadIdx_y_107636; l107634: ; threadIdx_y_107636 = pthreadIdx_y_107636; blockDim_y_107639 = blockDim_y(); pblockDim_y_107639 = blockDim_y_107639; l107637: ; blockDim_y_107639 = pblockDim_y_107639; blockIdx_y_107642 = blockIdx_y(); pblockIdx_y_107642 = blockIdx_y_107642; l107640: ; blockIdx_y_107642 = pblockIdx_y_107642; threadIdx_x_107645 = threadIdx_x(); pthreadIdx_x_107645 = threadIdx_x_107645; l107643: ; threadIdx_x_107645 = pthreadIdx_x_107645; blockDim_x_107648 = blockDim_x(); pblockDim_x_107648 = blockDim_x_107648; l107646: ; blockDim_x_107648 = pblockDim_x_107648; blockIdx_x_107651 = blockIdx_x(); pblockIdx_x_107651 = blockIdx_x_107651; l107649: ; blockIdx_x_107651 = pblockIdx_x_107651; int _107652; _107652 = blockDim_y_107639 * blockIdx_y_107642; int _107655; _107655 = blockDim_x_107648 * blockIdx_x_107651; int _107656; _107656 = threadIdx_x_107645 + _107655; unsigned char _107661; _107661 = (unsigned char)_107593; int _107653; _107653 = threadIdx_y_107636 + _107652; int _107654; _107654 = 256 * _107653; int _107657; _107657 = _107654 + _107656; int _107658; _107658 = 3 * _107657; int _107659; _107659 = 1 + _107658; unsigned char* _107660; _107660 = _71818_104836 + _107659; *_107660 = _107661; threadIdx_y_107665 = threadIdx_y(); pthreadIdx_y_107665 = threadIdx_y_107665; l107663: ; threadIdx_y_107665 = pthreadIdx_y_107665; blockDim_y_107668 = blockDim_y(); pblockDim_y_107668 = blockDim_y_107668; l107666: ; blockDim_y_107668 = pblockDim_y_107668; blockIdx_y_107671 = blockIdx_y(); pblockIdx_y_107671 = blockIdx_y_107671; l107669: ; blockIdx_y_107671 = pblockIdx_y_107671; threadIdx_x_107674 = threadIdx_x(); pthreadIdx_x_107674 = threadIdx_x_107674; l107672: ; threadIdx_x_107674 = pthreadIdx_x_107674; blockDim_x_107677 = blockDim_x(); pblockDim_x_107677 = blockDim_x_107677; l107675: ; blockDim_x_107677 = pblockDim_x_107677; blockIdx_x_107680 = blockIdx_x(); pblockIdx_x_107680 = blockIdx_x_107680; l107678: ; blockIdx_x_107680 = pblockIdx_x_107680; int _107684; _107684 = blockDim_x_107677 * blockIdx_x_107680; int _107681; _107681 = blockDim_y_107668 * blockIdx_y_107671; unsigned char _107690; _107690 = (unsigned char)_107604; int _107685; _107685 = threadIdx_x_107674 + _107684; int _107682; _107682 = threadIdx_y_107665 + _107681; int _107683; _107683 = 256 * _107682; int _107686; _107686 = _107683 + _107685; int _107687; _107687 = 3 * _107686; int _107688; _107688 = 2 + _107687; unsigned char* _107689; _107689 = _71818_104836 + _107688; *_107689 = _107690; return ; l104912: ; double _104944; _104944 = (double)lower_104904; double _104945; _104945 = _104944 / 2.000000e+00; double _104946; _104946 = _104943 + _104945; double _104947; _104947 = _104946 - 1.280000e+02; double _104948; _104948 = -0.000000e+00 - _104947; double py_104949; py_104949 = _104948 / 1.280000e+02; double _104950; _104950 = py_104949 * py_104949; plower_104915 = 0; pupper_104916 = 2; pstep_104917 = 1; pr_104918 = r_104907; pg_104919 = g_104908; pb_104920 = b_104909; pstate_104921 = state_104910; goto l104913; l104913: ; lower_104915 = plower_104915; upper_104916 = pupper_104916; step_104917 = pstep_104917; r_104918 = pr_104918; g_104919 = pg_104919; b_104920 = pb_104920; state_104921 = pstate_104921; bool _104922; _104922 = lower_104915 < upper_104916; if (_104922) goto l104923; else goto l107565; l107565: ; int _107566; _107566 = lower_104904 + step_104906; plower_104904 = _107566; pupper_104905 = upper_104905; pstep_104906 = step_104906; pr_104907 = r_104918; pg_104908 = g_104919; pb_104909 = b_104920; pstate_104910 = state_104921; goto l104902; l104923: ; double _104932; _104932 = (double)lower_104915; double _104934; _104934 = _104932 / 2.000000e+00; double _104935; _104935 = _104931 + _104934; double _104937; _104937 = _104935 - 1.280000e+02; double px_104938; px_104938 = _104937 / 1.280000e+02; double _104939; _104939 = px_104938 * px_104938; double _104951; _104951 = _104939 + _104950; double _104952; _104952 = 1.000000e+00 + _104951; length_104955 = sqrt(_104952); plength_104955 = length_104955; l104953: ; length_104955 = plength_104955; _104962 = fabs(length_104955); p_104962 = _104962; l104960: ; _104962 = p_104962; bool _104964; _104964 = 1.000000e-17 < _104962; if (_104964) goto l104965; else goto l107563; l107563: ; struct_vec_9645 _107564; _107564.e0 = px_104938; _107564.e1 = py_104949; _107564.e2 = -1.000000e+00; pvnormalize_104968 = _107564; goto l104966; l104965: ; double _107561; _107561 = -1.000000e+00 / length_104955; double _107559; _107559 = px_104938 / length_104955; double _107560; _107560 = py_104949 / length_104955; struct_vec_9645 _107562; _107562.e0 = _107559; _107562.e1 = _107560; _107562.e2 = _107561; pvnormalize_104968 = _107562; goto l104966; l104966: ; vnormalize_104968 = pvnormalize_104968; double _104970; _104970 = vnormalize_104968.e0; double _104978; _104978 = vnormalize_104968.e2; double _104973; _104973 = vnormalize_104968.e1; double _104971; _104971 = 2.000000e+00 * _104970; double _104979; _104979 = 3.500000e+00 * _104978; double _104974; _104974 = 0.000000e+00 * _104973; double _104975; _104975 = _104971 + _104974; double _104980; _104980 = _104975 + _104979; double _104981; _104981 = _104980 * _104980; double D_104983; D_104983 = _104981 - 1.600000e+01; bool _104984; _104984 = 0.000000e+00 < D_104983; if (_104984) goto l104985; else goto l107558; l107558: ; goto l107555; l104985: ; _104988 = sqrt(D_104983); p_104988 = _104988; l104986: ; _104988 = p_104988; double _104989; _104989 = -0.000000e+00 - _104980; double t_104990; t_104990 = _104989 - _104988; bool _104991; _104991 = 0.000000e+00 < t_104990; if (_104991) goto l104992; else goto l107557; l107557: ; goto l107554; l104992: ; bool _104994; _104994 = t_104990 < 1.000000e+17; if (_104994) goto l104995; else goto l107553; l107553: ; goto l107554; l107554: ; goto l107555; l107555: ; struct_Isect_9647 _107049_64; _107049_64.e0 = 1.000000e+17; // bottom: _107049_64.e1 = // bottom: struct_vec_9645 _107047_68;; // bottom: _107049_64.e2 = _107047_68; _107049_64.e3 = 0; pray_sphere_intersect_105025 = _107049_64; goto l105023; l104995: ; double _104996; _104996 = _104970 * t_104990; double _105001; _105001 = _104973 * t_104990; double _105006; _105006 = _104978 * t_104990; double _104997; _104997 = 0.000000e+00 + _104996; double _105002; _105002 = 0.000000e+00 + _105001; double _105007; _105007 = 0.000000e+00 + _105006; double _104999; _104999 = _104997 - -2.000000e+00; double _105003; _105003 = _105002 - 0.000000e+00; double _105009; _105009 = _105007 - -3.500000e+00; double _105000; _105000 = _104999 * _104999; double _105004; _105004 = _105003 * _105003; double _105010; _105010 = _105009 * _105009; double _105005; _105005 = _105000 + _105004; double _105011; _105011 = _105005 + _105010; length_105014 = sqrt(_105011); plength_105014 = length_105014; l105012: ; length_105014 = plength_105014; _105017 = fabs(length_105014); p_105017 = _105017; l105015: ; _105017 = p_105017; bool _105018; _105018 = 1.000000e-17 < _105017; if (_105018) goto l105019; else goto l107551; l107551: ; struct_vec_9645 n_107552; n_107552.e0 = _104999; n_107552.e1 = _105003; n_107552.e2 = _105009; pvnormalize_105022 = n_107552; goto l105020; l105019: ; double _107548; _107548 = _105003 / length_105014; double _107547; _107547 = _104999 / length_105014; double _107549; _107549 = _105009 / length_105014; struct_vec_9645 _107550; _107550.e0 = _107547; _107550.e1 = _107548; _107550.e2 = _107549; pvnormalize_105022 = _107550; goto l105020; l105020: ; vnormalize_105022 = pvnormalize_105022; struct_vec_9645 p_107545; p_107545.e0 = _104997; p_107545.e1 = _105002; p_107545.e2 = _105007; struct_Isect_9647 isect_107546; isect_107546.e0 = t_104990; isect_107546.e1 = p_107545; isect_107546.e2 = vnormalize_105022; isect_107546.e3 = 1; pray_sphere_intersect_105025 = isect_107546; goto l105023; l105023: ; ray_sphere_intersect_105025 = pray_sphere_intersect_105025; double _105030; _105030 = 3.000000e+00 * _104978; double _105027; _105027 = 5.000000e-01 * _104970; double _105028; _105028 = _105027 + _104974; double _105031; _105031 = _105028 + _105030; double _105032; _105032 = _105031 * _105031; double D_105034; D_105034 = _105032 - 9.000000e+00; bool _105035; _105035 = 0.000000e+00 < D_105034; if (_105035) goto l105036; else goto l107544; l107544: ; goto l107541; l105036: ; _105039 = sqrt(D_105034); p_105039 = _105039; l105037: ; _105039 = p_105039; double _105040; _105040 = -0.000000e+00 - _105031; double t_105041; t_105041 = _105040 - _105039; bool _105042; _105042 = 0.000000e+00 < t_105041; if (_105042) goto l105043; else goto l107543; l107543: ; goto l107540; l105043: ; double _105044; _105044 = ray_sphere_intersect_105025.e0; bool _105045; _105045 = t_105041 < _105044; if (_105045) goto l105046; else goto l107539; l107539: ; goto l107540; l107540: ; goto l107541; l107541: ; pray_sphere_intersect_105076 = ray_sphere_intersect_105025; goto l105074; l105046: ; double _105047; _105047 = _104970 * t_105041; double _105052; _105052 = _104973 * t_105041; double _105057; _105057 = _104978 * t_105041; double _105053; _105053 = 0.000000e+00 + _105052; double _105054; _105054 = _105053 - 0.000000e+00; double _105048; _105048 = 0.000000e+00 + _105047; double _105058; _105058 = 0.000000e+00 + _105057; double _105055; _105055 = _105054 * _105054; double _105050; _105050 = _105048 - -5.000000e-01; double _105060; _105060 = _105058 - -3.000000e+00; double _105051; _105051 = _105050 * _105050; double _105061; _105061 = _105060 * _105060; double _105056; _105056 = _105051 + _105055; double _105062; _105062 = _105056 + _105061; length_105065 = sqrt(_105062); plength_105065 = length_105065; l105063: ; length_105065 = plength_105065; _105068 = fabs(length_105065); p_105068 = _105068; l105066: ; _105068 = p_105068; bool _105069; _105069 = 1.000000e-17 < _105068; if (_105069) goto l105070; else goto l107537; l107537: ; struct_vec_9645 n_107538; n_107538.e0 = _105050; n_107538.e1 = _105054; n_107538.e2 = _105060; pvnormalize_105073 = n_107538; goto l105071; l105070: ; double _107533; _107533 = _105050 / length_105065; double _107534; _107534 = _105054 / length_105065; double _107535; _107535 = _105060 / length_105065; struct_vec_9645 _107536; _107536.e0 = _107533; _107536.e1 = _107534; _107536.e2 = _107535; pvnormalize_105073 = _107536; goto l105071; l105071: ; vnormalize_105073 = pvnormalize_105073; struct_vec_9645 p_107531; p_107531.e0 = _105048; p_107531.e1 = _105053; p_107531.e2 = _105058; struct_Isect_9647 isect_107532; isect_107532.e0 = t_105041; isect_107532.e1 = p_107531; isect_107532.e2 = vnormalize_105073; isect_107532.e3 = 1; pray_sphere_intersect_105076 = isect_107532; goto l105074; l105074: ; ray_sphere_intersect_105076 = pray_sphere_intersect_105076; double _105081; _105081 = 2.200000e+00 * _104978; double _105078; _105078 = -1.000000e+00 * _104970; double _105079; _105079 = _105078 + _104974; double _105082; _105082 = _105079 + _105081; double _105083; _105083 = _105082 * _105082; double D_105085; D_105085 = _105083 - 5.590000e+00; bool _105086; _105086 = 0.000000e+00 < D_105085; if (_105086) goto l105087; else goto l107530; l107530: ; goto l107527; l105087: ; _105090 = sqrt(D_105085); p_105090 = _105090; l105088: ; _105090 = p_105090; double _105091; _105091 = -0.000000e+00 - _105082; double t_105092; t_105092 = _105091 - _105090; bool _105093; _105093 = 0.000000e+00 < t_105092; if (_105093) goto l105094; else goto l107529; l107529: ; goto l107526; l105094: ; double _105095; _105095 = ray_sphere_intersect_105076.e0; bool _105096; _105096 = t_105092 < _105095; if (_105096) goto l105097; else goto l107525; l107525: ; goto l107526; l107526: ; goto l107527; l107527: ; pray_sphere_intersect_105126 = ray_sphere_intersect_105076; goto l105124; l105097: ; double _105107; _105107 = _104978 * t_105092; double _105098; _105098 = _104970 * t_105092; double _105108; _105108 = 0.000000e+00 + _105107; double _105102; _105102 = _104973 * t_105092; double _105099; _105099 = 0.000000e+00 + _105098; double _105110; _105110 = _105108 - -2.200000e+00; double _105103; _105103 = 0.000000e+00 + _105102; double _105100; _105100 = _105099 - 1.000000e+00; double _105111; _105111 = _105110 * _105110; double _105104; _105104 = _105103 - 0.000000e+00; double _105101; _105101 = _105100 * _105100; double _105105; _105105 = _105104 * _105104; double _105106; _105106 = _105101 + _105105; double _105112; _105112 = _105106 + _105111; length_105115 = sqrt(_105112); plength_105115 = length_105115; l105113: ; length_105115 = plength_105115; _105118 = fabs(length_105115); p_105118 = _105118; l105116: ; _105118 = p_105118; bool _105119; _105119 = 1.000000e-17 < _105118; if (_105119) goto l105120; else goto l107523; l107523: ; struct_vec_9645 n_107524; n_107524.e0 = _105100; n_107524.e1 = _105104; n_107524.e2 = _105110; pvnormalize_105123 = n_107524; goto l105121; l105120: ; double _107520; _107520 = _105104 / length_105115; double _107521; _107521 = _105110 / length_105115; double _107519; _107519 = _105100 / length_105115; struct_vec_9645 _107522; _107522.e0 = _107519; _107522.e1 = _107520; _107522.e2 = _107521; pvnormalize_105123 = _107522; goto l105121; l105121: ; vnormalize_105123 = pvnormalize_105123; struct_vec_9645 p_107517; p_107517.e0 = _105099; p_107517.e1 = _105103; p_107517.e2 = _105108; struct_Isect_9647 isect_107518; isect_107518.e0 = t_105092; isect_107518.e1 = p_107517; isect_107518.e2 = vnormalize_105123; isect_107518.e3 = 1; pray_sphere_intersect_105126 = isect_107518; goto l105124; l105124: ; ray_sphere_intersect_105126 = pray_sphere_intersect_105126; double _105128; _105128 = 1.000000e+00 * _104973; double _105127; _105127 = 0.000000e+00 * _104970; double _105130; _105130 = 0.000000e+00 * _104978; double _105129; _105129 = _105127 + _105128; double _105131; _105131 = _105129 + _105130; _105134 = fabs(_105131); p_105134 = _105134; l105132: ; _105134 = p_105134; bool _105135; _105135 = 1.000000e-17 <= _105134; if (_105135) goto l105136; else goto l107516; l107516: ; goto l107513; l105136: ; double t_105137; t_105137 = -5.000000e-01 / _105131; bool _105138; _105138 = 0.000000e+00 < t_105137; if (_105138) goto l105139; else goto l107515; l107515: ; goto l107512; l105139: ; double _105140; _105140 = ray_sphere_intersect_105126.e0; bool _105141; _105141 = t_105137 < _105140; if (_105141) goto l105142; else goto l107511; l107511: ; goto l107512; l107512: ; goto l107513; l107513: ; pray_plane_intersect_105145 = ray_sphere_intersect_105126; goto l105143; l105142: ; double _107505; _107505 = _104973 * t_105137; double _107506; _107506 = 0.000000e+00 + _107505; double _107507; _107507 = _104978 * t_105137; double _107503; _107503 = _104970 * t_105137; double _107508; _107508 = 0.000000e+00 + _107507; double _107504; _107504 = 0.000000e+00 + _107503; struct_vec_9645 p_107509; p_107509.e0 = _107504; p_107509.e1 = _107506; p_107509.e2 = _107508; struct_vec_9645 _106999_162; _106999_162.e0 = 0.000000e+00; _106999_162.e1 = 1.000000e+00; _106999_162.e2 = 0.000000e+00; struct_Isect_9647 isect_107510; isect_107510.e0 = t_105137; isect_107510.e1 = p_107509; isect_107510.e2 = _106999_162; isect_107510.e3 = 1; pray_plane_intersect_105145 = isect_107510; goto l105143; l105143: ; ray_plane_intersect_105145 = pray_plane_intersect_105145; int _105147; _105147 = ray_plane_intersect_105145.e3; bool _105149; _105149 = _105147 == 1; if (_105149) goto l105150; else goto l107502; l107502: ; pr_107461 = r_104918; pg_107462 = g_104919; pb_107463 = b_104920; pstate_107464 = state_104921; goto l107459; l105150: ; struct_vec_9645 _105151; _105151 = ray_plane_intersect_105145.e2; double _105152; _105152 = _105151.e0; double _105165; _105165 = _105151.e1; double _105163; _105163 = _105151.e2; bool _105154; _105154 = _105152 < 6.000000e-01; if (_105154) goto l105155; else goto l107501; l107501: ; goto l107486; l105155: ; bool _105157; _105157 = -6.000000e-01 < _105152; if (_105157) goto l105158; else goto l107485; l107485: ; goto l107486; l107486: ; bool _107487; _107487 = _105165 < 6.000000e-01; if (_107487) goto l107488; else goto l107500; l107500: ; goto l107492; l107488: ; bool _107489; _107489 = -6.000000e-01 < _105165; if (_107489) goto l107490; else goto l107491; l107491: ; goto l107492; l107492: ; bool _107493; _107493 = _105163 < 6.000000e-01; if (_107493) goto l107494; else goto l107499; l107499: ; goto l107498; l107494: ; bool _107495; _107495 = -6.000000e-01 < _105163; if (_107495) goto l107496; else goto l107497; l107497: ; goto l107498; l107498: ; p_105160 = 1.000000e+00; p_105161 = 0.000000e+00; p_105162 = 0.000000e+00; goto l105159; l107496: ; p_105160 = 0.000000e+00; p_105161 = 0.000000e+00; p_105162 = 1.000000e+00; goto l105159; l107490: ; p_105160 = 0.000000e+00; p_105161 = 1.000000e+00; p_105162 = 0.000000e+00; goto l105159; l105158: ; p_105160 = 1.000000e+00; p_105161 = 0.000000e+00; p_105162 = 0.000000e+00; goto l105159; l105159: ; _105160 = p_105160; _105161 = p_105161; _105162 = p_105162; double _105169; _105169 = _105162 * _105152; double _105164; _105164 = _105161 * _105163; double _105174; _105174 = _105160 * _105165; double _105170; _105170 = _105160 * _105163; double _105166; _105166 = _105162 * _105165; double _105175; _105175 = _105161 * _105152; double _105171; _105171 = _105169 - _105170; double _105167; _105167 = _105164 - _105166; double _105176; _105176 = _105174 - _105175; double _105172; _105172 = _105171 * _105171; double _105168; _105168 = _105167 * _105167; double _105177; _105177 = _105176 * _105176; double _105173; _105173 = _105168 + _105172; double _105178; _105178 = _105173 + _105177; length_105181 = sqrt(_105178); plength_105181 = length_105181; l105179: ; length_105181 = plength_105181; _105184 = fabs(length_105181); p_105184 = _105184; l105182: ; _105184 = p_105184; bool _105185; _105185 = 1.000000e-17 < _105184; if (_105185) goto l105186; else goto l107483; l107483: ; struct_vec_9645 _107484; _107484.e0 = _105167; _107484.e1 = _105171; _107484.e2 = _105176; pvnormalize_105189 = _107484; goto l105187; l105186: ; double _107481; _107481 = _105176 / length_105181; double _107479; _107479 = _105167 / length_105181; double _107480; _107480 = _105171 / length_105181; struct_vec_9645 _107482; _107482.e0 = _107479; _107482.e1 = _107480; _107482.e2 = _107481; pvnormalize_105189 = _107482; goto l105187; l105187: ; vnormalize_105189 = pvnormalize_105189; double _105190; _105190 = vnormalize_105189.e2; double _105196; _105196 = vnormalize_105189.e0; double _105192; _105192 = vnormalize_105189.e1; double _105191; _105191 = _105165 * _105190; double _105198; _105198 = _105152 * _105190; double _105203; _105203 = _105165 * _105196; double _105197; _105197 = _105163 * _105196; double _105202; _105202 = _105152 * _105192; double _105193; _105193 = _105163 * _105192; double _105194; _105194 = _105191 - _105193; double _105199; _105199 = _105197 - _105198; double _105204; _105204 = _105202 - _105203; double _105195; _105195 = _105194 * _105194; double _105200; _105200 = _105199 * _105199; double _105205; _105205 = _105204 * _105204; double _105201; _105201 = _105195 + _105200; double _105206; _105206 = _105201 + _105205; length_105209 = sqrt(_105206); plength_105209 = length_105209; l105207: ; length_105209 = plength_105209; _105212 = fabs(length_105209); p_105212 = _105212; l105210: ; _105212 = p_105212; bool _105213; _105213 = 1.000000e-17 < _105212; if (_105213) goto l105214; else goto l107477; l107477: ; struct_vec_9645 _107478; _107478.e0 = _105194; _107478.e1 = _105199; _107478.e2 = _105204; pvnormalize_105217 = _107478; goto l105215; l105214: ; double _107475; _107475 = _105204 / length_105209; double _107473; _107473 = _105194 / length_105209; double _107474; _107474 = _105199 / length_105209; struct_vec_9645 _107476; _107476.e0 = _107473; _107476.e1 = _107474; _107476.e2 = _107475; pvnormalize_105217 = _107476; goto l105215; l105215: ; vnormalize_105217 = pvnormalize_105217; double _105299; _105299 = 1.000000e-04 * _105163; struct_vec_9645 _105271; _105271 = ray_plane_intersect_105145.e1; double _105298; _105298 = _105271.e2; double _105274; _105274 = 1.000000e-04 * _105152; double _105300; _105300 = _105298 + _105299; double _105303; _105303 = vnormalize_105217.e2; double _105291; _105291 = vnormalize_105217.e1; double _105272; _105272 = _105271.e0; double _105286; _105286 = _105271.e1; double _105414; _105414 = _105300 - -2.200000e+00; double _105361; _105361 = _105300 - -3.000000e+00; double _105280; _105280 = vnormalize_105217.e0; double _105287; _105287 = 1.000000e-04 * _105165; double _105275; _105275 = _105272 + _105274; double _105477; _105477 = 0.000000e+00 * _105300; double _105301; _105301 = _105300 - -3.500000e+00; double _105288; _105288 = _105286 + _105287; double _105420; _105420 = _105414 * _105414; double _105367; _105367 = _105361 * _105361; double _105358; _105358 = _105275 - -5.000000e-01; double _105276; _105276 = _105275 - -2.000000e+00; double _105474; _105474 = 0.000000e+00 * _105275; double _105411; _105411 = _105275 - 1.000000e+00; double _105314; _105314 = _105301 * _105301; double _105475; _105475 = 1.000000e+00 * _105288; double _105289; _105289 = _105288 - 0.000000e+00; double _105365; _105365 = _105358 * _105358; double _105311; _105311 = _105276 * _105276; double _105476; _105476 = _105474 + _105475; double _105418; _105418 = _105411 * _105411; double _105312; _105312 = _105289 * _105289; double _105366; _105366 = _105365 + _105312; double _105313; _105313 = _105311 + _105312; double _105478; _105478 = _105476 + _105477; double _105419; _105419 = _105418 + _105312; double _105368; _105368 = _105366 + _105367; double _105315; _105315 = _105313 + _105314; double _105479; _105479 = 5.000000e-01 + _105478; double _105421; _105421 = _105419 + _105420; double C_105369; C_105369 = _105368 - 2.500000e-01; double C_105317; C_105317 = _105315 - 2.500000e-01; double _105480; _105480 = -0.000000e+00 - _105479; double C_105422; C_105422 = _105421 - 2.500000e-01; plower_105220 = 0; pupper_105221 = 8; pstep_105222 = 1; pocclusion_105223 = 0.000000e+00; pstate_105224 = state_104921; goto l105218; l105218: ; lower_105220 = plower_105220; upper_105221 = pupper_105221; step_105222 = pstep_105222; occlusion_105223 = pocclusion_105223; state_105224 = pstate_105224; bool _105225; _105225 = lower_105220 < upper_105221; if (_105225) goto l105226; else goto l107458; l107458: ; double _107467; _107467 = 6.400000e+01 - occlusion_105223; double _107468; _107468 = _107467 / 6.400000e+01; double _107469; _107469 = r_104918 + _107468; double _107471; _107471 = b_104920 + _107468; double _107470; _107470 = g_104919 + _107468; pr_107461 = _107469; pg_107462 = _107470; pb_107463 = _107471; pstate_107464 = state_105224; goto l107459; l107459: ; r_107461 = pr_107461; g_107462 = pg_107462; b_107463 = pb_107463; state_107464 = pstate_107464; int _107465; _107465 = lower_104915 + step_104917; plower_104915 = _107465; pupper_104916 = upper_104916; pstep_104917 = step_104917; pr_104918 = r_107461; pg_104919 = g_107462; pb_104920 = b_107463; pstate_104921 = state_107464; goto l104913; l105226: ; unsigned long hi_105232; hi_105232 = state_105224 >> 32; unsigned long lo_105229; lo_105229 = 4294967295 & state_105224; unsigned int _105230; _105230 = (unsigned int)lo_105229; unsigned int _105233; _105233 = (unsigned int)hi_105232; unsigned int _105234; _105234 = _105230 ^ _105233; double _105235; _105235 = (double)_105234; double _105236; _105236 = 2.328306e-10 * _105235; theta_105239 = sqrt(_105236); ptheta_105239 = theta_105239; l105237: ; theta_105239 = ptheta_105239; unsigned long _105246; _105246 = 4294883355 * lo_105229; unsigned long _105247; _105247 = _105246 + hi_105232; unsigned long lo_105248; lo_105248 = 4294967295 & _105247; unsigned long hi_105250; hi_105250 = _105247 >> 32; unsigned int _105249; _105249 = (unsigned int)lo_105248; unsigned int _105251; _105251 = (unsigned int)hi_105250; unsigned int _105252; _105252 = _105249 ^ _105251; double _105253; _105253 = (double)_105252; double _105254; _105254 = 2.328306e-10 * _105253; double phi_105255; phi_105255 = 6.283185e+00 * _105254; _105258 = cos(phi_105255); p_105258 = _105258; l105256: ; _105258 = p_105258; _105265 = sin(phi_105255); p_105265 = _105265; l105263: ; _105265 = p_105265; double _105266; _105266 = theta_105239 * theta_105239; double _105267; _105267 = 1.000000e+00 - _105266; z_105270 = sqrt(_105267); pz_105270 = z_105270; l105268: ; z_105270 = pz_105270; double _105294; _105294 = z_105270 * _105165; double _105283; _105283 = z_105270 * _105152; double y_105279; y_105279 = _105265 * theta_105239; double _105292; _105292 = y_105279 * _105291; double _105281; _105281 = y_105279 * _105280; double x_105277; x_105277 = _105258 * theta_105239; double _105306; _105306 = z_105270 * _105163; double _105304; _105304 = y_105279 * _105303; double _105278; _105278 = x_105277 * _105196; double _105290; _105290 = x_105277 * _105192; double _105293; _105293 = _105290 + _105292; double _105282; _105282 = _105278 + _105281; double _105302; _105302 = x_105277 * _105190; double _105305; _105305 = _105302 + _105304; double ry_105295; ry_105295 = _105293 + _105294; double rx_105284; rx_105284 = _105282 + _105283; double rz_105307; rz_105307 = _105305 + _105306; double _105296; _105296 = _105289 * ry_105295; double _105285; _105285 = _105276 * rx_105284; double _105308; _105308 = _105301 * rz_105307; double _105297; _105297 = _105285 + _105296; double _105309; _105309 = _105297 + _105308; double _105310; _105310 = _105309 * _105309; double D_105318; D_105318 = _105310 - C_105317; bool _105319; _105319 = 0.000000e+00 < D_105318; if (_105319) goto l105320; else goto l107457; l107457: ; goto l107454; l105320: ; _105323 = sqrt(D_105318); p_105323 = _105323; l105321: ; _105323 = p_105323; double _105324; _105324 = -0.000000e+00 - _105309; double t_105325; t_105325 = _105324 - _105323; bool _105326; _105326 = 0.000000e+00 < t_105325; if (_105326) goto l105327; else goto l107456; l107456: ; goto l107453; l105327: ; bool _105328; _105328 = t_105325 < 1.000000e+17; if (_105328) goto l105329; else goto l107452; l107452: ; goto l107453; l107453: ; goto l107454; l107454: ; struct_Isect_9647 _107049_259; _107049_259.e0 = 1.000000e+17; // bottom: _107049_259.e1 = // bottom: struct_vec_9645 _107047_263;; // bottom: _107049_259.e2 = _107047_263; _107049_259.e3 = 0; pray_sphere_intersect_105357 = _107049_259; goto l105355; l105329: ; double _105334; _105334 = ry_105295 * t_105325; double _105339; _105339 = rz_105307 * t_105325; double _105330; _105330 = rx_105284 * t_105325; double _105335; _105335 = _105288 + _105334; double _105340; _105340 = _105300 + _105339; double _105331; _105331 = _105275 + _105330; double _105336; _105336 = _105335 - 0.000000e+00; double _105341; _105341 = _105340 - -3.500000e+00; double _105332; _105332 = _105331 - -2.000000e+00; double _105337; _105337 = _105336 * _105336; double _105342; _105342 = _105341 * _105341; double _105333; _105333 = _105332 * _105332; double _105338; _105338 = _105333 + _105337; double _105343; _105343 = _105338 + _105342; length_105346 = sqrt(_105343); plength_105346 = length_105346; l105344: ; length_105346 = plength_105346; _105349 = fabs(length_105346); p_105349 = _105349; l105347: ; _105349 = p_105349; bool _105350; _105350 = 1.000000e-17 < _105349; if (_105350) goto l105351; else goto l107450; l107450: ; struct_vec_9645 n_107451; n_107451.e0 = _105332; n_107451.e1 = _105336; n_107451.e2 = _105341; pvnormalize_105354 = n_107451; goto l105352; l105351: ; double _107448; _107448 = _105341 / length_105346; double _107447; _107447 = _105336 / length_105346; double _107446; _107446 = _105332 / length_105346; struct_vec_9645 _107449; _107449.e0 = _107446; _107449.e1 = _107447; _107449.e2 = _107448; pvnormalize_105354 = _107449; goto l105352; l105352: ; vnormalize_105354 = pvnormalize_105354; struct_vec_9645 p_107444; p_107444.e0 = _105331; p_107444.e1 = _105335; p_107444.e2 = _105340; struct_Isect_9647 isect_107445; isect_107445.e0 = t_105325; isect_107445.e1 = p_107444; isect_107445.e2 = vnormalize_105354; isect_107445.e3 = 1; pray_sphere_intersect_105357 = isect_107445; goto l105355; l105355: ; ray_sphere_intersect_105357 = pray_sphere_intersect_105357; double _105359; _105359 = _105358 * rx_105284; double _105360; _105360 = _105359 + _105296; double _105362; _105362 = _105361 * rz_105307; double _105363; _105363 = _105360 + _105362; double _105364; _105364 = _105363 * _105363; double D_105370; D_105370 = _105364 - C_105369; bool _105371; _105371 = 0.000000e+00 < D_105370; if (_105371) goto l105372; else goto l107443; l107443: ; goto l107440; l105372: ; _105375 = sqrt(D_105370); p_105375 = _105375; l105373: ; _105375 = p_105375; double _105376; _105376 = -0.000000e+00 - _105363; double t_105377; t_105377 = _105376 - _105375; bool _105378; _105378 = 0.000000e+00 < t_105377; if (_105378) goto l105379; else goto l107442; l107442: ; goto l107439; l105379: ; double _105380; _105380 = ray_sphere_intersect_105357.e0; bool _105381; _105381 = t_105377 < _105380; if (_105381) goto l105382; else goto l107438; l107438: ; goto l107439; l107439: ; goto l107440; l107440: ; pray_sphere_intersect_105410 = ray_sphere_intersect_105357; goto l105408; l105382: ; double _105383; _105383 = rx_105284 * t_105377; double _105387; _105387 = ry_105295 * t_105377; double _105384; _105384 = _105275 + _105383; double _105392; _105392 = rz_105307 * t_105377; double _105388; _105388 = _105288 + _105387; double _105385; _105385 = _105384 - -5.000000e-01; double _105393; _105393 = _105300 + _105392; double _105389; _105389 = _105388 - 0.000000e+00; double _105386; _105386 = _105385 * _105385; double _105394; _105394 = _105393 - -3.000000e+00; double _105390; _105390 = _105389 * _105389; double _105391; _105391 = _105386 + _105390; double _105395; _105395 = _105394 * _105394; double _105396; _105396 = _105391 + _105395; length_105399 = sqrt(_105396); plength_105399 = length_105399; l105397: ; length_105399 = plength_105399; _105402 = fabs(length_105399); p_105402 = _105402; l105400: ; _105402 = p_105402; bool _105403; _105403 = 1.000000e-17 < _105402; if (_105403) goto l105404; else goto l107436; l107436: ; struct_vec_9645 n_107437; n_107437.e0 = _105385; n_107437.e1 = _105389; n_107437.e2 = _105394; pvnormalize_105407 = n_107437; goto l105405; l105404: ; double _107434; _107434 = _105394 / length_105399; double _107433; _107433 = _105389 / length_105399; double _107432; _107432 = _105385 / length_105399; struct_vec_9645 _107435; _107435.e0 = _107432; _107435.e1 = _107433; _107435.e2 = _107434; pvnormalize_105407 = _107435; goto l105405; l105405: ; vnormalize_105407 = pvnormalize_105407; struct_vec_9645 p_107430; p_107430.e0 = _105384; p_107430.e1 = _105388; p_107430.e2 = _105393; struct_Isect_9647 isect_107431; isect_107431.e0 = t_105377; isect_107431.e1 = p_107430; isect_107431.e2 = vnormalize_105407; isect_107431.e3 = 1; pray_sphere_intersect_105410 = isect_107431; goto l105408; l105408: ; ray_sphere_intersect_105410 = pray_sphere_intersect_105410; double _105412; _105412 = _105411 * rx_105284; double _105415; _105415 = _105414 * rz_105307; double _105413; _105413 = _105412 + _105296; double _105416; _105416 = _105413 + _105415; double _105417; _105417 = _105416 * _105416; double D_105423; D_105423 = _105417 - C_105422; bool _105424; _105424 = 0.000000e+00 < D_105423; if (_105424) goto l105425; else goto l107429; l107429: ; goto l107426; l105425: ; _105428 = sqrt(D_105423); p_105428 = _105428; l105426: ; _105428 = p_105428; double _105429; _105429 = -0.000000e+00 - _105416; double t_105430; t_105430 = _105429 - _105428; bool _105431; _105431 = 0.000000e+00 < t_105430; if (_105431) goto l105432; else goto l107428; l107428: ; goto l107425; l105432: ; double _105433; _105433 = ray_sphere_intersect_105410.e0; bool _105434; _105434 = t_105430 < _105433; if (_105434) goto l105435; else goto l107424; l107424: ; goto l107425; l107425: ; goto l107426; l107426: ; pray_sphere_intersect_105463 = ray_sphere_intersect_105410; goto l105461; l105435: ; double _105436; _105436 = rx_105284 * t_105430; double _105445; _105445 = rz_105307 * t_105430; double _105440; _105440 = ry_105295 * t_105430; double _105437; _105437 = _105275 + _105436; double _105438; _105438 = _105437 - 1.000000e+00; double _105446; _105446 = _105300 + _105445; double _105441; _105441 = _105288 + _105440; double _105439; _105439 = _105438 * _105438; double _105447; _105447 = _105446 - -2.200000e+00; double _105442; _105442 = _105441 - 0.000000e+00; double _105448; _105448 = _105447 * _105447; double _105443; _105443 = _105442 * _105442; double _105444; _105444 = _105439 + _105443; double _105449; _105449 = _105444 + _105448; length_105452 = sqrt(_105449); plength_105452 = length_105452; l105450: ; length_105452 = plength_105452; _105455 = fabs(length_105452); p_105455 = _105455; l105453: ; _105455 = p_105455; bool _105456; _105456 = 1.000000e-17 < _105455; if (_105456) goto l105457; else goto l107422; l107422: ; struct_vec_9645 n_107423; n_107423.e0 = _105438; n_107423.e1 = _105442; n_107423.e2 = _105447; pvnormalize_105460 = n_107423; goto l105458; l105457: ; double _107418; _107418 = _105438 / length_105452; double _107419; _107419 = _105442 / length_105452; double _107420; _107420 = _105447 / length_105452; struct_vec_9645 _107421; _107421.e0 = _107418; _107421.e1 = _107419; _107421.e2 = _107420; pvnormalize_105460 = _107421; goto l105458; l105458: ; vnormalize_105460 = pvnormalize_105460; struct_vec_9645 p_107416; p_107416.e0 = _105437; p_107416.e1 = _105441; p_107416.e2 = _105446; struct_Isect_9647 isect_107417; isect_107417.e0 = t_105430; isect_107417.e1 = p_107416; isect_107417.e2 = vnormalize_105460; isect_107417.e3 = 1; pray_sphere_intersect_105463 = isect_107417; goto l105461; l105461: ; ray_sphere_intersect_105463 = pray_sphere_intersect_105463; double _105464; _105464 = 0.000000e+00 * rx_105284; double _105465; _105465 = 1.000000e+00 * ry_105295; double _105467; _105467 = 0.000000e+00 * rz_105307; double _105466; _105466 = _105464 + _105465; double _105468; _105468 = _105466 + _105467; _105471 = fabs(_105468); p_105471 = _105471; l105469: ; _105471 = p_105471; bool _105472; _105472 = 1.000000e-17 <= _105471; if (_105472) goto l105473; else goto l107415; l107415: ; goto l107412; l105473: ; double t_105481; t_105481 = _105480 / _105468; bool _105482; _105482 = 0.000000e+00 < t_105481; if (_105482) goto l105483; else goto l107414; l107414: ; goto l107411; l105483: ; double _105484; _105484 = ray_sphere_intersect_105463.e0; bool _105485; _105485 = t_105481 < _105484; if (_105485) goto l105486; else goto l107410; l107410: ; goto l107411; l107411: ; goto l107412; l107412: ; pray_plane_intersect_105489 = ray_sphere_intersect_105463; goto l105487; l105486: ; double _107406; _107406 = rz_105307 * t_105481; double _107407; _107407 = _105300 + _107406; double _107402; _107402 = rx_105284 * t_105481; double _107403; _107403 = _105275 + _107402; double _107404; _107404 = ry_105295 * t_105481; double _107405; _107405 = _105288 + _107404; struct_vec_9645 p_107408; p_107408.e0 = _107403; p_107408.e1 = _107405; p_107408.e2 = _107407; struct_vec_9645 _106999_338; _106999_338.e0 = 0.000000e+00; _106999_338.e1 = 1.000000e+00; _106999_338.e2 = 0.000000e+00; struct_Isect_9647 isect_107409; isect_107409.e0 = t_105481; isect_107409.e1 = p_107408; isect_107409.e2 = _106999_338; isect_107409.e3 = 1; pray_plane_intersect_105489 = isect_107409; goto l105487; l105487: ; ray_plane_intersect_105489 = pray_plane_intersect_105489; int _105490; _105490 = ray_plane_intersect_105489.e3; bool _105491; _105491 = _105490 == 1; if (_105491) goto l105492; else goto l107401; l107401: ; pocclusion_105495 = occlusion_105223; goto l105493; l105492: ; double _107400; _107400 = 1.000000e+00 + occlusion_105223; pocclusion_105495 = _107400; goto l105493; l105493: ; occlusion_105495 = pocclusion_105495; unsigned long _105496; _105496 = 4294883355 * lo_105248; unsigned long _105497; _105497 = _105496 + hi_105250; unsigned long lo_105498; lo_105498 = 4294967295 & _105497; unsigned long hi_105500; hi_105500 = _105497 >> 32; unsigned int _105499; _105499 = (unsigned int)lo_105498; unsigned int _105501; _105501 = (unsigned int)hi_105500; unsigned int _105502; _105502 = _105499 ^ _105501; double _105503; _105503 = (double)_105502; double _105504; _105504 = 2.328306e-10 * _105503; theta_105507 = sqrt(_105504); ptheta_105507 = theta_105507; l105505: ; theta_105507 = ptheta_105507; unsigned long _105508; _105508 = 4294883355 * lo_105498; unsigned long _105509; _105509 = _105508 + hi_105500; unsigned long lo_105510; lo_105510 = 4294967295 & _105509; unsigned long hi_105512; hi_105512 = _105509 >> 32; unsigned int _105511; _105511 = (unsigned int)lo_105510; unsigned int _105513; _105513 = (unsigned int)hi_105512; unsigned int _105514; _105514 = _105511 ^ _105513; double _105515; _105515 = (double)_105514; double _105516; _105516 = 2.328306e-10 * _105515; double phi_105517; phi_105517 = 6.283185e+00 * _105516; _105520 = cos(phi_105517); p_105520 = _105520; l105518: ; _105520 = p_105520; _105523 = sin(phi_105517); p_105523 = _105523; l105521: ; _105523 = p_105523; double _105524; _105524 = theta_105507 * theta_105507; double _105525; _105525 = 1.000000e+00 - _105524; z_105528 = sqrt(_105525); pz_105528 = z_105528; l105526: ; z_105528 = pz_105528; double _105540; _105540 = z_105528 * _105165; double _105547; _105547 = z_105528 * _105163; double x_105529; x_105529 = _105520 * theta_105507; double y_105531; y_105531 = _105523 * theta_105507; double _105530; _105530 = x_105529 * _105196; double _105537; _105537 = x_105529 * _105192; double _105538; _105538 = y_105531 * _105291; double _105534; _105534 = z_105528 * _105152; double _105532; _105532 = y_105531 * _105280; double _105544; _105544 = x_105529 * _105190; double _105545; _105545 = y_105531 * _105303; double _105533; _105533 = _105530 + _105532; double _105539; _105539 = _105537 + _105538; double rx_105535; rx_105535 = _105533 + _105534; double _105546; _105546 = _105544 + _105545; double ry_105541; ry_105541 = _105539 + _105540; double _105536; _105536 = _105276 * rx_105535; double rz_105548; rz_105548 = _105546 + _105547; double _105542; _105542 = _105289 * ry_105541; double _105543; _105543 = _105536 + _105542; double _105549; _105549 = _105301 * rz_105548; double _105550; _105550 = _105543 + _105549; double _105551; _105551 = _105550 * _105550; double D_105552; D_105552 = _105551 - C_105317; bool _105553; _105553 = 0.000000e+00 < D_105552; if (_105553) goto l105554; else goto l107399; l107399: ; goto l107396; l105554: ; _105557 = sqrt(D_105552); p_105557 = _105557; l105555: ; _105557 = p_105557; double _105558; _105558 = -0.000000e+00 - _105550; double t_105559; t_105559 = _105558 - _105557; bool _105560; _105560 = 0.000000e+00 < t_105559; if (_105560) goto l105561; else goto l107398; l107398: ; goto l107395; l105561: ; bool _105562; _105562 = t_105559 < 1.000000e+17; if (_105562) goto l105563; else goto l107394; l107394: ; goto l107395; l107395: ; goto l107396; l107396: ; struct_Isect_9647 _107049_367; _107049_367.e0 = 1.000000e+17; // bottom: _107049_367.e1 = // bottom: struct_vec_9645 _107047_371;; // bottom: _107049_367.e2 = _107047_371; _107049_367.e3 = 0; pray_sphere_intersect_105591 = _107049_367; goto l105589; l105563: ; double _105573; _105573 = rz_105548 * t_105559; double _105564; _105564 = rx_105535 * t_105559; double _105568; _105568 = ry_105541 * t_105559; double _105569; _105569 = _105288 + _105568; double _105574; _105574 = _105300 + _105573; double _105565; _105565 = _105275 + _105564; double _105570; _105570 = _105569 - 0.000000e+00; double _105575; _105575 = _105574 - -3.500000e+00; double _105566; _105566 = _105565 - -2.000000e+00; double _105571; _105571 = _105570 * _105570; double _105576; _105576 = _105575 * _105575; double _105567; _105567 = _105566 * _105566; double _105572; _105572 = _105567 + _105571; double _105577; _105577 = _105572 + _105576; length_105580 = sqrt(_105577); plength_105580 = length_105580; l105578: ; length_105580 = plength_105580; _105583 = fabs(length_105580); p_105583 = _105583; l105581: ; _105583 = p_105583; bool _105584; _105584 = 1.000000e-17 < _105583; if (_105584) goto l105585; else goto l107392; l107392: ; struct_vec_9645 n_107393; n_107393.e0 = _105566; n_107393.e1 = _105570; n_107393.e2 = _105575; pvnormalize_105588 = n_107393; goto l105586; l105585: ; double _107389; _107389 = _105570 / length_105580; double _107388; _107388 = _105566 / length_105580; double _107390; _107390 = _105575 / length_105580; struct_vec_9645 _107391; _107391.e0 = _107388; _107391.e1 = _107389; _107391.e2 = _107390; pvnormalize_105588 = _107391; goto l105586; l105586: ; vnormalize_105588 = pvnormalize_105588; struct_vec_9645 p_107386; p_107386.e0 = _105565; p_107386.e1 = _105569; p_107386.e2 = _105574; struct_Isect_9647 isect_107387; isect_107387.e0 = t_105559; isect_107387.e1 = p_107386; isect_107387.e2 = vnormalize_105588; isect_107387.e3 = 1; pray_sphere_intersect_105591 = isect_107387; goto l105589; l105589: ; ray_sphere_intersect_105591 = pray_sphere_intersect_105591; double _105592; _105592 = _105358 * rx_105535; double _105594; _105594 = _105361 * rz_105548; double _105593; _105593 = _105592 + _105542; double _105595; _105595 = _105593 + _105594; double _105596; _105596 = _105595 * _105595; double D_105597; D_105597 = _105596 - C_105369; bool _105598; _105598 = 0.000000e+00 < D_105597; if (_105598) goto l105599; else goto l107385; l107385: ; goto l107382; l105599: ; _105602 = sqrt(D_105597); p_105602 = _105602; l105600: ; _105602 = p_105602; double _105603; _105603 = -0.000000e+00 - _105595; double t_105604; t_105604 = _105603 - _105602; bool _105605; _105605 = 0.000000e+00 < t_105604; if (_105605) goto l105606; else goto l107384; l107384: ; goto l107381; l105606: ; double _105607; _105607 = ray_sphere_intersect_105591.e0; bool _105608; _105608 = t_105604 < _105607; if (_105608) goto l105609; else goto l107380; l107380: ; goto l107381; l107381: ; goto l107382; l107382: ; pray_sphere_intersect_105637 = ray_sphere_intersect_105591; goto l105635; l105609: ; double _105614; _105614 = ry_105541 * t_105604; double _105619; _105619 = rz_105548 * t_105604; double _105615; _105615 = _105288 + _105614; double _105610; _105610 = rx_105535 * t_105604; double _105611; _105611 = _105275 + _105610; double _105620; _105620 = _105300 + _105619; double _105616; _105616 = _105615 - 0.000000e+00; double _105612; _105612 = _105611 - -5.000000e-01; double _105621; _105621 = _105620 - -3.000000e+00; double _105617; _105617 = _105616 * _105616; double _105613; _105613 = _105612 * _105612; double _105622; _105622 = _105621 * _105621; double _105618; _105618 = _105613 + _105617; double _105623; _105623 = _105618 + _105622; length_105626 = sqrt(_105623); plength_105626 = length_105626; l105624: ; length_105626 = plength_105626; _105629 = fabs(length_105626); p_105629 = _105629; l105627: ; _105629 = p_105629; bool _105630; _105630 = 1.000000e-17 < _105629; if (_105630) goto l105631; else goto l107378; l107378: ; struct_vec_9645 n_107379; n_107379.e0 = _105612; n_107379.e1 = _105616; n_107379.e2 = _105621; pvnormalize_105634 = n_107379; goto l105632; l105631: ; double _107375; _107375 = _105616 / length_105626; double _107374; _107374 = _105612 / length_105626; double _107376; _107376 = _105621 / length_105626; struct_vec_9645 _107377; _107377.e0 = _107374; _107377.e1 = _107375; _107377.e2 = _107376; pvnormalize_105634 = _107377; goto l105632; l105632: ; vnormalize_105634 = pvnormalize_105634; struct_vec_9645 p_107372; p_107372.e0 = _105611; p_107372.e1 = _105615; p_107372.e2 = _105620; struct_Isect_9647 isect_107373; isect_107373.e0 = t_105604; isect_107373.e1 = p_107372; isect_107373.e2 = vnormalize_105634; isect_107373.e3 = 1; pray_sphere_intersect_105637 = isect_107373; goto l105635; l105635: ; ray_sphere_intersect_105637 = pray_sphere_intersect_105637; double _105638; _105638 = _105411 * rx_105535; double _105640; _105640 = _105414 * rz_105548; double _105639; _105639 = _105638 + _105542; double _105641; _105641 = _105639 + _105640; double _105642; _105642 = _105641 * _105641; double D_105643; D_105643 = _105642 - C_105422; bool _105644; _105644 = 0.000000e+00 < D_105643; if (_105644) goto l105645; else goto l107371; l107371: ; goto l107368; l105645: ; _105648 = sqrt(D_105643); p_105648 = _105648; l105646: ; _105648 = p_105648; double _105649; _105649 = -0.000000e+00 - _105641; double t_105650; t_105650 = _105649 - _105648; bool _105651; _105651 = 0.000000e+00 < t_105650; if (_105651) goto l105652; else goto l107370; l107370: ; goto l107367; l105652: ; double _105653; _105653 = ray_sphere_intersect_105637.e0; bool _105654; _105654 = t_105650 < _105653; if (_105654) goto l105655; else goto l107366; l107366: ; goto l107367; l107367: ; goto l107368; l107368: ; pray_sphere_intersect_105683 = ray_sphere_intersect_105637; goto l105681; l105655: ; double _105660; _105660 = ry_105541 * t_105650; double _105661; _105661 = _105288 + _105660; double _105665; _105665 = rz_105548 * t_105650; double _105656; _105656 = rx_105535 * t_105650; double _105657; _105657 = _105275 + _105656; double _105662; _105662 = _105661 - 0.000000e+00; double _105666; _105666 = _105300 + _105665; double _105658; _105658 = _105657 - 1.000000e+00; double _105663; _105663 = _105662 * _105662; double _105667; _105667 = _105666 - -2.200000e+00; double _105659; _105659 = _105658 * _105658; double _105664; _105664 = _105659 + _105663; double _105668; _105668 = _105667 * _105667; double _105669; _105669 = _105664 + _105668; length_105672 = sqrt(_105669); plength_105672 = length_105672; l105670: ; length_105672 = plength_105672; _105675 = fabs(length_105672); p_105675 = _105675; l105673: ; _105675 = p_105675; bool _105676; _105676 = 1.000000e-17 < _105675; if (_105676) goto l105677; else goto l107364; l107364: ; struct_vec_9645 n_107365; n_107365.e0 = _105658; n_107365.e1 = _105662; n_107365.e2 = _105667; pvnormalize_105680 = n_107365; goto l105678; l105677: ; double _107360; _107360 = _105658 / length_105672; double _107362; _107362 = _105667 / length_105672; double _107361; _107361 = _105662 / length_105672; struct_vec_9645 _107363; _107363.e0 = _107360; _107363.e1 = _107361; _107363.e2 = _107362; pvnormalize_105680 = _107363; goto l105678; l105678: ; vnormalize_105680 = pvnormalize_105680; struct_vec_9645 p_107358; p_107358.e0 = _105657; p_107358.e1 = _105661; p_107358.e2 = _105666; struct_Isect_9647 isect_107359; isect_107359.e0 = t_105650; isect_107359.e1 = p_107358; isect_107359.e2 = vnormalize_105680; isect_107359.e3 = 1; pray_sphere_intersect_105683 = isect_107359; goto l105681; l105681: ; ray_sphere_intersect_105683 = pray_sphere_intersect_105683; double _105687; _105687 = 0.000000e+00 * rz_105548; double _105684; _105684 = 0.000000e+00 * rx_105535; double _105685; _105685 = 1.000000e+00 * ry_105541; double _105686; _105686 = _105684 + _105685; double _105688; _105688 = _105686 + _105687; _105691 = fabs(_105688); p_105691 = _105691; l105689: ; _105691 = p_105691; bool _105692; _105692 = 1.000000e-17 <= _105691; if (_105692) goto l105693; else goto l107357; l107357: ; goto l107354; l105693: ; double t_105694; t_105694 = _105480 / _105688; bool _105695; _105695 = 0.000000e+00 < t_105694; if (_105695) goto l105696; else goto l107356; l107356: ; goto l107353; l105696: ; double _105697; _105697 = ray_sphere_intersect_105683.e0; bool _105698; _105698 = t_105694 < _105697; if (_105698) goto l105699; else goto l107352; l107352: ; goto l107353; l107353: ; goto l107354; l107354: ; pray_plane_intersect_105702 = ray_sphere_intersect_105683; goto l105700; l105699: ; double _107348; _107348 = rz_105548 * t_105694; double _107349; _107349 = _105300 + _107348; double _107346; _107346 = ry_105541 * t_105694; double _107344; _107344 = rx_105535 * t_105694; double _107345; _107345 = _105275 + _107344; double _107347; _107347 = _105288 + _107346; struct_vec_9645 p_107350; p_107350.e0 = _107345; p_107350.e1 = _107347; p_107350.e2 = _107349; struct_vec_9645 _106999_446; _106999_446.e0 = 0.000000e+00; _106999_446.e1 = 1.000000e+00; _106999_446.e2 = 0.000000e+00; struct_Isect_9647 isect_107351; isect_107351.e0 = t_105694; isect_107351.e1 = p_107350; isect_107351.e2 = _106999_446; isect_107351.e3 = 1; pray_plane_intersect_105702 = isect_107351; goto l105700; l105700: ; ray_plane_intersect_105702 = pray_plane_intersect_105702; int _105703; _105703 = ray_plane_intersect_105702.e3; bool _105704; _105704 = _105703 == 1; if (_105704) goto l105705; else goto l107343; l107343: ; pocclusion_105708 = occlusion_105495; goto l105706; l105705: ; double _107342; _107342 = 1.000000e+00 + occlusion_105495; pocclusion_105708 = _107342; goto l105706; l105706: ; occlusion_105708 = pocclusion_105708; unsigned long _105709; _105709 = 4294883355 * lo_105510; unsigned long _105710; _105710 = _105709 + hi_105512; unsigned long lo_105711; lo_105711 = 4294967295 & _105710; unsigned long hi_105713; hi_105713 = _105710 >> 32; unsigned int _105712; _105712 = (unsigned int)lo_105711; unsigned int _105714; _105714 = (unsigned int)hi_105713; unsigned int _105715; _105715 = _105712 ^ _105714; double _105716; _105716 = (double)_105715; double _105717; _105717 = 2.328306e-10 * _105716; theta_105720 = sqrt(_105717); ptheta_105720 = theta_105720; l105718: ; theta_105720 = ptheta_105720; unsigned long _105721; _105721 = 4294883355 * lo_105711; unsigned long _105722; _105722 = _105721 + hi_105713; unsigned long lo_105723; lo_105723 = 4294967295 & _105722; unsigned long hi_105725; hi_105725 = _105722 >> 32; unsigned int _105724; _105724 = (unsigned int)lo_105723; unsigned int _105726; _105726 = (unsigned int)hi_105725; unsigned int _105727; _105727 = _105724 ^ _105726; double _105728; _105728 = (double)_105727; double _105729; _105729 = 2.328306e-10 * _105728; double phi_105730; phi_105730 = 6.283185e+00 * _105729; _105733 = cos(phi_105730); p_105733 = _105733; l105731: ; _105733 = p_105733; _105736 = sin(phi_105730); p_105736 = _105736; l105734: ; _105736 = p_105736; double _105737; _105737 = theta_105720 * theta_105720; double _105738; _105738 = 1.000000e+00 - _105737; z_105741 = sqrt(_105738); pz_105741 = z_105741; l105739: ; z_105741 = pz_105741; double _105760; _105760 = z_105741 * _105163; double _105747; _105747 = z_105741 * _105152; double _105753; _105753 = z_105741 * _105165; double x_105742; x_105742 = _105733 * theta_105720; double y_105744; y_105744 = _105736 * theta_105720; double _105757; _105757 = x_105742 * _105190; double _105743; _105743 = x_105742 * _105196; double _105750; _105750 = x_105742 * _105192; double _105758; _105758 = y_105744 * _105303; double _105745; _105745 = y_105744 * _105280; double _105751; _105751 = y_105744 * _105291; double _105759; _105759 = _105757 + _105758; double _105746; _105746 = _105743 + _105745; double _105752; _105752 = _105750 + _105751; double rz_105761; rz_105761 = _105759 + _105760; double rx_105748; rx_105748 = _105746 + _105747; double ry_105754; ry_105754 = _105752 + _105753; double _105762; _105762 = _105301 * rz_105761; double _105749; _105749 = _105276 * rx_105748; double _105755; _105755 = _105289 * ry_105754; double _105756; _105756 = _105749 + _105755; double _105763; _105763 = _105756 + _105762; double _105764; _105764 = _105763 * _105763; double D_105765; D_105765 = _105764 - C_105317; bool _105766; _105766 = 0.000000e+00 < D_105765; if (_105766) goto l105767; else goto l107341; l107341: ; goto l107338; l105767: ; _105770 = sqrt(D_105765); p_105770 = _105770; l105768: ; _105770 = p_105770; double _105771; _105771 = -0.000000e+00 - _105763; double t_105772; t_105772 = _105771 - _105770; bool _105773; _105773 = 0.000000e+00 < t_105772; if (_105773) goto l105774; else goto l107340; l107340: ; goto l107337; l105774: ; bool _105775; _105775 = t_105772 < 1.000000e+17; if (_105775) goto l105776; else goto l107336; l107336: ; goto l107337; l107337: ; goto l107338; l107338: ; struct_Isect_9647 _107049_475; _107049_475.e0 = 1.000000e+17; // bottom: _107049_475.e1 = // bottom: struct_vec_9645 _107047_479;; // bottom: _107049_475.e2 = _107047_479; _107049_475.e3 = 0; pray_sphere_intersect_105804 = _107049_475; goto l105802; l105776: ; double _105781; _105781 = ry_105754 * t_105772; double _105786; _105786 = rz_105761 * t_105772; double _105777; _105777 = rx_105748 * t_105772; double _105782; _105782 = _105288 + _105781; double _105787; _105787 = _105300 + _105786; double _105788; _105788 = _105787 - -3.500000e+00; double _105778; _105778 = _105275 + _105777; double _105783; _105783 = _105782 - 0.000000e+00; double _105789; _105789 = _105788 * _105788; double _105779; _105779 = _105778 - -2.000000e+00; double _105784; _105784 = _105783 * _105783; double _105780; _105780 = _105779 * _105779; double _105785; _105785 = _105780 + _105784; double _105790; _105790 = _105785 + _105789; length_105793 = sqrt(_105790); plength_105793 = length_105793; l105791: ; length_105793 = plength_105793; _105796 = fabs(length_105793); p_105796 = _105796; l105794: ; _105796 = p_105796; bool _105797; _105797 = 1.000000e-17 < _105796; if (_105797) goto l105798; else goto l107334; l107334: ; struct_vec_9645 n_107335; n_107335.e0 = _105779; n_107335.e1 = _105783; n_107335.e2 = _105788; pvnormalize_105801 = n_107335; goto l105799; l105798: ; double _107331; _107331 = _105783 / length_105793; double _107332; _107332 = _105788 / length_105793; double _107330; _107330 = _105779 / length_105793; struct_vec_9645 _107333; _107333.e0 = _107330; _107333.e1 = _107331; _107333.e2 = _107332; pvnormalize_105801 = _107333; goto l105799; l105799: ; vnormalize_105801 = pvnormalize_105801; struct_vec_9645 p_107328; p_107328.e0 = _105778; p_107328.e1 = _105782; p_107328.e2 = _105787; struct_Isect_9647 isect_107329; isect_107329.e0 = t_105772; isect_107329.e1 = p_107328; isect_107329.e2 = vnormalize_105801; isect_107329.e3 = 1; pray_sphere_intersect_105804 = isect_107329; goto l105802; l105802: ; ray_sphere_intersect_105804 = pray_sphere_intersect_105804; double _105805; _105805 = _105358 * rx_105748; double _105806; _105806 = _105805 + _105755; double _105807; _105807 = _105361 * rz_105761; double _105808; _105808 = _105806 + _105807; double _105809; _105809 = _105808 * _105808; double D_105810; D_105810 = _105809 - C_105369; bool _105811; _105811 = 0.000000e+00 < D_105810; if (_105811) goto l105812; else goto l107327; l107327: ; goto l107324; l105812: ; _105815 = sqrt(D_105810); p_105815 = _105815; l105813: ; _105815 = p_105815; double _105816; _105816 = -0.000000e+00 - _105808; double t_105817; t_105817 = _105816 - _105815; bool _105818; _105818 = 0.000000e+00 < t_105817; if (_105818) goto l105819; else goto l107326; l107326: ; goto l107323; l105819: ; double _105820; _105820 = ray_sphere_intersect_105804.e0; bool _105821; _105821 = t_105817 < _105820; if (_105821) goto l105822; else goto l107322; l107322: ; goto l107323; l107323: ; goto l107324; l107324: ; pray_sphere_intersect_105850 = ray_sphere_intersect_105804; goto l105848; l105822: ; double _105827; _105827 = ry_105754 * t_105817; double _105832; _105832 = rz_105761 * t_105817; double _105833; _105833 = _105300 + _105832; double _105823; _105823 = rx_105748 * t_105817; double _105828; _105828 = _105288 + _105827; double _105834; _105834 = _105833 - -3.000000e+00; double _105824; _105824 = _105275 + _105823; double _105829; _105829 = _105828 - 0.000000e+00; double _105835; _105835 = _105834 * _105834; double _105825; _105825 = _105824 - -5.000000e-01; double _105830; _105830 = _105829 * _105829; double _105826; _105826 = _105825 * _105825; double _105831; _105831 = _105826 + _105830; double _105836; _105836 = _105831 + _105835; length_105839 = sqrt(_105836); plength_105839 = length_105839; l105837: ; length_105839 = plength_105839; _105842 = fabs(length_105839); p_105842 = _105842; l105840: ; _105842 = p_105842; bool _105843; _105843 = 1.000000e-17 < _105842; if (_105843) goto l105844; else goto l107320; l107320: ; struct_vec_9645 n_107321; n_107321.e0 = _105825; n_107321.e1 = _105829; n_107321.e2 = _105834; pvnormalize_105847 = n_107321; goto l105845; l105844: ; double _107316; _107316 = _105825 / length_105839; double _107318; _107318 = _105834 / length_105839; double _107317; _107317 = _105829 / length_105839; struct_vec_9645 _107319; _107319.e0 = _107316; _107319.e1 = _107317; _107319.e2 = _107318; pvnormalize_105847 = _107319; goto l105845; l105845: ; vnormalize_105847 = pvnormalize_105847; struct_vec_9645 p_107314; p_107314.e0 = _105824; p_107314.e1 = _105828; p_107314.e2 = _105833; struct_Isect_9647 isect_107315; isect_107315.e0 = t_105817; isect_107315.e1 = p_107314; isect_107315.e2 = vnormalize_105847; isect_107315.e3 = 1; pray_sphere_intersect_105850 = isect_107315; goto l105848; l105848: ; ray_sphere_intersect_105850 = pray_sphere_intersect_105850; double _105853; _105853 = _105414 * rz_105761; double _105851; _105851 = _105411 * rx_105748; double _105852; _105852 = _105851 + _105755; double _105854; _105854 = _105852 + _105853; double _105855; _105855 = _105854 * _105854; double D_105856; D_105856 = _105855 - C_105422; bool _105857; _105857 = 0.000000e+00 < D_105856; if (_105857) goto l105858; else goto l107313; l107313: ; goto l107310; l105858: ; _105861 = sqrt(D_105856); p_105861 = _105861; l105859: ; _105861 = p_105861; double _105862; _105862 = -0.000000e+00 - _105854; double t_105863; t_105863 = _105862 - _105861; bool _105864; _105864 = 0.000000e+00 < t_105863; if (_105864) goto l105865; else goto l107312; l107312: ; goto l107309; l105865: ; double _105866; _105866 = ray_sphere_intersect_105850.e0; bool _105867; _105867 = t_105863 < _105866; if (_105867) goto l105868; else goto l107308; l107308: ; goto l107309; l107309: ; goto l107310; l107310: ; pray_sphere_intersect_105896 = ray_sphere_intersect_105850; goto l105894; l105868: ; double _105869; _105869 = rx_105748 * t_105863; double _105878; _105878 = rz_105761 * t_105863; double _105873; _105873 = ry_105754 * t_105863; double _105870; _105870 = _105275 + _105869; double _105879; _105879 = _105300 + _105878; double _105874; _105874 = _105288 + _105873; double _105871; _105871 = _105870 - 1.000000e+00; double _105880; _105880 = _105879 - -2.200000e+00; double _105875; _105875 = _105874 - 0.000000e+00; double _105872; _105872 = _105871 * _105871; double _105881; _105881 = _105880 * _105880; double _105876; _105876 = _105875 * _105875; double _105877; _105877 = _105872 + _105876; double _105882; _105882 = _105877 + _105881; length_105885 = sqrt(_105882); plength_105885 = length_105885; l105883: ; length_105885 = plength_105885; _105888 = fabs(length_105885); p_105888 = _105888; l105886: ; _105888 = p_105888; bool _105889; _105889 = 1.000000e-17 < _105888; if (_105889) goto l105890; else goto l107306; l107306: ; struct_vec_9645 n_107307; n_107307.e0 = _105871; n_107307.e1 = _105875; n_107307.e2 = _105880; pvnormalize_105893 = n_107307; goto l105891; l105890: ; double _107303; _107303 = _105875 / length_105885; double _107302; _107302 = _105871 / length_105885; double _107304; _107304 = _105880 / length_105885; struct_vec_9645 _107305; _107305.e0 = _107302; _107305.e1 = _107303; _107305.e2 = _107304; pvnormalize_105893 = _107305; goto l105891; l105891: ; vnormalize_105893 = pvnormalize_105893; struct_vec_9645 p_107300; p_107300.e0 = _105870; p_107300.e1 = _105874; p_107300.e2 = _105879; struct_Isect_9647 isect_107301; isect_107301.e0 = t_105863; isect_107301.e1 = p_107300; isect_107301.e2 = vnormalize_105893; isect_107301.e3 = 1; pray_sphere_intersect_105896 = isect_107301; goto l105894; l105894: ; ray_sphere_intersect_105896 = pray_sphere_intersect_105896; double _105900; _105900 = 0.000000e+00 * rz_105761; double _105898; _105898 = 1.000000e+00 * ry_105754; double _105897; _105897 = 0.000000e+00 * rx_105748; double _105899; _105899 = _105897 + _105898; double _105901; _105901 = _105899 + _105900; _105904 = fabs(_105901); p_105904 = _105904; l105902: ; _105904 = p_105904; bool _105905; _105905 = 1.000000e-17 <= _105904; if (_105905) goto l105906; else goto l107299; l107299: ; goto l107296; l105906: ; double t_105907; t_105907 = _105480 / _105901; bool _105908; _105908 = 0.000000e+00 < t_105907; if (_105908) goto l105909; else goto l107298; l107298: ; goto l107295; l105909: ; double _105910; _105910 = ray_sphere_intersect_105896.e0; bool _105911; _105911 = t_105907 < _105910; if (_105911) goto l105912; else goto l107294; l107294: ; goto l107295; l107295: ; goto l107296; l107296: ; pray_plane_intersect_105915 = ray_sphere_intersect_105896; goto l105913; l105912: ; double _107290; _107290 = rz_105761 * t_105907; double _107286; _107286 = rx_105748 * t_105907; double _107287; _107287 = _105275 + _107286; double _107288; _107288 = ry_105754 * t_105907; double _107291; _107291 = _105300 + _107290; double _107289; _107289 = _105288 + _107288; struct_vec_9645 p_107292; p_107292.e0 = _107287; p_107292.e1 = _107289; p_107292.e2 = _107291; struct_vec_9645 _106999_554; _106999_554.e0 = 0.000000e+00; _106999_554.e1 = 1.000000e+00; _106999_554.e2 = 0.000000e+00; struct_Isect_9647 isect_107293; isect_107293.e0 = t_105907; isect_107293.e1 = p_107292; isect_107293.e2 = _106999_554; isect_107293.e3 = 1; pray_plane_intersect_105915 = isect_107293; goto l105913; l105913: ; ray_plane_intersect_105915 = pray_plane_intersect_105915; int _105916; _105916 = ray_plane_intersect_105915.e3; bool _105917; _105917 = _105916 == 1; if (_105917) goto l105918; else goto l107285; l107285: ; pocclusion_105921 = occlusion_105708; goto l105919; l105918: ; double _107284; _107284 = 1.000000e+00 + occlusion_105708; pocclusion_105921 = _107284; goto l105919; l105919: ; occlusion_105921 = pocclusion_105921; unsigned long _105922; _105922 = 4294883355 * lo_105723; unsigned long _105923; _105923 = _105922 + hi_105725; unsigned long hi_105926; hi_105926 = _105923 >> 32; unsigned long lo_105924; lo_105924 = 4294967295 & _105923; unsigned int _105927; _105927 = (unsigned int)hi_105926; unsigned int _105925; _105925 = (unsigned int)lo_105924; unsigned int _105928; _105928 = _105925 ^ _105927; double _105929; _105929 = (double)_105928; double _105930; _105930 = 2.328306e-10 * _105929; theta_105933 = sqrt(_105930); ptheta_105933 = theta_105933; l105931: ; theta_105933 = ptheta_105933; unsigned long _105934; _105934 = 4294883355 * lo_105924; unsigned long _105935; _105935 = _105934 + hi_105926; unsigned long lo_105936; lo_105936 = 4294967295 & _105935; unsigned long hi_105938; hi_105938 = _105935 >> 32; unsigned int _105937; _105937 = (unsigned int)lo_105936; unsigned int _105939; _105939 = (unsigned int)hi_105938; unsigned int _105940; _105940 = _105937 ^ _105939; double _105941; _105941 = (double)_105940; double _105942; _105942 = 2.328306e-10 * _105941; double phi_105943; phi_105943 = 6.283185e+00 * _105942; _105946 = cos(phi_105943); p_105946 = _105946; l105944: ; _105946 = p_105946; _105949 = sin(phi_105943); p_105949 = _105949; l105947: ; _105949 = p_105949; double _105950; _105950 = theta_105933 * theta_105933; double _105951; _105951 = 1.000000e+00 - _105950; z_105954 = sqrt(_105951); pz_105954 = z_105954; l105952: ; z_105954 = pz_105954; double _105960; _105960 = z_105954 * _105152; double x_105955; x_105955 = _105946 * theta_105933; double _105963; _105963 = x_105955 * _105192; double _105970; _105970 = x_105955 * _105190; double _105966; _105966 = z_105954 * _105165; double _105973; _105973 = z_105954 * _105163; double _105956; _105956 = x_105955 * _105196; double y_105957; y_105957 = _105949 * theta_105933; double _105971; _105971 = y_105957 * _105303; double _105958; _105958 = y_105957 * _105280; double _105964; _105964 = y_105957 * _105291; double _105972; _105972 = _105970 + _105971; double _105959; _105959 = _105956 + _105958; double _105965; _105965 = _105963 + _105964; double rz_105974; rz_105974 = _105972 + _105973; double rx_105961; rx_105961 = _105959 + _105960; double ry_105967; ry_105967 = _105965 + _105966; double _105975; _105975 = _105301 * rz_105974; double _105962; _105962 = _105276 * rx_105961; double _105968; _105968 = _105289 * ry_105967; double _105969; _105969 = _105962 + _105968; double _105976; _105976 = _105969 + _105975; double _105977; _105977 = _105976 * _105976; double D_105978; D_105978 = _105977 - C_105317; bool _105979; _105979 = 0.000000e+00 < D_105978; if (_105979) goto l105980; else goto l107283; l107283: ; goto l107280; l105980: ; _105983 = sqrt(D_105978); p_105983 = _105983; l105981: ; _105983 = p_105983; double _105984; _105984 = -0.000000e+00 - _105976; double t_105985; t_105985 = _105984 - _105983; bool _105986; _105986 = 0.000000e+00 < t_105985; if (_105986) goto l105987; else goto l107282; l107282: ; goto l107279; l105987: ; bool _105988; _105988 = t_105985 < 1.000000e+17; if (_105988) goto l105989; else goto l107278; l107278: ; goto l107279; l107279: ; goto l107280; l107280: ; struct_Isect_9647 _107049_583; _107049_583.e0 = 1.000000e+17; // bottom: _107049_583.e1 = // bottom: struct_vec_9645 _107047_587;; // bottom: _107049_583.e2 = _107047_587; _107049_583.e3 = 0; pray_sphere_intersect_106017 = _107049_583; goto l106015; l105989: ; double _105994; _105994 = ry_105967 * t_105985; double _105995; _105995 = _105288 + _105994; double _105999; _105999 = rz_105974 * t_105985; double _105990; _105990 = rx_105961 * t_105985; double _105996; _105996 = _105995 - 0.000000e+00; double _106000; _106000 = _105300 + _105999; double _105991; _105991 = _105275 + _105990; double _105997; _105997 = _105996 * _105996; double _106001; _106001 = _106000 - -3.500000e+00; double _105992; _105992 = _105991 - -2.000000e+00; double _106002; _106002 = _106001 * _106001; double _105993; _105993 = _105992 * _105992; double _105998; _105998 = _105993 + _105997; double _106003; _106003 = _105998 + _106002; length_106006 = sqrt(_106003); plength_106006 = length_106006; l106004: ; length_106006 = plength_106006; _106009 = fabs(length_106006); p_106009 = _106009; l106007: ; _106009 = p_106009; bool _106010; _106010 = 1.000000e-17 < _106009; if (_106010) goto l106011; else goto l107276; l107276: ; struct_vec_9645 n_107277; n_107277.e0 = _105992; n_107277.e1 = _105996; n_107277.e2 = _106001; pvnormalize_106014 = n_107277; goto l106012; l106011: ; double _107273; _107273 = _105996 / length_106006; double _107272; _107272 = _105992 / length_106006; double _107274; _107274 = _106001 / length_106006; struct_vec_9645 _107275; _107275.e0 = _107272; _107275.e1 = _107273; _107275.e2 = _107274; pvnormalize_106014 = _107275; goto l106012; l106012: ; vnormalize_106014 = pvnormalize_106014; struct_vec_9645 p_107270; p_107270.e0 = _105991; p_107270.e1 = _105995; p_107270.e2 = _106000; struct_Isect_9647 isect_107271; isect_107271.e0 = t_105985; isect_107271.e1 = p_107270; isect_107271.e2 = vnormalize_106014; isect_107271.e3 = 1; pray_sphere_intersect_106017 = isect_107271; goto l106015; l106015: ; ray_sphere_intersect_106017 = pray_sphere_intersect_106017; double _106020; _106020 = _105361 * rz_105974; double _106018; _106018 = _105358 * rx_105961; double _106019; _106019 = _106018 + _105968; double _106021; _106021 = _106019 + _106020; double _106022; _106022 = _106021 * _106021; double D_106023; D_106023 = _106022 - C_105369; bool _106024; _106024 = 0.000000e+00 < D_106023; if (_106024) goto l106025; else goto l107269; l107269: ; goto l107266; l106025: ; _106028 = sqrt(D_106023); p_106028 = _106028; l106026: ; _106028 = p_106028; double _106029; _106029 = -0.000000e+00 - _106021; double t_106030; t_106030 = _106029 - _106028; bool _106031; _106031 = 0.000000e+00 < t_106030; if (_106031) goto l106032; else goto l107268; l107268: ; goto l107265; l106032: ; double _106033; _106033 = ray_sphere_intersect_106017.e0; bool _106034; _106034 = t_106030 < _106033; if (_106034) goto l106035; else goto l107264; l107264: ; goto l107265; l107265: ; goto l107266; l107266: ; pray_sphere_intersect_106063 = ray_sphere_intersect_106017; goto l106061; l106035: ; double _106045; _106045 = rz_105974 * t_106030; double _106040; _106040 = ry_105967 * t_106030; double _106041; _106041 = _105288 + _106040; double _106046; _106046 = _105300 + _106045; double _106042; _106042 = _106041 - 0.000000e+00; double _106036; _106036 = rx_105961 * t_106030; double _106047; _106047 = _106046 - -3.000000e+00; double _106043; _106043 = _106042 * _106042; double _106037; _106037 = _105275 + _106036; double _106048; _106048 = _106047 * _106047; double _106038; _106038 = _106037 - -5.000000e-01; double _106039; _106039 = _106038 * _106038; double _106044; _106044 = _106039 + _106043; double _106049; _106049 = _106044 + _106048; length_106052 = sqrt(_106049); plength_106052 = length_106052; l106050: ; length_106052 = plength_106052; _106055 = fabs(length_106052); p_106055 = _106055; l106053: ; _106055 = p_106055; bool _106056; _106056 = 1.000000e-17 < _106055; if (_106056) goto l106057; else goto l107262; l107262: ; struct_vec_9645 n_107263; n_107263.e0 = _106038; n_107263.e1 = _106042; n_107263.e2 = _106047; pvnormalize_106060 = n_107263; goto l106058; l106057: ; double _107258; _107258 = _106038 / length_106052; double _107259; _107259 = _106042 / length_106052; double _107260; _107260 = _106047 / length_106052; struct_vec_9645 _107261; _107261.e0 = _107258; _107261.e1 = _107259; _107261.e2 = _107260; pvnormalize_106060 = _107261; goto l106058; l106058: ; vnormalize_106060 = pvnormalize_106060; struct_vec_9645 p_107256; p_107256.e0 = _106037; p_107256.e1 = _106041; p_107256.e2 = _106046; struct_Isect_9647 isect_107257; isect_107257.e0 = t_106030; isect_107257.e1 = p_107256; isect_107257.e2 = vnormalize_106060; isect_107257.e3 = 1; pray_sphere_intersect_106063 = isect_107257; goto l106061; l106061: ; ray_sphere_intersect_106063 = pray_sphere_intersect_106063; double _106066; _106066 = _105414 * rz_105974; double _106064; _106064 = _105411 * rx_105961; double _106065; _106065 = _106064 + _105968; double _106067; _106067 = _106065 + _106066; double _106068; _106068 = _106067 * _106067; double D_106069; D_106069 = _106068 - C_105422; bool _106070; _106070 = 0.000000e+00 < D_106069; if (_106070) goto l106071; else goto l107255; l107255: ; goto l107252; l106071: ; _106074 = sqrt(D_106069); p_106074 = _106074; l106072: ; _106074 = p_106074; double _106075; _106075 = -0.000000e+00 - _106067; double t_106076; t_106076 = _106075 - _106074; bool _106077; _106077 = 0.000000e+00 < t_106076; if (_106077) goto l106078; else goto l107254; l107254: ; goto l107251; l106078: ; double _106079; _106079 = ray_sphere_intersect_106063.e0; bool _106080; _106080 = t_106076 < _106079; if (_106080) goto l106081; else goto l107250; l107250: ; goto l107251; l107251: ; goto l107252; l107252: ; pray_sphere_intersect_106109 = ray_sphere_intersect_106063; goto l106107; l106081: ; double _106091; _106091 = rz_105974 * t_106076; double _106086; _106086 = ry_105967 * t_106076; double _106092; _106092 = _105300 + _106091; double _106093; _106093 = _106092 - -2.200000e+00; double _106087; _106087 = _105288 + _106086; double _106082; _106082 = rx_105961 * t_106076; double _106083; _106083 = _105275 + _106082; double _106084; _106084 = _106083 - 1.000000e+00; double _106088; _106088 = _106087 - 0.000000e+00; double _106085; _106085 = _106084 * _106084; double _106094; _106094 = _106093 * _106093; double _106089; _106089 = _106088 * _106088; double _106090; _106090 = _106085 + _106089; double _106095; _106095 = _106090 + _106094; length_106098 = sqrt(_106095); plength_106098 = length_106098; l106096: ; length_106098 = plength_106098; _106101 = fabs(length_106098); p_106101 = _106101; l106099: ; _106101 = p_106101; bool _106102; _106102 = 1.000000e-17 < _106101; if (_106102) goto l106103; else goto l107248; l107248: ; struct_vec_9645 n_107249; n_107249.e0 = _106084; n_107249.e1 = _106088; n_107249.e2 = _106093; pvnormalize_106106 = n_107249; goto l106104; l106103: ; double _107244; _107244 = _106084 / length_106098; double _107246; _107246 = _106093 / length_106098; double _107245; _107245 = _106088 / length_106098; struct_vec_9645 _107247; _107247.e0 = _107244; _107247.e1 = _107245; _107247.e2 = _107246; pvnormalize_106106 = _107247; goto l106104; l106104: ; vnormalize_106106 = pvnormalize_106106; struct_vec_9645 p_107242; p_107242.e0 = _106083; p_107242.e1 = _106087; p_107242.e2 = _106092; struct_Isect_9647 isect_107243; isect_107243.e0 = t_106076; isect_107243.e1 = p_107242; isect_107243.e2 = vnormalize_106106; isect_107243.e3 = 1; pray_sphere_intersect_106109 = isect_107243; goto l106107; l106107: ; ray_sphere_intersect_106109 = pray_sphere_intersect_106109; double _106111; _106111 = 1.000000e+00 * ry_105967; double _106113; _106113 = 0.000000e+00 * rz_105974; double _106110; _106110 = 0.000000e+00 * rx_105961; double _106112; _106112 = _106110 + _106111; double _106114; _106114 = _106112 + _106113; _106117 = fabs(_106114); p_106117 = _106117; l106115: ; _106117 = p_106117; bool _106118; _106118 = 1.000000e-17 <= _106117; if (_106118) goto l106119; else goto l107241; l107241: ; goto l107238; l106119: ; double t_106120; t_106120 = _105480 / _106114; bool _106121; _106121 = 0.000000e+00 < t_106120; if (_106121) goto l106122; else goto l107240; l107240: ; goto l107237; l106122: ; double _106123; _106123 = ray_sphere_intersect_106109.e0; bool _106124; _106124 = t_106120 < _106123; if (_106124) goto l106125; else goto l107236; l107236: ; goto l107237; l107237: ; goto l107238; l107238: ; pray_plane_intersect_106128 = ray_sphere_intersect_106109; goto l106126; l106125: ; double _107230; _107230 = ry_105967 * t_106120; double _107231; _107231 = _105288 + _107230; double _107228; _107228 = rx_105961 * t_106120; double _107232; _107232 = rz_105974 * t_106120; double _107233; _107233 = _105300 + _107232; double _107229; _107229 = _105275 + _107228; struct_vec_9645 p_107234; p_107234.e0 = _107229; p_107234.e1 = _107231; p_107234.e2 = _107233; struct_vec_9645 _106999_662; _106999_662.e0 = 0.000000e+00; _106999_662.e1 = 1.000000e+00; _106999_662.e2 = 0.000000e+00; struct_Isect_9647 isect_107235; isect_107235.e0 = t_106120; isect_107235.e1 = p_107234; isect_107235.e2 = _106999_662; isect_107235.e3 = 1; pray_plane_intersect_106128 = isect_107235; goto l106126; l106126: ; ray_plane_intersect_106128 = pray_plane_intersect_106128; int _106129; _106129 = ray_plane_intersect_106128.e3; bool _106130; _106130 = _106129 == 1; if (_106130) goto l106131; else goto l107227; l107227: ; pocclusion_106134 = occlusion_105921; goto l106132; l106131: ; double _107226; _107226 = 1.000000e+00 + occlusion_105921; pocclusion_106134 = _107226; goto l106132; l106132: ; occlusion_106134 = pocclusion_106134; unsigned long _106135; _106135 = 4294883355 * lo_105936; unsigned long _106136; _106136 = _106135 + hi_105938; unsigned long lo_106137; lo_106137 = 4294967295 & _106136; unsigned long hi_106139; hi_106139 = _106136 >> 32; unsigned int _106138; _106138 = (unsigned int)lo_106137; unsigned int _106140; _106140 = (unsigned int)hi_106139; unsigned int _106141; _106141 = _106138 ^ _106140; double _106142; _106142 = (double)_106141; double _106143; _106143 = 2.328306e-10 * _106142; theta_106146 = sqrt(_106143); ptheta_106146 = theta_106146; l106144: ; theta_106146 = ptheta_106146; unsigned long _106147; _106147 = 4294883355 * lo_106137; unsigned long _106148; _106148 = _106147 + hi_106139; unsigned long hi_106151; hi_106151 = _106148 >> 32; unsigned long lo_106149; lo_106149 = 4294967295 & _106148; unsigned int _106152; _106152 = (unsigned int)hi_106151; unsigned int _106150; _106150 = (unsigned int)lo_106149; unsigned int _106153; _106153 = _106150 ^ _106152; double _106154; _106154 = (double)_106153; double _106155; _106155 = 2.328306e-10 * _106154; double phi_106156; phi_106156 = 6.283185e+00 * _106155; _106159 = cos(phi_106156); p_106159 = _106159; l106157: ; _106159 = p_106159; _106162 = sin(phi_106156); p_106162 = _106162; l106160: ; _106162 = p_106162; double _106163; _106163 = theta_106146 * theta_106146; double _106164; _106164 = 1.000000e+00 - _106163; z_106167 = sqrt(_106164); pz_106167 = z_106167; l106165: ; z_106167 = pz_106167; double x_106168; x_106168 = _106159 * theta_106146; double _106169; _106169 = x_106168 * _105196; double _106179; _106179 = z_106167 * _105165; double _106186; _106186 = z_106167 * _105163; double y_106170; y_106170 = _106162 * theta_106146; double _106176; _106176 = x_106168 * _105192; double _106173; _106173 = z_106167 * _105152; double _106183; _106183 = x_106168 * _105190; double _106184; _106184 = y_106170 * _105303; double _106171; _106171 = y_106170 * _105280; double _106177; _106177 = y_106170 * _105291; double _106178; _106178 = _106176 + _106177; double _106185; _106185 = _106183 + _106184; double _106172; _106172 = _106169 + _106171; double ry_106180; ry_106180 = _106178 + _106179; double rz_106187; rz_106187 = _106185 + _106186; double rx_106174; rx_106174 = _106172 + _106173; double _106181; _106181 = _105289 * ry_106180; double _106188; _106188 = _105301 * rz_106187; double _106175; _106175 = _105276 * rx_106174; double _106182; _106182 = _106175 + _106181; double _106189; _106189 = _106182 + _106188; double _106190; _106190 = _106189 * _106189; double D_106191; D_106191 = _106190 - C_105317; bool _106192; _106192 = 0.000000e+00 < D_106191; if (_106192) goto l106193; else goto l107225; l107225: ; goto l107222; l106193: ; _106196 = sqrt(D_106191); p_106196 = _106196; l106194: ; _106196 = p_106196; double _106197; _106197 = -0.000000e+00 - _106189; double t_106198; t_106198 = _106197 - _106196; bool _106199; _106199 = 0.000000e+00 < t_106198; if (_106199) goto l106200; else goto l107224; l107224: ; goto l107221; l106200: ; bool _106201; _106201 = t_106198 < 1.000000e+17; if (_106201) goto l106202; else goto l107220; l107220: ; goto l107221; l107221: ; goto l107222; l107222: ; struct_Isect_9647 _107049_691; _107049_691.e0 = 1.000000e+17; // bottom: _107049_691.e1 = // bottom: struct_vec_9645 _107047_695;; // bottom: _107049_691.e2 = _107047_695; _107049_691.e3 = 0; pray_sphere_intersect_106230 = _107049_691; goto l106228; l106202: ; double _106207; _106207 = ry_106180 * t_106198; double _106212; _106212 = rz_106187 * t_106198; double _106203; _106203 = rx_106174 * t_106198; double _106208; _106208 = _105288 + _106207; double _106213; _106213 = _105300 + _106212; double _106204; _106204 = _105275 + _106203; double _106209; _106209 = _106208 - 0.000000e+00; double _106214; _106214 = _106213 - -3.500000e+00; double _106205; _106205 = _106204 - -2.000000e+00; double _106210; _106210 = _106209 * _106209; double _106215; _106215 = _106214 * _106214; double _106206; _106206 = _106205 * _106205; double _106211; _106211 = _106206 + _106210; double _106216; _106216 = _106211 + _106215; length_106219 = sqrt(_106216); plength_106219 = length_106219; l106217: ; length_106219 = plength_106219; _106222 = fabs(length_106219); p_106222 = _106222; l106220: ; _106222 = p_106222; bool _106223; _106223 = 1.000000e-17 < _106222; if (_106223) goto l106224; else goto l107218; l107218: ; struct_vec_9645 n_107219; n_107219.e0 = _106205; n_107219.e1 = _106209; n_107219.e2 = _106214; pvnormalize_106227 = n_107219; goto l106225; l106224: ; double _107216; _107216 = _106214 / length_106219; double _107214; _107214 = _106205 / length_106219; double _107215; _107215 = _106209 / length_106219; struct_vec_9645 _107217; _107217.e0 = _107214; _107217.e1 = _107215; _107217.e2 = _107216; pvnormalize_106227 = _107217; goto l106225; l106225: ; vnormalize_106227 = pvnormalize_106227; struct_vec_9645 p_107212; p_107212.e0 = _106204; p_107212.e1 = _106208; p_107212.e2 = _106213; struct_Isect_9647 isect_107213; isect_107213.e0 = t_106198; isect_107213.e1 = p_107212; isect_107213.e2 = vnormalize_106227; isect_107213.e3 = 1; pray_sphere_intersect_106230 = isect_107213; goto l106228; l106228: ; ray_sphere_intersect_106230 = pray_sphere_intersect_106230; double _106231; _106231 = _105358 * rx_106174; double _106233; _106233 = _105361 * rz_106187; double _106232; _106232 = _106231 + _106181; double _106234; _106234 = _106232 + _106233; double _106235; _106235 = _106234 * _106234; double D_106236; D_106236 = _106235 - C_105369; bool _106237; _106237 = 0.000000e+00 < D_106236; if (_106237) goto l106238; else goto l107211; l107211: ; goto l107208; l106238: ; _106241 = sqrt(D_106236); p_106241 = _106241; l106239: ; _106241 = p_106241; double _106242; _106242 = -0.000000e+00 - _106234; double t_106243; t_106243 = _106242 - _106241; bool _106244; _106244 = 0.000000e+00 < t_106243; if (_106244) goto l106245; else goto l107210; l107210: ; goto l107207; l106245: ; double _106246; _106246 = ray_sphere_intersect_106230.e0; bool _106247; _106247 = t_106243 < _106246; if (_106247) goto l106248; else goto l107206; l107206: ; goto l107207; l107207: ; goto l107208; l107208: ; pray_sphere_intersect_106276 = ray_sphere_intersect_106230; goto l106274; l106248: ; double _106253; _106253 = ry_106180 * t_106243; double _106254; _106254 = _105288 + _106253; double _106249; _106249 = rx_106174 * t_106243; double _106258; _106258 = rz_106187 * t_106243; double _106259; _106259 = _105300 + _106258; double _106260; _106260 = _106259 - -3.000000e+00; double _106255; _106255 = _106254 - 0.000000e+00; double _106250; _106250 = _105275 + _106249; double _106261; _106261 = _106260 * _106260; double _106256; _106256 = _106255 * _106255; double _106251; _106251 = _106250 - -5.000000e-01; double _106252; _106252 = _106251 * _106251; double _106257; _106257 = _106252 + _106256; double _106262; _106262 = _106257 + _106261; length_106265 = sqrt(_106262); plength_106265 = length_106265; l106263: ; length_106265 = plength_106265; _106268 = fabs(length_106265); p_106268 = _106268; l106266: ; _106268 = p_106268; bool _106269; _106269 = 1.000000e-17 < _106268; if (_106269) goto l106270; else goto l107204; l107204: ; struct_vec_9645 n_107205; n_107205.e0 = _106251; n_107205.e1 = _106255; n_107205.e2 = _106260; pvnormalize_106273 = n_107205; goto l106271; l106270: ; double _107202; _107202 = _106260 / length_106265; double _107200; _107200 = _106251 / length_106265; double _107201; _107201 = _106255 / length_106265; struct_vec_9645 _107203; _107203.e0 = _107200; _107203.e1 = _107201; _107203.e2 = _107202; pvnormalize_106273 = _107203; goto l106271; l106271: ; vnormalize_106273 = pvnormalize_106273; struct_vec_9645 p_107198; p_107198.e0 = _106250; p_107198.e1 = _106254; p_107198.e2 = _106259; struct_Isect_9647 isect_107199; isect_107199.e0 = t_106243; isect_107199.e1 = p_107198; isect_107199.e2 = vnormalize_106273; isect_107199.e3 = 1; pray_sphere_intersect_106276 = isect_107199; goto l106274; l106274: ; ray_sphere_intersect_106276 = pray_sphere_intersect_106276; double _106277; _106277 = _105411 * rx_106174; double _106279; _106279 = _105414 * rz_106187; double _106278; _106278 = _106277 + _106181; double _106280; _106280 = _106278 + _106279; double _106281; _106281 = _106280 * _106280; double D_106282; D_106282 = _106281 - C_105422; bool _106283; _106283 = 0.000000e+00 < D_106282; if (_106283) goto l106284; else goto l107197; l107197: ; goto l107194; l106284: ; _106287 = sqrt(D_106282); p_106287 = _106287; l106285: ; _106287 = p_106287; double _106288; _106288 = -0.000000e+00 - _106280; double t_106289; t_106289 = _106288 - _106287; bool _106290; _106290 = 0.000000e+00 < t_106289; if (_106290) goto l106291; else goto l107196; l107196: ; goto l107193; l106291: ; double _106292; _106292 = ray_sphere_intersect_106276.e0; bool _106293; _106293 = t_106289 < _106292; if (_106293) goto l106294; else goto l107192; l107192: ; goto l107193; l107193: ; goto l107194; l107194: ; pray_sphere_intersect_106322 = ray_sphere_intersect_106276; goto l106320; l106294: ; double _106295; _106295 = rx_106174 * t_106289; double _106296; _106296 = _105275 + _106295; double _106297; _106297 = _106296 - 1.000000e+00; double _106298; _106298 = _106297 * _106297; double _106299; _106299 = ry_106180 * t_106289; double _106304; _106304 = rz_106187 * t_106289; double _106300; _106300 = _105288 + _106299; double _106305; _106305 = _105300 + _106304; double _106301; _106301 = _106300 - 0.000000e+00; double _106306; _106306 = _106305 - -2.200000e+00; double _106302; _106302 = _106301 * _106301; double _106307; _106307 = _106306 * _106306; double _106303; _106303 = _106298 + _106302; double _106308; _106308 = _106303 + _106307; length_106311 = sqrt(_106308); plength_106311 = length_106311; l106309: ; length_106311 = plength_106311; _106314 = fabs(length_106311); p_106314 = _106314; l106312: ; _106314 = p_106314; bool _106315; _106315 = 1.000000e-17 < _106314; if (_106315) goto l106316; else goto l107190; l107190: ; struct_vec_9645 n_107191; n_107191.e0 = _106297; n_107191.e1 = _106301; n_107191.e2 = _106306; pvnormalize_106319 = n_107191; goto l106317; l106316: ; double _107186; _107186 = _106297 / length_106311; double _107188; _107188 = _106306 / length_106311; double _107187; _107187 = _106301 / length_106311; struct_vec_9645 _107189; _107189.e0 = _107186; _107189.e1 = _107187; _107189.e2 = _107188; pvnormalize_106319 = _107189; goto l106317; l106317: ; vnormalize_106319 = pvnormalize_106319; struct_vec_9645 p_107184; p_107184.e0 = _106296; p_107184.e1 = _106300; p_107184.e2 = _106305; struct_Isect_9647 isect_107185; isect_107185.e0 = t_106289; isect_107185.e1 = p_107184; isect_107185.e2 = vnormalize_106319; isect_107185.e3 = 1; pray_sphere_intersect_106322 = isect_107185; goto l106320; l106320: ; ray_sphere_intersect_106322 = pray_sphere_intersect_106322; double _106326; _106326 = 0.000000e+00 * rz_106187; double _106323; _106323 = 0.000000e+00 * rx_106174; double _106324; _106324 = 1.000000e+00 * ry_106180; double _106325; _106325 = _106323 + _106324; double _106327; _106327 = _106325 + _106326; _106330 = fabs(_106327); p_106330 = _106330; l106328: ; _106330 = p_106330; bool _106331; _106331 = 1.000000e-17 <= _106330; if (_106331) goto l106332; else goto l107183; l107183: ; goto l107180; l106332: ; double t_106333; t_106333 = _105480 / _106327; bool _106334; _106334 = 0.000000e+00 < t_106333; if (_106334) goto l106335; else goto l107182; l107182: ; goto l107179; l106335: ; double _106336; _106336 = ray_sphere_intersect_106322.e0; bool _106337; _106337 = t_106333 < _106336; if (_106337) goto l106338; else goto l107178; l107178: ; goto l107179; l107179: ; goto l107180; l107180: ; pray_plane_intersect_106341 = ray_sphere_intersect_106322; goto l106339; l106338: ; double _107174; _107174 = rz_106187 * t_106333; double _107172; _107172 = ry_106180 * t_106333; double _107173; _107173 = _105288 + _107172; double _107170; _107170 = rx_106174 * t_106333; double _107175; _107175 = _105300 + _107174; double _107171; _107171 = _105275 + _107170; struct_vec_9645 p_107176; p_107176.e0 = _107171; p_107176.e1 = _107173; p_107176.e2 = _107175; struct_vec_9645 _106999_770; _106999_770.e0 = 0.000000e+00; _106999_770.e1 = 1.000000e+00; _106999_770.e2 = 0.000000e+00; struct_Isect_9647 isect_107177; isect_107177.e0 = t_106333; isect_107177.e1 = p_107176; isect_107177.e2 = _106999_770; isect_107177.e3 = 1; pray_plane_intersect_106341 = isect_107177; goto l106339; l106339: ; ray_plane_intersect_106341 = pray_plane_intersect_106341; int _106342; _106342 = ray_plane_intersect_106341.e3; bool _106343; _106343 = _106342 == 1; if (_106343) goto l106344; else goto l107169; l107169: ; pocclusion_106347 = occlusion_106134; goto l106345; l106344: ; double _107168; _107168 = 1.000000e+00 + occlusion_106134; pocclusion_106347 = _107168; goto l106345; l106345: ; occlusion_106347 = pocclusion_106347; unsigned long _106348; _106348 = 4294883355 * lo_106149; unsigned long _106349; _106349 = _106348 + hi_106151; unsigned long lo_106350; lo_106350 = 4294967295 & _106349; unsigned long hi_106352; hi_106352 = _106349 >> 32; unsigned int _106351; _106351 = (unsigned int)lo_106350; unsigned int _106353; _106353 = (unsigned int)hi_106352; unsigned int _106354; _106354 = _106351 ^ _106353; double _106355; _106355 = (double)_106354; double _106356; _106356 = 2.328306e-10 * _106355; theta_106359 = sqrt(_106356); ptheta_106359 = theta_106359; l106357: ; theta_106359 = ptheta_106359; unsigned long _106360; _106360 = 4294883355 * lo_106350; unsigned long _106361; _106361 = _106360 + hi_106352; unsigned long lo_106362; lo_106362 = 4294967295 & _106361; unsigned int _106363; _106363 = (unsigned int)lo_106362; unsigned long hi_106364; hi_106364 = _106361 >> 32; unsigned int _106365; _106365 = (unsigned int)hi_106364; unsigned int _106366; _106366 = _106363 ^ _106365; double _106367; _106367 = (double)_106366; double _106368; _106368 = 2.328306e-10 * _106367; double phi_106369; phi_106369 = 6.283185e+00 * _106368; _106372 = cos(phi_106369); p_106372 = _106372; l106370: ; _106372 = p_106372; _106375 = sin(phi_106369); p_106375 = _106375; l106373: ; _106375 = p_106375; double _106376; _106376 = theta_106359 * theta_106359; double _106377; _106377 = 1.000000e+00 - _106376; z_106380 = sqrt(_106377); pz_106380 = z_106380; l106378: ; z_106380 = pz_106380; double x_106381; x_106381 = _106372 * theta_106359; double y_106383; y_106383 = _106375 * theta_106359; double _106399; _106399 = z_106380 * _105163; double _106384; _106384 = y_106383 * _105280; double _106389; _106389 = x_106381 * _105192; double _106390; _106390 = y_106383 * _105291; double _106391; _106391 = _106389 + _106390; double _106386; _106386 = z_106380 * _105152; double _106392; _106392 = z_106380 * _105165; double _106396; _106396 = x_106381 * _105190; double _106382; _106382 = x_106381 * _105196; double _106397; _106397 = y_106383 * _105303; double _106385; _106385 = _106382 + _106384; double ry_106393; ry_106393 = _106391 + _106392; double rx_106387; rx_106387 = _106385 + _106386; double _106398; _106398 = _106396 + _106397; double _106394; _106394 = _105289 * ry_106393; double _106388; _106388 = _105276 * rx_106387; double rz_106400; rz_106400 = _106398 + _106399; double _106395; _106395 = _106388 + _106394; double _106401; _106401 = _105301 * rz_106400; double _106402; _106402 = _106395 + _106401; double _106403; _106403 = _106402 * _106402; double D_106404; D_106404 = _106403 - C_105317; bool _106405; _106405 = 0.000000e+00 < D_106404; if (_106405) goto l106406; else goto l107167; l107167: ; goto l107164; l106406: ; _106409 = sqrt(D_106404); p_106409 = _106409; l106407: ; _106409 = p_106409; double _106410; _106410 = -0.000000e+00 - _106402; double t_106411; t_106411 = _106410 - _106409; bool _106412; _106412 = 0.000000e+00 < t_106411; if (_106412) goto l106413; else goto l107166; l107166: ; goto l107163; l106413: ; bool _106414; _106414 = t_106411 < 1.000000e+17; if (_106414) goto l106415; else goto l107162; l107162: ; goto l107163; l107163: ; goto l107164; l107164: ; struct_Isect_9647 _107049_799; _107049_799.e0 = 1.000000e+17; // bottom: _107049_799.e1 = // bottom: struct_vec_9645 _107047_803;; // bottom: _107049_799.e2 = _107047_803; _107049_799.e3 = 0; pray_sphere_intersect_106443 = _107049_799; goto l106441; l106415: ; double _106425; _106425 = rz_106400 * t_106411; double _106416; _106416 = rx_106387 * t_106411; double _106417; _106417 = _105275 + _106416; double _106420; _106420 = ry_106393 * t_106411; double _106426; _106426 = _105300 + _106425; double _106418; _106418 = _106417 - -2.000000e+00; double _106421; _106421 = _105288 + _106420; double _106427; _106427 = _106426 - -3.500000e+00; double _106419; _106419 = _106418 * _106418; double _106422; _106422 = _106421 - 0.000000e+00; double _106428; _106428 = _106427 * _106427; double _106423; _106423 = _106422 * _106422; double _106424; _106424 = _106419 + _106423; double _106429; _106429 = _106424 + _106428; length_106432 = sqrt(_106429); plength_106432 = length_106432; l106430: ; length_106432 = plength_106432; _106435 = fabs(length_106432); p_106435 = _106435; l106433: ; _106435 = p_106435; bool _106436; _106436 = 1.000000e-17 < _106435; if (_106436) goto l106437; else goto l107160; l107160: ; struct_vec_9645 n_107161; n_107161.e0 = _106418; n_107161.e1 = _106422; n_107161.e2 = _106427; pvnormalize_106440 = n_107161; goto l106438; l106437: ; double _107157; _107157 = _106422 / length_106432; double _107158; _107158 = _106427 / length_106432; double _107156; _107156 = _106418 / length_106432; struct_vec_9645 _107159; _107159.e0 = _107156; _107159.e1 = _107157; _107159.e2 = _107158; pvnormalize_106440 = _107159; goto l106438; l106438: ; vnormalize_106440 = pvnormalize_106440; struct_vec_9645 p_107154; p_107154.e0 = _106417; p_107154.e1 = _106421; p_107154.e2 = _106426; struct_Isect_9647 isect_107155; isect_107155.e0 = t_106411; isect_107155.e1 = p_107154; isect_107155.e2 = vnormalize_106440; isect_107155.e3 = 1; pray_sphere_intersect_106443 = isect_107155; goto l106441; l106441: ; ray_sphere_intersect_106443 = pray_sphere_intersect_106443; double _106444; _106444 = _105358 * rx_106387; double _106446; _106446 = _105361 * rz_106400; double _106445; _106445 = _106444 + _106394; double _106447; _106447 = _106445 + _106446; double _106448; _106448 = _106447 * _106447; double D_106449; D_106449 = _106448 - C_105369; bool _106450; _106450 = 0.000000e+00 < D_106449; if (_106450) goto l106451; else goto l107153; l107153: ; goto l107150; l106451: ; _106454 = sqrt(D_106449); p_106454 = _106454; l106452: ; _106454 = p_106454; double _106455; _106455 = -0.000000e+00 - _106447; double t_106456; t_106456 = _106455 - _106454; bool _106457; _106457 = 0.000000e+00 < t_106456; if (_106457) goto l106458; else goto l107152; l107152: ; goto l107149; l106458: ; double _106459; _106459 = ray_sphere_intersect_106443.e0; bool _106460; _106460 = t_106456 < _106459; if (_106460) goto l106461; else goto l107148; l107148: ; goto l107149; l107149: ; goto l107150; l107150: ; pray_sphere_intersect_106489 = ray_sphere_intersect_106443; goto l106487; l106461: ; double _106462; _106462 = rx_106387 * t_106456; double _106471; _106471 = rz_106400 * t_106456; double _106463; _106463 = _105275 + _106462; double _106466; _106466 = ry_106393 * t_106456; double _106472; _106472 = _105300 + _106471; double _106467; _106467 = _105288 + _106466; double _106464; _106464 = _106463 - -5.000000e-01; double _106473; _106473 = _106472 - -3.000000e+00; double _106468; _106468 = _106467 - 0.000000e+00; double _106465; _106465 = _106464 * _106464; double _106474; _106474 = _106473 * _106473; double _106469; _106469 = _106468 * _106468; double _106470; _106470 = _106465 + _106469; double _106475; _106475 = _106470 + _106474; length_106478 = sqrt(_106475); plength_106478 = length_106478; l106476: ; length_106478 = plength_106478; _106481 = fabs(length_106478); p_106481 = _106481; l106479: ; _106481 = p_106481; bool _106482; _106482 = 1.000000e-17 < _106481; if (_106482) goto l106483; else goto l107146; l107146: ; struct_vec_9645 n_107147; n_107147.e0 = _106464; n_107147.e1 = _106468; n_107147.e2 = _106473; pvnormalize_106486 = n_107147; goto l106484; l106483: ; double _107143; _107143 = _106468 / length_106478; double _107144; _107144 = _106473 / length_106478; double _107142; _107142 = _106464 / length_106478; struct_vec_9645 _107145; _107145.e0 = _107142; _107145.e1 = _107143; _107145.e2 = _107144; pvnormalize_106486 = _107145; goto l106484; l106484: ; vnormalize_106486 = pvnormalize_106486; struct_vec_9645 p_107140; p_107140.e0 = _106463; p_107140.e1 = _106467; p_107140.e2 = _106472; struct_Isect_9647 isect_107141; isect_107141.e0 = t_106456; isect_107141.e1 = p_107140; isect_107141.e2 = vnormalize_106486; isect_107141.e3 = 1; pray_sphere_intersect_106489 = isect_107141; goto l106487; l106487: ; ray_sphere_intersect_106489 = pray_sphere_intersect_106489; double _106492; _106492 = _105414 * rz_106400; double _106490; _106490 = _105411 * rx_106387; double _106491; _106491 = _106490 + _106394; double _106493; _106493 = _106491 + _106492; double _106494; _106494 = _106493 * _106493; double D_106495; D_106495 = _106494 - C_105422; bool _106496; _106496 = 0.000000e+00 < D_106495; if (_106496) goto l106497; else goto l107139; l107139: ; goto l107136; l106497: ; _106500 = sqrt(D_106495); p_106500 = _106500; l106498: ; _106500 = p_106500; double _106501; _106501 = -0.000000e+00 - _106493; double t_106502; t_106502 = _106501 - _106500; bool _106503; _106503 = 0.000000e+00 < t_106502; if (_106503) goto l106504; else goto l107138; l107138: ; goto l107135; l106504: ; double _106505; _106505 = ray_sphere_intersect_106489.e0; bool _106506; _106506 = t_106502 < _106505; if (_106506) goto l106507; else goto l107134; l107134: ; goto l107135; l107135: ; goto l107136; l107136: ; pray_sphere_intersect_106535 = ray_sphere_intersect_106489; goto l106533; l106507: ; double _106517; _106517 = rz_106400 * t_106502; double _106518; _106518 = _105300 + _106517; double _106508; _106508 = rx_106387 * t_106502; double _106509; _106509 = _105275 + _106508; double _106512; _106512 = ry_106393 * t_106502; double _106513; _106513 = _105288 + _106512; double _106519; _106519 = _106518 - -2.200000e+00; double _106510; _106510 = _106509 - 1.000000e+00; double _106514; _106514 = _106513 - 0.000000e+00; double _106520; _106520 = _106519 * _106519; double _106511; _106511 = _106510 * _106510; double _106515; _106515 = _106514 * _106514; double _106516; _106516 = _106511 + _106515; double _106521; _106521 = _106516 + _106520; length_106524 = sqrt(_106521); plength_106524 = length_106524; l106522: ; length_106524 = plength_106524; _106527 = fabs(length_106524); p_106527 = _106527; l106525: ; _106527 = p_106527; bool _106528; _106528 = 1.000000e-17 < _106527; if (_106528) goto l106529; else goto l107132; l107132: ; struct_vec_9645 n_107133; n_107133.e0 = _106510; n_107133.e1 = _106514; n_107133.e2 = _106519; pvnormalize_106532 = n_107133; goto l106530; l106529: ; double _107129; _107129 = _106514 / length_106524; double _107128; _107128 = _106510 / length_106524; double _107130; _107130 = _106519 / length_106524; struct_vec_9645 _107131; _107131.e0 = _107128; _107131.e1 = _107129; _107131.e2 = _107130; pvnormalize_106532 = _107131; goto l106530; l106530: ; vnormalize_106532 = pvnormalize_106532; struct_vec_9645 p_107126; p_107126.e0 = _106509; p_107126.e1 = _106513; p_107126.e2 = _106518; struct_Isect_9647 isect_107127; isect_107127.e0 = t_106502; isect_107127.e1 = p_107126; isect_107127.e2 = vnormalize_106532; isect_107127.e3 = 1; pray_sphere_intersect_106535 = isect_107127; goto l106533; l106533: ; ray_sphere_intersect_106535 = pray_sphere_intersect_106535; double _106539; _106539 = 0.000000e+00 * rz_106400; double _106537; _106537 = 1.000000e+00 * ry_106393; double _106536; _106536 = 0.000000e+00 * rx_106387; double _106538; _106538 = _106536 + _106537; double _106540; _106540 = _106538 + _106539; _106543 = fabs(_106540); p_106543 = _106543; l106541: ; _106543 = p_106543; bool _106544; _106544 = 1.000000e-17 <= _106543; if (_106544) goto l106545; else goto l107125; l107125: ; goto l107122; l106545: ; double t_106546; t_106546 = _105480 / _106540; bool _106547; _106547 = 0.000000e+00 < t_106546; if (_106547) goto l106548; else goto l107124; l107124: ; goto l107121; l106548: ; double _106549; _106549 = ray_sphere_intersect_106535.e0; bool _106550; _106550 = t_106546 < _106549; if (_106550) goto l106551; else goto l107120; l107120: ; goto l107121; l107121: ; goto l107122; l107122: ; pray_plane_intersect_106554 = ray_sphere_intersect_106535; goto l106552; l106551: ; double _107116; _107116 = rz_106400 * t_106546; double _107112; _107112 = rx_106387 * t_106546; double _107114; _107114 = ry_106393 * t_106546; double _107113; _107113 = _105275 + _107112; double _107117; _107117 = _105300 + _107116; double _107115; _107115 = _105288 + _107114; struct_vec_9645 p_107118; p_107118.e0 = _107113; p_107118.e1 = _107115; p_107118.e2 = _107117; struct_vec_9645 _106999_878; _106999_878.e0 = 0.000000e+00; _106999_878.e1 = 1.000000e+00; _106999_878.e2 = 0.000000e+00; struct_Isect_9647 isect_107119; isect_107119.e0 = t_106546; isect_107119.e1 = p_107118; isect_107119.e2 = _106999_878; isect_107119.e3 = 1; pray_plane_intersect_106554 = isect_107119; goto l106552; l106552: ; ray_plane_intersect_106554 = pray_plane_intersect_106554; int _106555; _106555 = ray_plane_intersect_106554.e3; bool _106556; _106556 = _106555 == 1; if (_106556) goto l106557; else goto l107111; l107111: ; pocclusion_106560 = occlusion_106347; goto l106558; l106557: ; double _107110; _107110 = 1.000000e+00 + occlusion_106347; pocclusion_106560 = _107110; goto l106558; l106558: ; occlusion_106560 = pocclusion_106560; unsigned long _106561; _106561 = 4294883355 * lo_106362; unsigned long _106562; _106562 = _106561 + hi_106364; unsigned long lo_106563; lo_106563 = 4294967295 & _106562; unsigned long hi_106565; hi_106565 = _106562 >> 32; unsigned int _106564; _106564 = (unsigned int)lo_106563; unsigned int _106566; _106566 = (unsigned int)hi_106565; unsigned int _106567; _106567 = _106564 ^ _106566; double _106568; _106568 = (double)_106567; double _106569; _106569 = 2.328306e-10 * _106568; theta_106572 = sqrt(_106569); ptheta_106572 = theta_106572; l106570: ; theta_106572 = ptheta_106572; unsigned long _106573; _106573 = 4294883355 * lo_106563; unsigned long _106574; _106574 = _106573 + hi_106565; unsigned long lo_106575; lo_106575 = 4294967295 & _106574; unsigned long hi_106577; hi_106577 = _106574 >> 32; unsigned int _106576; _106576 = (unsigned int)lo_106575; unsigned int _106578; _106578 = (unsigned int)hi_106577; unsigned int _106579; _106579 = _106576 ^ _106578; double _106580; _106580 = (double)_106579; double _106581; _106581 = 2.328306e-10 * _106580; double phi_106582; phi_106582 = 6.283185e+00 * _106581; _106585 = cos(phi_106582); p_106585 = _106585; l106583: ; _106585 = p_106585; _106588 = sin(phi_106582); p_106588 = _106588; l106586: ; _106588 = p_106588; double _106589; _106589 = theta_106572 * theta_106572; double _106590; _106590 = 1.000000e+00 - _106589; z_106593 = sqrt(_106590); pz_106593 = z_106593; l106591: ; z_106593 = pz_106593; double _106612; _106612 = z_106593 * _105163; double y_106596; y_106596 = _106588 * theta_106572; double _106603; _106603 = y_106596 * _105291; double x_106594; x_106594 = _106585 * theta_106572; double _106599; _106599 = z_106593 * _105152; double _106605; _106605 = z_106593 * _105165; double _106610; _106610 = y_106596 * _105303; double _106597; _106597 = y_106596 * _105280; double _106609; _106609 = x_106594 * _105190; double _106595; _106595 = x_106594 * _105196; double _106602; _106602 = x_106594 * _105192; double _106611; _106611 = _106609 + _106610; double _106598; _106598 = _106595 + _106597; double _106604; _106604 = _106602 + _106603; double rz_106613; rz_106613 = _106611 + _106612; double rx_106600; rx_106600 = _106598 + _106599; double ry_106606; ry_106606 = _106604 + _106605; double _106614; _106614 = _105301 * rz_106613; double _106601; _106601 = _105276 * rx_106600; double _106607; _106607 = _105289 * ry_106606; double _106608; _106608 = _106601 + _106607; double _106615; _106615 = _106608 + _106614; double _106616; _106616 = _106615 * _106615; double D_106617; D_106617 = _106616 - C_105317; bool _106618; _106618 = 0.000000e+00 < D_106617; if (_106618) goto l106619; else goto l107109; l107109: ; goto l107106; l106619: ; _106622 = sqrt(D_106617); p_106622 = _106622; l106620: ; _106622 = p_106622; double _106623; _106623 = -0.000000e+00 - _106615; double t_106624; t_106624 = _106623 - _106622; bool _106625; _106625 = 0.000000e+00 < t_106624; if (_106625) goto l106626; else goto l107108; l107108: ; goto l107105; l106626: ; bool _106627; _106627 = t_106624 < 1.000000e+17; if (_106627) goto l106628; else goto l107104; l107104: ; goto l107105; l107105: ; goto l107106; l107106: ; struct_Isect_9647 _107049_907; _107049_907.e0 = 1.000000e+17; // bottom: _107049_907.e1 = // bottom: struct_vec_9645 _107047_911;; // bottom: _107049_907.e2 = _107047_911; _107049_907.e3 = 0; pray_sphere_intersect_106656 = _107049_907; goto l106654; l106628: ; double _106633; _106633 = ry_106606 * t_106624; double _106634; _106634 = _105288 + _106633; double _106629; _106629 = rx_106600 * t_106624; double _106638; _106638 = rz_106613 * t_106624; double _106635; _106635 = _106634 - 0.000000e+00; double _106630; _106630 = _105275 + _106629; double _106639; _106639 = _105300 + _106638; double _106636; _106636 = _106635 * _106635; double _106631; _106631 = _106630 - -2.000000e+00; double _106640; _106640 = _106639 - -3.500000e+00; double _106632; _106632 = _106631 * _106631; double _106641; _106641 = _106640 * _106640; double _106637; _106637 = _106632 + _106636; double _106642; _106642 = _106637 + _106641; length_106645 = sqrt(_106642); plength_106645 = length_106645; l106643: ; length_106645 = plength_106645; _106648 = fabs(length_106645); p_106648 = _106648; l106646: ; _106648 = p_106648; bool _106649; _106649 = 1.000000e-17 < _106648; if (_106649) goto l106650; else goto l107102; l107102: ; struct_vec_9645 n_107103; n_107103.e0 = _106631; n_107103.e1 = _106635; n_107103.e2 = _106640; pvnormalize_106653 = n_107103; goto l106651; l106650: ; double _107099; _107099 = _106635 / length_106645; double _107098; _107098 = _106631 / length_106645; double _107100; _107100 = _106640 / length_106645; struct_vec_9645 _107101; _107101.e0 = _107098; _107101.e1 = _107099; _107101.e2 = _107100; pvnormalize_106653 = _107101; goto l106651; l106651: ; vnormalize_106653 = pvnormalize_106653; struct_vec_9645 p_107096; p_107096.e0 = _106630; p_107096.e1 = _106634; p_107096.e2 = _106639; struct_Isect_9647 isect_107097; isect_107097.e0 = t_106624; isect_107097.e1 = p_107096; isect_107097.e2 = vnormalize_106653; isect_107097.e3 = 1; pray_sphere_intersect_106656 = isect_107097; goto l106654; l106654: ; ray_sphere_intersect_106656 = pray_sphere_intersect_106656; double _106659; _106659 = _105361 * rz_106613; double _106657; _106657 = _105358 * rx_106600; double _106658; _106658 = _106657 + _106607; double _106660; _106660 = _106658 + _106659; double _106661; _106661 = _106660 * _106660; double D_106662; D_106662 = _106661 - C_105369; bool _106663; _106663 = 0.000000e+00 < D_106662; if (_106663) goto l106664; else goto l107095; l107095: ; goto l107092; l106664: ; _106667 = sqrt(D_106662); p_106667 = _106667; l106665: ; _106667 = p_106667; double _106668; _106668 = -0.000000e+00 - _106660; double t_106669; t_106669 = _106668 - _106667; bool _106670; _106670 = 0.000000e+00 < t_106669; if (_106670) goto l106671; else goto l107094; l107094: ; goto l107091; l106671: ; double _106672; _106672 = ray_sphere_intersect_106656.e0; bool _106673; _106673 = t_106669 < _106672; if (_106673) goto l106674; else goto l107090; l107090: ; goto l107091; l107091: ; goto l107092; l107092: ; pray_sphere_intersect_106702 = ray_sphere_intersect_106656; goto l106700; l106674: ; double _106684; _106684 = rz_106613 * t_106669; double _106675; _106675 = rx_106600 * t_106669; double _106679; _106679 = ry_106606 * t_106669; double _106685; _106685 = _105300 + _106684; double _106676; _106676 = _105275 + _106675; double _106680; _106680 = _105288 + _106679; double _106686; _106686 = _106685 - -3.000000e+00; double _106677; _106677 = _106676 - -5.000000e-01; double _106681; _106681 = _106680 - 0.000000e+00; double _106687; _106687 = _106686 * _106686; double _106678; _106678 = _106677 * _106677; double _106682; _106682 = _106681 * _106681; double _106683; _106683 = _106678 + _106682; double _106688; _106688 = _106683 + _106687; length_106691 = sqrt(_106688); plength_106691 = length_106691; l106689: ; length_106691 = plength_106691; _106694 = fabs(length_106691); p_106694 = _106694; l106692: ; _106694 = p_106694; bool _106695; _106695 = 1.000000e-17 < _106694; if (_106695) goto l106696; else goto l107088; l107088: ; struct_vec_9645 n_107089; n_107089.e0 = _106677; n_107089.e1 = _106681; n_107089.e2 = _106686; pvnormalize_106699 = n_107089; goto l106697; l106696: ; double _107084; _107084 = _106677 / length_106691; double _107085; _107085 = _106681 / length_106691; double _107086; _107086 = _106686 / length_106691; struct_vec_9645 _107087; _107087.e0 = _107084; _107087.e1 = _107085; _107087.e2 = _107086; pvnormalize_106699 = _107087; goto l106697; l106697: ; vnormalize_106699 = pvnormalize_106699; struct_vec_9645 p_107082; p_107082.e0 = _106676; p_107082.e1 = _106680; p_107082.e2 = _106685; struct_Isect_9647 isect_107083; isect_107083.e0 = t_106669; isect_107083.e1 = p_107082; isect_107083.e2 = vnormalize_106699; isect_107083.e3 = 1; pray_sphere_intersect_106702 = isect_107083; goto l106700; l106700: ; ray_sphere_intersect_106702 = pray_sphere_intersect_106702; double _106705; _106705 = _105414 * rz_106613; double _106703; _106703 = _105411 * rx_106600; double _106704; _106704 = _106703 + _106607; double _106706; _106706 = _106704 + _106705; double _106707; _106707 = _106706 * _106706; double D_106708; D_106708 = _106707 - C_105422; bool _106709; _106709 = 0.000000e+00 < D_106708; if (_106709) goto l106710; else goto l107081; l107081: ; goto l107078; l106710: ; _106713 = sqrt(D_106708); p_106713 = _106713; l106711: ; _106713 = p_106713; double _106714; _106714 = -0.000000e+00 - _106706; double t_106715; t_106715 = _106714 - _106713; bool _106716; _106716 = 0.000000e+00 < t_106715; if (_106716) goto l106717; else goto l107080; l107080: ; goto l107077; l106717: ; double _106718; _106718 = ray_sphere_intersect_106702.e0; bool _106719; _106719 = t_106715 < _106718; if (_106719) goto l106720; else goto l107076; l107076: ; goto l107077; l107077: ; goto l107078; l107078: ; pray_sphere_intersect_106748 = ray_sphere_intersect_106702; goto l106746; l106720: ; double _106730; _106730 = rz_106613 * t_106715; double _106721; _106721 = rx_106600 * t_106715; double _106725; _106725 = ry_106606 * t_106715; double _106726; _106726 = _105288 + _106725; double _106727; _106727 = _106726 - 0.000000e+00; double _106722; _106722 = _105275 + _106721; double _106723; _106723 = _106722 - 1.000000e+00; double _106731; _106731 = _105300 + _106730; double _106732; _106732 = _106731 - -2.200000e+00; double _106728; _106728 = _106727 * _106727; double _106724; _106724 = _106723 * _106723; double _106733; _106733 = _106732 * _106732; double _106729; _106729 = _106724 + _106728; double _106734; _106734 = _106729 + _106733; length_106737 = sqrt(_106734); plength_106737 = length_106737; l106735: ; length_106737 = plength_106737; _106740 = fabs(length_106737); p_106740 = _106740; l106738: ; _106740 = p_106740; bool _106741; _106741 = 1.000000e-17 < _106740; if (_106741) goto l106742; else goto l107074; l107074: ; struct_vec_9645 n_107075; n_107075.e0 = _106723; n_107075.e1 = _106727; n_107075.e2 = _106732; pvnormalize_106745 = n_107075; goto l106743; l106742: ; double _107072; _107072 = _106732 / length_106737; double _107070; _107070 = _106723 / length_106737; double _107071; _107071 = _106727 / length_106737; struct_vec_9645 _107073; _107073.e0 = _107070; _107073.e1 = _107071; _107073.e2 = _107072; pvnormalize_106745 = _107073; goto l106743; l106743: ; vnormalize_106745 = pvnormalize_106745; struct_vec_9645 p_107068; p_107068.e0 = _106722; p_107068.e1 = _106726; p_107068.e2 = _106731; struct_Isect_9647 isect_107069; isect_107069.e0 = t_106715; isect_107069.e1 = p_107068; isect_107069.e2 = vnormalize_106745; isect_107069.e3 = 1; pray_sphere_intersect_106748 = isect_107069; goto l106746; l106746: ; ray_sphere_intersect_106748 = pray_sphere_intersect_106748; double _106749; _106749 = 0.000000e+00 * rx_106600; double _106752; _106752 = 0.000000e+00 * rz_106613; double _106750; _106750 = 1.000000e+00 * ry_106606; double _106751; _106751 = _106749 + _106750; double _106753; _106753 = _106751 + _106752; _106756 = fabs(_106753); p_106756 = _106756; l106754: ; _106756 = p_106756; bool _106757; _106757 = 1.000000e-17 <= _106756; if (_106757) goto l106758; else goto l107067; l107067: ; goto l107064; l106758: ; double t_106759; t_106759 = _105480 / _106753; bool _106760; _106760 = 0.000000e+00 < t_106759; if (_106760) goto l106761; else goto l107066; l107066: ; goto l107063; l106761: ; double _106762; _106762 = ray_sphere_intersect_106748.e0; bool _106763; _106763 = t_106759 < _106762; if (_106763) goto l106764; else goto l107062; l107062: ; goto l107063; l107063: ; goto l107064; l107064: ; pray_plane_intersect_106767 = ray_sphere_intersect_106748; goto l106765; l106764: ; double _107054; _107054 = rx_106600 * t_106759; double _107056; _107056 = ry_106606 * t_106759; double _107055; _107055 = _105275 + _107054; double _107057; _107057 = _105288 + _107056; double _107058; _107058 = rz_106613 * t_106759; double _107059; _107059 = _105300 + _107058; struct_vec_9645 p_107060; p_107060.e0 = _107055; p_107060.e1 = _107057; p_107060.e2 = _107059; struct_vec_9645 _106999_986; _106999_986.e0 = 0.000000e+00; _106999_986.e1 = 1.000000e+00; _106999_986.e2 = 0.000000e+00; struct_Isect_9647 isect_107061; isect_107061.e0 = t_106759; isect_107061.e1 = p_107060; isect_107061.e2 = _106999_986; isect_107061.e3 = 1; pray_plane_intersect_106767 = isect_107061; goto l106765; l106765: ; ray_plane_intersect_106767 = pray_plane_intersect_106767; int _106768; _106768 = ray_plane_intersect_106767.e3; bool _106769; _106769 = _106768 == 1; if (_106769) goto l106770; else goto l107053; l107053: ; pocclusion_106773 = occlusion_106560; goto l106771; l106770: ; double _107052; _107052 = 1.000000e+00 + occlusion_106560; pocclusion_106773 = _107052; goto l106771; l106771: ; occlusion_106773 = pocclusion_106773; unsigned long _106774; _106774 = 4294883355 * lo_106575; unsigned long _106775; _106775 = _106774 + hi_106577; unsigned long hi_106778; hi_106778 = _106775 >> 32; unsigned long lo_106776; lo_106776 = 4294967295 & _106775; unsigned int _106779; _106779 = (unsigned int)hi_106778; unsigned int _106777; _106777 = (unsigned int)lo_106776; unsigned int _106780; _106780 = _106777 ^ _106779; double _106781; _106781 = (double)_106780; double _106782; _106782 = 2.328306e-10 * _106781; theta_106785 = sqrt(_106782); ptheta_106785 = theta_106785; l106783: ; theta_106785 = ptheta_106785; unsigned long _106786; _106786 = 4294883355 * lo_106776; unsigned long _106787; _106787 = _106786 + hi_106778; unsigned long hi_106790; hi_106790 = _106787 >> 32; unsigned long lo_106788; lo_106788 = 4294967295 & _106787; unsigned int _106791; _106791 = (unsigned int)hi_106790; unsigned int _106789; _106789 = (unsigned int)lo_106788; unsigned int _106792; _106792 = _106789 ^ _106791; double _106793; _106793 = (double)_106792; double _106794; _106794 = 2.328306e-10 * _106793; double phi_106795; phi_106795 = 6.283185e+00 * _106794; _106798 = cos(phi_106795); p_106798 = _106798; l106796: ; _106798 = p_106798; _106801 = sin(phi_106795); p_106801 = _106801; l106799: ; _106801 = p_106801; double _106802; _106802 = theta_106785 * theta_106785; double _106803; _106803 = 1.000000e+00 - _106802; z_106806 = sqrt(_106803); pz_106806 = z_106806; l106804: ; z_106806 = pz_106806; double x_106807; x_106807 = _106798 * theta_106785; double y_106809; y_106809 = _106801 * theta_106785; double _106815; _106815 = x_106807 * _105192; double _106823; _106823 = y_106809 * _105303; double _106812; _106812 = z_106806 * _105152; double _106810; _106810 = y_106809 * _105280; double _106822; _106822 = x_106807 * _105190; double _106824; _106824 = _106822 + _106823; double _106825; _106825 = z_106806 * _105163; double _106818; _106818 = z_106806 * _105165; double _106816; _106816 = y_106809 * _105291; double _106808; _106808 = x_106807 * _105196; double _106817; _106817 = _106815 + _106816; double _106811; _106811 = _106808 + _106810; double rz_106826; rz_106826 = _106824 + _106825; double ry_106819; ry_106819 = _106817 + _106818; double rx_106813; rx_106813 = _106811 + _106812; double _106827; _106827 = _105301 * rz_106826; double _106820; _106820 = _105289 * ry_106819; double _106814; _106814 = _105276 * rx_106813; double _106821; _106821 = _106814 + _106820; double _106828; _106828 = _106821 + _106827; double _106829; _106829 = _106828 * _106828; double D_106830; D_106830 = _106829 - C_105317; bool _106831; _106831 = 0.000000e+00 < D_106830; if (_106831) goto l106832; else goto l107051; l107051: ; goto l107045; l106832: ; _106835 = sqrt(D_106830); p_106835 = _106835; l106833: ; _106835 = p_106835; double _106836; _106836 = -0.000000e+00 - _106828; double t_106837; t_106837 = _106836 - _106835; bool _106838; _106838 = 0.000000e+00 < t_106837; if (_106838) goto l106839; else goto l107050; l107050: ; goto l107044; l106839: ; bool _106840; _106840 = t_106837 < 1.000000e+17; if (_106840) goto l106841; else goto l107043; l107043: ; goto l107044; l107044: ; goto l107045; l107045: ; struct_Isect_9647 _107049_1015; _107049_1015.e0 = 1.000000e+17; // bottom: _107049_1015.e1 = // bottom: struct_vec_9645 _107047_1019;; // bottom: _107049_1015.e2 = _107047_1019; _107049_1015.e3 = 0; pray_sphere_intersect_106869 = _107049_1015; goto l106867; l106841: ; double _106846; _106846 = ry_106819 * t_106837; double _106847; _106847 = _105288 + _106846; double _106842; _106842 = rx_106813 * t_106837; double _106851; _106851 = rz_106826 * t_106837; double _106848; _106848 = _106847 - 0.000000e+00; double _106843; _106843 = _105275 + _106842; double _106852; _106852 = _105300 + _106851; double _106849; _106849 = _106848 * _106848; double _106844; _106844 = _106843 - -2.000000e+00; double _106853; _106853 = _106852 - -3.500000e+00; double _106845; _106845 = _106844 * _106844; double _106854; _106854 = _106853 * _106853; double _106850; _106850 = _106845 + _106849; double _106855; _106855 = _106850 + _106854; length_106858 = sqrt(_106855); plength_106858 = length_106858; l106856: ; length_106858 = plength_106858; _106861 = fabs(length_106858); p_106861 = _106861; l106859: ; _106861 = p_106861; bool _106862; _106862 = 1.000000e-17 < _106861; if (_106862) goto l106863; else goto l107041; l107041: ; struct_vec_9645 n_107042; n_107042.e0 = _106844; n_107042.e1 = _106848; n_107042.e2 = _106853; pvnormalize_106866 = n_107042; goto l106864; l106863: ; double _107038; _107038 = _106848 / length_106858; double _107039; _107039 = _106853 / length_106858; double _107037; _107037 = _106844 / length_106858; struct_vec_9645 _107040; _107040.e0 = _107037; _107040.e1 = _107038; _107040.e2 = _107039; pvnormalize_106866 = _107040; goto l106864; l106864: ; vnormalize_106866 = pvnormalize_106866; struct_vec_9645 p_107035; p_107035.e0 = _106843; p_107035.e1 = _106847; p_107035.e2 = _106852; struct_Isect_9647 isect_107036; isect_107036.e0 = t_106837; isect_107036.e1 = p_107035; isect_107036.e2 = vnormalize_106866; isect_107036.e3 = 1; pray_sphere_intersect_106869 = isect_107036; goto l106867; l106867: ; ray_sphere_intersect_106869 = pray_sphere_intersect_106869; double _106872; _106872 = _105361 * rz_106826; double _106870; _106870 = _105358 * rx_106813; double _106871; _106871 = _106870 + _106820; double _106873; _106873 = _106871 + _106872; double _106874; _106874 = _106873 * _106873; double D_106875; D_106875 = _106874 - C_105369; bool _106876; _106876 = 0.000000e+00 < D_106875; if (_106876) goto l106877; else goto l107034; l107034: ; goto l107031; l106877: ; _106880 = sqrt(D_106875); p_106880 = _106880; l106878: ; _106880 = p_106880; double _106881; _106881 = -0.000000e+00 - _106873; double t_106882; t_106882 = _106881 - _106880; bool _106883; _106883 = 0.000000e+00 < t_106882; if (_106883) goto l106884; else goto l107033; l107033: ; goto l107030; l106884: ; double _106885; _106885 = ray_sphere_intersect_106869.e0; bool _106886; _106886 = t_106882 < _106885; if (_106886) goto l106887; else goto l107029; l107029: ; goto l107030; l107030: ; goto l107031; l107031: ; pray_sphere_intersect_106915 = ray_sphere_intersect_106869; goto l106913; l106887: ; double _106892; _106892 = ry_106819 * t_106882; double _106888; _106888 = rx_106813 * t_106882; double _106889; _106889 = _105275 + _106888; double _106890; _106890 = _106889 - -5.000000e-01; double _106897; _106897 = rz_106826 * t_106882; double _106893; _106893 = _105288 + _106892; double _106891; _106891 = _106890 * _106890; double _106898; _106898 = _105300 + _106897; double _106894; _106894 = _106893 - 0.000000e+00; double _106899; _106899 = _106898 - -3.000000e+00; double _106895; _106895 = _106894 * _106894; double _106900; _106900 = _106899 * _106899; double _106896; _106896 = _106891 + _106895; double _106901; _106901 = _106896 + _106900; length_106904 = sqrt(_106901); plength_106904 = length_106904; l106902: ; length_106904 = plength_106904; _106907 = fabs(length_106904); p_106907 = _106907; l106905: ; _106907 = p_106907; bool _106908; _106908 = 1.000000e-17 < _106907; if (_106908) goto l106909; else goto l107027; l107027: ; struct_vec_9645 n_107028; n_107028.e0 = _106890; n_107028.e1 = _106894; n_107028.e2 = _106899; pvnormalize_106912 = n_107028; goto l106910; l106909: ; double _107023; _107023 = _106890 / length_106904; double _107024; _107024 = _106894 / length_106904; double _107025; _107025 = _106899 / length_106904; struct_vec_9645 _107026; _107026.e0 = _107023; _107026.e1 = _107024; _107026.e2 = _107025; pvnormalize_106912 = _107026; goto l106910; l106910: ; vnormalize_106912 = pvnormalize_106912; struct_vec_9645 p_107021; p_107021.e0 = _106889; p_107021.e1 = _106893; p_107021.e2 = _106898; struct_Isect_9647 isect_107022; isect_107022.e0 = t_106882; isect_107022.e1 = p_107021; isect_107022.e2 = vnormalize_106912; isect_107022.e3 = 1; pray_sphere_intersect_106915 = isect_107022; goto l106913; l106913: ; ray_sphere_intersect_106915 = pray_sphere_intersect_106915; double _106918; _106918 = _105414 * rz_106826; double _106916; _106916 = _105411 * rx_106813; double _106917; _106917 = _106916 + _106820; double _106919; _106919 = _106917 + _106918; double _106920; _106920 = _106919 * _106919; double D_106921; D_106921 = _106920 - C_105422; bool _106922; _106922 = 0.000000e+00 < D_106921; if (_106922) goto l106923; else goto l107020; l107020: ; goto l107017; l106923: ; _106926 = sqrt(D_106921); p_106926 = _106926; l106924: ; _106926 = p_106926; double _106927; _106927 = -0.000000e+00 - _106919; double t_106928; t_106928 = _106927 - _106926; bool _106929; _106929 = 0.000000e+00 < t_106928; if (_106929) goto l106930; else goto l107019; l107019: ; goto l107016; l106930: ; double _106931; _106931 = ray_sphere_intersect_106915.e0; bool _106932; _106932 = t_106928 < _106931; if (_106932) goto l106933; else goto l107015; l107015: ; goto l107016; l107016: ; goto l107017; l107017: ; pray_sphere_intersect_106961 = ray_sphere_intersect_106915; goto l106959; l106933: ; double _106934; _106934 = rx_106813 * t_106928; double _106943; _106943 = rz_106826 * t_106928; double _106938; _106938 = ry_106819 * t_106928; double _106944; _106944 = _105300 + _106943; double _106935; _106935 = _105275 + _106934; double _106939; _106939 = _105288 + _106938; double _106945; _106945 = _106944 - -2.200000e+00; double _106936; _106936 = _106935 - 1.000000e+00; double _106940; _106940 = _106939 - 0.000000e+00; double _106946; _106946 = _106945 * _106945; double _106937; _106937 = _106936 * _106936; double _106941; _106941 = _106940 * _106940; double _106942; _106942 = _106937 + _106941; double _106947; _106947 = _106942 + _106946; length_106950 = sqrt(_106947); plength_106950 = length_106950; l106948: ; length_106950 = plength_106950; _106953 = fabs(length_106950); p_106953 = _106953; l106951: ; _106953 = p_106953; bool _106954; _106954 = 1.000000e-17 < _106953; if (_106954) goto l106955; else goto l107013; l107013: ; struct_vec_9645 n_107014; n_107014.e0 = _106936; n_107014.e1 = _106940; n_107014.e2 = _106945; pvnormalize_106958 = n_107014; goto l106956; l106955: ; double _107011; _107011 = _106945 / length_106950; double _107010; _107010 = _106940 / length_106950; double _107009; _107009 = _106936 / length_106950; struct_vec_9645 _107012; _107012.e0 = _107009; _107012.e1 = _107010; _107012.e2 = _107011; pvnormalize_106958 = _107012; goto l106956; l106956: ; vnormalize_106958 = pvnormalize_106958; struct_vec_9645 p_107007; p_107007.e0 = _106935; p_107007.e1 = _106939; p_107007.e2 = _106944; struct_Isect_9647 isect_107008; isect_107008.e0 = t_106928; isect_107008.e1 = p_107007; isect_107008.e2 = vnormalize_106958; isect_107008.e3 = 1; pray_sphere_intersect_106961 = isect_107008; goto l106959; l106959: ; ray_sphere_intersect_106961 = pray_sphere_intersect_106961; double _106965; _106965 = 0.000000e+00 * rz_106826; double _106963; _106963 = 1.000000e+00 * ry_106819; double _106962; _106962 = 0.000000e+00 * rx_106813; double _106964; _106964 = _106962 + _106963; double _106966; _106966 = _106964 + _106965; _106969 = fabs(_106966); p_106969 = _106969; l106967: ; _106969 = p_106969; bool _106970; _106970 = 1.000000e-17 <= _106969; if (_106970) goto l106971; else goto l107006; l107006: ; goto l107003; l106971: ; double t_106972; t_106972 = _105480 / _106966; bool _106973; _106973 = 0.000000e+00 < t_106972; if (_106973) goto l106974; else goto l107005; l107005: ; goto l107002; l106974: ; double _106975; _106975 = ray_sphere_intersect_106961.e0; bool _106976; _106976 = t_106972 < _106975; if (_106976) goto l106977; else goto l107001; l107001: ; goto l107002; l107002: ; goto l107003; l107003: ; pray_plane_intersect_106980 = ray_sphere_intersect_106961; goto l106978; l106977: ; double _106996; _106996 = rz_106826 * t_106972; double _106992; _106992 = rx_106813 * t_106972; double _106997; _106997 = _105300 + _106996; double _106994; _106994 = ry_106819 * t_106972; double _106993; _106993 = _105275 + _106992; double _106995; _106995 = _105288 + _106994; struct_vec_9645 p_106998; p_106998.e0 = _106993; p_106998.e1 = _106995; p_106998.e2 = _106997; struct_vec_9645 _106999_1094; _106999_1094.e0 = 0.000000e+00; _106999_1094.e1 = 1.000000e+00; _106999_1094.e2 = 0.000000e+00; struct_Isect_9647 isect_107000; isect_107000.e0 = t_106972; isect_107000.e1 = p_106998; isect_107000.e2 = _106999_1094; isect_107000.e3 = 1; pray_plane_intersect_106980 = isect_107000; goto l106978; l106978: ; ray_plane_intersect_106980 = pray_plane_intersect_106980; int _106981; _106981 = ray_plane_intersect_106980.e3; bool _106982; _106982 = _106981 == 1; if (_106982) goto l106983; else goto l106991; l106991: ; pocclusion_106986 = occlusion_106773; goto l106984; l106983: ; double _106990; _106990 = 1.000000e+00 + occlusion_106773; pocclusion_106986 = _106990; goto l106984; l106984: ; occlusion_106986 = pocclusion_106986; int _106987; _106987 = lower_105220 + step_105222; unsigned long _106988; _106988 = 4294883355 * lo_106788; unsigned long _106989; _106989 = _106988 + hi_106790; plower_105220 = _106987; pupper_105221 = upper_105221; pstep_105222 = step_105222; pocclusion_105223 = occlusion_106986; pstate_105224 = _106989; goto l105218; } }
21,514
#include "FluidGPU-unidyn.cuh" #include <cmath> #include <cuda_runtime.h> #include <iostream> #include <thrust/sort.h> #include <device_launch_parameters.h> //#include <device_functions.h> #include <cuda_runtime_api.h> #include <cuda.h> float kernel(float r) { if (r >= 0 && r <= cutoff) { return 1. / 3.14159 / (powf(cutoff, 3))*(1 - 3. / 2. * powf((r / cutoff), 2) + 3. / 4. * powf((r / cutoff), 3)); } else if (r > cutoff && r < (2 * cutoff)) { return 1. / 3.14159 / (powf(cutoff, 3)) * 1 / 4. * powf(2 - (r / cutoff), 3); } else { return 0; } } float kernel_test(float r) { if (r >= 0 && r <= cutoff) { return 1. / 3.14159 / (powf(cutoff, 4))*(1 - 3. * powf((r / cutoff), 1) + 9. / 4. * powf((r / cutoff), 2)); } else if (r > cutoff && r < (2 * cutoff)) { return -1. / 3.14159 / (powf(cutoff, 4)) * 1 / 2. * powf(2 - (r / cutoff), 2); } else { return 0; } } float kernel_derivative(float r) { if (r < cutoff) { return -45.0 / 3.14159 / powf(cutoff, 6)*powf((cutoff - r), 2); } else { return 0; } } //Dot product inline float dot_prod(float x1, float y1, float z1, float x2, float y2, float z2) { return x1*x2 + y1*y2 + z1*z2; } //Cross products inline float cross_prod_x(float x1, float y1, float z1, float x2, float y2, float z2) { return y1*z2 - z1*y2; } inline float cross_prod_y(float x1, float y1, float z1, float x2, float y2, float z2) { return -x1*z2 + z1*x2; } inline float cross_prod_z(float x1, float y1, float z1, float x2, float y2, float z2) { return x1*y2 - y1*x2; } __device__ int morton(unsigned int x, unsigned int y, unsigned int z) { //int x = (bidx / GRIDSIZE / GRIDSIZE); //int y = (bidx / GRIDSIZE % GRIDSIZE); //int z = (bidx % GRIDSIZE); x = (x | (x << 16)) & 0x030000FF; x = (x | (x << 8)) & 0x0300F00F; x = (x | (x << 4)) & 0x030C30C3; x = (x | (x << 2)) & 0x09249249; y = (y | (y << 16)) & 0x030000FF; y = (y | (y << 8)) & 0x0300F00F; y = (y | (y << 4)) & 0x030C30C3; y = (y | (y << 2)) & 0x09249249; z = (z | (z << 16)) & 0x030000FF; z = (z | (z << 8)) & 0x0300F00F; z = (z | (z << 4)) & 0x030C30C3; z = (z | (z << 2)) & 0x09249249; return x | (y << 1) | (z << 2); } __device__ inline int demorton(unsigned int x, int b) { //b should be 0 for x, 1 for y, 2 for z switch (b) { case 0: break; case 1: x = (x >> 1); break; case 2: x = (x >> 2); break; } x &= 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0 x = (x | (x >> 2)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10 x = (x | (x >> 4)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210 x = (x | (x >> 8)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210 x = (x | (x >> 16)) & 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210 return x; } __global__ void findneighbours(int *cell, int *start, int *start_copy, int *end, int nspts, int x) { int idx = blockIdx.x * blockDim.x + threadIdx.x; //if (idx<1) {printf("%d, %d, %d\n",idx, cell[idx],start[cell[idx]-x]); } if (idx < nspts) { if (idx == 0 || cell[idx] != cell[idx - 1] ) { start[cell[idx]-x] = idx; start_copy[cell[idx]-x] = idx; } if (idx == nspts-1 || cell[idx] != cell[idx + 1] ) { end[cell[idx]-x] = idx; } } } __global__ void force_calc_coarse(Particle *SPptr, int *particleindex, int *cell, int *start, int *end, int *split, int nspts,int x, int dev, int buffer, int *numsplit) { //x: start cellnumber int idx = blockIdx.x * blockDim.x + threadIdx.x; int bidx = blockIdx.x; int tidx = threadIdx.x; const int threadsperblockmax = 2048; int nb[27] = { -GRIDSIZE*GRIDSIZE - GRIDSIZE - 1, -GRIDSIZE*GRIDSIZE - GRIDSIZE,-GRIDSIZE*GRIDSIZE - GRIDSIZE + 1, -GRIDSIZE*GRIDSIZE - 1, -GRIDSIZE*GRIDSIZE, -GRIDSIZE*GRIDSIZE + 1, -GRIDSIZE*GRIDSIZE + GRIDSIZE - 1, -GRIDSIZE*GRIDSIZE + GRIDSIZE, -GRIDSIZE*GRIDSIZE + GRIDSIZE + 1, -GRIDSIZE - 1, -GRIDSIZE,-GRIDSIZE + 1, -1, 0, +1, GRIDSIZE - 1, GRIDSIZE, GRIDSIZE + 1, GRIDSIZE*GRIDSIZE - GRIDSIZE - 1, GRIDSIZE*GRIDSIZE - GRIDSIZE,GRIDSIZE*GRIDSIZE - GRIDSIZE + 1, GRIDSIZE*GRIDSIZE - 1, GRIDSIZE*GRIDSIZE, GRIDSIZE*GRIDSIZE + 1, GRIDSIZE*GRIDSIZE + GRIDSIZE - 1, GRIDSIZE*GRIDSIZE + GRIDSIZE, GRIDSIZE*GRIDSIZE + GRIDSIZE + 1 }; __shared__ short int p[27]; __shared__ short int pidx[27]; int __shared__ sum[threadsperblockmax];// = 0; int __shared__ jj[threadsperblockmax];// = 0; volatile __shared__ int total; volatile __shared__ int blockpop; if (idx < threadsperblockmax) { sum[idx] = 6500; jj[idx] = 6500; } __syncthreads(); if (tidx < 27) { p[tidx] = 0; } if (bidx < x && start[bidx] >= 0) { if (tidx < 27 && bidx+ nb[tidx] >= 0 && bidx + nb[tidx] < x+buffer && start[bidx + nb[tidx]] >= 0 &&end[bidx + nb[tidx]] >= 0 && start[bidx + nb[tidx]] < nspts && 1 + end[bidx + nb[tidx]] - start[bidx + nb[tidx]] > 0 ) { p[tidx] = 1 +end[bidx + nb[tidx]] - start[bidx + nb[tidx]]; //count population of neighbour cells so we know how many threads to use pidx[tidx] = tidx; } if (tidx == 13) { blockpop = p[tidx]; } } else { if (tidx == 13) { blockpop = 0; } } __syncthreads(); for (int c = 0; c<blockpop;c+=64){ if (blockpop > MAXBLOCKPOP && tidx+c < blockpop && bidx < x && start[bidx] >= 0 && particleindex[start[bidx]] >= 0){ SPptr[particleindex[start[bidx]+tidx+c]].subindex = 1-(int((SPptr[particleindex[start[bidx]+tidx+c]].xcoord-XMIN)/CELLSIZE) == int((SPptr[particleindex[start[bidx]+tidx+c]].xcoord-XMIN+CELLSIZE/2)/CELLSIZE)) + 2 - 2*(int((SPptr[particleindex[start[bidx]+tidx+c]].ycoord-YMIN)/CELLSIZE) == int((SPptr[particleindex[start[bidx]+tidx+c]].ycoord-YMIN+CELLSIZE/2)/CELLSIZE)) +4*(int((SPptr[particleindex[start[bidx]+tidx+c]].zcoord-ZMIN)/CELLSIZE) == int((SPptr[particleindex[start[bidx]+tidx+c]].zcoord-ZMIN+CELLSIZE/2)/CELLSIZE)); if (tidx==0){ split[bidx] = bidx; atomicAdd(&(numsplit[0]), 1); } } } __syncthreads(); if (bidx < x && start[bidx] >= 0) { if (tidx == 0) { total = 0; for (int i = 0; i < 27; i++) { if (p[i]>0 && bidx + nb[i] >= 0 && bidx + nb[i] < x && start[bidx + nb[i]] >= 0 &&end[bidx + nb[i]] >= 0 && start[bidx + nb[i]] < nspts) { total += p[i]; } } } } else { if (tidx == 0) {total = 0; } } __syncthreads(); if (bidx<x && start[bidx] >= 0) { if (tidx == 0) { int count = 0; for (int i = 0; i < 27; i++) { if (p[i] != 0) { p[count++] = p[i]; pidx[count - 1] = pidx[i]; //sort } } while (count < 27) { p[count++] = 0; pidx[count - 1] = 0; } } } __syncthreads(); if (bidx<x && start[bidx] >= 0) { for (int c = 0; c<total;c+=256){ if (tidx +c< total) { sum[tidx+c] = 0; jj[tidx+c] = 0; while (tidx+c + 1 > sum[tidx+c] && jj[tidx+c] < 27) { sum[tidx+c] += p[jj[tidx+c]]; jj[tidx+c]++; } } } } __syncthreads(); if (bidx<x && start[bidx] >= 0 && particleindex[start[bidx]] >= 0 && split[bidx] ==-1 && particleindex[start[bidx]] < nspts) { //should exclude the buffer here for (int c = 0; c<total;c+=256){ //only execute 64 threads at once if (tidx +c < total && bidx + nb[pidx[jj[tidx+c] - 1]] >= 0 && bidx + nb[pidx[jj[tidx+c] - 1]] < x) { /////////////////////////////////////////////////////////////////// volatile int j = particleindex[start[bidx + nb[pidx[jj[tidx+c] - 1]]] + sum[tidx+c] - (tidx+c + 1)]; if (particleindex[start[bidx + nb[pidx[jj[tidx+c] - 1]]]] >= 0 && j < nspts && j >= 0) { for (volatile int i = start[bidx]; i <= end[bidx]; i++) { volatile int ii = particleindex[i]; float ds = (SPptr[ii]).distance((SPptr[j])); //Particle merging if (ds <= (MERGEDIST) && ds > 0 && SPptr[ii].mass>0 && SPptr[j].mass> 0 && SPptr[ii].mass<2 && SPptr[j].mass<2 && !SPptr[ii].boundary &&!SPptr[j].boundary && powf(SPptr[ii].diffusionx,2)+powf(SPptr[ii].diffusiony,2)+powf(SPptr[ii].diffusionz,2) < 20 && powf(SPptr[j].diffusionx,2)+powf(SPptr[j].diffusiony,2)+powf(SPptr[j].diffusionz,2) < 20 ) { SPptr[ii].mass = 2.75; SPptr[j].mass = 0; SPptr[j].boundary = true; SPptr[ii].xvel= (SPptr[ii].xvel + SPptr[j].xvel)/2.0; SPptr[ii].yvel= (SPptr[ii].yvel + SPptr[j].yvel)/2.0; SPptr[ii].zvel= (SPptr[ii].zvel + SPptr[j].zvel)/2.0; SPptr[ii].xcoord= (SPptr[ii].xcoord + SPptr[j].xcoord)/2.0; SPptr[ii].ycoord= (SPptr[ii].ycoord + SPptr[j].ycoord)/2.0; SPptr[ii].zcoord= (SPptr[ii].zcoord + SPptr[j].zcoord)/2.0; SPptr[j].xcoord = SPptr[j].ycoord = SPptr[j].zcoord=90.99; //SPptr[j].cellnumber = NUMCELLS+1; //printf("%4.4f \n",ds); } //Particle splitting if (SPptr[ii].mass>3 && SPptr[ii].cellnumber < NUMCELLS && !SPptr[ii].boundary && ((powf(SPptr[ii].diffusionx,2)+powf(SPptr[ii].diffusiony,2)+powf(SPptr[ii].diffusionz,2)) > 35000 || (SPptr[ii].dens < 9400))) { SPptr[ii].mass = 1; SPptr[ii].split = true; SPptr[ii].ycoord += 0.015; //SPptr[j].cellnumber = NUMCELLS+1; //printf("%4.4f \n",ds); } if (ds <= (2 * cutoff) && ds > 0) { volatile float k = kernel(ds); volatile float rabx = (SPptr[ii]).rab_x((SPptr[j])); volatile float raby = (SPptr[ii]).rab_y((SPptr[j])); volatile float rabz = (SPptr[ii]).rab_z((SPptr[j])); volatile float vabx = (SPptr[ii]).vab_x((SPptr[j])); volatile float vaby = (SPptr[ii]).vab_y((SPptr[j])); volatile float vabz = (SPptr[ii]).vab_z((SPptr[j])); volatile float dkx = kernel_derivative(ds)*rabx / ds; volatile float dky = kernel_derivative(ds)*raby / ds; volatile float dkz = kernel_derivative(ds)*rabz / ds; //float dkxtest = kernel_test(ds)*rabx / ds; //float dkytest = kernel_test(ds)*raby / ds; //float dkztest = kernel_test(ds)*rabz / ds; volatile float d = dot_prod(vabx, vaby, vabz, rabx, raby, rabz); volatile float d2 = powf(ds, 2); //added mass volatile float s = (((SPptr[ii].solid*9+1)*ALPHA_FLUID)* SOUND * (powf(SPptr[i].mass,1)*cutoff * (d / (d2 + 0.01*powf(cutoff, 2))) + 50 * 1.0 / SOUND*powf(cutoff * (d / (d2 + 0.01*powf(cutoff, 2))), 2)) / (((SPptr[ii]).dens + (SPptr[j]).dens) / 2.0)) *(d < 0)*(1 + (!(SPptr[ii]).boundary)*((SPptr[j]).boundary) * ((1+3*SPptr[ii].fluid*SPptr[ii].fluid)*ALPHA__SAND_BOUNDARY)); //float s2 = ALPHA_LAMINAR_FLUID * SOUND * cutoff / ((SPptr[i]).dens + (SPptr[j]).dens)*d*(d < 0) / (d2 + 0.01*pow(cutoff, 2))*(1 + (!(SPptr[i]).boundary)*((SPptr[j]).boundary) *ALPHA_LAMINAR_BOUNDARY); //laminar volatile float dpx = ((SPptr[j]).press / powf((SPptr[j]).dens, 2) + (SPptr[ii]).press / powf((SPptr[ii]).dens, 2) + s)*dkx; volatile float dpy = ((SPptr[j]).press / powf((SPptr[j]).dens, 2) + (SPptr[ii]).press / powf((SPptr[ii]).dens, 2) + s)*dky; volatile float dpz = ((SPptr[j]).press / powf((SPptr[j]).dens, 2) + (SPptr[ii]).press / powf((SPptr[ii]).dens, 2) + s)*dkz; volatile float mass_solid_frac = SPptr[ii].solid*RHO_0_SAND / (RHO_0_SAND*SPptr[ii].solid + RHO_0*(SPptr[ii].fluid)); volatile float mass_fluid_frac = SPptr[ii].fluid*RHO_0 / (RHO_0_SAND*SPptr[ii].solid + RHO_0*(SPptr[ii].fluid)); if (mass_solid_frac > 0.001 && mass_solid_frac < 0.999 && mass_fluid_frac > 0.001 && mass_fluid_frac < 0.999 && !SPptr[ii].boundary && !SPptr[j].boundary) { volatile float solidgradx = (SPptr[j].solid - SPptr[ii].solid)*dkx; //note that fluidgrad = -solidgrad if there are only 2 types of particles volatile float solidgrady = (SPptr[j].solid - SPptr[ii].solid)*dky; volatile float solidgradz = (SPptr[j].solid - SPptr[ii].solid)*dkz; float fluidgradx = (SPptr[j].fluid - SPptr[ii].fluid)*dkx; float fluidgrady = (SPptr[j].fluid - SPptr[ii].fluid)*dky; float fluidgradz = (SPptr[j].fluid - SPptr[ii].fluid)*dkz; float solidbrownianx = (solidgradx / (SPptr[ii].solid) - (mass_solid_frac*solidgradx / (SPptr[ii].solid) + mass_fluid_frac*fluidgradx / (SPptr[ii].fluid))); float solidbrowniany = (solidgrady / (SPptr[ii].solid) - (mass_solid_frac*solidgrady / (SPptr[ii].solid) + mass_fluid_frac*fluidgrady / (SPptr[ii].fluid))); float solidbrownianz = (solidgradz / (SPptr[ii].solid) - (mass_solid_frac*solidgradz / (SPptr[ii].solid) + mass_fluid_frac*fluidgradz / (SPptr[ii].fluid))); float fluidbrownianx = (fluidgradx / (SPptr[ii].fluid) - (mass_fluid_frac*fluidgradx / (SPptr[ii].fluid) + mass_solid_frac*solidgradx / (SPptr[ii].solid))); float fluidbrowniany = (fluidgrady / (SPptr[ii].fluid) - (mass_fluid_frac*fluidgrady / (SPptr[ii].fluid) + mass_solid_frac*solidgrady / (SPptr[ii].solid))); float fluidbrownianz = (fluidgradz / (SPptr[ii].fluid) - (mass_fluid_frac*fluidgradz / (SPptr[ii].fluid) + mass_solid_frac*solidgradz / (SPptr[ii].solid))); float solidpressureslipx = (SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dkx - mass_solid_frac*(SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dkx - mass_fluid_frac*((SPptr[ii].fluid)*SPptr[ii].press - (SPptr[j].fluid)*SPptr[j].press)*dkx; float solidpressureslipy = (SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dky - mass_solid_frac*(SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dky - mass_fluid_frac*((SPptr[ii].fluid)*SPptr[ii].press - (SPptr[j].fluid)*SPptr[j].press)*dky; float solidpressureslipz = (SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dkz - mass_solid_frac*(SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dkz - mass_fluid_frac*((SPptr[ii].fluid)*SPptr[ii].press - (SPptr[j].fluid)*SPptr[j].press)*dkz; float fluidpressureslipx = (SPptr[ii].fluid*SPptr[ii].press - SPptr[j].fluid*SPptr[j].press)*dkx - mass_solid_frac*(SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dkx - mass_fluid_frac*((SPptr[ii].fluid)*SPptr[ii].press - (SPptr[j].fluid)*SPptr[j].press)*dkx; float fluidpressureslipy = (SPptr[ii].fluid*SPptr[ii].press - SPptr[j].fluid*SPptr[j].press)*dky - mass_solid_frac*(SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dky - mass_fluid_frac*((SPptr[ii].fluid)*SPptr[ii].press - (SPptr[j].fluid)*SPptr[j].press)*dky; float fluidpressureslipz = (SPptr[ii].fluid*SPptr[ii].press - SPptr[j].fluid*SPptr[j].press)*dkz - mass_solid_frac*(SPptr[ii].solid*SPptr[ii].press - SPptr[j].solid*SPptr[j].press)*dkz - mass_fluid_frac*((SPptr[ii].fluid)*SPptr[ii].press - (SPptr[j].fluid)*SPptr[j].press)*dkz; float solidbodyx = (SPptr[ii].solid*SPptr[ii].dens - (mass_solid_frac*SPptr[ii].solid*SPptr[ii].dens + mass_fluid_frac*SPptr[ii].fluid*SPptr[ii].dens)) * ((150.0 / SPptr[ii].dens)*SPptr[ii].delpressx - SPptr[ii].xvel*dkx*vabx - SPptr[ii].yvel*dky*vabx - SPptr[ii].zvel*dkz*vabx); float solidbodyy = (SPptr[ii].solid*SPptr[ii].dens - (mass_solid_frac*SPptr[ii].solid*SPptr[ii].dens + mass_fluid_frac*SPptr[ii].fluid*SPptr[ii].dens)) * ((150.0 / SPptr[ii].dens)*SPptr[ii].delpressy - SPptr[ii].xvel*dkx*vaby - SPptr[ii].yvel*dky*vaby - SPptr[ii].zvel*dkz*vaby); float solidbodyz = (SPptr[ii].solid*SPptr[ii].dens - (mass_solid_frac*SPptr[ii].solid*SPptr[ii].dens + mass_fluid_frac*SPptr[ii].fluid*SPptr[ii].dens)) * (GRAVITY + (150.0 / SPptr[ii].dens)*SPptr[ii].delpressz - SPptr[ii].xvel*dkx*vabz - SPptr[ii].yvel*dky*vabz - SPptr[ii].zvel*dkz*vabz); float fluidbodyx = (SPptr[ii].fluid*SPptr[ii].dens - (mass_solid_frac*SPptr[ii].solid*SPptr[ii].dens + mass_fluid_frac*SPptr[ii].fluid*SPptr[ii].dens)) * ((150.0 / SPptr[ii].dens)*SPptr[ii].delpressx - SPptr[ii].xvel*dkx*vabx - SPptr[ii].yvel*dky*vabx - SPptr[ii].zvel*dkz*vabx); float fluidbodyy = (SPptr[ii].fluid*SPptr[ii].dens - (mass_solid_frac*SPptr[ii].solid*SPptr[ii].dens + mass_fluid_frac*SPptr[ii].fluid*SPptr[ii].dens)) * ((150.0 / SPptr[ii].dens)*SPptr[ii].delpressy - SPptr[ii].xvel*dkx*vaby - SPptr[ii].yvel*dky*vaby - SPptr[ii].zvel*dkz*vaby); float fluidbodyz = (SPptr[ii].fluid*SPptr[ii].dens - (mass_solid_frac*SPptr[ii].solid*SPptr[ii].dens + mass_fluid_frac*SPptr[ii].fluid*SPptr[ii].dens)) * (GRAVITY + (150.0 / SPptr[ii].dens)*SPptr[ii].delpressz - SPptr[ii].xvel*dkx*vabz - SPptr[ii].yvel*dky*vabz - SPptr[ii].zvel*dkz*vabz); atomicAdd(&(SPptr[ii].soliddriftvelx), MIXPRESSURE*(solidbodyx + solidpressureslipx) - MIXBROWNIAN*solidbrownianx); atomicAdd(&(SPptr[ii].soliddriftvely), MIXPRESSURE*(solidbodyy + solidpressureslipy) - MIXBROWNIAN*solidbrowniany); atomicAdd(&(SPptr[ii].soliddriftvelz), MIXPRESSURE*(solidbodyz + solidpressureslipz) - MIXBROWNIAN*solidbrownianz); atomicAdd(&(SPptr[ii].fluiddriftvelx), MIXPRESSURE*(fluidbodyx + fluidpressureslipx) - MIXBROWNIAN*fluidbrownianx); atomicAdd(&(SPptr[ii].fluiddriftvely), MIXPRESSURE*(fluidbodyy + fluidpressureslipy) - MIXBROWNIAN*fluidbrowniany); atomicAdd(&(SPptr[ii].fluiddriftvelz), MIXPRESSURE*(fluidbodyz + fluidpressureslipz) - MIXBROWNIAN*fluidbrownianz); } atomicAdd(&(SPptr[ii].newdelpressx), dpx*SPptr[j].mass); //added mass atomicAdd(&(SPptr[ii].newdelpressy), dpy*SPptr[j].mass); atomicAdd(&(SPptr[ii].newdelpressz), dpz*SPptr[j].mass); atomicAdd(&(SPptr[ii].newdens), k *(1 + float(!(SPptr[ii]).boundary)*float((SPptr[j]).boundary)*BDENSFACTOR)*SPptr[j].mass); //added mass atomicAdd(&(SPptr[ii].diffusionx),SPptr[j].mass/ SPptr[j].dens*dkx*!SPptr[j].boundary*!SPptr[ii].boundary); //added boundary atomicAdd(&(SPptr[ii].diffusiony), SPptr[j].mass/ SPptr[j].dens*dky*!SPptr[j].boundary*!SPptr[ii].boundary); atomicAdd(&(SPptr[ii].diffusionz), SPptr[j].mass / SPptr[j].dens*dkz*!SPptr[j].boundary*!SPptr[ii].boundary); volatile float mixfactor = (!SPptr[j].boundary)*(!SPptr[ii].boundary)*(SPptr[ii].solid > 0.0)* (SPptr[j].solid > 0.0) * 2 * (SPptr[ii].solid-0.0)*(SPptr[j].solid-0.0) / (SPptr[ii].solid-0.0 + SPptr[j].solid-0.0+0.01); atomicAdd(&(SPptr[ii].vel_grad[0][0]), -mixfactor*vabx*dkx *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[0][1]), -mixfactor*vaby*dkx *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[0][2]), -mixfactor*vabz*dkx *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[1][0]), -mixfactor*vabx*dky *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[1][1]), -mixfactor*vaby*dky *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[1][2]), -mixfactor*vabz*dky *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[2][0]), -mixfactor*vabx*dkz *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[2][1]), -mixfactor*vaby*dkz *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].vel_grad[2][2]), -mixfactor*vabz*dkz *1./ (SPptr[ii]).dens); atomicAdd(&(SPptr[ii].stress_accel[0]), mixfactor*((SPptr[ii]).stress_tensor[0][0] * dkx + (SPptr[ii]).stress_tensor[0][1] * dky + (SPptr[ii]).stress_tensor[0][2] * dkz) / pow((SPptr[ii]).dens, 2) + ((SPptr[ii]).stress_tensor[0][0] * dkx + (SPptr[ii]).stress_tensor[0][1] * dky + (SPptr[ii]).stress_tensor[0][2] * dkz) / pow((SPptr[ii]).dens, 2)); atomicAdd(&(SPptr[ii].stress_accel[1]), mixfactor*((SPptr[ii]).stress_tensor[1][0] * dkx + (SPptr[ii]).stress_tensor[1][1] * dky + (SPptr[ii]).stress_tensor[1][2] * dkz) / pow((SPptr[ii]).dens, 2) + ((SPptr[ii]).stress_tensor[1][0] * dkx + (SPptr[ii]).stress_tensor[1][1] * dky + (SPptr[ii]).stress_tensor[1][2] * dkz) / pow((SPptr[ii]).dens, 2)); atomicAdd(&(SPptr[ii].stress_accel[2]), mixfactor*((SPptr[ii]).stress_tensor[2][0] * dkx + (SPptr[ii]).stress_tensor[2][1] * dky + (SPptr[ii]).stress_tensor[2][2] * dkz) / pow((SPptr[ii]).dens, 2) + ((SPptr[ii]).stress_tensor[2][0] * dkx + (SPptr[ii]).stress_tensor[2][1] * dky + (SPptr[ii]).stress_tensor[2][2] * dkz) / pow((SPptr[ii]).dens, 2)); volatile float ds2 = dot_prod(SPptr[j].soliddriftvelx, SPptr[j].soliddriftvely, SPptr[j].soliddriftvelz, dkx, dky, dkz); volatile float ds = dot_prod(SPptr[ii].soliddriftvelx, SPptr[ii].soliddriftvely, SPptr[ii].soliddriftvelz, dkx, dky, dkz); volatile float df2 = dot_prod(SPptr[j].fluiddriftvelx, SPptr[j].fluiddriftvely, SPptr[j].fluiddriftvelz, dkx, dky, dkz); volatile float df = dot_prod(SPptr[ii].fluiddriftvelx, SPptr[ii].fluiddriftvely, SPptr[ii].fluiddriftvelz, dkx, dky, dkz); atomicAdd(&(SPptr[ii].mixture_accel[0]), -1 / SPptr[ii].dens / SPptr[j].dens*(SPptr[j].solid*SPptr[j].dens*(SPptr[j].solid*SPptr[j].soliddriftvelx*ds2 + SPptr[ii].solid*SPptr[ii].soliddriftvelx*ds) + SPptr[j].fluid*SPptr[j].dens*(SPptr[j].fluid*SPptr[j].fluiddriftvelx*df2 + SPptr[ii].fluid*SPptr[ii].fluiddriftvelx*df))); atomicAdd(&(SPptr[ii].mixture_accel[1]), -1 / SPptr[ii].dens / SPptr[j].dens*(SPptr[j].solid*SPptr[j].dens*(SPptr[j].solid*SPptr[j].soliddriftvely*ds2 + SPptr[ii].solid*SPptr[ii].soliddriftvely*ds) + SPptr[j].fluid*SPptr[j].dens*(SPptr[j].fluid*SPptr[j].fluiddriftvely*df2 + SPptr[ii].fluid*SPptr[ii].fluiddriftvely*df))); atomicAdd(&(SPptr[ii].mixture_accel[2]), -1 / SPptr[ii].dens / SPptr[j].dens*(SPptr[j].solid*SPptr[j].dens*(SPptr[j].solid*SPptr[j].soliddriftvelz*ds2 + SPptr[ii].solid*SPptr[ii].soliddriftvelz*ds) + SPptr[j].fluid*SPptr[j].dens*(SPptr[j].fluid*SPptr[j].fluiddriftvelz*df2 + SPptr[ii].fluid*SPptr[ii].fluiddriftvelz*df))); atomicAdd(&(SPptr[ii].delsolid), (!SPptr[j].boundary) *(!SPptr[ii].boundary) *-0.5 / SPptr[j].dens*(SPptr[ii].solid + SPptr[j].solid) * (dkx*vabx + dky*vaby + dkz*vabz) + (-(SPptr[ii].solid*SPptr[ii].soliddriftvelx + SPptr[j].solid*SPptr[j].soliddriftvelx)*dkx - (SPptr[ii].solid*SPptr[ii].soliddriftvely + SPptr[j].solid*SPptr[j].soliddriftvely)*dky - (SPptr[ii].solid*SPptr[ii].soliddriftvelz + SPptr[j].solid*SPptr[j].soliddriftvelz)*dkz) / SPptr[j].dens); atomicAdd(&(SPptr[ii].delfluid), (!SPptr[j].boundary) *(!SPptr[ii].boundary) *-0.5 / SPptr[j].dens*(SPptr[ii].fluid + SPptr[j].fluid) * (dkx*vabx + dky*vaby + dkz*vabz) + (-(SPptr[ii].fluid*SPptr[ii].fluiddriftvelx + SPptr[j].fluid*SPptr[j].fluiddriftvelx)*dkx - (SPptr[ii].fluid*SPptr[ii].fluiddriftvely + SPptr[j].fluid*SPptr[j].fluiddriftvely)*dky - (SPptr[ii].fluid*SPptr[ii].fluiddriftvelz + SPptr[j].fluid*SPptr[j].fluiddriftvelz)*dkz) / SPptr[j].dens); } } } } } } __syncthreads(); if (idx < nspts) { if ((SPptr[idx]).solid) { float tr = 0; //trace of strain rate float tr2 = 0; //trace of stress tensor float tr3 = 0; //double dot of stress tensor float tr4 = 0; //trace of stress tensor times strain rate float tr5 = 0; //double dot of strain rate for (int p = 0; p < 3; p++) { for (int q = 0; q < 3; q++) { (SPptr[idx]).strain_rate[p][q] = 0.5*((SPptr[idx]).vel_grad[p][q] + (SPptr[idx]).vel_grad[q][p]); tr3 += 0.5*(SPptr[idx]).stress_tensor[p][q]*(SPptr[idx]).stress_tensor[p][q]; tr5 += (SPptr[idx]).strain_rate[p][q]*(SPptr[idx]).strain_rate[p][q]; tr4 += (SPptr[idx]).stress_tensor[p][q] * (SPptr[idx]).strain_rate[q][p]; } tr += (SPptr[idx]).strain_rate[p][p]; tr2 += (SPptr[idx]).stress_tensor[p][p]; } // std::cout << (SPptr[index]).press << "\n"; for (int p = 0; p < 3; p++) { for (int q = 0; q < 3; q++) { if (3 * tan(PHI) / (sqrt(9 + 12 * pow(tan(PHI), 2)))*(SPptr[idx]).press*(SPptr[idx].press>0) + KC / (sqrt(9 + 12 * pow(tan(PHI), 2))) < tr3 && tr3 != 0) { (SPptr[idx]).stress_tensor[p][q] *= (3 * tan(PHI) / (sqrt(9 + 12 * pow(tan(PHI), 2)))*(SPptr[idx]).press*(SPptr[idx].press>0) + KC / (sqrt(9 + 12 * pow(tan(PHI), 2)))) / tr3; } (SPptr[idx]).stress_rate[p][q] = 3 * C1*((SPptr[idx]).press)*((SPptr[idx]).strain_rate[p][q] - 1. / 3.*tr*(p == q)) + C1*C2*(tr4 + tr*(SPptr[idx]).press*(SPptr[idx].press>0)) / (pow((SPptr[idx]).press, 2) + 1e8)*(SPptr[idx]).stress_tensor[p][q] - C1*C3*sqrt(tr5)*(SPptr[idx]).stress_tensor[p][q]; //std::cout << tr4 << ", " << tr*(SPptr[index]).press << "\n"; } } } //} } __syncthreads(); } __global__ void update_all(Particle *SPptr, int *particleindex, int *cells, int *start_copy, int *start, int *end, int *split, int *numsplit, int nspts, int x, float *maxv, int dev, int buffer, int t, float *spts, float *a3, float *b3) { int index = blockIdx.x * blockDim.x + threadIdx.x; int lb = max(0, dev*NUMCELLS/2-buffer); int hb = lb+x; //for 4 devices use min(NUMCELLS, NUMCELLS/4*(dev+1)+buffer); if (index < nspts) { SPptr[particleindex[index]].flag = false; if (!(SPptr[particleindex[index]]).flag&&SPptr[particleindex[index]].cellnumber >= lb && SPptr[particleindex[index]].cellnumber < hb) { //printf("%d %d %d\n",dev, index, SPptr[index].cellnumber); spts[(3 * index)] = (SPptr[particleindex[index]]).xcoord; spts[(3 * index) + 1] = (SPptr[particleindex[index]]).ycoord; spts[(3 * index) + 2] = (SPptr[particleindex[index]]).zcoord; a3[index] = ((SPptr[particleindex[index]])).dens; b3[index] = ((SPptr[particleindex[index]])).boundary; } //if (SPptr[index].cellnumber >= dev*NUMCELLS/2 && SPptr[index].cellnumber < dev*NUMCELLS/2 + NUMCELLS/2){ // for 2 devices only! (SPptr[particleindex[index]]).update(t,*maxv); //} //(SPptr[index]).cellnumber = int((SPptr[index].xcoord - XMIN) / CELLSIZE)*GRIDSIZE*GRIDSIZE + int((SPptr[index].ycoord - YMIN) / CELLSIZE)*GRIDSIZE + int((SPptr[index].zcoord - ZMIN) / CELLSIZE); cells[index] = SPptr[particleindex[index]].cellnumber; SPptr[index].newdens = 0; SPptr[index].newdelpressx = 0; SPptr[index].newdelpressy = 0; SPptr[index].newdelpressz = 0; SPptr[index].vel_grad[0][0] = SPptr[index].vel_grad[0][1] = SPptr[index].vel_grad[0][2] = SPptr[index].vel_grad[1][0] = SPptr[index].vel_grad[1][1] = SPptr[index].vel_grad[1][2] = SPptr[index].vel_grad[2][0] = SPptr[index].vel_grad[2][1] = SPptr[index].vel_grad[2][2] = 0 ; SPptr[index].stress_accel[0] = SPptr[index].stress_accel[1] = SPptr[index].stress_accel[2] = 0; SPptr[index].fluiddriftvelx = SPptr[index].fluiddriftvely = SPptr[index].fluiddriftvelz = SPptr[index].soliddriftvelx = SPptr[index].soliddriftvely = SPptr[index].soliddriftvelz = 0; SPptr[index].mixture_accel[0] = SPptr[index].mixture_accel[1] = SPptr[index].mixture_accel[2] = 0; SPptr[index].delsolid = SPptr[index].delfluid = SPptr[index].diffusionx = SPptr[index].diffusionz =SPptr[index].diffusiony =0; } if (index < x) { start[index] = -1; start_copy[index] = -1; end[index] = -1; split[index] = -1; } if (index ==0) {numsplit[0]=0;} __syncthreads(); } __global__ void find_idx(int *SPptr, int dev, int npts, int buffer, int *xleft, int *xright, int *sleft, int *sright) { int idx = blockIdx.x * blockDim.x + threadIdx.x; int lb = dev*NUMCELLS/2; int hb = lb + NUMCELLS/2; if (idx < npts){ if (dev == 0 && SPptr[idx+1]>= hb && SPptr[idx]<hb){ xleft[0] = 0; // index of first updated particle xright[0] = idx; // index of final updated particle sleft[0] = 0; //index of final particle to transfer to dev-1 } if (dev == 0 && SPptr[idx+1]>= hb-buffer && SPptr[idx]<hb-buffer){ sright[0] = idx+1; } if (dev == 1 && SPptr[idx]<lb && SPptr[idx+1]>=lb){ xleft[0] = idx+1; xright[0] = npts-1; sright[0] = npts; //index of first particle to transfer to dev+1 } if (dev == 1 && SPptr[idx]<lb+buffer && SPptr[idx+1]>=lb+buffer){ sleft[0] = idx+1; } } // __syncthreads(); } __global__ void mem_shift(Particle *SPptr, Particle *buff, int *cells, int *ibuff, int dev, int shifts, int indexleft, int indexright) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (shifts !=0 && idx <= indexright && idx >= indexleft){ buff[idx-indexleft] = SPptr[idx]; } __syncthreads(); if (shifts !=0 && idx <= indexright && idx >= indexleft){ SPptr[idx-shifts] = buff[idx-indexleft]; } } __global__ void cell_calc(Particle *SPptr, int *particleindex, int *cells, int size, int dev) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx< size){ (SPptr[particleindex[idx]]).cellnumber = int((SPptr[particleindex[idx]].xcoord - XMIN) / CELLSIZE)*GRIDSIZE*GRIDSIZE + int((SPptr[particleindex[idx]].ycoord - YMIN) / CELLSIZE)*GRIDSIZE + int((SPptr[particleindex[idx]].zcoord - ZMIN) / CELLSIZE); cells[idx] = SPptr[particleindex[idx]].cellnumber; } } __global__ void count_after_merge(int *cells, int *particleindex, int size, int *newsize) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx > 0 && idx < size){ if (cells[idx]>=NUMCELLS & cells[idx-1]<NUMCELLS){ *newsize = idx; } } } //Find maximum velocity and store in B. Call with one block __global__ void VecMax(Particle* A, int* particleindex, float* B, int nspts, int blocks) { extern __shared__ float cache[1024]; int i = blockDim.x * blockIdx.x + threadIdx.x; int cacheIndex = threadIdx.x; float temp = 0; // register for each thread while (i < nspts) { float vel = powf(A[i].xvel,2)+powf(A[i].yvel,2)+powf(A[i].zvel,2); if(vel > temp) temp = vel; i += blockDim.x * gridDim.x; } cache[cacheIndex] = temp; // set the cache value __syncthreads(); // perform parallel reduction, threadsPerBlock must be 2^m int ib = blockDim.x / 2; while (ib != 0) { if(cacheIndex < ib && cache[cacheIndex + ib] > cache[cacheIndex]) cache[cacheIndex] = cache[cacheIndex + ib]; __syncthreads(); ib /= 2; } if(cacheIndex == 0) B[blockIdx.x] = cache[0]; if (i ==0){ for(int j = 0;j<blocks;j++){ if (B[0]<B[j]){ B[0]=B[j]; //set first element as max } } } } __global__ void force_calc_fine(Particle *SPptr, int *particleindex, int *cell, int *start, int *end, int *split, int nspts,int x, int dev, int buffer, int *numsplit) { //x: start cellnumber int idx = blockIdx.x * blockDim.x + threadIdx.x; int bidx = blockIdx.x; int tidx = threadIdx.x; const int threadsperblockmax = 2048; //bidx checks cell number split[bidx/8], subindex bidx % 8 int dirx = (bidx % 8) & 1; int diry = ((bidx % 8) & 2) >> 1; int dirz = ((bidx % 8) & 4) >> 2; int nb[8] = {0, pow(-1,1+dirx)*GRIDSIZE*GRIDSIZE, pow(-1,1+diry)*GRIDSIZE, pow(-1,dirz),pow(-1,1+dirx)*GRIDSIZE*GRIDSIZE + pow(-1,1+diry)*GRIDSIZE, pow(-1,1+dirx)*GRIDSIZE*GRIDSIZE + pow(-1,dirz), pow(-1,1+diry)*GRIDSIZE + pow(-1,dirz), pow(-1,1+dirx)*GRIDSIZE*GRIDSIZE + pow(-1,1+diry)*GRIDSIZE + pow(-1,dirz)}; //if (split[bidx/8] >=0 && bidx % 8 == 0 && tidx < 8 ){printf("%d %d\n",split[bidx/8], split[bidx/8] + nb[tidx]);} __shared__ short int p[8]; __shared__ short int pidx[8]; int __shared__ sum[threadsperblockmax];// = 0; int __shared__ jj[threadsperblockmax];// = 0; volatile __shared__ int total; volatile __shared__ int blockpop; if (idx < threadsperblockmax) { sum[idx] = 6500; jj[idx] = 6500; } __syncthreads(); if (tidx < 8) { p[tidx] = 0; } if (split[bidx/8] < x && start[split[bidx/8]] >= 0) { ///////////count and sort population of neighbour cells////////////// if (tidx < 8 && split[bidx/8] + nb[tidx] >= 0 && split[bidx/8] + nb[tidx] < x+buffer && start[split[bidx/8] + nb[tidx]] >= 0 && end[split[bidx/8] + nb[tidx]] >= 0 && start[split[bidx/8] + nb[tidx]] < nspts && 1 + end[split[bidx/8] + nb[tidx]] - start[split[bidx/8] + nb[tidx]] > 0 ) { p[tidx] = 1 + end[split[bidx/8] + nb[tidx]] - start[split[bidx/8] + nb[tidx]]; //count population of neighbour cells so we know how many threads to use pidx[tidx] = tidx; } if (tidx == 0) { blockpop = p[tidx]; } } else { if (tidx == 0) { blockpop = 0; } } __syncthreads(); if (split[bidx/8] < x && start[split[bidx/8]] >= 0) { if (tidx == 0) { total = 0; for (int i = 0; i < 8; i++) { if (p[i]>0 && split[bidx/8] + nb[i] >= 0 && split[bidx/8] + nb[i] < x && start[split[bidx/8] + nb[i]] >= 0 && end[split[bidx/8] + nb[i]] >= 0 && start[split[bidx/8] + nb[i]] < nspts) { total += p[i]; } } } } else { if (tidx == 0) {total = 0; } } __syncthreads(); if (split[bidx/8]<x &&start[split[bidx/8]] >= 0) { if (tidx == 0) { int count = 0; for (int i = 0; i < 8; i++) { if (p[i] != 0) { p[count++] = p[i]; pidx[count - 1] = pidx[i]; //sort } } while (count < 8) { p[count++] = 0; pidx[count - 1] = 0; } } } __syncthreads(); if (split[bidx/8]<x && start[split[bidx/8]] >= 0) { for (int c = 0; c<total;c+=256){ if (tidx +c< total) { sum[tidx+c] = 0; jj[tidx+c] = 0; while (tidx+c + 1 > sum[tidx+c] && jj[tidx+c] < threadsperblockmax) { sum[tidx+c] += p[jj[tidx+c]]; jj[tidx+c]++; } } } } __syncthreads(); if (split[bidx/8]<x && start[split[bidx/8]] >= 0 && split[bidx/8] >=0 && start[split[bidx/8]] < nspts) { //should exclude the buffer here for (int c = 0; c<total;c+=256){ if (tidx+c < total && split[bidx/8] + nb[pidx[jj[tidx+c] - 1]] >= 0 && split[bidx/8] + nb[pidx[jj[tidx+c] - 1]] < x) { /////////////////////////////////////////////////////////////////// volatile int j = particleindex[start[split[bidx/8] + nb[pidx[jj[tidx+c] - 1]]] + sum[tidx+c] - (tidx+c + 1)]; if (start[split[bidx/8] + nb[pidx[jj[tidx+c] - 1]]] >= 0 && j < nspts && j >= 0) { for (volatile int ii = start[split[bidx/8]]; ii <= end[split[bidx/8]]; ii++) { volatile int i = particleindex[ii]; if (SPptr[i].subindex == bidx % 8){ float ds = (SPptr[i]).distance((SPptr[j])); //Particle merging /*if (ds <= (MERGEDIST) && ds > 0 && SPptr[i].mass>0 && SPptr[j].mass> 0 && SPptr[i].mass<2 && SPptr[j].mass<2 && !SPptr[i].boundary &&!SPptr[j].boundary && powf(SPptr[i].diffusionx,2)+powf(SPptr[i].diffusiony,2)+powf(SPptr[i].diffusionz,2) < 20 && powf(SPptr[j].diffusionx,2)+powf(SPptr[j].diffusiony,2)+powf(SPptr[j].diffusionz,2) < 20 ) { SPptr[i].mass = 2.75; SPptr[j].mass = 0; SPptr[j].boundary = true; SPptr[i].xvel= (SPptr[i].xvel + SPptr[j].xvel)/2.0; SPptr[i].yvel= (SPptr[i].yvel + SPptr[j].yvel)/2.0; SPptr[i].zvel= (SPptr[i].zvel + SPptr[j].zvel)/2.0; SPptr[i].xcoord= (SPptr[i].xcoord + SPptr[j].xcoord)/2.0; SPptr[i].ycoord= (SPptr[i].ycoord + SPptr[j].ycoord)/2.0; SPptr[i].zcoord= (SPptr[i].zcoord + SPptr[j].zcoord)/2.0; SPptr[j].xcoord = SPptr[j].ycoord = SPptr[j].zcoord=9.99; //SPptr[j].cellnumber = NUMCELLS+1; //printf("%4.4f \n",SPptr[j].xcoord); } //Particle splitting if (SPptr[i].mass>3 && SPptr[i].cellnumber < NUMCELLS && !SPptr[i].boundary && ((powf(SPptr[i].diffusionx,2)+powf(SPptr[i].diffusiony,2)+powf(SPptr[i].diffusionz,2)) > 35000 || (SPptr[i].dens < 9400))) { SPptr[i].mass = 1; SPptr[i].split = true; SPptr[i].ycoord += 0.015; }*/ if (ds <= (2 * cutoff) && ds > 0) { volatile float k = kernel(ds); volatile float rabx = (SPptr[i]).rab_x((SPptr[j])); volatile float raby = (SPptr[i]).rab_y((SPptr[j])); volatile float rabz = (SPptr[i]).rab_z((SPptr[j])); volatile float vabx = (SPptr[i]).vab_x((SPptr[j])); volatile float vaby = (SPptr[i]).vab_y((SPptr[j])); volatile float vabz = (SPptr[i]).vab_z((SPptr[j])); volatile float dkx = kernel_derivative(ds)*rabx / ds; volatile float dky = kernel_derivative(ds)*raby / ds; volatile float dkz = kernel_derivative(ds)*rabz / ds; volatile float d = dot_prod(vabx, vaby, vabz, rabx, raby, rabz); volatile float d2 = powf(ds, 2); //added mass volatile float s = (((SPptr[i].solid*9+1)*ALPHA_FLUID)* SOUND * (powf(SPptr[i].mass,1)*cutoff * (d / (d2 + 0.01*powf(cutoff, 2))) + 50 * 1.0 / SOUND*powf(cutoff * (d / (d2 + 0.01*powf(cutoff, 2))), 2)) / (((SPptr[i]).dens + (SPptr[j]).dens) / 2.0)) *(d < 0)*(1 + (!(SPptr[i]).boundary)*((SPptr[j]).boundary) * ((1+3*SPptr[i].fluid*SPptr[i].fluid)*ALPHA__SAND_BOUNDARY)); //float s2 = ALPHA_LAMINAR_FLUID * SOUND * cutoff / ((SPptr[i]).dens + (SPptr[j]).dens)*d*(d < 0) / (d2 + 0.01*pow(cutoff, 2))*(1 + (!(SPptr[i]).boundary)*((SPptr[j]).boundary) *ALPHA_LAMINAR_BOUNDARY); //laminar volatile float dpx = ((SPptr[j]).press / powf((SPptr[j]).dens, 2) + (SPptr[i]).press / powf((SPptr[i]).dens, 2) + s)*dkx; volatile float dpy = ((SPptr[j]).press / powf((SPptr[j]).dens, 2) + (SPptr[i]).press / powf((SPptr[i]).dens, 2) + s)*dky; volatile float dpz = ((SPptr[j]).press / powf((SPptr[j]).dens, 2) + (SPptr[i]).press / powf((SPptr[i]).dens, 2) + s)*dkz; volatile float mass_solid_frac = SPptr[i].solid*RHO_0_SAND / (RHO_0_SAND*SPptr[i].solid + RHO_0*(SPptr[i].fluid)); volatile float mass_fluid_frac = SPptr[i].fluid*RHO_0 / (RHO_0_SAND*SPptr[i].solid + RHO_0*(SPptr[i].fluid)); if (mass_solid_frac > 0.001 && mass_solid_frac < 0.999 && mass_fluid_frac > 0.001 && mass_fluid_frac < 0.999 && !SPptr[i].boundary && !SPptr[j].boundary) { volatile float solidgradx = (SPptr[j].solid - SPptr[i].solid)*dkx; //note that fluidgrad = -solidgrad if there are only 2 types of particles volatile float solidgrady = (SPptr[j].solid - SPptr[i].solid)*dky; volatile float solidgradz = (SPptr[j].solid - SPptr[i].solid)*dkz; float fluidgradx = (SPptr[j].fluid - SPptr[i].fluid)*dkx; float fluidgrady = (SPptr[j].fluid - SPptr[i].fluid)*dky; float fluidgradz = (SPptr[j].fluid - SPptr[i].fluid)*dkz; float solidbrownianx = (solidgradx / (SPptr[i].solid) - (mass_solid_frac*solidgradx / (SPptr[i].solid) + mass_fluid_frac*fluidgradx / (SPptr[i].fluid))); float solidbrowniany = (solidgrady / (SPptr[i].solid) - (mass_solid_frac*solidgrady / (SPptr[i].solid) + mass_fluid_frac*fluidgrady / (SPptr[i].fluid))); float solidbrownianz = (solidgradz / (SPptr[i].solid) - (mass_solid_frac*solidgradz / (SPptr[i].solid) + mass_fluid_frac*fluidgradz / (SPptr[i].fluid))); float fluidbrownianx = (fluidgradx / (SPptr[i].fluid) - (mass_fluid_frac*fluidgradx / (SPptr[i].fluid) + mass_solid_frac*solidgradx / (SPptr[i].solid))); float fluidbrowniany = (fluidgrady / (SPptr[i].fluid) - (mass_fluid_frac*fluidgrady / (SPptr[i].fluid) + mass_solid_frac*solidgrady / (SPptr[i].solid))); float fluidbrownianz = (fluidgradz / (SPptr[i].fluid) - (mass_fluid_frac*fluidgradz / (SPptr[i].fluid) + mass_solid_frac*solidgradz / (SPptr[i].solid))); float solidpressureslipx = (SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dkx - mass_solid_frac*(SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dkx - mass_fluid_frac*((SPptr[i].fluid)*SPptr[i].press - (SPptr[j].fluid)*SPptr[j].press)*dkx; float solidpressureslipy = (SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dky - mass_solid_frac*(SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dky - mass_fluid_frac*((SPptr[i].fluid)*SPptr[i].press - (SPptr[j].fluid)*SPptr[j].press)*dky; float solidpressureslipz = (SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dkz - mass_solid_frac*(SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dkz - mass_fluid_frac*((SPptr[i].fluid)*SPptr[i].press - (SPptr[j].fluid)*SPptr[j].press)*dkz; float fluidpressureslipx = (SPptr[i].fluid*SPptr[i].press - SPptr[j].fluid*SPptr[j].press)*dkx - mass_solid_frac*(SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dkx - mass_fluid_frac*((SPptr[i].fluid)*SPptr[i].press - (SPptr[j].fluid)*SPptr[j].press)*dkx; float fluidpressureslipy = (SPptr[i].fluid*SPptr[i].press - SPptr[j].fluid*SPptr[j].press)*dky - mass_solid_frac*(SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dky - mass_fluid_frac*((SPptr[i].fluid)*SPptr[i].press - (SPptr[j].fluid)*SPptr[j].press)*dky; float fluidpressureslipz = (SPptr[i].fluid*SPptr[i].press - SPptr[j].fluid*SPptr[j].press)*dkz - mass_solid_frac*(SPptr[i].solid*SPptr[i].press - SPptr[j].solid*SPptr[j].press)*dkz - mass_fluid_frac*((SPptr[i].fluid)*SPptr[i].press - (SPptr[j].fluid)*SPptr[j].press)*dkz; float solidbodyx = (SPptr[i].solid*SPptr[i].dens - (mass_solid_frac*SPptr[i].solid*SPptr[i].dens + mass_fluid_frac*SPptr[i].fluid*SPptr[i].dens)) * ((150.0 / SPptr[i].dens)*SPptr[i].delpressx - SPptr[i].xvel*dkx*vabx - SPptr[i].yvel*dky*vabx - SPptr[i].zvel*dkz*vabx); float solidbodyy = (SPptr[i].solid*SPptr[i].dens - (mass_solid_frac*SPptr[i].solid*SPptr[i].dens + mass_fluid_frac*SPptr[i].fluid*SPptr[i].dens)) * ((150.0 / SPptr[i].dens)*SPptr[i].delpressy - SPptr[i].xvel*dkx*vaby - SPptr[i].yvel*dky*vaby - SPptr[i].zvel*dkz*vaby); float solidbodyz = (SPptr[i].solid*SPptr[i].dens - (mass_solid_frac*SPptr[i].solid*SPptr[i].dens + mass_fluid_frac*SPptr[i].fluid*SPptr[i].dens)) * (GRAVITY + (150.0 / SPptr[i].dens)*SPptr[i].delpressz - SPptr[i].xvel*dkx*vabz - SPptr[i].yvel*dky*vabz - SPptr[i].zvel*dkz*vabz); float fluidbodyx = (SPptr[i].fluid*SPptr[i].dens - (mass_solid_frac*SPptr[i].solid*SPptr[i].dens + mass_fluid_frac*SPptr[i].fluid*SPptr[i].dens)) * ((150.0 / SPptr[i].dens)*SPptr[i].delpressx - SPptr[i].xvel*dkx*vabx - SPptr[i].yvel*dky*vabx - SPptr[i].zvel*dkz*vabx); float fluidbodyy = (SPptr[i].fluid*SPptr[i].dens - (mass_solid_frac*SPptr[i].solid*SPptr[i].dens + mass_fluid_frac*SPptr[i].fluid*SPptr[i].dens)) * ((150.0 / SPptr[i].dens)*SPptr[i].delpressy - SPptr[i].xvel*dkx*vaby - SPptr[i].yvel*dky*vaby - SPptr[i].zvel*dkz*vaby); float fluidbodyz = (SPptr[i].fluid*SPptr[i].dens - (mass_solid_frac*SPptr[i].solid*SPptr[i].dens + mass_fluid_frac*SPptr[i].fluid*SPptr[i].dens)) * (GRAVITY + (150.0 / SPptr[i].dens)*SPptr[i].delpressz - SPptr[i].xvel*dkx*vabz - SPptr[i].yvel*dky*vabz - SPptr[i].zvel*dkz*vabz); atomicAdd(&(SPptr[i].soliddriftvelx), MIXPRESSURE*(solidbodyx + solidpressureslipx) - MIXBROWNIAN*solidbrownianx); atomicAdd(&(SPptr[i].soliddriftvely), MIXPRESSURE*(solidbodyy + solidpressureslipy) - MIXBROWNIAN*solidbrowniany); atomicAdd(&(SPptr[i].soliddriftvelz), MIXPRESSURE*(solidbodyz + solidpressureslipz) - MIXBROWNIAN*solidbrownianz); atomicAdd(&(SPptr[i].fluiddriftvelx), MIXPRESSURE*(fluidbodyx + fluidpressureslipx) - MIXBROWNIAN*fluidbrownianx); atomicAdd(&(SPptr[i].fluiddriftvely), MIXPRESSURE*(fluidbodyy + fluidpressureslipy) - MIXBROWNIAN*fluidbrowniany); atomicAdd(&(SPptr[i].fluiddriftvelz), MIXPRESSURE*(fluidbodyz + fluidpressureslipz) - MIXBROWNIAN*fluidbrownianz); } atomicAdd(&(SPptr[i].newdelpressx), dpx*SPptr[j].mass); //added mass atomicAdd(&(SPptr[i].newdelpressy), dpy*SPptr[j].mass); atomicAdd(&(SPptr[i].newdelpressz), dpz*SPptr[j].mass); atomicAdd(&(SPptr[i].newdens), k *(1 + float(!(SPptr[i]).boundary)*float((SPptr[j]).boundary)*BDENSFACTOR)*SPptr[j].mass); //added mass atomicAdd(&(SPptr[i].diffusionx), SPptr[j].mass / SPptr[j].dens*dkx*!SPptr[j].boundary*!SPptr[i].boundary); //added mass atomicAdd(&(SPptr[i].diffusiony), SPptr[j].mass / SPptr[j].dens*dky*!SPptr[j].boundary*!SPptr[i].boundary); atomicAdd(&(SPptr[i].diffusionz), SPptr[j].mass / SPptr[j].dens*dkz*!SPptr[j].boundary*!SPptr[i].boundary); volatile float mixfactor = (!SPptr[j].boundary)*(!SPptr[i].boundary)*(SPptr[i].solid > 0.0)* (SPptr[j].solid > 0.0) * 2 * (SPptr[i].solid-0.0)*(SPptr[j].solid-0.0) / (SPptr[i].solid-0.0 + SPptr[j].solid-0.0+0.01); atomicAdd(&(SPptr[i].vel_grad[0][0]), -mixfactor*vabx*dkx *1./ (SPptr[i]).dens); //added mass atomicAdd(&(SPptr[i].vel_grad[0][1]), -mixfactor*vaby*dkx *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[0][2]), -mixfactor*vabz*dkx *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[1][0]), -mixfactor*vabx*dky *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[1][1]), -mixfactor*vaby*dky *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[1][2]), -mixfactor*vabz*dky *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[2][0]), -mixfactor*vabx*dkz *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[2][1]), -mixfactor*vaby*dkz *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].vel_grad[2][2]), -mixfactor*vabz*dkz *1./ (SPptr[i]).dens); atomicAdd(&(SPptr[i].stress_accel[0]), mixfactor*((SPptr[i]).stress_tensor[0][0] * dkx + (SPptr[i]).stress_tensor[0][1] * dky + (SPptr[i]).stress_tensor[0][2] * dkz) / pow((SPptr[i]).dens, 2) + ((SPptr[i]).stress_tensor[0][0] * dkx + (SPptr[i]).stress_tensor[0][1] * dky + (SPptr[i]).stress_tensor[0][2] * dkz) / pow((SPptr[i]).dens, 2)); atomicAdd(&(SPptr[i].stress_accel[1]), mixfactor*((SPptr[i]).stress_tensor[1][0] * dkx + (SPptr[i]).stress_tensor[1][1] * dky + (SPptr[i]).stress_tensor[1][2] * dkz) / pow((SPptr[i]).dens, 2) + ((SPptr[i]).stress_tensor[1][0] * dkx + (SPptr[i]).stress_tensor[1][1] * dky + (SPptr[i]).stress_tensor[1][2] * dkz) / pow((SPptr[i]).dens, 2)); atomicAdd(&(SPptr[i].stress_accel[2]), mixfactor*((SPptr[i]).stress_tensor[2][0] * dkx + (SPptr[i]).stress_tensor[2][1] * dky + (SPptr[i]).stress_tensor[2][2] * dkz) / pow((SPptr[i]).dens, 2) + ((SPptr[i]).stress_tensor[2][0] * dkx + (SPptr[i]).stress_tensor[2][1] * dky + (SPptr[i]).stress_tensor[2][2] * dkz) / pow((SPptr[i]).dens, 2)); volatile float ds2 = dot_prod(SPptr[j].soliddriftvelx, SPptr[j].soliddriftvely, SPptr[j].soliddriftvelz, dkx, dky, dkz); volatile float ds = dot_prod(SPptr[i].soliddriftvelx, SPptr[i].soliddriftvely, SPptr[i].soliddriftvelz, dkx, dky, dkz); volatile float df2 = dot_prod(SPptr[j].fluiddriftvelx, SPptr[j].fluiddriftvely, SPptr[j].fluiddriftvelz, dkx, dky, dkz); volatile float df = dot_prod(SPptr[i].fluiddriftvelx, SPptr[i].fluiddriftvely, SPptr[i].fluiddriftvelz, dkx, dky, dkz); atomicAdd(&(SPptr[i].mixture_accel[0]), -1 / SPptr[i].dens / SPptr[j].dens*(SPptr[j].solid*SPptr[j].dens*(SPptr[j].solid*SPptr[j].soliddriftvelx*ds2 + SPptr[i].solid*SPptr[i].soliddriftvelx*ds) + SPptr[j].fluid*SPptr[j].dens*(SPptr[j].fluid*SPptr[j].fluiddriftvelx*df2 + SPptr[i].fluid*SPptr[i].fluiddriftvelx*df))); atomicAdd(&(SPptr[i].mixture_accel[1]), -1 / SPptr[i].dens / SPptr[j].dens*(SPptr[j].solid*SPptr[j].dens*(SPptr[j].solid*SPptr[j].soliddriftvely*ds2 + SPptr[i].solid*SPptr[i].soliddriftvely*ds) + SPptr[j].fluid*SPptr[j].dens*(SPptr[j].fluid*SPptr[j].fluiddriftvely*df2 + SPptr[i].fluid*SPptr[i].fluiddriftvely*df))); atomicAdd(&(SPptr[i].mixture_accel[2]), -1 / SPptr[i].dens / SPptr[j].dens*(SPptr[j].solid*SPptr[j].dens*(SPptr[j].solid*SPptr[j].soliddriftvelz*ds2 + SPptr[i].solid*SPptr[i].soliddriftvelz*ds) + SPptr[j].fluid*SPptr[j].dens*(SPptr[j].fluid*SPptr[j].fluiddriftvelz*df2 + SPptr[i].fluid*SPptr[i].fluiddriftvelz*df))); atomicAdd(&(SPptr[i].delsolid), (!SPptr[j].boundary) *(!SPptr[i].boundary) *-0.5 / SPptr[j].dens*(SPptr[i].solid + SPptr[j].solid) * (dkx*vabx + dky*vaby + dkz*vabz) + (-(SPptr[i].solid*SPptr[i].soliddriftvelx + SPptr[j].solid*SPptr[j].soliddriftvelx)*dkx - (SPptr[i].solid*SPptr[i].soliddriftvely + SPptr[j].solid*SPptr[j].soliddriftvely)*dky - (SPptr[i].solid*SPptr[i].soliddriftvelz + SPptr[j].solid*SPptr[j].soliddriftvelz)*dkz) / SPptr[j].dens); atomicAdd(&(SPptr[i].delfluid), (!SPptr[j].boundary) *(!SPptr[i].boundary) *-0.5 / SPptr[j].dens*(SPptr[i].fluid + SPptr[j].fluid) * (dkx*vabx + dky*vaby + dkz*vabz) + (-(SPptr[i].fluid*SPptr[i].fluiddriftvelx + SPptr[j].fluid*SPptr[j].fluiddriftvelx)*dkx - (SPptr[i].fluid*SPptr[i].fluiddriftvely + SPptr[j].fluid*SPptr[j].fluiddriftvely)*dky - (SPptr[i].fluid*SPptr[i].fluiddriftvelz + SPptr[j].fluid*SPptr[j].fluiddriftvelz)*dkz) / SPptr[j].dens); } } } } } } } __syncthreads(); if (idx < nspts) { if ((SPptr[idx]).solid) { float tr = 0; //trace of strain rate float tr2 = 0; //trace of stress tensor float tr3 = 0; //double dot of stress tensor float tr4 = 0; //trace of stress tensor times strain rate float tr5 = 0; //double dot of strain rate for (int p = 0; p < 3; p++) { for (int q = 0; q < 3; q++) { (SPptr[idx]).strain_rate[p][q] = 0.5*((SPptr[idx]).vel_grad[p][q] + (SPptr[idx]).vel_grad[q][p]); tr3 += 0.5*(SPptr[idx]).stress_tensor[p][q]*(SPptr[idx]).stress_tensor[p][q]; tr5 += (SPptr[idx]).strain_rate[p][q]*(SPptr[idx]).strain_rate[p][q]; tr4 += (SPptr[idx]).stress_tensor[p][q] * (SPptr[idx]).strain_rate[q][p]; } tr += (SPptr[idx]).strain_rate[p][p]; tr2 += (SPptr[idx]).stress_tensor[p][p]; } // std::cout << (SPptr[index]).press << "\n"; for (int p = 0; p < 3; p++) { for (int q = 0; q < 3; q++) { if (3 * tan(PHI) / (sqrt(9 + 12 * pow(tan(PHI), 2)))*(SPptr[idx]).press*(SPptr[idx].press>0) + KC / (sqrt(9 + 12 * pow(tan(PHI), 2))) < tr3 && tr3 != 0) { (SPptr[idx]).stress_tensor[p][q] *= (3 * tan(PHI) / (sqrt(9 + 12 * pow(tan(PHI), 2)))*(SPptr[idx]).press*(SPptr[idx].press>0) + KC / (sqrt(9 + 12 * pow(tan(PHI), 2)))) / tr3; } (SPptr[idx]).stress_rate[p][q] = 3 * C1*((SPptr[idx]).press)*((SPptr[idx]).strain_rate[p][q] - 1. / 3.*tr*(p == q)) + C1*C2*(tr4 + tr*(SPptr[idx]).press*(SPptr[idx].press>0)) / (pow((SPptr[idx]).press, 2) + 1e8)*(SPptr[idx]).stress_tensor[p][q] - C1*C3*sqrt(tr5)*(SPptr[idx]).stress_tensor[p][q]; //std::cout << tr4 << ", " << tr*(SPptr[index]).press << "\n"; } } } //} } __syncthreads(); }
21,515
#include "includes.h" __global__ void misaligned_write_test(float* a, float* b, float *c, int size, int offset) { int gid = blockIdx.x * blockDim.x + threadIdx.x; int k = gid + offset; if (k < size) c[k] = a[gid] + b[gid]; }
21,516
/* #include "headers/myHeaders.h" #include "headers/myUtilityFunctions.h" using namespace std; int main(int argc, char const *argv[]) { //create array at host : initialize accordingly cellType *h_array; h_array = create_array_host(); //initialize base row arguments (cellType *h_array, int rowNumber, int mode, int value) // : mode =1 for random initialization, put any value in that case initialize_this_row(h_array, 0, 1, -1); //Create array at device cellType *d_array; cudaMalloc((void**) &d_array, sizeof(cellType)*(nRows*TOTAL_COLS)); //copy host array to device arrray, if needed copy_host_to_device(h_array, d_array); //configure kernel configure_kernal(TOTAL_COLS); GpuTimer phase1; phase1.Start(); //execute on GPU, row by row for (int i = 1; i < nRows; ++i) { update_array_gpu_hybrid<<<dim3(g,1,1), dim3(x,1,1)>>>(i, TOTAL_COLS, d_array); //update_array_cpu(i, h_array); } phase1.Stop(); cout <<"Time (Basic GPU): " <<phase1.Elapsed()<< " Milli Seconds\n"; //copy back to cpu copy_device_to_host(h_array, d_array); //Access the resultant matrix : dump into output file //write_array_console(h_array); ofstream myfile ("files_output/o_gpu_basic.txt"); write_array_file(h_array, myfile); return 0; } __global__ void update_array_gpu_hybrid(int i, int numberOfThreadsRequired, cellType *d_array ) { long j=blockIdx.x *blockDim.x + threadIdx.x + 1; if (j>= numberOfThreadsRequired || j < dependencyWidthLeft) {} else { d_array(i,j)= (d_array(i-1,j) + d_array(i-1,j-1) + d_array(i-1,j-2) + 1) % 10; } } void update_array_cpu_hybrid(int i, cellType *h_array) { //#pragma omp parallel for for (int j = dependencyWidthLeft; j < nCols + dependencyWidthLeft ; ++j) { h_array(i,j)= (h_array(i-1,j) + h_array(i-1,j-1) + h_array(i-1,j-2) + 1) % 10; } }*/
21,517
#define N 512 #define NUM_BLOCKS 16 #define NUM_THREADS 48 //Do not change above three lines. //Submission should be named as <RollNumber>_Prog.cu //Upload just this cu file and nothing else. If you upload it as a zip, it will not be evaluated. /*Remember the following guidelines to avoid losing marks This exercise is quite simple. The only tricky part is that total number of threads (NUM_BLOCKS*NUM_THREADS) may be different (higher or lower) from N. Index of an array should not exceed the array size. No output array-element should be computed more than once No marks will be given if the program does not compile or run (TAs will not debug your program at all) Do not change the name of any variable that we have introduced. */ #include <stdio.h> //TODO: WRITE GPU KERNEL. It should not be called repeatedly from the host, but just once. Each time it is called, it may process more than array-element or not process any array-element at all. __global__ void add(int *a,int *b,int *c){ int index1 = threadIdx.x + blockIdx.x *blockDim.x; int totalthreads=NUM_BLOCKS*NUM_THREADS; if(totalthreads>=N) { if (index1 < N) { for(int i=0;i<N;i++) { *(c+index1*N+i) = *(a+index1*N+i) + *(b+index1*N+i); } } } else if(totalthreads<N) { if(index1<totalthreads-1) { for(int i=0;i<N;i++) { *(c+index1*N+i) = *(a+index1*N+i) + *(b+index1*N+i); } } else if(index1==totalthreads-1) { for(int j=index1;j<N;j++) { for(int i=0;i<N;i++) { *(c+j*N+i) = *(a+j*N+i) + *(b+j*N+i); } } } } } int main (int argc, char **argv) { int A[N][N], B[N][N], C[N][N]; int *d_A, *d_B, *d_C; // These are the copies of A, B and C on the GPU int *h_C; // This is a host copy of the output of B from the GPU int i, j; for(i=0;i<N;i++) { for(j=0;j<N;j++) { A[i][j] = i+j; B[i][j]= 2*j-1; } } // sequential implementation of main computation for(i=0;i<N;i++) { for(j=0;j<N;j++) { C[i][j] = A[i][j]+B[i][j]; } } int size = N * N * sizeof(int); h_C=(int *)malloc(size); // TODO: ALLOCATE MEMORY FOR GPU COPIES OF d_A, d_B and d_C cudaMalloc((void **)&d_A, size); cudaMalloc((void **)&d_B, size); cudaMalloc((void **)&d_C, size); // TODO: COPY A TO d_A // TODO: COPY B TO d_B cudaMemcpy(d_A, A, size, cudaMemcpyHostToDevice); cudaMemcpy(d_B, B, size, cudaMemcpyHostToDevice); // TODO: CREATE BLOCKS with THREADS AND INVOKE GPU KERNEL //Use NUM_BLOCKS blocks, each with NUM_THREADS threads add<<<NUM_BLOCKS,NUM_THREADS>>>(d_A, d_B, d_C); // TODO: COPY d_C BACK FROM GPU to CPU in variable h_C cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost); cudaFree(d_A); cudaFree(d_B); cudaFree(d_C); // TODO: Verify result is correct by comparing for(i=0;i<N;i++) { for(j=0;j<N;j++) { if((*(h_C+i*N+j)-C[i][j])!=0) { printf("error: h_C[%d][%d]!=C[%d][%d] (%d, %d)\n",i,j,i,j,*(h_C+i*N+j),C[i][j]); } //TODO: compare each element of h_C and C by subtracting them //print only those elements for which the above subtraction is non-zero } } //IF even one element of h_C and C differ, report an error. //Otherwise, there is no error. //If your program is correct, no error should occur. }
21,518
#include <stdio.h> template<typename T> __global__ void non_maxima_supression_cuda(T* image_in,T* image_out,int widthImage,int heightImage) { unsigned int x=blockIdx.x*blockDim.x+threadIdx.x; unsigned int y=blockIdx.y*blockDim.y+threadIdx.y; unsigned int index = x * widthImage + y; if(y>0 && y<(widthImage-1) && x>0 && x<(heightImage-1) ){ T curr=image_in[index]; T curr_up=image_in[(x-1)*widthImage+(y)]; T curr_down=image_in[(x+1)*widthImage+(y)]; T curr_up_left=image_in[(x-1)*widthImage+(y-1)]; T curr_up_right=image_in[(x-1)*widthImage+(y+1)]; T curr_down_left=image_in[(x+1)*widthImage+(y-1)]; T curr_down_right=image_in[(x+1)*widthImage+(y+1)]; T curr_left=image_in[(x)*widthImage+(y-1)]; T curr_right=image_in[(x)*widthImage+(y+1)]; T max_element=curr; if(curr_up>max_element)max_element=curr_up; if(curr_down>max_element)max_element=curr_down; if(curr_down_left>max_element)max_element=curr_down_left; if(curr_down_right>max_element)max_element=curr_down_right; if(curr_up_left>max_element)max_element=curr_up_left; if(curr_up_right>max_element)max_element=curr_up_right; if(curr_left>max_element)max_element=curr_left; if(curr_right>max_element)max_element=curr_right; if ( max_element != curr ||max_element==curr_up||max_element==curr_up_left||max_element==curr_up_right||max_element==curr_left ) image_out[ index ] = 0; else image_out[ index ] = curr; } else { image_out[index]=0; } } template <typename T> void calculate_non_maxima_cuda(T* image_in, T* image_out, int heightImage, int widthImage, int threadsX, int threadsY,cudaStream_t stream) { dim3 block(threadsX, threadsY, 1); dim3 grid(heightImage / block.x, widthImage / block.y, 1); non_maxima_supression_cuda<<<grid,block,0,stream>>>(image_in, image_out, widthImage, heightImage); }
21,519
/* This is a automatically generated test. Do not modify */ #include <stdio.h> #include <stdlib.h> #include <math.h> __global__ void compute(float comp, int var_1,float var_2,float var_3,float var_4,float var_5,float var_6,float* var_7,float var_8,float var_9,float var_10,float var_11,float var_12,float var_13,float var_14,float var_15,float var_16) { comp += (+1.5541E-37f + -1.3636E34f); comp = ldexpf((var_2 / (var_3 * atan2f(-1.0594E-41f - +1.6283E-36f * -1.9377E-21f - (var_4 / (var_5 / +1.8236E-43f)), -1.5945E-36f + var_6 + (-1.3738E16f / +1.1848E36f)))), 2); for (int i=0; i < var_1; ++i) { comp = var_8 * +1.1670E34f - +1.4449E6f; var_7[i] = fmodf(powf(-1.6888E-41f, +1.7914E-37f), +0.0f); comp = var_7[i] - (-1.7142E-14f / -0.0f * cosf(fabsf(floorf((+1.6936E-9f + +1.6304E34f - +1.2672E-41f))))); comp = var_9 * -0.0f * -1.6677E-36f; } if (comp > +0.0f + (-1.7399E-44f - floorf(var_10 / var_11))) { comp = tanhf((var_12 - +1.7844E34f)); comp = (+0.0f / sqrtf(var_13 - (var_14 / (-0.0f * acosf(+1.6184E-36f))))); float tmp_1 = -1.8708E-37f; comp = tmp_1 + (var_15 / var_16); } printf("%.17g\n", comp); } float* initPointer(float v) { float *ret = (float*) malloc(sizeof(float)*10); for(int i=0; i < 10; ++i) ret[i] = v; return ret; } int main(int argc, char** argv) { /* Program variables */ float tmp_1 = atof(argv[1]); int tmp_2 = atoi(argv[2]); float tmp_3 = atof(argv[3]); float tmp_4 = atof(argv[4]); float tmp_5 = atof(argv[5]); float tmp_6 = atof(argv[6]); float tmp_7 = atof(argv[7]); float* tmp_8 = initPointer( atof(argv[8]) ); float tmp_9 = atof(argv[9]); float tmp_10 = atof(argv[10]); float tmp_11 = atof(argv[11]); float tmp_12 = atof(argv[12]); float tmp_13 = atof(argv[13]); float tmp_14 = atof(argv[14]); float tmp_15 = atof(argv[15]); float tmp_16 = atof(argv[16]); float tmp_17 = atof(argv[17]); compute<<<1,1>>>(tmp_1,tmp_2,tmp_3,tmp_4,tmp_5,tmp_6,tmp_7,tmp_8,tmp_9,tmp_10,tmp_11,tmp_12,tmp_13,tmp_14,tmp_15,tmp_16,tmp_17); cudaDeviceSynchronize(); return 0; }
21,520
__global__ void div_kernel(int n, const float *x, const float *y, float *z) { int i = blockIdx.x*blockDim.x + threadIdx.x; if (i < n) z[i] = x[i] / y[i]; } void div(int n, const float *x, const float *y, float *z) { div_kernel<<<(n+255)/256, 256>>>(n, x, y, z); }
21,521
#include <stdio.h> #define N 5 __global__ void cuda_hello() { printf("Hello world from GPU!\n"); } __global__ void vector_add(float *out, float *a, float *b, int n) { for (int i = 0; i < n; ++i) out[i] = a[i] + b[i]; } int main() { cuda_hello<<<1, 1>>>(); printf("Hello world from host\n"); float a[N], b[N], out[N]; float *dev_a, *dev_b, *dev_out; cudaMalloc((void **)&dev_a, N * sizeof(float)); cudaMalloc((void **)&dev_b, N * sizeof(float)); cudaMalloc((void **)&dev_out, N * sizeof(float)); for (int i = 0; i < N; ++i) a[i] = b[i] = i; cudaMemcpy(dev_a, a, N * sizeof(float), cudaMemcpyHostToDevice); cudaMemcpy(dev_b, b, N * sizeof(float), cudaMemcpyHostToDevice); vector_add<<<1, 1>>>(dev_out, dev_a, dev_b, N); cudaMemcpy(out, dev_out, N * sizeof(float), cudaMemcpyDeviceToHost); for (int i = 0; i < N; ++i) printf("%f + %f = %f\n", a[i], b[i], out[i]); cudaFree(dev_a); cudaFree(dev_b); cudaFree(dev_out); return 0; }
21,522
/* * testCuda.cu * Copyright (C) 2016 <@BLUEYI-PC> * * Distributed under terms of the MIT license. */ #include <iostream> struct XYZ { int x; int y; int z; }; __global__ void add(XYZ *a, XYZ *b, XYZ *c) { c->x = a->x + b->x; c->y = a->y + b->y; c->z = a->z + b->z; } int main(void) { XYZ a{1, 2, 3}; XYZ b{1, 2, 3}; XYZ c; XYZ *dev_a; XYZ *dev_b; XYZ *dev_c; cudaMalloc((void**)&dev_a, sizeof(a)); cudaMalloc((void**)&dev_b, sizeof(b)); cudaMalloc((void**)&dev_c, sizeof(c)); cudaMemcpy(dev_a, &a, sizeof(a), cudaMemcpyHostToDevice); cudaMemcpy(dev_b, &b, sizeof(b), cudaMemcpyHostToDevice); add<<<1, 1>>>(dev_a, dev_b, dev_c); cudaMemcpy(&c, &dev_c, sizeof(c), cudaMemcpyDeviceToHost); std::cout << c.x << " " << c.y << " " << c.z << std::endl; cudaFree(dev_a); cudaFree(dev_b); cudaFree(dev_c); return 0; }
21,523
#include "includes.h" // Possible weight coefficients for tracking cost evaluation : // Gaussian discretisation /* * 1 4 6 4 1 * 4 16 24 16 4 * 6 24 36 24 6 * 4 16 24 16 4 * 1 4 6 4 1 */ // Compute spatial derivatives using Scharr operator - Naive implementation.. // Compute spatial derivatives using Scharr operator - Naive implementation.. // Compute spatial derivatives using Sobel operator - Naive implementation.. // Compute spatial derivatives using Sobel operator - Naive implementation.. // Low pass gaussian-like filtering before subsampling // Low pass gaussian-like filtering before subsampling /* // Upsample a picture using the "magic" kernel */ __global__ void kernelSmoothX(float *in, int w, int h, float *out) { int x = blockIdx.x*blockDim.x + threadIdx.x; int y = blockIdx.y*blockDim.y + threadIdx.y; if(x >= w || y >= h) return; int idx = y*w; int a = x-2; int b = x-1; int c = x; int d = x+1; int e = x+2; if(a < 0) a = 0; if(b < 0) b = 0; if(d >= w) d = w-1; if(e >= w) e = w-1; out[y*w+x] = 0.0625f*in[idx+a] + 0.25f*in[idx+b] + 0.375f*in[idx+c] + 0.25f*in[idx+d] + 0.0625f*in[idx+e]; }
21,524
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <cuda.h> #include <cuda_runtime.h> #include <cufft.h> #include <cuComplex.h> #define nThrdsX 16 #define nThrdsY 16 #define nThrds 256 #define clight 299792458 #define PI 3.14159265358979323846 #define cmtops (2*PI*clight*1.e-10) __device__ __forceinline__ cuComplex my_cexpc (cuComplex z) { cuComplex res; float t = expf(z.x); res.x=cosf(z.y); res.y=sinf(z.y); res.x *= t; res.y *= t; return res; } __device__ __forceinline__ cuComplex my_cexpf (float z) { cuComplex res; res.x=expf(z); res.y=0.f; return res; } __device__ __forceinline__ cuComplex my_cexpi (float z) { cuComplex res; res.x=cosf(z); res.y=sinf(z); return res; } __device__ float g(float t, float* d_param) { float f1,f2,f3,f4; f1=d_param[3]*expf(-t/d_param[6]); f2=d_param[4]*expf(-t/d_param[7]); f3=d_param[5]*expf(-t/d_param[8]); f4=(d_param[0]+d_param[1]+d_param[2])*t-(d_param[3]+d_param[4]+d_param[5]); return ( f1 + f2 + f3 + f4 ); } __device__ cuComplex Rr(float t1 ,float t2, float t3, float* d_param) { cuComplex f1; cuComplex f2; f1 = my_cexpi( -( t3 - t1 ) * d_param[9] * cmtops ); f1 = cuCsubf(f1 , cuCmulf( my_cexpi( -cmtops * ( ( d_param[9] - d_param[10] ) * t3 - ( d_param[9] * t1 ) ) ) , my_cexpf( -(d_param[12]*t3)/(2.*d_param[11]) ) ) ); f1 = cuCmulf(f1 , my_cexpf( -( t1 + t3 + (2.*t2) ) / ( 2.*d_param[11] ) ) ); f2 = my_cexpf(-g(t1,d_param) + g(t2,d_param) - g(t3,d_param) - g(t1+t2,d_param) - g(t2+t3,d_param) + g(t1+t2+t3,d_param) ); return cuCmulf( f1 , f2 ); } __device__ cuComplex Rnr(float t1 ,float t2, float t3, float* d_param) { cuComplex f1; cuComplex f2; f1 = my_cexpi( -( t3 + t1 ) * d_param[9] * cmtops ); f1 = cuCsubf(f1 , cuCmulf( my_cexpi( -cmtops * ( ( d_param[9] - d_param[10] ) * t3 + ( d_param[9] * t1 ) ) ) , my_cexpf( -(d_param[12]*t3)/(2.*d_param[11]) ) ) ); f1 = cuCmulf(f1 , my_cexpf( -( t1 + t3 + (2.*t2) ) / ( 2.*d_param[11] ) ) ); f2 = my_cexpf( -g(t1,d_param) - g(t2,d_param) - g(t3,d_param) + g(t1+t2,d_param) + g(t2+t3,d_param) - g(t1+t2+t3,d_param) ); return cuCmulf( f1 , f2 ); } __global__ void kernelHalfFirstX(cufftComplex* d_r) { int i = blockIdx.x * blockDim.x + threadIdx.x; cuComplex half; half.x=0.5f; half.y=0.0f; d_r[i]=cuCmulf(d_r[i],half); } __global__ void kernelHalfFirstY(cufftComplex* d_r, int n) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = i*n; cuComplex half; half.x=0.5f; half.y=0.0f; if (j>0) d_r[j]=cuCmulf(d_r[j],half); } __global__ void kernelPermutation(cufftComplex* d_r,cufftComplex* d_ftr, int n) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int k = i * n + j; int l = i * n + j + n/2 ; int m = ( i + n/2 ) * n + j; int p = ( i + n/2 ) * n + j + n/2; d_r[m]=d_ftr[l]; d_r[p]=d_ftr[k]; d_r[l]=d_ftr[m]; d_r[k]=d_ftr[p]; } __global__ void kernelRephasing(cufftComplex* d_r,float dt,float t2,int n,float* d_param) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int k = i * n + j; float t1 = i * dt; float t3 = j * dt; d_r[k]=Rr(t1,t2,t3,d_param); } __global__ void kernelNonRephasing(cufftComplex* d_r, float dt, float t2,int n,float* d_param) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int k= i * n + j; float t1 = i * dt; float t3 = j * dt; d_r[k]=Rnr(t1,t2,t3,d_param); } int main(int argc, char* argv[]) { FILE* input=fopen(argv[1],"r"); FILE* out=fopen("spec.dat","w"); int i,ii,j,l,test; int iw1,iw3,iFirst,iLast,jFirst,jLast; int ndata,nw1,nw3,nave; float t2,dt; float w1min,w1max,w3min,w3max; float w1,w3; float **res; float c1,tau1,c2,tau2,c3,tau3,wm,delta,tLife,alpha; fscanf(input,"%f %f %f %f",&w1min,&w1max,&w3min,&w3max); fscanf(input,"%f %f %f",&delta,&tLife,&alpha); fscanf(input,"%d %f",&ndata,&dt); fscanf(input,"%f",&t2); fscanf(input,"%d",&nave); ndata=(int)(ndata/nThrds)+1; ndata=nThrds*2*(int)(ndata/2); printf("FFT arrays size: %d, Number of cuda threads: %d\n",ndata,nThrds); nw1=0; test=1; for(i=ndata/2-1;i<ndata-1;i++) { w1=1.e10/((float)ndata*clight*dt)*(float)(1-ndata/2+i); if(w1>w1max) { iLast=i-1; break; } if( w1>=w1min ) { nw1++; if(test) { test=0; iFirst=i; } } } nw3=0; test=1; for(j=ndata/2-1;j<ndata-1;j++) { w3=1.e10/((float)ndata*clight*dt)*(float)(1-ndata/2+j); if(w3>w3max) { jLast=j-1; break; } if( w3>=w3min ) { nw3++; if(test) { test=0; jFirst=j; } } } res=(float**)malloc(nw1*sizeof(float*)); for(i=0;i<nw1;i++) { res[i]=(float*)malloc(nw3*sizeof(float)); for(j=0;j<nw3;j++) res[i][j]=0.0f; } float *param,*d_param; param=(float*)malloc(13*sizeof(float)); cudaMalloc((void**) &d_param,13*sizeof(float)); param[10]=delta; param[11]=tLife; param[12]=alpha; cufftComplex *d_rlist,*d_ftrlist,*rlist; cufftHandle d_pr; rlist=(cufftComplex*)malloc(ndata*ndata*sizeof(cufftComplex)); cudaMalloc((void**) &d_rlist,ndata*ndata*sizeof(cufftComplex)); cudaMalloc((void**) &d_ftrlist,ndata*ndata*sizeof(cufftComplex)); cufftPlan2d(&d_pr,ndata,ndata,CUFFT_C2C); dim3 threadsPerBlock(nThrdsX, nThrdsY); dim3 numBlocks(ndata / threadsPerBlock.x, ndata / threadsPerBlock.y); dim3 numHalfBlocks( (ndata/2) / threadsPerBlock.x, (ndata/2) / threadsPerBlock.y ); for(ii=0;ii<nave;ii++) { fscanf(input,"%f",&wm); fscanf(input,"%f",&c1); fscanf(input,"%f",&c2); fscanf(input,"%f",&c3); fscanf(input,"%f",&tau1); fscanf(input,"%f",&tau2); fscanf(input,"%f",&tau3); param[0]=c1*tau1; param[1]=c2*tau2; param[2]=c3*tau3; param[3]=param[0]*tau1; param[4]=param[1]*tau2; param[5]=param[2]*tau3; param[6]=tau1; param[7]=tau2; param[8]=tau3; param[9]=wm; cudaMemcpy(d_param,param,13*sizeof(float),cudaMemcpyHostToDevice); // Rephasing component kernelRephasing<<<numBlocks,threadsPerBlock>>>(d_rlist,dt,t2,ndata,d_param); kernelHalfFirstX<<<(ndata / threadsPerBlock.x),threadsPerBlock>>>(d_rlist); kernelHalfFirstY<<<(ndata / threadsPerBlock.x),threadsPerBlock>>>(d_rlist,ndata); cufftExecC2C(d_pr,d_rlist,d_ftrlist,CUFFT_INVERSE); kernelPermutation<<<numHalfBlocks,threadsPerBlock>>>(d_rlist,d_ftrlist,ndata); cudaMemcpy(rlist,d_rlist,ndata*ndata*sizeof(cufftComplex),cudaMemcpyDeviceToHost); iw1=0; for(i=iFirst;i<=iLast;i++) { iw3=0; for(j=jFirst;j<=jLast;j++) { l=ndata*(ndata-i-1)+j+1; res[iw1][iw3]+=cuCrealf(rlist[l]); iw3++; } iw1++; } // Non-rephasing component kernelNonRephasing<<<numBlocks,threadsPerBlock>>>(d_rlist,dt,t2,ndata,d_param); kernelHalfFirstX<<<(ndata / threadsPerBlock.x),threadsPerBlock>>>(d_rlist); kernelHalfFirstY<<<(ndata / threadsPerBlock.x),threadsPerBlock>>>(d_rlist,ndata); cufftExecC2C(d_pr,d_rlist,d_ftrlist,CUFFT_INVERSE); kernelPermutation<<<numHalfBlocks,threadsPerBlock>>>(d_rlist,d_ftrlist,ndata); cudaMemcpy(rlist,d_rlist,ndata*ndata*sizeof(cufftComplex),cudaMemcpyDeviceToHost); iw1=0; for(i=iFirst;i<=iLast;i++) { iw3=0; for(j=jFirst;j<=jLast;j++) { l=ndata*(i+1)+j+1; res[iw1][iw3]+=cuCrealf(rlist[l]); iw3++; } iw1++; } } cufftDestroy(d_pr); cudaFree(d_rlist); cudaFree(d_ftrlist); iw1=0; for(i=iFirst;i<=iLast;i++) { w1=1.e10/((float)ndata*clight*dt)*(float)(1-ndata/2+i); iw3=0; for(j=jFirst;j<=jLast;j++) { w3=1.e10/((float)ndata*clight*dt)*(float)(1-ndata/2+j); fprintf(out,"%f\t%f\t%e\n",w1,w3,res[iw1][iw3]/(float)ndata); iw3++; } fprintf(out,"\n"); iw1++; } for(i=0;i<nw1;i++) free(res[i]); free(res); fclose(out); fclose(input); return 0; }
21,525
#include "includes.h" #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif __global__ void mandelbrot(int* A, const int N, const int largeur, const int hauteur){ int y = threadIdx.y + (blockIdx.y * blockDim.y);; int x = threadIdx.x + (blockIdx.x * blockDim.x);; if (y < hauteur && x < largeur) { int cpt = 0; float x1 = 0.f; float y1 = 0.f; float x2 = 0.f; float y2 = 0.f; float a = 4.f * x / largeur - 2.f; float b = 4.f * y / hauteur - 2.f; float val = x1* x1 + y1 * y1; while (cpt < N && val <= 4.) { cpt ++; x2 = x1* x1 - y1 * y1 + a; y2 = 2. * x1 * y1 + b; x1 = x2; y1 = y2; val = x1* x1 + y1 * y1; } A[y*hauteur+x] = cpt; } }
21,526
#include "includes.h" //Library Definition //Constant Definition #define PI 3.141592654 #define blocksize 32 #define Repetitions 8192 //Print matrix into standard output void print(double * M,int cols,int rows); void dot(double * a,double * b, double & c, int cols); void Create_New_Matrix(double * M,double * New,int * vec, int p0, int pp,int nn); /* DEVICE FUNCTIONS */ //Matrix transposition (Rows and Cols of M) __global__ void NOVA(double * Beta,double * Inverse,int * Vec, int p0,double Sigma2) { int x = blockIdx.x * blockDim.x + threadIdx.x; double t0,Pvalue; t0=Beta[x]/sqrt(Sigma2*Inverse[x*p0+x]); Pvalue=2.*(1.-erf(t0)); if(Pvalue<0.25) { Vec[x]=1; } else { Vec[x]=0; } }
21,527
__global__ void sparseMatrixVectorSetFlags( unsigned int *d_flags, const unsigned int *d_rowindx, unsigned int numRows ) { unsigned int iGlobal = (blockIdx.x * (blockDim.x << 3)) + threadIdx.x; bool isLastBlock = (blockIdx.x == (gridDim.x-1)); for (unsigned int i = 0; i < 8; ++i) { if (isLastBlock) { if (iGlobal < numRows) { d_flags[d_rowindx[iGlobal]] = 1; } } else { d_flags[d_rowindx[iGlobal]] = 1; } iGlobal += blockDim.x; } __syncthreads(); }
21,528
//xfail:BOOGIE_ERROR //--blockDim=32 --gridDim=64 --no-inline //error: possible write-write race on #include <stdio.h> #include "cuda.h" #include <assert.h> #define M 2//32 #define N 4//64 __global__ void foo(int* p) { __shared__ unsigned char x[N]; for (unsigned int i=0; i<(N/4); i++) { ((unsigned int*)x)[i] = 1;//0; } /* for (int i = 0; i < N/4; i++) { p[i] = x[i]; } */ }
21,529
#include "processing.cuh" #include "Settings.cuh" #include <algorithm> #include <fstream> void processing_core(float ** field, const Analysis & A) { printf("Processing...\n"); int sz = A.size_i/* / A.dim_sieve*/; FloatMatrix source(sz), filtered; for (int i = 0; i < sz; i++) { source[i] = FloatVector(A.times); for (int t = 0; t < A.times; t++) { source[i][t] = fabs(field[t][i]); } } filtered = source; printf("Converting done.\n"); // free memory delete[] field; printf("Executing of specified methods...\n"); // *************** Perform processing for each coordinate *************** FloatVector sd_tau(sz), cf_tau(sz), arc_tau(sz); for (int i = 0; i < sz; i++) { // **** filtering **** filter(source[i], filtered[i], A.filter_type, A.filter_win, A.filter_runs); // **** tau calculation **** if (A.sec_der) { sd_tau[i] = A.begin_after + sd_method(filtered[i], A); } if (A.cf) { cf_tau[i] = A.begin_after + cf_method(filtered[i], A); } if (A.arc) { arc_tau[i] = A.begin_after + arc_method(filtered[i], A); } } // save original field if (A.save_original) { fprintf("original_field.txt", source, A.time_sieve, A.dim_sieve); printf("Original field saved.\n"); } // save filtered field if (A.save_filtered) { fprintf("filtered_field.txt", filtered, A.time_sieve, A.dim_sieve); printf("Filtered field saved.\n"); } // save tau results in files if (A.sec_der) fprintf("SD.txt", sd_tau, A.dim_sieve, A.pos_i); if (A.cf) fprintf("CF.txt", cf_tau, A.dim_sieve, A.pos_i); if (A.arc) fprintf("ARC.txt", arc_tau, A.dim_sieve, A.pos_i); printf("TOFs saved in files.\n"); // simulation of detector behaviour if (A.sim_detect) { printf("Processing simulation of detector behaviour...\n"); FloatMatrix real_detector(A.size_i - A.detector_size, FloatVector(A.times, 0)); filtered = real_detector; FloatVector det_weights(A.detector_size, 1.0/A.detector_size); float buf = 0; if (A.load_weights) { std::ifstream ist("DetWeights.txt"); for (int i = 0; ist && i < A.detector_size; i++) { if (!ist.eof()) { ist >> det_weights[i]; } else { det_weights = FloatVector(A.detector_size, 1); break; } } ist.close(); } int half_size = A.detector_size / 2; for (int t = 0; t < A.times; t++) { for (int i = 0; i < sz - A.detector_size; i++) { buf = 0; for (int j = -half_size; j < A.detector_size - half_size; j++) { buf += source[i + half_size + j][t] * det_weights[j + half_size]; } real_detector[i][t] = buf / A.detector_size; } } sd_tau = FloatVector(sz - A.detector_size), cf_tau = sd_tau, arc_tau = sd_tau; for (int i = 0; i < (sz - A.detector_size); i++) { // **** filtering **** filter(real_detector[i], filtered[i], A.filter_type, A.filter_win, A.filter_runs); // **** tau calculation **** if (A.sec_der) { sd_tau[i] = A.begin_after + sd_method(filtered[i], A); } if (A.cf) { cf_tau[i] = A.begin_after + cf_method(filtered[i], A); } if (A.arc) { arc_tau[i] = A.begin_after + arc_method(filtered[i], A); } } printf("Detector simulation OK.\n"); // save original field from detector sim if (A.save_detector && A.save_original) { fprintf("detector_original_field.txt", real_detector, A.time_sieve, A.dim_sieve); printf("Original real detector field saved.\n"); } // save filtered field if (A.save_detector && A.save_filtered) { fprintf("detector_filtered_field.txt", filtered, A.time_sieve, A.dim_sieve); printf("Filtered real detector field saved.\n"); } // save tau results in files if (A.sec_der) fprintf("det_SD.txt", sd_tau, A.dim_sieve, A.pos_i + half_size); if (A.cf) fprintf("det_CF.txt", cf_tau, A.dim_sieve, A.pos_i + half_size); if (A.arc) fprintf("det_ARC.txt", arc_tau, A.dim_sieve, A.pos_i + half_size); printf("Detector sim times saved in files.\n"); } printf("Execution finished.\n"); printf("Processing done.\n"); } void filter(FloatVector & src, FloatVector & dst, int filter_type, int win_size, int iters) { FloatVector tmp = src; for (int i = 0; i < iters; i++) { switch (filter_type) { // moving average case 1: moving_average(tmp, dst, win_size); tmp = dst; break; // medfilt case 2: medfilt(tmp, dst, win_size); tmp = dst; break; // no_filter default: break; } } } void moving_average(FloatVector & src, FloatVector & dst, int win_size) { float buf = 0; int half_win = int(win_size / 2); int times = src.size(); for (int t = 0; t < win_size - 1; t++) { buf += src[t]; } for (int t = win_size - 1; t < times; t++) { buf += src[t]; dst[t - half_win] = buf / win_size; buf -= src[t - win_size + 1]; } for (int t = half_win; t > 0; t--) { dst[t] = dst[half_win - 1]; dst[times - t] = dst[times - half_win - 1]; } } void medfilt(FloatVector & src, FloatVector & dst, int win_size) { FloatVector buf, tmp; int half_win = int(win_size / 2); int times = src.size(); for (int t = 0; t < win_size - 1; t++) { buf.push_back(src[t]); } for (int t = win_size - 1; t < times; t++) { buf.push_back(src[t]); tmp = buf; //sort(tmp); std::sort(tmp.begin(), tmp.end()); dst[t - half_win] = tmp[half_win]; buf.erase(buf.begin()); } for (int t = half_win; t > 0; t--) { dst[t] = dst[half_win - 1]; dst[times - t] = dst[times - half_win - 1]; } } int sd_method(FloatVector & src, const Analysis & A) { FloatVector tmp1 = src, tmp2 = tmp1; float min_level = A.tau_threshold * max(src); sec_diff(src, tmp1); filter(tmp1, tmp2, A.sdf_type, A.sdf_win, A.sdf_runs); for (int i = 0; i < src.size() - 1; i++) { if (src[i] > min_level) { if (tmp2[i] * tmp2[i + 1] < 0) return i; } } return src.size() - 1; } int cf_method(FloatVector & src, const Analysis & A) { FloatVector tmp1 = src, tmp2 = tmp1; float min_level = A.tau_threshold * max(src), p1, p2; for (int t = 0; t < src.size() - (A.cf_tau + 1); t++) { if (src[t] > min_level) { p1 = src[t] + A.cf_k * src[t + A.cf_tau]; p2 = src[t + 1] + A.cf_k * src[t + A.cf_tau + 1]; // if (p1 * p2 < 0) return t; } } return src.size() - 1; } int arc_method(FloatVector & src, const Analysis & A) { FloatVector tmp1 = src, tmp2 = tmp1; float min_level = A.tau_threshold * max(src), p1, p2; for (int t = 0; t < src.size() - (A.arc_tau + 1); t++) { if (src[t] > min_level) { p1 = src[t] + A.arc_k * src[t + A.arc_tau]; p2 = src[t + 1] + A.arc_k * src[t + A.arc_tau + 1]; // if (p1 * p2 < 0) return t; } } return src.size() - 1; } void fprintf(const char *fname, FloatVector & v, int sieve = 1, int line_no_from = 0) { FILE * out = fopen(fname, "w"); for (int i = 0; i < v.size(); i += sieve) { fprintf(out, "%d %lf %lf\n", line_no_from + i, v[i], (dt*v[i])*TIME_PRINT_COEF); } fclose(out); } void fprintf(const char *fname, FloatMatrix & m, int t_sieve = 1, int d_sieve = 1) { FILE * out = fopen(fname, "w"); int cols = m.size(); if (!cols) return; int rows = m[0].size(); for (int t = 0; t < rows; t += t_sieve) { for (int i = 0; i < cols; i += d_sieve) { fprintf(out, "%f\t", m[i][t]); } fprintf(out, "\n"); } fclose(out); } void sec_diff(FloatVector & src, FloatVector & dst) { dst[0] = dst[dst.size() - 1] = 0; for (int i = 1; i < src.size() - 1; i++) { dst[i] = src[i - 1] - 2 * src[i] + src[i + 1]; } } float max(FloatVector & v) { if (!v.size()) return 0; float res = v[0]; for (int i = 1; i < v.size(); i++) { if (v[i] > res) res = v[i]; } return res; }
21,530
/** * André Luiz Abdalla Silveira 8030353 * Mauricio Luiz Cardoso 6796479 * * Esse programa escrito em CUDA visa criar um algoritmo que gera uma redução * de matrizes. Cada matriz é representada por um vetor e todos estão * reunidos num vetor de vetores. A ideia é fazer uma função que faz uma * comparação entre vetores fazendo o mínimo de operações */ #include <stdio.h> #include <stdlib.h> #define E 9 // qtde de elementos de cada matriz #define linhaElementos 3 // quantidade de elementos da linha int numMatrizes; __global__ void os_menores(int *d_matrizes, int posLimite, int jump) { int indexIni = threadIdx.x + blockIdx.x * blockDim.x; for(int i = indexIni; i < posLimite; i += jump) if(d_matrizes[indexIni] > d_matrizes[i]) d_matrizes[indexIni] = d_matrizes[i]; } /* Imprime todas as matrizes de dimensão ExE contidas em matrizes*/ void leitura (int *matrizes, int numMats) { int i, k; for (i = 0; i < numMats * linhaElementos; i++) { for (k = 0; k < linhaElementos; k++) printf("%d\t", *(matrizes++)); printf("\n"); if((i+1) % linhaElementos == 0) printf("********************\n"); } } void menorMatriz(int *d_matrizes, int numMats) { if(numMats > 1) { int numBlocks = 0; int numMatResto; int jump = 0; int numThreads = 0; int posLimite; // carga de tamanho de um bloco if(numMats <= E * 10) { numMatResto = 1; numThreads = E; numBlocks = 1; } else { const int numMatThreads = 3; // 3 foi escolhido para que numthreads seja maior multiplo de E(tamanho de cada matriz) e menor que um warp(32) numThreads = E * numMatThreads; int espacoTrabThre = 10 * numThreads; //cada thread devera comparar ate E * 10 matrizes numBlocks = E * numMats / espacoTrabThre; numMatResto = numBlocks * numMatThreads; } posLimite = numMats * E; jump = numBlocks * numThreads; os_menores<<<numBlocks, numThreads>>>(d_matrizes, posLimite, jump); cudaDeviceSynchronize(); menorMatriz(d_matrizes, numMatResto); } } void encontraMenorMatriz(int* matrizes) { int tam = numMatrizes * E * sizeof(int); int *d_matrizes; // Alloc space for device copies of a, b, c cudaMalloc((void **) &d_matrizes, tam); // Copy inputs to device cudaMemcpy(d_matrizes, matrizes, tam, cudaMemcpyHostToDevice); // encontra menor matriz menorMatriz(d_matrizes, numMatrizes); // Copy result back to host cudaMemcpy(matrizes, d_matrizes, tam, cudaMemcpyDeviceToHost); cudaFree(d_matrizes); } /* Le o arquivo arq que contem matrizes no formato declarado no enunciado e retorna um vetor com todas matrizes lidas*/ int* alocaMatrizesArquivo(FILE *arq){ char asteriscos[10]; int *matrizes, *matrizesAux; fscanf(arq, "%d", &numMatrizes); matrizes = (int *) malloc(E * numMatrizes * sizeof(int)); matrizesAux = matrizes; for(int i = 0; i < numMatrizes; i++) { fscanf(arq, "%s", asteriscos); //pula a linha de asteriscos for(int j = 0; j < E; j++) fscanf(arq, "%d", matrizesAux++); } return matrizes; } int main (int argc, char* argv[]) { if(argc != 2) { printf("Argumento do programa: nome do arquivo\n"); } else { FILE *entrada; entrada = fopen(argv[1], "r"); if (entrada == NULL) { printf("Deu ruim pra abrir o arquivo\n"); return EXIT_FAILURE; } int *matrizes = alocaMatrizesArquivo(entrada); fclose(entrada); encontraMenorMatriz(matrizes); leitura(matrizes, 1); // leitura(get_min(mat, 0, qtde)); free(matrizes); return EXIT_SUCCESS; } }
21,531
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> void query_device() { int deviceCount = 0; // how many cuda devices are installed. cudaGetDeviceCount(&deviceCount); // for mutiple cuda device if (deviceCount == 0) { printf("No CUDA support device found"); }else{ printf("More than one CUDA support device found. Modify the code for others.\n"); } int devNo = 0; // assuming only one cuda device. cudaDeviceProp iProp; cudaGetDeviceProperties(&iProp, devNo); printf("Device %d: %s\n", devNo, iProp.name); printf(" Number of multiprocessors: %d\n", iProp.multiProcessorCount); printf(" clock rate : %d\n", iProp.clockRate); printf(" Compute capability : %d.%d\n", iProp.major, iProp.minor); printf(" Total amount of global memory: %4.2f KB\n", iProp.totalGlobalMem / 1024.0); printf(" Total amount of constant memory: %4.2f KB\n", iProp.totalConstMem / 1024.0); printf(" Total amount of shared memory per block: %4.2f KB\n", iProp.sharedMemPerBlock / 1024.0); printf(" Total amount of shared memory per MP: %4.2f KB\n", iProp.sharedMemPerMultiprocessor / 1024.0); printf(" Total number of registers available per block: %d\n", iProp.regsPerBlock); printf(" Warp size: %d\n", iProp.warpSize); printf(" Maximum number of threads per block: %d\n", iProp.maxThreadsPerBlock); printf(" Maximum number of threads per multiprocessor: %d\n", iProp.maxThreadsPerMultiProcessor); printf(" Maximum number of warps per multiprocessor: %d\n", iProp.maxThreadsPerMultiProcessor / 32); printf(" Maximum Grid size : (%d,%d,%d)\n", iProp.maxGridSize[0], iProp.maxGridSize[1], iProp.maxGridSize[2]); printf(" Maximum block dimension : (%d,%d,%d)\n", iProp.maxThreadsDim[0], iProp.maxThreadsDim[1], iProp.maxThreadsDim[2]); } //int main() //{ // query_device(); //}
21,532
#include "date.cuh" namespace hlp { // CUDA_CALLABLE_MEMBER Date::Date(const char* str_date) { if (3 != sscanf(str_date, "%d-%d-%d;", &(this->year), &(this->month), &(this->day))) printf("error scanning date. format: YYYY-MM-DD\n"); } CUDA_CALLABLE_MEMBER int Date::get_day() const { return this->day; } CUDA_CALLABLE_MEMBER int Date::get_month() const { return this->month; } CUDA_CALLABLE_MEMBER int Date::get_year() const { return this->year; } CUDA_CALLABLE_MEMBER void Date::set_day(int day) { if (day > 0 && day < 32) this->day = day; } CUDA_CALLABLE_MEMBER void Date::set_month(int month) { if (month > 0 and month < 13) this->month = month; } CUDA_CALLABLE_MEMBER void Date::set_year(int year) { if (year > 1900 && year < 2100) this->year = year; } CUDA_CALLABLE_MEMBER bool Date::operator<(const Date& d) const { // check year if (this->year < d.get_year()) return true; else if (this->year > d.get_year()) return false; // check month if (this->month < d.get_month()) return true; else if (this->month > d.get_month()) return false; // check day if (this->day < d.get_day()) return true; return false; } CUDA_CALLABLE_MEMBER bool Date::operator>(const Date& d) const { return !(*this < d); } CUDA_CALLABLE_MEMBER bool Date::operator==(const Date& d) const { return this->year == d.get_year() && this->month == d.get_month() && this->day == d.get_day(); } CUDA_CALLABLE_MEMBER bool Date::operator<=(const Date& d) const { return *this < d || *this == d; } CUDA_CALLABLE_MEMBER bool Date::operator>=(const Date& d) const { return *this > d || *this == d; } CUDA_CALLABLE_MEMBER bool Date::operator!=(const Date& d) const { return !(*this == d); } std::ostream& operator<<(std::ostream& os, const Date& d) { // write obj to stream std::cout << d.get_year() << "-" << d.get_month() << "-" << d.get_day(); return os; } }
21,533
#include <stdio.h> #include <string.h> #include <math.h> /* TODO project: reciprocal */ /* TODO project: negative number human readable */ //KEY CONSTANTS //THIS IS THE STARTING SIZE FOR THE TWO TEMP BUFFERS //THE LARGER YOU EXPECT YOUR NUMBERS TO GROW, INCREASES THE SIZE OF THIS VALUE TO INCREASE PERFORMANCE const unsigned int ORIGINAL_TEMP_BUFFER_SIZE = 1024; //THESE TWO WILL SERVE AS OUR TEMPORARY BUFFERS IN OUR COMPUTATIONS int *temp_buffer1; int *temp_buffer2; int *temp_buffer3; unsigned int temp_buffer_size; //NORMALIZATION KEY VALUES const unsigned int BITS_PER_DIGIT = 32; const unsigned int NORMALIZATION_EXPANSION = (unsigned int)ceil((BITS_PER_DIGIT * log(2.0)) / (log(10.0))); //KEY PROCESSING CONSTANTS //THIS IS THE DEVICE NUMBER THAT WE WILL DO OUR CALCULATIONS ON const int DEVICE_NUM = 0; int MAX_THREADS_PER_BLOCK; //THE FOLLOWING IS HELPFUL INPUT CODE //BCD - binary coded decimal //A BCD IS THE DATA STRUCTURE THAT WE WILL USE TO REPRESENT OUR LARGE NUMBERS //decpos IS THE POSITION OF THE DECIMAL IN THE NUMBER //length IS THE NUMBER OF DIGITS IN THE NUMBER //values IS AN ARRAY OF THE DIGITS //gpuP IS THE POINTER TO THE DIGITS THAT HAVE BEEN COPIED TO THE GPU'S MEMORY typedef struct bcd { unsigned int decpos; unsigned int length; int *values; int *gpuP; } bdc; //THIS TAKES A STRING REPRESENTATION OF OUR NUMBER, SUCH AS "123456544.23" AND LOADS IT INTO A BCD void bcdFromString(char* input, bcd* output); //THIS CREATES A BCD THAT CAN STORE A NUMBER WITH len DIGITS bcd* createBcd(unsigned int len); //THIS PRINTS A BCD OUT TO THE CONSOLE void printBcd(bcd* input); void printBcdNotNormal(bcd* input); void zeroBcd(bcd* input); void freeBcd(bcd* input); //THE IMPLEMENTATION OF THESE THREE FUNCTIONS FOLLOWS: void bcdFromString(char* input, bcd* output) { unsigned int len = strlen(input); unsigned int lenstore = len; unsigned int x = 0; unsigned char decFound = 0; unsigned int negative = 0; for (x = 0; x < len; ++x) { char temp = input[x]; switch (temp) { case '-': //lenstore -= 1; negative = 1; break; case '0': if (decFound > 0) { output->values[x - 1] = 0; } else { output->values[x] = 0; } break; case '1': if (decFound > 0) { output->values[x - 1] = 1; } else { output->values[x] = 1; } break; case '2': if (decFound > 0) { output->values[x - 1] = 2; } else { output->values[x] = 2; } break; case '3': if (decFound > 0) { output->values[x - 1] = 3; } else { output->values[x] = 3; } break; case '4': if (decFound > 0) { output->values[x - 1] = 4; } else { output->values[x] = 4; } break; case '5': if (decFound > 0) { output->values[x - 1] = 5; } else { output->values[x] = 5; } break; case '6': if (decFound > 0) { output->values[x - 1] = 6; } else { output->values[x] = 6; } break; case '7': if (decFound > 0) { output->values[x - 1] = 7; } else { output->values[x] = 7; } break; case '8': if (decFound > 0) { output->values[x - 1] = 8; } else { output->values[x] = 8; } break; case '9': if (decFound > 0) { output->values[x - 1] = 9; } else { output->values[x] = 9; } break; case '.': output->decpos = x; lenstore -= 1; decFound = 1; break; } } output->length = lenstore; if (negative == 1) { int i = 0; for(i = 0; i < lenstore; i++) { output->values[i] = output->values[i] * (-1); } } if (decFound == 0) { output->decpos = lenstore; } } bcd* createBcd(unsigned int len) { bcd* output = (bcd *)malloc(sizeof(bcd)); output->length = len; output->values = (int *)malloc(len * sizeof(int)); return output; } void zeroBcd(bcd* input) { int c = 0; for (c = 0; c < input->length; ++c) { *(input->values + c) = 0; } } void printBcd(bcd* input) { int i = 0; for(i = 0; i < input->length; i++) { if (i == input->decpos) { printf("."); } printf("%i", input->values[i]); } printf("\n"); } void printBcdNotNormal(bcd* input) { int i = 0; for(i = 0; i < input->length; i++) { if (i == input->decpos) { printf("."); } printf("%i", input->values[i]); printf("|"); } printf("\n"); } void freeBcd(bcd* input) { cudaFree(input->gpuP); free(input->values); free(input); } //cudaFree //GPU CODE //THIS FUNCTION LOADS THE VALUES OF A BCD INTO TEH GPU'S MEMORY AND SETS THE gpuP OF THE BCD TO POINT TO THE GPU-STORED VALUES void loadBcdIntoGPU(bcd* input); //THIS COPIES BACK THE RESULTS FROM THE GPU TO THE BCD void getCompResult(bcd* output); void loadBcdIntoGPU(bcd* input) { cudaMalloc(&input->gpuP, input->length * sizeof(int)); cudaMemcpy(input->gpuP, input->values, input->length * sizeof(int), cudaMemcpyHostToDevice); } void getCompResult(bcd* output) { cudaMemcpy(output->values,output->gpuP, output->length * sizeof(int), cudaMemcpyDeviceToHost); } //THE FOLLOWING IS ALL SETUP CODE void cudaSetup(); void initTempBuffers(); void reallocTempBuffers(unsigned int size); void freeTempBuffers(); void zeroTempBuffers(); //THIS IS THE MAIN SETUP FUNCTION. CALL THIS EARLY ON IN MAIN. BEFORE ANY ADDITIONS OR MULTIPLICATIONS ON BCD'S //will call initTempBuffers void cudaSetup() { //LET'S FIGURE OUT THE MAXIMUM THREADS PER BLOCK cudaDeviceProp deviceProp; cudaGetDeviceProperties(&deviceProp, DEVICE_NUM); MAX_THREADS_PER_BLOCK = deviceProp.maxThreadsPerBlock; initTempBuffers(); } void initTempBuffers() { cudaMalloc(&temp_buffer1, ORIGINAL_TEMP_BUFFER_SIZE * sizeof(int)); cudaMalloc(&temp_buffer2, ORIGINAL_TEMP_BUFFER_SIZE * sizeof(int)); cudaMalloc(&temp_buffer3, ORIGINAL_TEMP_BUFFER_SIZE * sizeof(int)); cudaMemset(temp_buffer1, 0, ORIGINAL_TEMP_BUFFER_SIZE * sizeof(int)); cudaMemset(temp_buffer2, 0, ORIGINAL_TEMP_BUFFER_SIZE * sizeof(int)); cudaMemset(temp_buffer3, 0, ORIGINAL_TEMP_BUFFER_SIZE * sizeof(int)); temp_buffer_size = ORIGINAL_TEMP_BUFFER_SIZE; } void reallocTempBuffers(unsigned int size) { if (temp_buffer_size < size) { freeTempBuffers(); cudaMalloc(&temp_buffer1, size * sizeof(int)); cudaMalloc(&temp_buffer2, size * sizeof(int)); cudaMalloc(&temp_buffer3, size * sizeof(int)); cudaMemset(temp_buffer1, 0, size * sizeof(int)); cudaMemset(temp_buffer2, 0, size * sizeof(int)); cudaMemset(temp_buffer3, 0, size * sizeof(int)); temp_buffer_size = size; } } void zeroTempBuffers() { cudaMemset(temp_buffer1, 0, temp_buffer_size * sizeof(int)); cudaMemset(temp_buffer2, 0, temp_buffer_size * sizeof(int)); cudaMemset(temp_buffer3, 0, temp_buffer_size * sizeof(int)); } void freeTempBuffers() { cudaFree(temp_buffer1); cudaFree(temp_buffer2); cudaFree(temp_buffer3); } //MEMORY REQUIREMENT CALCULATION CODE unsigned int memReqForAddition(bcd* num1, bcd* num2); unsigned int memReqForMulitiplcation(bcd* num1, bcd* num2); unsigned int memReqForAddition(bcd* num1, bcd* num2) { unsigned int maxlen = 0; if (num1->length > num2->length) { maxlen = num1->length; } else { maxlen = num2->length; } return (maxlen + NORMALIZATION_EXPANSION + 1); } unsigned int memReqForMulitiplcation(bcd* num1, bcd* num2) { return num1->length + num2->length + (2 * NORMALIZATION_EXPANSION); } //DECIMAL POSITION CHANGE CODE unsigned int decimalMovementAddition(bcd* num1, bcd* num2, unsigned int memReq); unsigned int decimalMovementMultiplication(bcd* num1, bcd* num2, unsigned int memReq); unsigned int decimalMovementAddition(bcd* num1, bcd* num2, unsigned int memReq) { if (num1->length > num2->length) { return (memReq - num1->length) + num1->decpos; } else { return (memReq - num2->length) + num2->decpos; } } /* TODO START HERE */ unsigned int decimalMovementMultiplication(bcd* num1, bcd* num2, unsigned int memReq) { return memReq - ((num1->length - num1->decpos) + (num2->length - num2->decpos)); } //KERNELS __global__ void addition(int *num1, int *num2, unsigned int num1Len, unsigned int num2Len, unsigned int num1offset, unsigned int num2offset, int *temp_buffer1, int *temp_buffer2, int *output, unsigned int memReq, unsigned int reps); __global__ void normalize(int *num,unsigned int num1Len, int *result,unsigned int memReq); __global__ void multiplication(int *num1, int *num2, unsigned int num1Len, unsigned int num2Len, unsigned int num1offset, unsigned int num2offset, int *temp_buffer1, int *temp_buffer2, int *temp_buffer3, int *output, unsigned int memReq, unsigned int reps); /* TODO normalize: need to get this working for reps. EG: numbers longer the 512 digits */ /* TODO normalize: need to make this work for negative numbers */ __global__ void normalize(int *num, unsigned int numLen, int *result,unsigned int memReq, unsigned int reps) { int x = threadIdx.x; if (x < memReq) { if (reps == 1) { if (x >= memReq - numLen) { result[x] = num[x - (memReq - numLen)]; } } else { //result[x] = num[x - (memReq - numLen)]; int d = 0; for (d = 0; d < reps; ++d) { if (d == 0) { if (x >= memReq - numLen) { result[x + (512 * d)] = num[x + (512 * d) - (memReq - numLen)]; } } else { if ((x + (512 * d) - (memReq - numLen)) < numLen) result[x + (512 * d)] = num[x + (512 * d) - (memReq - numLen)]; } } } } __shared__ int carry; carry = 1; __syncthreads(); while (carry) { if (reps == 1) { int c = 0; if (x < memReq) { c = result[x] / 10; result[x] %= 10; } __syncthreads(); if (x < memReq && x != 0) { result[x - 1] += c; } } else { int d = 0; for(d = 0; d < reps; ++d) { int c = 0; if ((x + (512 * d)) < memReq) { c = result[x + (512 * d)] / 10; result[x + (512 * d)] %= 10; } __syncthreads(); if ((x + (512 * d)) < memReq && (x != 0)) { result[x - 1 + (512 * d)] += c; } } } carry = 0; __syncthreads(); if (x < memReq) { if (reps == 1) { if (abs(result[x]) > 9) { carry = 1; } } else { int d = 0; for(d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq && (abs(result[x + (512 * d)]) > 9)) { carry = 1; } } } } __syncthreads(); } } /* TODO addition kernel: need to add in normalization */ __global__ void addition(int *num1, int *num2, unsigned int num1Len, unsigned int num2Len, unsigned int num1offset, unsigned int num2offset, int *temp_buffer1, int *temp_buffer2, int *output, unsigned int memReq, unsigned int reps) { int x = threadIdx.x; if (reps == 1) { if (x < memReq) { if (x >= memReq - num1Len) { temp_buffer1[x] = num1[x - (memReq - num1Len)]; } if (x >= memReq - num2Len) { temp_buffer2[x] = num2[x - (memReq - num2Len)]; } } } else { int d = 0; for (d = 0; d < reps; ++d) { if (d == 0) { if (x >= memReq - num1Len) { temp_buffer1[x + (512 * d)] = num1[x + (512 * d) - (memReq - num1Len)]; } if (x >= memReq - num2Len) { temp_buffer2[x + (512 * d)] = num2[x + (512 * d) - (memReq - num2Len)]; } } else { if ((x + (512 * d) - (memReq - num1Len)) < num1Len) { temp_buffer1[x + (512 * d)] = num1[x + (512 * d) - (memReq - num1Len)]; } if ((x + (512 * d) - (memReq - num2Len)) < num2Len) { temp_buffer2[x + (512 * d)] = num2[x + (512 * d) - (memReq - num2Len)]; } } } } //move everything to temp buffers __shared__ int carry; carry = 0; __syncthreads(); if (reps == 1) { if (((unsigned int)(temp_buffer1[x] & ((unsigned int)3 << 30)) > 0) || ((unsigned int)(temp_buffer2[x] & ((unsigned int)3 << 30)) > 0)) { carry = 1; } } else { int d = 0; for (d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq) { if (((unsigned int)(temp_buffer1[x + (512 * d)] & ((unsigned int)3 << 30)) > 0) || ((unsigned int)(temp_buffer2[x + (512 * d)] & ((unsigned int)3 << 30)) > 0)) { carry = 1; } } } } __syncthreads(); while (carry) { if (reps == 1) { int c1 = 0; int c2 = 0; if (x < memReq) { c1 = temp_buffer1[x] / 10; c2 = temp_buffer2[x] / 10; temp_buffer1[x] %= 10; temp_buffer2[x] %= 10; } __syncthreads(); if (x < memReq && x != 0) { temp_buffer1[x - 1] += c1; temp_buffer2[x - 1] += c2; } carry = 0; __syncthreads(); if (x < memReq) { if ((abs(temp_buffer1[x]) > 9) || (abs(temp_buffer2[x]) > 9)) { carry = 1; } } __syncthreads(); } else { int d = 0; for(d = 0; d < reps; ++d) { int c1 = 0; int c2 = 0; if ((x + (512 * d)) < memReq) { c1 = temp_buffer1[x + (512 * d)] / 10; c2 = temp_buffer2[x + (512 * d)] / 10; temp_buffer1[x + (512 * d)] %= 10; temp_buffer2[x + (512 * d)] %= 10; } __syncthreads(); if ((x + (512 * d)) < memReq && (x != 0)) { temp_buffer1[x - 1 + (512 * d)] += c1; temp_buffer2[x - 1 + (512 * d)] += c2; } } } carry = 0; __syncthreads(); if (x < memReq) { if (reps == 1) { if ((abs(temp_buffer1[x]) > 9) || (abs(temp_buffer2[x]) > 9)) { carry = 1; } } else { int d = 0; for(d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq && ((abs(temp_buffer1[x + (512 * d)]) > 9) || (abs(temp_buffer2[x + (512 * d)]) > 9))) { carry = 1; } } } } __syncthreads(); } if (x < memReq) { if (reps == 1) { if (((x + num1offset) < memReq) && ((x + num2offset) < memReq)) { output[x] = temp_buffer1[x + num1offset] + temp_buffer2[x + num2offset]; } else if ((x + num2offset) < memReq) { output[x] = temp_buffer2[x + num2offset]; } else if ((x + num1offset) < memReq) { output[x] = temp_buffer1[x + num1offset]; } else { //do nothing } } else { int d = 0; for(d = 0; d < reps; ++d) { if ((((x + (512 * d)) + num1offset) < memReq) && (((x + (512 * d)) + num2offset) < memReq)) { output[(x + (512 * d))] = temp_buffer1[(x + (512 * d)) + num1offset] + temp_buffer2[(x + (512 * d)) + num2offset]; } else if (((x + (512 * d)) + num2offset) < memReq) { output[(x + (512 * d))] = temp_buffer2[(x + (512 * d)) + num2offset]; } else if (((x + (512 * d)) + num1offset) < memReq) { output[(x + (512 * d))] = temp_buffer1[(x + (512 * d)) + num1offset]; } else { //do nothing } } } } } //multiplication<<<1,MAX_THREADS_PER_BLOCK>>>(num1->gpuP, num2->gpuP, num1->length, num2->length, dec1_offset, dec2_offset, temp_buffer1, temp_buffer2, temp_buffer3, output->gpuP, result_req, reps); __global__ void multiplication(int *num1, int *num2, unsigned int num1Len, unsigned int num2Len, unsigned int num1offset, unsigned int num2offset, int *temp_buffer1, int *temp_buffer2, int *temp_buffer3, int *output2, unsigned int memReq, unsigned int reps) { int x = threadIdx.x; if (reps == 1) { if (x < memReq) { if (x >= memReq - num1Len) { temp_buffer1[x] = num1[x - (memReq - num1Len)]; } if (x >= memReq - num2Len) { temp_buffer2[x] = num2[x - (memReq - num2Len)]; } } } else { int d = 0; for (d = 0; d < reps; ++d) { if (d == 0) { if (x >= memReq - num1Len) { temp_buffer1[x + (512 * d)] = num1[x + (512 * d) - (memReq - num1Len)]; //output2[x + (512 * d)] = num1[x + (512 * d) - (memReq - num1Len)]; } if (x >= memReq - num2Len) { temp_buffer2[x + (512 * d)] = num2[x + (512 * d) - (memReq - num2Len)]; } } else { if ((x + (512 * d) - (memReq - num1Len)) < num1Len) { temp_buffer1[x + (512 * d)] = num1[x + (512 * d) - (memReq - num1Len)]; //output2[x + (512 * d)] = num1[x + (512 * d) - (memReq - num1Len)]; } if ((x + (512 * d) - (memReq - num2Len)) < num2Len) { temp_buffer2[x + (512 * d)] = num2[x + (512 * d) - (memReq - num2Len)]; } } } } //move everything to temp buffers __shared__ int carry; carry = 1; __syncthreads(); while (carry) { if (reps == 1) { int c1 = 0; int c2 = 0; if (x < memReq) { c1 = temp_buffer1[x] / 10; c2 = temp_buffer2[x] / 10; temp_buffer1[x] %= 10; temp_buffer2[x] %= 10; } __syncthreads(); if (x < memReq && x != 0) { temp_buffer1[x - 1] += c1; temp_buffer2[x - 1] += c2; } } else { int d = 0; for(d = 0; d < reps; ++d) { int c1 = 0; int c2 = 0; if ((x + (512 * d)) < memReq) { c1 = temp_buffer1[x + (512 * d)] / 10; c2 = temp_buffer2[x + (512 * d)] / 10; temp_buffer1[x + (512 * d)] %= 10; temp_buffer2[x + (512 * d)] %= 10; } __syncthreads(); if ((x + (512 * d)) < memReq && (x != 0)) { temp_buffer1[x - 1 + (512 * d)] += c1; temp_buffer2[x - 1 + (512 * d)] += c2; } } } carry = 0; __syncthreads(); if (reps == 1) { if ((x < memReq) && ((abs(temp_buffer1[x]) > 9) || (abs(temp_buffer2[x]) > 9))) { carry = 1; } } else { int d = 0; for(d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq && ((abs(temp_buffer1[x + (512 * d)]) > 9) || (abs(temp_buffer2[x + (512 * d)]) > 9))) { carry = 1; } } } __syncthreads(); } //good till here //TEST INTITIAL NORMALIZATION __shared__ int multCount; multCount = 0; __syncthreads(); //output2[x] = reps; //__syncthreads(); <-- uncomment this too //TEST JUST ONE ITERATION //while (multCount < num2Len) while (multCount < num2Len) { int tempMultCountStore = multCount; tempMultCountStore += 1; if (reps == 1) { if (x < memReq) { if (x > multCount) { temp_buffer3[x - multCount] = temp_buffer2[memReq - multCount - 1] * temp_buffer1[x]; } } //check for overflow } else { int d = 0; for(d = 0; d < reps; ++d) { if (d == 0) { if ((x > multCount) && ((x + (512 * d)) < memReq)) { temp_buffer3[(x + (512 * d)) - multCount] = temp_buffer2[memReq - multCount - 1] * temp_buffer1[(x + (512 * d))]; } } else { if ((x + (512 * d)) < memReq) { temp_buffer3[(x + (512 * d)) - multCount] = temp_buffer2[memReq - multCount - 1] * temp_buffer1[(x + (512 * d))]; } } } } carry = 0; __syncthreads(); int d = 0; if (reps == 1) { for (d = 0; d <= reps; ++d) { if ((x + (512 * d)) < memReq) { if (((unsigned int)(temp_buffer3[x + (512 * d)] & ((unsigned int)3 << 30)) > 0) || ((unsigned int)(output2[x + (512 * d)] & ((unsigned int)3 << 30)) > 0)) { carry = 1; } } } } else { for (d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq) { if (((unsigned int)(temp_buffer3[x + (512 * d)] & ((unsigned int)3 << 30)) > 0) || ((unsigned int)(output2[x + (512 * d)] & ((unsigned int)3 << 30)) > 0)) { carry = 1; } } } } __syncthreads(); while (carry) { if (reps == 1) { int c1 = 0; int c2 = 0; if ((reps == 1) && (x < memReq)) { c1 = temp_buffer3[x] / 10; c2 = output2[x] / 10; temp_buffer3[x] %= 10; output2[x] %= 10; } __syncthreads(); if (x < memReq && x != 0) { temp_buffer3[x - 1] += c1; output2[x - 1] += c2; } carry = 0; __syncthreads(); if (x < memReq) { if ((abs(temp_buffer3[x]) > 9) || abs((output2[x]) > 9)) { carry = 1; } } __syncthreads(); } else { int d = 0; for(d = 0; d < reps; ++d) { int c1 = 0; int c2 = 0; if ((x + (512 * d)) < memReq) { c1 = temp_buffer3[x + (512 * d)] / 10; c2 = output2[x + (512 * d)] / 10; temp_buffer3[x + (512 * d)] %= 10; output2[x + (512 * d)] %= 10; } __syncthreads(); if (d == 0) { if ((x != 0) && ((x + (512 * d)) < memReq)) { //SOMEHOW DESYNCRONIZED temp_buffer3[x - 1 + (512 * d)] += c1; output2[x - 1 + (512 * d)] += c2; } } else { if (((x + (512 * d)) < memReq)) { //SOMEHOW DESYNCRONIZED temp_buffer3[x - 1 + (512 * d)] += c1; output2[x - 1 + (512 * d)] += c2; } } __syncthreads(); carry = 0; int d = 0; for(d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq && ((abs(temp_buffer3[x + (512 * d)]) > 9) || (abs(output2[x + (512 * d)]) > 9))) { carry = 1; } } __syncthreads(); } } } //perform addition if (reps == 1) { if (x < memReq) { output2[x] += temp_buffer3[x]; temp_buffer3[x] = 0; } //check for overflow } else { int d = 0; for(d = 0; d < reps; ++d) { if ((x + (512 * d)) < memReq) { output2[x + (512 * d)] += temp_buffer3[x + (512 * d)]; temp_buffer3[x + (512 * d)] = 0; } } } //update counter multCount = tempMultCountStore; __syncthreads(); } } //ARITHMETIC FUNCTIONS bcd* normalize(bcd *num) { unsigned int memReq = NORMALIZATION_EXPANSION + num->length; bcd *output = createBcd(memReq); zeroBcd(output); loadBcdIntoGPU(output); output->decpos = num->decpos + (memReq - num->length); unsigned int reps = (memReq / MAX_THREADS_PER_BLOCK) + 1; //printf("reps: %u",reps); normalize<<<1,MAX_THREADS_PER_BLOCK>>>(num->gpuP, num->length, output->gpuP,memReq,reps); return output; } bcd* add(bcd *num1, bcd *num2) { //first calc memory requirement for result unsigned int result_req = memReqForAddition(num1, num2); bcd *output = createBcd(result_req); if (result_req > temp_buffer_size) { reallocTempBuffers(result_req * 2); } zeroBcd(output); loadBcdIntoGPU(output); output->decpos = decimalMovementAddition(num1, num2, result_req); unsigned int reps = (result_req / MAX_THREADS_PER_BLOCK) + 1; //now we'll figure out the decimal offset unsigned int decdiff1 = num1->length - num1->decpos; unsigned int decdiff2 = num2->length - num2->decpos; unsigned int dec1_offset = 0; unsigned int dec2_offset = 0; if (decdiff1 > decdiff2) { dec2_offset = decdiff1 - decdiff2; } else { dec1_offset = decdiff2 - decdiff1; } zeroTempBuffers(); addition<<<1,MAX_THREADS_PER_BLOCK>>>(num1->gpuP, num2->gpuP, num1->length, num2->length, dec1_offset, dec2_offset, temp_buffer1, temp_buffer2, output->gpuP, result_req, reps); return output; } bcd *multiply(bcd *num1, bcd *num2) { unsigned int result_req = memReqForMulitiplcation(num1, num2); bcd *output = createBcd(result_req); if (result_req > temp_buffer_size) { reallocTempBuffers(result_req * 2); } zeroBcd(output); loadBcdIntoGPU(output); output->decpos = decimalMovementMultiplication(num1, num2, result_req); unsigned int reps = (result_req / MAX_THREADS_PER_BLOCK) + 1; //printf("REPS: %u\n", reps); //now we'll figure out the decimal offset unsigned int decdiff1 = num1->length - num1->decpos; unsigned int decdiff2 = num2->length - num2->decpos; unsigned int dec1_offset = 0; unsigned int dec2_offset = 0; if (decdiff1 > decdiff2) { dec2_offset = decdiff1 - decdiff2; } else { dec1_offset = decdiff2 - decdiff1; } //printf("RESULT REQ: %u\n", result_req); zeroTempBuffers(); multiplication<<<1,MAX_THREADS_PER_BLOCK>>>(num1->gpuP, num2->gpuP, num1->length, num2->length, dec1_offset, dec2_offset, temp_buffer1, temp_buffer2, temp_buffer3, output->gpuP, result_req, reps); return output; } //THIS IS A TESTBED FOR OUR LIBRARY //AN EXAMPLE int main() { //bcd *num1 = createBcd(903); bcd *num1 = createBcd(7); bcd *num2 = createBcd(2); //bcdFromString("111111111111111111111111111111111111111111111112111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.9", num1); bcdFromString("112341.9", num1); bcdFromString("1.2", num2); printBcdNotNormal(num1); printBcd(num2); cudaSetup(); loadBcdIntoGPU(num1); loadBcdIntoGPU(num2); bcd* result = multiply(num1, num2); bcd* normResult = normalize(result); getCompResult(normResult); printf("\n"); printBcd(normResult); //printBcd(result); freeBcd(num1); freeBcd(num2); freeBcd(result); freeBcd(normResult); freeTempBuffers(); return 0; }
21,534
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define MAX_SIZE 10000 #define GET_TIME(now){ \ struct timeval t; \ gettimeofday(&t, NULL); \ now = t.tv_sec * 1000 + t.tv_usec / 1000.0; \ } int arr[MAX_SIZE]; int brr[MAX_SIZE]; __global__ void mp(int *a, int *b) { int i; int id_0 = blockIdx.x * blockDim.x; int id_1; for(i = 1 ;; i=i*2){ if(i >= blockDim.x)break; id_1 = i * 2 * threadIdx.x; if(id_1 + i < blockDim.x){ id_1 += id_0; if(a[id_1] < a[id_1 + i]){ a[id_1] = a[id_1 + i]; } } __syncthreads(); } if(threadIdx.x == 0)b[blockIdx.x] = a[id_0]; } int main(void) { int *d_a, *d_b; int block_size; int size = sizeof(int) * MAX_SIZE; int i, n, num; double start_time, end_time; FILE *fp = fopen("array.txt","r"); printf("Input the Block Size: "); scanf("%d",&block_size); for(i = 0; i < MAX_SIZE; i++){ fscanf(fp,"%d",&arr[i]); } fclose(fp); cudaMalloc((void **)&d_a, size); cudaMalloc((void **)&d_b, size); cudaMemcpy(d_a, arr, size, cudaMemcpyHostToDevice); GET_TIME(start_time); dim3 dimBlock (block_size, 1); if(MAX_SIZE / dimBlock.x != 0)n = 1; else{ n = 0; } dim3 dimGrid(MAX_SIZE / dimBlock.x + n, 1); mp<<<dimGrid, dimBlock>>>(d_a,d_b); GET_TIME(end_time); cudaMemcpy(brr, d_b, size, cudaMemcpyDeviceToHost); num = 0; for(i = 0; i < MAX_SIZE / block_size + n; i++){ if(num < brr[i]){ num = brr[i]; } } printf("THE MAXIMUM NUMBER IS %d\n",num); printf("VER C: Elapsed time is %e (msec)\n",end_time - start_time); cudaFree(d_a); cudaFree(d_b); return 0; }
21,535
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<cuda.h> #include<cuda_runtime.h> #include<time.h> #include<sys/time.h> #define SQUARE(x) ((x)*(x)) #define PI 3.14 #define BLOCK_SIZE 16 #define N 4096 __device__ __constant__ float pi=3.14; __device__ __constant__ int sobelX[9]={1,0,-1,2,0,-2,1,0,-1}; __device__ __constant__ int sobelY[9]={1,2,1,0,0,0,-1,-2,-1}; __global__ void NormalizeGrayGPU(double input[], int width, int height, unsigned char output[], double min, double max) { int index= blockIdx.x*blockDim.x+threadIdx.x; if(index < (width*height)) { output[index]=(input[index]-min)*255/(max-min); } } double FindMin(double input[], int width, int height) { double min = input[0]; for (int i = 0; i < width*height; i++) { if (input[i] < min) min = input[i]; } return min; } double FindMax(double input[], int width, int height) { double max = input[0]; for (int i = 0; i < width*height; i++) { if (input[i] > max) max = input[i]; } return max; } __global__ void SobelFilter_gpu(unsigned char* A, double *gradImageX, double *gradImageY, double *gradMag, int width, int height) { int row= blockIdx.y*blockDim.y+threadIdx.y; int col= blockIdx.x*blockDim.x+threadIdx.x; double tempx=0; double tempy=0; if(row < height && col < width){ tempx = 0; tempy = 0; for (int r2=-1; r2<=1; r2++){ for (int c2=-1; c2<=1; c2++) { tempx += A[(row+r2)*width+(col+c2)]*sobelX[(r2+1)*3+c2+1]; tempy += A[(row+r2)*width+(col+c2)]*sobelY[(r2+1)*3+c2+1]; } } gradImageX[(row*width)+col]=tempx; gradImageY[(row*width)+col]=tempy; gradMag[(row*width)+col]= sqrt((double) (tempx*tempx)+(tempy*tempy)); } } __global__ void theta_gpu(double *gradImageY, double *gradImageX, double *gradPhase, int width, int height){ int index= blockIdx.x*blockDim.x+threadIdx.x; if(index<(width*height)){ float theta = atan2(gradImageY[index],gradImageX[index]); theta=theta*180/pi; gradPhase[index]=theta; } } int main(int argc, char *argv[]) { FILE *fptr; char *inputHeader, *testHeader; int inputCols, inputRows, inputBytes; int testCols, testRows, testBytes; char Header_1[320], Header_2[320]; unsigned char *inputImage, *testImage; unsigned char *normalGradMag, *normalGrad_x, *normalGrad_y, *normalGradPhase; unsigned char *normaltestMag, *normaltest_x, *normaltest_y, *normaltestPhase; double *gradPhase, *gradMag; double *testgradPhase, *testgradMag; double max=0; double min=0; float gpu_time_1 = 0; float gpu_time_2 = 0; float gpu_time_3 = 0; //GPU variables double *d_gradImageX, *d_gradImageY, *d_gradPhase, *d_gradMag; unsigned char *d_inputImage, *d_normalGradMag, *d_normalGradX, *d_normalGradY, *d_normalGradPhase; unsigned char *d_testImage; double *d_testgradImageX, *d_testgradImageY, *d_testgradMag, *d_testgradPhase; unsigned char *d_testnormalGradMag, *d_testnormalGradX, *d_testnormalGradY, *d_testnormalGradPhase; cudaError_t err; struct timeval cstart1, cstart2, cstart3, cend1, cend2, cend3; cudaEvent_t start1, start2, start3, stop1, stop2, stop3; printf("Initialization done!\n"); gettimeofday(&cstart1, NULL); if ((fptr=fopen(argv[1],"r"))==NULL) { printf("Unable to open input file for reading\n"); exit(0); } //Open and load input image fptr = fopen(argv[1], "r"); fscanf(fptr,"%s %d %d %d",&inputHeader, &inputCols, &inputRows, &inputBytes); Header_1[0]=fgetc(fptr); /* read white-space character that separates header */ inputImage = (unsigned char*)calloc(inputCols*inputRows,sizeof(unsigned char)); fread(inputImage, 1, inputCols*inputRows, fptr); fclose(fptr); printf("Input file opened!\n"); if ((fptr = fopen(argv[2], "r")) == NULL) { printf("Unable to open test file for reading\n"); exit(0); } //Open and load test image fptr = fopen(argv[2], "rb"); fscanf(fptr, "%s %d %d %d", &testHeader, &testCols, &testRows, &testBytes); Header_2[0] = fgetc(fptr); /* read white-space character that separates header */ testImage = (unsigned char*)calloc(testCols*testRows, sizeof(unsigned char)); fread(testImage, 1, testCols*testRows, fptr); fclose(fptr); printf("Test file opened!\n"); gettimeofday(&cend1, NULL); cudaEventCreate(&start1); cudaEventCreate(&stop1); cudaEventRecord(start1); cudaEventSynchronize(start1); //cudaMalloc for Input image err=cudaMalloc(&d_inputImage,(inputRows*inputCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_inputImage"); err=cudaMalloc(&d_gradImageX,(inputRows*inputCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_gradImageX"); err=cudaMalloc(&d_gradImageY,(inputRows*inputCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_gradImageY"); err=cudaMalloc(&d_gradPhase,(inputRows*inputCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_gradPhase"); err=cudaMalloc(&d_gradMag,(inputRows*inputCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_gradMag"); err=cudaMalloc(&d_normalGradMag,(inputRows*inputCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_normalGradMag"); err=cudaMalloc(&d_normalGradX,(inputRows*inputCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_normalGradX"); err=cudaMalloc(&d_normalGradY,(inputRows*inputCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_normalGradY"); err=cudaMalloc(&d_normalGradPhase,(inputRows*inputCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_normalGradPhase"); //cudaMalloc for test image err=cudaMalloc(&d_testImage,(testRows*testCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testImage"); err=cudaMalloc(&d_testgradImageX,(testRows*testCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testgradImageX"); err=cudaMalloc(&d_testgradImageY,(testRows*testCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testgradImageY"); err=cudaMalloc(&d_testgradPhase,(testRows*testCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testgradPhase"); err=cudaMalloc(&d_testgradMag,(testRows*testCols*sizeof(double))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testgradMag"); err=cudaMalloc(&d_testnormalGradMag,(testRows*testCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testnormalGradMag"); err=cudaMalloc(&d_testnormalGradX,(testRows*testCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testnormalGradX"); err=cudaMalloc(&d_testnormalGradY,(testRows*testCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testnormalGradY"); err=cudaMalloc(&d_testnormalGradPhase,(testRows*testCols*sizeof(unsigned char))); if(err != cudaSuccess) printf("/n Error in cudaMalloc d_testnormalGradPhase"); cudaEventSynchronize(stop1); cudaEventRecord(stop1); gettimeofday(&cstart2, NULL); //Normalized input gradient images normalGradMag = (unsigned char*)calloc(inputCols*inputRows, sizeof(unsigned char)); normalGrad_x = (unsigned char*)calloc(inputCols*inputRows, sizeof(unsigned char)); normalGrad_y = (unsigned char*)calloc(inputCols*inputRows, sizeof(unsigned char)); normalGradPhase = (unsigned char*)calloc(inputCols*inputRows, sizeof(unsigned char)); gradPhase = (double*)calloc(inputCols*inputRows, sizeof(double)); gradMag = (double*)calloc(inputCols*inputRows, sizeof(double)); //Normalized test gradient images normaltestMag = (unsigned char*)calloc(testCols*testRows, sizeof(unsigned char)); normaltest_x = (unsigned char*)calloc(testCols*testRows, sizeof(unsigned char)); normaltest_y = (unsigned char*)calloc(testCols*testRows, sizeof(unsigned char)); normaltestPhase = (unsigned char*)calloc(testCols*testRows, sizeof(unsigned char)); testgradPhase = (double*)calloc(testCols*testRows, sizeof(double)); testgradMag = (double*)calloc(testCols*testRows, sizeof(double)); gettimeofday(&cend2, NULL); cudaEventCreate(&start2); cudaEventCreate(&stop2); cudaEventRecord(start2); cudaEventSynchronize(start2); //Compute gradients and phase for input image err=cudaMemcpy(d_inputImage, inputImage, (inputRows*inputCols*sizeof(unsigned char)), cudaMemcpyHostToDevice); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of d_inputImage"); /* Launch Kernel*/ dim3 dimGrid(ceil((float)(N+2)/BLOCK_SIZE), ceil((float)(N+2)/BLOCK_SIZE),1); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); SobelFilter_gpu<<<dimGrid,dimBlock>>>(d_inputImage, d_gradImageX, d_gradImageY, d_gradMag, inputCols, inputRows); cudaDeviceSynchronize(); dim3 BlockDim = dim3(1024,1,1); dim3 GridDim = dim3(10000,1,1); theta_gpu<<<GridDim,BlockDim>>>(d_gradImageY,d_gradImageX, d_gradPhase, inputCols, inputRows); //Compute gradients and phase for test image err=cudaMemcpy(d_testImage, testImage, (testRows*testCols*sizeof(unsigned char)), cudaMemcpyHostToDevice); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of d_testImage"); /* Launch Kernel*/ SobelFilter_gpu<<<dimGrid,dimBlock>>>(d_testImage, d_testgradImageX, d_testgradImageY, d_testgradMag, testCols, testRows); cudaDeviceSynchronize(); theta_gpu<<<GridDim,BlockDim>>>(d_testgradImageY,d_testgradImageX, d_testgradPhase, testCols, testRows); cudaMemcpy(gradMag, d_gradMag,(inputCols*inputRows*sizeof(double)),cudaMemcpyDeviceToHost); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of normalGrad_x"); min = FindMin(gradMag, inputCols, inputRows); max = FindMax(gradMag, inputCols, inputRows); NormalizeGrayGPU<<<GridDim,BlockDim>>>(d_gradMag, inputCols, inputRows, d_normalGradMag, min, max); cudaDeviceSynchronize(); cudaMemcpy(testgradMag, d_testgradMag,(inputCols*inputRows*sizeof(double)),cudaMemcpyDeviceToHost); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of normalGrad_x"); min = FindMin(testgradMag, testCols, testRows); max = FindMax(testgradMag, testCols, testRows); NormalizeGrayGPU<<<GridDim,BlockDim>>>(d_testgradMag, testCols, testRows, d_testnormalGradMag, min, max); cudaDeviceSynchronize(); cudaMemcpy(gradPhase, d_gradPhase,(inputCols*inputRows*sizeof(double)),cudaMemcpyDeviceToHost); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of gradPhase"); cudaMemcpy(testgradPhase, d_testgradPhase,(testCols*testRows*sizeof(double)),cudaMemcpyDeviceToHost); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of testgradPhase"); cudaMemcpy(normalGradMag, d_normalGradMag,(inputCols*inputRows*sizeof(unsigned char)),cudaMemcpyDeviceToHost); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of normalGradMag"); cudaMemcpy(normaltestMag, d_testnormalGradMag,(testCols*testRows*sizeof(unsigned char)),cudaMemcpyDeviceToHost); if(err != cudaSuccess) printf("/n Error in cudaMemcpy of normaltestMag"); cudaEventRecord(stop2); cudaEventSynchronize(stop2); gettimeofday(&cstart3, NULL); int histo[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int testhisto[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int difference[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //Compute histogram of gradient orientations of input image double angle = 0; for (int i = 0; i < inputRows*inputCols; i++) { if (normalGradMag[i] > 25) { angle = fabs(gradPhase[i]); if (angle > 0 && angle < 21) histo[0]++; else if (angle > 21 && angle < 41) histo[1]++; else if (angle > 41 && angle < 61) histo[2]++; else if (angle > 61 && angle < 81) histo[3]++; else if (angle > 81 && angle < 101) histo[4]++; else if (angle > 101 && angle < 121) histo[5]++; else if (angle > 121 && angle < 141) histo[6]++; else if (angle > 141 && angle < 161) histo[7]++; else histo[8]++; } } printf("here6\n"); //Compute histogram of gradient orientations of test image angle = 0; for (int i = 0; i < testRows*testCols; i++) { if (normaltestMag[i] > 25) { angle = fabs(testgradPhase[i]); if (angle > 0 && angle < 21) testhisto[0]++; else if (angle > 21 && angle < 41) testhisto[1]++; else if (angle > 41 && angle < 61) testhisto[2]++; else if (angle > 61 && angle < 81) testhisto[3]++; else if (angle > 81 && angle < 101) testhisto[4]++; else if (angle > 101 && angle < 121) testhisto[5]++; else if (angle > 121 && angle < 141) testhisto[6]++; else if (angle > 141 && angle < 161) testhisto[7]++; else testhisto[8]++; } } printf("here7\n"); //Check the dissimilarity in histogram of gradient orientations int sumDiff = 0; for (int i = 0; i < 9; i++) { difference[i] = abs(histo[i] - testhisto[i]); printf("diff[%d] = %d\n", i, difference[i]); sumDiff += difference[i]; } //float mismatch = (float)sumDiff*100/(testCols*testRows); printf("HOG mismatch = %d\n", sumDiff); fptr=fopen("input_grad_mag.pgm","w"); fprintf(fptr,"P5 %d %d 255\n",inputCols,inputRows); fwrite(normalGradMag,inputCols*inputRows,1,fptr); fclose(fptr); fptr=fopen("test_grad_mag.pgm","w"); fprintf(fptr,"P5 %d %d 255\n",testCols,testRows); fwrite(normaltestMag,testCols*testRows,1,fptr); fclose(fptr); //Free allocated memory free(normalGradMag); free(normalGradPhase); free(normalGrad_x); free(normalGrad_y); free(normaltestMag); free(normaltestPhase); free(normaltest_x); free(normaltest_y); gettimeofday(&cend3, NULL); //Free Allocated memory on the device. Don't forget. cudaEventCreate(&start3); cudaEventCreate(&stop3); cudaEventRecord(start3); cudaEventSynchronize(start3); cudaFree(d_gradImageX); cudaFree(d_gradImageY); cudaFree(d_gradPhase); cudaFree(d_gradMag); cudaFree(d_inputImage); cudaFree(d_normalGradMag); cudaFree(d_normalGradX); cudaFree(d_normalGradY); cudaFree(d_normalGradPhase); cudaFree(d_testgradImageX); cudaFree(d_testgradImageY); cudaFree(d_testgradPhase); cudaFree(d_testgradMag); cudaFree(d_testImage); cudaFree(d_testnormalGradMag); cudaFree(d_testnormalGradX); cudaFree(d_testnormalGradY); cudaFree(d_testnormalGradPhase); cudaEventRecord(stop3); cudaEventSynchronize(stop3); //Calculate time tiaken // float gpu_time_1 = 0; // float gpu_time_2 = 0; // float gpu_time_3 = 0; cudaEventElapsedTime(&gpu_time_1, start1, stop1); cudaEventElapsedTime(&gpu_time_2, start2, stop2); cudaEventElapsedTime(&gpu_time_3, start3, stop3); printf("gpu_time_1 = %f\t gpu_time_2 = %f\t gpu_time_3 = %f\n",gpu_time_1, gpu_time_2, gpu_time_3); printf("Total GPU time = %f\n", gpu_time_1+gpu_time_2+gpu_time_3); float cpu_time_1 = (((cend1.tv_sec * 1000000 + cend1.tv_usec) - (cstart1.tv_sec * 1000000 + cstart1.tv_usec))/1000.0); float cpu_time_2 = (((cend2.tv_sec * 1000000 + cend2.tv_usec) - (cstart2.tv_sec * 1000000 + cstart2.tv_usec))/1000.0); float cpu_time_3 = (((cend3.tv_sec * 1000000 + cend3.tv_usec) - (cstart3.tv_sec * 1000000 + cstart3.tv_usec))/1000.0); printf("cpu_time_1 = %f\t cpu_time_2 = %f\t cpu_time_3 = %f\n",cpu_time_1, cpu_time_2, cpu_time_3); printf("Total CPU time = %f\n", cpu_time_1+cpu_time_2+cpu_time_3); printf(" Total time = %f\n", gpu_time_1+gpu_time_2+gpu_time_3+ cpu_time_1+cpu_time_2+cpu_time_3); return 0; }
21,536
#include "k_hilbert.cuh" namespace timemachine { // k_coords_to_kv_gather assigns coordinates to the relevant hilbert curve bin. // Note that this kernel requires the use of double precision as imaging into the home box // with float precision can result in the final coordinates being outside of the home box for // coordinates with large magnitudes. void __global__ k_coords_to_kv_gather( const int N, const unsigned int *__restrict__ atom_idxs, const double *__restrict__ coords, const double *__restrict__ box, const unsigned int *__restrict__ bin_to_idx, unsigned int *__restrict__ keys, unsigned int *__restrict__ vals) { int idx = blockIdx.x * blockDim.x + threadIdx.x; const double bx = box[0 * 3 + 0]; const double by = box[1 * 3 + 1]; const double bz = box[2 * 3 + 2]; const double inv_bx = 1 / bx; const double inv_by = 1 / by; const double inv_bz = 1 / bz; const double inv_bin_width = min(min(inv_bx, inv_by), inv_bz) * (HILBERT_GRID_DIM - 1.0); while (idx < N) { int atom_idx = atom_idxs[idx]; double x = coords[atom_idx * 3 + 0]; double y = coords[atom_idx * 3 + 1]; double z = coords[atom_idx * 3 + 2]; // floor is used in place of nearbyint here to ensure all particles are imaged into the home box. This differs // from distances calculations where the nearest possible image is calculated rather than imaging into // the home box. x -= bx * floor(x * inv_bx); y -= by * floor(y * inv_by); z -= bz * floor(z * inv_bz); unsigned int bin_x = static_cast<unsigned int>(x * inv_bin_width); unsigned int bin_y = static_cast<unsigned int>(y * inv_bin_width); unsigned int bin_z = static_cast<unsigned int>(z * inv_bin_width); keys[idx] = bin_to_idx[bin_x * HILBERT_GRID_DIM * HILBERT_GRID_DIM + bin_y * HILBERT_GRID_DIM + bin_z]; // uncomment below if you want to preserve the atom ordering // keys[idx] = atom_idx; vals[idx] = atom_idx; idx += gridDim.x * blockDim.x; } } } // namespace timemachine
21,537
#include "includes.h" __global__ void kernel_128_one_512(float *A, float *B, float *bnBias, float *bnScale, float *C) { int tile = blockIdx.x, part = blockIdx.y, in_channel = threadIdx.x, line = threadIdx.y; int ind = line*128 + in_channel; extern __shared__ float shared_[]; float *weights = shared_ + 128*4, *output = weights + 128*64, *input = shared_; float *bias = output + 4*128, *scale = bias + 128; input[ind] = A[tile * 512 + ind]; bias[in_channel] = bnBias[part*128 + in_channel]; scale[in_channel] = bnScale[part*128+ in_channel]; output[ind] = 0.0f; __syncthreads(); for (int k = 0; k < 128; k += 64) { for (int i = 0; i < 16; i++) weights[ind + 512*i] = B[(k + i*4 + line)*512 + part*128 + in_channel]; __syncthreads(); float *A_start = input + k; for (int p = 0; p < 64; p++) { output[ind] += A_start[line*128 + p] * weights[in_channel + p*128]; } __syncthreads(); } float *C_start = C + tile*2048 + part*128; float res = scale[in_channel] * output[ind] + bias[in_channel]; C_start[line * 512 + in_channel] = res; }
21,538
// (c) Copyright 2013 Lev Barash, Landau Institute for Theoretical Physics, Russian Academy of Sciences // This is supplement to the paper: // L.Yu. Barash, L.N. Shchur, "PRAND: GPU accelerated parallel random number generation library: Using most reliable algorithms and applying parallelism of modern GPUs and CPUs". // e-mail: barash @ itp.ac.ru (remove space) #include<stdio.h> #define gq58x1_CUDA_CALL(x) do { if((x) != cudaSuccess) { printf("Error: %s at %s:%d\n",cudaGetErrorString(cudaGetLastError()),__FILE__,__LINE__); exit(1);}} while(0) #define gq58x1_BLOCKS 512 #define gq58x1_THREADS 128 #define gq58x1_ARRAY_SECTIONS (gq58x1_BLOCKS*gq58x1_THREADS/32) #define gq58x1_k 8 #define gq58x1_q 48 #define gq58x1_g 288230374541099008ULL #define gq58x1_halfg 144115187270549504ULL typedef unsigned long long lt; typedef struct{ lt xN[32] __attribute__ ((aligned(16))), xP[32] __attribute__ ((aligned(16))); } gq58x1_state; typedef gq58x1_state gq58x1_sse_state; lt gq58x1_sse_Consts[10] __attribute__ ((aligned(16))) = {13835057977972752384ULL,13835057977972752384ULL,1610612736ULL,1610612736ULL, 288230371923853311ULL,288230371923853311ULL,288230374541099008ULL,288230374541099008ULL, 805306368ULL,805306368ULL}; __host__ unsigned int gq58x1_sse_generate_(gq58x1_sse_state* state){ unsigned output1 __attribute__ ((unused)); unsigned output2; asm volatile("movaps (%4),%%xmm0\n" \ "movaps (%3),%%xmm1\n" \ "movaps (%2),%%xmm4\n" \ "movaps %%xmm4,(%3)\n" \ "psllq $3,%%xmm4\n" \ "paddq %%xmm0,%%xmm4\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm4\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm4\n" \ "movaps %%xmm4,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm4\n" \ "paddq %%xmm3,%%xmm4\n" \ "movaps %%xmm4,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm4\n" \ "movaps %%xmm4,(%2)\n" \ "paddq 64(%4),%%xmm4\n" \ "movaps 16(%3),%%xmm1\n" \ "movaps 16(%2),%%xmm5\n" \ "movaps %%xmm5,16(%3)\n" \ "psllq $3,%%xmm5\n" \ "paddq %%xmm0,%%xmm5\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "paddq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,16(%2)\n" \ "paddq 64(%4),%%xmm5\n" \ "movaps 32(%3),%%xmm1\n" \ "movaps 32(%2),%%xmm6\n" \ "movaps %%xmm6,32(%3)\n" \ "psllq $3,%%xmm6\n" \ "paddq %%xmm0,%%xmm6\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "paddq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,32(%2)\n" \ "paddq 64(%4),%%xmm6\n" \ "movaps 48(%3),%%xmm1\n" \ "movaps 48(%2),%%xmm7\n" \ "movaps %%xmm7,48(%3)\n" \ "psllq $3,%%xmm7\n" \ "paddq %%xmm0,%%xmm7\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "paddq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,48(%2)\n" \ "paddq 64(%4),%%xmm7\n" \ "psrlq $57,%%xmm4\n" \ "psrlq $57,%%xmm5\n" \ "psrlq $57,%%xmm6\n" \ "psrlq $57,%%xmm7\n" \ "packssdw %%xmm5,%%xmm4\n" \ "packssdw %%xmm7,%%xmm6\n" \ "packssdw %%xmm6,%%xmm4\n" \ "movaps 64(%3),%%xmm1\n" \ "movaps 64(%2),%%xmm5\n" \ "movaps %%xmm5,64(%3)\n" \ "psllq $3,%%xmm5\n" \ "paddq %%xmm0,%%xmm5\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "paddq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,64(%2)\n" \ "paddq 64(%4),%%xmm5\n" \ "movaps 80(%3),%%xmm1\n" \ "movaps 80(%2),%%xmm6\n" \ "movaps %%xmm6,80(%3)\n" \ "psllq $3,%%xmm6\n" \ "paddq %%xmm0,%%xmm6\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "paddq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,80(%2)\n" \ "paddq 64(%4),%%xmm6\n" \ "movaps 96(%3),%%xmm1\n" \ "movaps 96(%2),%%xmm7\n" \ "movaps %%xmm7,96(%3)\n" \ "psllq $3,%%xmm7\n" \ "paddq %%xmm0,%%xmm7\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "paddq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,96(%2)\n" \ "paddq 64(%4),%%xmm7\n" \ "movaps 112(%3),%%xmm1\n" \ "movaps 112(%2),%%xmm2\n" \ "movaps %%xmm2,112(%3)\n" \ "psllq $3,%%xmm2\n" \ "paddq %%xmm0,%%xmm2\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm2\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm2\n" \ "movaps %%xmm2,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm2\n" \ "paddq %%xmm3,%%xmm2\n" \ "movaps %%xmm2,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm2\n" \ "movaps %%xmm2,112(%2)\n" \ "paddq 64(%4),%%xmm2\n" \ "psrlq $57,%%xmm5\n" \ "psrlq $57,%%xmm6\n" \ "psrlq $57,%%xmm7\n" \ "psrlq $57,%%xmm2\n" \ "packssdw %%xmm6,%%xmm5\n" \ "packssdw %%xmm2,%%xmm7\n" \ "packssdw %%xmm7,%%xmm5\n" \ "packsswb %%xmm5,%%xmm4\n" \ "psllw $7,%%xmm4\n" \ "pmovmskb %%xmm4,%0\n" \ "movaps 128(%3),%%xmm1\n" \ "movaps 128(%2),%%xmm4\n" \ "movaps %%xmm4,128(%3)\n" \ "psllq $3,%%xmm4\n" \ "paddq %%xmm0,%%xmm4\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm4\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm4\n" \ "movaps %%xmm4,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm4\n" \ "paddq %%xmm3,%%xmm4\n" \ "movaps %%xmm4,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm4\n" \ "movaps %%xmm4,128(%2)\n" \ "paddq 64(%4),%%xmm4\n" \ "movaps 144(%3),%%xmm1\n" \ "movaps 144(%2),%%xmm5\n" \ "movaps %%xmm5,144(%3)\n" \ "psllq $3,%%xmm5\n" \ "paddq %%xmm0,%%xmm5\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "paddq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,144(%2)\n" \ "paddq 64(%4),%%xmm5\n" \ "movaps 160(%3),%%xmm1\n" \ "movaps 160(%2),%%xmm6\n" \ "movaps %%xmm6,160(%3)\n" \ "psllq $3,%%xmm6\n" \ "paddq %%xmm0,%%xmm6\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "paddq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,160(%2)\n" \ "paddq 64(%4),%%xmm6\n" \ "movaps 176(%3),%%xmm1\n" \ "movaps 176(%2),%%xmm7\n" \ "movaps %%xmm7,176(%3)\n" \ "psllq $3,%%xmm7\n" \ "paddq %%xmm0,%%xmm7\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "paddq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,176(%2)\n" \ "paddq 64(%4),%%xmm7\n" \ "psrlq $57,%%xmm4\n" \ "psrlq $57,%%xmm5\n" \ "psrlq $57,%%xmm6\n" \ "psrlq $57,%%xmm7\n" \ "packssdw %%xmm5,%%xmm4\n" \ "packssdw %%xmm7,%%xmm6\n" \ "packssdw %%xmm6,%%xmm4\n" \ "movaps 192(%3),%%xmm1\n" \ "movaps 192(%2),%%xmm5\n" \ "movaps %%xmm5,192(%3)\n" \ "psllq $3,%%xmm5\n" \ "paddq %%xmm0,%%xmm5\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm5\n" \ "paddq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm5\n" \ "movaps %%xmm5,192(%2)\n" \ "paddq 64(%4),%%xmm5\n" \ "movaps 208(%3),%%xmm1\n" \ "movaps 208(%2),%%xmm6\n" \ "movaps %%xmm6,208(%3)\n" \ "psllq $3,%%xmm6\n" \ "paddq %%xmm0,%%xmm6\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm6\n" \ "paddq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm6\n" \ "movaps %%xmm6,208(%2)\n" \ "paddq 64(%4),%%xmm6\n" \ "movaps 224(%3),%%xmm1\n" \ "movaps 224(%2),%%xmm7\n" \ "movaps %%xmm7,224(%3)\n" \ "psllq $3,%%xmm7\n" \ "paddq %%xmm0,%%xmm7\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm7\n" \ "paddq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm7\n" \ "movaps %%xmm7,224(%2)\n" \ "paddq 64(%4),%%xmm7\n" \ "movaps 240(%3),%%xmm1\n" \ "movaps 240(%2),%%xmm2\n" \ "movaps %%xmm2,240(%3)\n" \ "psllq $3,%%xmm2\n" \ "paddq %%xmm0,%%xmm2\n" \ "psllq $4,%%xmm1\n" \ "psubq %%xmm1,%%xmm2\n" \ "psllq $1,%%xmm1\n" \ "psubq %%xmm1,%%xmm2\n" \ "movaps %%xmm2,%%xmm1\n" \ "psrlq $58,%%xmm1\n" \ "psllq $29,%%xmm1\n" \ "movaps %%xmm1,%%xmm3\n" \ "psllq $1,%%xmm3\n" \ "paddq %%xmm1,%%xmm3\n" \ "psllq $29,%%xmm1\n" \ "psubq %%xmm1,%%xmm2\n" \ "paddq %%xmm3,%%xmm2\n" \ "movaps %%xmm2,%%xmm1\n" \ "paddq 16(%4),%%xmm1\n" \ "pshufd $245,%%xmm1,%%xmm3\n" \ "pcmpgtd 32(%4),%%xmm3\n" \ "pand 48(%4),%%xmm3\n" \ "psubq %%xmm3,%%xmm2\n" \ "movaps %%xmm2,240(%2)\n" \ "paddq 64(%4),%%xmm2\n" \ "psrlq $57,%%xmm5\n" \ "psrlq $57,%%xmm6\n" \ "psrlq $57,%%xmm7\n" \ "psrlq $57,%%xmm2\n" \ "packssdw %%xmm6,%%xmm5\n" \ "packssdw %%xmm2,%%xmm7\n" \ "packssdw %%xmm7,%%xmm5\n" \ "packsswb %%xmm5,%%xmm4\n" \ "psllw $7,%%xmm4\n" \ "pmovmskb %%xmm4,%1\n" \ "shll $16,%1\n" \ "addl %0,%1\n" \ "":"=&r"(output1),"=&r"(output2):"r"(state->xN),"r"(state->xP),"r"(gq58x1_sse_Consts)); return output2; } __device__ __host__ void gq58x1_get_sse_state_(gq58x1_state* state,gq58x1_sse_state* sse_state){ int i; for(i=0;i<32;i++) {sse_state->xN[i]=state->xN[i]; sse_state->xP[i]=state->xP[i];} } __device__ __host__ lt gq58x1_mod_g(lt x){ // returns x (mod g) lt F,G; F = (x>>58); G = x-(F<<58)+(F<<29)+(F<<30); return ((G>=gq58x1_g) ? (G-gq58x1_g) : G); } __device__ __host__ lt gq58x1_MyMult(lt A,lt B){ // returns AB (mod gq58x1_g), where it is implied that A,B<gq58x1_g; lt A1,A0,B1,B0,curr,x,m; A1=A>>32; B1=B>>32; A0=A-(A1<<32)+(12*A1); B0=B-(B1<<32)+(12*B1); if(A0>>32) {A0-=4294967284ULL; A1++;} if(B0>>32) {B0-=4294967284ULL; B1++;} curr=A1*B0+B1*A0; m=curr>>26; x=curr-(m<<26); curr=((3*m+(x<<4))<<28)+(gq58x1_g-12*x)+(144*A1*B1)+(gq58x1_mod_g(A0*B0)); return gq58x1_mod_g(curr); } __device__ __host__ lt gq58x1_CNext2(lt N,lt P,lt myk,lt myq){ // returns (myk*N-myq*P) (mod gq58x1_g) lt curr1,curr2; curr1=gq58x1_MyMult(myk,N); curr2=gq58x1_MyMult(myq,P); if(curr1>=curr2) return (curr1-curr2); else return (gq58x1_g+curr1-curr2); } __device__ __host__ lt gq58x1_CNext(lt N,lt P){ // returns (8N-48P) (mod gq58x1_g) return gq58x1_mod_g((N+6*(gq58x1_g-P))<<3); } __device__ __host__ lt gq58x1_GetNextN(lt x0,lt x1,unsigned int n){ //returns x_{2^n} lt myk=gq58x1_k,myq=gq58x1_q,i,x=x1; for(i=0;i<n;i++){ x=gq58x1_CNext2(x,x0,myk,myq); myk=gq58x1_CNext2(myk,2,myk,myq); myq=gq58x1_CNext2(myq,0,myq,0); } return x; } __device__ __host__ lt gq58x1_GetNextAny(lt x0,lt x1,lt N64,lt N0){ //N=2^64*N64+N0+1 lt i,xp=x0,xn=x1,xpnew,xnnew,shift=0; i=N0; while(i>0){ if(i%2==1){ // xp,xn ----> 2^shift xpnew=gq58x1_GetNextN(xp,xn,shift); xnnew=gq58x1_GetNextN(xn,gq58x1_CNext(xn,xp),shift); xp=xpnew; xn=xnnew; } i/=2; shift++; } i=N64; shift=64; while(i>0){ if(i%2==1){ // xp,xn ----> 2^shift xpnew=gq58x1_GetNextN(xp,xn,shift); xnnew=gq58x1_GetNextN(xn,gq58x1_CNext(xn,xp),shift); xp=xpnew; xn=xnnew; } i/=2; shift++; } return xp; // returns x_N, where N=2^64*N64+N0+1 } __device__ __host__ void gq58x1_skipahead_(gq58x1_state* state, lt offset64, lt offset0){ // offset=offset64*2^64+offset0+1 lt xn,xp,j; for(j=0;j<32;j++){ xp=gq58x1_GetNextAny(state->xP[j],state->xN[j],offset64,offset0); xn=gq58x1_GetNextAny(state->xP[j],state->xN[j],offset64,offset0+1); state->xP[j]=xp; state->xN[j]=xn; } } __device__ __host__ void gq58x1_init_(gq58x1_state* state){ lt x0=100152853817629549ULL,x1=132388305121829306ULL,xp,xn,j; for(j=0;j<32;j++){ xp=gq58x1_GetNextAny(x0,x1,0,9003035591327124ULL); xn=gq58x1_GetNextAny(x0,x1,0,9003035591327125ULL); state->xP[j]=xp; state->xN[j]=xn; x0=xp; x1=xn; } } __device__ __host__ void gq58x1_init_short_sequence_(gq58x1_state* state,unsigned SequenceNumber){ gq58x1_init_(state); // 0 <= SequenceNumber < 10^8; length of each sequence <= 8*10^7 gq58x1_skipahead_(state,0,82927047ULL*(unsigned long long)SequenceNumber); } __device__ __host__ void gq58x1_init_medium_sequence_(gq58x1_state* state,unsigned SequenceNumber){ gq58x1_init_(state); // 0 <= SequenceNumber < 10^6; length of each sequence <= 8*10^9 gq58x1_skipahead_(state,0,8799201913ULL*(unsigned long long)SequenceNumber); } __device__ __host__ void gq58x1_init_long_sequence_(gq58x1_state* state,unsigned SequenceNumber){ gq58x1_init_(state); // 0 <= SequenceNumber < 10^4; length of each sequence <= 8*10^11 gq58x1_skipahead_(state,0,828317697521ULL*(unsigned long long)SequenceNumber); } __device__ __host__ unsigned int gq58x1_generate_(gq58x1_state* state){ unsigned int sum=0,bit=1; int i; lt temp; for(i=0;i<32;i++){ temp=gq58x1_mod_g((state->xN[i]+6*(gq58x1_g-state->xP[i]))<<3); state->xP[i]=state->xN[i]; state->xN[i]=temp; sum+= ((temp<gq58x1_halfg)?0:bit); bit*=2; } return sum; } __device__ __host__ float gq58x1_generate_uniform_float_(gq58x1_state* state){ unsigned int sum=0,bit=1; int i; lt temp; for(i=0;i<32;i++){ temp=gq58x1_mod_g((state->xN[i]+6*(gq58x1_g-state->xP[i]))<<3); state->xP[i]=state->xN[i]; state->xN[i]=temp; sum+= ((temp<gq58x1_halfg)?0:bit); bit*=2; } return ((float) sum) * 2.3283064365386963e-10; } __host__ void gq58x1_print_state_(gq58x1_state* state){int i; printf("Generator State:\nxN={"); for(i=0;i<32;i++) {printf("%llu",state->xN[i]%gq58x1_g); printf((i<31)?",":"}\nxP={");} for(i=0;i<32;i++) {printf("%llu",state->xP[i]%gq58x1_g); printf((i<31)?",":"}\n\n");} } __host__ void gq58x1_print_sse_state_(gq58x1_sse_state* state){int i; printf("Generator State:\nxN={"); for(i=0;i<32;i++) {printf("%llu",state->xN[i]%gq58x1_g); printf((i<31)?",":"}\nxP={");} for(i=0;i<32;i++) {printf("%llu",state->xP[i]%gq58x1_g); printf((i<31)?",":"}\n\n");} } __global__ void gq58x1_kernel_generate_array(gq58x1_state* state, unsigned int* out, long* length) { unsigned sum,i,orbit,seqNum; long offset; lt temp; __shared__ lt xP[gq58x1_THREADS]; // one generator per s=32 threads, i.e. one orbit __shared__ lt xN[gq58x1_THREADS]; // per thread, i.e. blockDim.x orbits per block __shared__ unsigned a[gq58x1_THREADS]; // array "a" contains corresponding parts of output orbit = threadIdx.x % 32; seqNum = (threadIdx.x + blockIdx.x * blockDim.x)>>5; // RNG_sequence index offset = seqNum*(*length); // start of the section in the output array xP[threadIdx.x]=gq58x1_GetNextAny(state->xP[orbit],state->xN[orbit],0,offset); xN[threadIdx.x]=gq58x1_GetNextAny(state->xP[orbit],state->xN[orbit],0,offset+1); for(i=0;i<(*length);i++){ // each s=32 threads result in "length" values in the output array temp = gq58x1_CNext( xN[threadIdx.x], xP[threadIdx.x] ); xP[threadIdx.x] = xN[threadIdx.x]; xN[threadIdx.x] = temp; a[threadIdx.x] = (temp < gq58x1_halfg ? 0 : (1<<orbit) ); __syncthreads(); if((orbit&3)==0) a[threadIdx.x] = a[threadIdx.x]+a[threadIdx.x+1]+a[threadIdx.x+2]+a[threadIdx.x+3]; __syncthreads(); if((orbit&15)==0) a[threadIdx.x] = a[threadIdx.x]+a[threadIdx.x+4]+a[threadIdx.x+8]+a[threadIdx.x+12]; __syncthreads(); if(orbit==0){ sum=a[threadIdx.x]+a[threadIdx.x+16]; out[offset+i]=sum; } } } __host__ void gq58x1_generate_gpu_array_(gq58x1_state* state, unsigned int* dev_out, long length){ long mylength = length/gq58x1_ARRAY_SECTIONS; gq58x1_state* dev_state; long* dev_length; if((mylength*gq58x1_ARRAY_SECTIONS)<length) mylength++; gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_state,sizeof(gq58x1_state))); gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_length,sizeof(long))); gq58x1_CUDA_CALL(cudaMemcpy(dev_state,state,sizeof(gq58x1_state),cudaMemcpyHostToDevice)); gq58x1_CUDA_CALL(cudaMemcpy(dev_length,&mylength,sizeof(long),cudaMemcpyHostToDevice)); gq58x1_kernel_generate_array<<<gq58x1_BLOCKS,gq58x1_THREADS>>>(dev_state,dev_out,dev_length); gq58x1_CUDA_CALL(cudaGetLastError()); gq58x1_CUDA_CALL(cudaFree(dev_state)); gq58x1_CUDA_CALL(cudaFree(dev_length)); } __global__ void gq58x1_kernel_generate_array_float(gq58x1_state* state, float* out, long* length) { unsigned sum,i,orbit,seqNum; long offset; lt temp; __shared__ lt xP[gq58x1_THREADS]; // one generator per s=32 threads, i.e. one orbit __shared__ lt xN[gq58x1_THREADS]; // per thread, i.e. blockDim.x orbits per block __shared__ unsigned a[gq58x1_THREADS]; // array "a" contains corresponding parts of output orbit = threadIdx.x % 32; seqNum = (threadIdx.x + blockIdx.x * blockDim.x)>>5; // RNG_sequence index offset = seqNum*(*length); // start of the section in the output array xP[threadIdx.x]=gq58x1_GetNextAny(state->xP[orbit],state->xN[orbit],0,offset); xN[threadIdx.x]=gq58x1_GetNextAny(state->xP[orbit],state->xN[orbit],0,offset+1); for(i=0;i<(*length);i++){ // each s=32 threads result in "length" values in the output array temp = gq58x1_CNext( xN[threadIdx.x], xP[threadIdx.x] ); xP[threadIdx.x] = xN[threadIdx.x]; xN[threadIdx.x] = temp; a[threadIdx.x] = (temp < gq58x1_halfg ? 0 : (1<<orbit) ); __syncthreads(); if((orbit&3)==0) a[threadIdx.x] = a[threadIdx.x]+a[threadIdx.x+1]+a[threadIdx.x+2]+a[threadIdx.x+3]; __syncthreads(); if((orbit&15)==0) a[threadIdx.x] = a[threadIdx.x]+a[threadIdx.x+4]+a[threadIdx.x+8]+a[threadIdx.x+12]; __syncthreads(); if(orbit==0){ sum=a[threadIdx.x]+a[threadIdx.x+16]; out[offset+i]=((float)sum) * 2.3283064365386963e-10; } } } __host__ void gq58x1_generate_gpu_array_float_(gq58x1_state* state, float* dev_out, long length){ long mylength = length/gq58x1_ARRAY_SECTIONS; gq58x1_state* dev_state; long* dev_length; if((mylength*gq58x1_ARRAY_SECTIONS)<length) mylength++; gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_state,sizeof(gq58x1_state))); gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_length,sizeof(long))); gq58x1_CUDA_CALL(cudaMemcpy(dev_state,state,sizeof(gq58x1_state),cudaMemcpyHostToDevice)); gq58x1_CUDA_CALL(cudaMemcpy(dev_length,&mylength,sizeof(long),cudaMemcpyHostToDevice)); gq58x1_kernel_generate_array_float<<<gq58x1_BLOCKS,gq58x1_THREADS>>>(dev_state,dev_out,dev_length); gq58x1_CUDA_CALL(cudaGetLastError()); gq58x1_CUDA_CALL(cudaFree(dev_state)); gq58x1_CUDA_CALL(cudaFree(dev_length)); } __global__ void gq58x1_kernel_generate_array_double(gq58x1_state* state, double* out, long* length) { unsigned sum,i,orbit,seqNum; long offset; lt temp; __shared__ lt xP[gq58x1_THREADS]; // one generator per s=32 threads, i.e. one orbit __shared__ lt xN[gq58x1_THREADS]; // per thread, i.e. blockDim.x orbits per block __shared__ unsigned a[gq58x1_THREADS]; // array "a" contains corresponding parts of output orbit = threadIdx.x % 32; seqNum = (threadIdx.x + blockIdx.x * blockDim.x)>>5; // RNG_sequence index offset = seqNum*(*length); // start of the section in the output array xP[threadIdx.x]=gq58x1_GetNextAny(state->xP[orbit],state->xN[orbit],0,offset); xN[threadIdx.x]=gq58x1_GetNextAny(state->xP[orbit],state->xN[orbit],0,offset+1); for(i=0;i<(*length);i++){ // each s=32 threads result in "length" values in the output array temp = gq58x1_CNext( xN[threadIdx.x], xP[threadIdx.x] ); xP[threadIdx.x] = xN[threadIdx.x]; xN[threadIdx.x] = temp; a[threadIdx.x] = (temp < gq58x1_halfg ? 0 : (1<<orbit) ); __syncthreads(); if((orbit&3)==0) a[threadIdx.x] = a[threadIdx.x]+a[threadIdx.x+1]+a[threadIdx.x+2]+a[threadIdx.x+3]; __syncthreads(); if((orbit&15)==0) a[threadIdx.x] = a[threadIdx.x]+a[threadIdx.x+4]+a[threadIdx.x+8]+a[threadIdx.x+12]; __syncthreads(); if(orbit==0){ sum=a[threadIdx.x]+a[threadIdx.x+16]; out[offset+i]=((double)sum) * 2.3283064365386963e-10; } } } __host__ void gq58x1_generate_gpu_array_double_(gq58x1_state* state, double* dev_out, long length){ long mylength = length/gq58x1_ARRAY_SECTIONS; gq58x1_state* dev_state; long* dev_length; if((mylength*gq58x1_ARRAY_SECTIONS)<length) mylength++; gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_state,sizeof(gq58x1_state))); gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_length,sizeof(long))); gq58x1_CUDA_CALL(cudaMemcpy(dev_state,state,sizeof(gq58x1_state),cudaMemcpyHostToDevice)); gq58x1_CUDA_CALL(cudaMemcpy(dev_length,&mylength,sizeof(long),cudaMemcpyHostToDevice)); gq58x1_kernel_generate_array_double<<<gq58x1_BLOCKS,gq58x1_THREADS>>>(dev_state,dev_out,dev_length); gq58x1_CUDA_CALL(cudaGetLastError()); gq58x1_CUDA_CALL(cudaFree(dev_state)); gq58x1_CUDA_CALL(cudaFree(dev_length)); } __host__ void gq58x1_generate_array_(gq58x1_state* state, unsigned int* out, long length){ long mylength = length/gq58x1_ARRAY_SECTIONS; gq58x1_state* dev_state; unsigned int* dev_out; long* dev_length; if((mylength*gq58x1_ARRAY_SECTIONS)<length) mylength++; gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_state,sizeof(gq58x1_state))); gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_out,mylength*gq58x1_ARRAY_SECTIONS*sizeof(unsigned int))); gq58x1_CUDA_CALL(cudaMalloc((void**)&dev_length,sizeof(long))); gq58x1_CUDA_CALL(cudaMemcpy(dev_state,state,sizeof(gq58x1_state),cudaMemcpyHostToDevice)); gq58x1_CUDA_CALL(cudaMemcpy(dev_length,&mylength,sizeof(long),cudaMemcpyHostToDevice)); gq58x1_kernel_generate_array<<<gq58x1_BLOCKS,gq58x1_THREADS>>>(dev_state,dev_out,dev_length); gq58x1_CUDA_CALL(cudaGetLastError()); gq58x1_CUDA_CALL(cudaMemcpy(out,dev_out,length*sizeof(unsigned int),cudaMemcpyDeviceToHost)); gq58x1_CUDA_CALL(cudaFree(dev_state)); gq58x1_CUDA_CALL(cudaFree(dev_out)); gq58x1_CUDA_CALL(cudaFree(dev_length)); }
21,539
/* *Derek Trom *HW5 CSCI364 */ #include <stdlib.h> #include <iostream> #include <math.h> #include <iomanip> #include <cstdio> __device__ float add(float num){ float outnum = num + 1; return outnum; } __global__ void func1(float *xd, float *yd, int n) { int threadId = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = threadId; i < n; i+= stride) { yd[i] = add(xd[i]); } } __global__ void createArrays(float *in, float *out, int n){ int threadId = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = threadId; i < n; i+=stride) { in[i] = 1.0f; out[i] = 0.0f; } } int main(int argc, char **argv){ using namespace std; if( argc< 3){ cerr<<"Usage: "<<argv[0]<<" <length of arrays> <num threads/block>"<<endl; return 1; } int threads_per_block = atoi(argv[2]); int sizeOfArray = atoi(argv[1]); if (sizeOfArray < 1 or threads_per_block < 1){ cerr<<"Array length and block size must be > 0"<<endl; return 1; } float *xd, *yd; cudaMallocManaged(&xd, sizeOfArray*sizeof(float)); cudaMallocManaged(&yd, sizeOfArray*sizeof(float)); //---------PHASE ONE----------// int numBlocks = (sizeOfArray + threads_per_block- 1) / threads_per_block; createArrays<<<numBlocks, threads_per_block>>>(xd, yd, sizeOfArray); func1<<<numBlocks, threads_per_block>>>(xd,yd,sizeOfArray); cudaDeviceSynchronize(); float maxError = 0.0f; for (int i = 0; i < sizeOfArray; i++) { maxError = fmax(maxError, fabs(yd[i]-2.0f)); } cout<<"Phase 1"<<endl; cout<<endl<<"Array size: "<<sizeOfArray<<endl; cout<<"Threads per block: "<<threads_per_block<<endl; cout<<"Number of blocks: "<<numBlocks<<endl; cout << "Max error: " << maxError << endl; //--------Phase 2-------// //Use half the number of blocks to get the next number but use //the same kernel function threads_per_block = threads_per_block/2; createArrays<<<numBlocks, threads_per_block>>>(xd, yd, sizeOfArray); func1<<<numBlocks, threads_per_block>>>(xd,yd,sizeOfArray); cudaDeviceSynchronize(); for (int i = 0; i < sizeOfArray; i++) { maxError = fmax(maxError, fabs(yd[i]-2.0f)); } cout<<"Phase 2"<<endl; cout<<endl<<"Array size: "<<sizeOfArray<<endl; cout<<"Threads per block: "<<threads_per_block<<endl; cout<<"Number of blocks: "<<numBlocks<<endl; cout << "Max error: " << maxError << endl; cudaFree(xd); cudaFree(yd); return 0; }
21,540
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define MAXPOINTS 1000000 #define MAXSTEPS 1000000 #define MINPOINTS 20 #define PI 3.14159265 #define THREAD_COUNT 1024 void check_param(void); void init_line(void); void update (void); void printfinal (void); int nsteps, /* number of time steps */ tpoints, /* total points along string */ rcode; /* generic return code */ float values[MAXPOINTS+2], /* values at time t */ oldval[MAXPOINTS+2], /* values at time (t-dt) */ newval[MAXPOINTS+2]; /* values at time (t+dt) */ /********************************************************************** * Checks input values from parameters *********************************************************************/ void check_param(void) { char tchar[20]; /* check number of points, number of iterations */ while ((tpoints < MINPOINTS) || (tpoints > MAXPOINTS)) { printf("Enter number of points along vibrating string [%d-%d]: " ,MINPOINTS, MAXPOINTS); scanf("%s", tchar); tpoints = atoi(tchar); if ((tpoints < MINPOINTS) || (tpoints > MAXPOINTS)) printf("Invalid. Please enter value between %d and %d\n", MINPOINTS, MAXPOINTS); } while ((nsteps < 1) || (nsteps > MAXSTEPS)) { printf("Enter number of time steps [1-%d]: ", MAXSTEPS); scanf("%s", tchar); nsteps = atoi(tchar); if ((nsteps < 1) || (nsteps > MAXSTEPS)) printf("Invalid. Please enter value between 1 and %d\n", MAXSTEPS); } printf("Using points = %d, steps = %d\n", tpoints, nsteps); } /********************************************************************** * Initialize points on line *********************************************************************/ void init_line(void) { int i, j; float x, fac, k, tmp; /* Calculate initial values based on sine curve */ fac = 2.0 * PI; k = 0.0; tmp = tpoints - 1; for (j = 1; j <= tpoints; j++) { x = k/tmp; values[j] = sin (fac * x); k = k + 1.0; } /* Initialize old values array */ for (i = 1; i <= tpoints; i++) oldval[i] = values[i]; } /********************************************************************** * Calculate new values using wave equation *********************************************************************/ void do_math(int i) { float dtime, c, dx, tau, sqtau; dtime = 0.3; c = 1.0; dx = 1.0; tau = (c * dtime / dx); sqtau = tau * tau; newval[i] = (2.0 * values[i]) - oldval[i] + (sqtau * (-2.0)*values[i]); } /********************************************************************** * Update all values along line a specified number of times *********************************************************************/ void update() { int i, j; /* Update values for each time step */ for (i = 1; i<= nsteps; i++) { /* Update points along line for this time step */ for (j = 1; j <= tpoints; j++) { /* global endpoints */ if ((j == 1) || (j == tpoints)) newval[j] = 0.0; else do_math(j); } /* Update old values with new values */ for (j = 1; j <= tpoints; j++) { oldval[j] = values[j]; values[j] = newval[j]; } } } // implement algorithm described in: Exact and Approximated error of the FMA // by Sylvie Boldo and Jean-Michel Muller __device__ float Fast2Sum(float a, float b, float *err) { if (abs(a) < abs(b)) { float t=a; a=b; b=t; } float x = a + b; float t = x - a; *err = b - t; return x; } __device__ float Fast2Mult(float a, float b, float *err) { float x = a * b; *err = __fmaf_rn(a, b, -x); return x; } /* update one value for each thread */ __global__ void updateKernel(float *gpuMem, int nsteps, int tpoints) { int j = blockIdx.x * blockDim.x + threadIdx.x; float dtime, c, dx, tau, sqtau; dtime = 0.3; c = 1.0; dx = 1.0; tau = (c * dtime / dx); sqtau = tau * tau; /* thread might be unused */ if (j >= tpoints-1) return ; float value = gpuMem[j]; float oldval = value; /* update values for each time step */ int i; for (i = 1; i <= nsteps; i++) { float newval, e1, e2, e3; float moment = Fast2Sum(2.0f * value, -oldval, &e1); float damp = Fast2Mult((-2.0f) * sqtau, value, &e2); newval = Fast2Sum(moment, damp, &e3); newval = e1 + e2 + e3 + newval; oldval = value; value = newval; } gpuMem[j] = value; } /********************************************************************** * Print final results *********************************************************************/ void printfinal() { int i; for (i = 1; i <= tpoints; i++) { printf("%6.4f ", values[i]); if (i%10 == 0) printf("\n"); } } void TellError(int err) { if (err == cudaErrorInvalidValue) puts("invalid value"); if (err == cudaErrorInvalidDevicePointer) puts("invalid device pointer"); if (err == cudaErrorInvalidMemcpyDirection) puts("invalid memcpy direction"); } /********************************************************************** * Main program *********************************************************************/ int main(int argc, char *argv[]) { if (argc < 3) return 1; sscanf(argv[1],"%d",&tpoints); sscanf(argv[2],"%d",&nsteps); check_param(); /* calculate block size and allocate gpu memory */ int blockCount = (tpoints + (THREAD_COUNT)) / (THREAD_COUNT); int size = blockCount * (THREAD_COUNT); int usedSize = (tpoints + 1); float *gpuMem; int e; e = cudaMalloc((void**)&gpuMem, size * sizeof(float)); if (e != cudaSuccess) TellError(e); printf("Initializing points on the line...\n"); init_line(); e = cudaMemcpy(gpuMem, values+1, usedSize * sizeof(float), cudaMemcpyHostToDevice); if (e != cudaSuccess) TellError(e); printf("Updating all points for all time steps...\n"); updateKernel<<<blockCount, THREAD_COUNT>>>(gpuMem, nsteps, tpoints); e = cudaMemcpy(values+1, gpuMem, usedSize * sizeof(float), cudaMemcpyDeviceToHost); if (e != cudaSuccess) TellError(e); printf("Printing final results...\n"); printfinal(); cudaFree(gpuMem); printf("\nDone.\n\n"); return 0; }
21,541
// #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <algorithm> //Limite de hilos 1024 #define M 4096 __global__ void filtroKernel(unsigned char *a) { int j=blockIdx.x; int i=threadIdx.x; int R,G,B; int tr,tg,tb; for(i=threadIdx.x;i<4096;i+=1024) { if(i>1||i<4094||j>1||j<4094) { tr=tg=tb=0; R=i+j*4096; G=i+j*4096+4096*4096; B=i+j*4096+4096*4096*2; for(int x=-2;x<3;x++) { for(int y=-2;y<3;y++) { tr+=a[R+x+y*M]; tg+=a[G+x+y*M]; tb+=a[B+x+y*M]; } } __syncthreads: a[R]=tr/25; a[G]=tg/25; a[B]=tb/25; } } } __global__ void bordeKernel(unsigned char* a,int* temp) { int j=blockIdx.x; int i=threadIdx.x; int R,G,B; if(j==0||j==4095) return; for(i=threadIdx.x;i<4095;i+=1024) { if(i==0) continue; R=i+j*4096; G=i+j*4096+4096*4096; B=i+j*4096+4096*4096*2; for(int x=-1;x<2;x++) { for(int y=-1;y<2;y++) { temp[R]+=std::abs(a[R]-a[R+x+M*y]); temp[R]+=std::abs(a[G]-a[G+x+M*y]); temp[R]+=std::abs(a[B]-a[B+x+M*y]); } } temp[R]/=9; } } __global__ void finKernel(unsigned char* a,int* temp) { int j=blockIdx.x; int i=threadIdx.x; int R,G,B; for(i=threadIdx.x;i<4096;i+=1024) { R=i+j*4096; G=i+j*4096+4096*4096; B=i+j*4096+4096*4096*2; //a[R]=a[G]=a[B]=temp[R]; if(temp[R]>20) { a[R]=a[G]=a[B]=255; } else { a[R]=a[G]=a[B]=0; } } } extern "C" int main2(unsigned char* imag) { cudaError_t cudaStatus=cudaSetDevice(0); unsigned char *Im=0; int *Temp=0; cudaStatus = cudaMalloc<unsigned char>(&Im,4096*4096*3*sizeof(unsigned char)); cudaStatus = cudaMalloc<int>(&Temp,4096*4096*sizeof(int)); cudaStatus = cudaMemcpy(Im,imag,4096*4096*3*sizeof(unsigned char),cudaMemcpyHostToDevice); cudaStatus= cudaMemset(Temp,0,4096*4096*sizeof(int)); filtroKernel<<<4096,1024>>>(Im); cudaDeviceSynchronize(); bordeKernel<<<4096,1024>>>(Im,Temp); cudaStatus=cudaGetLastError(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "1! fallo lanzamiento! Lo has hecho bien?\n"); } cudaDeviceSynchronize(); finKernel<<<4096,1024>>>(Im,Temp); cudaStatus=cudaMemcpy(imag,Im,4096*4096*3*sizeof(unsigned char),cudaMemcpyDeviceToHost); if (cudaStatus != cudaSuccess) { fprintf(stderr, "2! fallo lanzamiento! Lo has hecho bien?\n"); } 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; } */
21,542
#include "main.cuh" #include <cuda_runtime.h> #include <iostream> using namespace std; int main(int argc, char **argv) { // Error code to check return values for CUDA calls cudaError_t err = cudaSuccess; // Print the vector length to be used, and compute its size int numElements = 1 << 19; cout << "Vector addition of " << numElements << " elements" << endl; // host memory float *h_A = NULL; float *h_B = NULL; float *h_C = NULL; // device memory float *d_A = NULL; float *d_B = NULL; float *d_C = NULL; if (!AlocateHstMemory(&h_A, numElements, "h_A")) exit(1); if (!AlocateHstMemory(&h_B, numElements, "h_B")) exit(1); if (!AlocateHstMemory(&h_C, numElements, "h_C")) exit(1); RandInit(h_A, numElements); RandInit(h_B, numElements); if (!AlocateDevMemory(&d_A, numElements, "d_A")) exit(1); if (!AlocateDevMemory(&d_B, numElements, "d_B")) exit(1); if (!AlocateDevMemory(&d_C, numElements, "d_C")) exit(1); // Copy input to the device CopyHst2DevMemory(h_A, d_A, numElements); CopyHst2DevMemory(h_B, d_B, numElements); // Launch the Vector Add CUDA Kernel int threadsPerBlock = 256; int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock; cout << "CUDA kernel launch with " << blocksPerGrid; cout << " blocks of " << threadsPerBlock << " threads" << endl; vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, numElements); err = cudaGetLastError(); if (err != cudaSuccess) { cout << "status: " << cudaGetErrorString(err) << endl; cout << "Failed to launch vectorAdd kernel" << endl; exit(1); } // Copy results to the host CopyDev2HstMemory(d_C, h_C, numElements); // Verify that the result vector is correct for (int i = 0; i < numElements; ++i) { if (fabs(h_A[i] + h_B[i] - h_C[i]) > 1e-5) { cout << "Result verification failed at element " << i << endl; exit(1); } } cout << "Test PASSED" << endl; // Free device global memory freeDev(d_A); freeDev(d_B); freeDev(d_C); // Free host memory free(h_A); free(h_B); free(h_C); cout << "Done" << endl; cout << endl; return 0; } // --------------------------------------------------------------------------- // Alocate host memory and perform validation. // --------------------------------------------------------------------------- bool AlocateHstMemory(float** h, const int& numElements, const string& name) { size_t size = numElements * sizeof(float); *h = (float *)malloc(size); if (*h != NULL) return true; cout << "Failed to allocate host memory: " << name << endl; return false; } // --------------------------------------------------------------------------- // Alocate device memory and perform validation. // --------------------------------------------------------------------------- bool AlocateDevMemory(float** d, const int& numElements, const string& name) { cudaError_t err = cudaSuccess; size_t size = numElements * sizeof(float); err = cudaMalloc((void **)d, size); if (err == cudaSuccess) return true; cout << "status: " << cudaGetErrorString(err) << endl; cout << "Failed to allocate device memory: " << name << endl; return false; } // --------------------------------------------------------------------------- // Initialize host memory. // --------------------------------------------------------------------------- void RandInit(float* h, const int& numElements) { for (int i = 0; i < numElements; ++i) h[i] = rand()/(float)RAND_MAX; } // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- bool CopyHst2DevMemory(float* h, float* d, const int& numElements) { cudaError_t err = cudaSuccess; size_t size = numElements * sizeof(float); cout << "Copy host memory to the CUDA device." << endl; err = cudaMemcpy(d, h, size, cudaMemcpyHostToDevice); if (err == cudaSuccess) return true; cout << "status: " << cudaGetErrorString(err) << endl; cout << "Failed to copy host memory to the CUDA device." << endl; return false; } // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- bool CopyDev2HstMemory(float* d, float* h, const int& numElements) { cudaError_t err = cudaSuccess; size_t size = numElements * sizeof(float); cout << "Copy device memory to the host." << endl; err = cudaMemcpy(h, d, size, cudaMemcpyDeviceToHost); if (err == cudaSuccess) return true; cout << "status: " << cudaGetErrorString(err) << endl; cout << "Failed to copy device memory to the host." << endl; return false; } // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- bool freeDev(float* d_A) { cudaError_t err = cudaSuccess; err = cudaFree(d_A); if (err == cudaSuccess) return true; cout << "status: " << cudaGetErrorString(err) << endl; cout << "Failed to free device vector A" << endl; return false; }
21,543
/* ********************************************** * CS314 Principles of Programming Languages * * Spring 2020 * ********************************************** */ #include <stdio.h> #include <stdlib.h> __global__ void check_handshaking_gpu(int * strongNeighbor, int * matches, int numNodes) { /*YOUR CODE HERE*/ }
21,544
/* * An exercise on the different types of memory available in CUDA */ #include <iostream> #include <cstdlib> // Error checking macro function #define myCudaCheck(result) { cudaErrorCheck((result), __FILE__, __LINE__); } inline void cudaErrorCheck(cudaError_t err, const char* file, int line) { if (err != cudaSuccess) { std::cerr << "CUDA error: " << cudaGetErrorString(err) << " at " << file << ":" << line << std::endl; exit(err); } } // Array size // HANDSON 2.1 Change the array size to a static __constant__ int #define ARRAY_SIZE 65536 static __constant__ int arr_size; // CUDA threads per block #define nThreads 128 // Array reversing kernel __global__ void reverse(float* devA, float* devB) { // HANDSON 2.3 Create a __shared__ temporary array of length nThreads for the swap __shared__ float tmp_arr[nThreads]; // Get the index in this block int idx = blockIdx.x * blockDim.x + threadIdx.x; // HANDSON 2.4 Fill the temporary array tmp_arr[nThreads - (threadIdx.x+1)] = devA[idx]; // HANDSON 2.5 synchronize the threads __syncthreads(); // HANDSON 2.6 Calculate the initial position of this block in the grid int blockOffset = arr_size - (blockIdx.x + 1) * blockDim.x; // Reverse the elements // HANDSON 2.7 Fill the output array with the reversed elements from this block devB[blockOffset + threadIdx.x] = tmp_arr[threadIdx.x]; } // Main host function int main( ) { // HANDSON 2.2 Replace the host array size by a const int // Here and elsewhere // size of the array in char const int host_size = 65536; size_t sizeChar = host_size * sizeof(float); // Allocate host memory float* hostIn = (float*) malloc(sizeChar); float* hostOut = (float*) malloc(sizeChar); // Allocate device memory float* devIn; float* devOut; myCudaCheck( cudaMalloc(&devIn, sizeChar) ); myCudaCheck( cudaMalloc(&devOut, sizeChar) ); // Initialize the arrays for (int i = 0; i < host_size; i++) { hostIn[i] = i; hostOut[i] = 0; } myCudaCheck( cudaMemcpyToSymbol( arr_size, &host_size, sizeof(int)) ); // Copy the input array from the host to the device myCudaCheck( cudaMemcpy(devIn, hostIn, sizeChar, cudaMemcpyHostToDevice) ); // Define the size of the task dim3 blocksPerGrid(host_size/nThreads); dim3 threadsPerBlock(nThreads); reverse<<<blocksPerGrid, threadsPerBlock>>>(devIn, devOut); // Wait for all threads to complete myCudaCheck( cudaDeviceSynchronize() ); // Copy the result array back to the host myCudaCheck( cudaMemcpy(hostOut, devOut, sizeChar, cudaMemcpyDeviceToHost) ); // Check and print the result int nCorrect = 0; for (int i = 0; i < host_size; i++) { nCorrect += (hostOut[i] == hostIn[host_size - (i+1)]) ? 1 : 0; } std::cout << ((nCorrect == host_size) ? "Success! " : "Failure: "); std::cout << nCorrect << " elements were correctly swapped." << std::endl; // Free device memory myCudaCheck( cudaFree(devIn) ); myCudaCheck( cudaFree(devOut) ); // Free host memory free(hostIn); free(hostOut); return 0; }
21,545
extern "C" { __device__ inline int threadIdx_x() { return threadIdx.x; } __device__ inline int threadIdx_y() { return threadIdx.y; } __device__ inline int threadIdx_z() { return threadIdx.z; } __device__ inline int blockIdx_x() { return blockIdx.x; } __device__ inline int blockIdx_y() { return blockIdx.y; } __device__ inline int blockIdx_z() { return blockIdx.z; } __device__ inline int blockDim_x() { return blockDim.x; } __device__ inline int blockDim_y() { return blockDim.y; } __device__ inline int blockDim_z() { return blockDim.z; } __device__ inline int gridDim_x() { return gridDim.x; } __device__ inline int gridDim_y() { return gridDim.y; } __device__ inline int gridDim_z() { return gridDim.z; } __global__ void lambda_111055(float*, float*); __global__ __launch_bounds__ (32 * 4 * 1) void lambda_111055(float* _111058_136790, float* _111059_136791) { int threadIdx_x_136797; int pthreadIdx_x_136797; int blockDim_x_136803; int pblockDim_x_136803; int blockIdx_x_136809; int pblockIdx_x_136809; int _136815; int p_136815; int _136821; int p_136821; int _136827; int p_136827; int _136830; int p_136830; int converge_136839; int pconverge_136839; int converge_136844; int pconverge_136844; int converge_136851; int pconverge_136851; int converge_136855; int pconverge_136855; float _136865; float p_136865; int converge_136869; int pconverge_136869; int converge_136873; int pconverge_136873; int converge_136877; int pconverge_136877; int converge_136881; int pconverge_136881; float _136887; float p_136887; float s_136898; float ps_136898; int converge_136901; int pconverge_136901; int converge_136905; int pconverge_136905; int converge_136908; int pconverge_136908; int converge_136912; int pconverge_136912; float _136918; float p_136918; int converge_136924; int pconverge_136924; int converge_136928; int pconverge_136928; int converge_136931; int pconverge_136931; int converge_136935; int pconverge_136935; float _136941; float p_136941; int converge_136944; int pconverge_136944; int converge_136948; int pconverge_136948; int converge_136951; int pconverge_136951; int converge_136955; int pconverge_136955; float _136961; float p_136961; float s_136967; float ps_136967; int converge_136970; int pconverge_136970; int converge_136974; int pconverge_136974; int converge_136977; int pconverge_136977; int converge_136981; int pconverge_136981; float _136987; float p_136987; int converge_136990; int pconverge_136990; int converge_136994; int pconverge_136994; int converge_136997; int pconverge_136997; int converge_137001; int pconverge_137001; float _137007; float p_137007; int converge_137010; int pconverge_137010; int converge_137014; int pconverge_137014; int converge_137017; int pconverge_137017; int converge_137021; int pconverge_137021; float _137027; float p_137027; float s_137033; float ps_137033; int converge_137036; int pconverge_137036; int converge_137040; int pconverge_137040; int converge_137043; int pconverge_137043; int converge_137047; int pconverge_137047; float _137053; float p_137053; int converge_137059; int pconverge_137059; int converge_137063; int pconverge_137063; int converge_137066; int pconverge_137066; int converge_137070; int pconverge_137070; float _137076; float p_137076; int converge_137079; int pconverge_137079; int converge_137083; int pconverge_137083; int converge_137086; int pconverge_137086; int converge_137090; int pconverge_137090; float _137096; float p_137096; float s_137102; float ps_137102; int converge_137105; int pconverge_137105; int converge_137109; int pconverge_137109; int converge_137112; int pconverge_137112; int converge_137116; int pconverge_137116; float _137122; float p_137122; int converge_137128; int pconverge_137128; int converge_137132; int pconverge_137132; int converge_137135; int pconverge_137135; int converge_137139; int pconverge_137139; float _137145; float p_137145; int converge_137148; int pconverge_137148; int converge_137152; int pconverge_137152; int converge_137155; int pconverge_137155; int converge_137159; int pconverge_137159; float _137165; float p_137165; float s_137171; float ps_137171; int converge_137174; int pconverge_137174; int converge_137178; int pconverge_137178; int converge_137181; int pconverge_137181; int converge_137185; int pconverge_137185; float _137191; float p_137191; int converge_137194; int pconverge_137194; int converge_137198; int pconverge_137198; int converge_137203; int pconverge_137203; int converge_137207; int pconverge_137207; float _137213; float p_137213; int converge_137216; int pconverge_137216; int converge_137220; int pconverge_137220; int converge_137223; int pconverge_137223; int converge_137227; int pconverge_137227; float _137233; float p_137233; float s_137239; float ps_137239; int converge_137242; int pconverge_137242; int converge_137246; int pconverge_137246; int converge_137249; int pconverge_137249; int converge_137253; int pconverge_137253; float _137259; float p_137259; int converge_137262; int pconverge_137262; int converge_137266; int pconverge_137266; int converge_137269; int pconverge_137269; int converge_137273; int pconverge_137273; float _137279; float p_137279; int converge_137282; int pconverge_137282; int converge_137286; int pconverge_137286; int converge_137289; int pconverge_137289; int converge_137293; int pconverge_137293; float _137299; float p_137299; float s_137305; float ps_137305; int converge_137308; int pconverge_137308; int converge_137312; int pconverge_137312; int converge_137315; int pconverge_137315; int converge_137319; int pconverge_137319; float _137325; float p_137325; int converge_137328; int pconverge_137328; int converge_137332; int pconverge_137332; int converge_137335; int pconverge_137335; int converge_137339; int pconverge_137339; float _137345; float p_137345; int converge_137348; int pconverge_137348; int converge_137352; int pconverge_137352; int converge_137355; int pconverge_137355; int converge_137359; int pconverge_137359; float _137365; float p_137365; float s_137371; float ps_137371; int converge_137374; int pconverge_137374; int converge_137378; int pconverge_137378; int converge_137381; int pconverge_137381; int converge_137385; int pconverge_137385; float _137391; float p_137391; int converge_137394; int pconverge_137394; int converge_137398; int pconverge_137398; int converge_137401; int pconverge_137401; int converge_137405; int pconverge_137405; float _137411; float p_137411; int converge_137414; int pconverge_137414; int converge_137418; int pconverge_137418; int converge_137421; int pconverge_137421; int converge_137425; int pconverge_137425; float _137431; float p_137431; float s_137437; float ps_137437; int converge_137440; int pconverge_137440; int converge_137444; int pconverge_137444; int converge_137447; int pconverge_137447; int converge_137451; int pconverge_137451; float _137457; float p_137457; int converge_137460; int pconverge_137460; int converge_137464; int pconverge_137464; int converge_137467; int pconverge_137467; int converge_137471; int pconverge_137471; float _137477; float p_137477; int converge_137480; int pconverge_137480; int converge_137484; int pconverge_137484; int converge_137487; int pconverge_137487; int converge_137491; int pconverge_137491; float _137497; float p_137497; float s_137503; float ps_137503; int converge_137506; int pconverge_137506; int converge_137510; int pconverge_137510; int converge_137513; int pconverge_137513; int converge_137517; int pconverge_137517; float _137523; float p_137523; int converge_137526; int pconverge_137526; int converge_137530; int pconverge_137530; int converge_137533; int pconverge_137533; int converge_137537; int pconverge_137537; float _137543; float p_137543; int converge_137546; int pconverge_137546; int converge_137550; int pconverge_137550; int converge_137553; int pconverge_137553; int converge_137557; int pconverge_137557; float _137563; float p_137563; float s_137569; float ps_137569; int converge_137572; int pconverge_137572; int converge_137576; int pconverge_137576; int converge_137579; int pconverge_137579; int converge_137583; int pconverge_137583; float _137589; float p_137589; int converge_137592; int pconverge_137592; int converge_137596; int pconverge_137596; int converge_137599; int pconverge_137599; int converge_137603; int pconverge_137603; float _137609; float p_137609; int converge_137612; int pconverge_137612; int converge_137616; int pconverge_137616; int converge_137619; int pconverge_137619; int converge_137623; int pconverge_137623; float _137629; float p_137629; float s_137635; float ps_137635; int converge_137638; int pconverge_137638; int converge_137642; int pconverge_137642; int converge_137645; int pconverge_137645; int converge_137649; int pconverge_137649; float _137655; float p_137655; int converge_137658; int pconverge_137658; int converge_137662; int pconverge_137662; int converge_137665; int pconverge_137665; int converge_137669; int pconverge_137669; float _137675; float p_137675; int converge_137678; int pconverge_137678; int converge_137682; int pconverge_137682; int converge_137685; int pconverge_137685; int converge_137689; int pconverge_137689; float _137695; float p_137695; float s_137701; float ps_137701; int converge_137704; int pconverge_137704; int converge_137708; int pconverge_137708; int converge_137711; int pconverge_137711; int converge_137715; int pconverge_137715; float _137721; float p_137721; int converge_137724; int pconverge_137724; int converge_137728; int pconverge_137728; int converge_137731; int pconverge_137731; int converge_137735; int pconverge_137735; float _137741; float p_137741; int converge_137744; int pconverge_137744; int converge_137748; int pconverge_137748; int converge_137751; int pconverge_137751; int converge_137755; int pconverge_137755; float _137761; float p_137761; float s_137767; float ps_137767; int converge_137770; int pconverge_137770; int converge_137774; int pconverge_137774; int converge_137777; int pconverge_137777; int converge_137781; int pconverge_137781; float _137787; float p_137787; int converge_137790; int pconverge_137790; int converge_137794; int pconverge_137794; int converge_137797; int pconverge_137797; int converge_137801; int pconverge_137801; float _137807; float p_137807; int converge_137810; int pconverge_137810; int converge_137814; int pconverge_137814; int converge_137817; int pconverge_137817; int converge_137821; int pconverge_137821; float _137827; float p_137827; float s_137833; float ps_137833; int converge_137836; int pconverge_137836; int converge_137840; int pconverge_137840; int converge_137843; int pconverge_137843; int converge_137847; int pconverge_137847; float _137853; float p_137853; int converge_137856; int pconverge_137856; int converge_137860; int pconverge_137860; int converge_137865; int pconverge_137865; int converge_137869; int pconverge_137869; float _137875; float p_137875; int converge_137878; int pconverge_137878; int converge_137882; int pconverge_137882; int converge_137885; int pconverge_137885; int converge_137889; int pconverge_137889; float _137895; float p_137895; float s_137901; float ps_137901; int converge_137904; int pconverge_137904; int converge_137908; int pconverge_137908; int converge_137911; int pconverge_137911; int converge_137915; int pconverge_137915; float _137921; float p_137921; int converge_137924; int pconverge_137924; int converge_137928; int pconverge_137928; int converge_137931; int pconverge_137931; int converge_137935; int pconverge_137935; float _137941; float p_137941; int converge_137944; int pconverge_137944; int converge_137948; int pconverge_137948; int converge_137951; int pconverge_137951; int converge_137955; int pconverge_137955; float _137961; float p_137961; float s_137967; float ps_137967; int converge_137970; int pconverge_137970; int converge_137974; int pconverge_137974; int converge_137977; int pconverge_137977; int converge_137981; int pconverge_137981; float _137987; float p_137987; int converge_137990; int pconverge_137990; int converge_137994; int pconverge_137994; int converge_137997; int pconverge_137997; int converge_138001; int pconverge_138001; float _138007; float p_138007; int converge_138010; int pconverge_138010; int converge_138014; int pconverge_138014; int converge_138017; int pconverge_138017; int converge_138021; int pconverge_138021; float _138027; float p_138027; float s_138033; float ps_138033; int converge_138036; int pconverge_138036; int converge_138040; int pconverge_138040; int converge_138043; int pconverge_138043; int converge_138047; int pconverge_138047; float _138053; float p_138053; int converge_138056; int pconverge_138056; int converge_138060; int pconverge_138060; int converge_138063; int pconverge_138063; int converge_138067; int pconverge_138067; float _138073; float p_138073; int converge_138076; int pconverge_138076; int converge_138080; int pconverge_138080; int converge_138083; int pconverge_138083; int converge_138087; int pconverge_138087; float _138093; float p_138093; float s_138099; float ps_138099; int converge_138102; int pconverge_138102; int converge_138106; int pconverge_138106; int converge_138109; int pconverge_138109; int converge_138113; int pconverge_138113; float _138119; float p_138119; int converge_138122; int pconverge_138122; int converge_138126; int pconverge_138126; int converge_138129; int pconverge_138129; int converge_138133; int pconverge_138133; float _138139; float p_138139; int converge_138142; int pconverge_138142; int converge_138146; int pconverge_138146; int converge_138149; int pconverge_138149; int converge_138153; int pconverge_138153; float _138159; float p_138159; float s_138165; float ps_138165; int converge_138168; int pconverge_138168; int converge_138172; int pconverge_138172; int converge_138175; int pconverge_138175; int converge_138179; int pconverge_138179; float _138185; float p_138185; int converge_138188; int pconverge_138188; int converge_138192; int pconverge_138192; int converge_138197; int pconverge_138197; int converge_138201; int pconverge_138201; float _138207; float p_138207; int converge_138210; int pconverge_138210; int converge_138214; int pconverge_138214; int converge_138217; int pconverge_138217; int converge_138221; int pconverge_138221; float _138227; float p_138227; float s_138233; float ps_138233; int converge_138236; int pconverge_138236; int converge_138240; int pconverge_138240; int converge_138243; int pconverge_138243; int converge_138247; int pconverge_138247; float _138253; float p_138253; int converge_138256; int pconverge_138256; int converge_138260; int pconverge_138260; int converge_138263; int pconverge_138263; int converge_138267; int pconverge_138267; float _138273; float p_138273; int converge_138276; int pconverge_138276; int converge_138280; int pconverge_138280; int converge_138283; int pconverge_138283; int converge_138287; int pconverge_138287; float _138293; float p_138293; float s_138299; float ps_138299; int converge_138302; int pconverge_138302; int converge_138306; int pconverge_138306; int converge_138309; int pconverge_138309; int converge_138313; int pconverge_138313; float _138319; float p_138319; int converge_138322; int pconverge_138322; int converge_138326; int pconverge_138326; int converge_138329; int pconverge_138329; int converge_138333; int pconverge_138333; float _138339; float p_138339; int converge_138342; int pconverge_138342; int converge_138346; int pconverge_138346; int converge_138349; int pconverge_138349; int converge_138353; int pconverge_138353; float _138359; float p_138359; float s_138365; float ps_138365; int converge_138368; int pconverge_138368; int converge_138372; int pconverge_138372; int converge_138375; int pconverge_138375; int converge_138379; int pconverge_138379; float _138385; float p_138385; int converge_138388; int pconverge_138388; int converge_138392; int pconverge_138392; int converge_138395; int pconverge_138395; int converge_138399; int pconverge_138399; float _138405; float p_138405; int converge_138408; int pconverge_138408; int converge_138412; int pconverge_138412; int converge_138415; int pconverge_138415; int converge_138419; int pconverge_138419; float _138425; float p_138425; float s_138431; float ps_138431; int converge_138434; int pconverge_138434; int converge_138438; int pconverge_138438; int converge_138441; int pconverge_138441; int converge_138445; int pconverge_138445; float _138451; float p_138451; int converge_138454; int pconverge_138454; int converge_138458; int pconverge_138458; int converge_138461; int pconverge_138461; int converge_138465; int pconverge_138465; float _138471; float p_138471; int converge_138474; int pconverge_138474; int converge_138478; int pconverge_138478; int converge_138481; int pconverge_138481; int converge_138485; int pconverge_138485; float _138491; float p_138491; float s_138497; float ps_138497; int converge_138500; int pconverge_138500; int converge_138504; int pconverge_138504; int converge_138507; int pconverge_138507; int converge_138511; int pconverge_138511; float _138517; float p_138517; threadIdx_x_136797 = threadIdx_x(); pthreadIdx_x_136797 = threadIdx_x_136797; l136795: ; threadIdx_x_136797 = pthreadIdx_x_136797; blockDim_x_136803 = blockDim_x(); pblockDim_x_136803 = blockDim_x_136803; l136801: ; blockDim_x_136803 = pblockDim_x_136803; blockIdx_x_136809 = blockIdx_x(); pblockIdx_x_136809 = blockIdx_x_136809; l136807: ; blockIdx_x_136809 = pblockIdx_x_136809; _136815 = threadIdx_y(); p_136815 = _136815; l136813: ; _136815 = p_136815; _136821 = blockDim_y(); p_136821 = _136821; l136819: ; _136821 = p_136821; _136827 = blockIdx_y(); p_136827 = _136827; l136825: ; _136827 = p_136827; _136830 = blockDim_y(); p_136830 = _136830; l136828: ; _136830 = p_136830; int _136832; _136832 = blockDim_x_136803 * blockIdx_x_136809; int _136833; _136833 = threadIdx_x_136797 + _136832; int _136834; _136834 = -2 + _136833; bool _136836; _136836 = _136834 < 0; if (_136836) goto l136837; else goto l138930; l138930: ; pconverge_136839 = _136834; goto l136838; l136837: ; pconverge_136839 = 0; goto l136838; l136838: ; converge_136839 = pconverge_136839; bool _136841; _136841 = 1024 <= converge_136839; if (_136841) goto l136842; else goto l138929; l138929: ; pconverge_136844 = converge_136839; goto l136843; l136842: ; pconverge_136844 = 1023; goto l136843; l136843: ; converge_136844 = pconverge_136844; int _136845; _136845 = _136821 * _136827; int gid_y_136846; gid_y_136846 = _136815 + _136845; int _136847; _136847 = -2 + gid_y_136846; bool _136848; _136848 = _136847 < 0; if (_136848) goto l136849; else goto l138928; l138928: ; pconverge_136851 = _136847; goto l136850; l136849: ; pconverge_136851 = 0; goto l136850; l136850: ; converge_136851 = pconverge_136851; bool _136852; _136852 = 1024 <= converge_136851; if (_136852) goto l136853; else goto l138927; l138927: ; pconverge_136855 = converge_136851; goto l136854; l136853: ; pconverge_136855 = 1023; goto l136854; l136854: ; converge_136855 = pconverge_136855; int _136860; _136860 = 1024 * converge_136855; int _136861; _136861 = _136860 + converge_136844; float* idx_136862; idx_136862 = _111058_136790 + _136861; _136865 = __ldg(idx_136862); p_136865 = _136865; l136863: ; _136865 = p_136865; bool _136866; _136866 = _136833 < 0; if (_136866) goto l136867; else goto l138926; l138926: ; pconverge_136869 = _136833; goto l136868; l136867: ; pconverge_136869 = 0; goto l136868; l136868: ; converge_136869 = pconverge_136869; bool _136870; _136870 = 1024 <= converge_136869; if (_136870) goto l136871; else goto l138925; l138925: ; pconverge_136873 = converge_136869; goto l136872; l136871: ; pconverge_136873 = 1023; goto l136872; l136872: ; converge_136873 = pconverge_136873; bool _136874; _136874 = gid_y_136846 < 0; if (_136874) goto l136875; else goto l138924; l138924: ; pconverge_136877 = gid_y_136846; goto l136876; l136875: ; pconverge_136877 = 0; goto l136876; l136876: ; converge_136877 = pconverge_136877; bool _136878; _136878 = 1024 <= converge_136877; if (_136878) goto l136879; else goto l138923; l138923: ; pconverge_136881 = converge_136877; goto l136880; l136879: ; pconverge_136881 = 1023; goto l136880; l136880: ; converge_136881 = pconverge_136881; int _136882; _136882 = 1024 * converge_136881; int _136883; _136883 = _136882 + converge_136873; float* idx_136884; idx_136884 = _111058_136790 + _136883; _136887 = __ldg(idx_136884); p_136887 = _136887; l136885: ; _136887 = p_136887; float diff_136893; diff_136893 = _136865 - _136887; float _136894; _136894 = -2.000000e-02f * diff_136893; float _136895; _136895 = _136894 * diff_136893; s_136898 = expf(_136895); ps_136898 = s_136898; l136896: ; s_136898 = ps_136898; if (_136836) goto l136899; else goto l138922; l138922: ; pconverge_136901 = _136834; goto l136900; l136899: ; pconverge_136901 = 0; goto l136900; l136900: ; converge_136901 = pconverge_136901; bool _136902; _136902 = 1024 <= converge_136901; if (_136902) goto l136903; else goto l138921; l138921: ; pconverge_136905 = converge_136901; goto l136904; l136903: ; pconverge_136905 = 1023; goto l136904; l136904: ; converge_136905 = pconverge_136905; if (_136848) goto l136906; else goto l138920; l138920: ; pconverge_136908 = _136847; goto l136907; l136906: ; pconverge_136908 = 0; goto l136907; l136907: ; converge_136908 = pconverge_136908; bool _136909; _136909 = 1024 <= converge_136908; if (_136909) goto l136910; else goto l138919; l138919: ; pconverge_136912 = converge_136908; goto l136911; l136910: ; pconverge_136912 = 1023; goto l136911; l136911: ; converge_136912 = pconverge_136912; int _136913; _136913 = 1024 * converge_136912; int _136914; _136914 = _136913 + converge_136905; float* idx_136915; idx_136915 = _111058_136790 + _136914; _136918 = __ldg(idx_136915); p_136918 = _136918; l136916: ; _136918 = p_136918; int _136920; _136920 = -1 + _136833; bool _136921; _136921 = _136920 < 0; if (_136921) goto l136922; else goto l138918; l138918: ; pconverge_136924 = _136920; goto l136923; l136922: ; pconverge_136924 = 0; goto l136923; l136923: ; converge_136924 = pconverge_136924; bool _136925; _136925 = 1024 <= converge_136924; if (_136925) goto l136926; else goto l138917; l138917: ; pconverge_136928 = converge_136924; goto l136927; l136926: ; pconverge_136928 = 1023; goto l136927; l136927: ; converge_136928 = pconverge_136928; if (_136848) goto l136929; else goto l138916; l138916: ; pconverge_136931 = _136847; goto l136930; l136929: ; pconverge_136931 = 0; goto l136930; l136930: ; converge_136931 = pconverge_136931; bool _136932; _136932 = 1024 <= converge_136931; if (_136932) goto l136933; else goto l138915; l138915: ; pconverge_136935 = converge_136931; goto l136934; l136933: ; pconverge_136935 = 1023; goto l136934; l136934: ; converge_136935 = pconverge_136935; int _136936; _136936 = 1024 * converge_136935; int _136937; _136937 = _136936 + converge_136928; float* idx_136938; idx_136938 = _111058_136790 + _136937; _136941 = __ldg(idx_136938); p_136941 = _136941; l136939: ; _136941 = p_136941; if (_136866) goto l136942; else goto l138914; l138914: ; pconverge_136944 = _136833; goto l136943; l136942: ; pconverge_136944 = 0; goto l136943; l136943: ; converge_136944 = pconverge_136944; bool _136945; _136945 = 1024 <= converge_136944; if (_136945) goto l136946; else goto l138913; l138913: ; pconverge_136948 = converge_136944; goto l136947; l136946: ; pconverge_136948 = 1023; goto l136947; l136947: ; converge_136948 = pconverge_136948; if (_136874) goto l136949; else goto l138912; l138912: ; pconverge_136951 = gid_y_136846; goto l136950; l136949: ; pconverge_136951 = 0; goto l136950; l136950: ; converge_136951 = pconverge_136951; bool _136952; _136952 = 1024 <= converge_136951; if (_136952) goto l136953; else goto l138911; l138911: ; pconverge_136955 = converge_136951; goto l136954; l136953: ; pconverge_136955 = 1023; goto l136954; l136954: ; converge_136955 = pconverge_136955; int _136956; _136956 = 1024 * converge_136955; int _136957; _136957 = _136956 + converge_136948; float* idx_136958; idx_136958 = _111058_136790 + _136957; _136961 = __ldg(idx_136958); p_136961 = _136961; l136959: ; _136961 = p_136961; float diff_136962; diff_136962 = _136941 - _136961; float _136963; _136963 = -2.000000e-02f * diff_136962; float _136964; _136964 = _136963 * diff_136962; s_136967 = expf(_136964); ps_136967 = s_136967; l136965: ; s_136967 = ps_136967; if (_136921) goto l136968; else goto l138910; l138910: ; pconverge_136970 = _136920; goto l136969; l136968: ; pconverge_136970 = 0; goto l136969; l136969: ; converge_136970 = pconverge_136970; bool _136971; _136971 = 1024 <= converge_136970; if (_136971) goto l136972; else goto l138909; l138909: ; pconverge_136974 = converge_136970; goto l136973; l136972: ; pconverge_136974 = 1023; goto l136973; l136973: ; converge_136974 = pconverge_136974; if (_136848) goto l136975; else goto l138908; l138908: ; pconverge_136977 = _136847; goto l136976; l136975: ; pconverge_136977 = 0; goto l136976; l136976: ; converge_136977 = pconverge_136977; bool _136978; _136978 = 1024 <= converge_136977; if (_136978) goto l136979; else goto l138907; l138907: ; pconverge_136981 = converge_136977; goto l136980; l136979: ; pconverge_136981 = 1023; goto l136980; l136980: ; converge_136981 = pconverge_136981; int _136982; _136982 = 1024 * converge_136981; int _136983; _136983 = _136982 + converge_136974; float* idx_136984; idx_136984 = _111058_136790 + _136983; _136987 = __ldg(idx_136984); p_136987 = _136987; l136985: ; _136987 = p_136987; if (_136866) goto l136988; else goto l138906; l138906: ; pconverge_136990 = _136833; goto l136989; l136988: ; pconverge_136990 = 0; goto l136989; l136989: ; converge_136990 = pconverge_136990; bool _136991; _136991 = 1024 <= converge_136990; if (_136991) goto l136992; else goto l138905; l138905: ; pconverge_136994 = converge_136990; goto l136993; l136992: ; pconverge_136994 = 1023; goto l136993; l136993: ; converge_136994 = pconverge_136994; if (_136848) goto l136995; else goto l138904; l138904: ; pconverge_136997 = _136847; goto l136996; l136995: ; pconverge_136997 = 0; goto l136996; l136996: ; converge_136997 = pconverge_136997; bool _136998; _136998 = 1024 <= converge_136997; if (_136998) goto l136999; else goto l138903; l138903: ; pconverge_137001 = converge_136997; goto l137000; l136999: ; pconverge_137001 = 1023; goto l137000; l137000: ; converge_137001 = pconverge_137001; int _137002; _137002 = 1024 * converge_137001; int _137003; _137003 = _137002 + converge_136994; float* idx_137004; idx_137004 = _111058_136790 + _137003; _137007 = __ldg(idx_137004); p_137007 = _137007; l137005: ; _137007 = p_137007; if (_136866) goto l137008; else goto l138902; l138902: ; pconverge_137010 = _136833; goto l137009; l137008: ; pconverge_137010 = 0; goto l137009; l137009: ; converge_137010 = pconverge_137010; bool _137011; _137011 = 1024 <= converge_137010; if (_137011) goto l137012; else goto l138901; l138901: ; pconverge_137014 = converge_137010; goto l137013; l137012: ; pconverge_137014 = 1023; goto l137013; l137013: ; converge_137014 = pconverge_137014; if (_136874) goto l137015; else goto l138900; l138900: ; pconverge_137017 = gid_y_136846; goto l137016; l137015: ; pconverge_137017 = 0; goto l137016; l137016: ; converge_137017 = pconverge_137017; bool _137018; _137018 = 1024 <= converge_137017; if (_137018) goto l137019; else goto l138899; l138899: ; pconverge_137021 = converge_137017; goto l137020; l137019: ; pconverge_137021 = 1023; goto l137020; l137020: ; converge_137021 = pconverge_137021; int _137022; _137022 = 1024 * converge_137021; int _137023; _137023 = _137022 + converge_137014; float* idx_137024; idx_137024 = _111058_136790 + _137023; _137027 = __ldg(idx_137024); p_137027 = _137027; l137025: ; _137027 = p_137027; float diff_137028; diff_137028 = _137007 - _137027; float _137029; _137029 = -2.000000e-02f * diff_137028; float _137030; _137030 = _137029 * diff_137028; s_137033 = expf(_137030); ps_137033 = s_137033; l137031: ; s_137033 = ps_137033; if (_136866) goto l137034; else goto l138898; l138898: ; pconverge_137036 = _136833; goto l137035; l137034: ; pconverge_137036 = 0; goto l137035; l137035: ; converge_137036 = pconverge_137036; bool _137037; _137037 = 1024 <= converge_137036; if (_137037) goto l137038; else goto l138897; l138897: ; pconverge_137040 = converge_137036; goto l137039; l137038: ; pconverge_137040 = 1023; goto l137039; l137039: ; converge_137040 = pconverge_137040; if (_136848) goto l137041; else goto l138896; l138896: ; pconverge_137043 = _136847; goto l137042; l137041: ; pconverge_137043 = 0; goto l137042; l137042: ; converge_137043 = pconverge_137043; bool _137044; _137044 = 1024 <= converge_137043; if (_137044) goto l137045; else goto l138895; l138895: ; pconverge_137047 = converge_137043; goto l137046; l137045: ; pconverge_137047 = 1023; goto l137046; l137046: ; converge_137047 = pconverge_137047; int _137048; _137048 = 1024 * converge_137047; int _137049; _137049 = _137048 + converge_137040; float* idx_137050; idx_137050 = _111058_136790 + _137049; _137053 = __ldg(idx_137050); p_137053 = _137053; l137051: ; _137053 = p_137053; int _137055; _137055 = 1 + _136833; bool _137056; _137056 = _137055 < 0; if (_137056) goto l137057; else goto l138894; l138894: ; pconverge_137059 = _137055; goto l137058; l137057: ; pconverge_137059 = 0; goto l137058; l137058: ; converge_137059 = pconverge_137059; bool _137060; _137060 = 1024 <= converge_137059; if (_137060) goto l137061; else goto l138893; l138893: ; pconverge_137063 = converge_137059; goto l137062; l137061: ; pconverge_137063 = 1023; goto l137062; l137062: ; converge_137063 = pconverge_137063; if (_136848) goto l137064; else goto l138892; l138892: ; pconverge_137066 = _136847; goto l137065; l137064: ; pconverge_137066 = 0; goto l137065; l137065: ; converge_137066 = pconverge_137066; bool _137067; _137067 = 1024 <= converge_137066; if (_137067) goto l137068; else goto l138891; l138891: ; pconverge_137070 = converge_137066; goto l137069; l137068: ; pconverge_137070 = 1023; goto l137069; l137069: ; converge_137070 = pconverge_137070; int _137071; _137071 = 1024 * converge_137070; int _137072; _137072 = _137071 + converge_137063; float* idx_137073; idx_137073 = _111058_136790 + _137072; _137076 = __ldg(idx_137073); p_137076 = _137076; l137074: ; _137076 = p_137076; if (_136866) goto l137077; else goto l138890; l138890: ; pconverge_137079 = _136833; goto l137078; l137077: ; pconverge_137079 = 0; goto l137078; l137078: ; converge_137079 = pconverge_137079; bool _137080; _137080 = 1024 <= converge_137079; if (_137080) goto l137081; else goto l138889; l138889: ; pconverge_137083 = converge_137079; goto l137082; l137081: ; pconverge_137083 = 1023; goto l137082; l137082: ; converge_137083 = pconverge_137083; if (_136874) goto l137084; else goto l138888; l138888: ; pconverge_137086 = gid_y_136846; goto l137085; l137084: ; pconverge_137086 = 0; goto l137085; l137085: ; converge_137086 = pconverge_137086; bool _137087; _137087 = 1024 <= converge_137086; if (_137087) goto l137088; else goto l138887; l138887: ; pconverge_137090 = converge_137086; goto l137089; l137088: ; pconverge_137090 = 1023; goto l137089; l137089: ; converge_137090 = pconverge_137090; int _137091; _137091 = 1024 * converge_137090; int _137092; _137092 = _137091 + converge_137083; float* idx_137093; idx_137093 = _111058_136790 + _137092; _137096 = __ldg(idx_137093); p_137096 = _137096; l137094: ; _137096 = p_137096; float diff_137097; diff_137097 = _137076 - _137096; float _137098; _137098 = -2.000000e-02f * diff_137097; float _137099; _137099 = _137098 * diff_137097; s_137102 = expf(_137099); ps_137102 = s_137102; l137100: ; s_137102 = ps_137102; if (_137056) goto l137103; else goto l138886; l138886: ; pconverge_137105 = _137055; goto l137104; l137103: ; pconverge_137105 = 0; goto l137104; l137104: ; converge_137105 = pconverge_137105; bool _137106; _137106 = 1024 <= converge_137105; if (_137106) goto l137107; else goto l138885; l138885: ; pconverge_137109 = converge_137105; goto l137108; l137107: ; pconverge_137109 = 1023; goto l137108; l137108: ; converge_137109 = pconverge_137109; if (_136848) goto l137110; else goto l138884; l138884: ; pconverge_137112 = _136847; goto l137111; l137110: ; pconverge_137112 = 0; goto l137111; l137111: ; converge_137112 = pconverge_137112; bool _137113; _137113 = 1024 <= converge_137112; if (_137113) goto l137114; else goto l138883; l138883: ; pconverge_137116 = converge_137112; goto l137115; l137114: ; pconverge_137116 = 1023; goto l137115; l137115: ; converge_137116 = pconverge_137116; int _137117; _137117 = 1024 * converge_137116; int _137118; _137118 = _137117 + converge_137109; float* idx_137119; idx_137119 = _111058_136790 + _137118; _137122 = __ldg(idx_137119); p_137122 = _137122; l137120: ; _137122 = p_137122; int _137124; _137124 = 2 + _136833; bool _137125; _137125 = _137124 < 0; if (_137125) goto l137126; else goto l138882; l138882: ; pconverge_137128 = _137124; goto l137127; l137126: ; pconverge_137128 = 0; goto l137127; l137127: ; converge_137128 = pconverge_137128; bool _137129; _137129 = 1024 <= converge_137128; if (_137129) goto l137130; else goto l138881; l138881: ; pconverge_137132 = converge_137128; goto l137131; l137130: ; pconverge_137132 = 1023; goto l137131; l137131: ; converge_137132 = pconverge_137132; if (_136848) goto l137133; else goto l138880; l138880: ; pconverge_137135 = _136847; goto l137134; l137133: ; pconverge_137135 = 0; goto l137134; l137134: ; converge_137135 = pconverge_137135; bool _137136; _137136 = 1024 <= converge_137135; if (_137136) goto l137137; else goto l138879; l138879: ; pconverge_137139 = converge_137135; goto l137138; l137137: ; pconverge_137139 = 1023; goto l137138; l137138: ; converge_137139 = pconverge_137139; int _137140; _137140 = 1024 * converge_137139; int _137141; _137141 = _137140 + converge_137132; float* idx_137142; idx_137142 = _111058_136790 + _137141; _137145 = __ldg(idx_137142); p_137145 = _137145; l137143: ; _137145 = p_137145; if (_136866) goto l137146; else goto l138878; l138878: ; pconverge_137148 = _136833; goto l137147; l137146: ; pconverge_137148 = 0; goto l137147; l137147: ; converge_137148 = pconverge_137148; bool _137149; _137149 = 1024 <= converge_137148; if (_137149) goto l137150; else goto l138877; l138877: ; pconverge_137152 = converge_137148; goto l137151; l137150: ; pconverge_137152 = 1023; goto l137151; l137151: ; converge_137152 = pconverge_137152; if (_136874) goto l137153; else goto l138876; l138876: ; pconverge_137155 = gid_y_136846; goto l137154; l137153: ; pconverge_137155 = 0; goto l137154; l137154: ; converge_137155 = pconverge_137155; bool _137156; _137156 = 1024 <= converge_137155; if (_137156) goto l137157; else goto l138875; l138875: ; pconverge_137159 = converge_137155; goto l137158; l137157: ; pconverge_137159 = 1023; goto l137158; l137158: ; converge_137159 = pconverge_137159; int _137160; _137160 = 1024 * converge_137159; int _137161; _137161 = _137160 + converge_137152; float* idx_137162; idx_137162 = _111058_136790 + _137161; _137165 = __ldg(idx_137162); p_137165 = _137165; l137163: ; _137165 = p_137165; float diff_137166; diff_137166 = _137145 - _137165; float _137167; _137167 = -2.000000e-02f * diff_137166; float _137168; _137168 = _137167 * diff_137166; s_137171 = expf(_137168); ps_137171 = s_137171; l137169: ; s_137171 = ps_137171; if (_137125) goto l137172; else goto l138874; l138874: ; pconverge_137174 = _137124; goto l137173; l137172: ; pconverge_137174 = 0; goto l137173; l137173: ; converge_137174 = pconverge_137174; bool _137175; _137175 = 1024 <= converge_137174; if (_137175) goto l137176; else goto l138873; l138873: ; pconverge_137178 = converge_137174; goto l137177; l137176: ; pconverge_137178 = 1023; goto l137177; l137177: ; converge_137178 = pconverge_137178; if (_136848) goto l137179; else goto l138872; l138872: ; pconverge_137181 = _136847; goto l137180; l137179: ; pconverge_137181 = 0; goto l137180; l137180: ; converge_137181 = pconverge_137181; bool _137182; _137182 = 1024 <= converge_137181; if (_137182) goto l137183; else goto l138871; l138871: ; pconverge_137185 = converge_137181; goto l137184; l137183: ; pconverge_137185 = 1023; goto l137184; l137184: ; converge_137185 = pconverge_137185; int _137186; _137186 = 1024 * converge_137185; int _137187; _137187 = _137186 + converge_137178; float* idx_137188; idx_137188 = _111058_136790 + _137187; _137191 = __ldg(idx_137188); p_137191 = _137191; l137189: ; _137191 = p_137191; if (_136836) goto l137192; else goto l138870; l138870: ; pconverge_137194 = _136834; goto l137193; l137192: ; pconverge_137194 = 0; goto l137193; l137193: ; converge_137194 = pconverge_137194; bool _137195; _137195 = 1024 <= converge_137194; if (_137195) goto l137196; else goto l138869; l138869: ; pconverge_137198 = converge_137194; goto l137197; l137196: ; pconverge_137198 = 1023; goto l137197; l137197: ; converge_137198 = pconverge_137198; int _137199; _137199 = -1 + gid_y_136846; bool _137200; _137200 = _137199 < 0; if (_137200) goto l137201; else goto l138868; l138868: ; pconverge_137203 = _137199; goto l137202; l137201: ; pconverge_137203 = 0; goto l137202; l137202: ; converge_137203 = pconverge_137203; bool _137204; _137204 = 1024 <= converge_137203; if (_137204) goto l137205; else goto l138867; l138867: ; pconverge_137207 = converge_137203; goto l137206; l137205: ; pconverge_137207 = 1023; goto l137206; l137206: ; converge_137207 = pconverge_137207; int _137208; _137208 = 1024 * converge_137207; int _137209; _137209 = _137208 + converge_137198; float* idx_137210; idx_137210 = _111058_136790 + _137209; _137213 = __ldg(idx_137210); p_137213 = _137213; l137211: ; _137213 = p_137213; if (_136866) goto l137214; else goto l138866; l138866: ; pconverge_137216 = _136833; goto l137215; l137214: ; pconverge_137216 = 0; goto l137215; l137215: ; converge_137216 = pconverge_137216; bool _137217; _137217 = 1024 <= converge_137216; if (_137217) goto l137218; else goto l138865; l138865: ; pconverge_137220 = converge_137216; goto l137219; l137218: ; pconverge_137220 = 1023; goto l137219; l137219: ; converge_137220 = pconverge_137220; if (_136874) goto l137221; else goto l138864; l138864: ; pconverge_137223 = gid_y_136846; goto l137222; l137221: ; pconverge_137223 = 0; goto l137222; l137222: ; converge_137223 = pconverge_137223; bool _137224; _137224 = 1024 <= converge_137223; if (_137224) goto l137225; else goto l138863; l138863: ; pconverge_137227 = converge_137223; goto l137226; l137225: ; pconverge_137227 = 1023; goto l137226; l137226: ; converge_137227 = pconverge_137227; int _137228; _137228 = 1024 * converge_137227; int _137229; _137229 = _137228 + converge_137220; float* idx_137230; idx_137230 = _111058_136790 + _137229; _137233 = __ldg(idx_137230); p_137233 = _137233; l137231: ; _137233 = p_137233; float diff_137234; diff_137234 = _137213 - _137233; float _137235; _137235 = -2.000000e-02f * diff_137234; float _137236; _137236 = _137235 * diff_137234; s_137239 = expf(_137236); ps_137239 = s_137239; l137237: ; s_137239 = ps_137239; if (_136836) goto l137240; else goto l138862; l138862: ; pconverge_137242 = _136834; goto l137241; l137240: ; pconverge_137242 = 0; goto l137241; l137241: ; converge_137242 = pconverge_137242; bool _137243; _137243 = 1024 <= converge_137242; if (_137243) goto l137244; else goto l138861; l138861: ; pconverge_137246 = converge_137242; goto l137245; l137244: ; pconverge_137246 = 1023; goto l137245; l137245: ; converge_137246 = pconverge_137246; if (_137200) goto l137247; else goto l138860; l138860: ; pconverge_137249 = _137199; goto l137248; l137247: ; pconverge_137249 = 0; goto l137248; l137248: ; converge_137249 = pconverge_137249; bool _137250; _137250 = 1024 <= converge_137249; if (_137250) goto l137251; else goto l138859; l138859: ; pconverge_137253 = converge_137249; goto l137252; l137251: ; pconverge_137253 = 1023; goto l137252; l137252: ; converge_137253 = pconverge_137253; int _137254; _137254 = 1024 * converge_137253; int _137255; _137255 = _137254 + converge_137246; float* idx_137256; idx_137256 = _111058_136790 + _137255; _137259 = __ldg(idx_137256); p_137259 = _137259; l137257: ; _137259 = p_137259; if (_136921) goto l137260; else goto l138858; l138858: ; pconverge_137262 = _136920; goto l137261; l137260: ; pconverge_137262 = 0; goto l137261; l137261: ; converge_137262 = pconverge_137262; bool _137263; _137263 = 1024 <= converge_137262; if (_137263) goto l137264; else goto l138857; l138857: ; pconverge_137266 = converge_137262; goto l137265; l137264: ; pconverge_137266 = 1023; goto l137265; l137265: ; converge_137266 = pconverge_137266; if (_137200) goto l137267; else goto l138856; l138856: ; pconverge_137269 = _137199; goto l137268; l137267: ; pconverge_137269 = 0; goto l137268; l137268: ; converge_137269 = pconverge_137269; bool _137270; _137270 = 1024 <= converge_137269; if (_137270) goto l137271; else goto l138855; l138855: ; pconverge_137273 = converge_137269; goto l137272; l137271: ; pconverge_137273 = 1023; goto l137272; l137272: ; converge_137273 = pconverge_137273; int _137274; _137274 = 1024 * converge_137273; int _137275; _137275 = _137274 + converge_137266; float* idx_137276; idx_137276 = _111058_136790 + _137275; _137279 = __ldg(idx_137276); p_137279 = _137279; l137277: ; _137279 = p_137279; if (_136866) goto l137280; else goto l138854; l138854: ; pconverge_137282 = _136833; goto l137281; l137280: ; pconverge_137282 = 0; goto l137281; l137281: ; converge_137282 = pconverge_137282; bool _137283; _137283 = 1024 <= converge_137282; if (_137283) goto l137284; else goto l138853; l138853: ; pconverge_137286 = converge_137282; goto l137285; l137284: ; pconverge_137286 = 1023; goto l137285; l137285: ; converge_137286 = pconverge_137286; if (_136874) goto l137287; else goto l138852; l138852: ; pconverge_137289 = gid_y_136846; goto l137288; l137287: ; pconverge_137289 = 0; goto l137288; l137288: ; converge_137289 = pconverge_137289; bool _137290; _137290 = 1024 <= converge_137289; if (_137290) goto l137291; else goto l138851; l138851: ; pconverge_137293 = converge_137289; goto l137292; l137291: ; pconverge_137293 = 1023; goto l137292; l137292: ; converge_137293 = pconverge_137293; int _137294; _137294 = 1024 * converge_137293; int _137295; _137295 = _137294 + converge_137286; float* idx_137296; idx_137296 = _111058_136790 + _137295; _137299 = __ldg(idx_137296); p_137299 = _137299; l137297: ; _137299 = p_137299; float diff_137300; diff_137300 = _137279 - _137299; float _137301; _137301 = -2.000000e-02f * diff_137300; float _137302; _137302 = _137301 * diff_137300; s_137305 = expf(_137302); ps_137305 = s_137305; l137303: ; s_137305 = ps_137305; if (_136921) goto l137306; else goto l138850; l138850: ; pconverge_137308 = _136920; goto l137307; l137306: ; pconverge_137308 = 0; goto l137307; l137307: ; converge_137308 = pconverge_137308; bool _137309; _137309 = 1024 <= converge_137308; if (_137309) goto l137310; else goto l138849; l138849: ; pconverge_137312 = converge_137308; goto l137311; l137310: ; pconverge_137312 = 1023; goto l137311; l137311: ; converge_137312 = pconverge_137312; if (_137200) goto l137313; else goto l138848; l138848: ; pconverge_137315 = _137199; goto l137314; l137313: ; pconverge_137315 = 0; goto l137314; l137314: ; converge_137315 = pconverge_137315; bool _137316; _137316 = 1024 <= converge_137315; if (_137316) goto l137317; else goto l138847; l138847: ; pconverge_137319 = converge_137315; goto l137318; l137317: ; pconverge_137319 = 1023; goto l137318; l137318: ; converge_137319 = pconverge_137319; int _137320; _137320 = 1024 * converge_137319; int _137321; _137321 = _137320 + converge_137312; float* idx_137322; idx_137322 = _111058_136790 + _137321; _137325 = __ldg(idx_137322); p_137325 = _137325; l137323: ; _137325 = p_137325; if (_136866) goto l137326; else goto l138846; l138846: ; pconverge_137328 = _136833; goto l137327; l137326: ; pconverge_137328 = 0; goto l137327; l137327: ; converge_137328 = pconverge_137328; bool _137329; _137329 = 1024 <= converge_137328; if (_137329) goto l137330; else goto l138845; l138845: ; pconverge_137332 = converge_137328; goto l137331; l137330: ; pconverge_137332 = 1023; goto l137331; l137331: ; converge_137332 = pconverge_137332; if (_137200) goto l137333; else goto l138844; l138844: ; pconverge_137335 = _137199; goto l137334; l137333: ; pconverge_137335 = 0; goto l137334; l137334: ; converge_137335 = pconverge_137335; bool _137336; _137336 = 1024 <= converge_137335; if (_137336) goto l137337; else goto l138843; l138843: ; pconverge_137339 = converge_137335; goto l137338; l137337: ; pconverge_137339 = 1023; goto l137338; l137338: ; converge_137339 = pconverge_137339; int _137340; _137340 = 1024 * converge_137339; int _137341; _137341 = _137340 + converge_137332; float* idx_137342; idx_137342 = _111058_136790 + _137341; _137345 = __ldg(idx_137342); p_137345 = _137345; l137343: ; _137345 = p_137345; if (_136866) goto l137346; else goto l138842; l138842: ; pconverge_137348 = _136833; goto l137347; l137346: ; pconverge_137348 = 0; goto l137347; l137347: ; converge_137348 = pconverge_137348; bool _137349; _137349 = 1024 <= converge_137348; if (_137349) goto l137350; else goto l138841; l138841: ; pconverge_137352 = converge_137348; goto l137351; l137350: ; pconverge_137352 = 1023; goto l137351; l137351: ; converge_137352 = pconverge_137352; if (_136874) goto l137353; else goto l138840; l138840: ; pconverge_137355 = gid_y_136846; goto l137354; l137353: ; pconverge_137355 = 0; goto l137354; l137354: ; converge_137355 = pconverge_137355; bool _137356; _137356 = 1024 <= converge_137355; if (_137356) goto l137357; else goto l138839; l138839: ; pconverge_137359 = converge_137355; goto l137358; l137357: ; pconverge_137359 = 1023; goto l137358; l137358: ; converge_137359 = pconverge_137359; int _137360; _137360 = 1024 * converge_137359; int _137361; _137361 = _137360 + converge_137352; float* idx_137362; idx_137362 = _111058_136790 + _137361; _137365 = __ldg(idx_137362); p_137365 = _137365; l137363: ; _137365 = p_137365; float diff_137366; diff_137366 = _137345 - _137365; float _137367; _137367 = -2.000000e-02f * diff_137366; float _137368; _137368 = _137367 * diff_137366; s_137371 = expf(_137368); ps_137371 = s_137371; l137369: ; s_137371 = ps_137371; if (_136866) goto l137372; else goto l138838; l138838: ; pconverge_137374 = _136833; goto l137373; l137372: ; pconverge_137374 = 0; goto l137373; l137373: ; converge_137374 = pconverge_137374; bool _137375; _137375 = 1024 <= converge_137374; if (_137375) goto l137376; else goto l138837; l138837: ; pconverge_137378 = converge_137374; goto l137377; l137376: ; pconverge_137378 = 1023; goto l137377; l137377: ; converge_137378 = pconverge_137378; if (_137200) goto l137379; else goto l138836; l138836: ; pconverge_137381 = _137199; goto l137380; l137379: ; pconverge_137381 = 0; goto l137380; l137380: ; converge_137381 = pconverge_137381; bool _137382; _137382 = 1024 <= converge_137381; if (_137382) goto l137383; else goto l138835; l138835: ; pconverge_137385 = converge_137381; goto l137384; l137383: ; pconverge_137385 = 1023; goto l137384; l137384: ; converge_137385 = pconverge_137385; int _137386; _137386 = 1024 * converge_137385; int _137387; _137387 = _137386 + converge_137378; float* idx_137388; idx_137388 = _111058_136790 + _137387; _137391 = __ldg(idx_137388); p_137391 = _137391; l137389: ; _137391 = p_137391; if (_137056) goto l137392; else goto l138834; l138834: ; pconverge_137394 = _137055; goto l137393; l137392: ; pconverge_137394 = 0; goto l137393; l137393: ; converge_137394 = pconverge_137394; bool _137395; _137395 = 1024 <= converge_137394; if (_137395) goto l137396; else goto l138833; l138833: ; pconverge_137398 = converge_137394; goto l137397; l137396: ; pconverge_137398 = 1023; goto l137397; l137397: ; converge_137398 = pconverge_137398; if (_137200) goto l137399; else goto l138832; l138832: ; pconverge_137401 = _137199; goto l137400; l137399: ; pconverge_137401 = 0; goto l137400; l137400: ; converge_137401 = pconverge_137401; bool _137402; _137402 = 1024 <= converge_137401; if (_137402) goto l137403; else goto l138831; l138831: ; pconverge_137405 = converge_137401; goto l137404; l137403: ; pconverge_137405 = 1023; goto l137404; l137404: ; converge_137405 = pconverge_137405; int _137406; _137406 = 1024 * converge_137405; int _137407; _137407 = _137406 + converge_137398; float* idx_137408; idx_137408 = _111058_136790 + _137407; _137411 = __ldg(idx_137408); p_137411 = _137411; l137409: ; _137411 = p_137411; if (_136866) goto l137412; else goto l138830; l138830: ; pconverge_137414 = _136833; goto l137413; l137412: ; pconverge_137414 = 0; goto l137413; l137413: ; converge_137414 = pconverge_137414; bool _137415; _137415 = 1024 <= converge_137414; if (_137415) goto l137416; else goto l138829; l138829: ; pconverge_137418 = converge_137414; goto l137417; l137416: ; pconverge_137418 = 1023; goto l137417; l137417: ; converge_137418 = pconverge_137418; if (_136874) goto l137419; else goto l138828; l138828: ; pconverge_137421 = gid_y_136846; goto l137420; l137419: ; pconverge_137421 = 0; goto l137420; l137420: ; converge_137421 = pconverge_137421; bool _137422; _137422 = 1024 <= converge_137421; if (_137422) goto l137423; else goto l138827; l138827: ; pconverge_137425 = converge_137421; goto l137424; l137423: ; pconverge_137425 = 1023; goto l137424; l137424: ; converge_137425 = pconverge_137425; int _137426; _137426 = 1024 * converge_137425; int _137427; _137427 = _137426 + converge_137418; float* idx_137428; idx_137428 = _111058_136790 + _137427; _137431 = __ldg(idx_137428); p_137431 = _137431; l137429: ; _137431 = p_137431; float diff_137432; diff_137432 = _137411 - _137431; float _137433; _137433 = -2.000000e-02f * diff_137432; float _137434; _137434 = _137433 * diff_137432; s_137437 = expf(_137434); ps_137437 = s_137437; l137435: ; s_137437 = ps_137437; if (_137056) goto l137438; else goto l138826; l138826: ; pconverge_137440 = _137055; goto l137439; l137438: ; pconverge_137440 = 0; goto l137439; l137439: ; converge_137440 = pconverge_137440; bool _137441; _137441 = 1024 <= converge_137440; if (_137441) goto l137442; else goto l138825; l138825: ; pconverge_137444 = converge_137440; goto l137443; l137442: ; pconverge_137444 = 1023; goto l137443; l137443: ; converge_137444 = pconverge_137444; if (_137200) goto l137445; else goto l138824; l138824: ; pconverge_137447 = _137199; goto l137446; l137445: ; pconverge_137447 = 0; goto l137446; l137446: ; converge_137447 = pconverge_137447; bool _137448; _137448 = 1024 <= converge_137447; if (_137448) goto l137449; else goto l138823; l138823: ; pconverge_137451 = converge_137447; goto l137450; l137449: ; pconverge_137451 = 1023; goto l137450; l137450: ; converge_137451 = pconverge_137451; int _137452; _137452 = 1024 * converge_137451; int _137453; _137453 = _137452 + converge_137444; float* idx_137454; idx_137454 = _111058_136790 + _137453; _137457 = __ldg(idx_137454); p_137457 = _137457; l137455: ; _137457 = p_137457; if (_137125) goto l137458; else goto l138822; l138822: ; pconverge_137460 = _137124; goto l137459; l137458: ; pconverge_137460 = 0; goto l137459; l137459: ; converge_137460 = pconverge_137460; bool _137461; _137461 = 1024 <= converge_137460; if (_137461) goto l137462; else goto l138821; l138821: ; pconverge_137464 = converge_137460; goto l137463; l137462: ; pconverge_137464 = 1023; goto l137463; l137463: ; converge_137464 = pconverge_137464; if (_137200) goto l137465; else goto l138820; l138820: ; pconverge_137467 = _137199; goto l137466; l137465: ; pconverge_137467 = 0; goto l137466; l137466: ; converge_137467 = pconverge_137467; bool _137468; _137468 = 1024 <= converge_137467; if (_137468) goto l137469; else goto l138819; l138819: ; pconverge_137471 = converge_137467; goto l137470; l137469: ; pconverge_137471 = 1023; goto l137470; l137470: ; converge_137471 = pconverge_137471; int _137472; _137472 = 1024 * converge_137471; int _137473; _137473 = _137472 + converge_137464; float* idx_137474; idx_137474 = _111058_136790 + _137473; _137477 = __ldg(idx_137474); p_137477 = _137477; l137475: ; _137477 = p_137477; if (_136866) goto l137478; else goto l138818; l138818: ; pconverge_137480 = _136833; goto l137479; l137478: ; pconverge_137480 = 0; goto l137479; l137479: ; converge_137480 = pconverge_137480; bool _137481; _137481 = 1024 <= converge_137480; if (_137481) goto l137482; else goto l138817; l138817: ; pconverge_137484 = converge_137480; goto l137483; l137482: ; pconverge_137484 = 1023; goto l137483; l137483: ; converge_137484 = pconverge_137484; if (_136874) goto l137485; else goto l138816; l138816: ; pconverge_137487 = gid_y_136846; goto l137486; l137485: ; pconverge_137487 = 0; goto l137486; l137486: ; converge_137487 = pconverge_137487; bool _137488; _137488 = 1024 <= converge_137487; if (_137488) goto l137489; else goto l138815; l138815: ; pconverge_137491 = converge_137487; goto l137490; l137489: ; pconverge_137491 = 1023; goto l137490; l137490: ; converge_137491 = pconverge_137491; int _137492; _137492 = 1024 * converge_137491; int _137493; _137493 = _137492 + converge_137484; float* idx_137494; idx_137494 = _111058_136790 + _137493; _137497 = __ldg(idx_137494); p_137497 = _137497; l137495: ; _137497 = p_137497; float diff_137498; diff_137498 = _137477 - _137497; float _137499; _137499 = -2.000000e-02f * diff_137498; float _137500; _137500 = _137499 * diff_137498; s_137503 = expf(_137500); ps_137503 = s_137503; l137501: ; s_137503 = ps_137503; if (_137125) goto l137504; else goto l138814; l138814: ; pconverge_137506 = _137124; goto l137505; l137504: ; pconverge_137506 = 0; goto l137505; l137505: ; converge_137506 = pconverge_137506; bool _137507; _137507 = 1024 <= converge_137506; if (_137507) goto l137508; else goto l138813; l138813: ; pconverge_137510 = converge_137506; goto l137509; l137508: ; pconverge_137510 = 1023; goto l137509; l137509: ; converge_137510 = pconverge_137510; if (_137200) goto l137511; else goto l138812; l138812: ; pconverge_137513 = _137199; goto l137512; l137511: ; pconverge_137513 = 0; goto l137512; l137512: ; converge_137513 = pconverge_137513; bool _137514; _137514 = 1024 <= converge_137513; if (_137514) goto l137515; else goto l138811; l138811: ; pconverge_137517 = converge_137513; goto l137516; l137515: ; pconverge_137517 = 1023; goto l137516; l137516: ; converge_137517 = pconverge_137517; int _137518; _137518 = 1024 * converge_137517; int _137519; _137519 = _137518 + converge_137510; float* idx_137520; idx_137520 = _111058_136790 + _137519; _137523 = __ldg(idx_137520); p_137523 = _137523; l137521: ; _137523 = p_137523; if (_136836) goto l137524; else goto l138810; l138810: ; pconverge_137526 = _136834; goto l137525; l137524: ; pconverge_137526 = 0; goto l137525; l137525: ; converge_137526 = pconverge_137526; bool _137527; _137527 = 1024 <= converge_137526; if (_137527) goto l137528; else goto l138809; l138809: ; pconverge_137530 = converge_137526; goto l137529; l137528: ; pconverge_137530 = 1023; goto l137529; l137529: ; converge_137530 = pconverge_137530; if (_136874) goto l137531; else goto l138808; l138808: ; pconverge_137533 = gid_y_136846; goto l137532; l137531: ; pconverge_137533 = 0; goto l137532; l137532: ; converge_137533 = pconverge_137533; bool _137534; _137534 = 1024 <= converge_137533; if (_137534) goto l137535; else goto l138807; l138807: ; pconverge_137537 = converge_137533; goto l137536; l137535: ; pconverge_137537 = 1023; goto l137536; l137536: ; converge_137537 = pconverge_137537; int _137538; _137538 = 1024 * converge_137537; int _137539; _137539 = _137538 + converge_137530; float* idx_137540; idx_137540 = _111058_136790 + _137539; _137543 = __ldg(idx_137540); p_137543 = _137543; l137541: ; _137543 = p_137543; if (_136866) goto l137544; else goto l138806; l138806: ; pconverge_137546 = _136833; goto l137545; l137544: ; pconverge_137546 = 0; goto l137545; l137545: ; converge_137546 = pconverge_137546; bool _137547; _137547 = 1024 <= converge_137546; if (_137547) goto l137548; else goto l138805; l138805: ; pconverge_137550 = converge_137546; goto l137549; l137548: ; pconverge_137550 = 1023; goto l137549; l137549: ; converge_137550 = pconverge_137550; if (_136874) goto l137551; else goto l138804; l138804: ; pconverge_137553 = gid_y_136846; goto l137552; l137551: ; pconverge_137553 = 0; goto l137552; l137552: ; converge_137553 = pconverge_137553; bool _137554; _137554 = 1024 <= converge_137553; if (_137554) goto l137555; else goto l138803; l138803: ; pconverge_137557 = converge_137553; goto l137556; l137555: ; pconverge_137557 = 1023; goto l137556; l137556: ; converge_137557 = pconverge_137557; int _137558; _137558 = 1024 * converge_137557; int _137559; _137559 = _137558 + converge_137550; float* idx_137560; idx_137560 = _111058_136790 + _137559; _137563 = __ldg(idx_137560); p_137563 = _137563; l137561: ; _137563 = p_137563; float diff_137564; diff_137564 = _137543 - _137563; float _137565; _137565 = -2.000000e-02f * diff_137564; float _137566; _137566 = _137565 * diff_137564; s_137569 = expf(_137566); ps_137569 = s_137569; l137567: ; s_137569 = ps_137569; if (_136836) goto l137570; else goto l138802; l138802: ; pconverge_137572 = _136834; goto l137571; l137570: ; pconverge_137572 = 0; goto l137571; l137571: ; converge_137572 = pconverge_137572; bool _137573; _137573 = 1024 <= converge_137572; if (_137573) goto l137574; else goto l138801; l138801: ; pconverge_137576 = converge_137572; goto l137575; l137574: ; pconverge_137576 = 1023; goto l137575; l137575: ; converge_137576 = pconverge_137576; if (_136874) goto l137577; else goto l138800; l138800: ; pconverge_137579 = gid_y_136846; goto l137578; l137577: ; pconverge_137579 = 0; goto l137578; l137578: ; converge_137579 = pconverge_137579; bool _137580; _137580 = 1024 <= converge_137579; if (_137580) goto l137581; else goto l138799; l138799: ; pconverge_137583 = converge_137579; goto l137582; l137581: ; pconverge_137583 = 1023; goto l137582; l137582: ; converge_137583 = pconverge_137583; int _137584; _137584 = 1024 * converge_137583; int _137585; _137585 = _137584 + converge_137576; float* idx_137586; idx_137586 = _111058_136790 + _137585; _137589 = __ldg(idx_137586); p_137589 = _137589; l137587: ; _137589 = p_137589; if (_136921) goto l137590; else goto l138798; l138798: ; pconverge_137592 = _136920; goto l137591; l137590: ; pconverge_137592 = 0; goto l137591; l137591: ; converge_137592 = pconverge_137592; bool _137593; _137593 = 1024 <= converge_137592; if (_137593) goto l137594; else goto l138797; l138797: ; pconverge_137596 = converge_137592; goto l137595; l137594: ; pconverge_137596 = 1023; goto l137595; l137595: ; converge_137596 = pconverge_137596; if (_136874) goto l137597; else goto l138796; l138796: ; pconverge_137599 = gid_y_136846; goto l137598; l137597: ; pconverge_137599 = 0; goto l137598; l137598: ; converge_137599 = pconverge_137599; bool _137600; _137600 = 1024 <= converge_137599; if (_137600) goto l137601; else goto l138795; l138795: ; pconverge_137603 = converge_137599; goto l137602; l137601: ; pconverge_137603 = 1023; goto l137602; l137602: ; converge_137603 = pconverge_137603; int _137604; _137604 = 1024 * converge_137603; int _137605; _137605 = _137604 + converge_137596; float* idx_137606; idx_137606 = _111058_136790 + _137605; _137609 = __ldg(idx_137606); p_137609 = _137609; l137607: ; _137609 = p_137609; if (_136866) goto l137610; else goto l138794; l138794: ; pconverge_137612 = _136833; goto l137611; l137610: ; pconverge_137612 = 0; goto l137611; l137611: ; converge_137612 = pconverge_137612; bool _137613; _137613 = 1024 <= converge_137612; if (_137613) goto l137614; else goto l138793; l138793: ; pconverge_137616 = converge_137612; goto l137615; l137614: ; pconverge_137616 = 1023; goto l137615; l137615: ; converge_137616 = pconverge_137616; if (_136874) goto l137617; else goto l138792; l138792: ; pconverge_137619 = gid_y_136846; goto l137618; l137617: ; pconverge_137619 = 0; goto l137618; l137618: ; converge_137619 = pconverge_137619; bool _137620; _137620 = 1024 <= converge_137619; if (_137620) goto l137621; else goto l138791; l138791: ; pconverge_137623 = converge_137619; goto l137622; l137621: ; pconverge_137623 = 1023; goto l137622; l137622: ; converge_137623 = pconverge_137623; int _137624; _137624 = 1024 * converge_137623; int _137625; _137625 = _137624 + converge_137616; float* idx_137626; idx_137626 = _111058_136790 + _137625; _137629 = __ldg(idx_137626); p_137629 = _137629; l137627: ; _137629 = p_137629; float diff_137630; diff_137630 = _137609 - _137629; float _137631; _137631 = -2.000000e-02f * diff_137630; float _137632; _137632 = _137631 * diff_137630; s_137635 = expf(_137632); ps_137635 = s_137635; l137633: ; s_137635 = ps_137635; if (_136921) goto l137636; else goto l138790; l138790: ; pconverge_137638 = _136920; goto l137637; l137636: ; pconverge_137638 = 0; goto l137637; l137637: ; converge_137638 = pconverge_137638; bool _137639; _137639 = 1024 <= converge_137638; if (_137639) goto l137640; else goto l138789; l138789: ; pconverge_137642 = converge_137638; goto l137641; l137640: ; pconverge_137642 = 1023; goto l137641; l137641: ; converge_137642 = pconverge_137642; if (_136874) goto l137643; else goto l138788; l138788: ; pconverge_137645 = gid_y_136846; goto l137644; l137643: ; pconverge_137645 = 0; goto l137644; l137644: ; converge_137645 = pconverge_137645; bool _137646; _137646 = 1024 <= converge_137645; if (_137646) goto l137647; else goto l138787; l138787: ; pconverge_137649 = converge_137645; goto l137648; l137647: ; pconverge_137649 = 1023; goto l137648; l137648: ; converge_137649 = pconverge_137649; int _137650; _137650 = 1024 * converge_137649; int _137651; _137651 = _137650 + converge_137642; float* idx_137652; idx_137652 = _111058_136790 + _137651; _137655 = __ldg(idx_137652); p_137655 = _137655; l137653: ; _137655 = p_137655; if (_136866) goto l137656; else goto l138786; l138786: ; pconverge_137658 = _136833; goto l137657; l137656: ; pconverge_137658 = 0; goto l137657; l137657: ; converge_137658 = pconverge_137658; bool _137659; _137659 = 1024 <= converge_137658; if (_137659) goto l137660; else goto l138785; l138785: ; pconverge_137662 = converge_137658; goto l137661; l137660: ; pconverge_137662 = 1023; goto l137661; l137661: ; converge_137662 = pconverge_137662; if (_136874) goto l137663; else goto l138784; l138784: ; pconverge_137665 = gid_y_136846; goto l137664; l137663: ; pconverge_137665 = 0; goto l137664; l137664: ; converge_137665 = pconverge_137665; bool _137666; _137666 = 1024 <= converge_137665; if (_137666) goto l137667; else goto l138783; l138783: ; pconverge_137669 = converge_137665; goto l137668; l137667: ; pconverge_137669 = 1023; goto l137668; l137668: ; converge_137669 = pconverge_137669; int _137670; _137670 = 1024 * converge_137669; int _137671; _137671 = _137670 + converge_137662; float* idx_137672; idx_137672 = _111058_136790 + _137671; _137675 = __ldg(idx_137672); p_137675 = _137675; l137673: ; _137675 = p_137675; if (_136866) goto l137676; else goto l138782; l138782: ; pconverge_137678 = _136833; goto l137677; l137676: ; pconverge_137678 = 0; goto l137677; l137677: ; converge_137678 = pconverge_137678; bool _137679; _137679 = 1024 <= converge_137678; if (_137679) goto l137680; else goto l138781; l138781: ; pconverge_137682 = converge_137678; goto l137681; l137680: ; pconverge_137682 = 1023; goto l137681; l137681: ; converge_137682 = pconverge_137682; if (_136874) goto l137683; else goto l138780; l138780: ; pconverge_137685 = gid_y_136846; goto l137684; l137683: ; pconverge_137685 = 0; goto l137684; l137684: ; converge_137685 = pconverge_137685; bool _137686; _137686 = 1024 <= converge_137685; if (_137686) goto l137687; else goto l138779; l138779: ; pconverge_137689 = converge_137685; goto l137688; l137687: ; pconverge_137689 = 1023; goto l137688; l137688: ; converge_137689 = pconverge_137689; int _137690; _137690 = 1024 * converge_137689; int _137691; _137691 = _137690 + converge_137682; float* idx_137692; idx_137692 = _111058_136790 + _137691; _137695 = __ldg(idx_137692); p_137695 = _137695; l137693: ; _137695 = p_137695; float diff_137696; diff_137696 = _137675 - _137695; float _137697; _137697 = -2.000000e-02f * diff_137696; float _137698; _137698 = _137697 * diff_137696; s_137701 = expf(_137698); ps_137701 = s_137701; l137699: ; s_137701 = ps_137701; if (_136866) goto l137702; else goto l138778; l138778: ; pconverge_137704 = _136833; goto l137703; l137702: ; pconverge_137704 = 0; goto l137703; l137703: ; converge_137704 = pconverge_137704; bool _137705; _137705 = 1024 <= converge_137704; if (_137705) goto l137706; else goto l138777; l138777: ; pconverge_137708 = converge_137704; goto l137707; l137706: ; pconverge_137708 = 1023; goto l137707; l137707: ; converge_137708 = pconverge_137708; if (_136874) goto l137709; else goto l138776; l138776: ; pconverge_137711 = gid_y_136846; goto l137710; l137709: ; pconverge_137711 = 0; goto l137710; l137710: ; converge_137711 = pconverge_137711; bool _137712; _137712 = 1024 <= converge_137711; if (_137712) goto l137713; else goto l138775; l138775: ; pconverge_137715 = converge_137711; goto l137714; l137713: ; pconverge_137715 = 1023; goto l137714; l137714: ; converge_137715 = pconverge_137715; int _137716; _137716 = 1024 * converge_137715; int _137717; _137717 = _137716 + converge_137708; float* idx_137718; idx_137718 = _111058_136790 + _137717; _137721 = __ldg(idx_137718); p_137721 = _137721; l137719: ; _137721 = p_137721; if (_137056) goto l137722; else goto l138774; l138774: ; pconverge_137724 = _137055; goto l137723; l137722: ; pconverge_137724 = 0; goto l137723; l137723: ; converge_137724 = pconverge_137724; bool _137725; _137725 = 1024 <= converge_137724; if (_137725) goto l137726; else goto l138773; l138773: ; pconverge_137728 = converge_137724; goto l137727; l137726: ; pconverge_137728 = 1023; goto l137727; l137727: ; converge_137728 = pconverge_137728; if (_136874) goto l137729; else goto l138772; l138772: ; pconverge_137731 = gid_y_136846; goto l137730; l137729: ; pconverge_137731 = 0; goto l137730; l137730: ; converge_137731 = pconverge_137731; bool _137732; _137732 = 1024 <= converge_137731; if (_137732) goto l137733; else goto l138771; l138771: ; pconverge_137735 = converge_137731; goto l137734; l137733: ; pconverge_137735 = 1023; goto l137734; l137734: ; converge_137735 = pconverge_137735; int _137736; _137736 = 1024 * converge_137735; int _137737; _137737 = _137736 + converge_137728; float* idx_137738; idx_137738 = _111058_136790 + _137737; _137741 = __ldg(idx_137738); p_137741 = _137741; l137739: ; _137741 = p_137741; if (_136866) goto l137742; else goto l138770; l138770: ; pconverge_137744 = _136833; goto l137743; l137742: ; pconverge_137744 = 0; goto l137743; l137743: ; converge_137744 = pconverge_137744; bool _137745; _137745 = 1024 <= converge_137744; if (_137745) goto l137746; else goto l138769; l138769: ; pconverge_137748 = converge_137744; goto l137747; l137746: ; pconverge_137748 = 1023; goto l137747; l137747: ; converge_137748 = pconverge_137748; if (_136874) goto l137749; else goto l138768; l138768: ; pconverge_137751 = gid_y_136846; goto l137750; l137749: ; pconverge_137751 = 0; goto l137750; l137750: ; converge_137751 = pconverge_137751; bool _137752; _137752 = 1024 <= converge_137751; if (_137752) goto l137753; else goto l138767; l138767: ; pconverge_137755 = converge_137751; goto l137754; l137753: ; pconverge_137755 = 1023; goto l137754; l137754: ; converge_137755 = pconverge_137755; int _137756; _137756 = 1024 * converge_137755; int _137757; _137757 = _137756 + converge_137748; float* idx_137758; idx_137758 = _111058_136790 + _137757; _137761 = __ldg(idx_137758); p_137761 = _137761; l137759: ; _137761 = p_137761; float diff_137762; diff_137762 = _137741 - _137761; float _137763; _137763 = -2.000000e-02f * diff_137762; float _137764; _137764 = _137763 * diff_137762; s_137767 = expf(_137764); ps_137767 = s_137767; l137765: ; s_137767 = ps_137767; if (_137056) goto l137768; else goto l138766; l138766: ; pconverge_137770 = _137055; goto l137769; l137768: ; pconverge_137770 = 0; goto l137769; l137769: ; converge_137770 = pconverge_137770; bool _137771; _137771 = 1024 <= converge_137770; if (_137771) goto l137772; else goto l138765; l138765: ; pconverge_137774 = converge_137770; goto l137773; l137772: ; pconverge_137774 = 1023; goto l137773; l137773: ; converge_137774 = pconverge_137774; if (_136874) goto l137775; else goto l138764; l138764: ; pconverge_137777 = gid_y_136846; goto l137776; l137775: ; pconverge_137777 = 0; goto l137776; l137776: ; converge_137777 = pconverge_137777; bool _137778; _137778 = 1024 <= converge_137777; if (_137778) goto l137779; else goto l138763; l138763: ; pconverge_137781 = converge_137777; goto l137780; l137779: ; pconverge_137781 = 1023; goto l137780; l137780: ; converge_137781 = pconverge_137781; int _137782; _137782 = 1024 * converge_137781; int _137783; _137783 = _137782 + converge_137774; float* idx_137784; idx_137784 = _111058_136790 + _137783; _137787 = __ldg(idx_137784); p_137787 = _137787; l137785: ; _137787 = p_137787; if (_137125) goto l137788; else goto l138762; l138762: ; pconverge_137790 = _137124; goto l137789; l137788: ; pconverge_137790 = 0; goto l137789; l137789: ; converge_137790 = pconverge_137790; bool _137791; _137791 = 1024 <= converge_137790; if (_137791) goto l137792; else goto l138761; l138761: ; pconverge_137794 = converge_137790; goto l137793; l137792: ; pconverge_137794 = 1023; goto l137793; l137793: ; converge_137794 = pconverge_137794; if (_136874) goto l137795; else goto l138760; l138760: ; pconverge_137797 = gid_y_136846; goto l137796; l137795: ; pconverge_137797 = 0; goto l137796; l137796: ; converge_137797 = pconverge_137797; bool _137798; _137798 = 1024 <= converge_137797; if (_137798) goto l137799; else goto l138759; l138759: ; pconverge_137801 = converge_137797; goto l137800; l137799: ; pconverge_137801 = 1023; goto l137800; l137800: ; converge_137801 = pconverge_137801; int _137802; _137802 = 1024 * converge_137801; int _137803; _137803 = _137802 + converge_137794; float* idx_137804; idx_137804 = _111058_136790 + _137803; _137807 = __ldg(idx_137804); p_137807 = _137807; l137805: ; _137807 = p_137807; if (_136866) goto l137808; else goto l138758; l138758: ; pconverge_137810 = _136833; goto l137809; l137808: ; pconverge_137810 = 0; goto l137809; l137809: ; converge_137810 = pconverge_137810; bool _137811; _137811 = 1024 <= converge_137810; if (_137811) goto l137812; else goto l138757; l138757: ; pconverge_137814 = converge_137810; goto l137813; l137812: ; pconverge_137814 = 1023; goto l137813; l137813: ; converge_137814 = pconverge_137814; if (_136874) goto l137815; else goto l138756; l138756: ; pconverge_137817 = gid_y_136846; goto l137816; l137815: ; pconverge_137817 = 0; goto l137816; l137816: ; converge_137817 = pconverge_137817; bool _137818; _137818 = 1024 <= converge_137817; if (_137818) goto l137819; else goto l138755; l138755: ; pconverge_137821 = converge_137817; goto l137820; l137819: ; pconverge_137821 = 1023; goto l137820; l137820: ; converge_137821 = pconverge_137821; int _137822; _137822 = 1024 * converge_137821; int _137823; _137823 = _137822 + converge_137814; float* idx_137824; idx_137824 = _111058_136790 + _137823; _137827 = __ldg(idx_137824); p_137827 = _137827; l137825: ; _137827 = p_137827; float diff_137828; diff_137828 = _137807 - _137827; float _137829; _137829 = -2.000000e-02f * diff_137828; float _137830; _137830 = _137829 * diff_137828; s_137833 = expf(_137830); ps_137833 = s_137833; l137831: ; s_137833 = ps_137833; if (_137125) goto l137834; else goto l138754; l138754: ; pconverge_137836 = _137124; goto l137835; l137834: ; pconverge_137836 = 0; goto l137835; l137835: ; converge_137836 = pconverge_137836; bool _137837; _137837 = 1024 <= converge_137836; if (_137837) goto l137838; else goto l138753; l138753: ; pconverge_137840 = converge_137836; goto l137839; l137838: ; pconverge_137840 = 1023; goto l137839; l137839: ; converge_137840 = pconverge_137840; if (_136874) goto l137841; else goto l138752; l138752: ; pconverge_137843 = gid_y_136846; goto l137842; l137841: ; pconverge_137843 = 0; goto l137842; l137842: ; converge_137843 = pconverge_137843; bool _137844; _137844 = 1024 <= converge_137843; if (_137844) goto l137845; else goto l138751; l138751: ; pconverge_137847 = converge_137843; goto l137846; l137845: ; pconverge_137847 = 1023; goto l137846; l137846: ; converge_137847 = pconverge_137847; int _137848; _137848 = 1024 * converge_137847; int _137849; _137849 = _137848 + converge_137840; float* idx_137850; idx_137850 = _111058_136790 + _137849; _137853 = __ldg(idx_137850); p_137853 = _137853; l137851: ; _137853 = p_137853; if (_136836) goto l137854; else goto l138750; l138750: ; pconverge_137856 = _136834; goto l137855; l137854: ; pconverge_137856 = 0; goto l137855; l137855: ; converge_137856 = pconverge_137856; bool _137857; _137857 = 1024 <= converge_137856; if (_137857) goto l137858; else goto l138749; l138749: ; pconverge_137860 = converge_137856; goto l137859; l137858: ; pconverge_137860 = 1023; goto l137859; l137859: ; converge_137860 = pconverge_137860; int _137861; _137861 = 1 + gid_y_136846; bool _137862; _137862 = _137861 < 0; if (_137862) goto l137863; else goto l138748; l138748: ; pconverge_137865 = _137861; goto l137864; l137863: ; pconverge_137865 = 0; goto l137864; l137864: ; converge_137865 = pconverge_137865; bool _137866; _137866 = 1024 <= converge_137865; if (_137866) goto l137867; else goto l138747; l138747: ; pconverge_137869 = converge_137865; goto l137868; l137867: ; pconverge_137869 = 1023; goto l137868; l137868: ; converge_137869 = pconverge_137869; int _137870; _137870 = 1024 * converge_137869; int _137871; _137871 = _137870 + converge_137860; float* idx_137872; idx_137872 = _111058_136790 + _137871; _137875 = __ldg(idx_137872); p_137875 = _137875; l137873: ; _137875 = p_137875; if (_136866) goto l137876; else goto l138746; l138746: ; pconverge_137878 = _136833; goto l137877; l137876: ; pconverge_137878 = 0; goto l137877; l137877: ; converge_137878 = pconverge_137878; bool _137879; _137879 = 1024 <= converge_137878; if (_137879) goto l137880; else goto l138745; l138745: ; pconverge_137882 = converge_137878; goto l137881; l137880: ; pconverge_137882 = 1023; goto l137881; l137881: ; converge_137882 = pconverge_137882; if (_136874) goto l137883; else goto l138744; l138744: ; pconverge_137885 = gid_y_136846; goto l137884; l137883: ; pconverge_137885 = 0; goto l137884; l137884: ; converge_137885 = pconverge_137885; bool _137886; _137886 = 1024 <= converge_137885; if (_137886) goto l137887; else goto l138743; l138743: ; pconverge_137889 = converge_137885; goto l137888; l137887: ; pconverge_137889 = 1023; goto l137888; l137888: ; converge_137889 = pconverge_137889; int _137890; _137890 = 1024 * converge_137889; int _137891; _137891 = _137890 + converge_137882; float* idx_137892; idx_137892 = _111058_136790 + _137891; _137895 = __ldg(idx_137892); p_137895 = _137895; l137893: ; _137895 = p_137895; float diff_137896; diff_137896 = _137875 - _137895; float _137897; _137897 = -2.000000e-02f * diff_137896; float _137898; _137898 = _137897 * diff_137896; s_137901 = expf(_137898); ps_137901 = s_137901; l137899: ; s_137901 = ps_137901; if (_136836) goto l137902; else goto l138742; l138742: ; pconverge_137904 = _136834; goto l137903; l137902: ; pconverge_137904 = 0; goto l137903; l137903: ; converge_137904 = pconverge_137904; bool _137905; _137905 = 1024 <= converge_137904; if (_137905) goto l137906; else goto l138741; l138741: ; pconverge_137908 = converge_137904; goto l137907; l137906: ; pconverge_137908 = 1023; goto l137907; l137907: ; converge_137908 = pconverge_137908; if (_137862) goto l137909; else goto l138740; l138740: ; pconverge_137911 = _137861; goto l137910; l137909: ; pconverge_137911 = 0; goto l137910; l137910: ; converge_137911 = pconverge_137911; bool _137912; _137912 = 1024 <= converge_137911; if (_137912) goto l137913; else goto l138739; l138739: ; pconverge_137915 = converge_137911; goto l137914; l137913: ; pconverge_137915 = 1023; goto l137914; l137914: ; converge_137915 = pconverge_137915; int _137916; _137916 = 1024 * converge_137915; int _137917; _137917 = _137916 + converge_137908; float* idx_137918; idx_137918 = _111058_136790 + _137917; _137921 = __ldg(idx_137918); p_137921 = _137921; l137919: ; _137921 = p_137921; if (_136921) goto l137922; else goto l138738; l138738: ; pconverge_137924 = _136920; goto l137923; l137922: ; pconverge_137924 = 0; goto l137923; l137923: ; converge_137924 = pconverge_137924; bool _137925; _137925 = 1024 <= converge_137924; if (_137925) goto l137926; else goto l138737; l138737: ; pconverge_137928 = converge_137924; goto l137927; l137926: ; pconverge_137928 = 1023; goto l137927; l137927: ; converge_137928 = pconverge_137928; if (_137862) goto l137929; else goto l138736; l138736: ; pconverge_137931 = _137861; goto l137930; l137929: ; pconverge_137931 = 0; goto l137930; l137930: ; converge_137931 = pconverge_137931; bool _137932; _137932 = 1024 <= converge_137931; if (_137932) goto l137933; else goto l138735; l138735: ; pconverge_137935 = converge_137931; goto l137934; l137933: ; pconverge_137935 = 1023; goto l137934; l137934: ; converge_137935 = pconverge_137935; int _137936; _137936 = 1024 * converge_137935; int _137937; _137937 = _137936 + converge_137928; float* idx_137938; idx_137938 = _111058_136790 + _137937; _137941 = __ldg(idx_137938); p_137941 = _137941; l137939: ; _137941 = p_137941; if (_136866) goto l137942; else goto l138734; l138734: ; pconverge_137944 = _136833; goto l137943; l137942: ; pconverge_137944 = 0; goto l137943; l137943: ; converge_137944 = pconverge_137944; bool _137945; _137945 = 1024 <= converge_137944; if (_137945) goto l137946; else goto l138733; l138733: ; pconverge_137948 = converge_137944; goto l137947; l137946: ; pconverge_137948 = 1023; goto l137947; l137947: ; converge_137948 = pconverge_137948; if (_136874) goto l137949; else goto l138732; l138732: ; pconverge_137951 = gid_y_136846; goto l137950; l137949: ; pconverge_137951 = 0; goto l137950; l137950: ; converge_137951 = pconverge_137951; bool _137952; _137952 = 1024 <= converge_137951; if (_137952) goto l137953; else goto l138731; l138731: ; pconverge_137955 = converge_137951; goto l137954; l137953: ; pconverge_137955 = 1023; goto l137954; l137954: ; converge_137955 = pconverge_137955; int _137956; _137956 = 1024 * converge_137955; int _137957; _137957 = _137956 + converge_137948; float* idx_137958; idx_137958 = _111058_136790 + _137957; _137961 = __ldg(idx_137958); p_137961 = _137961; l137959: ; _137961 = p_137961; float diff_137962; diff_137962 = _137941 - _137961; float _137963; _137963 = -2.000000e-02f * diff_137962; float _137964; _137964 = _137963 * diff_137962; s_137967 = expf(_137964); ps_137967 = s_137967; l137965: ; s_137967 = ps_137967; if (_136921) goto l137968; else goto l138730; l138730: ; pconverge_137970 = _136920; goto l137969; l137968: ; pconverge_137970 = 0; goto l137969; l137969: ; converge_137970 = pconverge_137970; bool _137971; _137971 = 1024 <= converge_137970; if (_137971) goto l137972; else goto l138729; l138729: ; pconverge_137974 = converge_137970; goto l137973; l137972: ; pconverge_137974 = 1023; goto l137973; l137973: ; converge_137974 = pconverge_137974; if (_137862) goto l137975; else goto l138728; l138728: ; pconverge_137977 = _137861; goto l137976; l137975: ; pconverge_137977 = 0; goto l137976; l137976: ; converge_137977 = pconverge_137977; bool _137978; _137978 = 1024 <= converge_137977; if (_137978) goto l137979; else goto l138727; l138727: ; pconverge_137981 = converge_137977; goto l137980; l137979: ; pconverge_137981 = 1023; goto l137980; l137980: ; converge_137981 = pconverge_137981; int _137982; _137982 = 1024 * converge_137981; int _137983; _137983 = _137982 + converge_137974; float* idx_137984; idx_137984 = _111058_136790 + _137983; _137987 = __ldg(idx_137984); p_137987 = _137987; l137985: ; _137987 = p_137987; if (_136866) goto l137988; else goto l138726; l138726: ; pconverge_137990 = _136833; goto l137989; l137988: ; pconverge_137990 = 0; goto l137989; l137989: ; converge_137990 = pconverge_137990; bool _137991; _137991 = 1024 <= converge_137990; if (_137991) goto l137992; else goto l138725; l138725: ; pconverge_137994 = converge_137990; goto l137993; l137992: ; pconverge_137994 = 1023; goto l137993; l137993: ; converge_137994 = pconverge_137994; if (_137862) goto l137995; else goto l138724; l138724: ; pconverge_137997 = _137861; goto l137996; l137995: ; pconverge_137997 = 0; goto l137996; l137996: ; converge_137997 = pconverge_137997; bool _137998; _137998 = 1024 <= converge_137997; if (_137998) goto l137999; else goto l138723; l138723: ; pconverge_138001 = converge_137997; goto l138000; l137999: ; pconverge_138001 = 1023; goto l138000; l138000: ; converge_138001 = pconverge_138001; int _138002; _138002 = 1024 * converge_138001; int _138003; _138003 = _138002 + converge_137994; float* idx_138004; idx_138004 = _111058_136790 + _138003; _138007 = __ldg(idx_138004); p_138007 = _138007; l138005: ; _138007 = p_138007; if (_136866) goto l138008; else goto l138722; l138722: ; pconverge_138010 = _136833; goto l138009; l138008: ; pconverge_138010 = 0; goto l138009; l138009: ; converge_138010 = pconverge_138010; bool _138011; _138011 = 1024 <= converge_138010; if (_138011) goto l138012; else goto l138721; l138721: ; pconverge_138014 = converge_138010; goto l138013; l138012: ; pconverge_138014 = 1023; goto l138013; l138013: ; converge_138014 = pconverge_138014; if (_136874) goto l138015; else goto l138720; l138720: ; pconverge_138017 = gid_y_136846; goto l138016; l138015: ; pconverge_138017 = 0; goto l138016; l138016: ; converge_138017 = pconverge_138017; bool _138018; _138018 = 1024 <= converge_138017; if (_138018) goto l138019; else goto l138719; l138719: ; pconverge_138021 = converge_138017; goto l138020; l138019: ; pconverge_138021 = 1023; goto l138020; l138020: ; converge_138021 = pconverge_138021; int _138022; _138022 = 1024 * converge_138021; int _138023; _138023 = _138022 + converge_138014; float* idx_138024; idx_138024 = _111058_136790 + _138023; _138027 = __ldg(idx_138024); p_138027 = _138027; l138025: ; _138027 = p_138027; float diff_138028; diff_138028 = _138007 - _138027; float _138029; _138029 = -2.000000e-02f * diff_138028; float _138030; _138030 = _138029 * diff_138028; s_138033 = expf(_138030); ps_138033 = s_138033; l138031: ; s_138033 = ps_138033; if (_136866) goto l138034; else goto l138718; l138718: ; pconverge_138036 = _136833; goto l138035; l138034: ; pconverge_138036 = 0; goto l138035; l138035: ; converge_138036 = pconverge_138036; bool _138037; _138037 = 1024 <= converge_138036; if (_138037) goto l138038; else goto l138717; l138717: ; pconverge_138040 = converge_138036; goto l138039; l138038: ; pconverge_138040 = 1023; goto l138039; l138039: ; converge_138040 = pconverge_138040; if (_137862) goto l138041; else goto l138716; l138716: ; pconverge_138043 = _137861; goto l138042; l138041: ; pconverge_138043 = 0; goto l138042; l138042: ; converge_138043 = pconverge_138043; bool _138044; _138044 = 1024 <= converge_138043; if (_138044) goto l138045; else goto l138715; l138715: ; pconverge_138047 = converge_138043; goto l138046; l138045: ; pconverge_138047 = 1023; goto l138046; l138046: ; converge_138047 = pconverge_138047; int _138048; _138048 = 1024 * converge_138047; int _138049; _138049 = _138048 + converge_138040; float* idx_138050; idx_138050 = _111058_136790 + _138049; _138053 = __ldg(idx_138050); p_138053 = _138053; l138051: ; _138053 = p_138053; if (_137056) goto l138054; else goto l138714; l138714: ; pconverge_138056 = _137055; goto l138055; l138054: ; pconverge_138056 = 0; goto l138055; l138055: ; converge_138056 = pconverge_138056; bool _138057; _138057 = 1024 <= converge_138056; if (_138057) goto l138058; else goto l138713; l138713: ; pconverge_138060 = converge_138056; goto l138059; l138058: ; pconverge_138060 = 1023; goto l138059; l138059: ; converge_138060 = pconverge_138060; if (_137862) goto l138061; else goto l138712; l138712: ; pconverge_138063 = _137861; goto l138062; l138061: ; pconverge_138063 = 0; goto l138062; l138062: ; converge_138063 = pconverge_138063; bool _138064; _138064 = 1024 <= converge_138063; if (_138064) goto l138065; else goto l138711; l138711: ; pconverge_138067 = converge_138063; goto l138066; l138065: ; pconverge_138067 = 1023; goto l138066; l138066: ; converge_138067 = pconverge_138067; int _138068; _138068 = 1024 * converge_138067; int _138069; _138069 = _138068 + converge_138060; float* idx_138070; idx_138070 = _111058_136790 + _138069; _138073 = __ldg(idx_138070); p_138073 = _138073; l138071: ; _138073 = p_138073; if (_136866) goto l138074; else goto l138710; l138710: ; pconverge_138076 = _136833; goto l138075; l138074: ; pconverge_138076 = 0; goto l138075; l138075: ; converge_138076 = pconverge_138076; bool _138077; _138077 = 1024 <= converge_138076; if (_138077) goto l138078; else goto l138709; l138709: ; pconverge_138080 = converge_138076; goto l138079; l138078: ; pconverge_138080 = 1023; goto l138079; l138079: ; converge_138080 = pconverge_138080; if (_136874) goto l138081; else goto l138708; l138708: ; pconverge_138083 = gid_y_136846; goto l138082; l138081: ; pconverge_138083 = 0; goto l138082; l138082: ; converge_138083 = pconverge_138083; bool _138084; _138084 = 1024 <= converge_138083; if (_138084) goto l138085; else goto l138707; l138707: ; pconverge_138087 = converge_138083; goto l138086; l138085: ; pconverge_138087 = 1023; goto l138086; l138086: ; converge_138087 = pconverge_138087; int _138088; _138088 = 1024 * converge_138087; int _138089; _138089 = _138088 + converge_138080; float* idx_138090; idx_138090 = _111058_136790 + _138089; _138093 = __ldg(idx_138090); p_138093 = _138093; l138091: ; _138093 = p_138093; float diff_138094; diff_138094 = _138073 - _138093; float _138095; _138095 = -2.000000e-02f * diff_138094; float _138096; _138096 = _138095 * diff_138094; s_138099 = expf(_138096); ps_138099 = s_138099; l138097: ; s_138099 = ps_138099; if (_137056) goto l138100; else goto l138706; l138706: ; pconverge_138102 = _137055; goto l138101; l138100: ; pconverge_138102 = 0; goto l138101; l138101: ; converge_138102 = pconverge_138102; bool _138103; _138103 = 1024 <= converge_138102; if (_138103) goto l138104; else goto l138705; l138705: ; pconverge_138106 = converge_138102; goto l138105; l138104: ; pconverge_138106 = 1023; goto l138105; l138105: ; converge_138106 = pconverge_138106; if (_137862) goto l138107; else goto l138704; l138704: ; pconverge_138109 = _137861; goto l138108; l138107: ; pconverge_138109 = 0; goto l138108; l138108: ; converge_138109 = pconverge_138109; bool _138110; _138110 = 1024 <= converge_138109; if (_138110) goto l138111; else goto l138703; l138703: ; pconverge_138113 = converge_138109; goto l138112; l138111: ; pconverge_138113 = 1023; goto l138112; l138112: ; converge_138113 = pconverge_138113; int _138114; _138114 = 1024 * converge_138113; int _138115; _138115 = _138114 + converge_138106; float* idx_138116; idx_138116 = _111058_136790 + _138115; _138119 = __ldg(idx_138116); p_138119 = _138119; l138117: ; _138119 = p_138119; if (_137125) goto l138120; else goto l138702; l138702: ; pconverge_138122 = _137124; goto l138121; l138120: ; pconverge_138122 = 0; goto l138121; l138121: ; converge_138122 = pconverge_138122; bool _138123; _138123 = 1024 <= converge_138122; if (_138123) goto l138124; else goto l138701; l138701: ; pconverge_138126 = converge_138122; goto l138125; l138124: ; pconverge_138126 = 1023; goto l138125; l138125: ; converge_138126 = pconverge_138126; if (_137862) goto l138127; else goto l138700; l138700: ; pconverge_138129 = _137861; goto l138128; l138127: ; pconverge_138129 = 0; goto l138128; l138128: ; converge_138129 = pconverge_138129; bool _138130; _138130 = 1024 <= converge_138129; if (_138130) goto l138131; else goto l138699; l138699: ; pconverge_138133 = converge_138129; goto l138132; l138131: ; pconverge_138133 = 1023; goto l138132; l138132: ; converge_138133 = pconverge_138133; int _138134; _138134 = 1024 * converge_138133; int _138135; _138135 = _138134 + converge_138126; float* idx_138136; idx_138136 = _111058_136790 + _138135; _138139 = __ldg(idx_138136); p_138139 = _138139; l138137: ; _138139 = p_138139; if (_136866) goto l138140; else goto l138698; l138698: ; pconverge_138142 = _136833; goto l138141; l138140: ; pconverge_138142 = 0; goto l138141; l138141: ; converge_138142 = pconverge_138142; bool _138143; _138143 = 1024 <= converge_138142; if (_138143) goto l138144; else goto l138697; l138697: ; pconverge_138146 = converge_138142; goto l138145; l138144: ; pconverge_138146 = 1023; goto l138145; l138145: ; converge_138146 = pconverge_138146; if (_136874) goto l138147; else goto l138696; l138696: ; pconverge_138149 = gid_y_136846; goto l138148; l138147: ; pconverge_138149 = 0; goto l138148; l138148: ; converge_138149 = pconverge_138149; bool _138150; _138150 = 1024 <= converge_138149; if (_138150) goto l138151; else goto l138695; l138695: ; pconverge_138153 = converge_138149; goto l138152; l138151: ; pconverge_138153 = 1023; goto l138152; l138152: ; converge_138153 = pconverge_138153; int _138154; _138154 = 1024 * converge_138153; int _138155; _138155 = _138154 + converge_138146; float* idx_138156; idx_138156 = _111058_136790 + _138155; _138159 = __ldg(idx_138156); p_138159 = _138159; l138157: ; _138159 = p_138159; float diff_138160; diff_138160 = _138139 - _138159; float _138161; _138161 = -2.000000e-02f * diff_138160; float _138162; _138162 = _138161 * diff_138160; s_138165 = expf(_138162); ps_138165 = s_138165; l138163: ; s_138165 = ps_138165; if (_137125) goto l138166; else goto l138694; l138694: ; pconverge_138168 = _137124; goto l138167; l138166: ; pconverge_138168 = 0; goto l138167; l138167: ; converge_138168 = pconverge_138168; bool _138169; _138169 = 1024 <= converge_138168; if (_138169) goto l138170; else goto l138693; l138693: ; pconverge_138172 = converge_138168; goto l138171; l138170: ; pconverge_138172 = 1023; goto l138171; l138171: ; converge_138172 = pconverge_138172; if (_137862) goto l138173; else goto l138692; l138692: ; pconverge_138175 = _137861; goto l138174; l138173: ; pconverge_138175 = 0; goto l138174; l138174: ; converge_138175 = pconverge_138175; bool _138176; _138176 = 1024 <= converge_138175; if (_138176) goto l138177; else goto l138691; l138691: ; pconverge_138179 = converge_138175; goto l138178; l138177: ; pconverge_138179 = 1023; goto l138178; l138178: ; converge_138179 = pconverge_138179; int _138180; _138180 = 1024 * converge_138179; int _138181; _138181 = _138180 + converge_138172; float* idx_138182; idx_138182 = _111058_136790 + _138181; _138185 = __ldg(idx_138182); p_138185 = _138185; l138183: ; _138185 = p_138185; if (_136836) goto l138186; else goto l138690; l138690: ; pconverge_138188 = _136834; goto l138187; l138186: ; pconverge_138188 = 0; goto l138187; l138187: ; converge_138188 = pconverge_138188; bool _138189; _138189 = 1024 <= converge_138188; if (_138189) goto l138190; else goto l138689; l138689: ; pconverge_138192 = converge_138188; goto l138191; l138190: ; pconverge_138192 = 1023; goto l138191; l138191: ; converge_138192 = pconverge_138192; int _138193; _138193 = 2 + gid_y_136846; bool _138194; _138194 = _138193 < 0; if (_138194) goto l138195; else goto l138688; l138688: ; pconverge_138197 = _138193; goto l138196; l138195: ; pconverge_138197 = 0; goto l138196; l138196: ; converge_138197 = pconverge_138197; bool _138198; _138198 = 1024 <= converge_138197; if (_138198) goto l138199; else goto l138687; l138687: ; pconverge_138201 = converge_138197; goto l138200; l138199: ; pconverge_138201 = 1023; goto l138200; l138200: ; converge_138201 = pconverge_138201; int _138202; _138202 = 1024 * converge_138201; int _138203; _138203 = _138202 + converge_138192; float* idx_138204; idx_138204 = _111058_136790 + _138203; _138207 = __ldg(idx_138204); p_138207 = _138207; l138205: ; _138207 = p_138207; if (_136866) goto l138208; else goto l138686; l138686: ; pconverge_138210 = _136833; goto l138209; l138208: ; pconverge_138210 = 0; goto l138209; l138209: ; converge_138210 = pconverge_138210; bool _138211; _138211 = 1024 <= converge_138210; if (_138211) goto l138212; else goto l138685; l138685: ; pconverge_138214 = converge_138210; goto l138213; l138212: ; pconverge_138214 = 1023; goto l138213; l138213: ; converge_138214 = pconverge_138214; if (_136874) goto l138215; else goto l138684; l138684: ; pconverge_138217 = gid_y_136846; goto l138216; l138215: ; pconverge_138217 = 0; goto l138216; l138216: ; converge_138217 = pconverge_138217; bool _138218; _138218 = 1024 <= converge_138217; if (_138218) goto l138219; else goto l138683; l138683: ; pconverge_138221 = converge_138217; goto l138220; l138219: ; pconverge_138221 = 1023; goto l138220; l138220: ; converge_138221 = pconverge_138221; int _138222; _138222 = 1024 * converge_138221; int _138223; _138223 = _138222 + converge_138214; float* idx_138224; idx_138224 = _111058_136790 + _138223; _138227 = __ldg(idx_138224); p_138227 = _138227; l138225: ; _138227 = p_138227; float diff_138228; diff_138228 = _138207 - _138227; float _138229; _138229 = -2.000000e-02f * diff_138228; float _138230; _138230 = _138229 * diff_138228; s_138233 = expf(_138230); ps_138233 = s_138233; l138231: ; s_138233 = ps_138233; if (_136836) goto l138234; else goto l138682; l138682: ; pconverge_138236 = _136834; goto l138235; l138234: ; pconverge_138236 = 0; goto l138235; l138235: ; converge_138236 = pconverge_138236; bool _138237; _138237 = 1024 <= converge_138236; if (_138237) goto l138238; else goto l138681; l138681: ; pconverge_138240 = converge_138236; goto l138239; l138238: ; pconverge_138240 = 1023; goto l138239; l138239: ; converge_138240 = pconverge_138240; if (_138194) goto l138241; else goto l138680; l138680: ; pconverge_138243 = _138193; goto l138242; l138241: ; pconverge_138243 = 0; goto l138242; l138242: ; converge_138243 = pconverge_138243; bool _138244; _138244 = 1024 <= converge_138243; if (_138244) goto l138245; else goto l138679; l138679: ; pconverge_138247 = converge_138243; goto l138246; l138245: ; pconverge_138247 = 1023; goto l138246; l138246: ; converge_138247 = pconverge_138247; int _138248; _138248 = 1024 * converge_138247; int _138249; _138249 = _138248 + converge_138240; float* idx_138250; idx_138250 = _111058_136790 + _138249; _138253 = __ldg(idx_138250); p_138253 = _138253; l138251: ; _138253 = p_138253; if (_136921) goto l138254; else goto l138678; l138678: ; pconverge_138256 = _136920; goto l138255; l138254: ; pconverge_138256 = 0; goto l138255; l138255: ; converge_138256 = pconverge_138256; bool _138257; _138257 = 1024 <= converge_138256; if (_138257) goto l138258; else goto l138677; l138677: ; pconverge_138260 = converge_138256; goto l138259; l138258: ; pconverge_138260 = 1023; goto l138259; l138259: ; converge_138260 = pconverge_138260; if (_138194) goto l138261; else goto l138676; l138676: ; pconverge_138263 = _138193; goto l138262; l138261: ; pconverge_138263 = 0; goto l138262; l138262: ; converge_138263 = pconverge_138263; bool _138264; _138264 = 1024 <= converge_138263; if (_138264) goto l138265; else goto l138675; l138675: ; pconverge_138267 = converge_138263; goto l138266; l138265: ; pconverge_138267 = 1023; goto l138266; l138266: ; converge_138267 = pconverge_138267; int _138268; _138268 = 1024 * converge_138267; int _138269; _138269 = _138268 + converge_138260; float* idx_138270; idx_138270 = _111058_136790 + _138269; _138273 = __ldg(idx_138270); p_138273 = _138273; l138271: ; _138273 = p_138273; if (_136866) goto l138274; else goto l138674; l138674: ; pconverge_138276 = _136833; goto l138275; l138274: ; pconverge_138276 = 0; goto l138275; l138275: ; converge_138276 = pconverge_138276; bool _138277; _138277 = 1024 <= converge_138276; if (_138277) goto l138278; else goto l138673; l138673: ; pconverge_138280 = converge_138276; goto l138279; l138278: ; pconverge_138280 = 1023; goto l138279; l138279: ; converge_138280 = pconverge_138280; if (_136874) goto l138281; else goto l138672; l138672: ; pconverge_138283 = gid_y_136846; goto l138282; l138281: ; pconverge_138283 = 0; goto l138282; l138282: ; converge_138283 = pconverge_138283; bool _138284; _138284 = 1024 <= converge_138283; if (_138284) goto l138285; else goto l138671; l138671: ; pconverge_138287 = converge_138283; goto l138286; l138285: ; pconverge_138287 = 1023; goto l138286; l138286: ; converge_138287 = pconverge_138287; int _138288; _138288 = 1024 * converge_138287; int _138289; _138289 = _138288 + converge_138280; float* idx_138290; idx_138290 = _111058_136790 + _138289; _138293 = __ldg(idx_138290); p_138293 = _138293; l138291: ; _138293 = p_138293; float diff_138294; diff_138294 = _138273 - _138293; float _138295; _138295 = -2.000000e-02f * diff_138294; float _138296; _138296 = _138295 * diff_138294; s_138299 = expf(_138296); ps_138299 = s_138299; l138297: ; s_138299 = ps_138299; if (_136921) goto l138300; else goto l138670; l138670: ; pconverge_138302 = _136920; goto l138301; l138300: ; pconverge_138302 = 0; goto l138301; l138301: ; converge_138302 = pconverge_138302; bool _138303; _138303 = 1024 <= converge_138302; if (_138303) goto l138304; else goto l138669; l138669: ; pconverge_138306 = converge_138302; goto l138305; l138304: ; pconverge_138306 = 1023; goto l138305; l138305: ; converge_138306 = pconverge_138306; if (_138194) goto l138307; else goto l138668; l138668: ; pconverge_138309 = _138193; goto l138308; l138307: ; pconverge_138309 = 0; goto l138308; l138308: ; converge_138309 = pconverge_138309; bool _138310; _138310 = 1024 <= converge_138309; if (_138310) goto l138311; else goto l138667; l138667: ; pconverge_138313 = converge_138309; goto l138312; l138311: ; pconverge_138313 = 1023; goto l138312; l138312: ; converge_138313 = pconverge_138313; int _138314; _138314 = 1024 * converge_138313; int _138315; _138315 = _138314 + converge_138306; float* idx_138316; idx_138316 = _111058_136790 + _138315; _138319 = __ldg(idx_138316); p_138319 = _138319; l138317: ; _138319 = p_138319; if (_136866) goto l138320; else goto l138666; l138666: ; pconverge_138322 = _136833; goto l138321; l138320: ; pconverge_138322 = 0; goto l138321; l138321: ; converge_138322 = pconverge_138322; bool _138323; _138323 = 1024 <= converge_138322; if (_138323) goto l138324; else goto l138665; l138665: ; pconverge_138326 = converge_138322; goto l138325; l138324: ; pconverge_138326 = 1023; goto l138325; l138325: ; converge_138326 = pconverge_138326; if (_138194) goto l138327; else goto l138664; l138664: ; pconverge_138329 = _138193; goto l138328; l138327: ; pconverge_138329 = 0; goto l138328; l138328: ; converge_138329 = pconverge_138329; bool _138330; _138330 = 1024 <= converge_138329; if (_138330) goto l138331; else goto l138663; l138663: ; pconverge_138333 = converge_138329; goto l138332; l138331: ; pconverge_138333 = 1023; goto l138332; l138332: ; converge_138333 = pconverge_138333; int _138334; _138334 = 1024 * converge_138333; int _138335; _138335 = _138334 + converge_138326; float* idx_138336; idx_138336 = _111058_136790 + _138335; _138339 = __ldg(idx_138336); p_138339 = _138339; l138337: ; _138339 = p_138339; if (_136866) goto l138340; else goto l138662; l138662: ; pconverge_138342 = _136833; goto l138341; l138340: ; pconverge_138342 = 0; goto l138341; l138341: ; converge_138342 = pconverge_138342; bool _138343; _138343 = 1024 <= converge_138342; if (_138343) goto l138344; else goto l138661; l138661: ; pconverge_138346 = converge_138342; goto l138345; l138344: ; pconverge_138346 = 1023; goto l138345; l138345: ; converge_138346 = pconverge_138346; if (_136874) goto l138347; else goto l138660; l138660: ; pconverge_138349 = gid_y_136846; goto l138348; l138347: ; pconverge_138349 = 0; goto l138348; l138348: ; converge_138349 = pconverge_138349; bool _138350; _138350 = 1024 <= converge_138349; if (_138350) goto l138351; else goto l138659; l138659: ; pconverge_138353 = converge_138349; goto l138352; l138351: ; pconverge_138353 = 1023; goto l138352; l138352: ; converge_138353 = pconverge_138353; int _138354; _138354 = 1024 * converge_138353; int _138355; _138355 = _138354 + converge_138346; float* idx_138356; idx_138356 = _111058_136790 + _138355; _138359 = __ldg(idx_138356); p_138359 = _138359; l138357: ; _138359 = p_138359; float diff_138360; diff_138360 = _138339 - _138359; float _138361; _138361 = -2.000000e-02f * diff_138360; float _138362; _138362 = _138361 * diff_138360; s_138365 = expf(_138362); ps_138365 = s_138365; l138363: ; s_138365 = ps_138365; if (_136866) goto l138366; else goto l138658; l138658: ; pconverge_138368 = _136833; goto l138367; l138366: ; pconverge_138368 = 0; goto l138367; l138367: ; converge_138368 = pconverge_138368; bool _138369; _138369 = 1024 <= converge_138368; if (_138369) goto l138370; else goto l138657; l138657: ; pconverge_138372 = converge_138368; goto l138371; l138370: ; pconverge_138372 = 1023; goto l138371; l138371: ; converge_138372 = pconverge_138372; if (_138194) goto l138373; else goto l138656; l138656: ; pconverge_138375 = _138193; goto l138374; l138373: ; pconverge_138375 = 0; goto l138374; l138374: ; converge_138375 = pconverge_138375; bool _138376; _138376 = 1024 <= converge_138375; if (_138376) goto l138377; else goto l138655; l138655: ; pconverge_138379 = converge_138375; goto l138378; l138377: ; pconverge_138379 = 1023; goto l138378; l138378: ; converge_138379 = pconverge_138379; int _138380; _138380 = 1024 * converge_138379; int _138381; _138381 = _138380 + converge_138372; float* idx_138382; idx_138382 = _111058_136790 + _138381; _138385 = __ldg(idx_138382); p_138385 = _138385; l138383: ; _138385 = p_138385; if (_137056) goto l138386; else goto l138654; l138654: ; pconverge_138388 = _137055; goto l138387; l138386: ; pconverge_138388 = 0; goto l138387; l138387: ; converge_138388 = pconverge_138388; bool _138389; _138389 = 1024 <= converge_138388; if (_138389) goto l138390; else goto l138653; l138653: ; pconverge_138392 = converge_138388; goto l138391; l138390: ; pconverge_138392 = 1023; goto l138391; l138391: ; converge_138392 = pconverge_138392; if (_138194) goto l138393; else goto l138652; l138652: ; pconverge_138395 = _138193; goto l138394; l138393: ; pconverge_138395 = 0; goto l138394; l138394: ; converge_138395 = pconverge_138395; bool _138396; _138396 = 1024 <= converge_138395; if (_138396) goto l138397; else goto l138651; l138651: ; pconverge_138399 = converge_138395; goto l138398; l138397: ; pconverge_138399 = 1023; goto l138398; l138398: ; converge_138399 = pconverge_138399; int _138400; _138400 = 1024 * converge_138399; int _138401; _138401 = _138400 + converge_138392; float* idx_138402; idx_138402 = _111058_136790 + _138401; _138405 = __ldg(idx_138402); p_138405 = _138405; l138403: ; _138405 = p_138405; if (_136866) goto l138406; else goto l138650; l138650: ; pconverge_138408 = _136833; goto l138407; l138406: ; pconverge_138408 = 0; goto l138407; l138407: ; converge_138408 = pconverge_138408; bool _138409; _138409 = 1024 <= converge_138408; if (_138409) goto l138410; else goto l138649; l138649: ; pconverge_138412 = converge_138408; goto l138411; l138410: ; pconverge_138412 = 1023; goto l138411; l138411: ; converge_138412 = pconverge_138412; if (_136874) goto l138413; else goto l138648; l138648: ; pconverge_138415 = gid_y_136846; goto l138414; l138413: ; pconverge_138415 = 0; goto l138414; l138414: ; converge_138415 = pconverge_138415; bool _138416; _138416 = 1024 <= converge_138415; if (_138416) goto l138417; else goto l138647; l138647: ; pconverge_138419 = converge_138415; goto l138418; l138417: ; pconverge_138419 = 1023; goto l138418; l138418: ; converge_138419 = pconverge_138419; int _138420; _138420 = 1024 * converge_138419; int _138421; _138421 = _138420 + converge_138412; float* idx_138422; idx_138422 = _111058_136790 + _138421; _138425 = __ldg(idx_138422); p_138425 = _138425; l138423: ; _138425 = p_138425; float diff_138426; diff_138426 = _138405 - _138425; float _138427; _138427 = -2.000000e-02f * diff_138426; float _138428; _138428 = _138427 * diff_138426; s_138431 = expf(_138428); ps_138431 = s_138431; l138429: ; s_138431 = ps_138431; if (_137056) goto l138432; else goto l138646; l138646: ; pconverge_138434 = _137055; goto l138433; l138432: ; pconverge_138434 = 0; goto l138433; l138433: ; converge_138434 = pconverge_138434; bool _138435; _138435 = 1024 <= converge_138434; if (_138435) goto l138436; else goto l138645; l138645: ; pconverge_138438 = converge_138434; goto l138437; l138436: ; pconverge_138438 = 1023; goto l138437; l138437: ; converge_138438 = pconverge_138438; if (_138194) goto l138439; else goto l138644; l138644: ; pconverge_138441 = _138193; goto l138440; l138439: ; pconverge_138441 = 0; goto l138440; l138440: ; converge_138441 = pconverge_138441; bool _138442; _138442 = 1024 <= converge_138441; if (_138442) goto l138443; else goto l138643; l138643: ; pconverge_138445 = converge_138441; goto l138444; l138443: ; pconverge_138445 = 1023; goto l138444; l138444: ; converge_138445 = pconverge_138445; int _138446; _138446 = 1024 * converge_138445; int _138447; _138447 = _138446 + converge_138438; float* idx_138448; idx_138448 = _111058_136790 + _138447; _138451 = __ldg(idx_138448); p_138451 = _138451; l138449: ; _138451 = p_138451; if (_137125) goto l138452; else goto l138642; l138642: ; pconverge_138454 = _137124; goto l138453; l138452: ; pconverge_138454 = 0; goto l138453; l138453: ; converge_138454 = pconverge_138454; bool _138455; _138455 = 1024 <= converge_138454; if (_138455) goto l138456; else goto l138641; l138641: ; pconverge_138458 = converge_138454; goto l138457; l138456: ; pconverge_138458 = 1023; goto l138457; l138457: ; converge_138458 = pconverge_138458; if (_138194) goto l138459; else goto l138640; l138640: ; pconverge_138461 = _138193; goto l138460; l138459: ; pconverge_138461 = 0; goto l138460; l138460: ; converge_138461 = pconverge_138461; bool _138462; _138462 = 1024 <= converge_138461; if (_138462) goto l138463; else goto l138639; l138639: ; pconverge_138465 = converge_138461; goto l138464; l138463: ; pconverge_138465 = 1023; goto l138464; l138464: ; converge_138465 = pconverge_138465; int _138466; _138466 = 1024 * converge_138465; int _138467; _138467 = _138466 + converge_138458; float* idx_138468; idx_138468 = _111058_136790 + _138467; _138471 = __ldg(idx_138468); p_138471 = _138471; l138469: ; _138471 = p_138471; if (_136866) goto l138472; else goto l138638; l138638: ; pconverge_138474 = _136833; goto l138473; l138472: ; pconverge_138474 = 0; goto l138473; l138473: ; converge_138474 = pconverge_138474; bool _138475; _138475 = 1024 <= converge_138474; if (_138475) goto l138476; else goto l138637; l138637: ; pconverge_138478 = converge_138474; goto l138477; l138476: ; pconverge_138478 = 1023; goto l138477; l138477: ; converge_138478 = pconverge_138478; if (_136874) goto l138479; else goto l138636; l138636: ; pconverge_138481 = gid_y_136846; goto l138480; l138479: ; pconverge_138481 = 0; goto l138480; l138480: ; converge_138481 = pconverge_138481; bool _138482; _138482 = 1024 <= converge_138481; if (_138482) goto l138483; else goto l138635; l138635: ; pconverge_138485 = converge_138481; goto l138484; l138483: ; pconverge_138485 = 1023; goto l138484; l138484: ; converge_138485 = pconverge_138485; int _138486; _138486 = 1024 * converge_138485; int _138487; _138487 = _138486 + converge_138478; float* idx_138488; idx_138488 = _111058_136790 + _138487; _138491 = __ldg(idx_138488); p_138491 = _138491; l138489: ; _138491 = p_138491; float diff_138492; diff_138492 = _138471 - _138491; float _138493; _138493 = -2.000000e-02f * diff_138492; float _138494; _138494 = _138493 * diff_138492; s_138497 = expf(_138494); ps_138497 = s_138497; l138495: ; s_138497 = ps_138497; if (_137125) goto l138498; else goto l138634; l138634: ; pconverge_138500 = _137124; goto l138499; l138498: ; pconverge_138500 = 0; goto l138499; l138499: ; converge_138500 = pconverge_138500; bool _138501; _138501 = 1024 <= converge_138500; if (_138501) goto l138502; else goto l138633; l138633: ; pconverge_138504 = converge_138500; goto l138503; l138502: ; pconverge_138504 = 1023; goto l138503; l138503: ; converge_138504 = pconverge_138504; if (_138194) goto l138505; else goto l138632; l138632: ; pconverge_138507 = _138193; goto l138506; l138505: ; pconverge_138507 = 0; goto l138506; l138506: ; converge_138507 = pconverge_138507; bool _138508; _138508 = 1024 <= converge_138507; if (_138508) goto l138509; else goto l138631; l138631: ; pconverge_138511 = converge_138507; goto l138510; l138509: ; pconverge_138511 = 1023; goto l138510; l138510: ; converge_138511 = pconverge_138511; int _138512; _138512 = 1024 * converge_138511; int _138513; _138513 = _138512 + converge_138504; float* idx_138514; idx_138514 = _111058_136790 + _138513; _138517 = __ldg(idx_138514); p_138517 = _138517; l138515: ; _138517 = p_138517; float _138585; _138585 = 8.208500e-02f * s_138165; float _138567; _138567 = 6.065310e-01f * s_137767; float _138597; _138597 = 8.208500e-02f * s_138431; float _138594; _138594 = 1.353350e-01f * s_138365; float _138582; _138582 = 3.678790e-01f * s_138099; float _138527; _138527 = 8.208500e-02f * s_136967; float _138576; _138576 = 3.678790e-01f * s_137967; float _138579; _138579 = 6.065310e-01f * s_138033; float _138557; _138557 = 1.353350e-01f * s_137569; float _138564; _138564 = 1.000000e+00f * s_137701; float _138583; _138583 = _138582 * _138119; float _138591; _138591 = 8.208500e-02f * s_138299; float _138592; _138592 = _138591 * _138319; float _138560; _138560 = 6.065310e-01f * s_137635; float _138523; _138523 = 1.831600e-02f * s_136898; float _138537; _138537 = 1.831600e-02f * s_137171; float _138540; _138540 = 8.208500e-02f * s_137239; float _138558; _138558 = _138557 * _137589; float _138573; _138573 = 8.208500e-02f * s_137901; float _138561; _138561 = _138560 * _137655; float _138548; _138548 = 6.065310e-01f * s_137371; float _138586; _138586 = _138585 * _138185; int _138518; _138518 = 1024 * gid_y_136846; float _138544; _138544 = 3.678790e-01f * s_137305; float _138577; _138577 = _138576 * _137987; float _138528; _138528 = _138527 * _136987; float _138570; _138570 = 1.353350e-01f * s_137833; float _138600; _138600 = 1.831600e-02f * s_138497; float _138534; _138534 = 8.208500e-02f * s_137102; float _138598; _138598 = _138597 * _138451; float _138551; _138551 = 3.678790e-01f * s_137437; float _138603; _138603 = 0.000000e+00f + _138523; float _138588; _138588 = 1.831600e-02f * s_138233; float _138545; _138545 = _138544 * _137325; float _138574; _138574 = _138573 * _137921; float _138595; _138595 = _138594 * _138385; float _138538; _138538 = _138537 * _137191; int _138519; _138519 = _138518 + _136833; float _138568; _138568 = _138567 * _137787; float _138549; _138549 = _138548 * _137391; float _138554; _138554 = 8.208500e-02f * s_137503; float _138531; _138531 = 1.353350e-01f * s_137033; float _138604; _138604 = _138603 + _138527; float _138580; _138580 = _138579 * _138053; float _138565; _138565 = _138564 * _137721; float _138524; _138524 = _138523 * _136918; float _138541; _138541 = _138540 * _137259; float _138571; _138571 = _138570 * _137853; float _138601; _138601 = _138600 * _138517; float _138535; _138535 = _138534 * _137122; float _138552; _138552 = _138551 * _137457; float _138589; _138589 = _138588 * _138253; float* idx_138520; idx_138520 = _111059_136791 + _138519; float _138555; _138555 = _138554 * _137523; float _138605; _138605 = _138604 + _138531; float _138532; _138532 = _138531 * _137053; float _138525; _138525 = 0.000000e+00f + _138524; float _138606; _138606 = _138605 + _138534; float _138529; _138529 = _138525 + _138528; float _138607; _138607 = _138606 + _138537; float _138533; _138533 = _138529 + _138532; float _138608; _138608 = _138607 + _138540; float _138536; _138536 = _138533 + _138535; float _138609; _138609 = _138608 + _138544; float _138539; _138539 = _138536 + _138538; float _138610; _138610 = _138609 + _138548; float _138542; _138542 = _138539 + _138541; float _138611; _138611 = _138610 + _138551; float _138546; _138546 = _138542 + _138545; float _138612; _138612 = _138611 + _138554; float _138550; _138550 = _138546 + _138549; float _138613; _138613 = _138612 + _138557; float _138553; _138553 = _138550 + _138552; float _138614; _138614 = _138613 + _138560; float _138556; _138556 = _138553 + _138555; float _138615; _138615 = _138614 + _138564; float _138559; _138559 = _138556 + _138558; float _138616; _138616 = _138615 + _138567; float _138562; _138562 = _138559 + _138561; float _138617; _138617 = _138616 + _138570; float _138566; _138566 = _138562 + _138565; float _138618; _138618 = _138617 + _138573; float _138569; _138569 = _138566 + _138568; float _138619; _138619 = _138618 + _138576; float _138572; _138572 = _138569 + _138571; float _138620; _138620 = _138619 + _138579; float _138575; _138575 = _138572 + _138574; float _138621; _138621 = _138620 + _138582; float _138578; _138578 = _138575 + _138577; float _138622; _138622 = _138621 + _138585; float _138581; _138581 = _138578 + _138580; float _138623; _138623 = _138622 + _138588; float _138584; _138584 = _138581 + _138583; float _138624; _138624 = _138623 + _138591; float _138587; _138587 = _138584 + _138586; float _138625; _138625 = _138624 + _138594; float _138590; _138590 = _138587 + _138589; float _138626; _138626 = _138625 + _138597; float _138593; _138593 = _138590 + _138592; float _138627; _138627 = _138626 + _138600; float _138596; _138596 = _138593 + _138595; float _138599; _138599 = _138596 + _138598; float _138602; _138602 = _138599 + _138601; float _138628; _138628 = _138602 / _138627; *idx_138520 = _138628; return ; } }
21,546
#include <stdio.h> #include <assert.h> #include <cuda_runtime_api.h> #include <cuda.h> #include <cstdlib> #include <ctime> #include <iostream> __global__ void kernel(int * a) { int idx = blockIdx.x*blockDim.x +threadIdx.x; a[idx] = 7; } __global__ void kernelByThreadId(int * a) { int idx = blockIdx.x*blockDim.x +threadIdx.x; a[idx] = threadIdx.x; } __global__ void kernelByBlockId(int * a) { int idx = blockIdx.x*blockDim.x +threadIdx.x; a[idx] = blockIdx.x; } int main(void) { int N = 10; int *host_vector; int *dev_vector; dim3 blockSize(3,3,3); dim3 gridSize(1,1); //first experiment host_vector = (int*) malloc(N*sizeof(int)); for(int ii = 0; ii < N ;ii++) host_vector[ii] = 0; cudaMalloc((void**)&dev_vector,N*sizeof(int)); cudaMemcpy(dev_vector,host_vector,N*sizeof(int),cudaMemcpyHostToDevice); kernel<<<gridSize,blockSize>>>(dev_vector); cudaMemcpy(host_vector,dev_vector,N*sizeof(int),cudaMemcpyDeviceToHost); printf("first experiment\n"); for(int ii = 0; ii < N ;ii++) printf("%d \n",host_vector[ii]); free(host_vector); cudaFree(dev_vector); //second experiment host_vector = (int*) malloc(N*sizeof(int)); for(int ii = 0; ii < N ;ii++) host_vector[ii] = 0; cudaMalloc((void**)&dev_vector,N*sizeof(int)); cudaMemcpy(dev_vector,host_vector,N*sizeof(int),cudaMemcpyHostToDevice); kernelByBlockId<<<gridSize,blockSize>>>(dev_vector); cudaMemcpy(host_vector,dev_vector,N*sizeof(int),cudaMemcpyDeviceToHost); printf("second experiment\n"); for(int ii = 0; ii < N ;ii++) printf("%d \n",host_vector[ii]); free(host_vector); cudaFree(dev_vector); //third experiment host_vector = (int*) malloc(N*sizeof(int)); for(int ii = 0; ii < N ;ii++) host_vector[ii] = 0; cudaMalloc((void**)&dev_vector,N*sizeof(int)); cudaMemcpy(dev_vector,host_vector,N*sizeof(int),cudaMemcpyHostToDevice); kernelByThreadId<<<gridSize,blockSize>>>(dev_vector); cudaMemcpy(host_vector,dev_vector,N*sizeof(int),cudaMemcpyDeviceToHost); printf("third experiment\n"); for(int ii = 0; ii < N ;ii++) printf("%d \n",host_vector[ii]); free(host_vector); cudaFree(dev_vector); }
21,547
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <stdlib.h> __global__ void func4(int* a, int* t) { int n = threadIdx.x; int m = blockIdx.x; int size = blockDim.x; int size1 = gridDim.x; int dx = a[m*size+n]; if ((n != 0) && (m != 0) && (n != size-1) && (m != size1-1)) { int gx; int ax = 0; int fac = 1; while (dx != 0) { gx = dx%2; if (gx == 0) { ax += fac; } fac *= 10; dx /= 2; } t[m*size+n] = ax; } else { t[m*size+n] = dx; } } int main(void) { int *a,*t,m,n,i,j; int *d_a,*d_t; printf("Enter value of m: "); scanf("%d",&m); printf("Enter value of n: "); scanf("%d",&n); int size = sizeof(int)*m*n; a = (int*)malloc(m*n*sizeof(int)); t = (int*)malloc(m*n*sizeof(int)); printf("Enter input matrix:\n"); for (i = 0; i < m*n; i++) { scanf("%d",&a[i]); } cudaMalloc((void**)&d_a,size); cudaMalloc((void**)&d_t,size); cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice); func4<<<m,n>>>(d_a,d_t); cudaMemcpy(t,d_t,size,cudaMemcpyDeviceToHost); printf("The result vector is:\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("%d\t",t[i*n+j]); } printf("\n"); } getchar(); cudaFree(d_a); cudaFree(d_t); return 0; }
21,548
#include <iostream> #include <cstdio> #include <cuda_runtime.h> #include <device_launch_parameters.h> #include <cmath> using namespace std; const int TILE_WIDTH = 16; __global__ void MatrixMulKernel(int *d_M,int *d_N,int *d_P,int m,int n,int k) { __shared__ int ds_M[TILE_WIDTH][TILE_WIDTH]; __shared__ int ds_N[TILE_WIDTH][TILE_WIDTH]; int bx = blockIdx.x; int by = blockIdx.y; int tx = threadIdx.x; int ty = threadIdx.y; //Identify the row and column of the Pd element to work on int row = by * TILE_WIDTH + ty; int col = bx * TILE_WIDTH + tx; int pValue = 0; //loop over the Md and Nd tiles required to comput the Pd element for(int t = 0; t < (n-1) / TILE_WIDTH + 1; ++t) { if(row < m && t * TILE_WIDTH + tx < n) ds_M[ty][tx] = d_M[row * n + t * TILE_WIDTH + tx]; else ds_M[ty][tx] = 0; if(col < k && t * TILE_WIDTH + ty < n) ds_N[ty][tx] = d_N[(t * TILE_WIDTH + ty) * k + col]; else ds_N[ty][tx] = 0; __syncthreads(); for(int i = 0; i < TILE_WIDTH; ++i) pValue += ds_M[ty][i] * ds_N[i][tx]; __syncthreads(); } if(row < m && col < k) d_P[row * k + col] = pValue; } int main() { //freopen("out","w",stdout); int m = 600, n = 700, k = 1000; int *h_M, *h_N, *d_M, *d_N; int *h_P, *d_P; size_t sizeM = m * n * sizeof(int); size_t sizeN = n * k * sizeof(int); size_t sizeP = m * k * sizeof(int); h_M = (int *) malloc(sizeM); h_N = (int *) malloc(sizeN); h_P = (int *) malloc(sizeP); cudaMalloc(&d_M,sizeM); cudaMalloc(&d_N,sizeN); cudaMalloc(&d_P,sizeP); for(int i = 0; i < m * n; ++i) { if(i % 2 == 0) h_M[i] = 1; else h_M[i] = 0; } for(int i = 0;i < n * k; ++i) { if(i % 2 == 0) h_N[i] = 0; else h_N[i] = 1; } cudaMemcpy(d_M,h_M,sizeM,cudaMemcpyHostToDevice); cudaMemcpy(d_N,h_N,sizeN,cudaMemcpyHostToDevice); cudaEvent_t start,stop; cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start,0); dim3 grid((int)ceil(k*1.0 / TILE_WIDTH), (int)ceil(m*1.0/ TILE_WIDTH)); dim3 block(TILE_WIDTH,TILE_WIDTH); MatrixMulKernel<<<grid,block>>>(d_M,d_N,d_P,m,n,k); cudaEventRecord(stop,0); //cudaDeviceSynchronize(); cudaEventSynchronize(stop); float ElapsedTime; cudaEventElapsedTime(&ElapsedTime,start,stop); printf("Kernel Elpased Time: %.3f ms\n",ElapsedTime); cudaMemcpy(h_P,d_P,sizeP,cudaMemcpyDeviceToHost); /* for(int i = 0; i < m * k; ++i) printf("%d\n",h_P[i]); printf("\n"); */ return 0; }
21,549
#include <stdio.h> __device__ const char *STR = "HELLO WORLD!"; const char STR_LENGTH = 12; __global__ void hello() { printf("%c\n",STR[threadIdx.x % STR_LENGTH]); } int main(void) { int num_threads = STR_LENGTH; int num_blocks = 2; dim3 dimBlock(num_threads); dim3 dimGrid(1,1); hello<<<dimGrid,dimBlock>>>(); cudaDeviceSynchronize(); return 0; }
21,550
#include <thrust/host_vector.h> #include <thrust/device_vector.h> #include <thrust/transform.h> #include <thrust/functional.h> #include <thrust/for_each.h> #include <thrust/iterator/zip_iterator.h> #include <thrust/copy.h> #include <thrust/reduce.h> #include <thrust/for_each.h> #include <thrust/execution_policy.h> #include <iostream> //to compile: "/usr/local/cuda-5.5/bin/nvcc SmoothT.cu" struct smoother_functor { const float h; const float x_i; smoother_functor(float _h, float _x_i): h(_h), x_i(_x_i){}//constructor __host__ __device__ float operator()(const float&x, const float& y)const //FILL IN FROM TUTORIAL { return (fabs(x-x_i) < (float)h) ? 1.0 : 0.0;//1=avg, 0= dont avg val } }; //struct smoother_functor void smootht(float *x, float *y, float *m, int n, float h) { thrust::device_vector<float> dx(x,x+n); //x=pointer to x array, x+n end of array thrust::device_vector<float> dy(y,y+n); thrust::device_vector<float> dm(n,0.0);//create a vector of length n, init all to zero thrust::device_vector<float> dtemp(n,0.0); thrust::device_vector<float> dtemp2(n,0.0); //iterate through x for x_i, pass into smoother_functor... for (int i = 0; i < dx.size(); i++) { float total = 0.0; int count = 0; thrust::transform(dx.begin(),dx.end(), dy.begin(), dtemp.begin(),smoother_functor(h,*(dx.begin()+i))); //takes dx vector array and dy array, manipulate dx and stores to dtepm 4, smoother_func in line 19 //std::cout << "CHECK AVERAGING" << std::endl; //calculate averages thrust::transform(dy.begin(), dy.end(), dtemp.begin(), dtemp2.begin(), thrust::multiplies<float>()); total = thrust::reduce(dtemp2.begin(),dtemp2.end()); count = thrust::reduce(dtemp.begin(),dtemp.end()); *(dm.begin()+i) = (total/count); //std::cout << total << " " << count << " " << total/count << std::endl; }//end for i //copy and print //thrust::copy(dm.begin(), dm.end(), std::ostream_iterator<float>(std::cout, "\n")); for(int i = 0; i < dm.size(); i++) { m[i] = *(dm.begin()+i); //std::cout << m[i] << std::endl; } }//smootht //int main() //{ /* int a, n = 2000000; //took away 0 zeros //2000000 takes 11 minutes plus! //200000 takes ~1 minute float * x = new float[n]; float * y = new float[n]; float * m = new float[n]; float h = 2; a=rand();//range of float [-a, a] srand(time(NULL));//init rand() seed for (int i=0; i<n; i++) { x[i] = ((float)rand()/(float)(RAND_MAX)*2*a - a); y[i] = ((float)rand()/(float)(RAND_MAX)*2*a - a); }//generate random floats for x and y */ /* float x[20] = {1, 1,2,2, 3,3, 4,4, 5,5, 6,6, 7,7, 8,8, 9,9, 10,10}; float y[20] = {11,11, 12,12, 13,13, 14,14, 15,15, 16,16, 17,17, 18,18, 19,19, 20,20}; float m[20]; int n = 20; float h = 2; */ //smootht(x, y, m, n, h); //}//main
21,551
#include "kernels.cuh" #include<stdio.h> __global__ void init_primes_kernel(int *primes, unsigned int N){ unsigned int index = threadIdx.x + blockIdx.x * blockDim.x; unsigned int stride = gridDim.x * blockDim.x; unsigned int offset = 0; while(index + offset < N){ primes[index + offset] = index +offset + 1; offset +=stride; } }//end init primes_kenel __global__ void sieve_of_eratosthenes_kernel(int *primes, unsigned int N, unsigned int sqrtRootN){ unsigned int index = threadIdx.x + blockIdx.x * blockDim.x + 2; unsigned int stride = gridDim.x * blockDim.x; unsigned int offset = 0; // __shared__ unsigned int cache[N]; while(index + offset <= sqrtRootN){ unsigned int temp = index + offset; for(unsigned int i = temp * temp; i < N; i += temp){ primes[i-1] = 0; } offset += stride; } } //TO DO __global__ void sieve_of_eratosthenes_kernel2(){ }
21,552
#include "includes.h" __global__ void build_sequence_length_padding_offset(const int* sequence_length, const int batch_size, const int max_seq_len, int* valid_word_num, int* tmp_mask_offset) { // do cumulated sum int total_seq_len = 0; int cum_offset = 0; int index = 0; for(int i = 0; i < batch_size; i++) { const int seq_len = sequence_length[i]; for(int j = 0; j < seq_len; j++) { tmp_mask_offset[index] = cum_offset; index++; } cum_offset += max_seq_len - seq_len; total_seq_len += seq_len; } valid_word_num[0] = total_seq_len; }
21,553
/* * Instituto Tecnologico de Costa Rica * Centro de Investigaciones en Computacion * * Asesoria-Practica en Computacion Paralela * Instituto Costarricense de Electricidad * Julio-Agosto 2011 * * Autor: Santiago Nunez Corrales * Programa: suma de vectores en CUDA C */ #include <stdio.h> #define DEF_TAM 10 __global__ void suma_vec(float *a, float *b, float *c) { /* threadIdx.x es el identificador del hilo en la dimension x */ int idx = threadIdx.x; a[idx] = 0; b[idx] = idx; c[idx] = a[idx] + b[idx]; } int revisar_gpu(int disp_gpu) { int total_gpu = 0; int id_gpu; cudaGetDeviceCount(&total_gpu); if (disp_gpu > total_gpu) { printf("disp_gpu >= total_gpu ... terminando.\n"); exit(1); } cudaError_t ret_cuda; cudaDeviceProp props; cudaGetDeviceProperties(&props, disp_gpu); printf("[props.major.props.minor] = [%d.%d]\n", props.major, props.minor); if (props.major > 999) { printf("CUDA Device Emulation (CPU) detectado, terminando.\n"); exit(1); } ret_cuda = cudaSetDevice(disp_gpu); if (ret_cuda == cudaErrorInvalidDevice) { perror("cudaSetDevice retorno cudaErrorInvalidDevice"); } else { cudaGetDevice(&id_gpu); printf("cudaGetDevice()=%d\n",id_gpu); } } int main (int argc, char *argv[]) { int n; int i; float a[DEF_TAM]; float b[DEF_TAM]; float c[DEF_TAM]; float *ptr_dev_a; float *ptr_dev_b; float *ptr_dev_c; int mem_reservada; revisar_gpu(0); n = DEF_TAM; mem_reservada = DEF_TAM * sizeof(float); cudaMalloc((void **)&ptr_dev_a, mem_reservada); cudaMalloc((void **)&ptr_dev_b, mem_reservada); cudaMalloc((void **)&ptr_dev_c, mem_reservada); cudaMemcpy(ptr_dev_a, a, mem_reservada, cudaMemcpyHostToDevice); cudaMemcpy(ptr_dev_b, b, mem_reservada, cudaMemcpyHostToDevice); suma_vec<<<1, n>>>(ptr_dev_a, ptr_dev_b, ptr_dev_c); cudaMemcpy(c, ptr_dev_c, mem_reservada, cudaMemcpyDeviceToHost); for (i = 0; i < DEF_TAM; i++) printf("c[%d] = %f\n", i, c[i]); cudaFree(ptr_dev_a); cudaFree(ptr_dev_b); cudaFree(ptr_dev_c); return 0; }
21,554
// Josh Shepherd - 1700471 #include <stdio.h> #include <stdlib.h> #include <string.h> __device__ char* CudaCrypt(char* rawPassword) { char * newPassword = (char *) malloc(sizeof(char) * 11); newPassword[0] = rawPassword[0] + 2; newPassword[1] = rawPassword[0] - 2; newPassword[2] = rawPassword[0] + 1; newPassword[3] = rawPassword[1] + 3; newPassword[4] = rawPassword[1] - 3; newPassword[5] = rawPassword[1] - 1; newPassword[6] = rawPassword[2] + 2; newPassword[7] = rawPassword[2] - 2; newPassword[8] = rawPassword[3] + 4; newPassword[9] = rawPassword[3] - 4; newPassword[10] = '\0'; for(int i =0; i<10; i++){ if(i >= 0 && i < 6){ //checking all lower case letter limits if(newPassword[i] > 122){ newPassword[i] = (newPassword[i] - 122) + 97; }else if(newPassword[i] < 97){ newPassword[i] = (97 - newPassword[i]) + 97; } }else{ //checking number section if(newPassword[i] > 57){ newPassword[i] = (newPassword[i] - 57) + 48; }else if(newPassword[i] < 48){ newPassword[i] = (48 - newPassword[i]) + 48; } } } return newPassword; } /** Checks if one char string matches another char string of the length */ __device__ int isEncryptedMatching(char* one, char* two, int length) { int result = 1; for (int i = 0; i < length; i++) { if (one[i] != two[i]) { result = 0; break; } } return result; } /** Decrypts a pass using a CUDA thread */ __global__ void decryptPass(char* alphabet, char* numbers, char* encryptedPass, char* deviceOutputPass) { /// Get the unique cuda thread id int uid = blockDim.x * blockIdx.x + threadIdx.x; /// Check if another thread found output pass before starting if(*deviceOutputPass != NULL) { //printf("OutputPass not null! Cancelling CUDA thread '%d'\n", uid); return; } /// Create potential pass to check on this thread char potentialPass[4]; potentialPass[0] = alphabet[blockIdx.x]; potentialPass[1] = alphabet[blockIdx.y]; potentialPass[2] = numbers[threadIdx.x]; potentialPass[3] = numbers[threadIdx.y]; /// Encrypt the potential password char* encryptedPotential; encryptedPotential = CudaCrypt(potentialPass); //printf("UID: '%d' Plain: '%s' Encrypted Plain: '%s' Target Encrypted: '%s'\n", uid, potentialPass, encryptedPotential, encryptedPass); /// Check the current potential pass is matches the target encryptedPass if ( isEncryptedMatching(encryptedPass, encryptedPotential, 11) > 0 ) { /// Matches so set deviceOutputPass to the current combination printf("UID '%d' Encrypted pass '%s' from combination '%s' matches potential pass = '%s'\n", uid, encryptedPass, potentialPass, encryptedPotential); for (int i = 0; i < 4; i++ ) { deviceOutputPass[i] = potentialPass[i]; } } } /** */ int main(int argc, char** argv) { printf("Josh Shepherd - 1700471\n\n"); /// Get the encrypted pass to decrypt /* Test Encrypted Passwords: az01 = ccbdwy2253 aa52 = ccbddb7362 */ char* encryptedPass = "ccbddb7362"; if (argc > 1) { encryptedPass = argv[1]; } printf("Pass: '%s'\n", encryptedPass); // Init alphabet and numbers array to read only use in cuda char cpuAlphabet[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' }; char cpuNumbers[10] = { '0', '1', '2', '3', '4', '5', '6' ,'7', '8', '9' }; int sizeOfEncryptedPass = sizeof(char) * 11; char* gpuAlphabet; cudaMalloc( (void**) &gpuAlphabet, sizeof(char) * 26 ); cudaMemcpy( gpuAlphabet, cpuAlphabet, sizeof(char) * 26, cudaMemcpyHostToDevice ); char* gpuNumbers; cudaMalloc( (void**) &gpuNumbers, sizeof(char) * 10 ); cudaMemcpy( gpuNumbers, cpuNumbers, sizeof(char) * 10, cudaMemcpyHostToDevice ); char* gpuEncryptedPass; cudaMalloc( (void**) &gpuEncryptedPass, sizeOfEncryptedPass ); cudaMemcpy( gpuEncryptedPass, encryptedPass, sizeOfEncryptedPass, cudaMemcpyHostToDevice); char* gpuOutputPass; cudaMalloc( (void**) &gpuOutputPass, sizeOfEncryptedPass ); /// Launch cuda threads and await finish decryptPass<<< dim3(26, 26, 1), dim3(10, 10, 1) >>>(gpuAlphabet, gpuNumbers, gpuEncryptedPass, gpuOutputPass); cudaDeviceSynchronize(); printf("Finished synchronizing CUDA threads\n"); /// Copy GPU output pass to the CPU char* cpuOutputPass = (char*)malloc( sizeof(char) * 4 ); cudaMemcpy(cpuOutputPass, gpuOutputPass, sizeOfEncryptedPass, cudaMemcpyDeviceToHost); /// If output pass contained an output, print the results printf("---\n"); printf("Results:\n"); if (cpuOutputPass != NULL && cpuOutputPass[0] != 0) { printf("Given Encrypted Pass: '%s'\n", encryptedPass); printf("Found Decrypted Pass: '%s'\n", cpuOutputPass); } else { printf("Unable to determine a password.\n"); } /// Free any malloc'd memory cudaFree(gpuAlphabet); cudaFree(gpuNumbers); cudaFree(gpuEncryptedPass); cudaFree(gpuOutputPass); free(cpuOutputPass); }
21,555
#include <stdio.h> #include <math.h> #define THREADS 256 #define MINARG 2 #define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ )) static void HandleError( cudaError_t err, const char *file, int line ) { if (err != cudaSuccess) { printf( "%s in %s at line %d\n", cudaGetErrorString( err ),file, line ); exit( EXIT_FAILURE ); } } void startTimer(cudaEvent_t *start, cudaEvent_t *stop) { HANDLE_ERROR( cudaEventCreate(start)); HANDLE_ERROR( cudaEventCreate(stop)); HANDLE_ERROR( cudaEventRecord(*start, 0)); } void stopAndPrint(cudaEvent_t *start, cudaEvent_t *stop) { HANDLE_ERROR( cudaEventRecord(*stop, 0)); HANDLE_ERROR( cudaEventSynchronize(*stop)); float time=0; HANDLE_ERROR( cudaEventElapsedTime(&time, *start, *stop)); printf("Elapsed Time: %f in milliseconds\n", time); HANDLE_ERROR( cudaEventDestroy(*start)); HANDLE_ERROR( cudaEventDestroy(*stop)); } void print(int *array, int size){ int i =0; int c = 0; for (i=0;i<size;i++){ if (array[i]) { // printf("%d\n", i+1); c++; } } printf("Total number of primes: %d\n", c); } __global__ void eliminateMultiples(int *list, int end, int *next, int fine) { //caso di un singolo blocco unsigned int start; do { start = (*next)*(threadIdx.x + 2); for(int i = start-1; i < end; i += (*next)*blockDim.x) { //elimino i multipli list[i] = 0; } __syncthreads(); if(threadIdx.x == 0) { unsigned int j; bool found = false; //cambio il next if(*next == 2) { j = *next; } else j = *next + 1; for(; j < end && found == false; j+=2) { if(list[j] > *next) { *next = list[j]; found = true; } } } __syncthreads(); } while(*next < fine); } void findAllPrimeNumbers(int N){ //Definisco il numero di blocchi if(N%2) { N+=1; } //int blocks = (((N-2)/2)+(THREADS-1))/THREADS; int blocks = 1; //Variabili GPU int *dev_list, *dev_next; //Variabili CPU int *list = new int[N]; int next = 2; for(int i=0; i<N; i++) { list[i]=i+1; } int fine = (int) (sqrt(N)+0.5); //Timer cudaEvent_t start,stop; //Allocazione su GPU //cudaMalloc((void**)&dev_end, sizeof(int)); cudaMalloc((void**)&dev_next,sizeof(int)); cudaMalloc((void**)&dev_list,sizeof(int)*N); //Copia dati sulla GPU cudaMemcpy(dev_list,list, sizeof(int)*N, cudaMemcpyHostToDevice); cudaMemcpy(dev_next, &next, sizeof(int), cudaMemcpyHostToDevice); //Inizializzazione del Timer startTimer(&start,&stop); //Lancio del Kernel eliminateMultiples<<<blocks,THREADS>>>(dev_list, N, dev_next, fine); cudaDeviceSynchronize(); //Fine del timer stopAndPrint(&start,&stop); //Ricopio il risultato sulla GPU cudaMemcpy(list, dev_list, sizeof(int)*N, cudaMemcpyDeviceToHost); //DEBUG cudaMemcpy(&next, dev_next, sizeof(int), cudaMemcpyDeviceToHost); printf("next := %d\n", next); //FINE DEBUG //Libero Memoria cudaFree(dev_list); cudaFree(dev_next); //Stampo informazioni printf("Number of threads: %d, Number of blocks: %d\n",THREADS,blocks); print(list, N); delete[] list; } int main(int argc, char *argv[]) { if(argc<MINARG) { fprintf(stderr,"Usage: %s N\n",argv[0]); exit(-1); } int N = atoi(argv[1]); if(N<0) { fprintf(stderr,"Invalid number: %d must be > 0\n",N); exit(-1); } printf("Prime numbers from 0 to %d:\n",N); findAllPrimeNumbers(N); return 0; }
21,556
#include<stdlib.h> #include<math.h> #include<iostream> #include<time.h> #define BLOCK_DIM_X 16 #define BLOCK_DIM_Y 16 using namespace std; __global__ void dirihle (float* u, float* f, float* v, int N, float h2, int rb, float* eps) { __shared__ float s_u[BLOCK_DIM_X + 2][BLOCK_DIM_Y + 2]; int i = blockIdx.x*blockDim.x + threadIdx.x; int j = blockIdx.y*blockDim.y + threadIdx.y; int tx = threadIdx.x+1; int ty = threadIdx.y+1; int k = j*N + i; if ( i < N && j < N ) { s_u[ty][tx] = u[k]; if ( ty == 1 && j > 0 ) s_u[ty-1][tx] = u[k-N]; if ( ty == BLOCK_DIM_X && j < N-1 ) s_u[ty+1][tx] = u[k+N]; if ( tx == 1 && i > 0 ) s_u[ty][tx-1] = u[k-1]; if ( tx == BLOCK_DIM_Y && i < N-1 ) s_u[ty][tx+1] = u[k+1]; if ( i == 0 ) s_u[ty][tx-1] = u[k+N-2]; if( i == N-1 ) s_u[ty][tx+1] = u[k-N+2]; } __syncthreads(); eps[0] = 0; if ( (i > 0 ) && ( i < N-1 ) && ( j > 0 ) && ( j < N-1 ) && ( k < N*N ) && ( i + j )%2 == rb ) { u[k] = 0.25*(s_u[ty-1][tx] + s_u[ty+1][tx] + s_u[ty][tx-1] + s_u[ty][tx+1] - h2*f[k]); } if ( eps[0] < abs(v[k] - u[k] )){ eps[0] = abs(v[k] - u[k]); } } int main( int argc, char * argv [] ) { int rows = 256; int count = 1; float* eps1; float* eps; float h =(float)1/(rows-1); int numBytes = sizeof(float)*rows*rows; float* u; float* f; float* v; u = ( float* )malloc( numBytes ); f = ( float* )malloc( numBytes ); v = ( float* )malloc( numBytes ); eps = ((float*)malloc(sizeof(float))); eps1 = ((float*)malloc(sizeof(float))); eps[0] = 0; for ( int i = 0; i < rows; i++ ) for ( int j = 0; j < rows; j++ ) { float x = i*h; float y = j*h; f[i*rows + j] =4 + 2*x*x - 2*x + 2*y*y - 2*y; u[i*rows + j] = 0; v[i*rows + j] = (x*x - x + 1)*(y*y - y + 1); } for ( int i = 0; i < rows; i++ ) { float x = i*h; u[i*rows] = x*x - x + 1; u[i] = x*x - x + 1; u[i*rows+(rows-1)] = x*x - x + 1; u[(rows-1)*rows+i] = x*x - x + 1; } // allocate device memory float * devU = NULL; float * devV = NULL; float * devF = NULL; float * devE = NULL; cudaMalloc ( (void**)&devU, numBytes ); cudaMalloc ( (void**)&devV, numBytes ); cudaMalloc ( (void**)&devF, numBytes ); cudaMalloc ((void**)&devE, sizeof(float)); //set kernel launch configuration dim3 grid = dim3(16, 16); dim3 blocks = dim3(16, 16); cudaMemcpy ( devU, u, numBytes, cudaMemcpyHostToDevice); cudaMemcpy ( devV, v, numBytes, cudaMemcpyHostToDevice); cudaMemcpy ( devF, f, numBytes, cudaMemcpyHostToDevice); cudaMemcpy ( devE, eps, sizeof(float), cudaMemcpyHostToDevice); clock_t t1, t2; t1 = clock(); do{ dirihle<<<grid, blocks>>>(devU, devF, devV, rows, h*h, 0, devE); cudaMemcpy(eps1, devE, sizeof(float), cudaMemcpyDeviceToHost); dirihle<<<grid, blocks>>>(devU, devF, devV, rows, h*h, 1, devE); cudaMemcpy(eps, devE, sizeof(float), cudaMemcpyDeviceToHost); cerr<<count<<" "<<eps[0]<<" "<<eps1[0]<<endl; count++; } while (count < 35300 || eps[0] > 0.005 || eps1[0] > 0.005); cudaMemcpy(u,devU,numBytes,cudaMemcpyDeviceToHost); t2 = clock(); cerr<<" "<<((float)(t2 - t1))/(CLOCKS_PER_SEC)<<" sec" <<endl; for (int i = 0; i < rows; i++ ) for ( int j = 0; j < rows; j++ ) { cout << i*h << " " << j*h << " " << u[i*rows+j] <<endl; } delete [] u; delete [] f; delete [] v; delete [] eps; cudaFree ( devU ); cudaFree ( devV ); cudaFree ( devF ); cudaFree ( devE ); return 0; }
21,557
#include <stdio.h> #include <cuda_runtime.h> __global__ void helloCUDA(float e){ printf("Soy el hilo %d del bloque %d con valor e=%f\n",threadIdx.x,blockIdx.x,e); } int main(void){ printf("\nHello World\n"); helloCUDA<<<3,4>>>(2.5f); return(0); }
21,558
#include <cstdio> extern "C" { __device__ static int THREADS_IN_BLOCK = 1024; //index - size of left tree __global__ void init(int* parent, int* right, int* left, int* tree_size, int* height, bool* computed, int size, int* roots) { int thid = blockIdx.x * blockDim.x + threadIdx.x; if (thid >= size) { return; } // root = blockIdx.x * blockDim.x + root; // if (root >= size) { // root = blockIdx.x * blockDim.x; // } // __syncthreads(); atomicExch(roots + blockIdx.x, thid); __syncthreads(); int root = roots[blockIdx.x]; if (thid == root) { computed[thid] = true; parent[thid] = -1; height[thid] = 0; } else { computed[thid] = false; parent[thid] = root; } tree_size[thid] = 1; left[thid] = right[thid] = -1; } __global__ void quick_sort(int* to_sort, int* parent, int* left, int* right, int* tree_size, int* height, bool* computed, bool* sth_changed, int size) { int thid = blockIdx.x * blockDim.x + threadIdx.x; if (thid >= size || computed[thid]) { return; } *sth_changed = true; //I assume that I have parrent int parent_id = parent[thid]; int my_value = to_sort[thid]; int parent_value = to_sort[parent_id]; bool is_left = false; if ( my_value < parent_value || ( my_value == parent_value && thid < parent_id)) { is_left = true; } // __syncthreads(); if (is_left) atomicExch(left + parent_id, thid); __syncthreads(); if (is_left) { int left_parent = left[parent_id]; if (thid == left_parent) { // printf("%d\n", left_parent); computed[thid] = true; height[thid] = height[parent_id] + 1; } else { parent[thid] = left_parent; atomicAdd(tree_size + left_parent, 1); } return; } // __syncthreads(); atomicExch(right + parent_id, thid); __syncthreads(); int right_parent = right[parent_id]; if (thid == right_parent) { // printf("%d\n", right_parent); computed[thid] = true; height[thid] = height[parent_id] + 1; } else { parent[thid] = right_parent; atomicAdd(tree_size + right_parent, 1); } } __global__ void tree_to_array(int* tree, int* array, int* indexes, int* parent, int* left, int* tree_size, int* height, int h, bool* sth_changed, int size) { int thid = blockIdx.x * blockDim.x + threadIdx.x; if (thid >= size || height[thid] != h) { return; } *sth_changed = true; int index_in_array = blockIdx.x * blockDim.x; int left_child = left[thid]; int parent_id = parent[thid]; //I am root if (parent_id == -1) { if (left_child != -1) { index_in_array += tree_size[left_child]; } array[index_in_array] = tree[thid]; indexes[thid] = index_in_array; return; } int left_parents_child = left[parent_id]; bool is_left = (left_parents_child == thid); if (is_left) { index_in_array = indexes[parent_id] - tree_size[thid]; } else { index_in_array = indexes[parent_id] + 1; } if (left_child != -1) { index_in_array += tree_size[left_child]; } array[index_in_array] = tree[thid]; indexes[thid] = index_in_array; } }
21,559
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <cuComplex.h> #include <math.h> #include <math_constants.h> #include <iostream> __global__ void Goertzel(int N, cuFloatComplex *device_x, cuFloatComplex *device_X_k) { int xIndex, yIndex, zIndex, n; xIndex = threadIdx.x + blockIdx.x * blockDim.x; yIndex = threadIdx.y + blockIdx.y * blockDim.y; zIndex = threadIdx.z + blockIdx.z * blockDim.z; cuFloatComplex sk1,sk2,skn,ykn; cuFloatComplex aux_1,aux_2,aux_3,aux_4,aux_5; sk1 = make_cuFloatComplex(0.0,0.0); sk2 = make_cuFloatComplex(0.0,0.0); for(n = 0; n < N; n++) { aux_1 = cuCmulf(make_cuFloatComplex((2 * cos(2*CUDART_PI_F*xIndex/N)),0.0),sk1); aux_2 = cuCaddf(device_x[n],aux_1); aux_3 = cuCsubf(aux_2,sk2); skn = aux_3; sk2 = sk1; sk1 = skn; } aux_4 = cuCmulf(make_cuFloatComplex((2 * cos(2*CUDART_PI_F*xIndex/N)),0.0),sk1); skn = cuCsubf(aux_4,sk2); aux_5 = cuCmulf(make_cuFloatComplex(cos((-2*CUDART_PI_F*xIndex)/N), sin((-2*CUDART_PI_F*xIndex)/N)),sk1); ykn = cuCsubf(skn,aux_5); device_X_k[xIndex] = ykn; } int main() { //Ingrese el tamao del vector de entrada x[n] int N = 1000; //Numero de iteraciones int j; int loop = 500; //Pausa printf("\n---PRESIONA UNA TECLA PARA CONTINUAR---\n\n"); getchar(); for(j = 0;j < loop;j++) { //Comandos necesarios para medir el tiempo de la aplicacion (app) float elapsedTime_app; cudaEvent_t start_app, stop_app; cudaEventCreate(&start_app); cudaEventCreate(&stop_app); //--------------------------------------------------------------------------------------------- //Se empieza a medir el tiempo de ejecucion de la aplicacion cudaEventRecord(start_app,0); //Comandos necesarios para medir el tiempo del kernel (kernel) float elapsedTime_kernel; cudaEvent_t start_kernel, stop_kernel; cudaEventCreate(&start_kernel); cudaEventCreate(&stop_kernel); //Declaracion de variables int i; cuFloatComplex *host_x; cuFloatComplex *host_X_k; cuFloatComplex *device_x; cuFloatComplex *device_X_k; FILE *time_app; FILE *time_kernel; //Se crean los archivos binarios donde se guardaran los tiempos time_app = fopen("time_app.bin","a+b"); time_kernel = fopen("time_kernel.bin","a+b"); //Se reserva memoria en host y device para x host_x = (cuFloatComplex*) malloc(sizeof(cuFloatComplex)*N); host_X_k = (cuFloatComplex*) malloc(sizeof(cuFloatComplex)*N); cudaMalloc((void**)&device_x,sizeof(cuFloatComplex)*N); cudaMalloc((void**)&device_X_k,sizeof(cuFloatComplex)*N); //Se dan valores al vector de entrada x[n] for(i = 0;i < N;i++ ) { host_x[i] = make_cuFloatComplex(rand()%11,rand()%21); //host_x[i] = make_cuFloatComplex(i+1,0.0); } /* //Se imprime vector de entrada x[n] printf("\n\n---VECTOR DE ENTRADA x[n]---\n"); for(i = 0;i < N;i++) { printf("\nx[%d] = (%f) + (%f)\n ",i,cuCrealf(host_x[i]),cuCimagf(host_x[i])); } */ //-------------------------------------------------------------------------------------------- //Se empieza a medir el tiempo de ejecucion del kernel cudaEventRecord(start_kernel,0); //Se copia el arreglo x[n] a la memoria global de la GPU cudaMemcpy(device_x,host_x,sizeof(cuFloatComplex)*N,cudaMemcpyHostToDevice); //Variables necesarias para dimensionar el block y grid dim3 dimBlock; dim3 dimGrid; dimBlock.x = N; dimBlock.y = 1; dimBlock.z = 1; dimGrid.x = 1; dimGrid.y = 1; dimGrid.z = 1; //Se invoca el kernel Goertzel<<<dimGrid,dimBlock >>>(N, device_x, device_X_k); cudaThreadSynchronize(); //Se leen los resultados de la GPU cudaMemcpy(host_X_k,device_X_k,sizeof(cuFloatComplex)*N,cudaMemcpyDeviceToHost); //Se termina de medir el tiempo de ejecucion del kernel cudaEventRecord(stop_kernel,0); cudaEventSynchronize(stop_kernel); cudaEventElapsedTime(&elapsedTime_kernel,start_kernel,stop_kernel); //fprintf(time_kernel,"%f\n",elapsedTime_kernel); fwrite(&elapsedTime_kernel,sizeof(float),1,time_kernel); //printf("\n\nTiempo de ejecucion del kernel: %f milisegundos\n\n",elapsedTime_kernel); //-------------------------------------------------------------------------------------------- //Se destruyen los eventos que miden el tiempo del kernel cudaEventDestroy(start_kernel); cudaEventDestroy(stop_kernel); //Se cierra el archivo binario "time_kernel" fclose(time_kernel); /* //Se imprime vector de salida X[k] printf("\n\n---VECTOR DE SALIDA X[k]---\n"); for(i = 0;i < N;i++) { printf("\nX[%d] = (%f) + (%f)\n ",i,cuCrealf(host_X_k[i]),cuCimagf(host_X_k[i])); } */ //Se liberan memorias del Host y Device free(host_x); free(host_X_k); cudaFree(device_x); cudaFree(device_X_k); //Comandos necesarios para medir el tiempo de la aplicacion (app) cudaEventRecord(stop_app,0); cudaEventSynchronize(stop_app); cudaEventElapsedTime(&elapsedTime_app,start_app,stop_app); fwrite(&elapsedTime_app,sizeof(float),1,time_app); //fprintf(time_app,"%f\n",elapsedTime_app); //printf("\n\nTiempo de ejecucion de la aplicacion: %f milisegundos\n\n",elapsedTime_app); //-------------------------------------------------------------------------------------------- //Se destruyen los eventos que miden el tiempo de la aplicacion cudaEventDestroy(start_app); cudaEventDestroy(stop_app); //Se cierra el archivo binario "time_app" fclose(time_app); } }
21,560
#include <stdio.h> #include <stdlib.h> int is_power_of_two(int x) { // https://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2/600306#600306 return (x != 0) && ((x & (x - 1)) == 0); } __global__ void sum_arr(float *arr, int n, double *res) { for(int i=0; i<n; i++) *res += arr[i]; } int main(int argc, char const *argv[]) { const int N = argc-1; if(N == 0 || !is_power_of_two(N)) { printf("Please call the program with the 2^N array elements " "to be summed as arguments.\n"); return 1; } float *arr; double *sum; cudaMallocManaged(&arr, sizeof *arr * N); cudaMallocManaged(&sum, sizeof *sum); *sum = 0; // initialize array on host with program arguments for(int i=0; i<N; i++) { arr[i] = atof(argv[i+1]); } sum_arr<<<1, 1>>>(arr, N, sum); cudaDeviceSynchronize(); cudaFree(arr); printf("Result is: %f\n", *sum); return 0; }
21,561
#include <thrust/device_vector.h> #include <thrust/reduce.h> #include <thrust/copy.h> #include <thrust/iterator/permutation_iterator.h> #include <stdint.h> #include <iterator> #include <iostream> #include <iomanip> #define DUMPN(d_vector,N,dtype,width) \ std::cout << std::setw(width) << #d_vector << ": "; \ thrust::copy_n(d_vector.begin(), N, \ std::ostream_iterator<dtype>(std::cout, ", ")); \ std::cout << std::endl; #define DUMP(d_vector,dtype,width) DUMPN(d_vector,(d_vector.end() - \ d_vector.begin()),dtype,width) int main(void) { /* # Block positions # */ // `(x, y)` positions for 4 blocks (blocks 0-3 from Fig. 4.13). uint32_t p_x[] = {2, 3, 3, 0}; uint32_t p_y[] = {0, 0, 3, 2}; size_t block_count = sizeof(p_x) / sizeof(uint32_t); /* # Nets # * * Assume each block drives a net (i.e., there are 4 nets in the netlist): * * - Net 0: connects blocks `0, 1, 2, 3`. * - Net 1: connects blocks `1, 0`. * - Net 2: connects blocks `2, 1`. * - Net 3: connects blocks `3, 0, 1`. */ uint32_t net_keys[] = {0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}; uint32_t block_keys[] = {0, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 3}; size_t connection_count = sizeof(net_keys) / sizeof(uint32_t); // Create device vectors to store position of each block. thrust::device_vector<uint32_t> d__p_x(block_count); thrust::device_vector<uint32_t> d__p_y(block_count); // Create device vectors to store netlist connection info. thrust::device_vector<uint32_t> d__net_keys(connection_count); thrust::device_vector<uint32_t> d__block_keys(connection_count); thrust::device_vector<uint32_t> d__block_x(connection_count); thrust::device_vector<uint32_t> d__block_y(connection_count); // Create device vectors to store reduced keys/values. thrust::device_vector<uint32_t> d__reduce_keys(connection_count); thrust::device_vector<uint32_t> d__net_x_sums(connection_count); thrust::device_vector<uint32_t> d__net_y_sums(connection_count); typedef thrust::device_vector<uint32_t>::iterator dev_iterator; // Copy the initial position of each block to device memory. // N.B., the following copy operations are only performed // *once* in the CAMIP implementation at the start of placement. thrust::copy_n(&p_x[0], block_count, d__p_x.begin()); thrust::copy_n(&p_y[0], block_count, d__p_y.begin()); thrust::copy_n(&block_keys[0], connection_count, d__block_keys.begin()); thrust::copy_n(&net_keys[0], connection_count, d__net_keys.begin()); /* # Sum x-position of all blocks by net # * * For each net, compute the sum of the x-positions of all * connected blocks. */ // Look-up the x-position for the block associated with each // netlist connection. thrust::copy_n( thrust::make_permutation_iterator( d__p_x.begin(), d__block_keys.begin()), d__block_keys.size(), d__block_x.begin()); // Look-up the y-position for the block associated with each // netlist connection. thrust::copy_n( thrust::make_permutation_iterator( d__p_y.begin(), d__block_keys.begin()), d__block_keys.size(), d__block_y.begin()); thrust::pair<dev_iterator, dev_iterator> new_end; // Reduce (sum) connection x-positions by net key. new_end = thrust::reduce_by_key( d__net_keys.begin(), d__net_keys.end(), d__block_x.begin(), d__reduce_keys.begin(), d__net_x_sums.begin()); // Reduce (sum) connection y-positions by net key. new_end = thrust::reduce_by_key( d__net_keys.begin(), d__net_keys.end(), d__block_y.begin(), d__reduce_keys.begin(), d__net_y_sums.begin()); size_t net_count = new_end.first - d__reduce_keys.begin(); /* # Results # * * ## Summary ## * * - `net_count` is equal to 4 (i.e., the number of nets in the netlist). * - The first four positions in `d__reduce_keys` now contain: {0, 1, 2, 3}. * - The first four positions in `d__net_x_sums` now contain: {5, 8, 8, 5}. * - The first four positions in `d__net_y_sums` now contain: {3, 5, 5, 2}. * * ## Array contents ## * * The final array contents are as follows: * * d__net_keys: 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, * d__block_keys: 0, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 3, * d__block_x: 2, 3, 2, 3, 3, 0, 2, 3, 3, 0, 2, 3, 0, * d__block_y: 0, 3, 0, 0, 3, 2, 0, 0, 3, 2, 0, 0, 2, * * net_count * <--------> * d__reduce_keys: 0, 1, 2, 3, (remaining elements unused...) * d__net_x_sums: 5, 8, 8, 5, (remaining elements unused...) * d__net_y_sums: 3, 5, 5, 2, (remaining elements unused...) */ DUMP(d__net_keys, uint32_t, 20); DUMP(d__block_keys, uint32_t, 20); DUMP(d__block_x, uint32_t, 20); DUMP(d__block_y, uint32_t, 20); std::cout << std::endl; DUMPN(d__reduce_keys, net_count, uint32_t, 20); DUMPN(d__net_x_sums, net_count, uint32_t, 20); DUMPN(d__net_y_sums, net_count, uint32_t, 20); return 0; }
21,562
#include <stdlib.h> #include <stdio.h> #include <time.h> //#include "initial.h" #include <cuda_runtime.h> #define N 1024 __global__ void suma_GPU(float *a, float *b, float *c){ int myID= threadIdx.x + blockDim.x * blockIdx.x; if(myID<N){ c[myID]= a[myID]+ b[myID]; } } void OneD_InitialData(float *ip, int size){ for(int i=0; i<size;i++){ ip[i]= (float) rand()/10.0; } } void print1D_arrays(float *pi,int size){ for(int i=0;i<size;i++){ printf("%f\n", pi[i]); } } int main(){ size_t nBytes= N* sizeof(float); float *h_a, *h_b, *h_c; float *d_a, *d_b, *d_c; h_a=(float *)malloc(nBytes); h_b=(float *)malloc(nBytes); h_c=(float *)malloc(nBytes); cudaMalloc( (void**)&d_a,nBytes); cudaMalloc( (void**)&d_b,nBytes); cudaMalloc( (void**)&d_c,nBytes); OneD_InitialData(h_a,N); OneD_InitialData(h_b,N); cudaMemcpy(d_a,h_a,nBytes,cudaMemcpyHostToDevice); cudaMemcpy(d_b,h_b,nBytes,cudaMemcpyHostToDevice); int Blocks=32; int Nhilos= N/Blocks; suma_GPU<<<Blocks,Nhilos >>>(d_a,d_b,d_c); cudaMemcpy(h_c,d_c,nBytes,cudaMemcpyDeviceToHost); print1D_arrays(h_c,N); free(h_a); free(h_b); free(h_c); cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); return 0; }
21,563
#include <stdlib.h> #include <stdio.h> #include <stdbool.h> #include <time.h> #include <sys/time.h> #define TICKS 10 #define HEIGHT 512 #define WIDTH HEIGHT #define NUM_GENERATORS 512 #define SOFTENING 10 #define NUM_THREADS 128 typedef struct { float* seed; float* fitness; } GenData; GenData* alloc_gen_data() { GenData* gen_data = (GenData*)malloc(sizeof(GenData)); gen_data->seed = (float*)malloc(sizeof(float) * HEIGHT * WIDTH * NUM_GENERATORS); gen_data->fitness = (float*)malloc(sizeof(float) * NUM_GENERATORS); return gen_data; } __device__ void fitness(float* fitness_arr, float* seed, int* gen_compare_dev, int idx) { fitness_arr[idx] = 0.0; for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { if(gen_compare_dev[i * WIDTH + j] == 1) { fitness_arr[idx] -= 1.0 - seed[(idx * HEIGHT * WIDTH) + (i * WIDTH) + j]; // if(idx == 0) // printf("F"); } else { fitness_arr[idx] -= seed[(idx * HEIGHT * WIDTH) + (i * WIDTH) + j] - 1.0; // if(idx == 0) // printf("B"); } } } } __global__ void kernel(float* fitness_arr, float* seed, int* gen_compare){ int idx = ((blockIdx.x * NUM_THREADS) + threadIdx.x); if(idx >= 0 && idx < NUM_GENERATORS) fitness(fitness_arr, seed, gen_compare, idx); } unsigned long utime(void) { struct timeval tv; unsigned long result = 0; gettimeofday(&tv, NULL); result += (tv.tv_sec * 1000000); result += tv.tv_usec; return result; } float randPercent() { return ((rand() % 1000)/(1000.0)); } void init_generator(GenData* gen_data, int idx) { // printf("I AM IN THE FUNCTION\n"); // printf("CC%p\n", gen_data); // printf("CC%p\n", gen_data->fitness); // printf("CC%p\n", gen_data->seed); for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { // printf("%i, %i, %i = %i\n", i, j, idx, (idx * HEIGHT * WIDTH) + (i * WIDTH) + j); // printf("%f\n", *gen_data->seed); // this should (not) fail gen_data->seed[(idx * HEIGHT * WIDTH) + (i * WIDTH) + j] = randPercent(); // gen_data->image[(idx * HEIGHT * WIDTH) + (i * WIDTH) + j] = false; } } // printf("I AM OUT OF THE FORLOOP\n"); gen_data->fitness[idx] = 0; // printf("FASDF\n"); } /* bool* init_letter() { bool* gen_compare = (bool*)malloc(sizeof(bool) * WIDTH*HEIGHT); char* hold = (char*)malloc(sizeof(char) * WIDTH*HEIGHT); sscanf("0000000000011000001001000100001001111110010000100010010000011000", "%s", hold); for(int i = 0; i < WIDTH*HEIGHT; i++) { gen_compare[i] = hold[i] == '1'; } return gen_compare; } */ void exchange_float(float* arr, int a, int b) { float temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } void exchange_bool(bool* arr, int a, int b) { bool temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } void exchange_generators(GenData* gen_data, int a, int b) { exchange_float(gen_data->fitness, a, b); int aPrime = a * HEIGHT * WIDTH; int bPrime = b * HEIGHT * WIDTH; for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { int ij = i * WIDTH + j; exchange_float(gen_data->seed, aPrime+ij, bPrime+ij); // exchange_bool(gen_data->image, aPrime+ij, bPrime+ij); } } } int partition(GenData* gen_data, int p, int r) { int pivot = gen_data->fitness[r]; int i = p - 1; for(int j = p; j < r - 1; j++) { if(gen_data->fitness[j] < pivot) { i++; exchange_generators(gen_data, i, j); } } exchange_generators(gen_data, i + 1, r); return i + 1; } void sort(GenData* gen_data, int p, int r) { if(p < r) { int q = partition(gen_data, p, r); sort(gen_data, p, q-1); sort(gen_data, q+1, r); } } void mutate(GenData* gen_data, GenData* new_gen_data, int idx) { int l = (idx*2 + 0)*HEIGHT*WIDTH; int r = (idx*2 + 1)*HEIGHT*WIDTH; int o = idx*HEIGHT*WIDTH; //printf("pre mutate seed: %f\n", new_gen_data->seed[r]); for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { int ij = i * WIDTH + j; /* // image new_gen_data->image[l + ij] = gen_data->image[o + ij]; new_gen_data->image[r + ij] = gen_data->image[o + ij]; */ // seed float randAmount = (0.5 - randPercent())/SOFTENING; new_gen_data->seed[l + ij] = (gen_data->seed[o + ij] + randAmount); if(new_gen_data->seed[l + ij] < 0) { new_gen_data->seed[l + ij] = 0.0; } else if (new_gen_data->seed[l + ij] > 1) { new_gen_data->seed[l + ij] = 1.0; } new_gen_data->seed[r + ij] = (gen_data->seed[o + ij] - randAmount); if(new_gen_data->seed[r + ij] < 0) { new_gen_data->seed[r + ij] = 0.0; } else if (new_gen_data->seed[r + ij] > 1) { new_gen_data->seed[r + ij] = 1.0; } /* if(gen_compare[ij]) { new_gen_data->seed[l + ij] = gen_data->seed[o + ij] + .1; new_gen_data->seed[r + ij] = gen_data->seed[o + ij] + .1; } else { new_gen_data->seed[l + ij] = gen_data->seed[o + ij] - .1; new_gen_data->seed[r + ij] = gen_data->seed[o + ij] - .1; } */ /* new_gen_data->seed[l + ij] = gen_data->seed[o + ij] + (0.5 - randPercent()) / 50; new_gen_data->seed[r + ij] = gen_data->seed[o + ij] + (0.5 - randPercent()) / 50; */ } } //printf("post mutate seed: %f\n", new_gen_data->seed[r]); } void next_gen(GenData* gen_data, GenData* new_gen_data) { sort(gen_data, 0, NUM_GENERATORS-1); /* for(int i = 0; i < NUM_GENERATORS; i++) { printf("postsort: %i, %f\n", i, gen_data->fitness[i]); } */ // printf("best fitness of this tick is: %f\n", gen_data->fitness[0]); for(int i = 0; i < NUM_GENERATORS/2; i++) { mutate(gen_data, new_gen_data, i); } } void tick(GenData* gen_data, GenData* new_gen_data, int* gen_compare_dev, GenData* gen_data_dev) { // COPY IT IN // cudaMemcpy( gen_data_dev->fitness, gen_data->fitness, sizeof(float ) * NUM_GENERATORS, cudaMemcpyHostToDevice ); cudaMemcpy( gen_data_dev->seed, gen_data->seed, sizeof(float) * NUM_GENERATORS * HEIGHT * WIDTH, cudaMemcpyHostToDevice) ; // CALL IT dim3 grid((NUM_GENERATORS + NUM_THREADS - 1) / NUM_THREADS); kernel<<<grid, NUM_THREADS>>>(gen_data_dev->fitness, gen_data_dev->seed, gen_compare_dev); // YES cudaMemcpy( gen_data->fitness, gen_data_dev->fitness, sizeof(float) * NUM_GENERATORS, cudaMemcpyDeviceToHost ); // cudaMemcpy( gen_data->seed, gen_data_dev->seed, sizeof(float) * NUM_GENERATORS * HEIGHT * WIDTH, cudaMemcpyDeviceToHost ); next_gen(gen_data, new_gen_data); } int main(int arc, char **argv) { // allocate // printf("START \n"); GenData* gen_data = alloc_gen_data(); // init srand(time(NULL)); GenData* gen_data_dev = (GenData*) malloc (sizeof(GenData)); cudaMalloc(&gen_data_dev->fitness, sizeof(float) * NUM_GENERATORS); cudaMalloc(&gen_data_dev->seed, sizeof(float) * HEIGHT * WIDTH * NUM_GENERATORS); // printf("CUDAMALLOC \n"); for(int i = 0; i < NUM_GENERATORS; i++) { init_generator(gen_data, i); } // printf("STETSET\n"); // bool* gen_compare = init_letter(); int* gen_compare_int = (int*)malloc(sizeof(int) * HEIGHT * WIDTH); for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { /* if(gen_compare[i * WIDTH + j]) { gen_compare_int[i * WIDTH + j] = 1; } else { gen_compare_int[i * WIDTH + j] = 0; } */ if(randPercent() < .5) { gen_compare_int[i * WIDTH + j] = 1; } else { gen_compare_int[i * WIDTH + j] = 0; } } } int* gen_compare_dev; cudaMalloc(&gen_compare_dev, sizeof(int) * HEIGHT * WIDTH); cudaMemcpy(gen_compare_dev, gen_compare_int, sizeof(int) * HEIGHT * WIDTH, cudaMemcpyHostToDevice); // ticks GenData* new_gen_data = alloc_gen_data(); // printf("gen_data: %p, new_gen_data: %p\n", gen_data, new_gen_data); for(int i = 0; i < TICKS; i++) { // printf("Tick! %i \n", i); unsigned long start = utime(); tick(gen_data, new_gen_data, gen_compare_dev, gen_data_dev); unsigned long end = utime(); unsigned long elapsed = end - start; printf("for tick %i the time is %lu\n", i, elapsed); GenData* temp = gen_data; gen_data = new_gen_data; new_gen_data = temp; } // print /* for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) { if(gen_data->seed[i * WIDTH + j] > .5) { printf("0."); } else { printf("1."); } } printf("\n"); } */ // printf("That gen_par run had %i generators running for %i ticks and took %lu seconds!\n", NUM_GENERATORS, TICKS, elapsed/1000); // destroy free(gen_data); }
21,564
#include <iostream> #include <sstream> #include <iomanip> const int width = 34; static void cudaSafeCall(cudaError_t err) { if (err != cudaSuccess) { std::cerr << cudaGetErrorString(err) << std::endl; exit(1); } } static int getCoreNumPerSP(int major, int minor) { switch (major) { case 2: { if (minor == 0) { return 32; } else if (minor == 1) { return 48; } else { return 0; } } case 3: { return 192; } case 5: { return 128; } case 6: { if (minor == 0) { return 64; } else if ((minor == 1) || (minor == 2)) { return 128; } else { return 0; } } default: { return 0; } } } static std::string formatSize(size_t size) { std::ostringstream stringStream; stringStream.precision(1); stringStream << std::fixed; if (size < 1024 * 10) { stringStream << size; } else if (size < 1024 * 1024 * 10) { stringStream << (float)size / 1024 << " KiB"; } else if (size < size_t(1024) * size_t(1024) * size_t(1024) * size_t(10)) { stringStream << (float)size / (1024 * 1024) << " MiB"; } else { stringStream << (float)size / (1024 * 1024 * 1024) << " GiB"; } return stringStream.str(); } static void displayDeviceProperties(cudaDeviceProp& prop, int device) { std::cout << std::setw(width) << std::left << "GPU Device ID:" << device << std::endl; std::cout << std::setw(width) << std::left << " Name:" << prop.name << std::endl; std::cout << std::setw(width) << std::left << " Compute Capability:" << prop.major << "." << prop.minor << std::endl; std::cout << std::setw(width) << std::left << " MultiProcessor(s):" << prop.multiProcessorCount << std::endl; std::cout << std::setw(width) << std::left << " Cores Per MultiProcessor:" << getCoreNumPerSP(prop.major, prop.minor) << std::endl; std::cout << std::setw(width) << std::left << " Max Threads Per MultiProcessor:" << prop.maxThreadsPerMultiProcessor << std::endl; std::cout << std::setw(width) << std::left << " Clock Rate:" << prop.clockRate / (1000) << " MHz" << std::endl; std::cout << std::setw(width) << std::left << " Warp Size:" << prop.warpSize << std::endl; std::cout << std::setw(width) << std::left << " L2 Cache Size:" << formatSize(prop.l2CacheSize) << std::endl; std::cout << std::setw(width) << std::left << " Global Memory Size:" << formatSize(prop.totalGlobalMem) << std::endl; std::cout << std::setw(width) << std::left << " Constant Memory Size:" << formatSize(prop.totalConstMem) << std::endl; std::cout << std::setw(width) << std::left << " One-Dimension Texture Size:" << prop.maxTexture1D << std::endl; std::cout << std::setw(width) << std::left << " Two-Dimension Texture Size:" << prop.maxTexture2D[0] << " x " << prop.maxTexture2D[1] << std::endl; std::cout << std::setw(width) << std::left << " Three-Dimension Texture Size:" << prop.maxTexture3D[0] << " x " << prop.maxTexture3D[1] << " x " << prop.maxTexture3D[2] << std::endl; std::cout << std::setw(width) << std::left << " Shared Memory Size Per Block:" << formatSize(prop.sharedMemPerBlock) << std::endl; std::cout << std::setw(width) << std::left << " Max Threads Per Block:" << prop.maxThreadsPerBlock << std::endl; std::cout << std::setw(width) << std::left << " Registers Per Block:" << prop.regsPerBlock << std::endl; std::cout << std::setw(width) << std::left << " Block Dimension:" << prop.maxThreadsDim[0] << " x " << prop.maxThreadsDim[1] << " x " << prop.maxThreadsDim[2] << std::endl; std::cout << std::setw(width) << std::left << " Grid Dimension:" << prop.maxGridSize[0] << " x " << prop.maxGridSize[1] << " x " << prop.maxGridSize[2] << std::endl; std::cout << std::setw(width) << std::left << " Unified address space:" << std::boolalpha << (prop.unifiedAddressing ? true : false) << std::endl; return; } int main(void) { int gpuCount = 0; cudaSafeCall(cudaGetDeviceCount(&gpuCount)); int runtimeVersion = 0; cudaSafeCall(cudaRuntimeGetVersion(&runtimeVersion)); int driverVersion = 0; cudaSafeCall(cudaDriverGetVersion(&driverVersion)); cudaDeviceProp *prop = new cudaDeviceProp[gpuCount]; if (prop == NULL) { std::cout << "The memory is too small, and please enlarge it, thanks!" << std::endl; exit(1); } for (int i = 0; i < gpuCount; i++) { cudaSafeCall(cudaGetDeviceProperties(prop + i, i)); } std::cout << std::setw(width) << std::left << "CUDA Runtime Version:" << runtimeVersion / 1000 << "." << (runtimeVersion % 100) / 10 << std::endl; std::cout << std::setw(width) << std::left << "CUDA Driver Version:" << driverVersion / 1000 << "." << (driverVersion % 100) / 10 << std::endl; std::cout << std::setw(width) << std::left << "GPU(s):" << gpuCount << std::endl; for (int i = 0; i < gpuCount; i++) { displayDeviceProperties(prop[i], i); } delete[] prop; return 0; }
21,565
/* * Copyright 1993-2009 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 and * any modifications thereto. Any use, reproduction, disclosure, or distribution * of this software and related documentation without an express license * agreement from NVIDIA Corporation is strictly prohibited. * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define PI 3.1415926536f /* * Paint a 2D texture with a moving red/green hatch pattern on a * strobing blue background. Note that this kernel reads to and * writes from the texture, hence why this texture was not mapped * as WriteDiscard. */ __global__ void cudaKernelTexture2D(unsigned char* surface, int width, int height, size_t pitch, float t) { int x = blockIdx.x*blockDim.x + threadIdx.x; int y = blockIdx.y*blockDim.y + threadIdx.y; unsigned char* pixel; // in the case where, due to quantization into grids, we have // more threads than pixels, skip the threads which don't // correspond to valid pixels if (x >= width || y >= height) return; // get a pointer to the pixel at (x,y) pixel = (unsigned char*)(surface + y*pitch) + 4*x; // populate it float value_x = 0.5f + 0.5f*cos(t + 10.0f*( (2.0f*x)/width - 1.0f ) ); float value_y = 0.5f + 0.5f*cos(t + 10.0f*( (2.0f*y)/height - 1.0f ) ); // Color : DirectX BGRA, OpenGL RGBA pixel[0] = 255*(0.5f + 0.5f*cos(t)); // blue pixel[1] = 255*(0.5*pixel[1]/255.0 + 0.5*pow(value_y, 3.0f)); // green pixel[2] = 255*(0.5*pixel[0]/255.0 + 0.5*pow(value_x, 3.0f)); // red pixel[3] = 255; // alpha } extern "C" void cudaTextureUpdate(void* deviceTexture, int width, int height, float t) { dim3 Db = dim3(16, 16); // block dimensions are fixed to be 256 threads dim3 Dg = dim3((width+Db.x-1)/Db.x, (height+Db.y-1)/Db.y); size_t pitch = width*4; cudaKernelTexture2D<<<Dg,Db>>>((unsigned char*)deviceTexture, width, height, pitch, t); }
21,566
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <stdlib.h> #include <math.h> #define N (1024) __global__ void mult(float *A, float *B, float *C) { unsigned int idx_X = threadIdx.x + blockIdx.x * blockDim.x; unsigned int idx_Y = threadIdx.y + blockIdx.y * blockDim.y; float sum = 0.; if ((idx_X < N) && (idx_Y < N)) { for (int i = 0; i < N; i++) { sum += A[idx_X*N + i] * B[idx_Y + i*N]; } C[idx_X*N + idx_Y] = sum; } } int main(void) { cudaEvent_t GPUstart, GPUstop; float GPUtime = 0.0f; float *hostA, *hostB; float *hostC; float *devA, *devB; float *devC; size_t mem_size = N*N*sizeof(float); hostA = (float *)malloc(mem_size); hostB = (float *)malloc(mem_size); hostC = (float *)malloc(mem_size); cudaMalloc((void**)&devA, mem_size); cudaMalloc((void**)&devB, mem_size); cudaMalloc((void**)&devC, mem_size); for (int i = 0; i < N*N; i++) { hostA[i] = sqrtf(i); hostB[i] = sinf(i); hostC[i] = 0.; } int N_Threads = 8; int N_Blocks = 0; if (((N) % N_Threads) == 0) { N_Blocks = ((N) / N_Threads); } else { N_Blocks = ((N) / N_Threads) + 1; } dim3 Threads(N_Threads,N_Threads); dim3 Blocks(N_Blocks, N_Blocks); cudaEventCreate(&GPUstart); cudaEventCreate(&GPUstop); cudaEventRecord(GPUstart, 0); cudaMemcpy(devA, hostA, mem_size, cudaMemcpyHostToDevice); cudaMemcpy(devB, hostB, mem_size, cudaMemcpyHostToDevice); cudaMemset(devC, 0, mem_size); mult <<< Blocks, Threads >>> (devA, devB, devC); cudaMemcpy(hostC, devC, mem_size, cudaMemcpyDeviceToHost); cudaEventRecord(GPUstop, 0); cudaEventSynchronize(GPUstop); cudaEventElapsedTime(&GPUtime, GPUstart, GPUstop); printf("GPU time : %.3f ms\n", GPUtime); cudaFree(devA); cudaFree(devB); cudaFree(devC); free(hostA); free(hostB); free(hostC); return 0; }
21,567
#include "includes.h" __global__ void MyFloatScale(float *a, float scale, int size) { const int numThreads = blockDim.x * gridDim.x; const int threadID = blockIdx.x * blockDim.x + threadIdx.x; for (int i = threadID; i < size; i += numThreads) { a[i] *= scale; } }
21,568
#include "includes.h" __global__ void Vector_Addition ( const int *dev_a , const int *dev_b , int *dev_c) { //Get the id of thread within a block unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x ; while ( tid < N ) // check the boundry condition for the threads { dev_c [tid] = dev_a[tid] + dev_b[tid] ; tid+= blockDim.x * gridDim.x ; } }
21,569
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <stdlib.h> #include <cstring> #include <time.h> __global__ void mem_trs_test(int * input) { int gid = blockIdx.x * blockDim.x + threadIdx.x; printf("tid: %d, gid: %d, value : %d \n", threadIdx.x, gid, input[gid]); } __global__ void mem_trs_test2(int * input, int size) { int gid = blockIdx.x * blockDim.x + threadIdx.x; if (gid < size) { printf("tid: %d, gid: %d, value : %d \n", threadIdx.x, gid, input[gid]); } } int main() { int size = 150; int byte_size = size * sizeof(int); int * h_input; h_input = (int *)malloc(byte_size); time_t t; srand((unsigned) time(&t)); for (int i =0; i < size; i++) { h_input[i] = (int) (rand() & 0xff); } int * d_input; cudaMalloc((void **)&d_input, byte_size); cudaMemcpy(d_input, h_input, byte_size, cudaMemcpyHostToDevice); //dim3 block(64); //dim3 grid(2); // //mem_trs_test <<< grid, block>>>(d_input); dim3 block(32); dim3 grid(5); mem_trs_test2<<<grid, block>>>(d_input, size); cudaDeviceSynchronize(); cudaFree(d_input); free(h_input); cudaDeviceReset(); return 0; }
21,570
#ifndef PERISTALSIS_UTIL_CU__ #define PERISTALSIS_UTIL_CU__ __device__ int EvaluateShift(int pos, int size) { if (pos < 0) return -1; if (pos > size - 1) return 1; return 0; } #endif//PERISTALSIS_UTIL_CU__
21,571
#include <stdio.h> __global__ void add(float *a, float *b, float *c) { int id = threadIdx.x; c[id] = a[id] + b[id]; } int main() { float a[] = {1., 2., 3.}; float b[] = {4., 5., 6.}; float c[3]; float *a_; float *b_; float *c_; int size = 3 * sizeof(float); cudaMalloc((void**) &a_, size); cudaMalloc((void**) &b_, size); cudaMalloc((void**) &c_, size); cudaMemcpy(a_, a, size, cudaMemcpyHostToDevice); cudaMemcpy(b_, b, size, cudaMemcpyHostToDevice); dim3 dimGrid(1, 1, 1); dim3 dimBlock(3, 1, 1); add <<< dimGrid, dimBlock >>> (a_, b_, c_); cudaMemcpy(c, c_, size, cudaMemcpyDeviceToHost); cudaFree(a_); cudaFree(b_); cudaFree(c_); printf("%f %f %f\n", c[0], c[1], c[2]); }
21,572
// Задание 1.1. Сложение двух векторов // Написать программу, которая преобразует последовательный код в код на CUDA C с параметрами ядра // <<<1, 1 >>>. Затем изменить параметры ядра <<< N, 1 >>> (blockIdx.x) и <<< 1, N >>> (threadIdx.x) #include "stdio.h" #include "assert.h" #include "math.h" #include <iostream> using namespace std; #define N 5 __global__ void add(int *a, int *b, int *c, int count) { int idx = threadIdx.x; if (idx < count) { c[idx] = a[idx] + b[idx]; } } int main( void ) { const int maxerror = 1e-6; int ha[N], hb[N], hc[N]; int *da, *db, *dc; for (int i = 0; i < N; i++) { ha[i] = -i; hb[i] = i * i; } for (int i = 0; i < N; i++) { cout << ha[i] << endl; } for (int i = 0; i < N; i++) { cout << hb[i] << endl; } // выделяем память на GPU cudaMalloc((void**)&da, sizeof(int) * N); cudaMalloc((void**)&db, sizeof(int) * N); cudaMalloc((void**)&dc, sizeof(int) * N); // копируем cudaMemcpy(da, ha, sizeof(int) * N, cudaMemcpyHostToDevice); cudaMemcpy(db, hb, sizeof(int) * N, cudaMemcpyHostToDevice); // вызов в GPU add<<<1, N>>>(da, db, dc, N); // копируем с устройства на хост cudaMemcpy(hc, dc, sizeof(int) * N, cudaMemcpyDeviceToHost); for (int i = 0; i < N; i++) { cout << hc[i] << endl; } for (int i = 0; i < N; i++) { assert(abs(hc[i] - ha[i] - hb[i]) < maxerror); } printf("<1, N> case passed\n"); // копируем cudaMemcpy(da, ha, sizeof(int) * N, cudaMemcpyHostToDevice); cudaMemcpy(db, hb, sizeof(int) * N, cudaMemcpyHostToDevice); // вызов в GPU add<<<N, 1>>>(da, db, dc, N); // копируем с устройства на хост cudaMemcpy(hc, dc, sizeof(int) * N, cudaMemcpyDeviceToHost); for (int i = 0; i < N; i++) { cout << hc[i] << endl; } for (int i = 0; i < N; i++) { assert(abs(hc[i] - ha[i] - hb[i]) < maxerror); } printf("<N, 1> case passed\n"); cudaFree(da); cudaFree(db); cudaFree(dc); return 0; }
21,573
#include "includes.h" __global__ void pre_sort(unsigned int *in, unsigned int *in_pos, unsigned int *out, unsigned int *out_pos, unsigned int n, unsigned int nBins, unsigned int mask, unsigned int current_bits, unsigned int *d_hist) { extern __shared__ unsigned int pre_sort_blk_data[]; unsigned int* blk_value = pre_sort_blk_data; unsigned int* blk_pos = pre_sort_blk_data + blockDim.x; unsigned int* blk_hist = pre_sort_blk_data + 2*blockDim.x; unsigned int* blk_Scan = pre_sort_blk_data + nBins + 2*blockDim.x; int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) { blk_value[threadIdx.x] = in[i]; blk_pos[threadIdx.x] = in_pos[i]; } __syncthreads(); //Hist for(int j = threadIdx.x; j < nBins; j += blockDim.x) { blk_hist[j] = 0; blk_Scan[j] = 0; } __syncthreads(); unsigned int bin = (blk_value[threadIdx.x] >> current_bits) & mask; atomicAdd(&blk_hist[bin], 1); atomicAdd(&blk_Scan[bin], 1); __syncthreads(); //Scan for (int stride = 1; stride < nBins; stride *= 2) { for (int k = threadIdx.x; k < nBins; k += blockDim.x) { int inVal; if (k >= stride) inVal = blk_Scan[k - stride]; __syncthreads(); if (k >= stride) blk_Scan[k] += inVal; __syncthreads(); } } __syncthreads(); for (int i = threadIdx.x; i < nBins; i += blockDim.x) blk_Scan[i] -= blk_hist[i]; __syncthreads(); //Scatter if (threadIdx.x == 0) { for (int i = 0; i < blockDim.x; i++) { unsigned int bin = (blk_value[i] >> current_bits) & mask; out[blk_Scan[bin] + blockIdx.x*blockDim.x] = blk_value[i]; out_pos[blk_Scan[bin] + blockIdx.x*blockDim.x] = blk_pos[i]; blk_Scan[bin]++; } } }
21,574
#include <iostream> #include <stdio.h> #include <stdlib.h> #define TILE_WIDTH 32 using namespace std; __global__ void multTiled(float *A, float *B, float *C,int rA,int cA,int rB,int cB){ __shared__ int Mds[TILE_WIDTH][TILE_WIDTH]; __shared__ int Nds[TILE_WIDTH][TILE_WIDTH]; int bx = blockIdx.x; int by = blockIdx.y; int tx = threadIdx.x; int ty = threadIdx.y; int row = by * TILE_WIDTH + ty; int col = bx * TILE_WIDTH + tx; float Pvalue = 0; for(int k = 0; k < (cA+TILE_WIDTH-1)/(TILE_WIDTH); ++k){ if(k*TILE_WIDTH + tx < cA && row < rA){ Mds[ty][tx] = A[row*cA + k*TILE_WIDTH + tx]; }else{ Mds[ty][tx] = 0; } if(k*TILE_WIDTH + ty < cA && col < cB){ Nds[ty][tx] = B[(k*TILE_WIDTH + ty) * cB + col]; }else{ Nds[ty][tx] =0; } __syncthreads(); for(int k = 0; k < TILE_WIDTH; ++k){ Pvalue += Mds[ty][k] * Nds[k][tx]; } __syncthreads(); } if (row < rA && col < cB){ C[row*cB+col] = Pvalue; } } __global__ void matrixMulKernel(float *d_A, float *d_B, float *d_C,int rA,int cA,int rB,int cB){ int row = blockIdx.y*blockDim.y+threadIdx.y; int col = blockIdx.x*blockDim.x+threadIdx.x; float Pvalue; if((row < rA)&&(col < cB)){ Pvalue = 0; for (int k = 0; k < rB ; ++k){ Pvalue += d_A[row*cA+k] * d_B[k*cB+col]; } d_C[row*cB+col] = Pvalue; } } void multMxN(float *A,float *B, float *C, int rA,int cA,int rB,int cB){ //El numero de col de A, debe ser igual al numero de row de B; //Matriz C= FilasAxColumnasB for(int i=0;i<rA;i++){ for(int j=0;j<cB;j++){ float value=0; for(int k=0;k<rB;k++){ //h_c[n*i+j]+=h_a[n*i+k]*h_b[n*k+j]; value+=A[cA*i+k]*B[cB*k+j]; } C[cB*i+j]=value; } } } void llenar(float *MA,int n,int m){ int tam=n*m; for(int i = 0; i < tam; i++ ) MA[i] = 1.0; } void imprimir(float *MA,int n,int m){ int tam=n*m,cont=0; for(int i = 0; i < tam; i++ ) { printf("%f ",MA[i]); if(cont==m-1){ printf("\n"); cont=-1; } cont++; } printf("\n"); } void comparar(float *C1,float *C2,int rC,int cC){ int w=rC*cC; for(int i=0;i<w;i++){ if(C1[i]!=C2[i]){ cout<<"No son iguales"<<endl; break; } } cout<<"Son iguales"<<endl; } int main(){ int rA=128; int cA=1024; int cB=512; float blockSize = 4; int rB=cA; size_t bytesA=(rA*cA)*sizeof(float); size_t bytesB=(rB*cB)*sizeof(float); size_t bytesC=(rA*cB)*sizeof(float); float *A=(float*)malloc(bytesA); float *B=(float*)malloc(bytesB); float *C1=(float*)malloc(bytesC); float *C2=(float*)malloc(bytesC); float *C3=(float*)malloc(bytesC); //1.Lleno las matrices llenar(A,rA,cA); llenar(B,rB,cB); //2.imprimo las matrices //cout<<"Matriz A:"<<endl; //imprimir(A,rA,cA); //cout<<"Matriz B:"<<endl; //imprimir(B,rB,cB); //3.Multiplico matrices NxM Secuencial clock_t start = clock(); multMxN(A,B,C1,rA,cA,rB,cB); clock_t end= clock(); double elapsed_seconds=end-start; printf("Tiempo transcurrido Secuencial: %lf\n", (elapsed_seconds / CLOCKS_PER_SEC)); //cout<<"Secuencial"<<endl; //imprimir(C1,rA,cB); //3.Multiplico matrices NxM Paralelo sin Tile float *d_A; float *d_B; float *d_C; // Allocate memory for each vector on GPU cudaMalloc(&d_A,bytesA); cudaMalloc(&d_B,bytesB); cudaMalloc(&d_C,bytesC); // Copy host vectors to device cudaMemcpy( d_A, A, bytesA, cudaMemcpyHostToDevice); cudaMemcpy( d_B, B, bytesB, cudaMemcpyHostToDevice); //bloques dim3 dimGrid(ceil(cB/blockSize),ceil(rA/blockSize),1); //hilos dim3 dimBlock(blockSize,blockSize,1); //Tiempo de ejecucion Paralelo clock_t start2 = clock(); // Execute the kernel matrixMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C,rA,cA,rB,cB); //multTiled<<<dimGrid, dimBlock>>>(d_A, d_B, d_C,rA,cA,rB,cB); cudaDeviceSynchronize(); // Copy array back to host cudaMemcpy( C2, d_C, bytesC, cudaMemcpyDeviceToHost ); clock_t end2= clock(); double elapsed_seconds2=end2-start2; printf("Tiempo transcurrido Paralelo SinTile: %lf\n", (elapsed_seconds2 / CLOCKS_PER_SEC)); //4.Imprimo matriz C //cout<<endl<<"Paralelo sin tiling"<<endl; //imprimir(C2,rA,cB); //cout<<"Matriz Secuencial-SinTiling: "<<endl; //cout<<endl; // Execute the kernel //Tiempo de ejecucion Paralelo clock_t start3 = clock(); multTiled<<<dimGrid, dimBlock>>>(d_A, d_B, d_C,rA,cA,rB,cB); cudaDeviceSynchronize(); // Copy array back to host cudaMemcpy( C3, d_C, bytesC, cudaMemcpyDeviceToHost ); clock_t end3= clock(); double elapsed_seconds3=end3-start3; printf("Tiempo transcurrido Paralelo ConTile: %lf\n", (elapsed_seconds3 / CLOCKS_PER_SEC)); //4.Imprimo matriz C //cout<<endl<<"Paralelo con tiling"<<endl; //imprimir(C3,rA,cB); // cout<<"Matriz Secuencial-Tiling: "<<endl; comparar(C1,C3,rA,cB); comparar(C1,C2,rA,cB); // Release device memory cudaFree(d_A); cudaFree(d_B); cudaFree(d_C); // Release host memory free(A); free(B); free(C1); free(C2); return 0; }
21,575
#include "includes.h" #ifdef __INTELLISENSE__ void __syncthreads(); #endif // image dimensions WIDTH & HEIGHT #define WIDTH 256 #define HEIGHT 256 // Block width WIDTH & HEIGHT #define BLOCK_W 16 #define BLOCK_H 16 // buffer to read image into float image[HEIGHT][WIDTH]; // buffer for resulting image float final[HEIGHT][WIDTH]; // prototype declarations void load_image(); void call_kernel(); void save_image(); #define MAXLINE 128 float total, sobel; cudaEvent_t start_total, stop_total; cudaEvent_t start_sobel, stop_sobel; __global__ void sobelFilter(float *input, float *output, int width, int height) { int col = threadIdx.x + blockIdx.x * blockDim.x; int row = threadIdx.y + blockIdx.y * blockDim.y; int numcols = WIDTH; float gradient_h; float gradient_v; float gradient; float thresh = 30; if (row <= height && col <= width && row > 0 && col > 0) { int x0, x1, x2, x3, x5, x6, x7, x8; // horizontal // -1 0 1 // -2 0 2 // -1 0 1 // vertical // -1 -2 -1 // 0 0 0 // 1 2 1 x0 = input[(row - 1) * numcols + (col - 1)]; // leftup x1 = input[(row + 1) * numcols + col]; // up x2 = input[(row - 1) * numcols + (col + 1)]; // rightup x3 = input[row * numcols + (col - 1)]; // left x5 = input[row * numcols + (col + 1)]; // right x6 = input[(row + 1) * numcols + (col - 1)]; // leftdown x7 = input[(row + -1) * numcols + col]; // down x8 = input[(row + 1) * numcols + (col + 1)]; // rightdown gradient_h = (x0 * -1) + (x2 * 1) + (x3 * -2) + (x5 * 2) + (x6 * -1) + (x8 * 1); gradient_v = (x0 * -1) + (x1 * -2) + (x3 * -1) + (x6 * 1) + (x7 * 2) + (x8 * 1); gradient = sqrt((gradient_h * gradient_h) + (gradient_v * gradient_v)); if (gradient >= thresh) { gradient = 255; } else { gradient = 0; } output[row * numcols + col] = gradient; } }
21,576
#include <stdio.h> #include <sys/time.h> #include <cuda.h> long long getCurrentTime() { struct timeval te; gettimeofday(&te, NULL); // get current time long long microseconds = te.tv_sec*1000000LL + te.tv_usec; return microseconds; } #define CUDA_ERROR_CHECK #define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ ) inline void __cudaSafeCall( cudaError err, const char *file, const int line ) { #ifdef CUDA_ERROR_CHECK if ( cudaSuccess != err ) { fprintf( stderr, "cudaSafeCall() failed at %s:%i : %s\n", file, line, cudaGetErrorString( err ) ); exit( -1 ); } #endif return; } __global__ void assign(int *A, int streamId) { for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < 1024 * 1024; i += blockDim.x * gridDim.x) { A[i] += 1024*1024*streamId + i; } } int main() { cudaStream_t streams[4]; int *A[4], *dA[4]; int N = 1024*1024; // Initialization for (int i = 0; i < 4; i++) { CudaSafeCall(cudaStreamCreate(&streams[i])); CudaSafeCall(cudaMallocHost((void**)&A[i], sizeof(int) * N)); CudaSafeCall(cudaMalloc(&dA[i], N * sizeof(int))); } // Asynchronous H2D copies for (int i = 0; i < 4; i++) { CudaSafeCall(cudaMemcpyAsync(dA[i], A[i], N * sizeof(int), cudaMemcpyHostToDevice, streams[i])); } // Asynchronous kernel launches for (int i = 0; i < 4; i++) { assign<<<1, 1024, 0, streams[i]>>>(dA[i], i); } // Asynchronous D2H copies for (int i = 0; i < 4; i++) { CudaSafeCall(cudaMemcpyAsync(A[i], dA[i], N * sizeof(int), cudaMemcpyDeviceToHost, streams[i])); } // Synchronization for (int i = 0; i < 4; i++) { CudaSafeCall(cudaStreamSynchronize(streams[i])); } // Verification int error = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < N; j++) { if (A[i][j] != i * N + j) { error++; } } } if (!error) { printf("VERIFILED\n"); } else { printf("NOT VERIFIED"); } return 0; }
21,577
//#pragma comment (lib, "cublas.lib") //#include "stdio.h" //#include <cuda.h> //using namespace std; //#include <ctime> //#include "cuda_runtime.h" //#include "curand_kernel.h" //#include "device_launch_parameters.h" //#include <stdio.h> //#include <stdlib.h> // //#include <string> //#include <iomanip> //#include <time.h> //#include <iostream> //#include <cmath> //#include <math.h> // //#define TRAIN_NUM 60000 //#define TEST_NUM 10000 //#define ROW 28 //#define COL 28 //#define CONV_SIZE 24 //#define POOL_SIZE 12 //#define FC1_SIZE 5 //#define FC2_SIZE 10 //#define CONV_W_SIZE 5 //#define CONV_W_NUM 6 // //int correct_cnt; //float avg_error; //float max_acc; // //float alpha = 0.2; //int epochs = 5; //int minibatch = 1; // //float train_image[TRAIN_NUM][ROW][COL]; //int train_label[TRAIN_NUM]; //float test_image[TEST_NUM][ROW][COL]; //int test_label[TEST_NUM]; // //float conv_w[CONV_W_NUM][CONV_W_SIZE][CONV_W_SIZE]; //float conv_b[CONV_W_NUM]; //float fc1_b[FC1_SIZE]; //float fc1_w[FC1_SIZE][CONV_W_NUM][POOL_SIZE][POOL_SIZE]; //float fc2_b[FC2_SIZE]; //float fc2_w[FC2_SIZE][FC1_SIZE]; // //float input[ROW][COL]; //float conv_z[CONV_W_NUM][CONV_SIZE][CONV_SIZE]; //float conv_a[CONV_W_NUM][CONV_SIZE][CONV_SIZE]; //int pool_pos[CONV_W_NUM][POOL_SIZE][POOL_SIZE]; //float pool[CONV_W_NUM][POOL_SIZE][POOL_SIZE]; //float fc1_z[FC1_SIZE]; //float fc1_a[FC1_SIZE]; //float fc2_z[FC2_SIZE]; //float fc2_a[FC2_SIZE]; //float output[FC2_SIZE]; //int answer[FC2_SIZE]; // //float conv_dw[CONV_W_NUM][CONV_W_SIZE][CONV_W_SIZE]; //float conv_db[CONV_W_NUM]; //float fc1_db[FC1_SIZE]; //float fc1_dw[FC1_SIZE][CONV_W_NUM][POOL_SIZE][POOL_SIZE]; //float fc2_db[FC2_SIZE]; //float fc2_dw[FC2_SIZE][FC1_SIZE]; //float C[FC2_SIZE]; //float fc2_delta[FC2_SIZE]; //float fc1_delta[FC1_SIZE]; //float conv_sigma_delta[CONV_W_NUM]; //float conv_delta[CONV_W_NUM][POOL_SIZE][POOL_SIZE]; // //int swap_endian(int val) //{ // unsigned char c1, c2, c3, c4; // c1 = val & 255; // c2 = (val >> 8) & 255; // c3 = (val >> 16) & 255; // c4 = (val >> 24) & 255; // return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4; //} //void load_data() //{ // FILE* f_images = fopen("D:\\\\Zufar\\\\CUDA-CNN\\\\CudaCNN2\\\\CudaCNN2\\\\data\\\\train-images.idx3-ubyte", "rb"); // FILE* f_labels = fopen("D:\\\\Zufar\\\\CUDA-CNN\\\\CudaCNN2\\\\CudaCNN2\\\\data\\\\train-labels.idx1-ubyte", "rb"); // // int tmp; // // int magic_num; // fread(&magic_num, sizeof(int), 1, f_images); // fread(&magic_num, sizeof(int), 1, f_labels); // // // printf("debug:%d\n",swap_endian(magic_num)); // // int train_size; // fread(&train_size, sizeof(int), 1, f_images); // fread(&train_size, sizeof(int), 1, f_labels); // train_size = swap_endian(train_size); // // // printf("debug:%d\n",swap_endian(train_size)); // // int rows, cols; // fread(&rows, sizeof(int), 1, f_images); // fread(&cols, sizeof(int), 1, f_images); // rows = swap_endian(rows); // cols = swap_endian(cols); // // // printf("debug:%d\n",swap_endian(rows)); // // printf("debug:%d\n",swap_endian(cols)); // // for (int i = 0;i < train_size;i++) // { // fread(&train_label[i], 1, 1, f_labels); // if (i % 1000 == 0) // printf("Training labels : Already read %5d labels\r", i); // // printf("%d:debug:%d\r",i,train_label[i]); // // system("pause"); // } // printf("Training labels : Already read %5d labels\n", train_size); // // for (int i = 0;i < train_size;i++) // { // for (int j = 0;j < rows;j++) // for (int k = 0;k < cols;k++) // { // tmp = 0; // fread(&tmp, 1, 1, f_images); // train_image[i][j][k] = tmp; // train_image[i][j][k] /= 255; // // printf("%d %d %d debug: %f\n",i,j,k,train_image[i][j][k]); // // system("pause"); // } // if (i % 1000 == 0) // printf("Training images : Already read %5d images\r", i); // } // printf("Training images : Already read %5d images\n", train_size); // // fclose(f_images); // fclose(f_labels); // // f_images = fopen("D:\\\\Zufar\\\\CUDA-CNN\\\\CudaCNN2\\\\CudaCNN2\\\\data\\\\t10k-images.idx3-ubyte", "rb"); // f_labels = fopen("D:\\\\Zufar\\\\CUDA-CNN\\\\CudaCNN2\\\\CudaCNN2\\\\data\\\\t10k-labels.idx1-ubyte", "rb"); // // fread(&magic_num, sizeof(int), 1, f_images); // fread(&magic_num, sizeof(int), 1, f_labels); // // int test_size; // fread(&test_size, sizeof(int), 1, f_images); // fread(&test_size, sizeof(int), 1, f_labels); // test_size = swap_endian(test_size); // // fread(&rows, sizeof(int), 1, f_images); // fread(&cols, sizeof(int), 1, f_images); // rows = swap_endian(rows); // cols = swap_endian(cols); // // for (int i = 0;i < test_size;i++) // { // fread(&test_label[i], 1, 1, f_labels); // if (i % 1000 == 0) // printf("Testing labels : Already read %5d labels\r", i); // } // printf("Testing labels : Already read %5d labels\n", test_size); // // for (int i = 0;i < test_size;i++) // { // for (int j = 0;j < rows;j++) // for (int k = 0;k < cols;k++) // { // tmp = 0; // fread(&tmp, 1, 1, f_images); // test_image[i][j][k] = tmp; // test_image[i][j][k] /= 255; // } // if (i % 1000 == 0) // printf("Testing images : Already read %5d images\r", i); // } // printf("Testing images : Already read %5d images\n\n", test_size); // // fclose(f_images); // fclose(f_labels); //} // //float sigmoid(float x) //{ // return (1 / (1 + exp(-1 * x))); //} // //void set_input(int idx, float image[TRAIN_NUM][ROW][COL]) //{ // for (int i = 0;i < ROW;i++) // for (int j = 0;j < COL;j++) // input[i][j] = image[idx][i][j]; //} // //void input_conv() //{ // for (int i = 0;i < CONV_W_NUM;i++) // for (int j = 0;j < CONV_SIZE;j++) // for (int k = 0;k < CONV_SIZE;k++) // { // conv_z[i][j][k] = 0; // for (int l = 0;l < CONV_W_SIZE;l++) // for (int m = 0;m < CONV_W_SIZE;m++) // conv_z[i][j][k] += input[j + l][k + m] * conv_w[i][l][m]; // conv_z[i][j][k] += conv_b[i]; // conv_a[i][j][k] = sigmoid(conv_z[i][j][k]); // } //} // //void conv_pool() //{ // for (int i = 0;i < CONV_W_NUM;i++) // for (int j = 0;j < POOL_SIZE;j++) // for (int k = 0;k < POOL_SIZE;k++) // { // float _max = conv_a[i][j * 2][k * 2]; // pool_pos[i][j][k] = 0; // if (conv_a[i][j * 2][k * 2 + 1] > _max) // { // _max = conv_a[i][j * 2][k * 2 + 1]; // pool_pos[i][j][k] = 1; // } // if (conv_a[i][j * 2 + 1][k * 2] > _max) // { // _max = conv_a[i][j * 2 + 1][k * 2]; // pool_pos[i][j][k] = 2; // } // if (conv_a[i][j * 2 + 1][k * 2 + 1] > _max) // { // _max = conv_a[i][j * 2 + 1][k * 2 + 1]; // pool_pos[i][j][k] = 3; // } // pool[i][j][k] = _max; // } //} // //void pool_fc1() //{ // for (int i = 0;i < FC1_SIZE;i++) // { // fc1_z[i] = 0; // for (int j = 0;j < CONV_W_NUM;j++) // for (int k = 0;k < POOL_SIZE;k++) // for (int l = 0;l < POOL_SIZE;l++) // fc1_z[i] += pool[j][k][l] * fc1_w[i][j][k][l]; // fc1_z[i] += fc1_b[i]; // fc1_a[i] = sigmoid(fc1_z[i]); // } //} // //void fc1_fc2() //{ // for (int i = 0;i < FC2_SIZE;i++) // { // fc2_z[i] = 0; // for (int j = 0;j < FC1_SIZE;j++) // fc2_z[i] += fc1_a[j] * fc2_w[i][j]; // fc2_z[i] += fc2_b[i]; // fc2_a[i] = sigmoid(fc2_z[i]); // } //} // //void set_answer(int idx, int label[TRAIN_NUM]) //{ // for (int i = 0;i < FC2_SIZE;i++) // { // output[i] = fc2_a[i]; // answer[i] = (label[idx] == i) ? 1 : 0; // } //} // //void check_answer(int& correct_cnt) //{ // float _max = output[0]; // int max_pos = 0; // for (int i = 0;i < FC2_SIZE;i++) // { // if (_max < output[i]) // { // _max = output[i]; // max_pos = i; // } // } // if (answer[max_pos]) // correct_cnt++; //} // //void get_error(float& avg_error) //{ // for (int i = 0;i < FC2_SIZE;i++) // { // C[i] = output[i] - answer[i]; // avg_error += C[i] * C[i] * 0.5; // } //} // // //void update_fc2_b() //{ // for (int i = 0;i < FC2_SIZE;i++) // { // fc2_delta[i] = alpha * C[i] * (fc2_a[i] * (1.0 - fc2_a[i])); // fc2_db[i] += fc2_delta[i]; // } //} // //void update_fc2_w() //{ // for (int i = 0;i < FC2_SIZE;i++) // for (int j = 0;j < FC1_SIZE;j++) // fc2_dw[i][j] += fc2_delta[i] * fc1_a[j]; //} // //void update_fc1_b() //{ // for (int i = 0;i < FC1_SIZE;i++) // { // float error = 0; // for (int j = 0;j < FC2_SIZE;j++) // error += fc2_delta[j] * fc2_w[j][i]; // fc1_delta[i] = error * (fc1_a[i] * (1.0 - fc1_a[i])); // fc1_db[i] += fc1_delta[i]; // } //} // //void update_fc1_w() //{ // for (int i = 0;i < FC1_SIZE;i++) // for (int j = 0;j < CONV_W_NUM;j++) // for (int k = 0;k < POOL_SIZE;k++) // for (int l = 0;l < POOL_SIZE;l++) // fc1_dw[i][j][k][l] += fc1_delta[i] * pool[j][k][l]; //} // //void update_conv_b() //{ // for (int i = 0;i < CONV_W_NUM;i++) // { // conv_sigma_delta[i] = 0; // for (int j = 0;j < POOL_SIZE;j++) // for (int k = 0;k < POOL_SIZE;k++) // { // float error = 0; // conv_delta[i][j][k] = 0; // for (int l = 0;l < FC1_SIZE;l++) // error += fc1_delta[l] * fc1_w[l][i][j][k]; // conv_delta[i][j][k] = error * (pool[i][j][k] * (1.0 - pool[i][j][k])); // conv_sigma_delta[i] += error * (pool[i][j][k] * (1.0 - pool[i][j][k])); // } // conv_db[i] += conv_sigma_delta[i]; // } //} // //void update_conv_w() //{ // for (int i = 0;i < CONV_W_NUM;i++) // for (int j = 0;j < CONV_W_SIZE;j++) // for (int k = 0;k < CONV_W_SIZE;k++) // { // float error = 0; // for (int m = 0;m < POOL_SIZE;m++) // for (int n = 0;n < POOL_SIZE;n++) // { // int x = pool_pos[i][m][n] / 2; // int y = pool_pos[i][m][n] % 2; // error += conv_delta[i][m][n] * input[2 * m + j + x][2 * n + k + y]; // } // conv_dw[i][j][k] += error; // } //} // //void assign_grads() //{ // for (int i = 0;i < FC2_SIZE;i++) // { // fc2_b[i] -= (fc2_db[i] / minibatch); // fc2_db[i] = 0; // } // // for (int i = 0;i < FC2_SIZE;i++) // for (int j = 0;j < FC1_SIZE;j++) // { // fc2_w[i][j] -= (fc2_dw[i][j] / minibatch); // fc2_dw[i][j] = 0; // } // // for (int i = 0;i < FC1_SIZE;i++) // { // fc1_b[i] -= (fc1_db[i] / minibatch); // fc1_db[i] = 0; // } // // for (int i = 0;i < FC1_SIZE;i++) // for (int j = 0;j < CONV_W_NUM;j++) // for (int k = 0;k < POOL_SIZE;k++) // for (int l = 0;l < POOL_SIZE;l++) // { // fc1_w[i][j][k][l] -= (fc1_dw[i][j][k][l] / minibatch); // fc1_dw[i][j][k][l] = 0; // } // // for (int i = 0;i < CONV_W_NUM;i++) // { // conv_b[i] -= (conv_db[i] / minibatch); // conv_db[i] = 0; // } // // for (int i = 0;i < CONV_W_NUM;i++) // for (int l = 0;l < CONV_W_SIZE;l++) // for (int m = 0;m < CONV_W_SIZE;m++) // { // conv_w[i][l][m] -= (conv_dw[i][l][m] / minibatch); // conv_dw[i][l][m] = 0; // } //} // //float get_rand(float fan_in) //{ // float sum = 0; // for (int i = 0;i < 12;i++) // sum += (float)rand() / RAND_MAX; // sum -= 6; // sum *= 1 / sqrt(fan_in); // return sum; //} //void init_params() //{ // for (int i = 0;i < CONV_W_NUM;i++) // { // for (int j = 0;j < CONV_W_SIZE;j++) // for (int k = 0;k < CONV_W_SIZE;k++) // conv_w[i][j][k] = get_rand(CONV_W_SIZE * CONV_W_SIZE); // conv_b[i] = get_rand(CONV_W_SIZE * CONV_W_SIZE); // } // // for (int i = 0;i < FC1_SIZE;i++) // { // for (int j = 0;j < CONV_W_NUM;j++) // for (int k = 0;k < POOL_SIZE;k++) // for (int l = 0;l < POOL_SIZE;l++) // fc1_w[i][j][k][l] = get_rand(POOL_SIZE * POOL_SIZE * CONV_W_NUM); // fc1_b[i] = get_rand(POOL_SIZE * POOL_SIZE * CONV_W_NUM); // } // // for (int i = 0;i < FC2_SIZE;i++) // { // for (int j = 0;j < FC1_SIZE;j++) // fc2_w[i][j] = get_rand(FC1_SIZE); // fc2_b[i] = get_rand(FC1_SIZE); // } //} //int main() { // // load_data(); // clock_t t = clock(); // init_params(); // // for (int i = 1;i <= epochs;i++) // { // correct_cnt = 0; // avg_error = 0; // // for (int j = 0;j < TRAIN_NUM;j++) // { // set_input(j, train_image); // input_conv(); // conv_pool(); // pool_fc1(); // fc1_fc2(); // set_answer(j, train_label); // check_answer(correct_cnt); // get_error(avg_error); // // update_fc2_b(); // update_fc2_w(); // update_fc1_b(); // update_fc1_w(); // update_conv_b(); // update_conv_w(); // if ((j + 1) % minibatch == 0) // assign_grads(); // // if (j && j % 100 == 0) // printf("Training Time spent : %.0fs Image count : %d Accuracy : %0.4f%% Error : %0.4f%% Epoch : %d \r", floor(((float)(clock() - t)) / CLOCKS_PER_SEC), j, ((float)correct_cnt / j) * 100, (avg_error / j) * 100, i); // } // printf("Training Time spent : %.0fs Image count : %d Accuracy : %0.4f%% Error : %0.4f%% Epoch : %d \n", floor(((float)(clock() - t)) / CLOCKS_PER_SEC), TRAIN_NUM, ((float)correct_cnt / TRAIN_NUM) * 100, (avg_error / TRAIN_NUM) * 100, i); // // correct_cnt = 0; // avg_error = 0; // // for (int j = 0;j < TEST_NUM;j++) // { // set_input(j, test_image); // input_conv(); // conv_pool(); // pool_fc1(); // fc1_fc2(); // set_answer(j, test_label); // check_answer(correct_cnt); // get_error(avg_error); // // if (j && j % 100 == 0) // printf("Testing Time spent : %.0fs Image count : %d Accuracy : %0.4f%% Error : %0.4f%% \r", floor(((float)(clock() - t)) / CLOCKS_PER_SEC), j, ((float)correct_cnt / j) * 100, (avg_error / j) * 100); // } // printf("Testing Time spent : %.0fs Image count : %d Accuracy : %0.4f%% Error : %0.4f%% \n", floor(((float)(clock() - t)) / CLOCKS_PER_SEC), TEST_NUM, ((float)correct_cnt / TEST_NUM) * 100, (avg_error / TEST_NUM) * 100); // // if ((float)correct_cnt / TEST_NUM * 100 > max_acc) // { // max_acc = (float)correct_cnt / TEST_NUM * 100; // //export_params(); // printf("The new model has been exported.Accuracy has reached to %0.5f%%\n\n", max_acc); // } // else // { // alpha = alpha - (alpha / 3); // printf("Learning rate has been reduced to %f\n\n", alpha); // } // } // // // // //float train_image[ROW][COL] = { // //{ 3, 1, 2, 4, 3, 3 }, // //{ 2, 4, 3, 1, 1, 4 }, // //{ 1, 5, 2, 3, 2, 5 }, // //{ 2, 3, 4, 1, 4, 1 }, // //{ 1, 4, 2, 1, 2, 3 }, // //{ 2, 3, 6, 5, 4, 1 }, }; // //float conv_w[CONV_W_NUM][CONV_W_SIZE][CONV_W_SIZE] = { { // //{1, 2, 3}, // //{4, 3, 1}, // //{1, 2, 4}}, // //{{4, 2, 5}, // //{2, 3, 1}, // //{1, 2, 3}} }; // // ////float conv_z[CONV_W_NUM][CONV_SIZE][CONV_SIZE]; // //float conv_z[2][2][2]; // //float train_label[2] = { 3,2 }; // // //cudaMemcpyToSymbol(_train_image, train_image, ROW * COL * sizeof(float)); // //cudaMemcpyToSymbol(_conv_w, conv_w, CONV_W_NUM * CONV_W_SIZE * CONV_W_SIZE * sizeof(float)); // ////cudaMemcpy(_train_label, train_label, 2 * sizeof(float), cudaMemcpyHostToDevice); // ////cudaMemcpy(_train_image, train_image, ROW * COL * sizeof(float), cudaMemcpyHostToDevice); // ////cudaMemcpy(_conv_w, conv_w, CONV_W_NUM*CONV_W_SIZE*CONV_W_SIZE*sizeof(float), cudaMemcpyHostToDevice); // //dim3 grid2(2, 4, 4); // // ////_input_conv << <1, grid2>> > ((float (*)[4])_train_image, (float (*)[3][3])_conv_w, (float (*)[2][2])_conv_z); // //_input_conv << <1, grid2 >> > (); // //_conv_pool << <1, grid2 >> > (); // ////cudaMemcpyFromSymbol(&conv_z, _pool, CONV_W_NUM * CONV_SIZE * CONV_SIZE * sizeof(float)); // //cudaMemcpyFromSymbol(&conv_z, _pool, 8 * sizeof(float)); // //for (int i = 0;i < 2;i++) { // // for (int j = 0;j <2;j++) { // // cout << conv_z[0][i][j] << " "; // // } // // cout << endl; // //} // //for (int i = 0;i < 2;i++) { // // for (int j = 0;j < 2;j++) { // // cout << conv_z[1][i][j] << " "; // // } // // cout << endl; // //} // return 0; //}
21,578
#include <stdio.h> #include <stdlib.h> #include <math.h> // CUDA kernel. Each thread takes care of one element of c // threadIdx.x gives thread id __global__ void vecAdd(double *a, double *b, double *c, int n) { // Get our global thread ID int id = threadIdx.x; // Make sure we do not go out of bounds if (id < n) c[id] = a[id] + b[id]; } int main( int argc, char* argv[] ) { int n = 1000; // Size of vectors int i; double *h_a, *h_b; // input vectors double *h_c; // output vector size_t bytes = n*sizeof(double); // Size, in bytes, of each vector // Allocate memory for each vector on host h_a = (double*)malloc(bytes); h_b = (double*)malloc(bytes); h_c = (double*)malloc(bytes); // Initialize vectors on host for( i = 0; i < n; i++ ) { h_a[i] = rand(); h_b[i] = rand(); } double *d_a, *d_b; // Device input vectors double *d_c; //Device output vector // Allocate memory for each vector on GPU cudaMalloc(&d_a, bytes); cudaMalloc(&d_b, bytes); cudaMalloc(&d_c, bytes); // Copy data into device (GPU) memory cudaMemcpy( d_a, h_a, bytes, cudaMemcpyHostToDevice); cudaMemcpy( d_b, h_b, bytes, cudaMemcpyHostToDevice); // Launch kernels vecAdd<<<1, n>>>(d_a, d_b, d_c, n); // Copy output data into Host memory cudaMemcpy( d_a, h_a, bytes, cudaMemcpyDeviceToHost); cudaMemcpy( d_b, h_b, bytes, cudaMemcpyDeviceToHost); // Free device memory cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); // WE ARE DONE – back in Host (CPU) processing // Free memory free(h_a); free(h_b); free(h_c); free(d_a); free(d_b); free(d_c); return 0; }
21,579
#include "includes.h" __global__ void kernel_histo_stride_2d( unsigned int *ct, unsigned int *histo){ // get unique id for each thread in each block unsigned int tid_x = threadIdx.x + blockDim.x*blockIdx.x; unsigned int tid_y = threadIdx.y + blockDim.y*blockIdx.y; unsigned int size = blockDim.x * gridDim.x; unsigned int max = constant_n_hits*constant_n_test_vertices; // map the two 2D indices to a single linear, 1D index int tid = tid_y * size + tid_x; /* unsigned int vertex_index = (int)(tid/constant_n_time_bins); unsigned int time_index = tid - vertex_index * constant_n_time_bins; // skip if thread is assigned to nonexistent vertex if( vertex_index >= constant_n_test_vertices ) return; // skip if thread is assigned to nonexistent hit if( time_index >= constant_n_time_bins ) return; unsigned int vertex_block = constant_n_time_bins*vertex_index; unsigned int vertex_block2 = constant_n_PMTs*vertex_index; */ unsigned int stride = blockDim.y * gridDim.y*size; while( tid < max ){ atomicAdd( &histo[ct[tid]], 1); tid += stride; } }
21,580
#include <stdio.h> #include <stdlib.h> #define BLOCK_SIZE 16 typedef struct { int width; int height; float* elements; } Matrix; __global__ void MatMulKernel(Matrix A, Matrix B, Matrix C) { // Each thread computes one element of C // by accumulating results into Cvalue float Cvalue = 0; int row = blockIdx.y * blockDim.y + threadIdx.y; int col = blockIdx.x * blockDim.x + threadIdx.x; if(row < A.height && col < B.width){ for (int e = 0; e < A.width; ++e) Cvalue += A.elements[row * A.width + e] * B.elements[e * B.width + col]; C.elements[row * C.width + col] = Cvalue; } } // Matrix multiplication - Host code // Matrix dimensions are assumed to be multiples of BLOCK_SIZE void MatMul(const Matrix A, const Matrix B, Matrix C) { // Load A and B to device memory Matrix d_A; d_A.width = A.width; d_A.height = A.height; size_t size_a = A.width * A.height * sizeof(float); cudaMalloc((void **)&d_A.elements, size_a); cudaMemcpy(d_A.elements, A.elements, size_a, cudaMemcpyHostToDevice); Matrix d_B; d_B.width = B.width; d_B.height = B.height; size_t size_b = B.width * B.height * sizeof(float); cudaMalloc((void **)&d_B.elements, size_b); cudaMemcpy(d_B.elements, B.elements, size_b, cudaMemcpyHostToDevice); // Allocate C in device memory Matrix d_C; d_C.width = C.width; d_C.height = C.height; size_t size_c = C.width * C.height * sizeof(float); cudaMalloc((void **)&d_C.elements, size_c); // Invoke kernel dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid((B.width + dimBlock.x - 1) / dimBlock.x, (A.height + dimBlock.y - 1)/ dimBlock.y); // dim3 dimGrid(B.width / dimBlock.x, A.height / dimBlock.y); MatMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C); //cudaDeviceSynchronize(); // Read C from device memory cudaMemcpy(C.elements, d_C.elements, size_c, cudaMemcpyDeviceToHost); // Free device memory cudaFree(d_A.elements); cudaFree(d_B.elements); cudaFree(d_C.elements); } int main(int argc, char **argv){ //Initialize matrixs Matrix A; A.width = 3; A.height = 5; A.elements = (float*) malloc(A.width*A.height*sizeof(float)); for(int i = 0 ; i < A.width*A.height ; i++ ){ A.elements[i] = float(i); } Matrix B; B.width = 5; B.height = 3; B.elements = (float*) malloc(B.width*B.height*sizeof(float)); for(int i = 0 ; i < B.width*B.height ; i++ ){ B.elements[i] = float(i); } for(int i = 0 ; i < A.width*A.height ; i++ ){ printf("%f\t", A.elements[i]); if((i % A.width ) == A.width - 1){printf("\n");} } for(int i = 0 ; i < B.width*B.height ; i++ ){ printf("%f\t", B.elements[i]); if((i % B.width ) == B.width - 1){printf("\n");} } printf("=========================================\n"); Matrix C; C.width = 5; C.height = 5; C.elements = (float*) malloc(C.width*C.height*sizeof(float)); for(int i = 0 ; i < C.width*C.height ; i++ ){ C.elements[i] = float(i); } MatMul(A, B, C); for(int i = 0 ; i < C.width*C.height ; i++ ){ printf("%5f\t", C.elements[i]); if((i % C.width ) == C.width - 1){printf("\n");} } return 0; }
21,581
#include <stdio.h> #include <stdlib.h> #include <string.h> /* Note we send in a pointer to the file pointer becuase this function needs to update the position of the file pointer */ void get_file_data(FILE **fp, int *nchans, int *nsamples, int *nsamp, int *nifs, int *nbits, float *tsamp, float *tstart, float *fch1, float *foff) { fpos_t file_loc; char *string = (char *) malloc(80 * sizeof(char)); int nchar; int nbytes = sizeof(int); long int total_data; double temp; while (1) { strcpy(string, "ERROR"); fread(&nchar, sizeof(int), 1, *fp); if (feof(*fp)) exit(0); if (nchar > 1 && nchar < 80) { fread(string, nchar, 1, *fp); string[nchar] = '\0'; // For debugging only printf("\n%d\t%s", nchar, string), fflush(stdout); nbytes += nchar; if (strcmp(string, "HEADER_END") == 0) break; if (strcmp(string, "tsamp") == 0) { fread(&temp, sizeof(double), 1, *fp); *tsamp = (float) temp; } else if (strcmp(string, "tstart") == 0) { fread(&temp, sizeof(double), 1, *fp); *tstart = (float) temp; } else if (strcmp(string, "fch1") == 0) { fread(&temp, sizeof(double), 1, *fp); *fch1 = (float) temp; } else if (strcmp(string, "foff") == 0) { fread(&temp, sizeof(double), 1, *fp); *foff = (float) temp; } else if (strcmp(string, "nchans") == 0) { fread(nchans, sizeof(int), 1, *fp); } else if (strcmp(string, "nifs") == 0) { fread(nifs, sizeof(int), 1, *fp); } else if (strcmp(string, "nbits") == 0) { fread(nbits, sizeof(int), 1, *fp); } else if (strcmp(string, "nsamples") == 0) { fread(nsamples, sizeof(int), 1, *fp); } } } // Check that we are working with one IF channel if (*nifs != 1) { printf("\nERROR!! Can only work with one IF channel!\n"); exit(1); } fgetpos(*fp, &file_loc); long data_start = ftell(*fp); if (fseek(*fp, 0, SEEK_END) != 0) { printf("\nERROR!! Failed to seek to end of data file\n"); exit(1); } total_data = ftell(*fp); if (total_data == -1) { printf("\nERROR!! Failed to seek to end of data file\n"); exit(1); } total_data -= data_start; if (( *nbits ) == 32) { *nsamp = (total_data/sizeof(float)) - 1; } else if (( *nbits ) == 8) { *nsamp = (total_data/sizeof(unsigned char)) - 1; } else { printf("\n\n======================= ERROR =======================\n"); printf(" Currently this code only runs with 8 and 32 bit data\n"); printf("\n=====================================================\n"); } // Move the file pointer back to the end of the header fsetpos(*fp, &file_loc); }
21,582
#include "includes.h" __global__ void myfunc() { }
21,583
#include<iostream> using namespace std; int main() { int deviceCount; cudaGetDeviceCount(&deviceCount); //Returns in *deviceCount the number of devices cout<<"deviceCount: "<<deviceCount<<"\n\n"; if (deviceCount == 0) { cout<< "error: no devices supporting CUDA.\n"; exit(EXIT_FAILURE); } int dev = 0; cudaSetDevice(dev); //Sets dev=0 device as the current device for the calling host thread. cudaDeviceProp devProps; cudaGetDeviceProperties(&devProps, dev); cout<<"name: "<<devProps.name<<"\n"; cout<<"totalGlobalMem: "<<devProps.totalGlobalMem<<"\n"; cout<<"regsPerBlock: "<<devProps.regsPerBlock<<"\n"; cout<<"warpSize: "<<devProps.warpSize<<"\n"; cout<<"memPitch: "<<devProps.memPitch<<"\n\n"; cout<<"一个线程块中可使用的最大共享内存\n"; cout<<"devProps.sharedMemPerBlock: "<<devProps.sharedMemPerBlock<<"\n\n"; cout<<"一个线程块中可包含的最大线程数量\n"; cout<<"maxThreadsPerBlock: "<<devProps.maxThreadsPerBlock<<"\n\n"; cout<<"多维线程块数组中每一维可包含的最大线程数量\n"; cout<<"maxThreadsDim[0]: "<<devProps.maxThreadsDim[0]<<"\n"; cout<<"maxThreadsDim[1]: "<<devProps.maxThreadsDim[1]<<"\n"; cout<<"maxThreadsDim[2]: "<<devProps.maxThreadsDim[2]<<"\n\n"; cout<<"一个线程格中每一维可包含的最大线程块数量\n"; cout<<"maxGridSize[0]: "<<devProps.maxGridSize[0]<<"\n"; cout<<"maxGridSize[1]: "<<devProps.maxGridSize[1]<<"\n"; cout<<"maxGridSize[2]: "<<devProps.maxGridSize[2]<<"\n\n"; cout<<"clockRate: "<<devProps.clockRate<<"\n"; cout<<"totalConstMem: "<<devProps.totalConstMem<<"\n"; cout<<"textureAlignment: "<<devProps.textureAlignment<<"\n\n"; cout<<"计算能力:"<<devProps.major<< "." <<devProps.minor<<"\n\n"; cout<<"minor: "<<devProps.minor<<"\n"; cout<<"texturePitchAlignment: "<<devProps.texturePitchAlignment<<"\n"; cout<<"deviceOverlap: "<<devProps.deviceOverlap<<"\n"; cout<<"multiProcessorCount: "<<devProps.multiProcessorCount<<"\n"; cout<<"kernelExecTimeoutEnabled: "<<devProps.kernelExecTimeoutEnabled<<"\n"; cout<<"integrated: "<<devProps.integrated<<"\n"; cout<<"canMapHostMemory: "<<devProps.canMapHostMemory<<"\n"; cout<<"computeMode: "<<devProps.computeMode<<"\n"; cout<<"maxTexture1D: "<<devProps.maxTexture1D<<"\n"; cout<<"maxTexture1DMipmap: "<<devProps.maxTexture1DMipmap<<"\n"; cout<<"maxTexture1DLinear: "<<devProps.maxTexture1DLinear<<"\n"; cout<<"maxTexture2D: "<<devProps.maxTexture2D<<"\n"; cout<<"maxTexture2DMipmap: "<<devProps.maxTexture2DMipmap<<"\n"; cout<<"maxTexture2DLinear: "<<devProps.maxTexture2DLinear<<"\n"; cout<<"maxTexture2DGather: "<<devProps.maxTexture2DGather<<"\n"; cout<<"maxTexture3D: "<<devProps.maxTexture3D<<"\n"; cout<<"maxTexture3DAlt: "<<devProps.maxTexture3DAlt<<"\n"; cout<<"maxTextureCubemap: "<<devProps.maxTextureCubemap<<"\n"; cout<<"maxTexture1DLayered: "<<devProps.maxTexture1DLayered<<"\n"; cout<<"maxTexture2DLayered: "<<devProps.maxTexture2DLayered<<"\n"; cout<<"maxTextureCubemapLayered: "<<devProps.maxTextureCubemapLayered<<"\n"; cout<<"maxSurface1D: "<<devProps.maxSurface1D<<"\n"; cout<<"maxSurface2D: "<<devProps.maxSurface2D<<"\n"; cout<<"maxSurface3D: "<<devProps.maxSurface3D<<"\n"; cout<<"maxSurface1DLayered: "<<devProps.maxSurface1DLayered<<"\n"; cout<<"maxSurface2DLayered: "<<devProps.maxSurface2DLayered<<"\n"; cout<<"maxSurfaceCubemap: "<<devProps.maxSurfaceCubemap<<"\n"; cout<<"maxSurfaceCubemapLayered: "<<devProps.maxSurfaceCubemapLayered<<"\n"; cout<<"surfaceAlignment: "<<devProps.surfaceAlignment<<"\n"; cout<<"concurrentKernels: "<<devProps.concurrentKernels<<"\n"; cout<<"ECCEnabled: "<<devProps.ECCEnabled<<"\n"; cout<<"pciBusID: "<<devProps.pciBusID<<"\n"; cout<<"pciDeviceID: "<<devProps.pciDeviceID<<"\n"; cout<<"pciDomainID: "<<devProps.pciDomainID<<"\n"; cout<<"tccDriver: "<<devProps.tccDriver<<"\n"; cout<<"asyncEngineCount: "<<devProps.asyncEngineCount<<"\n"; cout<<"unifiedAddressing: "<<devProps.unifiedAddressing<<"\n"; cout<<"memoryClockRate: "<<devProps.memoryClockRate<<"\n"; cout<<"memoryBusWidth: "<<devProps.memoryBusWidth<<"\n"; cout<<"l2CacheSize: "<<devProps.l2CacheSize<<"\n"; cout<<"maxThreadsPerMultiProcessor: "<<devProps.maxThreadsPerMultiProcessor<<"\n"; cout<<"streamPrioritiesSupported: "<<devProps.streamPrioritiesSupported<<"\n"; cout<<"globalL1CacheSupported: "<<devProps.globalL1CacheSupported<<"\n"; cout<<"localL1CacheSupported: "<<devProps.localL1CacheSupported<<"\n"; cout<<"sharedMemPerMultiprocessor: "<<devProps.sharedMemPerMultiprocessor<<"\n"; cout<<"regsPerMultiprocessor: "<<devProps.regsPerMultiprocessor<<"\n"; cout<<"isMultiGpuBoard: "<<devProps.isMultiGpuBoard<<"\n"; cout<<"multiGpuBoardGroupID: "<<devProps.multiGpuBoardGroupID<<"\n"; cout<<"singleToDoublePrecisionPerfRatio: "<<devProps.singleToDoublePrecisionPerfRatio<<"\n"; cout<<"pageableMemoryAccess: "<<devProps.pageableMemoryAccess<<"\n"; cout<<"concurrentManagedAccess: "<<devProps.concurrentManagedAccess<<"\n"; }
21,584
#include <stdio.h> #include <math.h> int test(int a, int b) { printf(">>%d, %d\n", a, b); return 0; } void* makeVectorAddArgsFloat(int size) { float* mem = (float*)malloc(size); float* a1 = mem+1; float* b1 = a1+32; float* c1 = b1+32; for (int idx = 0; idx < 32; ++idx) { a1[idx] = idx; b1[idx] = idx; c1[idx] = 0; } mem[0] = 32; return (void*)mem; } void* makeVectorAddArgs(int N, int & size) { size = (3*N+1)*sizeof(int); int* mem = (int*)malloc(size); int* a1 = mem+1; int* b1 = a1+N; int* c1 = b1+N; for (int idx = 0; idx < N; ++idx) { a1[idx] = idx; b1[idx] = idx; c1[idx] = 0; } mem[0] = N; return (void*)mem; } float *makeMatrixTranspose(int ROW, int& size) { int COLUMN = ROW; int a=0, b=0; size = (2*ROW*ROW+1)*sizeof(float); float *stuff = (float *) malloc(size); stuff[0] = ROW; float* matrixIn = stuff+1; float* matrixOut = matrixIn + ROW*ROW; for(a=0; a<ROW;a++) { for(b=0; b<COLUMN;b++) { //matrix[b + a * ROW]=((float)rand())/((float) RAND_MAX); matrixIn[b + a * ROW]=b; matrixOut[b + a * ROW]=0; } } return stuff; } float *makeMatrixInverse(int ROW, int& size) { float* stuff = makeMatrixTranspose(ROW, size); float* matrixIdent = stuff + 1 + ROW*ROW; for (int idx = 0; idx < ROW; ++idx) { for (int jdx = 0; jdx < ROW; ++jdx) { if (idx == jdx) matrixIdent[idx*ROW+jdx] = 1; else matrixIdent[idx*ROW+jdx] = 0; } } return stuff; } void *makeMatrix(int ROW, int& size) { int COLUMN = ROW; int a=0, b=0; size = (1+2*ROW*COLUMN)*sizeof(float); float *stuff = (float *) malloc(size); stuff[0] = ROW; for(a=0; a<ROW;a++) { for(b=0; b<COLUMN;b++) { stuff[a + b * ROW]=((float)rand())/((float) RAND_MAX); stuff[a + b * ROW + ROW * COLUMN] = 0.0; } } return stuff; } void* makeMatrixMult(int ROW, int& size) { int COLUMN = ROW; int a=0, b=0; size = (3*ROW*ROW+1)*sizeof(float); float *stuff = (float *) malloc(size); float* orig = stuff; // first parameter is the matrix size *stuff = ROW; // increment the pointer by one stuff = stuff+1; for(a=0; a<ROW;a++) { for(b=0; b<COLUMN;b++) { stuff[a + b * ROW]= ((float)rand())/((float) RAND_MAX); stuff[a + b * ROW + ROW * COLUMN] = ((float)rand())/((float) RAND_MAX); stuff[a + b * ROW + 2*ROW * COLUMN] = 0.0; } } return orig; } void* makeMatrixVectorArgs(int ROWS, int& size) { size = (ROWS*ROWS+2*ROWS+1)*sizeof(int); int* param = (int*)malloc(size); param[0] = ROWS; int* matrix = param+1; int* vecA = matrix+ROWS*ROWS; int* vecB = vecA+ROWS; // idx = row for (int idx=0;idx<ROWS;++idx) { // for each column value, jdx = column for (int jdx=0;jdx<ROWS;++jdx) matrix[jdx+idx*ROWS]=idx; vecA[idx]=idx; vecB[idx]=idx; } return (void*)param; } void* allocateStencil(int N, int& size) { float xmin = 0.0f; float xmax = 3.5f; float ymin = 0.0f; //float ymax = 2.0f; float h = (xmax-xmin)/(N-1); float dt = 0.00001f; float alpha = 0.645f; float time = 0.4f; int steps = ceil(time/dt); int I; //float *u = new float[N*N]; //float *u_host = new float[N*N]; size = sizeof(float)*(5+2*N*N); float* param = (float*)malloc(sizeof(float)*size); param[0] = N; param[1] = h; param[2] = dt; param[3] = alpha; param[4] = N; float* u = param+5; float* u_host = u + N*N; // Generate mesh and intial condition for (int j=0; j<N; j++) { for (int i=0; i<N; i++) { I = N*j + i; u[I] = 0.0f; u_host[I] = 0.0f; if ( (i==0) || (j==0)) {u[I] = 200.0f;} } } return (void*)param; } float RandFloat(float low, float high) { float t = (float)rand() / (float)RAND_MAX; return (1.0f - t) * low + t * high; } void* allocateBlackScholes(int N, int& size) { const int OPT_N = N;//4000000; const int OPT_SZ = OPT_N * sizeof(float); const float RISKFREE = 0.02f; const float VOLATILITY = 0.30f; float *h_CallResultCPU, *h_PutResultCPU; float *h_StockPrice, *h_OptionStrike, *h_OptionYears; size = OPT_SZ*5+3*sizeof(float); float* param = (float*)malloc(size); param[0] = RISKFREE; param[1] = VOLATILITY; param[2] = OPT_N; h_CallResultCPU = param+3; h_PutResultCPU = h_CallResultCPU + OPT_N; h_StockPrice = h_PutResultCPU + OPT_N; h_OptionStrike = h_StockPrice + OPT_N; h_OptionYears = h_OptionStrike + OPT_N; srand(5347); //Generate options set for(int i = 0; i < OPT_N; i++) { h_CallResultCPU[i] = 0.0f; h_PutResultCPU[i] = -1.0f; h_StockPrice[i] = RandFloat(5.0f, 30.0f); h_OptionStrike[i] = RandFloat(1.0f, 100.0f); h_OptionYears[i] = RandFloat(0.25f, 10.0f); } return (void*)param; }
21,585
#include "includes.h" __global__ void reduction(int * in, int * out){ int globalid = blockIdx.x*blockDim.x + threadIdx.x; __shared__ int s_array[BLOCK_DIM]; s_array[threadIdx.x] = in[globalid]; __syncthreads(); for (int i = 1; i < blockDim.x; i *= 2){ if (threadIdx.x % (2*i) == 0){ s_array[threadIdx.x] += s_array[threadIdx.x+i]; } __syncthreads(); } if (threadIdx.x == 0) out[blockIdx.x] = s_array[0]; }
21,586
#include "includes.h" __global__ void sumMatrixOnGPU2D(float *A, float *B, float *C, const int nx, const int ny){ int ix = blockIdx.x * blockDim.x + threadIdx.x; int iy = blockIdx.y * blockDim.y + threadIdx.y; int idx = iy * nx + ix; if(ix < nx && iy < ny) C[idx] = A[idx] + B[idx]; }
21,587
#include "includes.h" __global__ void kernel_update( float4* d_positions, float4* d_og_positions, float4* d_velocities, float* d_masses, size_t numel) { size_t col = threadIdx.x + blockIdx.x * blockDim.x; if (col >= numel) { return; } float4 velocity = d_velocities[col]; float mag = sqrtf(velocity.x*velocity.x + velocity.y*velocity.y)*0.03; float pos = min(mag, 0.50f); d_positions[col] = make_float4( d_og_positions[col].x, d_og_positions[col].y, pos, 0 ); __syncthreads(); }
21,588
/* * CURAND API: inizio uso dei PseudoRandom Number Generator * limitazione a 65535 kernel, lancio monodimensionale/bidimensionale * attenzione al numero N...non riesco a processare molti dati usando in contemporanea il monitor */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <cuda.h> #include <curand_kernel.h> #define PI 3.14159265358979323846 #define N 128 #define N2 N __global__ void setup_kernel ( curandStateXORWOW_t * state){ int x = threadIdx.x + blockIdx.x * blockDim.x; int y = threadIdx.y + blockIdx.y * blockDim.y; int offset = x+ y*blockDim.x*gridDim.x; while (offset<N){ /* Each thread gets same seed , a different sequence number no offset */ curand_init (1234 , offset , 0 , &state[offset]); offset += blockDim.x*gridDim.x; __syncthreads(); } } __global__ void generate_bit_kernel ( curandStateXORWOW_t * state , float * result ){ int x = threadIdx.x + blockIdx.x * blockDim.x; int y = threadIdx.y + blockIdx.y * blockDim.y; int offset = x + y*blockDim.x*gridDim.x; while (offset < N){ curandStateXORWOW_t localState = state[offset]; float awgn = curand_normal(&localState); result[offset] = awgn; //state[offset]=localState; offset += blockDim.x*gridDim.x; __syncthreads(); } } int main ( int argc , char * argv []){ int i; //dim3 dimGrid(1024); //numero block //dim3 dimBlock(512); //numero threads per block dim3 dimGrid(8,8); dim3 dimBlock(16,16); // 2 dim max 512 curandStateXORWOW_t * devStates ; float *hostResults, *devResults; /* Allocate space for results on host */ hostResults = ( float *) calloc (N2 , sizeof(float) ); /* Allocate space for results on device */ cudaMalloc (( void **) &devResults , N2 * sizeof(float) ); /* Set results to 0 */ cudaMemset ( devResults , 2, N2 * sizeof(float) ); /* Allocate space for prng states on device */ cudaMalloc (( void **) &devStates , N2 * sizeof(curandStateXORWOW_t) ); /* Setup prng states */ setup_kernel <<<dimGrid, dimBlock>>>( devStates ) ; cudaThreadSynchronize(); /* Generate and use pseudo - random */ //for ( i = 0; i < 10; i++) { generate_bit_kernel <<<dimGrid,dimBlock>>>( devStates , devResults ) ; cudaThreadSynchronize(); //} /* Copy device memory to host */ cudaMemcpy ( hostResults , devResults , N2 * sizeof(float) , cudaMemcpyDeviceToHost ) ; /* Show result */ float tmp; for ( i=0; i < N; i++ ) { tmp+=hostResults[i]; } printf("%f \n",tmp); /* Cleanup */ cudaFree(devStates); cudaFree(devResults); free(hostResults); system("pause"); return EXIT_SUCCESS; }
21,589
/** * Copyright: Fynn Schröder, 2019 * Author: Fynn Schröder * License: MIT */ template<typename T, unsigned int blockSize> __global__ void kernelMax(const T* __restrict__ input, T* __restrict__ per_block_results, const size_t n) { __shared__ float sdata[blockSize]; __shared__ unsigned int sidx[blockSize]; T x = -1e38; unsigned int idx = UINT_MAX; for(unsigned int i=threadIdx.x; i < n; i += blockDim.x) { const T val = input[i + blockIdx.x * n]; if (val > x) { x = val; idx = i; } } sdata[threadIdx.x] = x; sidx[threadIdx.x] = idx; __syncthreads(); for (unsigned int s=blockDim.x/2; s>0; s>>=1) { if (threadIdx.x < s) { if (sdata[threadIdx.x + s] > sdata[threadIdx.x]) { sdata[threadIdx.x] = sdata[threadIdx.x + s]; sidx[threadIdx.x] = sidx[threadIdx.x + s]; } } __syncthreads(); } // thread 0 writes the final result if(threadIdx.x == 0) { per_block_results[blockIdx.x * 2] = sdata[0]; per_block_results[blockIdx.x * 2 + 1] = __int_as_float(sidx[0]); } } extern "C" void max_idx_all(const float * device_results, const unsigned int rows, const unsigned int columns, float * device_maxima, cudaStream_t stream = NULL) { dim3 blocksize(256); dim3 gridsize(columns); kernelMax<float, 256><<<gridsize, blocksize, 0, stream>>>(device_results, device_maxima, rows); } template<typename T> __global__ void kernelMaxOpt(const T* __restrict__ input, T* __restrict__ per_block_results, const size_t n, const size_t m, const unsigned int offset, const bool overwrite) { T x = -1e38; unsigned int idx = UINT_MAX; for (int i = 0; i < n; i += 1) { if (threadIdx.x + blockIdx.x * blockDim.x < m) { float val = input[i * m + threadIdx.x + blockIdx.x * blockDim.x]; if (val > x) { x = val; idx = i; } } } if (threadIdx.x + blockIdx.x * blockDim.x < m) { if (overwrite || x > per_block_results[(blockIdx.x * blockDim.x + threadIdx.x) * 2]) { per_block_results[(blockIdx.x * blockDim.x + threadIdx.x) * 2] = x; per_block_results[(blockIdx.x * blockDim.x + threadIdx.x) * 2 + 1] = __int_as_float(idx + offset); } } } extern "C" void max_idx_t(const float * device_results, const unsigned int rows, const unsigned int columns, float * device_maxima, const unsigned int offset, const bool overwrite, cudaStream_t stream = NULL) { int blocksize = 256; int gridsize = (rows + blocksize -1) / blocksize; kernelMaxOpt<float><<<gridsize, blocksize, 0, stream>>>(device_results, device_maxima, columns, rows , offset, overwrite); }
21,590
#include <iostream> #include <fstream> #include <sstream> #include <string> #include <cmath> #define TILE_W 32 //Decrease to increment block count and decrement thread count #define TILE_H 32 //Decrease to increment block count and decrement thread count #define R 1 #define D ((R*2)+1) #define S (D*D) //For debug purposes void printImageMatrix(unsigned char** image, int height, int width) { for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { std::cout << image[i][j] << " "; } std::cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << std::endl; } } unsigned char* char_flattenImage(unsigned char** image, int height, int width) { unsigned char* image_1D = new unsigned char[height*width]; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { image_1D[j + i*width] = image[i][j]; } } std::cout << "Char image flattened" << std::endl; return image_1D; } float* flattenImage(unsigned char** image, int height, int width) { float* image_1D = new float[height*width]; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { image_1D[j + i*width] = image[i][j]; } } std::cout << "Float image flattened" << std::endl; return image_1D; } void fileToMatrix(unsigned char** &image, char* fileName, int* height, int* width) { std::ifstream imageFile(fileName); if(imageFile.is_open()) { std::string line; getline(imageFile, line); std::istringstream iss(line); iss >> *(height) >> *(width); std::cout << "Height: " << *height << " ||-|| Width: " << *width << std::endl; image = new unsigned char*[*(height)]; for(int i = 0; i < *(height); i++) { image[i] = new unsigned char[*(width)]; } int h = 0; int val; while(getline(imageFile, line)) { int w = 0; std::istringstream iss(line); while(iss >> val) { image[h][w++] = val; } h++; } std::cout << "Image saved to matrix..." << std::endl; } imageFile.close(); } void matrixToFile(char* fileName, unsigned char* image, int height, int width) { std::ofstream outFile; outFile.open(fileName); outFile << height << " " << width << '\n'; for(int i = 0; i < height*width; i++) { int x = i % width; int y = i / width; if(i != 0 && x == 0) outFile << '\n'; outFile << int(image[x + y*width]) << " "; } outFile.close(); } void matrixToFile(char* fileName, float* image, int height, int width) { std::ofstream outFile; outFile.open(fileName); outFile << height << " " << width << '\n'; for(int i = 0; i < height*width; i++) { int x = i % width; int y = i / width; if(i != 0 && x == 0) outFile << '\n'; outFile << int(image[x + y*width]) << " "; } outFile.close(); } void getMinAndMax(unsigned char** image, int height, int width, int* min, int* max) { for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { if(image[i][j] > *(max)) { *max = image[i][j]; } if(image[i][j] < *(min)) { *min = image[i][j]; } } } std::cout << "Min: " << *min << " ||-|| Max: " << *max << std::endl; } double getAverage(unsigned char** image, int height, int width) { float sum = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { sum += image[i][j]; } } return sum / (width * height); } __global__ void linearScale(float* image, float* outImage, int* a, int* width, int* height, float* b) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; float u = image[index]; outImage[index] = (*b) * (u + (*a)); } __global__ void grayWorld(float* image, float* outImage, double* scalingValue, int* height, int* width) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; float u = image[index]; outImage[index] = u * *scalingValue; } __global__ void reflection(float* image, float* outImage, int* height, int* width) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; int i = index / *width; int j = index - i * *width; int reflectedIndex = i * *width + (*width - j - 1); float u = image[reflectedIndex]; outImage[index] = u; } __global__ void orderedDithering(float* image, float* outImage, int* height, int* width) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; int i = index / *width; int j = index - i * *width; float u = image[index]; if(i%2 == 0) { if(j%2 == 0) { if(u > 192) outImage[index] = 255; else outImage[index] = 0; } else { if(u > 64) outImage[index] = 255; else outImage[index] = 0; } } else { if(j%2 == 0) outImage[index] = 255; else { if(u > 128) outImage[index] = 255; else outImage[index] = 0; } } } __global__ void rotate90(float* result, float* image, int* height, int* width) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; int i = index / *width; int j = index - i * *width; int rotatedIndex = (*width - j - 1) * *height + i; float u = image[index]; result[rotatedIndex] = u; } __global__ void rotate180(float* result, float* image, int* height, int* width) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; int i = index / *width; int j = index - i * *width; int rotatedIndex = (*height - i - 1) * *width + (*width - j - 1); float u = image[rotatedIndex]; result[index] = u; } __global__ void medianFilter(unsigned char* image, unsigned char* outImage, int* height, int* width) { int im_y = blockIdx.y * blockDim.y + threadIdx.y; int im_x = blockIdx.x * blockDim.x + threadIdx.x; if(im_y == 0 || im_y == (*height) - 1 || im_x == 0 || im_x == (*width) - 1) { outImage[im_y * (*width) + im_x] = 0; return; } int index = im_y * (*width) + im_x; if(index >= (*height) * (*width)) return; int image_index; __shared__ unsigned char window[S]; int window_index = 0; for(int i = -1; i <= 1; i++) { for(int j = -1; j <= 1; j++) { image_index = index + (i * (*width)) + j; window[window_index++] = image[image_index]; } } int key, i ,j; for(i = 1; i < S; i++) { key = window[i]; j = i - 1; while(j >= 0 && window[j] > key) { window[j + 1] = window[j]; j = j - 1; } window[j + 1] = key; } outImage[index] = window[S/2]; } int main(int argc, char** argv) { char* inFileName = argv[1], *outFileName; int writeOut = atoi(argv[2]); //IMAGE INIT int height, width, max = -1, min = 256; unsigned char** hostImage; fileToMatrix(hostImage, inFileName, &height, &width); getMinAndMax(hostImage, height, width, &min, &max); float* hostFlattened = flattenImage(hostImage, height, width); unsigned char* char_hostFlattened = char_flattenImage(hostImage, height, width); //GPU INIT const int image_size = sizeof(float) * (height*width); const int charImage_size = sizeof(unsigned char) * (height*width); unsigned char* char_deviceImage; float* deviceImage; float* hostResult = new float[height*width]; cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop); int BLOCK_H = (int)std::ceil((float)width / TILE_H); int BLOCK_W = (int)std::ceil((float)height / TILE_W); dim3 deviceBlocks(BLOCK_H, BLOCK_W); dim3 deviceThreads(TILE_H, TILE_W); int *height_d, *width_d; float time = 0; cudaMalloc(&height_d, sizeof(int)); cudaMalloc(&width_d, sizeof(int)); cudaMemcpy(height_d, &height, sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(width_d, &width, sizeof(int), cudaMemcpyHostToDevice); cudaMalloc(&deviceImage, image_size); cudaMemcpy(deviceImage, hostFlattened, image_size, cudaMemcpyHostToDevice); //*************************LINSCALE******************************// int a = -1 * min, *d_a; float gmax = 255.0; float b = gmax / (max - min), *d_b; float* linearScaleResult; cudaMalloc(&linearScaleResult, image_size); cudaMalloc(&d_a, sizeof(int)); cudaMalloc(&d_b, sizeof(float)); cudaMemcpy(d_a, &a, sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_b, &b, sizeof(float), cudaMemcpyHostToDevice); cudaEventRecord(start, 0); linearScale<<<deviceBlocks, deviceThreads>>>(deviceImage, linearScaleResult, d_a, width_d, height_d, d_b); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); std::cout << "LinScale time: " << time/1000 << "sec" << std::endl; cudaMemcpy(hostResult, linearScaleResult, image_size, cudaMemcpyDeviceToHost); if(writeOut) { outFileName = "CUDA_linear_scale_.txt"; matrixToFile(outFileName, hostResult, height, width); } cudaFree(d_a); cudaFree(d_b); cudaFree(linearScaleResult); //*************************LINSCALE******************************// //*************************GRAYWORLD******************************// double average = getAverage(hostImage, height, width); double scalingValue = 127.5 / average, *d_scale;; float* grayWorldResult; cudaMalloc(&grayWorldResult, image_size); cudaMalloc((void**)&d_scale, sizeof(double)); cudaMemcpy(d_scale, &scalingValue, sizeof(double), cudaMemcpyHostToDevice); cudaEventRecord(start, 0); grayWorld<<<deviceBlocks, deviceThreads>>>(deviceImage, grayWorldResult, d_scale, height_d, width_d); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); cudaMemcpy(hostResult, grayWorldResult, image_size, cudaMemcpyDeviceToHost); std::cout << "GrayWorld time: " << time/1000 << "sec" << std::endl; if(writeOut) { outFileName = "CUDA_gray_world_.txt"; matrixToFile(outFileName, hostResult, height, width); } cudaFree(grayWorldResult); cudaFree(d_scale); //*************************GRAYWORLD******************************// //REFLECTION float* reflectionResult; cudaMalloc(&reflectionResult, image_size); cudaEventRecord(start, 0); reflection<<<deviceBlocks, deviceThreads>>>(deviceImage, reflectionResult, height_d, width_d); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); cudaMemcpy(hostResult, reflectionResult, image_size, cudaMemcpyDeviceToHost); std::cout << "Reflection time: " << time/1000 << "sec" << std::endl; if(writeOut) { outFileName = "CUDA_reflection_.txt"; matrixToFile(outFileName, hostResult, height, width); } cudaFree(reflectionResult); //ORDERED DITHERING float* orderedDitheringResult; cudaMalloc(&orderedDitheringResult, image_size); cudaEventRecord(start, 0); orderedDithering<<<deviceBlocks, deviceThreads>>>(deviceImage, orderedDitheringResult, height_d, width_d); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); cudaMemcpy(hostResult, orderedDitheringResult, image_size, cudaMemcpyDeviceToHost); std::cout << "Ordered Dithering time: " << time/1000 << "sec" << std::endl; if(writeOut) { outFileName = "CUDA_dithering_.txt"; matrixToFile(outFileName, hostResult, height, width); } cudaFree(orderedDitheringResult); //ROTATE90 float* result90; cudaMalloc((void**)&result90, image_size); cudaEventRecord(start, 0); rotate90<<<deviceBlocks, deviceThreads>>>(result90, deviceImage, height_d, width_d); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); cudaMemcpy(hostResult, result90, image_size, cudaMemcpyDeviceToHost); std::cout << "Rotation 90 time: " << time/1000 << "sec" << std::endl; if(writeOut) { outFileName = "CUDA_rotate_90_.txt"; matrixToFile(outFileName, hostResult, width, height); } cudaFree(result90); //ROTATE180 float* result180; cudaMalloc((void**)&result180, image_size); cudaEventRecord(start, 0); rotate180<<<deviceBlocks, deviceThreads>>>(result180, deviceImage, height_d, width_d); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); cudaMemcpy(hostResult, result180, image_size, cudaMemcpyDeviceToHost); std::cout << "Rotation 180 time: " << time/1000 << "sec" << std::endl; if(writeOut) { outFileName = "CUDA_rotate_180_.txt"; matrixToFile(outFileName, hostResult, height, width); } cudaFree(result180); cudaFree(deviceImage); //MEDFILTER unsigned char* outImage; unsigned char* charResult = new unsigned char[height*width]; cudaMalloc(&outImage, charImage_size); cudaMalloc(&char_deviceImage, charImage_size); cudaMemcpy(char_deviceImage, char_hostFlattened, charImage_size, cudaMemcpyHostToDevice); cudaEventRecord(start); medianFilter<<<deviceBlocks, deviceThreads>>>(char_deviceImage, outImage, height_d, width_d); cudaEventRecord(stop); cudaEventSynchronize(stop); cudaGetLastError(); float elapsed = 0; cudaEventElapsedTime(&elapsed, start, stop); std::cout << "Median Filter Time: " << elapsed / 1000 << std::endl; cudaMemcpy(charResult, outImage, charImage_size, cudaMemcpyDeviceToHost); if(writeOut) { outFileName = "CUDA_median_filter_.txt"; matrixToFile(outFileName, charResult, height, width); } cudaFree(char_deviceImage); cudaFree(outImage); cudaFree(height_d); cudaFree(width_d); return 0; }
21,591
#include <stdio.h> struct AoS { float x; float y; }; struct SoA { float* x; float* y; }; void misalignedReadHost(float* arrayA, float* arrayB, float* arrayC, int count, int offset) { for (int offsetIdx = offset, idx = 0; offsetIdx < count; offsetIdx++, idx++) { arrayC[idx] = arrayA[offsetIdx] + arrayB[offsetIdx]; } } void misalignedWriteHost(float* arrayA, float* arrayB, float* arrayC, int count, int offset) { for (int offsetIdx = offset, idx = 0; offsetIdx < count; offsetIdx++, idx++) { arrayC[offsetIdx] = arrayA[idx] + arrayB[idx]; } } __global__ void arrayOfStructuresTest(AoS* arrayA, AoS* arrayB, int count) { int idx = (blockIdx.x * blockDim.x) + threadIdx.x; if (idx < count) { arrayB[idx].x = arrayA[idx].x * 2; arrayB[idx].y = arrayA[idx].y * 2; } } __global__ void structureOfArraysTest(SoA* structureA, SoA* structureB, int count) { int idx = (blockIdx.x * blockDim.x) + threadIdx.x; if (idx < count) { structureB->x[idx] = structureA->x[idx] * 2; structureB->y[idx] = structureA->y[idx] * 2; } } __global__ void misalignedReadDevice(float* arrayA, float* arrayB, float* arrayC, int count, int offset) { int idx = (blockIdx.x * blockDim.x) + threadIdx.x; int offsetIdx = idx + offset; if (offsetIdx < count) { arrayC[idx] = arrayA[offsetIdx] + arrayB[offsetIdx]; } } __global__ void misalignedWriteDevice(float* arrayA, float* arrayB, float* arrayC, int count, int offset) { int idx = (blockIdx.x * blockDim.x) + threadIdx.x; int offsetIdx = idx + offset; if (offsetIdx < count) { arrayC[offsetIdx] = arrayA[idx] + arrayB[idx]; } } int main(void) { printf("\n"); int count = 1 << 16; dim3 block = (512); dim3 grid = ((count + block.x - 1) / block.x); int offset = 11; float* h_a = (float*)malloc(count*sizeof(float)); float* h_b = (float*)malloc(count*sizeof(float)); for (int x = 0; x < count; x++) { h_a[x] = x * 2; h_b[x] = (x * 2) + 1; } float* h_result = (float*)malloc(count*sizeof(float)); float* d_result = (float*)malloc(count*sizeof(float)); misalignedReadHost(h_a, h_b, h_result, count, offset); float *d_a, *d_b, *d_c; cudaMalloc((float**)&d_a, count*sizeof(float)); cudaMemcpy(d_a, h_a, count*sizeof(float), cudaMemcpyHostToDevice); cudaMalloc((float**)&d_b, count*sizeof(float)); cudaMemcpy(d_b, h_b, count*sizeof(float), cudaMemcpyHostToDevice); cudaMalloc((float**)&d_c, count*sizeof(float)); misalignedReadDevice<<<grid,block>>>(d_a, d_b, d_c, count, offset); cudaDeviceSynchronize(); cudaMemcpy(d_result, d_c, count*sizeof(float), cudaMemcpyDeviceToHost); bool indexFailed = false; for (int x = 0; x < count; x++) { if (h_result[x] != d_result[x]) { printf("error on index %d: %f, %f\n", x, h_result[x], d_result[x]); indexFailed = true; } } if (!indexFailed) { printf("all indices passed successfully\n"); } return 0; }
21,592
// // Created by zhaoxuanzhu on 3/21/21. // #include "constraints.cuh"
21,593
#include <stdint.h> #include<cufft.h> #include<cuda.h> #define IDX(i,j,ld) (((i)*(ld))+(j)) __global__ void zeroPadKernel(float *dx, int m, int n, float *dy, int p, int q, int s, int t){ int i = blockIdx.y*blockDim.y + threadIdx.y; int j = blockIdx.x*blockDim.x + threadIdx.x; if ( i<m && j<n ){ if( (s-1)<i && i<(p+s) && (t-1)<j && j<(q+t) ){ dx[IDX( i, j, n )] = dy[IDX( i-s, j-t, q )]; } else{ dx[IDX( i, j, n )] = 0.0f; } } } __global__ void zeroPadComplexKernel(cufftComplex *dx, int m, int n, float *dy, int p, int q, int s, int t){ int i = blockIdx.y*blockDim.y + threadIdx.y; int j = blockIdx.x*blockDim.x + threadIdx.x; if ( i<m && j<n ){ if( (s-1)<i && i<(p+s) && (t-1)<j && j<(q+t) ){ dx[IDX( i, j, n )].x = dy[IDX( i-s, j-t, q )]; dx[IDX( i, j, n )].y = 0.0f; } else{ dx[IDX( i, j, n )].x = 0.0f; dx[IDX( i, j, n )].y = 0.0f; } } } __global__ void crop_Kernel(float *dx, int m, int n, float *dy, int p, int q, int s, int t){ int i = blockIdx.y*blockDim.y + threadIdx.y; int j = blockIdx.x*blockDim.x + threadIdx.x; if ( (s-1)<i && i<m+s ){ if( (t-1)<j && j<n+t ){ dx[IDX( i-s, j-t, n )] = dy[IDX( i, j, q )]; } } } __global__ void crop_ComplexKernel(float *dx, int m, int n, cufftComplex *dy, int p, int q, int s, int t){ int i = blockIdx.y*blockDim.y + threadIdx.y; int j = blockIdx.x*blockDim.x + threadIdx.x; if ( (s-1)<i && i<m+s ){ if( (t-1)<j && j<n+t ){ dx[IDX( i-s, j-t, n )] = dy[IDX( i, j, q )].x; } } }
21,594
#define STRIDE 4 __global__ void kernel4(int m, int n, int k, double *d_A, double *d_B, double *d_C){ int j = (blockIdx.y * blockDim.y + threadIdx.y) * STRIDE; int i = blockIdx.x * blockDim.x + threadIdx.x; double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; if (i < m && j < n){ for(int s = 0; s < k; s++){ sum1 += d_A[i*k + s] * d_B[s*n + j]; if (j+1 < n) sum2 += d_A[i*k + s] * d_B[s*n + j+1]; if (j+2 < n) sum3 += d_A[i*k + s] * d_B[s*n + j+2]; if (j+3 < n) sum4 += d_A[i*k + s] * d_B[s*n + j+3]; } d_C[i*n + j] = sum1; if (j+1 < n) d_C[i*n + j+1] = sum2; if (j+2 < n) d_C[i*n + j+2] = sum3; if (j+3 < n) d_C[i*n + j+3] = sum4; } } extern "C" { void matmult_gpu4(int m, int n, int k, double *A, double *B, double *C) { double *d_A, *d_B, *d_C; //variable on device int size_matrix_A = m * k * sizeof(double); cudaMalloc((void**)&d_A, size_matrix_A); // allocate memory on GPU int size_matrix_B = k * n * sizeof(double); cudaMalloc((void**)&d_B, size_matrix_B); int size_matrix_C = m * n * sizeof(double); cudaMalloc((void**)&d_C, size_matrix_C); //copy A and B to GPU cudaMemcpy(d_A, A, size_matrix_A, cudaMemcpyHostToDevice); cudaMemcpy(d_B, B, size_matrix_B, cudaMemcpyHostToDevice); dim3 dimBlock(16,16,1); dim3 dimGrid((m -1)/dimBlock.x+1,((n-1)/STRIDE-1)/dimBlock.y+1) ; kernel4<<<dimGrid,dimBlock>>>(m, n, k, d_A, d_B, d_C); cudaDeviceSynchronize(); //transfer C back to CPU cudaMemcpy(C, d_C, size_matrix_C, cudaMemcpyDeviceToHost); cudaFree(d_A); cudaFree(d_B); cudaFree(d_C); } }
21,595
#include<cuda.h> #include<stdio.h> #include<time.h> #include<fstream> __global__ void matrixMulKernel(int *d_M, int *d_N, int *d_P, int width){ int row = blockIdx.y*blockDim.y+threadIdx.y; int col = blockIdx.x*blockDim.x+threadIdx.x; int Pvalue; if((row < width)&&(col < width)){ Pvalue = 0; for (int k = 0; k < width ; ++k){ Pvalue += d_M[row*width+k] * d_N[k*width+col]; } d_P[row*width+col] = Pvalue; } } int matrixMulHost(int *h_M, int *h_N, int *h_P, int width){ int Pvalue; for(int row = 0; row < width ; ++row){ for(int col = 0; col < width ; ++col){ Pvalue = 0; for(int k = 0; k < width ; ++k){ Pvalue += h_M[row*width+k] * h_N[k*width+col]; } h_P[row*width+col] = Pvalue; } } return 0; } int initValues(int *data, int width){ for(int i = 0; i < width*width; i++) data[i] = 2; return 0; } int printData(int *data, int width){ for(int i = 0; i < width; ++i){ for(int j = 0; j < width; ++j){ printf("%d ", data[(i*width)+j]); } printf("\n"); } return 0; } int testValues(int *A, int *B, int width){ for(int i = 0; i < width; ++i){ for(int j = 0; j < width; ++j){ if(A[(i*width)+j]!=B[(i*width)+j]){ printf("Mal Cálculo...\n"); return 0; } } } printf("Buen Cálculo ...\n"); return 0; } int main(){ int *h_M, *h_N, *h_P,*h_P_d; int *d_M, *d_N,*d_P; int width = 2048; cudaError_t error = cudaSuccess; int size = width * width * sizeof(int); clock_t start, end, startGPU, endGPU; double cpu_time_used, gpu_time_used; h_M = (int*)malloc(size); h_N = (int*)malloc(size); h_P = (int*)malloc(size); h_P_d = (int*)malloc(size); if(h_P_d == NULL) return 0; initValues(h_M, width); initValues(h_N, width); /////////Algoritmo Secuencial//////////////////////////////////////////// start = clock(); matrixMulHost(h_M, h_N, h_P, width); end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("Tiempo algoritmo secuencial: %.10f\n", cpu_time_used); /////////Algoritmo Secuencial///////////////////////////////////////////// error = cudaMalloc((void**)&d_M,size); if(error != cudaSuccess){ printf("Error reservando memoria para d_M"); exit(0); } error = cudaMalloc((void**)&d_N,size); if(error != cudaSuccess){ printf("Error reservando memoria para d_N"); exit(0); } error = cudaMalloc((void**)&d_P,size); if(error != cudaSuccess){ printf("Error reservando memoria para d_P"); exit(0); } //////////////////////Algoritmo Paralelo/////////////////////////// startGPU = clock(); error = cudaMemcpy(d_M, h_M, size, cudaMemcpyHostToDevice); if(error != cudaSuccess){ printf("Error copiando datos a d_M"); exit(0); } error = cudaMemcpy(d_N, h_N, size, cudaMemcpyHostToDevice); if(error != cudaSuccess){ printf("Error copiando datos a d_N"); exit(0); } int blockSize = 32; dim3 dimBlock(blockSize,blockSize,1); dim3 dimGrid(ceil(width/float(blockSize)),ceil(width/float(blockSize)),1); matrixMulKernel<<<dimGrid,dimBlock>>>(d_M,d_N,d_P,width); cudaDeviceSynchronize(); cudaMemcpy(h_P_d,d_P,size,cudaMemcpyDeviceToHost); endGPU = clock(); gpu_time_used = ((double) (endGPU - startGPU)) / CLOCKS_PER_SEC; printf("Tiempo algoritmo paralelo: %.10f\n", gpu_time_used); printf("La aceleración obtenida es de %.10fX\n",cpu_time_used/gpu_time_used); ///////////////////////Algoritmo Paralelo//////////////////////////// testValues(h_P_d,h_P,width); free(h_M); free(h_N); free(h_P); cudaFree(d_M); cudaFree(d_N); cudaFree(d_P); return 0; }
21,596
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <iostream> #include <string> #include <fstream> #include <sstream> #include <vector> #include <iterator> #include <unordered_map> #include <bitset> #include <thread> #include <math.h> #define BITS 64 #define MAX_LEN_WORD 20 using namespace std; string readFile(string file) { ifstream MyFile; MyFile.open(file); stringstream strStream; strStream << MyFile.rdbuf(); return strStream.str(); } vector<string> strToVector(string data, char stop_w = '\n') { vector<string> out; size_t last_init = 0; for (size_t i = 0; i < data.length(); i++) { if (data[i] == stop_w) { string temp = data.substr(last_init, i - last_init); out.push_back(temp); last_init = i + 1; } } return out; } void printVector(vector<string> data) { for (size_t i = 0; i < data.size(); i++) { printf("%s\n", data[i].c_str()); } } void printVectorInt(vector<int> data) { for (size_t i = 0; i < data.size(); i++) { printf("%i ", data[i]); } printf("\n"); } void printMap(unordered_map <string, int> data) { for (unordered_map<string, int >::const_iterator it = data.begin(); it != data.end(); ++it) { cout << it->first << " " << it->second << "\n"; } } string* text_generator(vector<string> my_dict, int len_words) { string* text = new string[len_words]; for (size_t i = 0; i < len_words; i++) { text[i] = my_dict[rand() % my_dict.size()]; } return text; } unordered_map <string, int> get_frec(string* words, int len_words) { unordered_map <string, int> out; for (size_t i = 0; i < len_words; i++) { if (out.count(words[i]) > 0) { // cuantas veces parece la palabra en el texto out[words[i]] += 1; } else { out[words[i]] = 1; } } return out; } string sum_matrix(vector<long*> in_matrix) { string out; for (size_t j = 0; j < BITS; j++) { long int temp = 0; for (size_t i = 0; i < in_matrix.size(); i++) { temp += in_matrix[i][j]; } if (temp > 0) { out += '1'; } else { out += '0'; } } return out; } size_t count_words(unordered_map <string, int>* in_words, int n_text) { size_t count_l = 0; for (size_t i = 0; i < n_text; i++) { count_l += in_words[i].size(); } return count_l; } char* strToChar(string data) { char* out = new char[MAX_LEN_WORD]; for (size_t i = 0; i < MAX_LEN_WORD; i++) { if (i < data.size()) { out[i] = data[i]; } else { out[i] = 0; } } return out; } void compress_sim_data_cuda(unordered_map <string, int>* in_words, char * & s_out, int * & f_out , int n_text) { size_t numerate = 0; for (size_t i = 0; i < n_text; i++) { for (unordered_map<string, int >::const_iterator it = in_words[i].begin(); it != in_words[i].end(); ++it) { char* t_str = strToChar(it->first); for (size_t i = 0; i < MAX_LEN_WORD; i++) { s_out[numerate * MAX_LEN_WORD + i] = t_str[i]; } f_out[numerate] = it->second; numerate++; } } } string* extract_sim_data_cuda(long * v_words, unordered_map <string, int>* in_words, int n_text) { string* out = new string[n_text]; size_t numerate = 0; for (size_t i = 0; i < n_text; i++) { vector<long*> temp; for (unordered_map<string, int >::const_iterator it = in_words[i].begin(); it != in_words[i].end(); ++it) { long * t_bits = new long[BITS]; for (size_t b = 0; b < BITS; b++) { t_bits[b] = v_words[numerate + b]; } temp.push_back(t_bits); numerate+= BITS; } out[i] = sum_matrix(temp); } return out; } void sim_hash_lineal(char* s_in, int* f_in, long* & out, int len) { for (size_t t = 0; t < len; t++) { unsigned long long int hash = 5381; for (size_t i = 0; i < MAX_LEN_WORD; i++) { if (s_in[t * MAX_LEN_WORD + i] != 0) { hash = ((hash << 5) + hash) + (int)s_in[t * MAX_LEN_WORD + i]; } } bool* bits = new bool[BITS]; for (size_t i = 0; i < BITS; i++) { bits[i] = hash % 2; hash = hash / 2; } for (size_t i = 0; i < BITS; i++) { size_t p = (t * BITS) + i; out[p] = (int)bits[i]; if (out[p] == 1) { out[p] += f_in[t]; } else { out[p] -= f_in[t]; } } } } __global__ void cuda_sim_hash(char * s_in, int * f_in, long * out, int len) { int t = (blockIdx.x * blockDim.x) + (threadIdx.x); if (t >= 0 && t < len) { unsigned long long int hash = 5381; for (size_t i = 0; i < MAX_LEN_WORD; i++) { if (s_in[t * MAX_LEN_WORD + i] != 0) { hash = ((hash << 5) + hash) + (int)s_in[t * MAX_LEN_WORD + i]; } } bool* bits = new bool[BITS]; for (size_t i = 0; i < BITS; i++) { bits[i] = hash % 2; hash = hash / 2; } for (size_t i = 0; i < BITS; i++) { size_t p = (t * BITS) + i; out[p] = (int)bits[i]; if (out[p] == 1) { out[p] += f_in[t]; } else { out[p] -= f_in[t]; } } delete bits; } } bool compare_str(string * a, string * b, int len) { for (size_t i = 0; i < len; i++) { if (a[i] != b[i]) { return false; } } return true; } int main() { clock_t begin, end; double elapsed_secs; long long int w_s; //palabras por segundo srand(time(NULL)); string words = readFile("words.txt"); //leer palabras vector<string> l_words = strToVector(words); //libreria de palabras int long_text = 4000; //longitud de palabras por texto int n_text = 2560; //cantidad de textos (documentos) printf("Num. textos: %i, Long Text: %i \n", n_text, long_text); unordered_map <string, int>* words_frec = new unordered_map <string, int>[n_text]; // diccionario de frecuencias por palabra de cada documento for (size_t i = 0; i < n_text; i++) { words_frec[i] = get_frec(text_generator(l_words, long_text), long_text); } size_t amount_words = count_words(words_frec, n_text); printf("Total Words: %i words \n", amount_words); char * s_in = new char[amount_words * MAX_LEN_WORD]; int * f_in = new int[amount_words]; long * out = new long[amount_words * BITS]; compress_sim_data_cuda(words_frec, s_in, f_in , n_text); char* cu_s_in = 0; int* cu_f_in = 0; long* cu_out = 0; cudaMalloc((void**)&cu_s_in, amount_words * sizeof(char) * MAX_LEN_WORD); cudaMalloc((void**)&cu_f_in, amount_words * sizeof(int)); cudaMalloc((void**)&cu_out, amount_words * sizeof(long) * BITS); begin = clock(); cudaMemcpy(cu_s_in, s_in, amount_words * sizeof(char) * MAX_LEN_WORD, cudaMemcpyHostToDevice); cudaMemcpy(cu_f_in, f_in, amount_words * sizeof(int), cudaMemcpyHostToDevice); int thr = 1024; int dim_grid = (amount_words/thr)+1; cuda_sim_hash <<< dim_grid, thr >>> (cu_s_in, cu_f_in, cu_out, amount_words); cudaMemcpy(out, cu_out, amount_words * sizeof(long) * BITS, cudaMemcpyDeviceToHost); end = clock(); elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; printf("Grilla: %d, Bloque: %d \n", dim_grid, thr); printf("Tiempo Cuda: %f ms \n", elapsed_secs); w_s = amount_words / elapsed_secs; printf("palabras por Seg: %d words \n", w_s); string* r_out = extract_sim_data_cuda(out, words_frec, n_text); long* out_l = new long[amount_words * BITS]; begin = clock(); sim_hash_lineal(s_in, f_in, out_l, amount_words); end = clock(); elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; printf("Tiempo Lineal: %f ms \n", elapsed_secs); w_s = amount_words / elapsed_secs; printf("palabras por Seg: %d words \n", w_s); string* r_out_l = extract_sim_data_cuda(out_l, words_frec, n_text); /* for (size_t i = 0; i < n_text; i++) { cout << r_out_l[i] << endl; }*/ if (compare_str(r_out, r_out_l, n_text)) { cout << "Ok" << endl; } else { cout << "Error" << endl; } }
21,597
#include "includes.h" __global__ void gaxpymm(double *y, double *a, double *b, int m, int n, int p){ int bid = blockIdx.x; int tid = threadIdx.x; extern __shared__ double dots_s[]; if(bid<m) if(tid<n){ for(int c=0;c<p;c++) dots_s[bid*n*p+tid*p+c] = a[bid*n+tid] * *(b+(tid*p+c)); __syncthreads(); if(tid == 0){ for(int c=0;c<p;c++) for(int i=1;i<n;i++){ dots_s[bid*n*p+c] +=dots_s[bid*n*p+i*p+c]; // printf("y=%d, dots_s=%d, bid=%d, tid=%d, i=%d, n=%d\n",dots_s[bid*n], dots_s[bid*n+i],bid,tid,i,n); } for(int c=0;c<p;c++) *(y+(bid*p+c))=dots_s[bid*n*p+c]; // printf("y[%d]=%d, bid=%d, tid=%d\n",bid,y[bid],bid,tid); } } }
21,598
#include<cuda.h> #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: %lu\n", devProp.totalGlobalMem); printf("Total shared memory per block: %lu\n", devProp.sharedMemPerBlock); printf("Total registers per block: %d\n", devProp.regsPerBlock); printf("Warp size: %d\n", devProp.warpSize); printf("Maximum memory pitch: %lu\n", devProp.memPitch); printf("Maximum threads per block: %d\n", devProp.maxThreadsPerBlock); printf("Maximum block dimensions: %dx%dx%d\n", devProp.maxThreadsDim[0], devProp.maxThreadsDim[1], devProp.maxThreadsDim[2]); printf("Maximum grid dimensions: %dx%dx%d\n", devProp.maxGridSize[0], devProp.maxGridSize[1], devProp.maxGridSize[2]); printf("Clock rate: %d\n", devProp.clockRate); printf("Total constant memory: %lu\n", devProp.totalConstMem); printf("Texture alignment: %lu\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); } return 0; }
21,599
#include <iostream> #include <cuda_runtime.h> #include <chrono> #include <cstdio> #include <cuda_fp16.h> // CUDA_VERSION >= 7050 #define CUDA_ARCH_FP16_SUPPORTED(CUDA_ARCH) (CUDA_ARCH >= 600) template <typename T> __device__ T math_exp(T a); template <> __device__ half math_exp<half>(half a) { #if CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) return hexp(a); #endif } template <> __device__ float math_exp<float>(float a) { return expf(a); } template <typename T> __global__ void swish_kernel(int num, const T *input, T *output, T beta) { int index = blockIdx.x * blockDim.x + threadIdx.x; if (index < num) { #if __CUDA_ARCH__ >= 350 output[index] = __ldg(input + index) / (static_cast<T>(1.0) + math_exp<T>(-beta * __ldg(input + index))); #else output[index] = input[index] / (static_cast<T>(1.0) + math_exp<T>(-beta * input[index])); #endif } } template <> __global__ void swish_kernel<half>(int num, const half *input, half *output, half beta) { int index = blockIdx.x * blockDim.x + threadIdx.x; // half2* input2 = (half2*)input; // half2* output2 = (half2*)output; if (index < num) { // #if CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) output[index] = __ldg(input + index) / (static_cast<half>(1.0) + math_exp<half>(-beta * __ldg(input + index))); // output[index] = __hdiv(__ldg(input + index), (__hadd(static_cast<half>(1.0), hexp(__hneg(beta * (__ldg(input + index))))))); // output2[index] = __h2div(__ldg(input2 + index), (__hadd2(__halves2half2(1.0, 1.0), hexp2(__hneg2(__hmul2(__halves2half2(beta,beta), (__ldg(input2 + index)))))))); // output2[index] = __h2div(input2[index], (__hadd2(__halves2half2(1.0, 1.0), hexp2(__hneg2(__hmul2(__halves2half2(beta, beta), (input2[index]))))))); // half2 val = __halves2half2(1.0, 1.0); printf("hehe"); // #endif } } int main() { using dtype = half; int num = 1 * 64 * 112 * 112; int size = num * sizeof(dtype); half beta = 1.0; int threads = 1024; int blocks = (num + threads - 1) / threads; dtype* h_input = new dtype[num]; dtype* h_output = new dtype[num]; h_input[0] = 5; int device_id = 2; cudaSetDevice(device_id); cudaStream_t stream; cudaStreamCreate(&stream); cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop); dtype *input, *output; cudaMalloc(&input, size); cudaMalloc(&output, size); // warmup for (int i = 0; i < 5; i ++) { cudaMemcpyAsync(input, h_input, size, cudaMemcpyHostToDevice, stream); swish_kernel<<<blocks, threads, 0, stream>>>(num, input, output, beta); cudaMemcpyAsync(h_output, output, size, cudaMemcpyDeviceToHost, stream); } cudaStreamSynchronize(stream); // run int repeats = 1; float cost = 0.0f; for (int i = 0; i < repeats; i ++) { cudaMemcpyAsync(input, h_input, size, cudaMemcpyHostToDevice, stream); cudaEventRecord(start, stream); swish_kernel<<<blocks, threads, 0, stream>>>(num, input, output, beta); cudaEventRecord(stop, stream); cudaEventSynchronize(stop); float elapse_time = 0.0f; cudaEventElapsedTime(&elapse_time, start, stop); cost += elapse_time; // std::cout << elapse_time << std::endl; cudaMemcpyAsync(h_output, output, size, cudaMemcpyDeviceToHost, stream); } cudaStreamSynchronize(stream); for(int i = 0; i < 6; i ++) { std::cout << __half2float(h_output[i]) << std::endl; } float avg_cost = cost / (1.0f * repeats); std::cout << "avg cost: " << avg_cost << std::endl; cudaEventDestroy(start); cudaEventDestroy(stop); cudaFree(input); cudaFree(output); return 0; }
21,600
#include<bits/stdc++.h> using namespace std; __global__ void MatrixMulKernel(int *M,int *N,int *P,int Tile_Width,int Width){ __shared__ double ds_M[32][32]; __shared__ double ds_N[32][32]; int bx=blockIdx.x; int by=blockIdx.y; int tx=threadIdx.x; int ty=threadIdx.y; int Row=by*blockDim.y+ty; int Col=bx*blockDim.x+tx; int Pvalue=0; for(int p=0;p<Width/Tile_Width;p++){ ds_M[ty][tx]=M[Row*Width+p*Tile_Width+tx]; ds_N[ty][tx]=N[(p*Tile_Width+ty)*Width+Col]; __syncthreads(); for(int i=0;i<Tile_Width;i++) Pvalue+=ds_M[ty][i]*ds_N[i][tx];//partial dot product __syncthreads(); } P[Row*Width+Col]=Pvalue;//final answer } __global__ void MatrixMulKernel2(int *M,int *N,int *P,int Width){ int Row=(blockIdx.y*blockDim.y)+threadIdx.y;//row number int Col=(blockIdx.x*blockDim.x)+threadIdx.x;//column number if((Row<Width)&&(Col<Width)){ float Pvalue=0; for(int k=0;k<Width;k++){ Pvalue+=M[Row*Width+k]*N[k*Width+Col]; } P[Row*Width+Col]=Pvalue;//final answer } } int main(int argc, char** argv){ ifstream infile(argv[1]); int *arr1_h,*arr1_d,*arr2_d,*arr3_d,*degree_h; int a,b,bd,gd,tile_width,f=0; long long int vertices,edges,size,original_vertices; bd=32; tile_width=bd; while(infile >> a >> b){ if(f==0){ vertices=a; original_vertices=a; edges=b; if(vertices<tile_width){ vertices=tile_width; } else{ long long int temp=tile_width; while(vertices>temp){ temp=temp*2; } vertices=temp; } size=(vertices*vertices)*sizeof(int); arr1_h=new int[vertices*vertices]; degree_h=new int[vertices]; for (long long int i = 0; i < vertices; ++i) { degree_h[i]=0; } for(long long int i=0;i<vertices;i++){ for(long long int j=0;j<vertices;j++){ arr1_h[i*vertices+j]=0; } } f=1; } else{ arr1_h[a*vertices+b]=1; arr1_h[b*vertices+a]=1; degree_h[a]++; degree_h[b]++; } } cudaMalloc(&arr1_d,size); cudaMalloc(&arr2_d,size); cudaMalloc(&arr3_d,size); cudaMemcpy(arr1_d,arr1_h,size,cudaMemcpyHostToDevice); gd=vertices/bd; dim3 grid(gd,gd); dim3 block(bd,bd); MatrixMulKernel<<< grid,block >>>(arr1_d,arr1_d,arr2_d,tile_width,vertices); MatrixMulKernel<<< grid,block >>>(arr2_d,arr1_d,arr3_d,tile_width,vertices); cudaMemcpy(arr1_h,arr3_d,size,cudaMemcpyDeviceToHost); float cc=0; for(int i=0;i<vertices;i++){ if(degree_h[i]>=2){ cc=cc+((float(arr1_h[i*vertices+i]/2))/((degree_h[i]*(degree_h[i]-1))/2)); } } cc=cc/original_vertices; cout<<cc<<endl; return 0; }