Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/ccv_cnnp_dataframe_core.c
Line
Count
Source
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
7
#pragma mark - Reducer
8
9
typedef struct {
10
  int column_idx;
11
  int batch_size;
12
  int iter_idx;
13
  ccv_cnnp_column_data_reduce_f reduce;
14
  ccv_cnnp_dataframe_t* dataframe;
15
  ccv_cnnp_dataframe_iter_t* iter;
16
  ccv_cnnp_column_data_deinit_f data_deinit;
17
  void* context;
18
  ccv_cnnp_column_data_context_deinit_f context_deinit;
19
  void* batch_data[1];
20
} ccv_cnnp_dataframe_reducer_t;
21
22
static void _ccv_cnnp_reducer_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)
23
3.46k
{
24
3.46k
  ccv_cnnp_dataframe_reducer_t* const reducer = (ccv_cnnp_dataframe_reducer_t*)context;
25
3.46k
  if (!reducer->iter)
26
9
  {
27
9
    reducer->iter = ccv_cnnp_dataframe_iter_new(reducer->dataframe, &reducer->column_idx, 1);
28
9
    reducer->iter_idx = -1;
29
9
  }
30
3.46k
  ccv_cnnp_dataframe_iter_t* const iter = reducer->iter;
31
3.46k
  int i, j;
32
6.92k
  for (i = 0; i < row_size; 
i++3.46k
)
33
3.46k
  {
34
3.46k
    if (reducer->iter_idx + 1 != row_idxs[i])
35
71
      ccv_cnnp_dataframe_iter_set_cursor(iter, row_idxs[i] * reducer->batch_size);
36
3.46k
    reducer->iter_idx = row_idxs[i];
37
3.46k
    ccv_cnnp_dataframe_iter_prefetch(iter, reducer->batch_size, stream_context);
38
3.52M
    for (j = 0; j < reducer->batch_size; 
j++3.52M
)
39
3.52M
      if (0 != ccv_cnnp_dataframe_iter_next(iter, reducer->batch_data + j, 1, stream_context))
40
76
        break;
41
3.46k
    reducer->reduce(reducer->batch_data, j, data + i, reducer->context, stream_context);
42
3.46k
  }
43
3.46k
}
44
45
static void _ccv_cnnp_reducer_data_deinit(void* const data, void* const context)
46
7
{
47
7
  ccv_cnnp_dataframe_reducer_t* const reducer = (ccv_cnnp_dataframe_reducer_t*)context;
48
7
  assert(reducer->data_deinit);
49
7
  reducer->data_deinit(data, reducer->context);
50
7
}
51
52
static void _ccv_cnnp_reducer_deinit(void* const context)
53
9
{
54
9
  ccv_cnnp_dataframe_reducer_t* const reducer = (ccv_cnnp_dataframe_reducer_t*)context;
55
9
  if (reducer->iter)
56
9
    ccv_cnnp_dataframe_iter_free(reducer->iter);
57
9
  if (reducer->context_deinit)
58
5
    reducer->context_deinit(reducer->context);
59
9
  ccfree(reducer);
60
9
}
61
62
ccv_cnnp_dataframe_t* ccv_cnnp_dataframe_reduce_new(ccv_cnnp_dataframe_t* const dataframe, ccv_cnnp_column_data_reduce_f reduce, 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)
63
9
{
64
9
  assert(batch_size > 0);
65
9
  ccv_cnnp_dataframe_reducer_t* const reducer = (ccv_cnnp_dataframe_reducer_t*)ccmalloc(sizeof(ccv_cnnp_dataframe_reducer_t) + sizeof(void*) * (batch_size - 1));
66
9
  reducer->column_idx = column_idx;
67
9
  reducer->batch_size = batch_size;
68
9
  reducer->reduce = reduce;
69
9
  reducer->dataframe = dataframe;
70
9
  reducer->iter = 0;
71
9
  reducer->data_deinit = data_deinit;
72
9
  reducer->context = context;
73
9
  reducer->context_deinit = context_deinit;
74
9
  ccv_cnnp_column_data_t reduce_column = {
75
9
    .data_enum = _ccv_cnnp_reducer_enum,
76
9
    .data_deinit = data_deinit ? 
_ccv_cnnp_reducer_data_deinit5
:
04
, // Redirect to our data deinit method.
77
9
    .context = reducer,
78
9
    .context_deinit = _ccv_cnnp_reducer_deinit,
79
9
  };
80
9
  return ccv_cnnp_dataframe_new(&reduce_column, 1, (ccv_cnnp_dataframe_row_count(dataframe) + batch_size - 1) / batch_size);
81
9
}
82
83
#pragma mark - Extract
84
85
static void _ccv_cnnp_extract_value(void*** 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)
86
5.18k
{
87
5.18k
  const off_t offset = (off_t)(uintptr_t)context;
88
5.18k
  int i;
89
5.28M
  for (i = 0; i < batch_size; 
i++5.28M
)
90
5.28M
  {
91
5.28M
    char* const values = (char*)column_data[0][i];
92
5.28M
    data[i] = *(void**)(values + offset);
93
5.28M
  }
94
5.18k
}
95
96
int ccv_cnnp_dataframe_extract_value(ccv_cnnp_dataframe_t* const dataframe, const int column_idx, const off_t offset)
97
7
{
98
7
  return ccv_cnnp_dataframe_map(dataframe, _ccv_cnnp_extract_value, 0, 0, &column_idx, 1, (void*)(uintptr_t)offset, 0);
99
7
}
100
101
#pragma mark - Make Tuple
102
103
static void _ccv_cnnp_tuple_deinit(void* const data, void* const context)
104
9.22k
{
105
9.22k
  ccfree(data);
106
9.22k
}
107
108
static void _ccv_cnnp_make_tuple(void*** 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)
109
5.19k
{
110
5.19k
  const ccv_cnnp_dataframe_tuple_t* const tuple = (ccv_cnnp_dataframe_tuple_t*)context;
111
5.19k
  int i, j;
112
5.28M
  for (i = 0; i < batch_size; 
i++5.28M
)
113
5.28M
  {
114
5.28M
    if (!data[i])
115
9.22k
      data[i] = ccmalloc(sizeof(void*) * tuple->size);
116
5.28M
    void** tuple_data = (void**)data[i];
117
14.0M
    for (j = 0; j < column_size; 
j++8.78M
)
118
8.78M
      tuple_data[j] = column_data[j][i];
119
5.28M
  }
120
5.19k
}
121
122
int ccv_cnnp_dataframe_make_tuple(ccv_cnnp_dataframe_t* const dataframe, const int* const column_idxs, const int column_idx_size)
123
10
{
124
10
  ccv_cnnp_dataframe_tuple_t* const tuple = (ccv_cnnp_dataframe_tuple_t*)ccmalloc(sizeof(ccv_cnnp_dataframe_tuple_t));
125
10
  tuple->size = column_idx_size;
126
10
  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);
127
10
}
128
129
int ccv_cnnp_dataframe_tuple_size(const ccv_cnnp_dataframe_t* const dataframe, const int column_idx)
130
2
{
131
2
  const ccv_cnnp_dataframe_tuple_t* const tuple = (ccv_cnnp_dataframe_tuple_t*)ccv_cnnp_dataframe_column_context(dataframe, column_idx);
132
2
  return tuple->size;
133
2
}
134
135
int ccv_cnnp_dataframe_extract_tuple(ccv_cnnp_dataframe_t* const dataframe, const int column_idx, const int index)
136
3
{
137
3
  return ccv_cnnp_dataframe_extract_value(dataframe, column_idx, index * sizeof(void*));
138
3
}