This repository has been archived by the owner on Nov 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
modified_permutohedral.hpp
116 lines (95 loc) · 3.37 KB
/
modified_permutohedral.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef PERMUTOHEDRAL_MODIFIED_PERMUTOHEDRAL_HPP_
#define PERMUTOHEDRAL_MODIFIED_PERMUTOHEDRAL_HPP_
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <cmath>
#include "hash_table.hpp"
#ifndef CPU_ONLY
#include "cuda_check.h"
#endif
/************************************************/
/*** ModifiedPermutohedral Lattice ***/
/************************************************/
namespace Permutohedral {
typedef struct MatrixEntry {
int index;
float weight;
} MatrixEntry;
class ModifiedPermutohedral
{
protected:
struct Neighbors{
int n1, n2;
Neighbors( int n1=0, int n2=0 ):n1(n1),n2(n2){
}
};
// Check if GPU hash table if initialize
bool is_init;
std::vector<int> offset_, rank_;
std::vector<float> barycentric_;
std::vector<Neighbors> blur_neighbors_;
// GPU specific
MatrixEntry *matrix;
HashTable table;
// Number of elements, size of sparse discretized space, dimension of features width and height
int N_, M_, d_, w_, h_;
void init_cpu(const float* features, int num_dimensions, int num_points);
void init_gpu(const float* features, int num_dimensions, int w, int h);
void compute_cpu(float* out, const float* in, int value_size, bool reverse = false, bool add = false) const;
void compute_cpu(double* out, const double* in, int value_size, bool reverse = false, bool add = false) const;
void compute_gpu(float* out, const float* in, int value_size, bool reverse = false, bool add = false) const;
void compute_gpu(double* out, const double* in, int value_size, bool reverse = false, bool add = false) const;
void sseCompute(float* out, const float* in, int value_size, bool reverse = false, bool add = false) const;
void sseCompute(double* out, const double* in, int value_size, bool reverse = false, bool add = false) const;
void seqCompute(float* out, const float* in, int value_size, bool reverse = false, bool add = false) const;
void seqCompute(double* out, const double* in, int value_size, bool reverse = false, bool add = false) const;
public:
ModifiedPermutohedral();
~ModifiedPermutohedral(){
#ifndef CPU_ONLY
if(is_init)
CUDA_CHECK(cudaFree(matrix));
#endif
}
void init (const float* features, int num_dimensions, int w, int h, bool useGPU = false){
if (!useGPU) {
init_cpu(features, num_dimensions, w*h);
}else{
#ifndef CPU_ONLY
init_gpu(features, num_dimensions, w, h);
is_init = true;
#else
throw std::runtime_error("Compiled without CUDA support!");
#endif
}
}
void compute(float* out, const float* in, int value_size, bool reverse = false,
bool add = false, bool useGPU = false) const{
if (!useGPU) {
compute_cpu(out, in, value_size, reverse, add);
}else{
#ifndef CPU_ONLY
compute_gpu(out, in, value_size, reverse, add);
#else
throw std::runtime_error("Compiled without CUDA support!");
#endif
}
}
void compute(double* out, const double* in, int value_size, bool reverse = false,
bool add = false, bool useGPU = false) const{
if (!useGPU) {
compute_cpu(out, in, value_size, reverse, add);
}else{
#ifndef CPU_ONLY
compute_gpu(out, in, value_size, reverse, add);
#else
throw std::runtime_error("Compiled without CUDA support!");
#endif
}
}
};
}//namespace Permutohedral
#endif //PERMUTOHEDRAL_MODIFIED_PERMUTOHEDRAL_HPP_