-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathpytaco.cpp
More file actions
162 lines (119 loc) · 4.28 KB
/
pytaco.cpp
File metadata and controls
162 lines (119 loc) · 4.28 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <Python.h>
#include <pybind11/pybind11.h>
#include "pyFormat.h"
#include "pyDatatypes.h"
#include "pyIndexNotation.h"
#include "pyTensor.h"
#include "pyTensorIO.h"
#include "pyParsers.h"
void addHelpers(py::module &m) {
m.def("unique_name", (std::string(*)(char)) &taco::util::uniqueName);
m.def("should_use_cuda_codegen", &taco::should_use_CUDA_codegen);
py::options options;
options.disable_function_signatures();
m.def("get_num_threads", &taco::taco_get_num_threads, R"(
get_num_threads()
Get the number of threads taco uses for computation.
Taco defaults to using one thread to perform computations.
The number of threads can be increased using :func:`~set_num_threads` to perform computations in parallel. The
user is encouraged to tune the number of threads in order to increase performance.
Examples
---------
>>> import pytaco as pt
>>> pt.set_num_threads(1)
>>> pt.get_num_threads()
1
Returns
--------
number_of_threads: int
The number of threads taco uses for performing tensor computations.
)");
m.def("set_num_threads", &taco::taco_set_num_threads, py::arg("num_threads"), R"(
set_num_threads(num_threads)
Set the number of threads taco should use to perform computations.
The number of threads taco should use when running operations that can be done in parallel.
Parameters
------------
num_threads: int
The number of threads taco should use when performing computations. The number of threads must be positive. Taco
will ignore attempting to set the number of threads to a number less than or equal to 0.
Notes
------
Attempting to parallelize some expressions might lead to incorrect behaviour. In this case, taco will use one thread if
it determines an expression not parallelizable.
Examples
----------
>>> import pytaco as pt
>>> pt.set_num_threads(4) # tell taco to use 4 threads
>>> pt.get_num_threads()
4
>>> pt.set_num_threads(0) # ignored
>>> pt.get_num_threads() # Will be 4 since the last set was ignored
4
)");
m.def("set_parallel_schedule", [](std::string sched_type, int chunk_size){
std::transform(sched_type.begin(), sched_type.end(), sched_type.begin(), ::tolower);
if(sched_type == "static") {
taco::taco_set_parallel_schedule(taco::ParallelSchedule::Static, chunk_size);
} else if (sched_type == "dynamic") {
taco::taco_set_parallel_schedule(taco::ParallelSchedule::Dynamic, chunk_size);
} else {
py::value_error(R"(Schedule can only be "static" or "dynamic")");
}
}, R"(
set_parallel_schedule(sched_type, chunk_size)
Sets the strategy for performing computations in parallel.
Parameters
-----------
sched_type: string
Either "static" or "dynamic". "static" indicates that Taco should parallelize
subsequent computations using a strategy that assigns the same number of
coordinates along a particular dimension to be processed by each thread.
"dynamic" indicates that Taco should parallelize subsequent computations
using a strategy that assigns work to the threads at runtime for better
load balance.
chunk_size: int
For a dynamic schedule, the amount of additional work that is assigned to
any idle thread.
Notes
-------
Examples
---------
)", py::arg("sched_type"), py::arg("chunk_size") = 1);
m.def("get_parallel_schedule", [](){
taco::ParallelSchedule sched;
int chunk_size = 0;
taco::taco_get_parallel_schedule(&sched, &chunk_size);
if(sched == taco::ParallelSchedule::Static) {
return py::make_tuple("static", chunk_size);
} else {
return py::make_tuple("dynamic", chunk_size);
}
}, R"(
get_parallel_schedule()
Gets the current strategy for performing computations in parallel.
Examples
---------
Notes
-------
Returns
--------
schedule: tuple (string, int)
A tuple where the first element indicates the strategy currently being used
to perform computations in parallel (either "static" or "dynamic") and the
second element is the chunk size used for parallel computation.
)");
}
PYBIND11_MODULE(core_modules, m){
m.doc() = "A Python module for operating on Sparse Tensors.";
using namespace taco::pythonBindings;
addHelpers(m);
defineTacoTypes(m);
defineModeFormats(m);
defineModeFormatPack(m);
defineFormat(m);
defineIndexNotation(m);
defineTensor(m);
defineIOFuncs(m);
defineParser(m);
}