[UAPI] Set model's npubinfmt version
[platform/adaptation/npu/trix-engine.git] / tests / apptests / dummy_triv_async.c
1 /**
2  * Proprietary
3  * Copyright (C) 2019 Samsung Electronics
4  * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
5  */
6 /**
7  * @file dummy_triv_async.c
8  * @date 9 Aug 2019
9  * @brief AppTest to test output callbacks in async execution.
10  * @author Dongju Chae <dongju.chae@samsung.com>
11  * @bug No known bugs except for NYI items
12  */
13
14 #include <libnpuhost.h>
15 #include <npubinfmt.h>
16 #include <errno.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <pthread.h>
21
22 #include <ne_test_utils.h>
23
24 /** @brief lock for private data */
25 #define TEST_LOCK()   pthread_mutex_lock(&priv.mutex)
26 #define TEST_UNLOCK() pthread_mutex_unlock(&priv.mutex)
27 #define TEST_WAKEUP() pthread_cond_broadcast(&priv.cond)
28 #define TEST_WAIT()   pthread_cond_wait(&priv.cond, &priv.mutex)
29
30 typedef struct {
31   uint32_t num_callbacks;
32   pthread_mutex_t mutex;
33   pthread_cond_t cond;
34 } test_priv;
35
36 #if defined(ENABLE_EMUL)
37 const static int TEST_NUM_TESTS = 1024;
38 #else
39 const static int TEST_NUM_TESTS = 3 * 4;
40 #endif
41 static test_priv priv;
42
43 /**
44  * @brief output callback function
45  * @param[in] output output buffer
46  * @param[in] sequence sequence number of each inference
47  * @param[in] data output callback data
48  */
49 static void
50 callback (output_buffers *output, uint64_t sequence, void *data)
51 {
52   uint32_t idx;
53
54   TEST_LOCK();
55
56   ++priv.num_callbacks;
57
58   /** user needs to free output buffers (if exists) */
59   for (idx = 0; idx < output->num_buffers; idx++) {
60     assert (output->bufs[idx].type == BUFFER_MAPPED);
61     free (output->bufs[idx].addr);
62   }
63
64   TEST_WAKEUP();
65   TEST_UNLOCK();
66 }
67
68 /**
69  * @brief inference entry which repeatedly calls runNPU_async
70  * @param[in] dev npu device pointer
71  * @return 0 if no error, otherwise a negative error value
72  */
73 static int
74 run_inference (npudev_h dev)
75 {
76   /* Constants */
77   const size_t program_size = 4096;
78   const size_t weight_size = 4096;
79   const size_t buffer_size = 4096;
80   const size_t input_offset = 0;
81   const size_t input_size = 1024;
82   const size_t output_offset = 512;
83   const size_t output_size = 2048;
84
85   uint64_t sequence;
86   uint32_t model_id;
87   int i, err = -1, num_tests = TEST_NUM_TESTS;
88
89   generic_buffer * model;
90   input_buffers input;
91
92   /* make dummy model data */
93   npubin_meta meta = {
94     /* below are compiler stuff */
95     .magiccode = NPUBIN_MAGICCODE | 0x1,
96     .name = "dummy model",
97     .model_id = 1,
98     .model_version = 1,
99     .buffer_size = buffer_size,
100     .size = NPUBIN_META_SIZE + program_size + weight_size,
101     .type = SMODEL_OPS_NPU,
102     .input_offset = input_offset,
103     .input_size = input_size,
104     .output_offset = output_offset,
105     .output_size = output_size,
106     .program_size = program_size,
107     .weight_size = weight_size,
108   };
109
110   pthread_mutex_init (&priv.mutex, NULL);
111   pthread_cond_init (&priv.cond, NULL);
112
113   /** make model */
114   model = make_model (dev, &meta, BUFFER_MAPPED);
115   if (!model)
116     goto out;
117
118   /** allocate I/O buffers */
119   input.num_buffers = 1;
120   input.bufs[0].size = buffer_size;
121   input.bufs[0].type = BUFFER_MAPPED;
122   if ((err = allocNPU_inputBuffers (dev, &input)) != 0)
123     goto out_free_model;
124
125   /** register the model to NPU Engine */
126   if ((err = registerNPUmodel(dev, model, &model_id)) != 0)
127     goto out_free_all;
128
129   priv.num_callbacks = 0;
130
131   /** run NPU inference */
132   for (i = 0; i < num_tests; i++) {
133     fill_input_data (&input.bufs[0], meta.input_offset, meta.input_size);
134     err = runNPU_async (dev, model_id, &input, callback, &sequence, NULL, NPUASYNC_WAIT);
135     if (err != 0 || sequence != (uint64_t) (i + 1)) {
136       goto out_unregister;
137     }
138   }
139
140   /** wait until all callbacks are called */
141   TEST_LOCK();
142   while (priv.num_callbacks != num_tests)
143     TEST_WAIT();
144   err = 0;
145   TEST_UNLOCK();
146
147 out_unregister:
148   unregisterNPUmodel_all (dev);
149 out_free_all:
150   cleanNPU_inputBuffers (dev, &input);
151 out_free_model:
152   destroy_model (dev, model);
153 out:
154   pthread_mutex_destroy (&priv.mutex);
155   pthread_cond_destroy (&priv.cond);
156
157   return err;
158 }
159
160 /** @brief apptest main  */
161 int
162 main (int argc, char **argv)
163 {
164   int num_devices = getnumNPUdeviceByType (NPUCOND_TRIV_CONN_SOCIP);
165   int result = -1;
166   npudev_h dev;
167
168   if (num_devices) {
169     if (getNPUdeviceByType (&dev, NPUCOND_TRIV_CONN_SOCIP, num_devices - 1) == 0) {
170       result = run_inference (dev);
171       if (result == 0)
172         fprintf(stderr, "[APPTEST] %s: PASSED\n", argv[0]);
173       else
174         fprintf(stderr, "[APPTEST] %s: FAILED (%d)\n", argv[0], result);
175       putNPUdevice (dev);
176     }
177   }
178
179   return result;
180 }