/* Simple BFS code: This code performs a breadth-first search on the supplied input 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 Device = 0; static const int ThreadsPerBlock = 512; static __global__ void init(const int size, int* const dist, const int src) { // initialize arrays const int v = threadIdx.x + blockIdx.x * ThreadsPerBlock; if (v < size) { dist[v] = INT_MAX - 1; if (v == src) dist[v] = 0; } } static __global__ void init_bfr(const int size, int* const dist, const int src) { // initialize arrays const int v = threadIdx.x + blockIdx.x * ThreadsPerBlock; if (v < size) { dist[v] = (INT_MAX / 2) + 1; if (v == src) dist[v] = 0; } } static __global__ void BFS(const ECLgraph g, volatile int* const dist, int* const dist_n, volatile bool* const goagain) { const int v = threadIdx.x + blockIdx.x * ThreadsPerBlock; if (v < g.nodes) { const int dv = dist[v]; const int beg = g.nindex[v]; const int end = g.nindex[v + 1]; int dmin = dv; int curr; for (int i = beg; i < end; i++) { curr = g.nlist[i]; dmin = min(dmin, dist[curr] + 1); } dist_n[v] = dmin; if (dmin < dv) { *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); } } int main(int argc, char* argv[]) { if (argc != 3) {fprintf(stderr, "USAGE: %s input_file_name source_node_number\n", argv[0]); exit(-1);} 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);} // process command line ECLgraph g = readECLgraph(argv[1]); printf("%s\n", argv[1]); const int source = atoi(argv[2]); if ((source < 0) || (source >= g.nodes)) {fprintf(stderr, "ERROR: source_node_number must be between 0 and %d\n", g.nodes); exit(-1);} const int blocks = (g.nodes + ThreadsPerBlock - 1) / ThreadsPerBlock; // allocate arrays int* const distance = new int [g.nodes]; int* d_distance; int* d_distance_new; if (cudaSuccess != cudaMalloc((void **)&d_distance, sizeof(int) * g.nodes)) {fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1);} if (cudaSuccess != cudaMalloc((void **)&d_distance_new, sizeof(int) * g.nodes)) {fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1);} // copy graph to GPU ECLgraph d_g = g; 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); // Running all kernels to get it all CUDA initialized init_bfr<<>>(g.nodes, d_distance, source); init<<>>(g.nodes, d_distance, source); bool goagain; bool* d_goagain; if (cudaSuccess != cudaMalloc((void **)&d_goagain, sizeof(bool))) {fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1);} goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) {fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1);} BFS<<>>(d_g, d_distance, d_distance_new, d_goagain); cudaDeviceSynchronize(); // Finding number of reps timeval beg, end; int reps = 0; gettimeofday(&beg, NULL); do { init<<>>(g.nodes, d_distance, source); if (cudaSuccess != cudaMalloc((void **)&d_goagain, sizeof(bool))) {fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1);} do { goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) {fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1);} BFS<<>>(d_g, d_distance, d_distance_new, d_goagain); if (cudaSuccess != cudaMemcpy(&goagain, d_goagain, sizeof(bool), cudaMemcpyDeviceToHost)) {fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1);} std::swap(d_distance, d_distance_new); } 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_distance, source); if (cudaSuccess != cudaMalloc((void **)&d_goagain, sizeof(bool))) {fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1);} do { goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) {fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1);} BFS<<>>(d_g, d_distance, d_distance_new, d_goagain); if (cudaSuccess != cudaMemcpy(&goagain, d_goagain, sizeof(bool), cudaMemcpyDeviceToHost)) {fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1);} std::swap(d_distance, d_distance_new); } while (goagain); cudaDeviceSynchronize(); } 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 init_bfr<<>>(g.nodes, d_distance, source); if (cudaSuccess != cudaMalloc((void **)&d_goagain, sizeof(bool))) {fprintf(stderr, "ERROR: could not allocate memory\n"); exit(-1);} do { goagain = false; if (cudaSuccess != cudaMemcpy(d_goagain, &goagain, sizeof(bool), cudaMemcpyHostToDevice)) {fprintf(stderr, "ERROR: copying to device failed\n"); exit(-1);} BFS<<>>(d_g, d_distance, d_distance_new, d_goagain); if (cudaSuccess != cudaMemcpy(&goagain, d_goagain, sizeof(bool), cudaMemcpyDeviceToHost)) {fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1);} std::swap(d_distance, d_distance_new); } while (goagain); cudaDeviceSynchronize(); } 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 CheckCuda(); if (cudaSuccess != cudaMemcpy(distance, d_distance_new, sizeof(int) * g.nodes, cudaMemcpyDeviceToHost)) {fprintf(stderr, "ERROR: copying from device failed\n"); exit(-1);} // clean up freeECLgraph(g); cudaFree(d_distance); cudaFree(d_g.nindex); cudaFree(d_g.nlist); return 0; }