Skip to content

Commit 4cd9aea

Browse files
committed
CImgSharpenShock: catch exception inside omp parallel
1 parent 5f27818 commit 4cd9aea

1 file changed

Lines changed: 68 additions & 50 deletions

File tree

CImg/SharpenShock/CImgSharpenShock.cpp

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -191,69 +191,87 @@ class CImgSharpenShockPlugin
191191
if (sigma > 0) {
192192
G.blur(sigma);
193193
}
194+
bool go = true;
194195
#ifdef cimg_use_openmp
195196
#pragma omp parallel for if (G.width()>=32 && G.height()>=16)
196197
#endif
197198
cimg_forY(G, y) {
198-
CImg<Tfloat> val, vec;
199-
Tfloat *ptrG0 = G.data(0, y, 0, 0), *ptrG1 = G.data(0, y, 0, 1), *ptrG2 = G.data(0, y, 0, 2);
200-
test_abort();
201-
cimg_forX(G, x) {
202-
G.get_tensor_at(x, y).symmetric_eigen(val, vec);
203-
if (val[0] < 0) {val[0] = 0; }
204-
if (val[1] < 0) {val[1] = 0; }
205-
*(ptrG0++) = vec(0, 0);
206-
*(ptrG1++) = vec(0, 1);
207-
*(ptrG2++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1], -(Tfloat)nedge);
199+
if (go) {
200+
try {
201+
CImg<Tfloat> val, vec;
202+
Tfloat *ptrG0 = G.data(0, y, 0, 0), *ptrG1 = G.data(0, y, 0, 1), *ptrG2 = G.data(0, y, 0, 2);
203+
test_abort();
204+
cimg_forX(G, x) {
205+
G.get_tensor_at(x, y).symmetric_eigen(val, vec);
206+
if (val[0] < 0) {val[0] = 0; }
207+
if (val[1] < 0) {val[1] = 0; }
208+
*(ptrG0++) = vec(0, 0);
209+
*(ptrG1++) = vec(0, 1);
210+
*(ptrG2++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1], -(Tfloat)nedge);
211+
}
212+
} catch (...) {
213+
#pragma omp atomic
214+
go &= false;
215+
}
208216
}
209217
}
218+
test_abort();
210219
#ifdef cimg_use_openmp
211220
#pragma omp parallel for if (cimg.width()*cimg.height()>=512 && cimg.spectrum()>=2)
212221
#endif
213222
cimg_forC(cimg, c) {
214-
Tfloat *ptrd = velocity.data(0, 0, 0, c), veloc_max = 0;
215-
216-
CImg_3x3(I, Tfloat);
217-
cimg_for3(cimg._height, y) {
218-
test_abort();
219-
for (int x = 0,
220-
_p1x = 0,
221-
_n1x = (int)(
222-
( I[0] = I[1] = (T)cimg(_p1x, _p1y, 0, c) ),
223-
( I[3] = I[4] = (T)cimg(0, y, 0, c) ),
224-
( I[6] = I[7] = (T)cimg(0, _n1y, 0, c) ),
225-
1 >= cimg._width ? cimg.width() - 1 : 1);
226-
( _n1x < cimg.width() && (
227-
( I[2] = (T)cimg(_n1x, _p1y, 0, c) ),
228-
( I[5] = (T)cimg(_n1x, y, 0, c) ),
229-
( I[8] = (T)cimg(_n1x, _n1y, 0, c) ), 1) ) ||
230-
x == --_n1x;
231-
I[0] = I[1], I[1] = I[2],
232-
I[3] = I[4], I[4] = I[5],
233-
I[6] = I[7], I[7] = I[8],
234-
_p1x = x++, ++_n1x) {
235-
const Tfloat u = G(x, y, 0),
236-
v = G(x, y, 1),
237-
amp = G(x, y, 2),
238-
ixx = Inc + Ipc - 2 * Icc,
239-
ixy = (Inn + Ipp - Inp - Ipn) / 4,
240-
iyy = Icn + Icp - 2 * Icc,
241-
ixf = Inc - Icc,
242-
ixb = Icc - Ipc,
243-
iyf = Icn - Icc,
244-
iyb = Icc - Icp,
245-
itt = u * u * ixx + v * v * iyy + 2 * u * v * ixy,
246-
it = u * cimg::minmod(ixf, ixb) + v * cimg::minmod(iyf, iyb),
247-
veloc = -amp*cimg::sign(itt) * cimg::abs(it);
248-
*(ptrd++) = veloc;
249-
if (veloc > veloc_max) {
250-
veloc_max = veloc;
251-
} else if (-veloc > veloc_max) {
252-
veloc_max = -veloc;
223+
if (go) {
224+
try {
225+
Tfloat *ptrd = velocity.data(0, 0, 0, c), veloc_max = 0;
226+
227+
CImg_3x3(I, Tfloat);
228+
cimg_for3(cimg._height, y) {
229+
if (go) {
230+
test_abort();
231+
for (int x = 0,
232+
_p1x = 0,
233+
_n1x = (int)(
234+
( I[0] = I[1] = (T)cimg(_p1x, _p1y, 0, c) ),
235+
( I[3] = I[4] = (T)cimg(0, y, 0, c) ),
236+
( I[6] = I[7] = (T)cimg(0, _n1y, 0, c) ),
237+
1 >= cimg._width ? cimg.width() - 1 : 1);
238+
( _n1x < cimg.width() && (
239+
( I[2] = (T)cimg(_n1x, _p1y, 0, c) ),
240+
( I[5] = (T)cimg(_n1x, y, 0, c) ),
241+
( I[8] = (T)cimg(_n1x, _n1y, 0, c) ), 1) ) ||
242+
x == --_n1x;
243+
I[0] = I[1], I[1] = I[2],
244+
I[3] = I[4], I[4] = I[5],
245+
I[6] = I[7], I[7] = I[8],
246+
_p1x = x++, ++_n1x) {
247+
const Tfloat u = G(x, y, 0),
248+
v = G(x, y, 1),
249+
amp = G(x, y, 2),
250+
ixx = Inc + Ipc - 2 * Icc,
251+
ixy = (Inn + Ipp - Inp - Ipn) / 4,
252+
iyy = Icn + Icp - 2 * Icc,
253+
ixf = Inc - Icc,
254+
ixb = Icc - Ipc,
255+
iyf = Icn - Icc,
256+
iyb = Icc - Icp,
257+
itt = u * u * ixx + v * v * iyy + 2 * u * v * ixy,
258+
it = u * cimg::minmod(ixf, ixb) + v * cimg::minmod(iyf, iyb),
259+
veloc = -amp*cimg::sign(itt) * cimg::abs(it);
260+
*(ptrd++) = veloc;
261+
if (veloc > veloc_max) {
262+
veloc_max = veloc;
263+
} else if (-veloc > veloc_max) {
264+
veloc_max = -veloc;
265+
}
266+
}
267+
}
253268
}
269+
_veloc_max[c] = veloc_max;
270+
} catch (...) {
271+
#pragma omp atomic
272+
go &= false;
254273
}
255274
}
256-
_veloc_max[c] = veloc_max;
257275
}
258276

259277
const Tfloat veloc_max = _veloc_max.max();

0 commit comments

Comments
 (0)