3 * Copyright (C) 2019 Samsung Electronics
4 * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
7 * @file dummy_triv_async.c
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
14 #include <libnpuhost.h>
15 #include <npubinfmt.h>
22 #include <ne_test_utils.h>
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)
31 uint32_t num_callbacks;
32 pthread_mutex_t mutex;
36 #if defined(ENABLE_EMUL)
37 const static int TEST_NUM_TESTS = 1024;
39 const static int TEST_NUM_TESTS = 3 * 4;
41 static test_priv priv;
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
50 callback (output_buffers *output, uint64_t sequence, void *data)
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);
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
74 run_inference (npudev_h dev)
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;
87 int i, err = -1, num_tests = TEST_NUM_TESTS;
89 generic_buffer * model;
92 /* make dummy model data */
94 /* below are compiler stuff */
95 .magiccode = NPUBIN_MAGICCODE | 0x1,
96 .name = "dummy model",
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,
110 pthread_mutex_init (&priv.mutex, NULL);
111 pthread_cond_init (&priv.cond, NULL);
114 model = make_model (dev, &meta, BUFFER_MAPPED);
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)
125 /** register the model to NPU Engine */
126 if ((err = registerNPUmodel(dev, model, &model_id)) != 0)
129 priv.num_callbacks = 0;
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)) {
140 /** wait until all callbacks are called */
142 while (priv.num_callbacks != num_tests)
148 unregisterNPUmodel_all (dev);
150 cleanNPU_inputBuffers (dev, &input);
152 destroy_model (dev, model);
154 pthread_mutex_destroy (&priv.mutex);
155 pthread_cond_destroy (&priv.cond);
160 /** @brief apptest main */
162 main (int argc, char **argv)
164 int num_devices = getnumNPUdeviceByType (NPUCOND_TRIV_CONN_SOCIP);
169 if (getNPUdeviceByType (&dev, NPUCOND_TRIV_CONN_SOCIP, num_devices - 1) == 0) {
170 result = run_inference (dev);
172 fprintf(stderr, "[APPTEST] %s: PASSED\n", argv[0]);
174 fprintf(stderr, "[APPTEST] %s: FAILED (%d)\n", argv[0], result);