-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
7 changed files
with
159 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "../exercise.h" | ||
|
||
// READ: 模板非类型实参 <https://zh.cppreference.com/w/cpp/language/template_parameters#%E6%A8%A1%E6%9D%BF%E9%9D%9E%E7%B1%BB%E5%9E%8B%E5%AE%9E%E5%8F%82> | ||
|
||
template<unsigned int N, class T> | ||
struct Tensor { | ||
unsigned int shape[N]; | ||
T *data; | ||
|
||
Tensor(unsigned int const shape_[N]) { | ||
unsigned int size = 1; | ||
// TODO: 填入正确的 shape 并计算 size | ||
data = new T[size]; | ||
std::memset(data, 0, size * sizeof(T)); | ||
} | ||
~Tensor() { | ||
delete[] data; | ||
} | ||
|
||
// 为了保持简单,禁止复制和移动 | ||
Tensor(Tensor const &) = delete; | ||
Tensor(Tensor &&) noexcept = delete; | ||
|
||
T &operator[](unsigned int const indices[N]) { | ||
return data[data_index(indices)]; | ||
} | ||
T const &operator[](unsigned int const indices[N]) const { | ||
return data[data_index(indices)]; | ||
} | ||
|
||
private: | ||
unsigned int data_index(unsigned int const indices[N]) const { | ||
unsigned int index = 0; | ||
for (unsigned int i = 0; i < N; ++i) { | ||
ASSERT(indices[i] < shape[i]); | ||
// TODO: 计算 index | ||
} | ||
} | ||
}; | ||
|
||
// ---- 不要修改以下代码 ---- | ||
int main(int argc, char **argv) { | ||
{ | ||
unsigned int shape[]{2, 3, 4, 5}; | ||
auto tensor = Tensor<4, int>(shape); | ||
|
||
unsigned int i0[]{0, 0, 0, 0}; | ||
tensor[i0] = 1; | ||
ASSERT(tensor[i0] == 1, "tensor[i0] should be 1"); | ||
|
||
unsigned int i1[]{1, 2, 3, 4}; | ||
tensor[i0] = 2; | ||
ASSERT(tensor[i0] == 2, "tensor[i1] should be 2"); | ||
} | ||
{ | ||
unsigned int shape[]{7, 8, 128}; | ||
auto tensor = Tensor<3, float>(shape); | ||
|
||
unsigned int i0[]{0, 0, 0}; | ||
tensor[i0] = 1.f; | ||
ASSERT(tensor[i0] == 1.f, "tensor[i0] should be 1"); | ||
|
||
unsigned int i1[]{3, 4, 99}; | ||
tensor[i0] = 2.f; | ||
ASSERT(tensor[i0] == 2.f, "tensor[i1] should be 2"); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "../exercise.h" | ||
#include <array> | ||
#include <cstring> | ||
|
||
// READ: std::array <https://zh.cppreference.com/w/cpp/container/array> | ||
|
||
// TODO: 将下列 `?` 替换为正确的代码 | ||
int main(int argc, char **argv) { | ||
{ | ||
std::array<int, 5> arr{{1, 2, 3, 4, 5}}; | ||
ASSERT(arr.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(arr) == ?, "Fill in the correct value."); | ||
int ans[]{1, 2, 3, 4, 5}; | ||
ASSERT(std::memcmp(arr.?, ans, ?) == 0, "Fill in the correct values."); | ||
} | ||
{ | ||
std::array<double, 8> arr; | ||
ASSERT(arr.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(arr) == ?, "Fill in the correct value."); | ||
} | ||
{ | ||
std::array<char, 21> arr{"Hello, InfiniTensor!"}; | ||
ASSERT(arr.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(arr) == ?, "Fill in the correct value."); | ||
ASSERT(std::strcmp(arr.?, "Hello, InfiniTensor!") == 0, "Fill in the correct value."); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
#ifndef __TEST_H__ | ||
#define __TEST_H__ | ||
|
||
bool test_exercise(int n, const char *log); | ||
#include <filesystem> | ||
#include <mutex> | ||
#include <variant> | ||
#include <vector> | ||
|
||
struct Console {}; | ||
struct Null {}; | ||
struct Log { | ||
std::variant<Console, Null, std::filesystem::path> dst; | ||
std::vector<bool> result; | ||
std::mutex mutex; | ||
Log &operator<<(unsigned int n); | ||
}; | ||
|
||
#endif// __TEST_H__ |