コード例 #1
0
int FormFunctionGradient (TAO_GA_APPLICATION gaapp, GAVec ga_X, double *f, GAVec ga_G, void *ptr)
{
  int lo, hi;			//the global coordinates
  AppCtx *user = (AppCtx *) ptr;
  int i,j;
  double *g, *x;
  double xx,yy,zz,temp,rij;
  

  MA_get_pointer(user->memHandle, &x);
  g = x + user->ndim*user->natoms;
    
  lo=0;
  hi=user->n-1; /* range of array indices */

  NGA_Get(ga_X, &lo, &hi, x, &hi);

  *f = 0;
  for (i=0; i < user->n; i++)
    g[i] = 0.0;

  if (user->ndim == 2) {
    for (j=1; j < user->natoms; j++) {
      for (i=0; i<j; i++) {
	xx = x[2*j] - x[2*i];
	yy = x[2*j+1] - x[2*i+1];
	rij = xx*xx + yy*yy;
	temp = 1.0/rij/rij/rij;
	*f += temp*(temp-2.0);
	temp *= 12.0*(temp-1.0)/rij;
	g[2*j] -= xx*temp;
	g[2*j+1] -= yy*temp;
	g[2*i] += xx*temp;
	g[2*i+1] += yy*temp;
      }
    }
  } else if (user->ndim == 3) {
    for (j=1; j < user->natoms; j++) {
      for (i=0; i < j; i++) {
	xx = x[3*j] - x[3*i];
	yy = x[3*j+1] - x[3*i+1];
	zz = x[3*j+2] - x[3*i+2];
	rij = xx*xx + yy*yy + zz*zz;
	temp = 1.0/rij/rij/rij;
	*f += temp*(temp-2.0);
	temp *= 12.0*(temp-1.0)/rij;
	g[3*j] -= xx*temp;
	g[3*j+1] -= yy*temp;
	g[3*j+2] -= zz*temp;
	g[3*i] += xx*temp;
	g[3*i+1] += yy*temp;
	g[3*i+2] += zz*temp;
      }
    }
  }
      
  NGA_Put(ga_G, &lo, &hi, g, &hi);
  return 0;
}
コード例 #2
0
int InitializeVariables(GAVec ga_X, AppCtx *user) 
{
  double *x;
  double xx, yy, zz;
  int isqrtn, icrtn, left, i, j, k, il, jl, ctr;
  int lo, hi;


  lo = 0; hi = user->n - 1; /* range of array indices */
  MA_get_pointer(user->memHandle, &x);

  NGA_Get (ga_X, &lo, &hi, x, &hi);

  if (user->ndim == 2) {
    isqrtn = (int) sqrt( (double) user->natoms);
    left = user->natoms - isqrtn * isqrtn;
    xx = 0.0;
    yy = 0.0;
    for (j=0; j<=isqrtn + left/isqrtn; j++) {
      for (i=0; i < TaoMin(isqrtn, user->natoms - j*isqrtn); i++) {
	ctr = j*isqrtn + i;
	x[2*ctr] = xx;
	x[2*ctr+1] = yy;
	xx += 1.0;
      }
      yy += 1.0;
      xx = 0.0;
    }	     
  }
  else if (user->ndim == 3) {
    icrtn = (int) pow((user->natoms + 0.5),1.0/3.0);
    left = user->natoms - icrtn * icrtn * icrtn;
    xx = yy = zz = 0.0;
    for (k=0; k <= icrtn + left; k++) {
      jl = TaoMin(icrtn, (user->natoms - k*icrtn*icrtn)/icrtn+1);
      for (j=0; j<jl; j++) {
	il = TaoMin(icrtn, user->natoms - k*icrtn*icrtn - j*icrtn);
	for (i=0; i<il; i++) {
	  ctr = k*icrtn*icrtn + j*icrtn + i;
	  x[3*ctr] = xx;
	  x[3*ctr+1] = yy;
	  x[3*ctr+2] = zz;
	  xx += 1.0;
	}
	yy += 1.0;
	xx = 0.0;
      }
      zz += 1.0;
      yy = 0.0;
    }
  }

  NGA_Put(ga_X, &lo, &hi, x, &hi);
  return 0;
}
コード例 #3
0
/**
 * Block Topology (of Force Matrix): 
 * Say for example: If there are 4 block and 100 atoms, the size of 
 * the force matrix is 100x100 and each block size is 50x50. 
 * 
 *  -----------
 * |     |     |
 * | 0,0 | 0,1 |
 *  -----------
 * | 1,0 | 1,1 |
 * |     |     |
 *  -----------
 */
