Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
random.pyx
Go to the documentation of this file.
1 import os, sys
2 import numpy as np
3 sys.path.insert(0, os.path.abspath("."))
4 
5 cdef extern from "ctf.hpp" namespace "CTF_int":
6  void init_rng(int seed)
7 
8 cdef extern from "ctf.hpp" namespace "CTF":
9  cdef cppclass World:
10  int rank, np;
11  World()
12  World(int)
13  World & get_universe()
14 
15 def seed(seed):
16  """
17  seed(seed)
18  Seed the random tensor generator.
19 
20  Parameters
21  ----------
22  seed: int
23  Seed for random. Each process has the seed with `seed + get_universe().rank`.
24  """
25 
26  init_rng(seed+get_universe().rank)
27 
28 def all_seed(seed):
29  """
30  all_seed(seed)
31  Seed the random tensor generator with the same seed in all processes.
32 
33  Parameters
34  ----------
35  seed: int
36  Seed for random.
37  """
38  init_rng(seed)
39 
40 def random(shape, sp=None, p=None, dtype=None):
41  """
42  random(shape, sp=None, p=None, dtype=None)
43  Return random float (in half-open interval [0.0, 1.0)) tensor with specified parameters. Result tensor is from the continuous uniform distribution over the interval.
44 
45  Parameters
46  ----------
47  shape: tensor_like
48  Input tensor with 1-D or 2-D dimensions. If A is 1-D tensor, return a 2-D tensor with A on diagonal.
49 
50  sp: bool, optional
51  When sp is specified True, the output tensor will be sparse.
52 
53  p: float, optional
54  When sp is True, p specifies the fraction of sparsity for the sparse tensor.
55 
56  dtype: data-type, optional
57  Not supportted in current CTF Python.
58 
59  Returns
60  -------
61  output: tensor
62  Random float tensor.
63 
64  Examples
65  --------
66  >>> import ctf
67  >>> import ctf.random as random
68  >>> random.random([2, 2])
69  array([[0.95027513, 0.79755613],
70  [0.27834548, 0.55310684]])
71  """
72  import ctf
73  if dtype is None:
74  dtype = np.float64
75  if sp is None or sp == False:
76  A = ctf.tensor(shape)
77  A.fill_random()
78  else:
79  if p is None:
80  p = 0.1
81  A = ctf.tensor(shape, sp=True)
82  A.fill_sp_random(frac_sp=p)
83  return A
84 
def random(shape, sp=None, p=None, dtype=None)
Definition: random.pyx:40
void init_rng(int rank)
initialized random number generator
Definition: common.cxx:23
CTF::World World
Definition: back_comp.h:7
def seed(seed)
Definition: random.pyx:15
World & get_universe()
Definition: world.cxx:309
def all_seed(seed)
Definition: random.pyx:28