Убрал поддиректорию
This commit is contained in:
47
src/csv_loader.cpp
Normal file
47
src/csv_loader.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "csv_loader.hpp"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
std::vector<Record> load_csv(const std::string& filename) {
|
||||
std::vector<Record> data;
|
||||
std::ifstream file(filename);
|
||||
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("Cannot open file: " + filename);
|
||||
}
|
||||
|
||||
std::string line;
|
||||
|
||||
// читаем первую строку (заголовок)
|
||||
std::getline(file, line);
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
std::stringstream ss(line);
|
||||
std::string item;
|
||||
|
||||
Record row;
|
||||
|
||||
std::getline(ss, item, ',');
|
||||
row.timestamp = std::stod(item);
|
||||
|
||||
std::getline(ss, item, ',');
|
||||
row.open = std::stod(item);
|
||||
|
||||
std::getline(ss, item, ',');
|
||||
row.high = std::stod(item);
|
||||
|
||||
std::getline(ss, item, ',');
|
||||
row.low = std::stod(item);
|
||||
|
||||
std::getline(ss, item, ',');
|
||||
row.close = std::stod(item);
|
||||
|
||||
std::getline(ss, item, ',');
|
||||
row.volume = std::stod(item);
|
||||
|
||||
data.push_back(row);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
6
src/csv_loader.hpp
Normal file
6
src/csv_loader.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "record.hpp"
|
||||
|
||||
std::vector<Record> load_csv(const std::string& filename);
|
||||
12
src/gpu_loader.cpp
Normal file
12
src/gpu_loader.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "gpu_loader.hpp"
|
||||
#include <dlfcn.h>
|
||||
|
||||
gpu_is_available_fn load_gpu_is_available() {
|
||||
void* h = dlopen("./libgpu_compute.so", RTLD_NOW | RTLD_LOCAL);
|
||||
if (!h) return nullptr;
|
||||
|
||||
auto fn = (gpu_is_available_fn)dlsym(h, "gpu_is_available");
|
||||
if (!fn) return nullptr;
|
||||
|
||||
return fn;
|
||||
}
|
||||
4
src/gpu_loader.hpp
Normal file
4
src/gpu_loader.hpp
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
using gpu_is_available_fn = int (*)();
|
||||
|
||||
gpu_is_available_fn load_gpu_is_available();
|
||||
8
src/gpu_plugin.cu
Normal file
8
src/gpu_plugin.cu
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
extern "C" int gpu_is_available() {
|
||||
int n = 0;
|
||||
cudaError_t err = cudaGetDeviceCount(&n);
|
||||
if (err != cudaSuccess) return 0;
|
||||
return (n > 0) ? 1 : 0;
|
||||
}
|
||||
87
src/main.cpp
Normal file
87
src/main.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <mpi.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "csv_loader.hpp"
|
||||
#include "utils.hpp"
|
||||
#include "record.hpp"
|
||||
#include "gpu_loader.hpp"
|
||||
|
||||
// Функция: отобрать записи для конкретного ранга
|
||||
std::vector<Record> select_records_for_rank(
|
||||
const std::map<long long, std::vector<Record>>& days,
|
||||
const std::vector<long long>& day_list)
|
||||
{
|
||||
std::vector<Record> out;
|
||||
for (auto d : day_list) {
|
||||
auto it = days.find(d);
|
||||
if (it != days.end()) {
|
||||
const auto& vec = it->second;
|
||||
out.insert(out.end(), vec.begin(), vec.end());
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
MPI_Init(&argc, &argv);
|
||||
|
||||
int rank, size;
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
||||
|
||||
std::vector<Record> local_records;
|
||||
|
||||
if (rank == 0) {
|
||||
std::cout << "Rank 0 loading CSV..." << std::endl;
|
||||
|
||||
// Запускаем из build
|
||||
auto records = load_csv("../data/data.csv");
|
||||
|
||||
auto days = group_by_day(records);
|
||||
auto parts = split_days(days, size);
|
||||
|
||||
// Рассылаем данные
|
||||
for (int r = 0; r < size; r++) {
|
||||
auto vec = select_records_for_rank(days, parts[r]);
|
||||
|
||||
if (r == 0) {
|
||||
// себе не отправляем — сразу сохраняем
|
||||
local_records = vec;
|
||||
continue;
|
||||
}
|
||||
|
||||
int count = vec.size();
|
||||
MPI_Send(&count, 1, MPI_INT, r, 0, MPI_COMM_WORLD);
|
||||
MPI_Send(vec.data(), count * sizeof(Record), MPI_BYTE, r, 1, MPI_COMM_WORLD);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Принимает данные
|
||||
int count = 0;
|
||||
MPI_Recv(&count, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
||||
|
||||
local_records.resize(count);
|
||||
MPI_Recv(local_records.data(), count * sizeof(Record),
|
||||
MPI_BYTE, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
||||
}
|
||||
|
||||
MPI_Barrier(MPI_COMM_WORLD);
|
||||
|
||||
std::cout << "Rank " << rank << " received "
|
||||
<< local_records.size() << " records" << std::endl;
|
||||
|
||||
|
||||
auto gpu_is_available = load_gpu_is_available();
|
||||
int have_gpu = 0;
|
||||
if (gpu_is_available) {
|
||||
std::cout << "Rank " << rank << " dll loaded" << std::endl;
|
||||
have_gpu = gpu_is_available();
|
||||
}
|
||||
|
||||
std::cout << "Rank " << rank << ": gpu_available=" << have_gpu << "\n";
|
||||
|
||||
MPI_Finalize();
|
||||
return 0;
|
||||
}
|
||||
11
src/mpi_utils.cpp
Normal file
11
src/mpi_utils.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "mpi_utils.hpp"
|
||||
#include <mpi.h>
|
||||
#include <iostream>
|
||||
|
||||
void mpi_print_basic() {
|
||||
int rank, size;
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
||||
|
||||
std::cout << "Hello from rank " << rank << " of " << size << std::endl;
|
||||
}
|
||||
2
src/mpi_utils.hpp
Normal file
2
src/mpi_utils.hpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
void mpi_print_basic();
|
||||
11
src/record.hpp
Normal file
11
src/record.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
struct Record {
|
||||
double timestamp;
|
||||
double open;
|
||||
double high;
|
||||
double low;
|
||||
double close;
|
||||
double volume;
|
||||
};
|
||||
25
src/utils.cpp
Normal file
25
src/utils.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "utils.hpp"
|
||||
|
||||
std::map<DayIndex, std::vector<Record>> group_by_day(const std::vector<Record>& recs) {
|
||||
std::map<DayIndex, std::vector<Record>> days;
|
||||
|
||||
for (const auto& r : recs) {
|
||||
DayIndex day = static_cast<DayIndex>(r.timestamp) / 86400;
|
||||
days[day].push_back(r);
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
std::vector<std::vector<DayIndex>> split_days(const std::map<DayIndex, std::vector<Record>>& days, int parts) {
|
||||
std::vector<std::vector<DayIndex>> out(parts);
|
||||
|
||||
int i = 0;
|
||||
for (auto& kv : days) {
|
||||
out[i % parts].push_back(kv.first);
|
||||
i++;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
11
src/utils.hpp
Normal file
11
src/utils.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "record.hpp"
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
using DayIndex = long long;
|
||||
|
||||
std::map<DayIndex, std::vector<Record>> group_by_day(const std::vector<Record>& recs);
|
||||
std::vector<std::vector<DayIndex>> split_days(const std::map<DayIndex, std::vector<Record>>& days, int parts);
|
||||
|
||||
Reference in New Issue
Block a user