Skip to content

Commit

Permalink
Added parallel fftshift
Browse files Browse the repository at this point in the history
  • Loading branch information
Clonkk committed Jun 10, 2020
1 parent be153f8 commit cedba26
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions doc/fftw3.html
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
<dt><pre><span class="Keyword">proc</span> <a href="#circshift%2CTensor%5BT%5D%2Cseq%5Bint%5D"><span class="Identifier">circshift</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Identifier">Tensor</span><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">shift</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><span class="Identifier">int</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">Tensor</span><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>

Circshift
Generic Circshift

</dd>
<a id="fftshift,Tensor[T]"></a>
Expand Down Expand Up @@ -1440,7 +1440,7 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-06-07 15:21:42 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-06-10 12:19:53 UTC</small>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion fftw3.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.2.2"
version = "0.2.3"
author = "rcaillaud"
description = "Nim FFTW bindings"
license = "LGPL-2.1"
Expand Down
53 changes: 50 additions & 3 deletions src/fftw3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,44 @@ type
fftw_plan* = pointer

# Utility procedures
proc circshift_impl[T](t: Tensor[T], xshift: int, yshift: int, zshift: int): Tensor[T]=
assert(t.rank == 3)
var X = t.shape[0]
var Y = t.shape[1]
var Z = t.shape[2]

proc circshift*[T](t: Tensor[T], shift: seq[int]): Tensor[T]=
## Circshift
assert(t.rank == shift.len)
result = newTensor[T](t.shape.toSeq)
for i in 0||(X-1):
var ii = (i + xshift) mod X
for j in 0||(Y-1):
var jj = (j + yshift) mod Y
for k in 0||(Z-1):
var kk = (k + zshift) mod Z
result[ii, jj, kk] = t[i, j, k]

proc circshift_impl[T](t: Tensor[T], xshift: int, yshift: int): Tensor[T]=
assert(t.rank == 2)
var X = t.shape[0]
var Y = t.shape[1]

result = newTensor[T](t.shape.toSeq)
for i in 0||(X-1):
var ii = (i + xshift) mod X
for j in 0||(Y-1):
var jj = (j + yshift) mod Y
result[ii, jj] = t[i, j]

proc circshift_impl[T](t: Tensor[T], xshift: int): Tensor[T]=
assert(t.rank == 1)
var X = t.shape[0]

result = newTensor[T](t.shape.toSeq)
for i in 0||(X-1):
var ii = (i + xshift) mod X
result[ii] = t[i]

# TODO : Generic implementation in parallel
proc circshift_impl[T](t: Tensor[T], shift: seq[int]): Tensor[T]=
let shape = t.shape.toSeq
result = newTensor[T](t.shape.toSeq)
for coord, values in t:
Expand All @@ -110,6 +144,19 @@ proc circshift*[T](t: Tensor[T], shift: seq[int]): Tensor[T]=
newcoord[i] = (coord[i]+shift[i]) mod shape[i]
result.atIndexMut(newcoord, values)

proc circshift*[T](t: Tensor[T], shift: seq[int]): Tensor[T]=
## Generic Circshift
assert(t.rank == shift.len)
case t.rank
of 1:
result = circshift_impl(t, shift[0])
of 2:
result = circshift_impl(t, shift[0], shift[1])
of 3:
result = circshift_impl(t, shift[0], shift[1], shift[2])
else:
result = circshift_impl(t, shift)

proc fftshift*[T](t: Tensor[T]): Tensor[T]=
## Calculate fftshift using circshift
let xshift = t.shape[0] div 2
Expand Down

0 comments on commit cedba26

Please sign in to comment.