OWL
Loading...
Searching...
No Matches
array2D.h
1// ======================================================================== //
2// Copyright 2019-2020 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include <owl/common/math/vec.h>
20
21namespace owl {
22 namespace common {
23 namespace array2D {
24
25 inline int linear(const vec2i &ID, const vec2i &dims)
26 { return ID.x + dims.x*ID.y; }
27
28 template<typename Lambda>
29 inline void for_each(const vec2i &dims, const Lambda &lambda)
30 {
31 for (int iy=0;iy<dims.y;iy++)
32 for (int ix=0;ix<dims.x;ix++)
33 lambda(vec2i(ix,iy));
34 }
35
36 template<typename Lambda>
37 inline void for_each(const vec2i &begin,
38 const vec2i &end,
39 const Lambda &lambda)
40 {
41 for (int iy=begin.y;iy<end.y;iy++)
42 for (int ix=begin.x;ix<end.x;ix++)
43 lambda(vec2i(ix,iy));
44 }
45
46// #if OWL_HAVE_PARALLEL_FOR
47 template<typename Lambda>
48 inline void parallel_for(const vec2i &dims, const Lambda &lambda)
49 {
50 owl::common::parallel_for(dims.x*dims.y,[&](int index){
51 lambda(vec2i(index%dims.x,index/dims.x));
52 });
53 }
54// #endif
55 template<typename Lambda>
56 inline void serial_for(const vec2i &dims, const Lambda &lambda)
57 {
58 owl::common::serial_for(dims.x*dims.y,[&](int index){
59 lambda(vec2i(index%dims.x,index/dims.x));
60 });
61 }
62
63 template<typename Lambda>
64 inline void parallel_for_blocked(const vec2i &dims,
65 const vec2i &blockSize,
66 const Lambda &lambda)
67 {
68 const vec2i numBlocks = divRoundUp(dims,blockSize);
69 array2D::parallel_for
70 (numBlocks,[&](const vec2i &block){
71 const vec2i begin = block*blockSize;
72 const vec2i end = min(begin+blockSize,dims);
73 lambda(begin,end);
74 // array2D::for_each(begin,end,
75 // [&](const vec2i &pixel)
76 // { lambda(pixel); });
77 });
78 }
79 } // owl::common::array2D
80 } // owl::common
81} // owl