Coverage Report

Created: 2026-11-15 13:46

/home/runner/work/charm/charm/src/ck-ldb/readmodel.C
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The file is based on the Random-Forest-Matlab at
3
 * https://github.com/karpathy/Random-Forest-Matlab available under BSD license
4
5
 * Copyright (c) <2014>, <Andrej Karpathy>
6
 * All rights reserved.
7
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
11
 * 1. Redistributions of source code must retain the above copyright notice, this
12
 *  list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright notice,
14
 *  this list of conditions and the following disclaimer in the documentation
15
 *  and/or other materials provided with the distribution.
16
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
 * The views and conclusions contained in the software and documentation are those
29
 * of the authors and should not be interpreted as representing official policies,
30
 * purpose.
31
 */
32
33
#include "RandomForestModel.h"
34
#if CMK_USE_ZLIB
35
#include "zlib.h"
36
#endif
37
#include <stdio.h>
38
#ifndef PATH_MAX
39
#define PATH_MAX 65535
40
#endif
41
71.7k
#define LINE_SIZE 1024
42
43
using namespace rfmodel;
44
45
2
void ForestModel::readModel(const char* dir) {
46
2
#if CMK_USE_ZLIB
47
2
  treeModels.resize(NUM_TREES);
48
49
2
  gzFile pFile;
50
2
  double f;
51
52
14
  for (int i = 0; i < LB_CLASSES; i++) classes[i] = i + 1;
53
54
2
  char buffer[PATH_MAX];
55
2
  char linebuffer[LINE_SIZE];
56
2
  snprintf(buffer, sizeof(buffer), "%s/big_leafdist.txt.gz", dir);
57
2
  pFile = gzopen(buffer, "r");
58
59
2
  if (pFile == NULL) CkAbort("\nUnable to open model files.\n");
60
42
  for (int k = 0; k < NUM_TREES; k++) {
61
40
    int i, X, D;
62
40
    gzgets(pFile, linebuffer, LINE_SIZE);
63
40
    sscanf(linebuffer, "%d %d %d", &i, &X, &D);
64
65
40
    treeModels[i - 1].classes = classes;
66
40
    treeModels[i - 1].l_X = X;
67
40
    treeModels[i - 1].l_D = D;
68
40
    treeModels[i - 1].leafdist.resize(X * D);
69
70
61.4k
    for (int j = 0; j < X * D; j++) {
71
61.4k
      gzgets(pFile, linebuffer, LINE_SIZE);
72
61.4k
      sscanf(linebuffer, "%lf", &f);
73
61.4k
      treeModels[i - 1].leafdist[j] = f;
74
61.4k
    }
75
40
  }
76
2
  gzclose(pFile);
77
78
2
  snprintf(buffer, sizeof(buffer), "%s/big_weakmodel.txt.gz", dir);
79
2
  pFile = gzopen(buffer, "r");
80
81
2
  if (pFile == NULL) CkAbort("\nUnable to open model files.\n");
82
42
  for (int k = 0; k < NUM_TREES; k++) {
83
40
    int classifierID, r1, r2;
84
40
    double val;
85
40
    std::vector<double> w(3);
86
87
40
    int w_i;
88
40
    gzgets(pFile, linebuffer, LINE_SIZE);
89
40
    sscanf(linebuffer, "%d", &w_i);
90
91
40
    int X, D;
92
93
40
    X = treeModels[w_i - 1].l_X;
94
40
    D = treeModels[w_i - 1].l_D;
95
96
40
    treeModels[w_i - 1].weakModels.resize(X * D);
97
98
10.2k
    for (int j = 0; j < X - 1; j++) {
99
10.2k
      gzgets(pFile, linebuffer, LINE_SIZE);
100
10.2k
      sscanf(linebuffer, "%lf", &val);
101
10.2k
      classifierID = (int)val;
102
10.2k
      if (classifierID != 0) {
103
1.06k
        double val1, val2;
104
1.06k
        sscanf(linebuffer, "%lf %lf %lf %lf %lf %lf", &val, &val1, &val2, &w[0], &w[1],
105
1.06k
               &w[2]);
106
1.06k
        r1 = (int)val1;
107
1.06k
        r2 = (int)val2;
108
1.06k
      }
109
110
#ifdef DEBUG
111
      if (classifierID != 0)
112
        printf("\nclassifier = %d %d %d %lf %lf %lf \n", classifierID, r1, r2, w[0], w[1],
113
               w[2]);
114
#endif
115
116
10.2k
      treeModels[w_i - 1].weakModels[j].classifierID = classifierID;
117
10.2k
      treeModels[w_i - 1].weakModels[j].r1 = r1;
118
10.2k
      treeModels[w_i - 1].weakModels[j].r2 = r2;
119
10.2k
      treeModels[w_i - 1].weakModels[j].w = w;
120
10.2k
    }
121
40
  }
122
123
2
  gzclose(pFile);
124
2
#endif
125
126
2
  return;
127
2
}