language/scan: fix random scan, add explicit scan, specify what runs on host/device

This commit is contained in:
Sebastien Bourdeauducq 2015-07-19 11:36:52 +02:00
parent deaa492566
commit 937ca853aa
1 changed files with 18 additions and 12 deletions

View File

@ -1,4 +1,6 @@
from random import Random from random import Random, shuffle
from artiq.language.core import *
class LinearScan: class LinearScan:
@ -7,28 +9,32 @@ class LinearScan:
self.max = max self.max = max
self.npoints = npoints self.npoints = npoints
@portable
def _gen(self): def _gen(self):
r = self.max - self.min r = self.max - self.min
d = self.npoints - 1 d = self.npoints - 1
for i in range(self.npoints): for i in range(self.npoints):
yield r*i/d + self.min yield r*i/d + self.min
@portable
def __iter__(self): def __iter__(self):
return self._gen() return self._gen()
class RandomScan: class RandomScan:
def __init__(self, min, max, npoints, seed=0): def __init__(self, min, max, npoints, seed=0):
self.min = min self.sequence = list(LinearScan(min, max, npoints))
self.max = max shuffle(self.sequence, Random(seed).random)
self.npoints = npoints
self.seed = 0
def _gen(self):
prng = Random(self.seed)
r = self.max - self.min
for i in range(self.npoints):
yield prng.random()*r + self.min
@portable
def __iter__(self): def __iter__(self):
return self._gen() return iter(self.sequence)
class ExplicitScan:
def __init__(self, sequence):
self.sequence = sequence
@portable
def __iter__(self):
return iter(self.sequence)