コード例 #1
0
ファイル: message.c プロジェクト: nelk/kernel
int8_t k_sendDelayedMessage(MessageInfo *messageInfo, ClockInfo *clockInfo, MemInfo *memInfo, ProcInfo *procInfo, Envelope *envelope, ProcId srcPid, ProcId dstPid, uint32_t delay) {
    uint8_t status = 0;

    k_zeroEnvelope(envelope);
    // Check pid - the src is from the bridge, but why not...
    if (k_getPCB(procInfo, srcPid) == NULL || k_getPCB(procInfo, dstPid) == NULL) {
        return EINVAL;
    }

    status = k_changeOwner(memInfo, (uint32_t)envelope, srcPid, PROC_ID_KERNEL);
    if (status != SUCCESS) {
        return status;
    }

    envelope->sendTime = k_getTime(clockInfo) + delay;
    envelope->srcPid = srcPid;
    envelope->dstPid = dstPid;

    mpqAdd(&(messageInfo->mpq), envelope);
    return 0;
}
コード例 #2
0
ファイル: message.c プロジェクト: nelk/kernel
void k_processDelayedMessages(MessageInfo *messageInfo, ProcInfo *procInfo, MemInfo *memInfo, ClockInfo *clockInfo) {
    MessagePQ *messageQueue = &(messageInfo->mpq);
    Envelope *message = NULL;
    uint32_t currentTime = k_getTime(clockInfo);

    if (messageQueue == NULL || messageQueue->size <= 0) {
        return;
    }

    while (1) {
        message = mpqTop(messageQueue);

        if (message == NULL || message->sendTime > currentTime) {
            return;
        }

        mpqRemove(messageQueue, 0);
        k_changeOwner(memInfo, (uint32_t)message, PROC_ID_KERNEL, message->srcPid);
        k_sendMessage(memInfo, procInfo, message, message->srcPid, message->dstPid);
    }
}
コード例 #3
0
double evaluate_derivatives(int n, int m, double* x, int* options) {
  int order = options[0];
  int nnz;
  double t1 = k_getTime();
  if (options[1] == 0) { // Teed = new double*[n];
    assert(m == 1);
    double** seed = new double*[n];
    for (int i = 0; i < n; i++) {
      seed[i] = new double[n];
      for (int j = 0; j < n; j++) {
        seed[i][j] = ((i==j)?1.0:0.0);
      }
    }
    int dim = binomi(n+order, order);
    double** tensorhelp = myalloc2(1, dim);
    tensor_eval(TAG, 1, n, order, n, x, tensorhelp, seed);
    for (int i = 0; i < n; i++) {
      delete[] seed[i];
    }
    delete[] seed; 
    myfree2(tensorhelp);
  } else {
    if (order == 2)  { // Hessian
      assert(m == 1);
      if (options[1] == 1 || options[1] == 2) { // Direct or Indirect
        int opt[2] = {0, 0}; // default is indirect;
        if (options[1] == 1) {opt[0] = 1;} // set direct;
        unsigned int * rind = NULL;
        unsigned int * cind = NULL;
        double * values = NULL;
        sparse_hess(TAG, n, 0, x, &nnz, &rind, &cind, &values, opt);
#ifdef PRINT_RESULT
        for (int i = 0; i < nnz; i++) {
          printf("H[%d, %d] = %.6f\n", rind[i], cind[i], values[i]);
        }
#endif
        free(rind);
        free(cind);
        free(values);
      } else if (options[1] == 3) { // FullHess
        double** H = new double*[n];
        for (int i = 0; i < n; i++) {
          H[i] = new double[n];
        }
        hessian(TAG, n, x, H);
        nnz = n*n;
#ifdef PRINT_RESULT
        for (int i = 0; i < n; i++) {
          for (int j = 0; j <= i; j++) {
            printf("H[%d, %d] = %.6f\n", i, j, H[i][j]);
          }
        }
#endif
        for (int i = 0; i < n; i++) {
          delete[] H[i];
        }
        delete[] H;
      } else if (options[1] == 4) { // Single Hv
        double v[n];
        double Hv[n];
        for (int i = 0; i < n; i++) {
          v[i] = 1.0;
          Hv[i] = 0.0;
        }
        hess_vec(TAG, n, x, v, Hv);
        nnz = n;
      } else if (options[1] == 5) { // dense second order reverse
        double** H = new double*[n];
        for (int i = 0; i < n; i++) {
          H[i] = new double[n];
        }
        hessian_dense(TAG, n, x, H);
        nnz = n*n;
#ifdef PRINT_RESULT
        for (int i = 0; i < n; i++) {
          for (int j = 0; j <= i; j++) {
            printf("H[%d, %d] = %.6f\n", i, j, H[i][j]);
          }
        }
#endif
        for (int i = 0; i < n; i++) {
          delete[] H[i];
        }
        delete[] H;
      } else if (options[1] == 6){ // sparse second order reverse
        unsigned int * rind = NULL;
        unsigned int * cind = NULL;
        double * values = NULL;
        hessian_sparse(TAG, n, x, &nnz, &rind, &cind, &values);
#ifdef PRINT_RESULT
        for (int i = 0; i < nnz; i++) {
          printf("H[%d, %d] = %.6f\n", rind[i], cind[i], values[i]);
        }
#endif
        free(rind);
        free(cind);
        free(values);
      } else if (options[1] == 7) { // Hess-matrix options
        double** H  = myalloc2(n, n);
        double y;
        double*** Xppp = myalloc3(n, n, 1);
        double*** Yppp = myalloc3(1, n, 1);
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
             Xppp[i][j][0] = 0;
          }
          Xppp[i][i][0] = 1.0;
        }
        double** Upp = myalloc2(1,2);
        Upp[0][0] = 1; Upp[0][1] = 0;
        double*** Zppp = myalloc3(n, n, 2);
        int ret_val = hov_wk_forward(TAG,1,n,1,2,n,x,Xppp,&y,Yppp);
        ret_val = hos_ov_reverse(TAG,1,n,1,n,Upp,Zppp);
        for (int i = 0; i < n; ++i) {
          for (int l = 0; l < n; ++l) {
            H[l][i] = Zppp[i][l][1];
          }
        }
#ifdef PRINT_RESULT
        for (int i = 0; i < n; i++) {
          for (int j = 0; j <= i; j++) {
            printf("H[%d, %d] = %.6f\n", i, j, H[i][j]);
          }
        }
#endif
        myfree2(H);
        myfree3(Xppp);
        myfree3(Yppp);
        myfree2(Upp);
        myfree3(Zppp);
      }
    } else if (order == 1) { // Gradient or Jacobian
      if (m == 1) { // gradient
        double g[n];
        gradient(TAG, n, x, g);
#ifdef PRINT_RESULT
        for (int i = 0; i < n; i++) {
          printf("g[%d] = %.6f\n", i, g[i]);
        }
#endif
      } else { // jacobian
        double** J = new double*[m];
        for (int i = 0; i < m; i++) {
          J[i] = new double[n];
        }
        jacobian(TAG, m, n, x, J);
#ifdef PRINT_RESULT
        for (int i = 0; i < m; i++) {
          for (int j = 0; j < n; j++) {
            printf("J[%d][%d] = %.6f\n", i, j, J[i][j]);
          }
        }
#endif
        for (int i = 0; i < m; i++) {
          delete[] J[i];
        }
        delete[] J;
      }
      nnz = n*m;
    }
  }


  double time_elapsed = k_getTime() - t1;
  size_t size;
  size_t** tind;
  double* values;
  printf("ADOLC nnz[%d] method[%d] order[%d] timing = %.6f\n", nnz, options[1], options[0], time_elapsed);
  return time_elapsed;
}
コード例 #4
0
double evaluate_derivatives(int n, int m, double* x) {
  typedef ctaylor<double, ORDER> ttype;
  ttype* xad = new ttype[n];
  ttype* yad = new ttype[m];
  for (int i = 0; i < n; i++) {
    xad[i] = x[i];
  }

  double t1 = k_getTime();
  if (METHOD == 0) {
    if (ORDER == 1) {
      for (int i = 0; i < n; i++) {
        xad[i].set(VAR0, 1);
        func_eval<ttype>(n, xad, m, yad);
        xad[i].set(VAR0, 0);
      }
    } else if (ORDER == 2) {
      for (int i = 0; i < n; i++) {
        xad[i].set(VAR0, 1);
        for (int j = 0; j <= i; j++) {
          xad[j].set(VAR1, 1);
          func_eval<ttype>(n, xad, m, yad);
          xad[j].set(VAR1, 0);
        }
        xad[i].set(VAR0, 0);
      }
    } else if (ORDER == 3) {
      for (int i = 0; i < n; i++) {
        xad[i].set(VAR0, 1);
        for (int j = 0; j <= i; j++) {
          xad[j].set(VAR1, 1);
          for (int k = 0; j <= k; k++) {
            xad[k].set(VAR2, 1);
            func_eval<ttype>(n, xad, m, yad);
            xad[k].set(VAR2, 0);
          }
          xad[j].set(VAR1, 0);
        }
        xad[i].set(VAR0, 0);
      }
    } else {
      std::cout << "Up to third order for libtaylor" << std::endl;
    }
  } else {
    std::ifstream inf("sparsity.txt", std::ifstream::in);
    size_t order;
    size_t size;
    inf >> order >> size;
    assert(order == ORDER);
    std::cout << "SpTaylor with " << size << " nonzeros." << std::endl;
    if (ORDER == 1) {
    } else if (ORDER == 2) {
      size_t tind[2];
      for (int i = 0; i < size; i++) {
        inf >> tind[0] >> tind[1];
        xad[tind[0]].set(VAR0, 1);
        xad[tind[1]].set(VAR1, 1);
        func_eval<ttype>(n, xad, m, yad);
        xad[tind[0]] = x[tind[0]];
        xad[tind[1]] = x[tind[1]];
      }
    } else if (ORDER == 3) {