Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
artv3 committed Nov 1, 2024
1 parent 573c407 commit 0af63f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ endif ()

set (raja_depends)

if (DEFINED caliper_INCLUDE_DIR)
find_package(caliper REQUIRED
NO_DEFAULT_PATH
PATHS ${caliper_DIR}
# ${caliper_DIR}/share/cmake/caliper
)
endif()

if (RAJA_ENABLE_OPENMP)
set (raja_depends
openmp)
Expand Down
94 changes: 50 additions & 44 deletions examples/tut_daxpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,32 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <caliper/cali.h>

#include "RAJA/RAJA.hpp"

/*
* Daxpy Example
*
* Computes a += b*c, where a, b are vectors of doubles
* and c is a scalar double. It illustrates similarities between a
* C-style for-loop and a RAJA forall loop.
* and c is a scalar double. It illustrates similarities between a
* C-style for-loop and a RAJA forall loop.
*
* RAJA features shown:
* - `forall` loop iteration template method
* - Index range segment
* - Index range segment
* - Execution policies
*/

//
// Functions for checking and printing results
//
void checkResult(double* v1, double* v2, int len);
void printResult(double* v, int len);
void printResult(double* v, int len);

int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{

CALI_CXX_MARK_FUNCTION;
std::cout << "\n\nRAJA daxpy example...\n";

//
Expand All @@ -48,17 +49,17 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))

double* ta = new double[N];
double* tb = new double[N];

double c = 3.14159;

for (int i = 0; i < N; i++) {
a0[i] = 1.0;
tb[i] = 2.0;
}

//
// Declare and set pointers to array data.
// We reset them for each daxpy version so that
// Declare and set pointers to array data.
// We reset them for each daxpy version so that
// they all look the same.
//

Expand All @@ -69,42 +70,47 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
//----------------------------------------------------------------------------//

std::cout << "\n Running C-version of daxpy...\n";

std::memcpy( a, a0, N * sizeof(double) );

for (int i = 0; i < N; ++i) {
a[i] += b[i] * c;
std::memcpy( a, a0, N * sizeof(double) );
{
CALI_CXX_MARK_SCOPE("Running C-version");
for (int i = 0; i < N; ++i) {
a[i] += b[i] * c;
}
}

std::memcpy( aref, a, N* sizeof(double) );
std::memcpy( aref, a, N* sizeof(double) );

//----------------------------------------------------------------------------//

//
// In the following, we show a RAJA version
// of the daxpy operation and how it can
// be run differently by choosing different
// RAJA execution policies.
// RAJA execution policies.
//
// Note that the only thing that changes in
// Note that the only thing that changes in
// these versions is the execution policy.
// To implement these cases using the
// To implement these cases using the
// programming model choices directly, would
// require unique changes for each.
//

//----------------------------------------------------------------------------//

std::cout << "\n Running RAJA sequential daxpy...\n";

std::memcpy( a, a0, N * sizeof(double) );

RAJA::forall<RAJA::seq_exec>(RAJA::RangeSegment(0, N), [=] (int i) {
a[i] += b[i] * c;
});
std::memcpy( a, a0, N * sizeof(double) );

{
CALI_CXX_MARK_SCOPE("Running RAJA sequential");
RAJA::forall<RAJA::seq_exec>(RAJA::RangeSegment(0, N), [=] (int i) {
a[i] += b[i] * c;
});
}

checkResult(a, aref, N);
//printResult(a, N);
//printResult(a, N);


//----------------------------------------------------------------------------//
Expand All @@ -113,30 +119,30 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
// RAJA SIMD version.
//
std::cout << "\n Running RAJA SIMD daxpy...\n";
std::memcpy( a, a0, N * sizeof(double) );

std::memcpy( a, a0, N * sizeof(double) );

RAJA::forall<RAJA::simd_exec>(RAJA::RangeSegment(0, N), [=] (int i) {
a[i] += b[i] * c;
});

checkResult(a, aref, N);
//printResult(a, N);
//printResult(a, N);


//----------------------------------------------------------------------------//

#if defined(RAJA_ENABLE_OPENMP)
std::cout << "\n Running RAJA OpenMP daxpy...\n";
std::memcpy( a, a0, N * sizeof(double) );

std::memcpy( a, a0, N * sizeof(double) );

RAJA::forall<RAJA::omp_parallel_for_exec>(RAJA::RangeSegment(0, N), [=] (int i) {
a[i] += b[i] * c;
});

checkResult(a, aref, N);
//printResult(a, N);
//printResult(a, N);
#endif

//----------------------------------------------------------------------------//
Expand All @@ -150,11 +156,11 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
a = 0; b = 0;
cudaErrchk(cudaMalloc( (void**)&a, N * sizeof(double) ));
cudaErrchk(cudaMalloc( (void**)&b, N * sizeof(double) ));

cudaErrchk(cudaMemcpy( a, a0, N * sizeof(double), cudaMemcpyHostToDevice ));
cudaErrchk(cudaMemcpy( b, tb, N * sizeof(double), cudaMemcpyHostToDevice ));

RAJA::forall<RAJA::cuda_exec<256>>(RAJA::RangeSegment(0, N),
cudaErrchk(cudaMemcpy( a, a0, N * sizeof(double), cudaMemcpyHostToDevice ));
cudaErrchk(cudaMemcpy( b, tb, N * sizeof(double), cudaMemcpyHostToDevice ));

RAJA::forall<RAJA::cuda_exec<256>>(RAJA::RangeSegment(0, N),
[=] RAJA_DEVICE (int i) {
a[i] += b[i] * c;
});
Expand All @@ -166,7 +172,7 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))

a = ta;
checkResult(a, aref, N);
//printResult(a, N);
//printResult(a, N);
#endif

//----------------------------------------------------------------------------//
Expand Down Expand Up @@ -202,13 +208,13 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
//----------------------------------------------------------------------------//

//
// Clean up.
// Clean up.
//
delete[] a0;
delete[] aref;
delete[] ta;
delete[] a0;
delete[] aref;
delete[] ta;
delete[] tb;

std::cout << "\n DONE!...\n";

return 0;
Expand All @@ -217,7 +223,7 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
//
// Function to compare result to reference and report P/F.
//
void checkResult(double* v1, double* v2, int len)
void checkResult(double* v1, double* v2, int len)
{
bool match = true;
for (int i = 0; i < len; i++) {
Expand All @@ -227,13 +233,13 @@ void checkResult(double* v1, double* v2, int len)
std::cout << "\n\t result -- PASS\n";
} else {
std::cout << "\n\t result -- FAIL\n";
}
}
}

//
// Function to print result.
// Function to print result.
//
void printResult(double* v, int len)
void printResult(double* v, int len)
{
std::cout << std::endl;
for (int i = 0; i < len; i++) {
Expand Down

0 comments on commit 0af63f7

Please sign in to comment.