-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelloworld.cu
More file actions
95 lines (75 loc) · 2.96 KB
/
helloworld.cu
File metadata and controls
95 lines (75 loc) · 2.96 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
95
/**
* @file helloworld.cu
* @brief Hello world example
* @author Song Liu (song.liu@bristol.ac.uk)
*
* This file contains all essential matrix operations.
* Whatever you do, please keep it as simple as possible.
*
Copyright (C) 2022 Song Liu (song.liu@bristol.ac.uk)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <fstream>
#include <thread>
#include "../cpp/juzhen.hpp"
#include "../ml/plotting.hpp"
// overriding the default printing function for matrices
std::ostream& operator<<(std::ostream& os, const M& M) {
using namespace std;
// write obj to stream
for (size_t i = 0; i < M.num_row(); i++) {
os << endl;
for (size_t j = 0; j < M.num_col(); j++) {
os << M.elem(i, j) << " ";
}
}
return os;
}
#define HLINE std::cout << "--------------------------------" << std::endl
int compute() {
// spdlog::set_level(spdlog::level::debug);
global_rand_gen.seed(0);
#ifdef CUDA
GPUSampler sampler(1);
#endif
{
std::cout << "The following code demonstrate how to use the Juzhen library." << std::endl;
std::cout << "all compuitations run on the GPU." << std::endl;
M A("A", {{1, 2, 3}, {4, 5, 6}});
std::cout << "A = " << A << std::endl;
M B("B", {{4, 5, 6}, {7, 8, 9}});
std::cout << "B = " << B << std::endl;
auto AB = vstack<float>({A, B});
std::cout << "AB = [A; B] = " << AB << std::endl;
HLINE;
std::cout << "A + B = " << A + B << std::endl;
std::cout << "A - B = " << A - B << std::endl;
std::cout << "A + 1 = " << A + 1 << std::endl;
std::cout << "A - 1 = " << A - 1 << std::endl;
std::cout << "A / 1.5 = " << A / 1.5 << std::endl;
std::cout << "A / B = " << A / B << std::endl;
try {
std::cout << "A / B^T = " << A / B.T() << std::endl;
} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
HLINE;
try {
std::cout << "A * B = " << A * B << std::endl;
} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
HLINE;
std::cout << "A * B^T = " << A * B.T() << std::endl;
}
return 0;
}