benchmark_rnn / rnn_opt2.c

#include <stdlib.h>
#include <math.h>

typedef struct __RNN__{
  int num_c, num_s;
  double *wcc, *bc, *ec, *uc, *xc;
} RNN;

#define Wcc(i,j) self->wcc[ self->num_c*(i) + (j) ]
#define Bc(i)    self->bc[(i)]
#define Ec(i)    self->ec[(i)]
#define Uc(i,j)  self->uc[ self->num_c*(i) + (j) ]
#define Xc(i,j)  self->xc[ self->num_c*(i) + (j) ]

void RNN_fptt(RNN *self)
{
  int s0, c0, c1;
  double wccxc_bc;

  s0 = 0;
  for (c0 = 0; c0 < self->num_c; ++c0){
    Xc(s0,c0) = tanh(Uc(s0,c0));
  }
  for (s0 = 1; s0 < self->num_s; ++s0){
    for (c0 = 0; c0 < self->num_c; ++c0){
      Uc(s0,c0) = (1 - Ec(c0)) * Uc(s0-1,c0);
      wccxc_bc = Bc(c0);
      for (c1 = 0; c1 < self->num_c; ++c1){
        wccxc_bc += Wcc(c0,c1) * Xc(s0-1,c1);
      }
      Uc(s0,c0) += Ec(c0) * wccxc_bc;
      Xc(s0,c0) = tanh(Uc(s0,c0));
    }
  }
}

void RNN_init(RNN *self)
{
  int c0, c1;

  for (c0 = 0; c0 < self->num_c; ++c0){
    for (c1 = 0; c1 < self->num_c; ++c1){
      Wcc(c0,c1) = 0.1;
    }
    Ec(c0) = 0.1;
    Bc(c0) = 0.1;
    Uc(0,c0) = 0;
    Xc(0,c0) = 0;
  }
}

int main(int argc, char *argv[])
{
  int nc = atoi((argv[1]));
  int ns = atoi((argv[2]));
  int i, repeat_times = atoi(argv[3]);
  RNN rnn;
  double wcc[nc*nc], bc[nc], ec[nc], uc[ns*nc], xc[ns*nc];

  rnn.num_s = ns;
  rnn.num_c = nc;
  rnn.ec  = ec;
  rnn.bc  = bc;
  rnn.wcc = wcc;
  rnn.uc  = uc;
  rnn.xc  = xc;

  RNN_init(&rnn);
  for (i = 0; i < repeat_times; ++i){
    RNN_fptt(&rnn);
  }

  return 0;
}
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.