cexp / cexp / ext / src / userestrict.c

typedef struct userestrict_{
  int num_s, num_n;
  double **x, **a, *b, e;
} UseRestrict;

/* Dynamics:
     x(s+1) = (1 - e) * x(s) + e * (a x(t) + b)
 */

inline void copy(double * restrict dest, double * restrict trg, int num)
{
  int i;
  for (i = 0; i < num; ++i){
    dest[i] = trg[i];
  }
}

/*[[[cog
import cog

template = """
inline double dot_%(method)s(
     double *%(restrict)s a, double *%(restrict)s b, int num)
{
  int i;
  double c = 0;
  for (i = 0; i < num; ++i){
    c += a[i] * b[i];
  }
  return c;
}

inline void run_%(method)s(
    double **%(restrict)s x, double **%(restrict)s a,
    double *%(restrict)s b, double e,
    int num_s, int num_n)
{
  int s, n;
  for (s = 1; s < num_s; ++s){
    for (n = 0; n < num_n; ++n){
      x[s][n] += (1 - e) * x[s-1][n] +
        e * (dot_%(method)s(a[n], x[s-1], num_n) + b[n]);
    }
  }
}

int UseRestrict_run_%(method)s(UseRestrict *self, int repeat)
{
  int i;
  for(i = 0; i < repeat; ++i){
    run_%(method)s(
      self->x, self->a, self->b, self->e, self->num_s, self->num_n);
  }
  copy(self->x[0], self->x[self->num_s - 1], self->num_n);
  return 0;
}
"""

for (method, restrict) in [('normal', ''), ('userestrict', ' restrict')]:
    cog.out(template % dict(method=method, restrict=restrict))
]]]*/

inline double dot_normal(
     double * a, double * b, int num)
{
  int i;
  double c = 0;
  for (i = 0; i < num; ++i){
    c += a[i] * b[i];
  }
  return c;
}

inline void run_normal(
    double ** x, double ** a,
    double * b, double e,
    int num_s, int num_n)
{
  int s, n;
  for (s = 1; s < num_s; ++s){
    for (n = 0; n < num_n; ++n){
      x[s][n] += (1 - e) * x[s-1][n] +
        e * (dot_normal(a[n], x[s-1], num_n) + b[n]);
    }
  }
}

int UseRestrict_run_normal(UseRestrict *self, int repeat)
{
  int i;
  for(i = 0; i < repeat; ++i){
    run_normal(
      self->x, self->a, self->b, self->e, self->num_s, self->num_n);
  }
  copy(self->x[0], self->x[self->num_s - 1], self->num_n);
  return 0;
}

inline double dot_userestrict(
     double * restrict a, double * restrict b, int num)
{
  int i;
  double c = 0;
  for (i = 0; i < num; ++i){
    c += a[i] * b[i];
  }
  return c;
}

inline void run_userestrict(
    double ** restrict x, double ** restrict a,
    double * restrict b, double e,
    int num_s, int num_n)
{
  int s, n;
  for (s = 1; s < num_s; ++s){
    for (n = 0; n < num_n; ++n){
      x[s][n] += (1 - e) * x[s-1][n] +
        e * (dot_userestrict(a[n], x[s-1], num_n) + b[n]);
    }
  }
}

int UseRestrict_run_userestrict(UseRestrict *self, int repeat)
{
  int i;
  for(i = 0; i < repeat; ++i){
    run_userestrict(
      self->x, self->a, self->b, self->e, self->num_s, self->num_n);
  }
  copy(self->x[0], self->x[self->num_s - 1], self->num_n);
  return 0;
}
/*[[[end]]]*/
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.