Skip to content

Commit

Permalink
modified testing and added atomicAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
qinatan committed Jun 30, 2023
1 parent a2e1be6 commit ae390c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
23 changes: 3 additions & 20 deletions src/process/process_set.cu
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@ namespace micm {
//one thread per reaction
//passing all device pointers


__device__ double atomicSub(double* address, double val)
{
unsigned long long int* address_as_ull =
(unsigned long long int*)address;
unsigned long long int old = *address_as_ull, assumed;

do {
assumed = old;
old = atomicCAS(address_as_ull, assumed,
__double_as_longlong(val -
__longlong_as_double(assumed)));

// Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
} while (assumed != old);

return __longlong_as_double(old);
}

__device__ double atomicAdd(double* address, double val)
{
unsigned long long int* address_as_ull =
Expand Down Expand Up @@ -87,7 +68,8 @@ __device__ double atomicAdd(double* address, double val)
for (int i_reactant = 0; i_reactant < reactant_num; i_reactant++){
int reactant_ids_index = initial_reactant_ids_index + i_reactant;
int state_forcing_col_index = reactant_ids_[reactant_ids_index];
atomicSub(&forcing[row_index * state_forcing_columns + state_forcing_col_index], rate);
double rate_subtration = 0 - rate;
atomicSub(&forcing[row_index * state_forcing_columns + state_forcing_col_index], rate_subtration);
}

for (int i_product = 0; i_product < product_num; i_product++){
Expand Down Expand Up @@ -211,6 +193,7 @@ __device__ double atomicAdd(double* address, double val)

cudaMemcpy(forcing_data, d_forcing, state_forcing_bytes, cudaMemcpyDeviceToHost);

//clean up
cudaFree(d_rate_constants);
cudaFree(d_state_variables);
cudaFree(d_forcing);
Expand Down
20 changes: 10 additions & 10 deletions test/unit/process/test_cuda_process_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ TEST(ProcessSet, Constructor)
state.variables_,
forcing);

EXPECT_NEAR(forcing[0][0], 1000.0 - 10.0 * 0.1 * 0.3 + 20.0 * 0.2, 1e-15);
EXPECT_NEAR(forcing[1][0], 1000.0 - 110.0 * 1.1 * 1.3 + 120.0 * 1.2, 1e-15);
EXPECT_NEAR(forcing[0][1], 1000.0 + 10.0 * 0.1 * 0.3 - 20.0 * 0.2, 1e-15);
EXPECT_NEAR(forcing[1][1], 1000.0 + 110.0 * 1.1 * 1.3 - 120.0 * 1.2,1e-15);
EXPECT_NEAR(forcing[0][2], 1000.0 - 10.0 * 0.1 * 0.3, 1e-15);
EXPECT_NEAR(forcing[1][2], 1000.0 - 110.0 * 1.1 * 1.3, 1e-15);
EXPECT_NEAR(forcing[0][3], 1000.0 + 20.0 * 0.2 * 1.4 - 30.0 * 0.4, 1e-15);
EXPECT_NEAR(forcing[1][3], 1000.0 + 120.0 * 1.2 * 1.4 - 130.0 * 1.4, 1e-15);
EXPECT_NEAR(forcing[0][4], 1000.0 + 10.0 * 0.1 * 0.3 * 2.4, 1e-15);
EXPECT_NEAR(forcing[1][4], 1000.0 + 110.0 * 1.1 * 1.3 * 2.4, 1e-15);
EXPECT_DOUBLE_EQ(forcing[0][0], 1000.0 - 10.0 * 0.1 * 0.3 + 20.0 * 0.2, 1e-15);
EXPECT_DOUBLE_EQ(forcing[1][0], 1000.0 - 110.0 * 1.1 * 1.3 + 120.0 * 1.2, 1e-15);
EXPECT_DOUBLE_EQ(forcing[0][1], 1000.0 + 10.0 * 0.1 * 0.3 - 20.0 * 0.2, 1e-15);
EXPECT_DOUBLE_EQ(forcing[1][1], 1000.0 + 110.0 * 1.1 * 1.3 - 120.0 * 1.2,1e-15);
EXPECT_DOUBLE_EQ(forcing[0][2], 1000.0 - 10.0 * 0.1 * 0.3, 1e-15);
EXPECT_DOUBLE_EQ(forcing[1][2], 1000.0 - 110.0 * 1.1 * 1.3, 1e-15);
EXPECT_DOUBLE_EQ(forcing[0][3], 1000.0 + 20.0 * 0.2 * 1.4 - 30.0 * 0.4, 1e-15);
EXPECT_DOUBLE_EQ(forcing[1][3], 1000.0 + 120.0 * 1.2 * 1.4 - 130.0 * 1.4, 1e-15);
EXPECT_DOUBLE_EQ(forcing[0][4], 1000.0 + 10.0 * 0.1 * 0.3 * 2.4, 1e-15);
EXPECT_DOUBLE_EQ(forcing[1][4], 1000.0 + 110.0 * 1.1 * 1.3 * 2.4, 1e-15);



Expand Down

0 comments on commit ae390c1

Please sign in to comment.