Coverage Report

Created: 2021-04-14 19:30

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/ccv_cnnp_dataframe_core.c
Line
Count
Source (jump to first uncovered line)
1
#include "ccv_nnc.h"
2
#include "ccv_nnc_easy.h"
3
#include "ccv_nnc_internal.h"
4
#include "ccv_internal.h"
5
#include "_ccv_cnnp_dataframe.h"
6
#ifdef CCV_BLOCK_SUPPORT
7
#include <Block.h>
8
#endif
9
10
// MARK - Reducer
11
12
typedef struct {
13
  int column_idx;
14
  int batch_size;
15
  int iter_idx;
16
  ccv_cnnp_column_data_sample_f sample;
17
  ccv_cnnp_dataframe_t* dataframe;
18
  ccv_cnnp_dataframe_iter_t* iter;
19
  ccv_cnnp_column_data_deinit_f data_deinit;
20
  void* context;
21
  ccv_cnnp_column_data_context_deinit_f context_deinit;
22
  void* batch_data[1];
23
} ccv_cnnp_dataframe_sampler_t;
24
25
static void _ccv_cnnp_sampler_enum(const int column_idx, const int* const row_idxs, const int row_size, void** const data, void* const context, ccv_nnc_stream_context_t* const stream_context)
26
371
{
27
371
  ccv_cnnp_dataframe_sampler_t* const sampler = (ccv_cnnp_dataframe_sampler_t*)context;
28
371
  if (!sampler->iter)
29
15
  {
30
15
    sampler->iter = ccv_cnnp_dataframe_iter_new(sampler->dataframe, &sampler->column_idx, 1);
31
15
    sampler->iter_idx = -1;
32
15
  }
33
371
  ccv_cnnp_dataframe_iter_t* const iter = sampler->iter;
34
371
  int i, j;
35
742
  for (i = 0; i < row_size; 
i++371
)
36
371
  {
37
371
    if (sampler->iter_idx + 1 != row_idxs[i])
38
0
      ccv_cnnp_dataframe_iter_set_cursor(iter, row_idxs[i] * sampler->batch_size);
39
371
    sampler->iter_idx = row_idxs[i];
40
371
    ccv_cnnp_dataframe_iter_prefetch(iter, sampler->batch_size, stream_context);
41
180k
    for (j = 0; j < sampler->batch_size; 
j++180k
)
42
180k
      if (0 != ccv_cnnp_dataframe_iter_next(iter, sampler->batch_data + j, 1, stream_context))
43
14
        break;
44
371
    sampler->sample(sampler->batch_data, j, data + i, sampler->context, stream_context);
45
371
  }
46
371
}
47
48
static void _ccv_cnnp_sampler_data_deinit(void* const data, void* const context)
49
14
{
50
14
  ccv_cnnp_dataframe_sampler_t* const sampler = (ccv_cnnp_dataframe_sampler_t*)context;
51
14
  assert(sampler->data_deinit);
52
14
  sampler->data_deinit(data, sampler->context);
53
14
}
54
55
static void _ccv_cnnp_sampler_deinit(void* const context)
56
15
{
57
15
  ccv_cnnp_dataframe_sampler_t* const sampler = (ccv_cnnp_dataframe_sampler_t*)context;
58
15
  if (sampler->iter)
59
15
    ccv_cnnp_dataframe_iter_free(sampler->iter);
60
15
  if (sampler->context_deinit)
61
11
    sampler->context_deinit(sampler->context);
62
15
  ccfree(sampler);
63
15
}
64
65
ccv_cnnp_dataframe_t* ccv_cnnp_dataframe_sample_new(ccv_cnnp_dataframe_t* const dataframe, ccv_cnnp_column_data_sample_f sample, ccv_cnnp_column_data_deinit_f data_deinit, const int column_idx, const int batch_size, void* const context, ccv_cnnp_column_data_context_deinit_f context_deinit)
66
15
{
67
15
  assert(batch_size > 0);
68
15
  ccv_cnnp_dataframe_sampler_t* const sampler = (ccv_cnnp_dataframe_sampler_t*)ccmalloc(sizeof(ccv_cnnp_dataframe_sampler_t) + sizeof(void*) * (batch_size - 1));
69
15
  sampler->column_idx = column_idx;
70
15
  sampler->batch_size = batch_size;
71
15
  sampler->sample = sample;
72
15
  sampler->dataframe = dataframe;
73
15
  sampler->iter = 0;
74
15
  sampler->data_deinit = data_deinit;
75
15
  sampler->context = context;
76
15
  sampler->context_deinit = context_deinit;
77
15
  ccv_cnnp_column_data_t sample_column = {
78
15
    .data_enum = _ccv_cnnp_sampler_enum,
79
15
    .data_deinit = data_deinit ? 
_ccv_cnnp_sampler_data_deinit11
:
04
, // Redirect to our data deinit method.
80
15
    .context = sampler,
81
15
    .context_deinit = _ccv_cnnp_sampler_deinit,
82
15
  };
83
15
  return ccv_cnnp_dataframe_new(&sample_column, 1, (ccv_cnnp_dataframe_row_count(dataframe) + batch_size - 1) / batch_size);
84
15
}
85
86
// MARK - Extract
87
88
static void _ccv_cnnp_extract_value(void* const* const* const column_data, const int column_size, const int batch_size, void** const data, void* const context, ccv_nnc_stream_context_t* const stream_context)
89
691
{
90
691
  const off_t offset = (off_t)(uintptr_t)context;
91
691
  int i;
92
301k
  for (i = 0; i < batch_size; 
i++301k
)
93
301k
  {
94
301k
    char* const values = (char*)column_data[0][i];
95
301k
    data[i] = *(void**)(values + offset);
96
301k
  }
97
691
}
98
99
int ccv_cnnp_dataframe_extract_value(ccv_cnnp_dataframe_t* const dataframe, const int column_idx, const off_t offset, const char* name)
100
45
{
101
45
  return ccv_cnnp_dataframe_map(dataframe, _ccv_cnnp_extract_value, 0, 0, &column_idx, 1, (void*)(uintptr_t)offset, 0, name);
102
45
}
103
104
// MARK - Make Tuple
105
106
static void _ccv_cnnp_tuple_deinit(void* const data, void* const context)
107
11.2k
{
108
11.2k
  ccfree(data);
109
11.2k
}
110
111
static void _ccv_cnnp_make_tuple(void* const* const* const column_data, const int column_size, const int batch_size, void** const data, void* const context, ccv_nnc_stream_context_t* const stream_context)
112
671
{
113
671
  const ccv_cnnp_dataframe_tuple_t* const tuple = (ccv_cnnp_dataframe_tuple_t*)context;
114
671
  int i, j;
115
301k
  for (i = 0; i < batch_size; 
i++300k
)
116
300k
  {
117
300k
    if (!data[i])
118
11.2k
      data[i] = ccmalloc(sizeof(void*) * tuple->size);
119
300k
    void** tuple_data = (void**)data[i];
120
752k
    for (j = 0; j < column_size; 
j++451k
)
121
451k
      tuple_data[j] = column_data[j][i];
122
300k
  }
123
671
}
124
125
int ccv_cnnp_dataframe_make_tuple(ccv_cnnp_dataframe_t* const dataframe, const int* const column_idxs, const int column_idx_size, const char* name)
126
18
{
127
18
  ccv_cnnp_dataframe_tuple_t* const tuple = (ccv_cnnp_dataframe_tuple_t*)ccmalloc(sizeof(ccv_cnnp_dataframe_tuple_t));
128
18
  tuple->size = column_idx_size;
129
18
  return ccv_cnnp_dataframe_map(dataframe, _ccv_cnnp_make_tuple, 0, _ccv_cnnp_tuple_deinit, column_idxs, column_idx_size, tuple, (ccv_cnnp_column_data_context_deinit_f)ccfree, name);
130
18
}
131
132
int ccv_cnnp_dataframe_tuple_size(const ccv_cnnp_dataframe_t* const dataframe, const int column_idx)
133
2
{
134
2
  const ccv_cnnp_dataframe_tuple_t* const tuple = (ccv_cnnp_dataframe_tuple_t*)ccv_cnnp_dataframe_column_context(dataframe, column_idx);
135
2
  return tuple->size;
136
2
}
137
138
int ccv_cnnp_dataframe_extract_tuple(ccv_cnnp_dataframe_t* const dataframe, const int column_idx, const int index, const char* name)
139
31
{
140
31
  return ccv_cnnp_dataframe_extract_value(dataframe, column_idx, index * sizeof(void*), name);
141
31
}