RCCE_share.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright 2010 Intel Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "RCCE.h"
  17. #include <stdio.h>
  18. int RCCE_APP(int argc, char **argv){
  19. int ID, ID_right, ID_left, nrounds, round, NP, size, baton, bufsize, i;
  20. double *buffer, sum, refval;
  21. RCCE_init(&argc, &argv);
  22. NP = RCCE_num_ues();
  23. ID = RCCE_ue();
  24. if (NP<2) {
  25. if (ID==0) printf("Need at least two cores to run this code\n");
  26. return(1);
  27. }
  28. if (argc != 3) {
  29. if (ID==0) printf("Need to specify buffer size and # rounds\n");
  30. return(1);
  31. }
  32. bufsize = atoi(*++argv);
  33. if ((bufsize<1)) {
  34. if (ID==0) printf("Buffer size must be greater than or equal to 1\n");
  35. return(1);
  36. }
  37. ID_right = (ID+1)%NP;
  38. ID_left = (ID-1+NP)%NP;
  39. nrounds = atoi(*++argv);
  40. if (nrounds < 0) {
  41. if (ID==0) printf("Number of rounds should be non-negative: %d\n", nrounds);
  42. return(1);
  43. }
  44. size = bufsize*sizeof(double);
  45. buffer = (double *) RCCE_shmalloc(size);
  46. if (!buffer) printf("Mark 02:RCCE failed to shmalloc %d doubles on proc %d\n",
  47. bufsize, ID);
  48. if (ID==0) {
  49. printf("Buffer is allocated %d doubles\n",bufsize);
  50. printf("allocated size in bytes: %d \n",size);
  51. }
  52. /* initialize shared buffer */
  53. sum = 0.0;
  54. if (ID==0) {
  55. for (i=0; i<bufsize; i++) {
  56. buffer[i] = (double)(1+i);
  57. sum += buffer[i];
  58. }
  59. printf("Initial sum on UE %03d equals %14.13e\n", ID, sum);
  60. }
  61. refval = 0.0;
  62. for (i=0; i<bufsize; i++) refval += (double)(i+1);
  63. for (round=0; round<nrounds; round++) {
  64. if (ID==(round%NP)) RCCE_send((char*)&baton, sizeof(int), ID_right);
  65. for (i=0; i<bufsize; i++) refval += (round+1)%NP;
  66. if (ID==(round+1)%NP) {
  67. RCCE_recv((char*)&baton, sizeof(int), ID_left);
  68. RCCE_shflush(); // need to make sure buffer is up to date before we read it
  69. for (i=0; i<bufsize; i++) buffer[i] += ID;
  70. RCCE_shflush(); // need to make sure other UEs see updated buffer contents
  71. if (round==nrounds-1) {
  72. sum = 0.0;
  73. for (i=0; i<bufsize; i++) sum += buffer[i];
  74. printf("\n*****\nFinal sum on UE %03d equals %14.13e, refval = %14.13e\n*****\n", ID, sum, refval);
  75. }
  76. }
  77. }
  78. return(0);
  79. }