CUDA Networks
matrix_read_csv.cu
Go to the documentation of this file.
1 /**
2  * @file matrix_read_csv.cu
3  * @brief Implementation of the Matrix::read_csv method.
4  */
5 #include "matrix.h"
6 #include <cuda_runtime.h>
7 #include <fstream>
8 #include <sstream>
9 #include <stdexcept>
10 #include <vector>
11 
12 void Matrix::read_csv(const char* filename) {
13  // Open the CSV file
14  std::ifstream file(filename);
15  if (!file.is_open()) {
16  throw std::runtime_error("Error opening file");
17  }
18 
19  // Vector to temporarily store the data read from the CSV
20  std::vector<double> temp_data;
21  std::string line, value;
22 
23  // Read the CSV file line by line
24  while (std::getline(file, line)) {
25  // Create a string stream from the current line
26  std::istringstream s(line);
27 
28  // Parse each value in the line, separated by commas
29  while (std::getline(s, value, ',')) {
30  // Convert the string value to double and add it to the temporary vector
31  temp_data.push_back(std::stod(value));
32  }
33  }
34 
35  // Check if the number of values read matches the matrix dimensions
36  if (temp_data.size() != rows * cols) {
37  throw std::runtime_error("CSV data size does not match matrix dimensions");
38  }
39 
40  // Copy data from the temporary vector to the device (GPU) memory
41  cudaMemcpy(d_data, temp_data.data(), rows * cols * sizeof(double), cudaMemcpyHostToDevice);
42 }
void read_csv(const char *filename)
Read data from a CSV file into the matrix.
Defines the Matrix class for GPU-accelerated matrix operations.