-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cu
133 lines (109 loc) · 2.56 KB
/
utils.cu
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef UTILS_CU
#define UTILS_CU
#include <stdlib.h>
#include <cuda.h>
#include <assert.h>
#include <cuda_runtime_api.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "utils.cuh"
/*
Definition of methods in the hostCUDAVariable class.
*/
template <typename T>
hostCUDAVariable<T>::hostCUDAVariable(const size_t size, const bool useGPU) : size_(size), useGPU_(useGPU)
{
/*
Constructor : Allocation of memory on host and device, as required.
*/
x_ = (T*)malloc(size_ * sizeof(T));
if(useGPU_)
{
assert(cudaSuccess == cudaMalloc((void**) &xd_, size_ * sizeof(T)));
}
}
template <typename T>
void hostCUDAVariable<T>::copyToDevice()
{
/*
Transfer of data from host to device.
*/
assert(cudaSuccess == cudaMemcpy(xd_, x_, size_ * sizeof(T), cudaMemcpyHostToDevice));
}
template <typename T>
void hostCUDAVariable<T>::copyToHost()
{
/*
Transfer of data from device to host.
*/
assert(cudaSuccess == cudaMemcpy(x_, xd_, size_ * sizeof(T), cudaMemcpyDeviceToHost));
}
template <typename T>
T*& hostCUDAVariable<T>::getDeviceVariable()
{
/*
Fetch the variable from device.
*/
return xd_;
}
template <typename T>
T*& hostCUDAVariable<T>::getHostVariable()
{
/*
Fetch of data from host.
*/
return x_;
}
template <typename T>
hostCUDAVariable<T>::~hostCUDAVariable()
{
/*
Destructor : De-allocation of memory on host and device, as required.
*/
if(useGPU_)
{
cudaFree(xd_);
}
free(x_);
}
/*
Definition of methods in the Solver class.
*/
template<typename T>
Solver<T>::Solver(const size_t size, const bool useGPU) : A_(size * size, useGPU), b_(size, useGPU),
x_current_(size, useGPU), x_next_(size, useGPU),
resolution_(size)
{
/*
Constructor : Get solver parameters and data structures.
*/
}
template<typename T>
T*& Solver<T>::solve()
{
/*
Pure Virtual Function : To be implemented in derived classes.
*/
}
std::vector<int> read_file()
{
/*
Method to read resolutions.
*/
std::string filename = "resolutions.txt";
std::vector<int> resolutions;
std::string size;
std::fstream file(filename, std::ios::in);
if(file.is_open())
{
while(std::getline(file, size))
{
resolutions.push_back(stoi(size));
}
}
return resolutions;
}
#endif