int SetupBlocks(AppCtx *user)
{
  int i,j,k=0;
  int n;
  int zero = 0;
  int x_space, g_space;

  if (user->natoms % user->BlockSize) {
    GA_Error("Number of atoms should be a multiple of block size. Choose a different block size.", 0L);
  }

  n = user->natoms / user->BlockSize;
  user->nBlocks = n*n;

  if (user->nBlocks > MAX_BLOCKS) 
    GA_Error("Number of blocks is greater that MAX_BLOCKS: Solution is either to increase the defined MAX_BLOCKS or increase your block size",0L);

  if (user->nBlocks < user->nproc)
    GA_Error("Number of blocks should be greater than or equal to the number of processors",0L);

  
  for (i=0;i<n;i++)
    for (j=0;j<n;j++,k++) {
      user->btopo[k].x = i;
      user->btopo[k].y = j;
    }
  
  /* Create task array */
  n = 1;
  user->atomicTask = NGA_Create(C_INT, 1, &n, "Atomic Task", NULL);
  if (!user->atomicTask)
    GA_Error("NGA_Create failed for Atomic Task",0);

  if (user->me == 0) 
    NGA_Put(user->atomicTask, &zero, &zero, &user->nproc, &zero);
  
  
  /* space for x values from two processors */
  x_space = 2 * user->BlockSize * user->ndim;
  /* space for ALL gradient value */
  g_space = user->natoms * user->ndim; 
             

  if (MA_push_stack(C_DBL, x_space + g_space+3, "GA LJ bufs", &user->memHandle))
    MA_get_pointer(user->memHandle, &user->x1);
  else
    GA_Error("ma_alloc_get failed",x_space + g_space);
  
  user->x2  = user->x1 + x_space/2 + 1;
  user->grad = user->x2 + x_space/2 + 1;
  GA_Sync();
  return 0;
}
コード例 #4
0
ファイル: nwchem_wrap.c プロジェクト: bwang2453/nwchem_csx
PyObject *wrap_rtdb_get(PyObject *self, PyObject *args)
{
  int nelem, ma_type;
  char *name;
#define MAXPTRS 2048
  char *ptrs[MAXPTRS];
  PyObject *returnObj = NULL;
  char format_char;
  void *array=NULL;
  int ma_handle;
  
  if (PyArg_ParseTuple(args, "s", &name)) {
    if (!rtdb_seq_ma_get(rtdb_handle, name, &ma_type, &nelem, &ma_handle)) {
      PyErr_SetString(NwchemError, "rtdb_ma_get failed");
      return NULL;
    }
    if (!MA_get_pointer(ma_handle, &array)) {
      PyErr_SetString(NwchemError, "rtdb_ma_get failed");
      return NULL;
    }
//    printf("name=%s ma_type=%d nelem=%d ptr=%x\n",name, ma_type, 
//      nelem, array);
    
    switch (ma_type) {
    case MT_F_INT:
    case MT_INT  : 
    case MT_BASE + 11  : 
      format_char = 'i'; break;
    case MT_F_DBL: 
    case MT_DBL  : 
      format_char = 'd'; break;
      break;
    case MT_CHAR : 
      format_char = 's'; break;
    default:
      PyErr_SetString(NwchemError, "rtdb_get: ma type incorrect");
      (void) MA_free_heap(ma_handle);
      return NULL;
      break;
    }
    
    /* For character string need to build an array of pointers */
    
    if (ma_type == MT_CHAR) {
      char *ptr, *next;
      nelem = 0;
      next = ptr = array;
      while (1) {
        int eos = (*ptr == 0);
        if ((*ptr == '\n') || (*ptr == 0)) {
          *ptr = 0;
          if (nelem >= MAXPTRS) {
            PyErr_SetString(PyExc_MemoryError,"rtdb_get too many strings");
            (void) MA_free_heap(ma_handle);
            return NULL;
          }
          if (strlen(next) > 0) {
            ptrs[nelem] = next;
            nelem++;
          }
          next = ptr+1;
          if (eos) break;
        }
        ptr++;
      }
    }
           
    switch (format_char) {
    case 'i':
      returnObj = nwwrap_integers(nelem, array); break;
    case 'd':
      returnObj = nwwrap_doubles(nelem, array); break;
    case 's':
      returnObj = nwwrap_strings(nelem, ptrs); break;
    }

    (void) MA_free_heap(ma_handle);

    if (!returnObj) {
      PyErr_SetString(PyExc_TypeError, "rtdb_get: conversion to python object failed.");
    }
  }
  else {
    PyErr_SetString(PyExc_TypeError, "Usage: value = rtdb_get(name)");
  }

  return returnObj;
}
コード例 #5
0
int InitializeVariables(GAVec ga_x, AppCtx *user) {
  double *x;
  int lo, hi, n, handle;
  double xx,yy,zz;
  int i,j,k, il, jl, ctr, icrtn, ileft, isqrtn;

  // This method is not parallelized
  if (user->me != 0) {
    GA_Sync();
    return 0;
  }

  n = user->ndim * user->natoms + 1;
  if (MA_push_stack(C_DBL, n, "InitializeVariables buffer", &handle))
    MA_get_pointer(handle, &x);
  else 
    ga_error("ma_alloc_get failed", n);

  lo = 0;
  hi = n-2;

  if (user->ndim == 2) {
    isqrtn = int(sqrt(user->natoms));
    ileft = user->natoms - isqrtn * isqrtn;
    xx = yy = 0.0;
    for (j=0; j <= isqrtn + ileft/isqrtn; j++) {
      for (i=0; i<TaoMin(isqrtn, user->natoms - j * isqrtn); i++) {
	ctr = j * isqrtn + i;
	x[2*ctr] = xx;
	x[2*ctr + 1] = yy;
	xx += 1.0;
      }
      yy += 1.0;
      xx = 0.0;
    }
  } else if (user->ndim == 3) {
    icrtn = (int)pow((int)(user->natoms + 0.5),1.0/3.0);
    ileft = user->natoms - icrtn*icrtn*icrtn;
    xx = yy = zz = 0.0;
    for (k=0; k<=icrtn + ileft/(icrtn*icrtn); k++) {
      jl = TaoMin(icrtn, (user->natoms - k*icrtn*icrtn)/icrtn + 1);
      for (j=0; j<jl; j++) {
	il = TaoMin(icrtn, user->natoms - k*icrtn*icrtn - j*icrtn);
	for (i = 0; i<il; i++) {
	  ctr = k*icrtn*icrtn + j*icrtn + i;
	  x[3*ctr] = xx;
	  x[3*ctr + 1] = yy;
	  x[3*ctr + 2] = zz;
	  xx += 1.0;
	}
	yy += 1.0;
	xx = 0.0;
      }
      zz += 1.0;
      yy = 0.0;
    }
  }

  // Distribute the array
  NGA_Put(ga_x, &lo, &hi, x, &hi);

  if (!MA_pop_stack(handle)) 
    ga_error("InitializeVariables:MA_pop_stack failed",0);

  GA_Sync();

  return 0;
}
コード例 #6
0
ファイル: rtdb.c プロジェクト: wadejong/NWChem-Json
/**
  \brief Retrieve data from the RTDB of unknown size

  Retrieve the data associated with the specified key from the RTDB. 
  In this case the size of the data is not known a priory. Hence the size is
  retrieved from the RTDB as well, an array of the appropriate size is
  allocated, and the MA handle as well as the size are returned. The calling
  program is responsible for deallocating the memory.

  \param handle  [Input] the RTDB handle
  \param name    [Input] the key for the data
  \param ma_type [Output] the type of the data specified by one of the MA data types (see mafdecls.fh)
  \param nelem   [Output] the number of elements of the specified type
  \param array   [Output] the actual data retrieved

  \return the status of the read operation
*/
int rtdb_ma_get(const int handle, const char *name, int *ma_type,
		    int *nelem, int *ma_handle)
{
  int status;
#ifdef GAGROUPS
  int me = GA_Nodeid();
#endif

  if (!verify_parallel_access()) return 0;

  if (handle < 0 || handle >= MAX_RTDB) {
    (void) fprintf(stderr, "rtdb_ma_get: handle out of range %d\n", handle);
    (void) fflush(stderr);
    return 0;
  }
  if (par_mode[handle] == INACTIVE) {
    (void) fprintf(stderr, "rtdb_ma_get: handle not active %d\n",handle);
    (void) fflush(stderr);
    return 0;
  }

  if (par_mode[handle] == RTDB_SEQ_MODE && parallel_mode == RTDB_PAR_MODE) {
    (void) fprintf(stderr, "rtdb_ma_get: seq. open and par. get\n");
    (void) fflush(stderr);
    return 0;
  }

  if (parallel_mode == RTDB_SEQ_MODE || me == 0)
    status = rtdb_seq_ma_get(handle, name, ma_type, nelem, ma_handle);

  if (parallel_mode == RTDB_PAR_MODE) {
    rtdb_broadcast(TYPE_RTDB_STATUS, MT_INT, 1, (void *) &status);
    
    if (status) {
      void *ma_data;
      
      rtdb_broadcast(TYPE_RTDB_TYPE,  MT_INT, 1, (void *) ma_type);
      rtdb_broadcast(TYPE_RTDB_NELEM, MT_INT, 1, (void *) nelem);

      if (me > 0) {
        Integer ma_handle_buf;
        Integer ma_type_buf = *ma_type;
        Integer nelem_buf = *nelem;
	if (!MA_allocate_heap(ma_type_buf, nelem_buf, name, &ma_handle_buf)) {
	  /* Cannot just return and error here since cannot propogate the
	     error condition to all the other processes ... exit via
	     the TCGMSG error routine */

	  GA_Error("rtdb_get_ma: rtdb_get_ma: MA_alloc failed, nelem=", (Integer)nelem);
	}
        *ma_handle = (int) ma_handle_buf;
      }
      
      if (!MA_get_pointer((Integer) *ma_handle, &ma_data)) {
	GA_Error("rtdb_get_ma: rtdb_get_ma: MA_get_ptr failed, nelem=", (Integer) nelem);
      }

      rtdb_broadcast(TYPE_RTDB_ARRAY, *ma_type, *nelem, (void *) ma_data);
    }
  }

  return status;
}
コード例 #7
0
int main(int argc, char *argv[])
{
  int rtdb = -1, ma_type, nelem;
  char filename[256], mode[256], name[1024], date[26];
  void *data;

  char carray[MAX_NELEM+1];
  int iarray[MAX_NELEM];
  double darray[MAX_NELEM];
  
#define N_OPTS 9
  const char *opts[N_OPTS] = {"quit", "open", "close", "info", "put", "get", 
			  "first", "next", "print"};

  if (!MA_init(MT_DBL, -1, -1))
    exit(1);

  while (1) {
     int opt;

     printf("\n\n\n      Interactive RTDB\n      ----------------\n\n");
     for (opt=0; opt<N_OPTS; opt++)
       printf("        %-8s %3d\n", opts[opt], opt);

     printf("\nEnter option number -> "); fflush(stdout);
     if (scanf("%d", &opt) != 1) 
       break;

     switch (opt) {
     case 0:
       exit(0); break;

     case 1:
       printf("\nOpen database\n-----------\n\n");
       printf("Enter filename -> "); fflush(stdout);
       if (scanf("%s",filename) != 1) 
	 exit(1);
       printf("Enter mode (new, old, unknown, empty, scratch) -> ");
       fflush(stdout);
       if (scanf("%s",mode) != 1) 
	 exit(1);
       if (!rtdb_seq_open(filename, mode, &rtdb))
	 printf("\nOpen of %s with mode %s failed\n", filename, mode);
       break;

     case 2:
       printf("\nClose database\n-------------\n\n"); fflush(stdout);
       if (rtdb < 0) 
	 printf("database is not open\n");
       else {
	 printf("Enter mode (keep, delete) -> "); fflush(stdout);
	 if (scanf("%s",mode) != 1) 
	   exit(1);
	 if (!rtdb_seq_close(rtdb, mode))
	   printf("Close of %s with mode %s failed\n", filename, mode);
	 else
	   rtdb = -1;
       }
       break;

     case 3:
       printf("\nInformation on entry\n--------------------\n\n");
       if (rtdb < 0) 
	 printf("database is not open\n");
       else {
	 printf("Enter name -> "); fflush(stdout);
  	 if (!read_string(name, sizeof name))
	   exit(1);

	 if (!rtdb_seq_get_info(rtdb, name, &ma_type, &nelem, date))
	   printf("Get info on \"%s\" failed\n", name);
	 else
	   printf("%s -> type=%s, nelem=%d, date=%s\n", 
		  name, ma_typename(ma_type), nelem, date);
       }
       break;

     case 4:
       printf("\nPut entry\n---------\n\n");
       if (rtdb < 0) 
	 printf("database is not open\n");
       else {
	 printf("Enter name -> "); fflush(stdout);
	 if (!read_string(name, sizeof name))
	   exit(1);
	 printf("Enter type (int, char, double) -> "); fflush(stdout);
	 if (scanf("%s", mode) != 1)
	   exit(1);
	 if (!strcmp(mode,"int")) {
	   int i;
	   if ((nelem = ReadNelem()) <= 0) break;
	   ma_type = MT_INT;
	   for (i=0; i<nelem; i++)
	     if (scanf("%d", iarray+i) != 1)
	       exit(1);
	   data = (void *) iarray;
	 }
	 else if (!strcmp(mode, "char")) {
	   int i;
	   ma_type = MT_CHAR;
	   if (!read_string(carray, MAX_NELEM)) break;
	   nelem = strlen(carray) + 1;
	   data = (void *) carray;
	 }
	 else if (!strcmp(mode, "double")) {
	   int i;
	   if ((nelem = ReadNelem()) <= 0) break;
	   ma_type = MT_DBL;
	   for (i=0; i<nelem; i++)
	     if (scanf("%lf", darray+i) != 1)
	       exit(1);
	   data = (void *) darray;
	 }
	 else {
	   printf("invalid type\n");
	   break;
	 }
	 if(!rtdb_seq_put(rtdb, name, ma_type, nelem, data))
	   printf("put %s, nelem=%d, type=%s failed", name, 
		  nelem, ma_typename(ma_type)); fflush(stdout);
       }
       break;


     case 5:
       printf("\nGet entry\n---------\n\n");
       if (rtdb < 0) 
	 printf("database is not open\n");
       else {
	 int ma_handle;

	 if(rtdb_seq_ma_get(rtdb, name, &ma_type, &nelem, &ma_handle)) {
	   ma_print(stdout, ma_type, nelem, MA_get_pointer(ma_handle, &data));
	   MA_free_heap(ma_handle);
	 }
	 else 
	   printf("Get of %s failed\n", name);
       }
       break;

     case 8:
       printf("\nPrint database\n--------------\n\n");
       if (rtdb < 0) 
	 printf("database is not open\n");
       else {
	 int values;
	 printf("Print values (0=no, 1=yes) -> "); fflush(stdout);
	 if (scanf("%d", &values) != 1)
	   exit(1);
	 rtdb_seq_print(rtdb, values);
       }
       break;
       
     default:
       break;
     }
   }
  return 0;
}