/* Simple MIS code: This code finds the maximal independent set of an input. It uses an int array instead of a byte array to store the combined statuses and priorities. Copyright 2022 Texas State University Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Authors: Martin Burtscher and Alex Fallin URL: The latest version of this code is available at https://cs.txstate.edu/~burtscher/research/bit-flips/. */ #include #include #include #include #include #include #include #include "ECLgraph.h" #include "gpu_energy_monitor.h" static const int in = 0; static const int out = INT_MAX; static const int bfr_out = (INT_MAX / 2) + 1; static const int Device = 0; static const int ThreadsPerBlock = 512; // https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key static __device__ unsigned int hash(unsigned int val) { val = ((val >> 16) ^ val) * 0x45d9f3b; val = ((val >> 16) ^ val) * 0x45d9f3b; return (val >> 16) ^ val; } static __global__ void init(const int size, int* const status) { // initialize arrays const int v = threadIdx.x + blockIdx.x * blockDim.x; if (v < size) { status[v] = hash(v) % (INT_MAX - 1) + 1; } } static __global__ void bfr_init(const int size, int* const status) { // initialize arrays const int v = threadIdx.x + blockIdx.x * blockDim.x; if (v < size) { status[v] = hash(v) % (INT_MAX / 2) + 1; } } static __global__ void mis(volatile const ECLgraph g, int* const status, bool* goagain) { const int v = threadIdx.x + blockIdx.x * blockDim.x; if (v < g.nodes) { const int dv = status[v]; if (dv != out && dv != in) { const int beg = g.nindex[v]; const int end = g.nindex[v + 1]; int dmin = INT_MAX; int curr; for (int i = beg; i < end; i++) { curr = g.nlist[i]; dmin = min(status[curr], dmin); } if (dmin == in) { status[v] = out; } else if (dmin > dv) { status[v] = in; } else { *goagain = true; } } } } static __global__ void bfr_mis(volatile const ECLgraph g, int* const status, bool* goagain) { const int v = threadIdx.x + blockIdx.x * blockDim.x; if (v < g.nodes) { const int dv = status[v]; if (dv != bfr_out && dv != in) { const int beg = g.nindex[v]; const int end = g.nindex[v + 1]; int dmin = INT_MAX; int curr; for (int i = beg; i < end; i++) { curr = g.nlist[i]; dmin = min(status[curr], dmin); } if (dmin == in) { status[v] = bfr_out; } else if (dmin > dv) { status[v] = in; } else { *goagain = true; } } } } static void CheckCuda() { cudaError_t e; cudaDeviceSynchronize(); if (cudaSuccess != (e = cudaGetLastError())) { fprintf(stderr, "CUDA error %d: %s\n", e, cudaGetErrorString(e)); exit(-1); } } static double median(double array[], const int n) { double median = 0; std::sort(array, array + n); if (n % 2 == 0) median = (array[(n - 1) / 2] + array[n / 2]) / 2.0; else median = array[n / 2]; return median; } int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "USAGE: %s input_file\n", argv[0]); exit(-1); } // read input ECLgraph g = readECLgraph(argv[1]); printf("%s\n", argv[1]); // set device cudaSetDevice(Device); cudaDeviceProp deviceProp; cudaGetDeviceProperties(&deviceProp, Device); if ((deviceProp.major == 9999) && (deviceProp.minor == 9999)) { fprintf(stderr, "ERROR: there is no CUDA capable device\n\n"); exit(-1); } // allocate arrays int* const status = new int[g.nodes]; const int blocks = (g.nodes + ThreadsPerBlock - 1) / ThreadsPerBlock; const int runs = 1; double runtimes[runs]; bool goagain; // GPU memory ECLgraph d_g = g; int* d_status; bool* d_goagain; if (cudaSuccess != cudaMalloc((void**) &d_goagain, sizeof(bool))) { fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1); } if (cudaSuccess != cudaMalloc((void**) &d_status, sizeof(int) * g.nodes)) { fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1); } cudaMalloc((void**) &d_g.nindex, sizeof(int) * (g.nodes + 1)); cudaMalloc((void**) &d_g.nlist, sizeof(int) * g.edges); cudaMemcpy(d_g.nindex, g.nindex, sizeof(int) * (g.nodes + 1), cudaMemcpyHostToDevice); cudaMemcpy(d_g.nlist, g.nlist, sizeof(int) * g.edges, cudaMemcpyHostToDevice); if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) {fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1);} // Running all kernels to get it all CUDA initialized bfr_init<<>>(g.nodes, d_status); init<<>>(g.nodes, d_status); bfr_mis<<>>(d_g, d_status, d_goagain); mis<<>>(d_g, d_status, d_goagain); cudaDeviceSynchronize(); // Finding number of reps timeval beg, end; int reps = 0; gettimeofday(&beg, NULL); do { init<<>>(g.nodes, d_status); do { goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) { fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1); } mis<<>>(d_g, d_status, d_goagain); if (cudaSuccess != cudaMemcpy(&goagain, d_goagain, sizeof(bool), cudaMemcpyDeviceToHost)) { fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1); } } while (goagain); cudaDeviceSynchronize(); gettimeofday(&end, NULL); reps++; } while ((end.tv_sec - beg.tv_sec + (end.tv_usec - beg.tv_usec) / 1000000.0) < 10.0); CheckCuda(); // Run experiments interleaved 10 times const int tot_runs = 10; // GPU Energy Setup GPUMonitor gpu; gpu.initMonitor(Device); // Trackers unsigned long long regular_consumption[tot_runs]; double regular_runtime[tot_runs]; unsigned long long bfr_consumption[tot_runs]; double bfr_runtime[tot_runs]; // Perform experiments for (int run = 0; run < tot_runs; run++) { // Regular gpu.startEnergy(); gettimeofday(&beg, NULL); for (int rep = 0; rep < reps; rep++) { // execute timed code init<<>>(g.nodes, d_status); do { goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) { fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1); } mis<<>>(d_g, d_status, d_goagain); if (cudaSuccess != cudaMemcpy(&goagain, d_goagain, sizeof(bool), cudaMemcpyDeviceToHost)) { fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1); } } while (goagain); } gettimeofday(&end, NULL); regular_consumption[run] = gpu.getEnergyConsumption(); regular_runtime[run] = end.tv_sec - beg.tv_sec + (end.tv_usec - beg.tv_usec) / 1000000.0; // BFR gpu.startEnergy(); gettimeofday(&beg, NULL); for (int rep = 0; rep < reps; rep++) { // execute timed code bfr_init<<>>(g.nodes, d_status); do { goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) { fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1); } bfr_mis<<>>(d_g, d_status, d_goagain); if (cudaSuccess != cudaMemcpy(&goagain, d_goagain, sizeof(bool), cudaMemcpyDeviceToHost)) { fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1); } } while (goagain); } gettimeofday(&end, NULL); bfr_consumption[run] = gpu.getEnergyConsumption(); bfr_runtime[run] = end.tv_sec - beg.tv_sec + (end.tv_usec - beg.tv_usec) / 1000000.0; } // Print results // reps, reg_con, reg_time, bfr_con, bfr_time for (int run = 0; run < tot_runs; run++) { printf("%d, %lld, %.4f, %lld, %.4f\n", reps, regular_consumption[run], regular_runtime[run], bfr_consumption[run], bfr_runtime[run]); } // get result from GPU if (cudaSuccess != cudaMemcpy(status, d_status, sizeof(int) * g.nodes, cudaMemcpyDeviceToHost)) { fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1); } // determine and print set size int count = 0; for (int v = 0; v < g.nodes; v++) { if (status[v] == in) { count++; } } printf("elements in set: %d (%.1f%%)\n", count, 100.0 * count / g.nodes); // clean up freeECLgraph(g); delete[] status; cudaFree(d_status); cudaFree(d_g.nindex); cudaFree(d_g.nlist); cudaFree(d_goagain); return 0; }