SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
ref_less_op.cpp
1 #include "smaug/core/backend.h"
3 #include "smaug/operators/less_op.h"
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
13 void ref_less(float* input0, float* input1, bool* results, int input_size) {
14  dmaLoad(input0, input0, input_size * sizeof(float));
15  dmaLoad(input1, input1, input_size * sizeof(float));
16  less_loop:
17  for (int i = 0; i < input_size; i++) {
18  results[i] = input0[i] < input1[i];
19  }
20  dmaStore(results, results, input_size * sizeof(bool));
21 }
22 
27 void ref_less_equal(float* input0,
28  float* input1,
29  bool* results,
30  int input_size) {
31  dmaLoad(input0, input0, input_size * sizeof(float));
32  dmaLoad(input1, input1, input_size * sizeof(float));
33  less_equal_loop:
34  for (int i = 0; i < input_size; i++) {
35  results[i] = input0[i] <= input1[i];
36  }
37  dmaStore(results, results, input_size * sizeof(bool));
38 }
39 
40 #ifdef __cplusplus
41 }
42 #endif
43 
44 namespace smaug {
45 
46 template <>
47 void LessOp<ReferenceBackend>::run() {
48  auto input0 = getInput(Input0);
49  auto input1 = getInput(Input1);
50  auto output = getOutput(Outputs);
51  const TensorShape& input0Shape = input0->getShape();
52  const TensorShape& input1Shape = input1->getShape();
53  const TensorShape& outputShape = output->getShape();
54  assert(input0Shape == input1Shape && input0Shape == outputShape);
55 
56  float* input0Data = input0->data<float>();
57  float* input1Data = input1->data<float>();
58  bool* outputData = output->data<bool>();
59  mapArrayToAccel(ref::kEltwiseOpHw, "input0", input0Data,
60  input0Shape.storageSize() * sizeof(float));
61  mapArrayToAccel(ref::kEltwiseOpHw, "input1", input1Data,
62  input1Shape.storageSize() * sizeof(float));
63  mapArrayToAccel(ref::kEltwiseOpHw, "results", outputData,
64  outputShape.storageSize() * sizeof(bool));
65  invokeKernel(ref::kEltwiseOpHw, ref_less, input0Data, input1Data,
66  outputData, input0Shape.size());
67 }
68 
69 template <>
70 void LessEqualOp<ReferenceBackend>::run() {
71  auto input0 = getInput(Input0);
72  auto input1 = getInput(Input1);
73  auto output = getOutput(Outputs);
74  const TensorShape& input0Shape = input0->getShape();
75  const TensorShape& input1Shape = input1->getShape();
76  const TensorShape& outputShape = output->getShape();
77  assert(input0Shape == input1Shape && input0Shape == outputShape);
78 
79  float* input0Data = input0->data<float>();
80  float* input1Data = input1->data<float>();
81  bool* outputData = output->data<bool>();
82  mapArrayToAccel(ref::kEltwiseOpHw, "input0", input0Data,
83  input0Shape.storageSize() * sizeof(float));
84  mapArrayToAccel(ref::kEltwiseOpHw, "input1", input1Data,
85  input1Shape.storageSize() * sizeof(float));
86  mapArrayToAccel(ref::kEltwiseOpHw, "results", outputData,
87  outputShape.storageSize() * sizeof(bool));
88  invokeKernel(ref::kEltwiseOpHw, ref_less_equal, input0Data, input1Data,
89  outputData, input0Shape.size());
90 }
91 
92 } // namespace smaug
93 
ref_less_equal
void ref_less_equal(float *input0, float *input1, bool *results, int input_size)
Definition: ref_less_op.cpp:27
ref_less
void ref_less(float *input0, float *input1, bool *results, int input_size)
Definition: ref_less_op.cpp:13
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
common.h
Utilities for writing and invoking Aladdin kernels from Operators.
smaug::mapArrayToAccel
void mapArrayToAccel(unsigned reqCode, const char *arrayName, void *baseAddr, size_t size)
Maps an array of data to the accelerator.
Definition: common.cpp:12
smaug::invokeKernel
void invokeKernel(int accelIdx, unsigned reqCode, const Kernel &kernel, Args &&... args)
The generic blocking interface for all accelerator kernel functions.
Definition: common.h:72