6 #include <cuda_runtime.h>
18 std::ifstream file(filename);
19 if (!file.is_open()) {
20 throw std::runtime_error(
"Error opening file");
24 if (startRow < 0 || endRow > fileRows || startRow >= endRow) {
25 throw std::runtime_error(
"Invalid row range specified");
29 if (rows != endRow - startRow || cols != fileCols) {
30 throw std::runtime_error(
"Matrix dimensions do not match the specified range and file columns");
34 std::vector<double> temp_data(rows * cols);
35 std::string line, value;
39 while (std::getline(file, line) && currentRow < fileRows) {
41 if (currentRow >= startRow && currentRow < endRow) {
42 std::istringstream s(line);
43 for (
int col = 0; col < fileCols; ++col) {
45 if (!std::getline(s, value,
',')) {
46 throw std::runtime_error(
"Insufficient columns in CSV file");
49 temp_data[(currentRow - startRow) * cols + col] = std::stod(value);
56 if (currentRow < endRow) {
57 throw std::runtime_error(
"Insufficient rows in CSV file");
61 cudaMemcpy(d_data, temp_data.data(), rows * cols *
sizeof(
double), cudaMemcpyHostToDevice);
void read_csv_limited(const char *filename, int startRow, int endRow, int fileRows, int fileCols)
Read a subset of data from a CSV file into the matrix.
Defines the Matrix class for GPU-accelerated matrix operations.