-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFaceColoring.h
159 lines (153 loc) · 5.8 KB
/
FaceColoring.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
// C++ libs
#include <memory>
// CUDA stuff
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
// Project files
#include "helper_cuda.h"
#include "CTimer.h"
#include "Vertices.h"
#include "Quadrilaterals.h"
#include "Halfedges.h"
#include "HalfedgeFaces.h"
#include "HalfedgeVertices.h"
#include "HalfedgeMesh.h"
#include "Edges.h"
#include "EdgeHashTable.h"
#include "VertexMap.h"
#include "QuadrilateralMap.h"
namespace p_mc {
/// <summary>
/// Faces in the DMC mesh will be colored using five colors.
/// The initial coloring is inherited from an edge coloring
/// of the input grid consisting of 24 colors, from 0 t0 23.
/// This class will eliminate 19 colors, the colors 5 to 23.
/// C is the index of the first color to be eliminated, cSz
/// is the total number of colors to be eliminated by this class.
/// </summary>
constexpr int C{ 5 };
constexpr int cSz{ 19 };
/// <summary>
/// Computes the face coloring of the DMC mesh consisting of
/// a total of 5 colors.
/// </summary>
struct FaceColoring {
/// <summary>
/// Helper class to count how many
/// faces have a given color.
/// </summary>
struct ColorCount {
int nr_f{ 0 };
/// <summary>
/// List of indices of a given color
/// </summary>
int* colors[cSz];
std::vector<std::shared_ptr<int>> colors_;
/// <summary>
/// atomic counter, given size of index array
/// </summary>
int* t_size[cSz];
std::vector<std::shared_ptr<int>> t_size_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="nr_q">nr. of quadrilaterals in the DMC mesh</param>
/// <returns></returns>
__host__ ColorCount(const int nr_q)
{
nr_f = 3 * nr_q / cSz;
colors_.resize(cSz);
t_size_.resize(cSz);
for (int i = 0; i < cSz; i++)
{
cudaMalloc(&colors[i], nr_f * sizeof(int));
cudaCheckError();
colors_[i].reset(colors[i], cudaFree);
//colors_[i] = std::make_shared<int>(colors[i], cudaFree);
cudaMalloc(&t_size[i], sizeof(int));
cudaCheckError(); // p_mc::cudaError(__FILE__, __LINE__);
cudaMemset(t_size[i], 0, sizeof(int));
t_size_[i].reset(t_size[i], cudaFree);
}
}
/// <summary>
/// Destructor
/// </summary>
/// <returns></returns>
__host__ ~ColorCount()
{
for (auto e : colors_)
{
e.reset();
}
for (auto e : t_size_)
{
e.reset();
}
}
/// <summary>
/// Set size of buffers
/// </summary>
/// <param name="nr_q">nr. of quadrilaterals in the DMC mesh</param>
/// <returns></returns>
__host__ void resize(const int nr_q)
{
const int sz = 3 * nr_q / cSz;
colors_.resize(cSz);
t_size_.resize(cSz);
for (int i = 0; i < cSz; i++)
{
cudaMalloc(&colors[i], sz * sizeof(int));
cudaCheckError(); //p_mc::cudaError(__FILE__, __LINE__);
colors_[i].reset(colors[i], cudaFree);
//colors_[i] = std::make_shared<int>(colors[i], cudaFree);
cudaMalloc(&t_size[i], sizeof(int));
cudaCheckError(); //p_mc::cudaError(__FILE__, __LINE__);
cudaMemset(t_size[i], 0, sizeof(int));
t_size_[i].reset(t_size[i], cudaFree);
}
}
/// <summary>
/// Buffer size
/// </summary>
/// <returns></returns>
__host__ __device__ int bufferSize() { return nr_f; }
/// <summary>
/// Add face index in array of all faces of a given color. It is
/// used to count the number of elements with a certain color.
/// </summary>
/// <param name="c">color</param>
/// <param name="faceId">face index</param>
/// <returns></returns>
__device__ void addColor(const int c, const int faceId)
{
const int addr = atomicAdd(t_size[c - C], 1);
colors[c - C][addr] = faceId;
}
/// <summary>
/// Get face id for input color, which is saved at position i in array.
/// </summary>
/// <param name="c">Color</param>
/// <param name="i">position in array for that color</param>
/// <returns></returns>
__device__ int getFace(const int c, const int i) { return colors[c - C][i]; }
__host__ int size(const int c)
{
int nr{ 0 };
cudaMemcpy(&nr, t_size[c - C], sizeof(int), cudaMemcpyDeviceToHost);
return nr;
}
};
/// <summary>
/// Classify faces accroding to color, i.e. it counts how many faces
/// has a certain color. It requires halfedge data structure to find
/// neighbor faces and check for equal colors.
/// </summary>
/// <param name="q">quadrilateral faces</param>
/// <param name="he">halfedge edges</param>
/// <param name="hef">halfedge faces</param>
/// <returns></returns>
__host__ void colorFaces(Quadrilaterals& q, Halfedges he, HalfedgeFaces& hef, CTimer& timer);
};
} // namespace