fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stddef.h>
  4.  
  5. class tensor
  6. {
  7. std::vector<int> data;
  8. size_t depth;
  9. size_t height;
  10. size_t width;
  11.  
  12. public:
  13.  
  14. tensor(size_t depth, size_t height, size_t width)
  15. : data(depth * height * width), depth(depth), height(height), width(width) {}
  16.  
  17. int& operator()(int z, int y, int x)
  18. {
  19. return data[(z * height + y) * width + x];
  20. }
  21.  
  22. int operator()(int z, int y, int x) const
  23. {
  24. return data[(z * height + y) * width + x];
  25. }
  26.  
  27. int* raw_pointer_for_gpu()
  28. {
  29. return &data[0];
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. tensor test(2, 3, 5);
  36. std::cout << "Should be contiguous:\n";
  37. for (int z = 0; z < 2; ++z)
  38. {
  39. for (int y = 0; y < 3; ++y)
  40. {
  41. for (int x = 0; x < 5; ++x)
  42. {
  43. std::cout << &test(z, y, x) << '\n';
  44. }
  45. }
  46. }
  47. }
  48.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Should be contiguous:
0x8c80008
0x8c8000c
0x8c80010
0x8c80014
0x8c80018
0x8c8001c
0x8c80020
0x8c80024
0x8c80028
0x8c8002c
0x8c80030
0x8c80034
0x8c80038
0x8c8003c
0x8c80040
0x8c80044
0x8c80048
0x8c8004c
0x8c80050
0x8c80054
0x8c80058
0x8c8005c
0x8c80060
0x8c80064
0x8c80068
0x8c8006c
0x8c80070
0x8c80074
0x8c80078
0x8c8007c