ZIO
Python and C++ interface to ZeroMQ and Zyre
test_tens.cpp
Go to the documentation of this file.
1 #include "zio/tens.hpp"
2 
3 #include <iostream>
4 
5 int main()
6 {
7 
8  float tensor[2][3][4] = {0};
9  const float* tensor1 = (float*) tensor;
10 
11  std::vector<size_t> shape={2,3,4};
12 
14 
15  // Add an initial, unrelated message part just to make sure tens
16  // respects it.
17  msg.add(zio::message_t((char*)nullptr,0));
18 
19  zio::tens::append(msg, tensor1, shape);
20  assert(msg.payload().size() == 2);
21 
22  assert(msg.form() == zio::tens::form);
23  auto lobj = msg.label_object();
24  std::cout << lobj << std::endl;
25  auto md = lobj[zio::tens::form]["tensors"][0];
26 
27  for (int ind=0; ind < 3; ++ind) {
28  assert(shape[ind] == md["shape"][ind].get<size_t>());
29  }
30 
31  const float* tensor2 = zio::tens::at<float>(msg, 0);
32  assert(tensor2);
33  for (size_t ind=0; ind<24; ++ind) {
34  std::cout<<ind<<" " << tensor1[ind] << " " << tensor2[ind] << std::endl;
35  assert(tensor1[ind] == tensor2[ind]);
36  }
37 
38  const zio::message_t& spmp = zio::tens::at(msg, 0);
39  assert(spmp.size());
40  assert(!spmp.empty());
41 
42  return 0;
43 }
std::string form() const
Definition: message.cpp:90
const zio::message_t & at(const Message &msg, size_t index)
Definition: tens.hpp:58
const char * form
Definition: tens.hpp:15
int main()
Definition: test_tens.cpp:5
ZMQ_NODISCARD bool empty() const ZMQ_NOTHROW
Definition: zmq.hpp:493
zio::json label_object() const
Helper, when label holds a JSON object.
Definition: message.cpp:80
void add(message_t &&spmsg)
Definition: message.hpp:112
const multipart_t & payload() const
Access payload(s)
Definition: message.hpp:110
void append(Message &msg, message_t &&data, const std::vector< size_t > &shape, size_t word_size, const char *tn)
Definition: tens.cpp:34
size_t size() const ZMQ_NOTHROW
Definition: zmq.hpp:488
a ZIO message
Definition: message.hpp:59