1 /**
2 * Copyright (C) Mellanox Technologies Ltd. 2018.  ALL RIGHTS RESERVED.
3 * See file LICENSE for terms.
4 */
5 
6 #ifndef TEST_UCP_DATATYPE_H_
7 #define TEST_UCP_DATATYPE_H_
8 
9 #include <common/gtest.h>
10 
11 #include <ucp/api/ucp.h>
12 extern "C" {
13 #include <ucp/dt/dt_contig.h>
14 #include <ucp/dt/dt_generic.h>
15 #include <ucp/dt/dt_iov.h>
16 }
17 
18 #include <string.h>
19 
20 namespace ucp {
21 
22 /* Can't be destroyed before related UCP request is completed */
23 class data_type_desc_t {
24 public:
25     enum {
26         MAX_IOV = 40
27     };
28 
data_type_desc_t()29     data_type_desc_t()
30         : m_origin(uintptr_t(NULL)), m_length(0), m_dt(0), m_buf(NULL),
31           m_count(0), m_iov_cnt_limit(sizeof(m_iov) / sizeof(m_iov[0])) {
32         memset(m_iov, 0, sizeof(m_iov));
33     };
34 
data_type_desc_t(ucp_datatype_t datatype,const void * buf,size_t length)35     data_type_desc_t(ucp_datatype_t datatype, const void *buf, size_t length)
36         : m_origin(uintptr_t(buf)), m_length(length), m_dt(0), m_buf(NULL),
37           m_iov_cnt_limit(sizeof(m_iov) / sizeof(m_iov[0])) {
38         make(datatype, buf, length);
39     }
40 
data_type_desc_t(ucp_datatype_t datatype,const void * buf,size_t length,size_t iov_count)41     data_type_desc_t(ucp_datatype_t datatype, const void *buf, size_t length,
42                      size_t iov_count)
43         : m_origin(uintptr_t(buf)), m_length(length), m_dt(0), m_buf(NULL),
44           m_iov_cnt_limit(sizeof(m_iov) / sizeof(m_iov[0])) {
45         make(datatype, buf, length, iov_count);
46     };
47 
make(ucp_datatype_t datatype,const void * buf,size_t length)48     data_type_desc_t &make(ucp_datatype_t datatype, const void *buf,
49                            size_t length) {
50         return make(datatype, buf, length, m_iov_cnt_limit);
51     };
52 
forward_to(size_t offset)53     data_type_desc_t &forward_to(size_t offset) {
54         EXPECT_LE(offset, m_length);
55         invalidate();
56         return make(m_dt, (const void *)(m_origin + offset), m_length - offset,
57                     m_iov_cnt_limit);
58     };
59 
dt()60     ucp_datatype_t dt() const {
61         EXPECT_TRUE(is_valid());
62         return m_dt;
63     };
64 
buf()65     void *buf() const {
66         EXPECT_TRUE(is_valid());
67         return const_cast<void *>(m_buf);
68     };
69 
buf_length()70     ssize_t buf_length() const {
71         EXPECT_TRUE(is_valid());
72         if (UCP_DT_IS_CONTIG(m_dt) || UCP_DT_IS_GENERIC(m_dt)) {
73             return m_length - (uintptr_t(m_buf) - m_origin);
74         } else if (UCP_DT_IS_IOV(m_dt)) {
75             size_t length = 0;
76             for (size_t i = 0; i < count(); ++i) {
77                 length += m_iov[i].length;
78             }
79             return length;
80         }
81         ADD_FAILURE() << "Not supported datatype";
82         return -1;
83     }
84 
count()85     size_t count() const {
86         EXPECT_TRUE(is_valid());
87         return m_count;
88     };
89 
is_valid()90     bool is_valid() const {
91         return (m_buf != NULL) && (m_count != 0) &&
92                (UCP_DT_IS_IOV(m_dt) ? (m_count <= m_iov_cnt_limit) :
93                (UCP_DT_IS_CONTIG(m_dt) || UCP_DT_IS_GENERIC(m_dt)));
94     }
95 
96 private:
97     data_type_desc_t &make(ucp_datatype_t datatype, const void *buf,
98                            size_t length, size_t iov_count);
99 
invalidate()100     void invalidate() {
101         EXPECT_TRUE(is_valid());
102         m_buf   = NULL;
103         m_count = 0;
104     }
105 
106     uintptr_t       m_origin;
107     size_t          m_length;
108 
109     ucp_datatype_t  m_dt;
110     const void     *m_buf;
111     size_t          m_count;
112 
113     const size_t    m_iov_cnt_limit;
114     ucp_dt_iov_t    m_iov[MAX_IOV];
115 };
116 
117 struct dt_gen_state {
118     size_t              count;
119     int                 started;
120     uint32_t            magic;
121     void                *context;
122 };
123 
124 extern int dt_gen_start_count;
125 extern int dt_gen_finish_count;
126 extern ucp_generic_dt_ops test_dt_uint32_ops;
127 extern ucp_generic_dt_ops test_dt_uint32_err_ops;
128 extern ucp_generic_dt_ops test_dt_uint8_ops;
129 
130 } // ucp
131 
132 #endif /* TEST_UCP_DATATYPE_H_ */
133