/* Simple SSSP code: This code computes the single-source shortest path. This is the simple code that includes the use of a bit-flip reduced initialization value 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 "ECLgraph.h" #include "gpu_energy_monitor.h" static const int ThreadsPerBlock = 256; static const int runs = 10; // Number of runs to perform and report static void CPUserialDijkstra(const int src, const ECLgraph &g, int* const dist) { // initialize dist array for (int i = 0; i < g.nodes; i++) dist[i] = INT_MAX; dist[src] = 0; // set up priority queue with just source node in it std::priority_queue> pq; pq.push(std::make_pair(0, src)); while (pq.size() > 0) { // process closest vertex const int v = pq.top().second; pq.pop(); const int dv = dist[v]; // visit outgoing neighbors for (int i = g.nindex[v]; i < g.nindex[v + 1]; i++) { const int n = g.nlist[i]; const int d = dv + g.eweight[i]; // check if new lower distance found if (d < dist[n]) { dist[n] = d; pq.push(std::make_pair(-d, n)); } } } } static __global__ void init(const int src, int* const dist, const int size) { // initialize dist array const int v = threadIdx.x + blockIdx.x * blockDim.x; int d = INT_MAX; if (v == src) d = 0; if (v < size) dist[v] = d; } static __global__ void init_bfr(const int src, int* const dist, const int size) { // initialize dist array const int v = threadIdx.x + blockIdx.x * blockDim.x; int d = (INT_MAX / 2) + 1; if (v == src) d = 0; if (v < size) dist[v] = d; } static __global__ void pull(const ECLgraph g, volatile int* const dist, bool* const updated) { const int v = threadIdx.x + blockIdx.x * blockDim.x; if (v < g.nodes) { for (int i = g.nindex[v]; i < g.nindex[v + 1]; i++) { if (dist[g.nlist[i]] != INT_MAX) { int new_distance = dist[g.nlist[i]] + g.eweight[i]; if (new_distance < dist[v]) { dist[v] = new_distance; *updated = true; } } } } } static __global__ void pull_bfr(const ECLgraph g, volatile int* const dist, bool* const updated) { const int v = threadIdx.x + blockIdx.x * blockDim.x; if (v < g.nodes) { for (int i = g.nindex[v]; i < g.nindex[v + 1]; i++) { if (dist[g.nlist[i]] != ((INT_MAX / 2) + 1)) { int new_distance = dist[g.nlist[i]] + g.eweight[i]; if (new_distance < dist[v]) { dist[v] = new_distance; *updated = true; } } } } } static void GPUparallelBellmanFordPull(const int src, const ECLgraph &g, int* const dist, const int device, char* argv[]) { timeval start, end; bool* updated_gpu; int* dist_gpu; if (cudaSuccess != cudaMalloc((void**) &updated_gpu, sizeof(bool))) fprintf(stderr, "ERROR: could not allocate updated\n"); if (cudaSuccess != cudaMalloc((void**) &dist_gpu, g.nodes * sizeof(int))) fprintf(stderr, "ERROR: could not allocate dist\n"); const int blocks = (g.nodes + ThreadsPerBlock - 1) / ThreadsPerBlock; // Initialize all kernels init_bfr<<>>(src, dist_gpu, g.nodes); pull_bfr<<>>(g, dist_gpu, updated_gpu); init<<>>(src, dist_gpu, g.nodes); pull<<>>(g, dist_gpu, updated_gpu); // Determine repetitions (to 1 second of runtime) int reps = 0; gettimeofday(&start, NULL); gettimeofday(&end, NULL); while ((end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1000000.0) < 10) { init_bfr<<>>(src, dist_gpu, g.nodes); // iterate until no more changes bool updated; int iterations = 0; do { iterations++; updated = false; if (cudaSuccess != cudaMemcpy(updated_gpu, &updated, sizeof(bool), cudaMemcpyHostToDevice)) fprintf(stderr, "ERROR: copying of updated to device failed\n"); pull_bfr<<>>(g, dist_gpu, updated_gpu); if (cudaSuccess != cudaMemcpy(&updated, updated_gpu, sizeof(bool), cudaMemcpyDeviceToHost)) fprintf(stderr, "ERROR: copying of updated from device failed\n"); } while (updated); reps++; gettimeofday(&end, NULL); } // Run measured tests GPUMonitor gpu; gpu.initMonitor(device); double reg_runtimes[runs]; unsigned long long reg_cons[runs]; double bfr_runtimes[runs]; unsigned long long bfr_cons[runs]; for (int run = 0; run < runs; run++) { // Regular gpu.startEnergy(); gettimeofday(&start, NULL); for (int i = 0; i < reps; i++) { init<<>>(src, dist_gpu, g.nodes); // iterate until no more changes bool updated; int iter = 0; do { iter++; updated = false; if (cudaSuccess != cudaMemcpy(updated_gpu, &updated, sizeof(bool), cudaMemcpyHostToDevice)) fprintf(stderr, "ERROR: copying of updated to device failed\n"); pull<<>>(g, dist_gpu, updated_gpu); if (cudaSuccess != cudaMemcpy(&updated, updated_gpu, sizeof(bool), cudaMemcpyDeviceToHost)) fprintf(stderr, "ERROR: copying of updated from device failed\n"); } while (updated); } gettimeofday(&end, NULL); reg_cons[run] = gpu.getEnergyConsumption(); reg_runtimes[run] = (end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0); // Bit-flip reduced gpu.startEnergy(); gettimeofday(&start, NULL); for (int i = 0; i < reps; i++) { init_bfr<<>>(src, dist_gpu, g.nodes); // iterate until no more changes bool updated; int iter = 0; do { iter++; updated = false; if (cudaSuccess != cudaMemcpy(updated_gpu, &updated, sizeof(bool), cudaMemcpyHostToDevice)) fprintf(stderr, "ERROR: copying of updated to device failed\n"); pull_bfr<<>>(g, dist_gpu, updated_gpu); if (cudaSuccess != cudaMemcpy(&updated, updated_gpu, sizeof(bool), cudaMemcpyDeviceToHost)) fprintf(stderr, "ERROR: copying of updated from device failed\n"); } while (updated); } gettimeofday(&end, NULL); bfr_cons[run] = gpu.getEnergyConsumption(); bfr_runtimes[run] = (end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0); } // Print results // reps, reg_con, reg_time, bfr_con, bfr_time for (int run = 0; run < runs; run++) { printf("%d, %lld, %.4f, %lld, %.4f\n", reps, reg_cons[run], reg_runtimes[run], bfr_cons[run], bfr_runtimes[run]); } if (cudaSuccess != cudaMemcpy(dist, dist_gpu, g.nodes * sizeof(int), cudaMemcpyDeviceToHost)) fprintf(stderr, "ERROR: copying of dist from device failed\n"); cudaFree(updated_gpu); cudaFree(dist_gpu); } int main(int argc, char* argv[]) { printf("SSSP (%s)\n", __FILE__); if (argc != 4) { fprintf(stderr, "USAGE: %s input_file_name source_node_number device\n", argv[0]); exit(-1); } int device = atoi(argv[3]); // process command line ECLgraph g = readECLgraph(argv[1]); printf("%s\n", argv[1]); if (g.eweight == NULL) { g.eweight = new int[g.edges]; srand(0); for (int i = 0; i < g.edges; i++) { g.eweight[i] = (rand() % 1024) + 1; } } printf("input: %s\n", argv[1]); printf("nodes: %d\n", g.nodes); printf("edges: %d\n", g.edges); 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); } printf("source: %d\n", source); // GPU information 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); } const int SMs = deviceProp.multiProcessorCount; printf("GPU: %s with %d SMs (%.1f MHz core and %.1f MHz mem)\n", deviceProp.name, SMs, deviceProp.clockRate * 0.001, deviceProp.memoryClockRate * 0.001); // allocate memory int* const distance = new int[g.nodes]; ECLgraph g_gpu = g; if (cudaSuccess != cudaMalloc((void**) &g_gpu.nindex, (g.nodes + 1) * sizeof(int))) fprintf(stderr, "ERROR: could not allocate nindex\n"); if (cudaSuccess != cudaMalloc((void**) &g_gpu.nlist, g.edges * sizeof(int))) fprintf(stderr, "ERROR: could not allocate nlist\n"); if (cudaSuccess != cudaMalloc((void**) &g_gpu.eweight, g.edges * sizeof(int))) fprintf(stderr, "ERROR: could not allocate eweight\n"); if (cudaSuccess != cudaMemcpy(g_gpu.nindex, g.nindex, (g.nodes + 1) * sizeof(int), cudaMemcpyHostToDevice)) fprintf(stderr, "ERROR: copying of index to device failed\n"); if (cudaSuccess != cudaMemcpy(g_gpu.nlist, g.nlist, g.edges * sizeof(int), cudaMemcpyHostToDevice)) fprintf(stderr, "ERROR: copying of nlist to device failed\n"); if (cudaSuccess != cudaMemcpy(g_gpu.eweight, g.eweight, g.edges * sizeof(int), cudaMemcpyHostToDevice)) fprintf(stderr, "ERROR: copying of eweight to device failed\n"); GPUparallelBellmanFordPull(source, g_gpu, distance, device, argv); printf("runtime: %.6f s\n", end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1000000.0); // print result int maxnode = 0; for (int v = 1; v < g.nodes; v++) { if (distance[maxnode] < distance[v]) maxnode = v; } printf("vertex %d has maximum distance %d\n", maxnode, distance[maxnode]); // compare solutions int* const verify = new int[g.nodes]; CPUserialDijkstra(source, g, verify); for (int v = 0; v < g.nodes; v++) { if (distance[v] != verify[v]) {fprintf(stderr, "ERROR: verification failed for node %d: %d instead of %d\n", v, distance[v], verify[v]); exit(-1);} } printf("verification passed\n\n"); // free memory delete [] verify; delete[] distance; cudaFree(g_gpu.nindex); cudaFree(g_gpu.nlist); cudaFree(g_gpu.eweight); freeECLgraph(g); return 0; }