-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.cpp
More file actions
94 lines (79 loc) · 2.57 KB
/
cpu.cpp
File metadata and controls
94 lines (79 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <cmath>
#include <cuda_runtime.h>
#define _CHUNK_SIZE 5 * 1024 * 1024
#define NUM_STREAMS 1
class FH {
private:
std::ifstream _file;
const uint _chunksize = _CHUNK_SIZE;
public:
FH(const std::string &filename) {
_file = std::ifstream(filename);
char* buffer = new char[_CHUNK_SIZE];
_file.rdbuf()->pubsetbuf(buffer, _CHUNK_SIZE);
}
bool eof() {
return _file.eof();
}
std::vector<float> parse_line_of_floats(const std::string& line) {
std::vector<float> numbers;
const char* str = line.c_str();
char* end = NULL;
while (true) {
float num = std::strtof(str, &end);
// is this safe?
if ( end == str) break;
numbers.push_back(num);
str = end;
}
return numbers;
}
std::vector<std::vector<float> > readDataFromFile() {
std::vector<std::vector<float> > data;
std::string line;
float value;
while (data.size() < _chunksize) {
if (!(std::getline(_file, line))) break;
std::vector<float> numbers = parse_line_of_floats(line);
data.push_back(numbers);
}
return data;
}
};
// Main function
int main(int argc, char *argv[]) {
// Check for correct argument count
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <file1> <file2> <output_file>" << std::endl;
return 1;
}
// Read data from files
FH host_A_file(argv[1]);
FH host_B_file(argv[2]);
std::ofstream outputFile(argv[3]);
char* buffer = new char[_CHUNK_SIZE];
outputFile.rdbuf()->pubsetbuf(buffer, _CHUNK_SIZE);
while (!host_A_file.eof()) {
std::vector<std::vector<float> > host_A = host_A_file.readDataFromFile();
std::vector<std::vector<float> > host_B = host_B_file.readDataFromFile();
for (int i = 0; i < host_A.size(); i++) {
float sum = 0;
for (int j = 0; j < host_A[i].size(); j++) {
sum += host_A[i][j];
}
for (int j = 0; j < host_B[i].size(); j++) {
sum += host_B[i][j];
}
// float value = host_A[i] + host_B[i];
// float temp = host_A[i] * std::exp(-host_B[i] / host_A[i]);
// float value = std::sin(host_A[i]) * std::cos(host_B[i]) + temp;
// Store the result
// C [i]= result;
outputFile << std::fixed << std::setprecision(6) << sum << "\n";
}
}
}