Advertisement
Guest User

eed

a guest
Mar 25th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. #include <sys/time.h>
  5. #include <sys/resource.h>
  6. #include<unistd.h>
  7. #include<sys/mman.h>
  8. int main(int argc, char** argv){
  9. struct timeval tv;
  10. struct timezone tz;
  11. int i,j,index,iterations,array_size;
  12. float *arrayPtr;
  13. double stop,start,init;
  14. // Lock down all memory for this process
  15. if(mlockall(MCL_CURRENT|MCL_FUTURE)<0){
  16. fprintf(stderr,"Memlock error\n");
  17. exit(1);
  18. }
  19. if (argc!=3) {
  20. fprintf(stderr, "Usage: %s <Size of array><no of iterations>\n", argv[0]);
  21. exit(1);
  22. }
  23. // progname=argv[0];
  24. array_size=atoi(argv[1]);
  25. iterations=atoi(argv[2]);
  26. // Record start
  27. gettimeofday( &tv,&tz);
  28. init=tv.tv_sec + tv.tv_usec*0.000001;
  29. 6
  30. // create large float arrays
  31. arrayPtr=calloc(array_size,sizeof(float));
  32. //initialise array
  33. for(i=0;i<array_size;++i) { arrayPtr[i]=9999.99; }
  34. // Main loop
  35. // Process full array and time each pass.. Use random number
  36. // generator to access array randomly .. greater likelihood of paging in unlocked version
  37. for(j=0;j<iterations;++j)
  38. {
  39. gettimeofday( &tv,&tz);
  40. start=tv.tv_sec + tv.tv_usec*0.000001;
  41. // random seed
  42. srand(time(NULL));
  43. for(i=0;i<array_size;++i)
  44. {
  45. // generate random index
  46. index = rand() % array_size;
  47. arrayPtr[index]=arrayPtr[index]/0.9999;
  48. }
  49. gettimeofday( &tv,&tz);
  50. stop=tv.tv_sec + tv.tv_usec*0.000001;
  51. printf("Run time for pass %d is %lf ms\n",j+1, (stop-start)*1000);
  52. // sleep for 10 sec
  53. sleep(10);
  54. // Run multiple versions so unlocked versions may be paged during
  55. // sleep period between passes
  56. }
  57. // release memory
  58. free(arrayPtr);
  59. munlockall(); // unlock memory
  60. gettimeofday( &tv,&tz);
  61. stop=tv.tv_sec + tv.tv_usec*0.000001;
  62. printf("Total Run time is %lf ms\n", (stop-init)*1000);
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement