Skip to content

Commit

Permalink
Implemented reduce_sum & reduce_max operations for buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
m0saan committed Jun 14, 2023
1 parent 0ae0294 commit 65035b7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/cpu_backend/aligned_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class AlignedBuffer {
*/
ScalarT get_element(size_t index) const;

ScalarT* data() const;

/**
* @brief Overload the << operator for the AlignedBuffer.
*
Expand Down
Binary file added src/a.out
Binary file not shown.
20 changes: 20 additions & 0 deletions src/a.out.dSYM/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.a.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Binary file added src/a.out.dSYM/Contents/Resources/DWARF/a.out
Binary file not shown.
4 changes: 4 additions & 0 deletions src/cpu_backend/aligned_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ ScalarT AlignedBuffer::get_element(size_t index) const {
}
}

ScalarT* AlignedBuffer::data() const {
return buffer_;
}

} // namespace cpu
} // namespace minima
25 changes: 25 additions & 0 deletions src/cpu_backend/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,28 @@ void minima::cpu::scalar_ge(const AlignedBuffer& a, ScalarT val, AlignedBuffer*
checkSizeMatch(a, *out);
scalarOperation(a, val, out, [](ScalarT x, ScalarT val) { return x >= val; });
}

template <typename ReduceOp>
void Reduce(const minima::cpu::AlignedBuffer& a, minima::cpu::AlignedBuffer* out, size_t reduce_size, ReduceOp op) {
if (!out) {
throw std::invalid_argument("Output buffer is a nullptr.");
}

for(size_t i = 0; i < out->size(); ++i) {
const minima::cpu::ScalarT* start_ptr = a.data() + (i * reduce_size);
const minima::cpu::ScalarT* end_ptr = a.data() + ((i+1) * reduce_size);
out->set_element(i, op(start_ptr, end_ptr));
}
}

void minima::cpu::reduce_max(const AlignedBuffer& a, AlignedBuffer* out, size_t reduce_size) {
Reduce(a, out, reduce_size, [](const ScalarT* start, const ScalarT* end){
return *std::max_element(start, end);
});
}

void minima::cpu::reduce_sum(const AlignedBuffer& a, AlignedBuffer* out, size_t reduce_size) {
Reduce(a, out, reduce_size, [](const ScalarT* start, const ScalarT* end){
return std::accumulate(start, end, 0.0f);
});
}
5 changes: 3 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ std::ostream& operator<<(std::ostream& os, const std::vector<int>& v) {
// Path: src/main.cc
int main() {
minima::cpu::AlignedBuffer a(24);
minima::cpu::AlignedBuffer b(24);
minima::cpu::AlignedBuffer b(4);
// std::cout << a << std::endl;

// minima::cpu::fill(&a, 1.0);
Expand All @@ -36,7 +36,8 @@ int main() {
// std::vector<int> shape = {2,3};

// std::vector<int> indices(shape.size(), 0); // All indices start at 0
minima::cpu::scalar_power(a, 2, &b);
// minima::cpu::scalar_power(a, 2, &b);
minima::cpu::reduce_sum(a, &b, 6);

std::cout << b << std::endl;

Expand Down

0 comments on commit 65035b7

Please sign in to comment.