Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions HIP-Basic/matrix_multiplication/main.hip
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
#include <algorithm>
#include <iostream>
#include <vector>

#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <memory>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed them in the new commit


/// \brief Multiplies matrices \p A and \p B and stores the result to \p C.
/// - The number of rows of the result matrix is equal to the number of rows of matrix A
Expand Down Expand Up @@ -108,6 +109,53 @@ __global__ void matrix_multiplication_kernel(const float* A,
// Every thread stores the final result to global memory.
C[block_offset + b_cols * ty + tx] = thread_result;
}

bool checkValidity (const float* A, const float* B, const float* C, size_t a_rows,size_t a_cols, size_t b_cols)
{
const float EPSILON = 0.001;
const int BLOCK_SIZE = 64;

std::vector<float> golden_c(b_cols * a_rows);

for (size_t i_block = 0; i_block < a_rows; i_block += BLOCK_SIZE) {
for (size_t j_block = 0; j_block < b_cols; j_block += BLOCK_SIZE) {
for (size_t t_block = 0; t_block < a_cols; t_block += BLOCK_SIZE) {
int i_end = std::min(i_block + BLOCK_SIZE, a_rows);
int j_end = std::min(j_block + BLOCK_SIZE, b_cols);
int t_end = std::min(t_block + BLOCK_SIZE, a_cols);


for (int i = i_block; i < i_end; ++i) {
for (int t = t_block; t < t_end; ++t) {
float a_val = A[i * a_cols + t];
for (int j = j_block; j < j_end; ++j) {
golden_c[i * b_cols + j] += a_val * B[t * b_cols + j];
}
}
}
}
}
}

for (size_t i = 0; i < a_rows; ++i)
{
for (size_t j = 0; j < b_cols; ++j)
{
float absdiff = abs(C[i*b_cols+j] - golden_c[i*b_cols+j]);
if(absdiff > EPSILON)
{
std::cerr << "\nVALIDATION FAILED!!!\n reference" << "[" << i << ", " << j << "] = "
<< golden_c[i*b_cols+j] << ",\n calculated" << "[" << i << ", " << j << "] = "
<< C[i*b_cols+j]
<< ",\n absolute difference" << "[" << i << ", " << j << "] = " << absdiff << "\n"
<< "Further validation was stopped\n\n";
return false;
}
}
}
return true;
}

template<unsigned int BlockSize>
void configure_parser(cli::Parser& parser)
{
Expand Down Expand Up @@ -165,11 +213,9 @@ int main(int argc, const char* argv[])
std::vector<float> B(b_cols * b_rows);
std::vector<float> C(c_cols * c_rows);

// Set matrix elements to a constant on the host.
std::fill(A.begin(), A.end(), 1.F);

constexpr float b_value = 0.02F;
std::fill(B.begin(), B.end(), b_value);
// Set matrix elements to random value on the host.
for (size_t i = 0; i < A.size(); ++i) A[i] = static_cast<float>(rand() / RAND_MAX );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be static_cast<double>(rand()) / RAND_MAX, static_cast<float>(rand() / RAND_MAX ) would result in 0 most of the time (integer division)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reminding. we can change it to be "static_cast(rand() / (RAND_MAX+1.0f) );", and it will generate [0,1) random float.

for (size_t i = 0; i < B.size(); ++i) B[i] = static_cast<float>(rand() / RAND_MAX );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it to be "static_cast(rand() / (RAND_MAX+1.0f) )", and verified that it can generate [0,1) random float value


const size_t a_bytes = sizeof(float) * A.size();
const size_t b_bytes = sizeof(float) * B.size();
Expand Down Expand Up @@ -203,11 +249,7 @@ int main(int argc, const char* argv[])
HIP_CHECK(hipFree(d_C));

// Check if the resulting elements match the expectation.
constexpr float tolerance = 0.001F;
const bool validation_passed = std::all_of(
C.begin(),
C.end(),
[=](const float value) { return tolerance > std::abs(value - a_cols * b_value); });
bool validation_passed = checkValidity(A.data(),B.data(),C.data(),a_rows,a_cols,b_cols);
if(validation_passed)
{
std::cout << "Validation passed." << std::endl;
Expand Down
Loading