2013-04-13 3 views
0

내가 루프 아래 코드의 주요 부분을 찾고 있어요 촬영 한 다음 루프 여기코드 X 시간의 조각은 평균

에서 수집 한 결과의 평균을 계산하는 방법 루프 코드입니다 :

class PingPongVariousLengths { 

    static public void main(String[] args) { 

     MPI.Init(args); 
     int myrank = MPI.COMM_WORLD.Rank(); 
     int tag = 99; 
     int maxlen = 104857600; //200 megabytes  104857600 characters * 2 bytes per character = 209715200 bytes total, or 200 megabytes 
     int minlen = 1; // 2 bytes 
     char [] sendbuff = new char [maxlen]; 
     char [] recvbuff = new char [maxlen]; 
     long speedKbps; 
     long speedMbps; 


     if (myrank == 0) { 
      for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time 
       long startTime = System.nanoTime();     
       MPI.COMM_WORLD.Send(sendbuff, 0, len, MPI.CHAR, 1, tag); 
       MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 1, tag); 
       long endTime = System.nanoTime(); 
       long duration = endTime - startTime; 
       long durationseconds = (duration * 10-9); // Converts nanoseconds to seconds 
       System.out.println("Time for the ping to be sent and recived of " + (len*2) + " bytes is " + durationseconds + " seconds"); // multiples by 2 becuase 1 character is 2 bytes 
       //double transferRate = ((len*2.0)/durationseconds) ; //amount of data in bytes transferred in 1 second. Currently returning 0 for every result 
       //System.out.println("transferRate: " + transferRate + " bytes per second"); 
       double transferRateMb = ((len*524288.0)/durationseconds) ; //amount of data in megabytes transferred in 1 second. 
       System.out.println("transferRate (megabytes) : " + transferRateMb + " megabytes per second"); 
       } 
     } else if (myrank == 1) { 
      for (int len = minlen; len <= maxlen; len *= 2) { 
       MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 0, tag); 
       MPI.COMM_WORLD.Send(recvbuff, 0, len, MPI.CHAR, 0, tag); 
      } 
     } 


     MPI.Finalize(); 
    } 
} 

은 내가

,691,363에

  for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time 

에서, 그것은 10 또는 20 회 말 루프에 코드를 시도하고있다 (210)

   long durationseconds = (duration * 10-9); // Converts nanoseconds to seconds 

은 내가 당신을 이해하면 내가 어떻게 가장 가장 효율적이

+0

당신이 정말이 요청하는 경우 ... 일반적으로 평균 ... 1/N * 합계 (기간)을 모든 기간을 추가하여 계산 한 후? 사이클의 수에 의해 분할 될 수있다 – gausss

+0

있는지 "를하지 않음 평균을 계산합니다. "너 무슨 소리 야? –

+0

위에서 편집 했으니 희망적으로 첫 번째 혼란스러운 질문에 대해 유감스럽게 생각합니다. – user2065929

답변

1

에 대해 갈 수있는 10 또는 20

평균 이상으로 duration 20 시간 후 결과를 실행하려면 정확하게 각 길이 증가분의 평균 지속 시간 (초)을 원했습니다. 평균 지속 시간을 초 단위로 얻으려면 평균 루핑 밖에서 루프 길이를 이동해야합니다. 아래의 코드 스 니펫을 참조하십시오.

long durationseconds; 
int MAX_LOOPS = 20; 

for (int len = minlen; len <= maxlen; len *= 2) { 
     if (myrank == 0) { 
       durationseconds = 0; 
       for (int i = 0; i < MAX_LOOPS; i++) { 
         long startTime = System.nanoTime();   
         MPI.COMM_WORLD.Send(sendbuff, 0, len, MPI.CHAR, 1, tag); 
         MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 1, tag); 
         long endTime = System.nanoTime(); 
         long duration = endTime - startTime; 
         durationseconds = durationseconds + (duration * 10-9); 
       } 
       durationseconds = durationseconds/MAX_LOOPS; 
       System.out.println("Average time for the ping to be sent and recived of " + (len*2) + " bytes is " + durationseconds + " seconds"); 
       double transferRateMb = ((len*524288.0)/durationseconds); 
       System.out.println("average transferRate (megabytes) : " + transferRateMb + " megabytes per second"); 
     } else if (myrank == 1) { 
       MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 0, tag); 
       MPI.COMM_WORLD.Send(recvbuff, 0, len, MPI.CHAR, 0, tag); 
     } 
} 
+0

정확하고 정확하게 설명하려고했던 것입니다. 감사합니다. – user2065929

+0

사랑스럽고, 30 분 전에 게시 한 것의 유일한 차이점은 실제로 넣은 것입니다. 루프의 중간에 코드를 수정하고 변수 이름을 변경했습니다. 그저 좋은데, 둘 다 :) –

0
long totalDuration = 0; 
for (int i = 0; i<nTimes;i++){ 
... //Code N stuff here 
totalDuration += duration; 
} 
long avgDuration = totalTime /nTimes; 
관련 문제