GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
device/modular.c.h
Go to the documentation of this file.
1 
2  /*
3  ***********************************************************************
4  Copyright (c) 2015 Advanced Micro Devices, Inc.
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10 
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13 
14  2. Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions and the following disclaimer in the
16  documentation and/or other materials provided with the distribution.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30  ***********************************************************************
31  */
32 
48 #pragma once
49 #ifndef PRIVATE_MODULAR_CH
50 
51 #ifndef MODULAR_NUMBER_TYPE
52 #error "MODULAR_NUMBER_TYPE must be defined"
53 #endif
54 
55 #ifdef MODULAR_FIXED_SIZE
56 # define N MODULAR_FIXED_SIZE
57 # define MATRIX_ELEM(mat, i, j) (mat[i][j])
58 #else
59 # define MATRIX_ELEM(mat, i, j) (mat[i * N + j])
60 #endif // MODULAR_FIXED_SIZE
61 
63 #if 1
64 #define modMult(a, s, c, m) ((MODULAR_NUMBER_TYPE)(((cl_ulong) a * s + c) % m))
65 #else
66 static MODULAR_NUMBER_TYPE modMult(MODULAR_NUMBER_TYPE a, MODULAR_NUMBER_TYPE s, MODULAR_NUMBER_TYPE c, MODULAR_NUMBER_TYPE m)
67 {
68  MODULAR_NUMBER_TYPE v;
69  v = (MODULAR_NUMBER_TYPE) (((cl_ulong) a * s + c) % m);
70  return v;
71 }
72 #endif
73 
74 
76 // @details Also works if v = s.
77 // @return v = A*s % m
78 #ifdef MODULAR_FIXED_SIZE
79 static void modMatVec (MODULAR_NUMBER_TYPE A[N][N], MODULAR_NUMBER_TYPE s[N], MODULAR_NUMBER_TYPE v[N], MODULAR_NUMBER_TYPE m)
80 #else
81 void modMatVec (size_t N, MODULAR_NUMBER_TYPE* A, MODULAR_NUMBER_TYPE* s, MODULAR_NUMBER_TYPE* v, MODULAR_NUMBER_TYPE m)
82 #endif
83 {
84  MODULAR_NUMBER_TYPE x[MODULAR_FIXED_SIZE]; // Necessary if v = s
85  for (size_t i = 0; i < N; ++i) {
86  x[i] = 0;
87  for (size_t j = 0; j < N; j++)
88  x[i] = modMult(MATRIX_ELEM(A,i,j), s[j], x[i], m);
89  }
90  for (size_t i = 0; i < N; ++i)
91  v[i] = x[i];
92 }
93 
94 #undef MATRIX_ELEM
95 #undef N
96 
97 #endif
void modMatVec(size_t N, MODULAR_NUMBER_TYPE *A, MODULAR_NUMBER_TYPE *s, MODULAR_NUMBER_TYPE *v, MODULAR_NUMBER_TYPE m)
Matrix-vector modular multiplication.
Definition: device/modular.c.h:81
#define MATRIX_ELEM(mat, i, j)
Definition: device/modular.c.h:59
#define modMult(a, s, c, m)
Compute (a*s + c) % m.
Definition: device/modular.c.h:64
m
Definition: genn_model.py:117