You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "../checker.h"
  2. #include <cstring>
  3. // READ: 类模板 <https://zh.cppreference.com/w/cpp/language/class_template>
  4. template<class T>
  5. struct Tensor4D {
  6. unsigned int shape[4];
  7. T *data;
  8. Tensor4D(unsigned int const shape_[4], T const *data_) {
  9. unsigned int size = 1;
  10. // TODO: 填入正确的 shape 并计算 size
  11. data = new T[size];
  12. std::memcpy(data, data_, size * sizeof(T));
  13. }
  14. ~Tensor4D() {
  15. delete[] data;
  16. }
  17. // 为了保持简单,禁止复制和移动
  18. Tensor4D(Tensor4D const &) = delete;
  19. Tensor4D(Tensor4D &&) noexcept = delete;
  20. // 这个加法需要支持“单向广播”。
  21. // 具体来说,`others` 可以具有与 `this` 不同的形状,形状不同的维度长度必须为 1。
  22. // `others` 长度为 1 但 `this` 长度不为 1 的维度将发生广播计算。
  23. // 例如,`this` 形状为 `[1, 2, 3, 4]`,`others` 形状为 `[1, 2, 1, 4]`,
  24. // 则 `this` 与 `others` 相加时,3 个形状为 `[1, 2, 1, 4]` 的子张量各自与 `others` 对应项相加。
  25. Tensor4D &operator+=(Tensor4D const &others) {
  26. // TODO: 实现单向广播的加法
  27. // check whether a dimension needs broadcast
  28. return *this;
  29. }
  30. };
  31. // ---- 不要修改以下代码 ----
  32. int main(int argc, char **argv) {
  33. {
  34. unsigned int shape[]{1, 2, 3, 4};
  35. // clang-format off
  36. int data[]{
  37. 1, 2, 3, 4,
  38. 5, 6, 7, 8,
  39. 9, 10, 11, 12,
  40. 13, 14, 15, 16,
  41. 17, 18, 19, 20,
  42. 21, 22, 23, 24};
  43. // clang-format on
  44. auto t0 = Tensor4D(shape, data);
  45. auto t1 = Tensor4D(shape, data);
  46. t0 += t1;
  47. for (auto i = 0u; i < sizeof(data) / sizeof(*data); ++i) {
  48. ASSERT(t0.data[i] == data[i] * 2, "Tensor doubled by plus its self.");
  49. }
  50. }
  51. {
  52. unsigned int s0[]{1, 2, 3, 4};
  53. // clang-format off
  54. float d0[]{
  55. 1, 1, 1, 1,
  56. 2, 2, 2, 2,
  57. 3, 3, 3, 3,
  58. 4, 4, 4, 4,
  59. 5, 5, 5, 5,
  60. 6, 6, 6, 6};
  61. // clang-format on
  62. unsigned int s1[]{1, 2, 3, 1};
  63. // clang-format off
  64. float d1[]{
  65. 6,
  66. 5,
  67. 4,
  68. 3,
  69. 2,
  70. 1};
  71. // clang-format on
  72. auto t0 = Tensor4D(s0, d0);
  73. auto t1 = Tensor4D(s1, d1);
  74. t0 += t1;
  75. for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) {
  76. ASSERT(t0.data[i] == 7.f, "Every element of t0 should be 7 after adding t1 to it.");
  77. }
  78. }
  79. {
  80. unsigned int s0[]{1, 2, 3, 4};
  81. // clang-format off
  82. double d0[]{
  83. 1, 2, 3, 4,
  84. 5, 6, 7, 8,
  85. 9, 10, 11, 12,
  86. 13, 14, 15, 16,
  87. 17, 18, 19, 20,
  88. 21, 22, 23, 24};
  89. // clang-format on
  90. unsigned int s1[]{1, 1, 1, 1};
  91. double d1[]{1};
  92. auto t0 = Tensor4D(s0, d0);
  93. auto t1 = Tensor4D(s1, d1);
  94. t0 += t1;
  95. for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) {
  96. ASSERT(t0.data[i] == d0[i] + 1, "Every element of t0 should be incremented by 1 after adding t1 to it.");
  97. }
  98. }
  99. }

计算机大作业