/********************************************************* * Leap Frog (step) (Stormer -Verlet method) *********************************************************/ void leapfrogStep( FLOAT *x0 , FLOAT *y0 , FLOAT*z0 , FLOAT *vx0 , FLOAT *vy0 , FLOAT *vz0 , FLOAT delta_t , int n_masses){ //accelerations FLOAT *ax0, *ay0, *az0; ax0 = getmemory(n_masses); ay0 = getmemory(n_masses); az0 = getmemory(n_masses); FLOAT *acc; acc = getmemory(3); //La Magia int i; for( i = 0 ; i<n_masses ; i++ ){ acc = accel(i, x0, y0, z0, n_masses); ax0[i] = acc[0]; ay0[i] = acc[1]; az0[i] = acc[2]; x0[i] = x0[i] + delta_t*( vx0[i] + acc[0]*(delta_t/2.0) ); y0[i] = y0[i] + delta_t*( vy0[i] + acc[1]*(delta_t/2.0) ); z0[i] = z0[i] + delta_t*( vz0[i] + acc[2]*(delta_t/2.0) ); } for( i = 0 ; i<n_masses ; i++ ){ acc = accel(i, x0, y0, z0, n_masses); vx0[i] = vx0[i] + (delta_t/2.0)*(ax0[i] + acc[0]); vy0[i] = vy0[i] + (delta_t/2.0)*(ay0[i] + acc[1]); vz0[i] = vz0[i] + (delta_t/2.0)*(az0[i] + acc[2]); } }
/********************************************************* * Aceleracion de la i-esima particula *********************************************************/ FLOAT *accel(int i, FLOAT *x, FLOAT *y, FLOAT *z, int n_masses){ int j; FLOAT ax, ay, az; ax = 0; ay = 0; az = 0; FLOAT *acc; acc = getmemory(3); FLOAT rijsq; for( j = 0 ; j<n_masses ; j++){ if(i!=j){ rijsq = pow(x[i] - x[j], 2 ) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2 ) + EPSILON; ax = ax + (x[j] - x[i])/pow(rijsq , 3.0/2.0); ay = ay + (y[j] - y[i])/pow(rijsq , 3.0/2.0); az = az + (z[j] - z[i])/pow(rijsq , 3.0/2.0); } } acc[0] = G_GRAV*ax; acc[1] = G_GRAV*ay; acc[2] = G_GRAV*az; return acc; }
int main(int argc, char *argv[]) { char *str = NULL; getmemory(&str); printf("%s\n", str); free(str); char szstr[10]; strcpy(szstr,"0123456789"); // 结果未知 printf("%s\n", szstr); return 0; }
int main(void) { char *str=NULL; getmemory(&str,100); strcpy(str,"hello"); free(str); if(str!=NULL) { strcpy(str,"world"); } printf("str is %s\n",str); return 0; }
/*------------------------------------------------------------------------ * xsh_consumemory - consume memory but not release it * //program introduced to avoid freeing stack space for process consumememory - assignment 6 Text problem 9.4 *------------------------------------------------------------------------ */ shellcmd xsh_consumemory(int nargs, char *args[]) { int *heapmemvar = getmemory(sizeof(int)); /* Output info for '--help' argument */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s\n\n", args[0]); printf("Description:\n"); printf("\tConsumes memory\n"); printf("\t--help\tdisplay this help and exit\n"); return 0; } /* Check argument count */ if (nargs > 1) { fprintf(stderr, "%s: too many arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return 1; } if (nargs == 1) { intmask mask; /* Saved interrupt mask */ mask = disable(); pid32 mypid = getpid(); struct procent myproc = proctab[mypid]; myproc.nofreestkflag = 1; char *value = (char)getmemory(sizeof(char)); kprintf("consumed memory\n"); restore(mask); return 0; } return 0; }
int main() { char *str = NULL; str = getmemory(); printf("%s\n",str); free(str); char *c; c = "cxd2"; fanc(c); printf("%s\n",c); char x[] = "cxd3"; fanc1(x); printf("%s\n",x); char d[] = "cxd4"; fanc2(d); printf("%s\n",d); return 0; }
MP_GLOBAL void * __mp_memalloc(memoryinfo *i, size_t *l, size_t a, int u) { void *p; #if MP_ARRAY_SUPPORT || TARGET == TARGET_UNIX void *t; unsigned long n; #endif /* MP_ARRAY_SUPPORT && TARGET */ if (*l == 0) *l = 1; #if MP_ARRAY_SUPPORT || TARGET == TARGET_UNIX || TARGET == TARGET_NETWARE /* Round up the size of the allocation to a multiple of the system page * size. */ *l = __mp_roundup(*l, i->page); #elif TARGET == TARGET_WINDOWS /* The VirtualAlloc() function on Windows only seems to allocate memory in * blocks of 65536 bytes, so we round up the size of the allocation to this * amount since otherwise the space would be wasted. */ *l = __mp_roundup(*l, 0x10000); #elif TARGET == TARGET_AMIGA /* We aren't guaranteed to allocate a block of memory that is page * aligned on the Amiga, so we have to assume the worst case scenario * and allocate more memory for the specified alignment. */ if (a > i->page) a = i->page; if (a > MEM_BLOCKSIZE) *l += __mp_poweroftwo(a) - MEM_BLOCKSIZE; #endif /* MP_ARRAY_SUPPORT && TARGET */ #if MP_ARRAY_SUPPORT || TARGET == TARGET_UNIX /* UNIX has a contiguous heap for a process, but we are not guaranteed to * have full control over it, so we must assume that each separate memory * allocation is independent. If we are using sbrk() to allocate memory * then we also try to ensure that all of our memory allocations are blocks * of pages. */ #if MP_MMAP_SUPPORT /* Decide if we are using mmap() or sbrk() to allocate the memory. Requests * for user memory will be allocated in the opposite way to internal memory. */ if ((((i->flags & FLG_USEMMAP) != 0) == (u != 0)) && (i->mfile != -1)) u = 1; else u = 0; if (u != 0) { #if MP_MMAP_ANONYMOUS if ((p = mmap(NULL, *l, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == (void *) -1) #else /* MP_MMAP_ANONYMOUS */ if ((p = mmap(NULL, *l, PROT_READ | PROT_WRITE, MAP_PRIVATE, i->mfile, 0)) == (void *) -1) #endif /* MP_MMAP_ANONYMOUS */ p = NULL; } else #endif /* MP_MMAP_SUPPORT */ { if (((t = getmemory(0)) == (void *) -1) || ((p = getmemory(*l)) == (void *) -1)) p = NULL; else { if (p < t) /* The heap has grown down, which is quite unusual except on * some weird systems where the stack grows up. */ n = (unsigned long) p - __mp_rounddown((unsigned long) p, i->page); else { t = p; n = __mp_roundup((unsigned long) p, i->page) - (unsigned long) p; } if (n > 0) { /* We need to allocate a little more memory in order to make the * allocation page-aligned. */ if ((p = getmemory(n)) == (void *) -1) { /* We failed to allocate more memory, but we try to be nice * and return our original allocation back to the system. */ getmemory(-*l); p = NULL; } else if (p >= t) p = (char *) t + n; } } } #elif TARGET == TARGET_AMIGA p = AllocMem(*l, MEMF_ANY | MEMF_CLEAR); #elif TARGET == TARGET_WINDOWS /* The VirtualProtect() function won't allow us to protect a range of pages * that span the allocation boundaries made by VirtualAlloc(). As mpatrol * tries to merge all bordering free memory areas, we must prevent the * pages allocated by different calls to VirtualAlloc() from being merged. * The easiest way to do this is to reserve a page of virtual memory after * each call to VirtualAlloc() since this won't actually take up any * physical memory. It's a bit of a hack, though! */ p = VirtualAlloc(NULL, *l, MEM_COMMIT, PAGE_READWRITE); VirtualAlloc(NULL, 0x10000, MEM_RESERVE, PAGE_NOACCESS); #elif TARGET == TARGET_NETWARE p = NXPageAlloc(*l / i->page, 0); #endif /* MP_ARRAY_SUPPORT && TARGET */ #if MP_ARRAY_SUPPORT || TARGET == TARGET_UNIX || TARGET == TARGET_NETWARE /* UNIX's sbrk() and Netware's NXPageAlloc() do not zero the allocated * memory, so we do this here for predictable behaviour. This is also the * case if we are using a simulated heap. */ #if MP_MMAP_SUPPORT if ((p != NULL) && (u == 0)) #else /* MP_MMAP_SUPPORT */ if (p != NULL) #endif /* MP_MMAP_SUPPORT */ __mp_memset(p, 0, *l); #endif /* MP_ARRAY_SUPPORT && TARGET */ if (p == NULL) errno = ENOMEM; return p; }
int main (int argc, char **argv) { extern char* optarg; extern int optind, optopt; int c, errflg, backflg, incrflg, nopagflg, pagflg, rank, nranks; hid_t fapl, file, group, group1, dset; size_t incr, page; unsigned iter, level, igroup, idset, maxiter; char name[MAX_LEN]; char g1name[MAX_LEN]; char g2name[MAX_LEN]; hsize_t dims[3] = { X, Y, Z }; float *buf; double start, stop; double t1, t2; char cdum[MAX_LEN]; int i, memtotal, memfree, buffers, cached, swapcached, active, inactive; float fdum; assert (MPI_Init (&argc, &argv) >= 0); assert (MPI_Comm_rank (MPI_COMM_WORLD, &rank) >= 0); assert (MPI_Comm_size (MPI_COMM_WORLD, &nranks) >= 0); /* parse arguments */ backflg = errflg = incrflg = nopagflg = pagflg = 0; incr = INCREMENT; page = PAGE_SIZE; maxiter = MAX_ITER; while ((c = getopt(argc, argv, ":bni:t:p:")) != -1) { switch (c) { case 'b': backflg++; break; case 'i': incrflg++; incr = (size_t)atol(optarg); if (incr == 0) { fprintf(stderr, "Option -%c requires a positive integer argument\n", optopt); errflg++; } break; case 'n': nopagflg++; break; case 'p': pagflg++; page = (size_t)atol(optarg); if (page == 0) { fprintf(stderr, "Option -%c requires a positive integer argument\n", optopt); errflg++; } break; case 't': maxiter = (unsigned)atol(optarg); if (maxiter == 0) { fprintf(stderr, "Option -%c requires a positive integer argument\n", optopt); errflg++; } break; case ':': /* -i or -p without operand */ fprintf(stderr, "Option -%c requires an operand\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); errflg++; break; default: errflg++; break; } } if (errflg) { if (rank == 0) { fprintf(stderr, "usage: . . . "); fprintf(stderr, "usage: h5core [OPTIONS]\n"); fprintf(stderr, " OPTIONS\n"); fprintf(stderr, " -b Write file to disk on exit\n"); fprintf(stderr, " -i I Memory buffer increment size in bytes [default: 512 MB]\n"); fprintf(stderr, " -n Disable write (to disk) paging\n"); fprintf(stderr, " -p P Page size in bytes [default: 64 MB]\n"); fprintf(stderr, " -t T Number of iterations [default: 5]\n"); fprintf(stderr, "\n"); fflush(stderr); } exit(2); } if ((backflg == 0) && (pagflg > 0)) { if (rank == 0) { fprintf(stderr, "The -p has no effect without the -b option"); fflush(stderr); } } if (pagflg && nopagflg) { if (rank == 0) { fprintf(stderr, "The -n and -p options are mutually exclusive."); fflush(stderr); } exit(3); } /* Let's go! */ fapl = H5Pcreate (H5P_FILE_ACCESS); assert (fapl >= 0); assert (H5Pset_libver_bounds (fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) >= 0); assert (H5Pset_fapl_core (fapl, incr, (hbool_t) backflg) >= 0); if (nopagflg == 0) /* the user didn't disable paging */ { assert (H5Pset_core_write_tracking (fapl, 1, page) >= 0); } if (rank == 0) { printf("\n"); printf("Write to disk: %s\n", (backflg > 0) ? "YES" : "NO"); printf("Increment size: %ld [bytes]\n", incr); if (nopagflg == 0) { printf("Page size: %ld [bytes]\n", page); } else { printf("Page size: PAGING DISABLED!\n"); } printf("Iterations: %d\n", maxiter); printf("\n"); fflush(stdout); } buf = (float *) malloc (X * Y * Z * sizeof (float)); // Get basic memory info on each node, see how memory usage changes // after flushing to disk; do we end up back where we started? getmemory (&memtotal, &memfree, &buffers, &cached, &swapcached, &active, &inactive); if (rank == 0) { fprintf (stdout, "BEFORE RUNNING:\n"); fflush (stdout); } for (i = 0; i < nranks; i++) { if (rank == i) { printmemory (rank, memtotal, memfree, buffers, cached, swapcached, active, inactive); } MPI_Barrier (MPI_COMM_WORLD); } start = MPI_Wtime (); /* outer loop - simulated time */ for (iter = 0; iter < maxiter; ++iter) { if (rank == 0) { printf ("Iteration: %u\n", iter); assert (fflush (stdout) == 0); } assert (sprintf (name, "rank%05dtime%04u.h5", rank, iter) > 0); file = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); assert (file >= 0); t1 = MPI_Wtime (); /* time level */ for (level = 0; level < MAX_TIME_LEVEL; ++level) { assert (sprintf (g1name, "level%03d", level) > 0); group = H5Gcreate2 (file, g1name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); assert (group >= 0); /* group level */ for (igroup = 0; igroup < MAX_GROUPS; ++igroup) { assert (sprintf (g2name, "group%02d", igroup) > 0); group1 = H5Gcreate2 (group, g2name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); assert (group1 >= 0); /* dataset level */ for (idset = 0; idset < MAX_DATASETS; ++idset) { assert (sprintf (name, "dataset%02d", idset) > 0); assert (H5LTmake_dataset_float (group1, name, RANK, dims, buf) >= 0); #ifdef DEBUG assert (fprintf (stderr, "rank %i time %i Wrote dataset %s/%s/%s\n", rank, level, g1name, g2name, name)); assert (fflush (stderr) == 0); #endif } assert (H5Gclose (group1) >= 0); } assert (H5Gclose (group) >= 0); } t2 = MPI_Wtime (); assert (fprintf (stdout, "rank %04i Total time for buffering chunks to memory:\t %f seconds\n", rank, t2 - t1)); assert (fflush (stdout) == 0); MPI_Barrier (MPI_COMM_WORLD); // To keep output less jumbled getmemory (&memtotal, &memfree, &buffers, &cached, &swapcached, &active, &inactive); if (rank == 0) { fprintf (stdout, "BEFORE FLUSHING:\n"); fflush (stdout); } for (i = 0; i < nranks; i++) { if (rank == i) { printmemory (rank, memtotal, memfree, buffers, cached, swapcached, active, inactive); } MPI_Barrier (MPI_COMM_WORLD); } /* close the file */ t1 = MPI_Wtime (); assert (H5Fclose (file) >= 0); t2 = MPI_Wtime (); getmemory (&memtotal, &memfree, &buffers, &cached, &swapcached, &active, &inactive); if (rank == 0) { fprintf (stdout, "AFTER FLUSHING:\n"); fflush (stdout); } for (i = 0; i < nranks; i++) { if (rank == i) { printmemory (rank, memtotal, memfree, buffers, cached, swapcached, active, inactive); assert (fprintf (stdout, "rank %04i Total time for flushing to disk:\t\t %10.2f seconds\n", rank, t2 - t1)); assert (fflush (stderr) == 0); } MPI_Barrier (MPI_COMM_WORLD); } } stop = MPI_Wtime (); MPI_Barrier (MPI_COMM_WORLD); if (rank == 0) { printf ("Total time: %f s\n", stop - start); printf ("Aggregate bandwidth per process: %f GB/s\n", ((float) X * Y * Z * sizeof (float)) * ((float) MAX_DATASETS * MAX_GROUPS * MAX_TIME_LEVEL * MAX_ITER) / (1024.0 * 1024.0 * 1024.0 * (stop - start))); } free ((void *) buf); assert (H5Pclose (fapl) >= 0); assert (MPI_Finalize () >= 0); return 0; }
size_t memdecode(const void * memory, size_t extent, char const * object, char const * string) { size_t length; if (! strcmp(object, "byte")) { uint8_t number; if (sizeof(number) > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } memcpy (& number, memory, sizeof(byte)); printf ("%u", number); return (sizeof(number)); } else if(! strcmp(object, "word")) { uint16_t number; if (sizeof(number) > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } memcpy (& number, memory, sizeof(number)); printf ("%u", LE16TOH(number)); return (sizeof(number)); } else if(! strcmp(object, "long")) { uint32_t number; if (sizeof(number) > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } memcpy (& number, memory, sizeof(number)); printf ("%u", LE32TOH(number)); return (sizeof(number)); } else if(! strcmp(object, "xbyte")) { uint8_t number; if (sizeof(number) > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } memcpy (& number, memory, sizeof(number)); printf ("0x%02X", number); return (sizeof(number)); } else if(! strcmp(object, "xword")) { uint16_t number; if (sizeof(number) > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } memcpy (& number, memory, sizeof(number)); printf ("0x%04X", LE16TOH(number)); return (sizeof(number)); } else if(! strcmp(object, "xlong")) { uint32_t number; if (sizeof(number) > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } memcpy (& number, memory, sizeof(number)); printf ("0x%08X", LE32TOH(number)); return (sizeof(number)); } else if(! strcmp(object, "data")) { char const * size = string; if (! size) { error (1, EINVAL, "%s needs a length", object); } length = (unsigned) (uintspec(size, 1, extent)); if (length > extent) { error (1, ECANCELED, "%s exceeds " SIZE_T_SPEC " bytes", object, length); } hexout (memory, length, 0, 0, stdout); return (length); } else if(! strcmp(object, "text")) { char const * size = string; if (! size) { error (1, EINVAL, "%s needs a length", object); } length = (unsigned) (uintspec(size, 1, extent)); getstring (memory, extent, object, length); return (length); } else if(! strcmp(object, "skip")) { char const * size = string; if (! size) { error (1, EINVAL, "%s needs a length", object); } length = (unsigned) (uintspec(size, 1, extent)); return (length); } else if(! strcmp(object, "mac")) { length = ETHER_ADDR_LEN; getmemory (memory, extent, object, length); return (length); } else if(! strcmp(object, "url")) { length = STRINGSIZE; getstring (memory, extent, object, length); return (length); } else { error (1, ENOTSUP, "%s", object); } return (0); }
void pointer3() { char *str = NULL; str = getmemory(); printf("%s\n", str); }