コード例 #1
0
ファイル: sim.c プロジェクト: ckucera3/CS2200
/**
 * Main body of the program.
 *
 * @param argc Number of arguments from the command line.
 * @param argv Array of strings containing the command line arguments.
 * @return The exit status of the program (EXIT_SUCCESS or EXIT_FAILURE).
 */
int main(int argc, char **argv) {
  sim_setoptions(argc, argv);
  sim_init();
  sim_readdata();
  display_statistics();
  sim_free();
  return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: sim-socket.c プロジェクト: ckw1375/mptcp
ssize_t sim_sock_sendmsg (struct SimSocket *socket, const struct msghdr *msg, int flags)
{
  struct socket *kernel_socket = (struct socket *)socket;
  struct iovec *kernel_iov = copy_iovec (msg->msg_iov, msg->msg_iovlen);
  struct msghdr kernel_msg = *msg;
  kernel_msg.msg_flags = flags;
  kernel_msg.msg_iov = kernel_iov;
  int retval = sock_sendmsg (kernel_socket, &kernel_msg, iov_size (msg));
  sim_free (kernel_iov);
  return retval;
}
コード例 #3
0
ファイル: sim-socket.c プロジェクト: ckw1375/mptcp
ssize_t sim_sock_recvmsg (struct SimSocket *socket, struct msghdr *msg, int flags)
{
  struct socket *kernel_socket = (struct socket *)socket;
  struct iovec *kernel_iov = copy_iovec (msg->msg_iov, msg->msg_iovlen);
  struct iovec *user_iov = msg->msg_iov;
  struct cmsghdr *user_cmsgh = msg->msg_control;
  size_t user_cmsghlen = msg->msg_controllen;
  msg->msg_iov = kernel_iov;
  int retval = sock_recvmsg (kernel_socket, msg, iov_size (msg), flags);
  msg->msg_iov = user_iov;
  msg->msg_control = user_cmsgh;
  msg->msg_controllen = user_cmsghlen - msg->msg_controllen;
  sim_free (kernel_iov);
  return retval;
}
コード例 #4
0
ファイル: _discsimmodule.c プロジェクト: JereKoskela/discsim
static void
Simulator_dealloc(Simulator* self)
{
    if (self->sim != NULL) {
        if (self->sim->sample != NULL) {
            PyMem_Free(self->sim->sample);
        }
        if (self->sim->event_classes != NULL) {
            PyMem_Free(self->sim->event_classes);
        }
        sim_free(self->sim);
        PyMem_Free(self->sim);
        self->sim = NULL;
    }
    Py_TYPE(self)->tp_free((PyObject*)self);
}
コード例 #5
0
/**
 * ## Running and timing the simulation
 * 
 * The `lmain` routine is basically analogous to the `main` routine in
 * the C driver.  Rather than taking (positional) command line arguments,
 * it takes a table of named parameters.  This is most easily described
 * by example; see the `params.lua` file included with this code.
 * 
 */
int lmain(lua_State* L)
{
    int narg = lua_gettop(L);
    if (narg != 1 || !lua_istable(L,1)) 
        return luaL_error(L, "Incorrect argument");

    // Set up default values
    int n        = get_lua_int(L, "n",      100);
    int nsteps   = get_lua_int(L, "nsteps", 100);
    int fstep    = get_lua_int(L, "fstep",  10);
    int verbose  = get_lua_int(L, "verbose", 1);
    double a     = get_lua_double(L, "a",  0.0);
    double b     = get_lua_double(L, "b",  1.0);
    double c     = get_lua_double(L, "c",  1.0);
    double dt    = get_lua_double(L, "dt", 0.8*(b-a)/(n-1)/c);

    // Get rank and nprocs
    int proc, nproc;
#ifdef USE_MPI
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank(MPI_COMM_WORLD, &proc);
#else
    proc = 0;
    nproc = 1;
#endif

    // Get file name
    const char* fname = NULL;
    lua_pushstring(L, "fname");
    lua_gettable(L, -2);
    if (lua_isstring(L,-1))
        fname = lua_tostring(L,-1);
    lua_pop(L,1);

    // Open output file
    FILE* fp = NULL;
    if (proc == 0 && fname != NULL) {
        fp = fopen(fname, "w+");
        if (!fp)
            luaL_error(L, "Could not open output file %s", fname);
    }

    // set initial conditions using indicator function
    lua_pushstring(L, "u0");
    lua_gettable(L, -2);
    if (!lua_isfunction(L,-1))
        return luaL_error(L, "u0 appears not to be a function");
    sim_t sim = sim_init(a, b, c, dt, n, proc, nproc, get_lua_fx, L);
    lua_pop(L,1);

    // Write header
    if (fp != NULL)
        sim_write_header(sim, nsteps/fstep, fp);

    // Run the PDE solver loop and time it 
    double start = MPI_Wtime();
    for (int step = 0; step < nsteps; ++step) {
        if (step % fstep == 0 && fname != NULL)
            sim_write_step(sim, fp);
        sim_advance(sim);
    }
    double t_elapsed = MPI_Wtime()-start;

    // Output the configuration and statistics.
    // The CFL condition (tau < 1) is needed for stability
    double dx  = (b-a)/(n-1);
    double tau = c * dt / dx;
    if (proc == 0 && verbose) {
        printf("n: %d\n"
               "nsteps: %d\n"
               "fsteps: %d\n"
               "c: %f\n"
               "dt: %f\n"
               "dx: %f\n"
               "CFL condition satisfied: %d\n"
               "nproc: %d\n"
               "Elapsed time: %f s\n", 
               n, nsteps, fstep, c, dt, dx, (tau < 1.0),
               nproc, t_elapsed);
    }

    if (fp != NULL)
        fclose(fp);
    sim_free(sim);
    
    lua_pushnumber(L, t_elapsed);
    return 1;
}