/* ECL Floyd-Warshall code: This code performs the Floyd-Warshall algorithms to compute all pairs shortest paths Additionally, it records the effect on energy by differing initialization values. This code is paired with ECL_FW_cuda_64.h 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 "ecl-apsp.h" using mtype = int; static void FW_cpu(const int size, int * const edge_mat, mtype* const AdjMat) { #pragma omp parallel for default(none) shared(edge_mat, AdjMat) for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { AdjMat[i * size + j] = ((i == j) ? 0 : (INT_MAX / 2)); } } #pragma omp parallel for default(none) shared(edge_mat, AdjMat) for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (edge_mat[i * size + j] != 0){ AdjMat[i * size + j] = edge_mat[i * size + j]; } } } #pragma omp parallel default(none) shared(edge_mat, AdjMat) for (int k = 0; k < size; k++) { #pragma omp for for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (AdjMat[i * size + j] > AdjMat[i * size + k] + AdjMat[k * size + j]) { AdjMat[i * size + j] = AdjMat[i * size + k] + AdjMat[k * size + j]; } } } } } static inline unsigned int hash(unsigned int val) { val = ((val >> 16) ^ val) * 0x45d9f3b; val = ((val >> 16) ^ val) * 0x45d9f3b; return (val >> 16) ^ val; } int main(int argc, char* argv[]) { printf("ECL-FW (%s)\n", __FILE__); if (argc != 4) {fprintf(stderr, "USAGE: %s size num_edges device\n", argv[0]); exit(-1);} const int size = atoi(argv[1]); const int num_edges = atoi(argv[2]); const int d = atoi(argv[3]); const int tiles_per_blk = 1; int* const edge_mat = new int[size * size]; printf("%d V %d E\n", size, num_edges); // Randomly generate edges // Initialize with 0s (to be replaced by the correct sentinel later) for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { edge_mat[i * size + j] = 0; } } for (int i = 0; i < num_edges; i++) { const int x = hash(i + 1) % size; const int y = hash(i + num_edges + 1) % size; // Assign a random positive weight that is lower than our max for a single edge int val = (hash(i + 2 * num_edges + 1) % (INT_MAX / 32)) + 1; edge_mat[x * size + y] = val; } // run GPU_64 code const int upper_64 = ((size + tile - 1) / tile) * tile; // round up mtype* const AdjMat3 = new mtype [upper_64 * upper_64]; FW_gpu_64(size, edge_mat, AdjMat3, d, tiles_per_blk); // run CPU code mtype* const AdjMat4 = new mtype [size * size]; FW_cpu(size, edge_mat, AdjMat4); // compare results int diffcount = 0; const int gn = size; for (int i = 0; i < gn; ++i) { for (int j = 0; j < gn; ++j) { if (AdjMat3[i * upper_64 + j] != AdjMat4[i * size + j]) { if ((AdjMat3[i * upper_64 + j] == (INT_MAX / 4) + 1) != (AdjMat4[i * size + j] == INT_MAX / 2)){ printf("%d, %d\n%d, %d\n", AdjMat3[i * upper_64 + j], AdjMat4[i * upper_64 + j], i, j); diffcount++; } } } } if (diffcount > 0) { printf("ERROR: results differ in %d places!\n", diffcount); } else { printf("results match\n"); } // clean up delete [] AdjMat3; delete [] AdjMat4; delete [] edge_mat; return 0; }