Function bodies 55 total
DataLoader class · cpp · L10-L33 (24 LOC)main.cpp
class DataLoader {
public:
bool loadDataFromFile(const std::string &filename, std::vector<float> &data) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
std::stringstream ss(line);
float value;
while (ss >> value) {
data.push_back(value);
}
}
file.close();
return true;
}
};loadDataFromFile method · cpp · L12-L32 (21 LOC)main.cpp
public:
bool loadDataFromFile(const std::string &filename, std::vector<float> &data) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
std::stringstream ss(line);
float value;
while (ss >> value) {
data.push_back(value);
}
}
file.close();
return true;
}main function · cpp · L34-L123 (90 LOC)main.cpp
int main() {
sjtu::GpuSimulator gpu_sim;
sjtu::MatrixMemoryAllocator matrix_memory_allocator;
std::vector<sjtu::Matrix *> keys;
std::vector<sjtu::Matrix *> values;
std::vector<sjtu::Matrix *> queries;
std::vector<sjtu::Matrix *> answers;
// Read Data from File
std::string filename = "./data/keys.txt";
std::vector<float> data;
DataLoader loader;
if (loader.loadDataFromFile(filename, data)) {
std::cerr << "Data loaded successfully!" << std::endl;
} else {
std::cerr << "Failed to load data from file." << std::endl;
}
for (int i = 0; i < 32; ++i) {
keys.push_back(
new sjtu::Matrix(1, 512,
std::vector<float>(data.begin() + i * 512,
data.begin() + (i + 1) * 512),
gpu_sim));
matrix_memory_allocator.Bind(keys.back(), "key_" + std::to_string(i));
}
data.clear();
filename = "./data/values.txt";
if (loader.loadDataFromFile(filename, dataMatrix class · cpp · L48-L182 (135 LOC)simulator.hpp
class Matrix {
public:
Matrix() = default;
Matrix(size_t row_num, size_t column_num);
Matrix(size_t row_num, size_t column_num, const std::vector<float> &data,
GpuSimulator &gpu_sim);
~Matrix() = default;
/* Set every element to 0.*/
void Zero() { std::fill(data_.begin(), data_.end(), 0.0f); }
/* Give every element a random value.*/
void Rand() {
for (size_t i = 0; i < GetSize(); ++i) {
data_[i] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
}
Position GetPosition() const { return position_; }
void PrintShape() const {
std::cerr << "(" << row_num_ << ", " << column_num_ << ")" << std::endl;
}
void Print() const {
std::cerr << "Matrix data: " << std::endl;
for (size_t i = 0; i < row_num_; ++i) {
for (size_t j = 0; j < column_num_; ++j) {
std::cerr << std::fixed << std::setprecision(1)
<< data_[GetDataIndex(i, j)] << " ";
}
std::cerr << std::endl;
}
stdRand method · cpp · L63-L67 (5 LOC)simulator.hpp
void Rand() {
for (size_t i = 0; i < GetSize(); ++i) {
data_[i] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
}PrintShape method · cpp · L70-L73 (4 LOC)simulator.hpp
void PrintShape() const {
std::cerr << "(" << row_num_ << ", " << column_num_ << ")" << std::endl;
}Print method · cpp · L74-L85 (12 LOC)simulator.hpp
void Print() const {
std::cerr << "Matrix data: " << std::endl;
for (size_t i = 0; i < row_num_; ++i) {
for (size_t j = 0; j < column_num_; ++j) {
std::cerr << std::fixed << std::setprecision(1)
<< data_[GetDataIndex(i, j)] << " ";
}
std::cerr << std::endl;
}
std::cerr << std::endl;
}All rows above produced by Repobility · https://repobility.com
GetDataIndex method · cpp · L92-L95 (4 LOC)simulator.hpp
size_t GetDataIndex(size_t row_index, size_t column_index) const {
return row_index * column_num_ + column_index;
}Transpose method · cpp · L100-L111 (12 LOC)simulator.hpp
private:
void Transpose() {
std::vector<float> transposed_data(GetSize());
for (size_t i = 0; i < row_num_; ++i) {
for (size_t j = 0; j < column_num_; ++j) {
transposed_data[j * row_num_ + i] = data_[i * column_num_ + j];
}
}
data_ = std::move(transposed_data);
std::swap(row_num_, column_num_);
}Reshape method · cpp · L114-L126 (13 LOC)simulator.hpp
void Reshape(size_t row_num, size_t column_num) {
if (row_num * column_num != GetSize()) {
std::cerr << "New shape does not match the number of elements."
<< std::endl;
return;
}
row_num_ = row_num;
column_num_ = column_num;
data_.resize(GetSize());
std::cerr << "Matrix reshaped to: ";
PrintShape();
}GpuSimulator class · cpp · L183-L413 (231 LOC)simulator.hpp
class GpuSimulator {
public:
/* ************************ Memory Instruction ************************ */
/*!
* \brief Move a matrix to GPU HBM.
* \param matrix The matrix to be moved.
* \note This operation will cost 300 size(matrix) cycles.
*/
void MoveMatrixToGpuHbm(Matrix *matrix);
/*!
* \brief Move a matrix to shared memory.
* \param matrix The matrix to be moved.
* \note This operation will cost 300 size(matrix) cycles.
*/
void MoveMatrixToSharedMem(Matrix *matrix);
/* ******************* Sram Calculation Instruction ******************* */
/*!
* \brief Add two matrices and store the result in a third matrix.
* \param lhs The left-hand side matrix.
* \param rhs The right-hand side matrix.
* \param result The matrix to store the result.
* \note This operation will cost size(lhs) cycles.
*/
void MatAdd(Matrix *lhs, Matrix *rhs, Matrix *result);
/*!
* \brief Subtract two matrices and store the result in a third matriRun method · cpp · L370-L375 (6 LOC)simulator.hpp
void Run(bool debug_print = false,
const MatrixMemoryAllocator *matrix_memory_allocator = nullptr) {
while (Advance(debug_print, matrix_memory_allocator))
;
}Rater class · cpp · L418-L463 (46 LOC)simulator.hpp
class Rater {
private:
std::vector<Matrix *> keys_;
std::vector<Matrix *> values_;
std::vector<Matrix *> queries_;
std::vector<Matrix *> answers_;
std::vector<int> errors_;
int current_query_index_ = 0;
bool next_query_available_ = true;
public:
Rater() = delete;
Rater(const Rater &) = delete;
Rater &operator=(const Rater &) = delete;
Rater(Rater &&) = delete;
~Rater() = default;
Rater(const std::vector<Matrix *> &keys, const std::vector<Matrix *> &values,
const std::vector<Matrix *> &queries,
const std::vector<Matrix *> &answers)
: keys_(keys), values_(values), queries_(queries), answers_(answers) {}
Matrix *GetNextQuery();
void CommitAnswer(Matrix &answer);
int GetErrorsCount() const {
int result = 0;
for (const auto &error : errors_) {
result += error;
}
return result;
}
void PrintResult(const GpuSimulator &) const;
friend void Test(Rater &rater, GpuSimulator &gpu_sim,
MaGetErrorsCount method · cpp · L448-L455 (8 LOC)simulator.hpp
int GetErrorsCount() const {
int result = 0;
for (const auto &error : errors_) {
result += error;
}
return result;
}MatrixMemoryAllocator class · cpp · L464-L495 (32 LOC)simulator.hpp
class MatrixMemoryAllocator {
private:
std::map<Matrix *, std::string> allocated_matrices_;
public:
MatrixMemoryAllocator() = default;
// ~MatrixMemoryAllocator() {
// for (auto matrix_pair : allocated_matrices_) {
// delete matrix_pair.first;
// }
// }
Matrix *Allocate(std::string name = "") {
Matrix *matrix = new Matrix();
allocated_matrices_[matrix] = name;
return matrix;
}
std::string GetMatrixName(Matrix *matrix) const {
auto it = allocated_matrices_.find(matrix);
if (it != allocated_matrices_.end()) {
return it->second;
}
return "Unknown Matrix";
}
void Bind(Matrix *matrix, const std::string &name) {
allocated_matrices_[matrix] = name;
}
};Generated by Repobility's multi-pass static-analysis pipeline (https://repobility.com)
GetMatrixName method · cpp · L483-L490 (8 LOC)simulator.hpp
std::string GetMatrixName(Matrix *matrix) const {
auto it = allocated_matrices_.find(matrix);
if (it != allocated_matrices_.end()) {
return it->second;
}
return "Unknown Matrix";
}Bind method · cpp · L491-L494 (4 LOC)simulator.hpp
void Bind(Matrix *matrix, const std::string &name) {
allocated_matrices_[matrix] = name;
}GetColumn method · cpp · L496-L514 (19 LOC)simulator.hpp
Matrix Matrix::GetColumn(const Matrix *matrix, size_t column_index,
GpuSimulator &gpu_sim) {
if (column_index >= matrix->GetColumnNum()) {
std::cerr << "Column index out of bounds." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(matrix->GetRowNum(), 1);
for (size_t i = 0; i < matrix->GetRowNum(); ++i) {
result.data_[i] = matrix->data_[i * matrix->GetColumnNum() + column_index];
}
result.position_ = matrix->position_;
if (result.position_ == kInGpuHbm) {
gpu_sim.gpu_hbm_used_ += result.GetSize();
} else {
gpu_sim.shared_mem_used_ += result.GetSize();
}
return result;
}GetRow method · cpp · L515-L533 (19 LOC)simulator.hpp
Matrix Matrix::GetRow(const Matrix *matrix, size_t row_index,
GpuSimulator &gpu_sim) {
if (row_index >= matrix->GetRowNum()) {
std::cerr << "Row index out of bounds." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(1, matrix->GetColumnNum());
for (size_t j = 0; j < matrix->GetColumnNum(); ++j) {
result.data_[j] = matrix->data_[row_index * matrix->GetColumnNum() + j];
}
result.position_ = matrix->position_;
if (result.position_ == kInGpuHbm) {
gpu_sim.gpu_hbm_used_ += result.GetSize();
} else {
gpu_sim.shared_mem_used_ += result.GetSize();
}
return result;
}Concat method · cpp · L534-L598 (65 LOC)simulator.hpp
Matrix Matrix::Concat(const Matrix *matrix1, const Matrix *matrix2, size_t axis,
GpuSimulator &gpu_sim) {
if (matrix1->GetPosition() != matrix2->GetPosition()) {
std::cerr
<< "Matrices must be in the same memory position for concatenation."
<< std::endl;
exit(EXIT_FAILURE);
}
if (axis == 0) { // Concatenate vertically
if (matrix1->GetColumnNum() != matrix2->GetColumnNum()) {
std::cerr << "Matrix concatenation dimension mismatch." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(matrix1->GetRowNum() + matrix2->GetRowNum(),
matrix1->GetColumnNum());
for (size_t i = 0; i < matrix1->GetRowNum(); ++i) {
for (size_t j = 0; j < matrix1->GetColumnNum(); ++j) {
result.data_[i * result.GetColumnNum() + j] =
matrix1->data_[i * matrix1->GetColumnNum() + j];
}
}
for (size_t i = 0; i < matrix2->GetRowNum(); ++i) {
for (size_t j = 0; j < matrix2->GetColumnNMulNum method · cpp · L599-L614 (16 LOC)simulator.hpp
Matrix Matrix::MulNum(const Matrix *matrix1, const Matrix *factor,
GpuSimulator &gpu_sim) {
if (factor->GetSize() != 1) {
std::cerr << "Factor must be a 1x1 matrix." << std::endl;
exit(EXIT_FAILURE);
}
auto factor_value = factor->data_[0];
Matrix result(matrix1->GetRowNum(), matrix1->GetColumnNum());
for (size_t i = 0; i < matrix1->GetSize(); ++i) {
result.data_[i] = matrix1->data_[i] * factor_value;
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}AddBias method · cpp · L615-L630 (16 LOC)simulator.hpp
Matrix Matrix::AddBias(const Matrix *matrix1, const Matrix *bias,
GpuSimulator &gpu_sim) {
if (bias->GetSize() != 1) {
std::cerr << "Bias must be a 1x1 matrix." << std::endl;
exit(EXIT_FAILURE);
}
auto bias_value = bias->data_[0];
Matrix result(matrix1->GetRowNum(), matrix1->GetColumnNum());
for (size_t i = 0; i < matrix1->GetSize(); ++i) {
result.data_[i] = matrix1->data_[i] + bias_value;
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}MatSub method · cpp · L631-L646 (16 LOC)simulator.hpp
Matrix Matrix::MatSub(const Matrix *matrix1, const Matrix *matrix2,
GpuSimulator &gpu_sim) {
if (matrix1->GetRowNum() != matrix2->GetRowNum() ||
matrix1->GetColumnNum() != matrix2->GetColumnNum()) {
std::cerr << "Matrix subtraction dimension mismatch." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(matrix1->GetRowNum(), matrix1->GetColumnNum());
for (size_t i = 0; i < matrix1->GetSize(); ++i) {
result.data_[i] = matrix1->data_[i] - matrix2->data_[i];
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}Methodology: Repobility · https://repobility.com/research/state-of-ai-code-2026/
MatAdd method · cpp · L647-L662 (16 LOC)simulator.hpp
Matrix Matrix::MatAdd(const Matrix *matrix1, const Matrix *matrix2,
GpuSimulator &gpu_sim) {
if (matrix1->GetRowNum() != matrix2->GetRowNum() ||
matrix1->GetColumnNum() != matrix2->GetColumnNum()) {
std::cerr << "Matrix addition dimension mismatch." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(matrix1->GetRowNum(), matrix1->GetColumnNum());
for (size_t i = 0; i < matrix1->GetSize(); ++i) {
result.data_[i] = matrix1->data_[i] + matrix2->data_[i];
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}position_ function · cpp · L680-L695 (16 LOC)simulator.hpp
position_(kInGpuHbm) {
if (row_num <= 0 || column_num <= 0) {
std::cerr << "Matrix dimensions must be positive. Set the matrix to "
"empty matrix."
<< std::endl;
row_num_ = 0;
column_num_ = 0;
}
if (data.size() != row_num * column_num) {
std::cerr << "Data size does not match the matrix dimensions. Set the "
"matrix to empty matrix."
<< std::endl;
exit(EXIT_FAILURE);
}
gpu_sim.gpu_hbm_used_ += GetSize();
}MatMul method · cpp · L696-L717 (22 LOC)simulator.hpp
Matrix Matrix::MatMul(const Matrix *matrix1, const Matrix *matrix2,
GpuSimulator &gpu_sim) {
if (matrix1->GetColumnNum() != matrix2->GetRowNum()) {
std::cerr << "Matrix multiplication dimension mismatch." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(matrix1->GetRowNum(), matrix2->GetColumnNum());
for (size_t i = 0; i < matrix1->GetRowNum(); ++i) {
for (size_t j = 0; j < matrix2->GetColumnNum(); ++j) {
float sum = 0.0f;
for (size_t k = 0; k < matrix1->GetColumnNum(); ++k) {
sum += matrix1->data_[i * matrix1->GetColumnNum() + k] *
matrix2->data_[k * matrix2->GetColumnNum() + j];
}
result.data_[i * result.GetColumnNum() + j] = sum;
}
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}MatDiv method · cpp · L718-L736 (19 LOC)simulator.hpp
Matrix Matrix::MatDiv(const Matrix *matrix1, const Matrix *divisor,
GpuSimulator &gpu_sim) {
if (divisor->GetSize() != 1) {
std::cerr << "Matrix division dimension mismatch." << std::endl;
exit(EXIT_FAILURE);
}
Matrix result(matrix1->GetRowNum(), matrix1->GetColumnNum());
for (size_t i = 0; i < matrix1->GetSize(); ++i) {
if (divisor->data_[0] == 0.0f) {
std::cerr << "Division by zero encountered." << std::endl;
exit(EXIT_FAILURE);
}
result.data_[i] = matrix1->data_[i] / divisor->data_[0];
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}MatExp method · cpp · L737-L746 (10 LOC)simulator.hpp
Matrix Matrix::MatExp(const Matrix *matrix, GpuSimulator &gpu_sim) {
Matrix result(matrix->GetRowNum(), matrix->GetColumnNum());
for (size_t i = 0; i < matrix->GetSize(); ++i) {
result.data_[i] = exp(matrix->data_[i]);
}
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}Sum method · cpp · L747-L758 (12 LOC)simulator.hpp
Matrix Matrix::Sum(const Matrix *matrix, GpuSimulator &gpu_sim) {
Matrix result(1, 1);
float sum = 0.0f;
for (const auto &value : matrix->data_) {
sum += value;
}
result.data_[0] = sum;
result.position_ = kInSharedMemory;
gpu_sim.shared_mem_used_ += result.GetSize();
return result;
}MoveMatrixToGpuHbm method · cpp · L759-L762 (4 LOC)simulator.hpp
void GpuSimulator::MoveMatrixToGpuHbm(Matrix *matrix) {
io_queue_.push({-1, InstructionType::kToGpuHbm, matrix});
}MoveMatrixToSharedMem method · cpp · L763-L766 (4 LOC)simulator.hpp
void GpuSimulator::MoveMatrixToSharedMem(Matrix *matrix) {
io_queue_.push({-1, InstructionType::kToSharedMem, matrix});
}Repobility · code-quality intelligence platform · https://repobility.com
ReleaseMatrix method · cpp · L767-L771 (5 LOC)simulator.hpp
void GpuSimulator::ReleaseMatrix(Matrix *matrix) {
calculate_queue_.push({-1, InstructionType::kRelease, matrix, nullptr, 0,
nullptr, kInSharedMemory});
}MatAdd method · cpp · L772-L776 (5 LOC)simulator.hpp
void GpuSimulator::MatAdd(Matrix *matrix1, Matrix *matrix2, Matrix *result) {
calculate_queue_.push({-1, InstructionType::kMatAdd, matrix1, matrix2, 0,
result, kInSharedMemory});
}MatSub method · cpp · L777-L781 (5 LOC)simulator.hpp
void GpuSimulator::MatSub(Matrix *matrix1, Matrix *matrix2, Matrix *result) {
calculate_queue_.push({-1, InstructionType::kMatSub, matrix1, matrix2, 0,
result, kInSharedMemory});
}MatMul method · cpp · L782-L786 (5 LOC)simulator.hpp
void GpuSimulator::MatMul(Matrix *matrix1, Matrix *matrix2, Matrix *result) {
calculate_queue_.push({-1, InstructionType::kMatmul, matrix1, matrix2, 0,
result, kInSharedMemory});
}MatDiv method · cpp · L787-L791 (5 LOC)simulator.hpp
void GpuSimulator::MatDiv(Matrix *matrix1, Matrix *divisor, Matrix *result) {
calculate_queue_.push({-1, InstructionType::kMatDiv, matrix1, divisor, 0,
result, kInSharedMemory});
}MatExp method · cpp · L792-L796 (5 LOC)simulator.hpp
void GpuSimulator::MatExp(Matrix *matrix, Matrix *result) {
calculate_queue_.push({-1, InstructionType::kMatExp, matrix, nullptr, 0,
result, kInSharedMemory});
}Copy method · cpp · L797-L801 (5 LOC)simulator.hpp
void GpuSimulator::Copy(Matrix *src, Matrix *dest, Position position) {
calculate_queue_.push(
{-1, InstructionType::kCopy, src, nullptr, 0, dest, position});
}GetColumn method · cpp · L802-L807 (6 LOC)simulator.hpp
void GpuSimulator::GetColumn(Matrix *matrix, size_t column_index,
Matrix *result, Position position) {
calculate_queue_.push({-1, InstructionType::kGetColumn, matrix, nullptr,
column_index, result, position});
}All rows above produced by Repobility · https://repobility.com
GetRow method · cpp · L808-L813 (6 LOC)simulator.hpp
void GpuSimulator::GetRow(Matrix *matrix, size_t row_index, Matrix *result,
Position position) {
calculate_queue_.push({-1, InstructionType::kGetRow, matrix, nullptr,
row_index, result, position});
}Concat method · cpp · L814-L819 (6 LOC)simulator.hpp
void GpuSimulator::Concat(Matrix *matrix1, Matrix *matrix2, Matrix *result,
size_t axis, Position position) {
calculate_queue_.push(
{-1, InstructionType::kConcat, matrix1, matrix2, axis, result, position});
}Sum method · cpp · L820-L824 (5 LOC)simulator.hpp
void GpuSimulator::Sum(Matrix *matrix, Matrix *result) {
calculate_queue_.push({-1, InstructionType::kSum, matrix, nullptr, -1, result,
kInSharedMemory});
}Transpose method · cpp · L825-L829 (5 LOC)simulator.hpp
void GpuSimulator::Transpose(Matrix *matrix, Position position) {
calculate_queue_.push(
{-1, InstructionType::kTranspose, matrix, nullptr, 0, nullptr, position});
}Reshape method · cpp · L830-L834 (5 LOC)simulator.hpp
void GpuSimulator::Reshape(Matrix *matrix, size_t new_row_num) {
calculate_queue_.push({-1, InstructionType::kReshape, matrix, nullptr,
new_row_num, nullptr, kInSharedMemory});
}UpdateTimeOfInstructions method · cpp · L835-L967 (133 LOC)simulator.hpp
void GpuSimulator::UpdateTimeOfInstructions() const {
// Update the time stamps of the instructions in the queues.
if (!calculate_queue_.empty()) {
auto &front_instruction =
const_cast<decltype(calculate_queue_)::value_type &>(
calculate_queue_.front());
if (std::get<0>(front_instruction) < 0) {
auto instruction_type = std::get<1>(front_instruction);
auto matrix1 = std::get<2>(front_instruction);
auto matrix2 = std::get<3>(front_instruction);
switch (instruction_type) {
case InstructionType::kConcat: {
if (matrix1->GetPosition() != matrix2->GetPosition() ||
matrix1->GetPosition() == Position::kReleased ||
matrix1->GetPosition() != std::get<6>(front_instruction)) {
break;
}
std::get<0>(front_instruction) =
(matrix1->GetSize() + matrix2->GetSize()) *
(matrix1->GetPosition() == Position::kInSharedMemory ? 1 : 25);
break;
}
casAdvance method · cpp · L968-L1051 (84 LOC)simulator.hpp
bool GpuSimulator::Advance(
bool debug_print, const MatrixMemoryAllocator *matrix_memory_allocator) {
UpdateTimeOfInstructions();
if (io_queue_.empty() && calculate_queue_.empty()) {
max_gpu_hbm_size_ = std::max(max_gpu_hbm_size_, gpu_hbm_used_);
max_shared_mem_size_ = std::max(max_shared_mem_size_, shared_mem_used_);
return false;
}
bool io_not_ready = io_queue_.empty() || std::get<0>(io_queue_.front()) < 0;
bool calc_not_ready =
calculate_queue_.empty() || std::get<0>(calculate_queue_.front()) < 0;
if (io_not_ready && calc_not_ready) {
std::cerr << "Both IO and calculation queues are empty or not ready."
<< std::endl;
exit(EXIT_FAILURE);
}
if (io_not_ready || calc_not_ready) {
if (io_not_ready) {
// We only need to execute calculation instructions.
CalculationInstruction instruction = calculate_queue_.front();
if (std::get<0>(instruction) >= 0) {
gpu_sim_cycle_ += std::get<0>(instruction);
DoCalc method · cpp · L1052-L1173 (122 LOC)simulator.hpp
inline void
GpuSimulator::DoCalc(GpuSimulator::CalculationInstruction instruction) {
auto result = std::get<5>(instruction);
if (result == nullptr &&
std::get<1>(instruction) != InstructionType::kRelease &&
std::get<1>(instruction) != InstructionType::kReshape &&
std::get<1>(instruction) != InstructionType::kTranspose) {
std::cerr << "Result matrix is null. The type of instruction is "
<< static_cast<int>(std::get<1>(instruction)) << std::endl;
exit(EXIT_FAILURE);
}
switch (std::get<1>(instruction)) {
case InstructionType::kMatAdd: {
*result = Matrix::MatAdd(std::get<2>(instruction), std::get<3>(instruction),
*this);
break;
};
case InstructionType::kMatSub: {
*result = Matrix::MatSub(std::get<2>(instruction), std::get<3>(instruction),
*this);
break;
};
case InstructionType::kMatmul: {
*result = Matrix::MatMul(std::get<2>(instruction), std::get<3>(instGenerated by Repobility's multi-pass static-analysis pipeline (https://repobility.com)
DoIO method · cpp · L1174-L1219 (46 LOC)simulator.hpp
inline void GpuSimulator::DoIO(GpuSimulator::IoInstruction instruction) {
auto matrix = std::get<2>(instruction);
switch (std::get<1>(instruction)) {
case InstructionType::kToGpuHbm: {
if (matrix->GetPosition() == Position::kInGpuHbm) {
std::cerr << "Matrix is already in GPU HBM." << std::endl;
return;
}
matrix->position_ = Position::kInGpuHbm;
shared_mem_used_ -= matrix->GetSize();
gpu_hbm_used_ += matrix->GetSize();
break;
}
case InstructionType::kToSharedMem: {
if (matrix->GetPosition() == Position::kInSharedMemory) {
std::cerr << "Matrix is already in shared memory." << std::endl;
return;
}
matrix->position_ = Position::kInSharedMemory;
gpu_hbm_used_ -= matrix->GetSize();
shared_mem_used_ += matrix->GetSize();
break;
}
case InstructionType::kTranspose:
case InstructionType::kRelease:
case InstructionType::kReshape:
case InstructionType::kMatAdd:
case InstructionType::kMatSub:
case InstrisEqual function · cpp · L1230-L1240 (11 LOC)simulator.hpp
bool isEqual(float a, float b, float absoluteEpsilon = 1e-6f,
float relativeEpsilon = 1e-5f) {
if (a == b)
return true;
float diff = std::abs(a - b);
if (diff < absoluteEpsilon)
return true;
float largest = std::max(std::abs(a), std::abs(b));
return diff <= relativeEpsilon * largest;
}CommitAnswer method · cpp · L1241-L1269 (29 LOC)simulator.hpp
void Rater::CommitAnswer(Matrix &answer) {
if (next_query_available_) {
std::cerr << "You must call GetNextQuery() before committing an answer.";
exit(EXIT_FAILURE);
}
assert(current_query_index_ < static_cast<int>(answers_.size()));
if (answer.GetPosition() != Position::kInGpuHbm) {
errors_.push_back(answers_[current_query_index_]->GetSize());
std::cerr << "Answer matrix must be in GPU HBM." << std::endl;
} else if (answer.GetColumnNum() !=
answers_[current_query_index_]->GetColumnNum() ||
answer.GetRowNum() !=
answers_[current_query_index_]->GetRowNum()) {
errors_.push_back(answers_[current_query_index_]->GetSize());
std::cerr << "Answer matrix size mismatch." << std::endl;
} else {
int error_count = 0;
for (size_t i = 0; i < answer.GetSize(); ++i) {
if (!isEqual(answer.data_[i], answers_[current_query_index_]->data_[i])) {
error_count++;
}
}
errors_.push_back(errorpage 1 / 2next ›