From 9e5fd40838892424d747319bd09a3c5c5df15784 Mon Sep 17 00:00:00 2001 From: Oskar Lappi Date: Thu, 4 Jun 2020 18:47:31 +0300 Subject: [PATCH] Changes after code review by Johannes, and clang-format --- include/astaroth_utils.h | 6 ++++ samples/mpitest/main.cc | 20 ++++++------- src/core/device.cc | 46 +++++++++++++--------------- src/utils/verification.c | 65 ++++++++++++++++++++++++++++++++-------- 4 files changed, 89 insertions(+), 48 deletions(-) diff --git a/include/astaroth_utils.h b/include/astaroth_utils.h index 88c5a05..d96074c 100644 --- a/include/astaroth_utils.h +++ b/include/astaroth_utils.h @@ -79,6 +79,12 @@ AcResult acMeshCreate(const AcMeshInfo mesh_info, AcMesh* mesh); /** */ AcResult acMeshDestroy(AcMesh* mesh); +/** */ +AcScalReductionTestCase acCreateScalReductionTestCase(const char* label, const VertexBufferHandle vtxbuf, const ReductionType rtype); + +/** */ +AcVecReductionTestCase acCreateVecReductionTestCase(const char* label, const VertexBufferHandle a, const VertexBufferHandle b, const VertexBufferHandle c, const ReductionType rtype); + /** */ AcResult acMeshSet(const AcReal value, AcMesh* mesh); diff --git a/samples/mpitest/main.cc b/samples/mpitest/main.cc index b25d990..f8de568 100644 --- a/samples/mpitest/main.cc +++ b/samples/mpitest/main.cc @@ -57,19 +57,19 @@ main(void) // clang-format off // Define scalar reduction tests here std::vector scalarReductionTests{ - AcScalReductionTestCase{"Scalar MAX", VTXBUF_UUX, RTYPE_MAX, 0}, - AcScalReductionTestCase{"Scalar MIN", VTXBUF_UUX, RTYPE_MIN, 0}, - AcScalReductionTestCase{"Scalar RMS", VTXBUF_UUX, RTYPE_RMS, 0}, - AcScalReductionTestCase{"Scalar RMS_EXP", VTXBUF_UUX, RTYPE_RMS_EXP, 0}, - AcScalReductionTestCase{"Scalar SUM", VTXBUF_UUX, RTYPE_SUM, 0} + acCreateScalReductionTestCase("Scalar MAX", VTXBUF_UUX, RTYPE_MAX), + acCreateScalReductionTestCase("Scalar MIN", VTXBUF_UUX, RTYPE_MIN), + acCreateScalReductionTestCase("Scalar RMS", VTXBUF_UUX, RTYPE_RMS), + acCreateScalReductionTestCase("Scalar RMS_EXP", VTXBUF_UUX, RTYPE_RMS_EXP), + acCreateScalReductionTestCase("Scalar SUM", VTXBUF_UUX, RTYPE_SUM) }; // Define vector reduction tests here std::vector vectorReductionTests{ - AcVecReductionTestCase{"Vector MAX", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_MAX, 0}, - AcVecReductionTestCase{"Vector MIN", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_MIN, 0}, - AcVecReductionTestCase{"Vector RMS", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_RMS, 0}, - AcVecReductionTestCase{"Vector RMS_EXP", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_RMS_EXP, 0}, - AcVecReductionTestCase{"Vector SUM", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_SUM, 0} + acCreateVecReductionTestCase("Vector MAX", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_MAX), + acCreateVecReductionTestCase("Vector MIN", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_MIN), + acCreateVecReductionTestCase("Vector RMS", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_RMS), + acCreateVecReductionTestCase("Vector RMS_EXP", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_RMS_EXP), + acCreateVecReductionTestCase("Vector SUM", VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ, RTYPE_SUM) }; // clang-format on diff --git a/src/core/device.cc b/src/core/device.cc index c82e80f..a3ead26 100644 --- a/src/core/device.cc +++ b/src/core/device.cc @@ -1283,9 +1283,9 @@ acGridIntegrate(const Stream stream, const AcReal dt) ERRCHK(grid.initialized); // acGridSynchronizeStream(stream); - const Device device = grid.device; - const int3 nn = grid.nn; - //CommData corner_data = grid.corner_data; // Do not rm: required for corners + const Device device = grid.device; + const int3 nn = grid.nn; + // CommData corner_data = grid.corner_data; // Do not rm: required for corners CommData edgex_data = grid.edgex_data; CommData edgey_data = grid.edgey_data; CommData edgez_data = grid.edgez_data; @@ -1621,7 +1621,6 @@ acGridPeriodicBoundconds(const Stream stream) return AC_SUCCESS; } - static AcResult acMPIReduceScal(const AcReal local_result, const ReductionType rtype, AcReal* result) { @@ -1629,19 +1628,22 @@ acMPIReduceScal(const AcReal local_result, const ReductionType rtype, AcReal* re MPI_Op op; if (rtype == RTYPE_MAX) { op = MPI_MAX; - } else if (rtype == RTYPE_MIN) { + } + else if (rtype == RTYPE_MIN) { op = MPI_MIN; - } else if (rtype == RTYPE_RMS || rtype == RTYPE_RMS_EXP || rtype == RTYPE_SUM) { + } + else if (rtype == RTYPE_RMS || rtype == RTYPE_RMS_EXP || rtype == RTYPE_SUM) { op = MPI_SUM; - } else { + } + else { ERROR("Unrecognised rtype"); } - #if AC_DOUBLE_PRECISION == 1 +#if AC_DOUBLE_PRECISION == 1 MPI_Datatype datatype = MPI_DOUBLE; - #else +#else MPI_Datatype datatype = MPI_FLOAT; - #endif +#endif /* int rank; @@ -1655,12 +1657,12 @@ acMPIReduceScal(const AcReal local_result, const ReductionType rtype, AcReal* re MPI_Allreduce(&local_result, &mpi_res, 1, datatype, op, MPI_COMM_WORLD); if (rtype == RTYPE_RMS || rtype == RTYPE_RMS_EXP) { - const AcReal inv_n = AcReal(1.) / (grid.nn.x*grid.decomposition.x * grid.nn.y*grid.decomposition.y * grid.nn.z*grid.decomposition.z); - mpi_res = sqrt(inv_n * mpi_res); + const AcReal inv_n = AcReal(1.) / (grid.nn.x * grid.decomposition.x * grid.nn.y * + grid.decomposition.y * grid.nn.z * grid.decomposition.z); + mpi_res = sqrt(inv_n * mpi_res); } *result = mpi_res; return AC_SUCCESS; - } AcResult @@ -1668,10 +1670,8 @@ acGridReduceScal(const Stream stream, const ReductionType rtype, const VertexBufferHandle vtxbuf_handle, AcReal* result) { ERRCHK(grid.initialized); - // acGridSynchronizeStream(stream); - const Device device = grid.device; - //const int3 nn = grid.nn; + const Device device = grid.device; acGridSynchronizeStream(STREAM_ALL); MPI_Barrier(MPI_COMM_WORLD); @@ -1679,20 +1679,16 @@ acGridReduceScal(const Stream stream, const ReductionType rtype, AcReal local_result; acDeviceReduceScal(device, stream, rtype, vtxbuf_handle, &local_result); - return acMPIReduceScal(local_result,rtype,result); + return acMPIReduceScal(local_result, rtype, result); } - AcResult -acGridReduceVec(const Stream stream, const ReductionType rtype, - const VertexBufferHandle vtxbuf0, const VertexBufferHandle vtxbuf1, - const VertexBufferHandle vtxbuf2, AcReal* result) +acGridReduceVec(const Stream stream, const ReductionType rtype, const VertexBufferHandle vtxbuf0, + const VertexBufferHandle vtxbuf1, const VertexBufferHandle vtxbuf2, AcReal* result) { ERRCHK(grid.initialized); - // acGridSynchronizeStream(stream); - const Device device = grid.device; - //const int3 nn = grid.nn; + const Device device = grid.device; acGridSynchronizeStream(STREAM_ALL); MPI_Barrier(MPI_COMM_WORLD); @@ -1700,7 +1696,7 @@ acGridReduceVec(const Stream stream, const ReductionType rtype, AcReal local_result; acDeviceReduceVec(device, stream, rtype, vtxbuf0, vtxbuf1, vtxbuf2, &local_result); - return acMPIReduceScal(local_result,rtype,result); + return acMPIReduceScal(local_result, rtype, result); } #endif // AC_MPI_ENABLED diff --git a/src/utils/verification.c b/src/utils/verification.c index fcae0bc..393fbc3 100644 --- a/src/utils/verification.c +++ b/src/utils/verification.c @@ -105,8 +105,9 @@ get_max_abs_error(const VertexBufferHandle vtxbuf_handle, const AcMesh model_mes error = curr_error; } - error.handle = vtxbuf_handle; - strcpy(error.label, vtxbuf_names[vtxbuf_handle]); + error.handle = vtxbuf_handle; + strncpy(error.label, vtxbuf_names[vtxbuf_handle], ERROR_LABEL_LENGTH - 1); + error.label[ERROR_LABEL_LENGTH - 1] = '\0'; error.maximum_magnitude = get_maximum_magnitude(model_vtxbuf, model_mesh.info); error.minimum_magnitude = get_minimum_magnitude(model_vtxbuf, model_mesh.info); @@ -178,16 +179,18 @@ acVerifyMesh(const AcMesh model, const AcMesh candidate) } /** Verification function for scalar reductions*/ -AcResult -acVerifyScalReductions(const AcMesh model, const AcScalReductionTestCase* testCases, const size_t numCases) +AcResult +acVerifyScalReductions(const AcMesh model, const AcScalReductionTestCase* testCases, + const size_t numCases) { printf("\nTesting scalar reductions:\n"); bool errors_found = false; - for (size_t i = 0; i < numCases; i++){ + for (size_t i = 0; i < numCases; i++) { AcReal model_reduction = acModelReduceScal(model, testCases[i].rtype, testCases[i].vtxbuf); - Error error = acGetError(model_reduction, testCases[i].candidate); - strcpy(error.label, testCases[i].label); + Error error = acGetError(model_reduction, testCases[i].candidate); + strncpy(error.label, testCases[i].label, ERROR_LABEL_LENGTH - 1); + error.label[ERROR_LABEL_LENGTH - 1] = '\0'; errors_found |= printErrorToScreen(error); } printf("%s\n", errors_found ? "Failure. Found errors in one or more scalar reductions" @@ -197,16 +200,19 @@ acVerifyScalReductions(const AcMesh model, const AcScalReductionTestCase* testCa } /** Verification function for vector reductions*/ -AcResult -acVerifyVecReductions(const AcMesh model, const AcVecReductionTestCase* testCases, const size_t numCases) +AcResult +acVerifyVecReductions(const AcMesh model, const AcVecReductionTestCase* testCases, + const size_t numCases) { printf("\nTesting vector reductions:\n"); bool errors_found = false; - for (size_t i = 0; i < numCases; i++){ - AcReal model_reduction = acModelReduceVec(model, testCases[i].rtype, testCases[i].a, testCases[i].b, testCases[i].c); - Error error = acGetError(model_reduction, testCases[i].candidate); - strcpy(error.label, testCases[i].label); + for (size_t i = 0; i < numCases; i++) { + AcReal model_reduction = acModelReduceVec(model, testCases[i].rtype, testCases[i].a, + testCases[i].b, testCases[i].c); + Error error = acGetError(model_reduction, testCases[i].candidate); + strncpy(error.label, testCases[i].label, ERROR_LABEL_LENGTH - 1); + error.label[ERROR_LABEL_LENGTH - 1] = '\0'; errors_found |= printErrorToScreen(error); } printf("%s\n", errors_found ? "Failure. Found errors in one or more vector reductions" @@ -214,3 +220,36 @@ acVerifyVecReductions(const AcMesh model, const AcVecReductionTestCase* testCase return errors_found ? AC_FAILURE : AC_SUCCESS; } + +/** Constructor for scalar reduction test case */ +AcScalReductionTestCase +acCreateScalReductionTestCase(const char* label, const VertexBufferHandle vtxbuf, const ReductionType rtype) +{ + AcScalReductionTestCase testCase; + + strncpy(testCase.label,label,ERROR_LABEL_LENGTH - 1); + testCase.label[ERROR_LABEL_LENGTH - 1] = '\0'; + testCase.vtxbuf = vtxbuf; + testCase.rtype = rtype; + testCase.candidate = 0; + + return testCase; +} + +/** Constructor for vector reduction test case */ +AcVecReductionTestCase +acCreateVecReductionTestCase(const char* label, const VertexBufferHandle a, + const VertexBufferHandle b, const VertexBufferHandle c, const ReductionType rtype) +{ + AcVecReductionTestCase testCase; + + strncpy(testCase.label,label,ERROR_LABEL_LENGTH - 1); + testCase.label[ERROR_LABEL_LENGTH - 1] = '\0'; + testCase.a = a; + testCase.b = b; + testCase.c = c; + testCase.rtype = rtype; + testCase.candidate = 0; + + return testCase; +}