Example #1
0
int main(int argc, const char *argv[]) {
    int i, nthreads, count[MAXNTHREADS];
    pthread_t tid_produce[MAXNTHREADS], tid_consume;
    if(argc != 3) {
        perror("Usage: producer-consumer1 <#item> <#threads>");
        exit(EXIT_FAILURE);
    }
    nitems = myMin(atoi(argv[1]), MAXNITEMS);
    nthreads = myMin(atoi(argv[2]), MAXNTHREADS);

    pthread_setconcurrency(nthreads);

    // start all producer threads
    for(i = 0; i < nthreads; ++i) {
        count[i] = 0;
        pthread_create(&tid_produce[i], NULL, produce, &count[i]);
    }

    // wait for all producer threads
    for(i = 0; i < nthreads; ++i) {
        pthread_join(tid_produce[i], NULL);
        printf("count[%d] = %d\n", i, count[i]);
    }

    // start, then wait for the consumer thread
    pthread_create(&tid_consume, NULL, consume, NULL);
    pthread_join(tid_consume, NULL);
    exit(EXIT_SUCCESS);
}
Example #2
0
int predicate(float eps, MyRect& r1, MyRect& r2)
{
  float delta = eps*(myMin(r1.width, r2.width) + myMin(r1.height, r2.height))*0.5;
  return myAbs(r1.x - r2.x) <= delta &&
    myAbs(r1.y - r2.y) <= delta &&
    myAbs(r1.x + r1.width - r2.x - r2.width) <= delta &&
    myAbs(r1.y + r1.height - r2.y - r2.height) <= delta;
}
Example #3
0
void Lexer::setToken()
{
  std::string tmp;
  std::size_t pos = 0, begin = 0;
  std::string::iterator end;
  std::string::size_type comment_start;
  std::string::size_type newline;

  std::replace(_string.begin(), _string.end(), '\t', ' ');
  _string += "exit";
  _string = trim_comments(_string);
  _string = trim_string(_string);

  for (std::string::iterator it = _string.begin() ; it + pos != _string.end() ; ++it)
    {
      if ((int)(pos = myMin(_string.find('\n', pos), _string.find(' ', pos))) == -1)
	{
	  _token.push_back("exit");
	  return;
	}
      tmp = _string.substr(begin, pos-begin);
      pos++;
      begin = pos;
      _token.push_back(tmp);
    }
}
Example #4
0
int takeNext(int i, int taken)
{
	int ret, next;
	if (i >= N)
	{
		ret = myAbs(taken - (superSum - taken));
		return ret;
	}

	ret = saved[i][taken];
	if (ret >= 0) return ret; // Return saved

	next = i + 1;
	int withCurrent = takeNext(next, taken + myData[i]);
	int withoutCurrent = takeNext(next, taken);
	ret = myMin(withCurrent, withoutCurrent);
	saved[i][taken] = ret; // Save

	return ret;
}
Example #5
0
void sendFile(int sockfd, char* fileName, struct sockaddr* cliAddr, socklen_t cliAddrLen, int cliWindow, int servWindow)
{
	struct sockaddr_in *destAddr;

	//bzero(&destAddr, sizeof(struct sockaddr));
	destAddr= (struct sockaddr_in *)cliAddr;
	printf("New Client address %s:%u\n",inet_ntoa(destAddr->sin_addr), ntohs(destAddr->sin_port));

	printf("Sockfd = %d\n", sockfd);

	fd_set rset;
	int maxfdp1;
	FD_ZERO(&rset);

	fd_set rset_probe;
	int maxfdp1_probe;
	FD_ZERO(&rset_probe);

	//Signals Initialization

	sigset_t mySignalSet;	

	if(sigemptyset(&mySignalSet) < 0)
	{
		perror("Sigemptyset error:");
		exit(1);
	}

	if(sigaddset(&mySignalSet, SIGALRM), 0)
	{
		perror("Sigaddset error");
		exit(1);
	} 

	signal(SIGALRM, sig_alarm);

	//End signal initialization

	//Congestion Control
	struct congestion myCongestion;
	myCongestion.ssthresh = myMin(cliWindow, servWindow);
	myCongestion.maxWindowSize = myMin(cliWindow, servWindow);
	myCongestion.cliWindowSize = cliWindow;
	myCongestion.currWindowSize = 1;
	myCongestion.state = MultiplicativeIncrease;
	
	int congestionFlag = MultiplicativeIncrease;
	int firstPacket = 1;

	//End Congestion control

	

	struct itimerval value, ovalue, pvalue, zeroValue;
	int which = ITIMER_REAL;
	int tempTime, tempSec, tempMSec;

	struct iovec iovsend[2], iovrecv[2];
	struct iovec *iovsendDup;
	
	struct myList* packet = NULL;	

	if (rttinit == 0) {
		my_rtt_init(&rttinfo);		/* first time we're called */
		rttinit = 1;
		rtt_d_flag = 1;
	}

	mybool flag = 1;
	mybool probe = 2;
	
	//int fin;
	FILE *fin;
	fin = fopen(fileName,"r");
	if(fin == NULL) {
		perror("Error opening a file:");
		exit(1);
	}
	
	//printf("Sizeof myheader = %d\n", sizeof(struct header));
	//printf("Sizeof file buffer = %d\n", FILEBUFSIZE);
	
	int currentTime=0;

	int countDup=0;			//For counting the duplicates Acks
	int sequenceNumber=0;		//The sequence number used while sending
	int tempClientWindow = cliWindow;
	int tempCounter=0;
	//int sequenceACK=1;		//Gives me the sequence number of the packet recently ACKed
	int firstSequence = 1;
	int firstSequenceSent=0;	//Gives me the sequence number of first packet sent
	int lastSequenceSent=0;		//Gives me the sequence number of 
	//int lastACKRecv =0;
	int receivedACK=0;		//Gives me the sequence number of the packet recently AC
	int expectedACK =0;
	int isLast = 0;

	int recvCliWindow=0;

	int sendAgain = 0;

	int readBytes =0;
	char buf[FILEBUFSIZE];
	memset(buf, 0, FILEBUFSIZE);
	char recvBuf[RECVBUFSIZE];
	memset(recvBuf, 0, RECVBUFSIZE);

	//printf("Hi I'm here \n");
	int packetSend =0;
	//int tempWindow = cliWindow;	
	int tempWindow = 1;

	int retransmitCounter = 0;
	struct timeval time_count;
	recvCliWindow = cliWindow;

	tempCounter =0;
start_sending:
	//tempCounter =0;
	//packetSend++;	
	
	//tempWindow = cliWindow;

	printf("Recieved Window = %d\n", recvCliWindow);

	if(sigprocmask(SIG_BLOCK, &mySignalSet, NULL) < 0)
	{
		perror("sigprocmask block error:");
		exit(1);
	}

	// Persistant timer code for probe packet
	FD_ZERO(&rset_probe);
	retransmitCounter = 0;
probe_check:
	if(recvCliWindow == 0)
	{
		printf("---------Sending Probe Packet-------------\n");
		retransmitCounter++;
		if(retransmitCounter > 12)
		{
			printf("Client window size = 0 after trying for 12 times\n");
			printf("Now will terminate the server\n");
			exit(1);
		}
		time_count.tv_sec = 3;
		time_count.tv_usec = 0;
		memset((void *)&sendhdr, 0, sizeof(struct header));
		memset(buf, 0, FILEBUFSIZE);
		
		sendhdr.seq = 0;
		sendhdr.isLast = probe;
		iovsend[0].iov_base = (void*)&sendhdr;
		iovsend[0].iov_len = sizeof(struct header);
		iovsend[1].iov_base = (void*)&buf;
		iovsend[1].iov_len = sizeof(buf);

		sleep(5);
		if(writev(sockfd, iovsend, 2) < 0)
		{
			perror("Writev failed:");
			exit(1);
		}

		FD_SET(sockfd, &rset_probe);
		maxfdp1_probe=sockfd+1;

		if(select(maxfdp1_probe, &rset_probe, NULL, NULL, &time_count) < 0)
		{
			if(errno == EINTR)
				goto check_window;
			else
			{
				perror("Select Error :");
				exit(1);
			}
		}
		if(FD_ISSET(sockfd, &rset_probe))
		{
			//printf("Hi I'm here");
			memset((void *)&recvhdr, 0, sizeof(struct header));
			memset(recvBuf, 0, RECVBUFSIZE);
			iovrecv[0].iov_base = (void*)&recvhdr;
			iovrecv[0].iov_len = sizeof(recvhdr);
			iovrecv[1].iov_base = recvBuf;
			iovrecv[1].iov_len = sizeof(recvBuf);
			if(readv(sockfd, iovrecv, 2) < 0)
			{
				perror("Error while reading the ACK:");
				exit(1);
			}
			if(recvhdr.isLast == 3)
			{
				recvCliWindow = recvhdr.availWindow;
				printf("Received Reply for Probe Packet : Window Size = %d\n", recvCliWindow);
			}
		
		}
		else
		{
			goto check_window;
		}

check_window:
		if(recvCliWindow <= 0)
			goto probe_check;

	}
	

	//Persistant timer code end

	printf("Will Send packets :\n");

	if(firstPacket != 1)
	{
		printf("Update congestion values\n");
		myCongestion.maxWindowSize = recvCliWindow;
		congestionValues(&myCongestion, myCongestion.state);
		tempWindow = myCongestion.currWindowSize;
		
	}
	printf("Current Window Size = %d, ssthreshold = %d, state = %d\n", myCongestion.currWindowSize, myCongestion.ssthresh, myCongestion.state);
	firstPacket = 0;
	//currWindowSize stores the info about the packets which is to be sent
	//printf("Hi before already packets\n");
	if((getLength(head) >= myCongestion.currWindowSize) &&  recvCliWindow > 0)
	{
		tempWindow = myCongestion.currWindowSize;
		printf("_____Packets already in Server Window_______\n");

		packet = head;
		my_rtt_newpack(&rttinfo);

		while(tempWindow > 0)
		{
			
			printf("Sending packet with sequence number = %d\n", ((struct header *)packet->iv[0].iov_base)->seq);
			iovsendDup = packet->iv;
			if(writev(sockfd, iovsendDup, 2) < 0)
			{
				perror("Writev failed:");
				exit(1);
			}
			lastSequenceSent = ((struct header *)packet->iv[0].iov_base)->seq;
			packet = packet->next;
			tempWindow--;
		}
		//goto wait_timer;
		
	}

	//printf("Hi after already packets\n");
	if(getLength(head) > 0 && (getLength(head) < myCongestion.currWindowSize) &&  recvCliWindow > 0)
	{
		tempWindow = myCongestion.currWindowSize;
		packet = head;
		my_rtt_newpack(&rttinfo);
		while(tempWindow > 0 && packet != NULL)
		{
			printf("Sending packet with sequence number = %d\n", ((struct header *)packet->iv[0].iov_base)->seq);
			iovsendDup = packet->iv;
			if(writev(sockfd, iovsendDup, 2) < 0)
			{
				perror("Writev failed:");
				exit(1);
			}
			lastSequenceSent = ((struct header *)packet->iv[0].iov_base)->seq;
			packet = packet->next;
			tempWindow--;
		}
	}

	//printf("Hi before adding to linked list\n");
	printf("TempWindow = %d\n", tempWindow);
	//while(tempCounter < packetSend && tempCounter < 6)
	while(tempWindow > 0 && recvCliWindow > 0)
	{
		
		memset((void *)&sendhdr, 0, sizeof(struct header));
		memset(buf, 0, FILEBUFSIZE);
		if((readBytes = fread(buf, FILEBUFSIZE, sizeof(char), fin)) < 0)
		{
			perror("fread Error:");
			exit(1);
		}
#ifdef DEBUG1
		printf("Read buffer : %s\n", buf);
#endif
		sendhdr.seq = ++sequenceNumber;
		
		if(feof(fin))
		{	
			printf("Last packet \n");
			sendhdr.isLast = flag;
			isLast = 1;
		}
		else
		{
			//sendhdr.isLast = 0;
		}
		printf("\nSequence number : %d\n", sendhdr.seq);
		iovsend[0].iov_base = (void*)&sendhdr;
		iovsend[0].iov_len = sizeof(struct header);
		iovsend[1].iov_base = (void*)&buf;
		iovsend[1].iov_len = sizeof(buf);
			
		
		head = addToList(head, iovsend);
#ifdef DEBUG1
		printf("Sequence check = %d\n", ((struct header *)head->iv[0].iov_base)->seq);
		printf("Data check = %s\n", (char*)head->iv[1].iov_base);
#endif
		//printList(head);
		
		my_rtt_newpack(&rttinfo);
		sendhdr.ts = my_rtt_ts(&rttinfo);		

		if(writev(sockfd, iovsend, 2) < 0)
		{
			perror("Writev failed:");
			exit(1);
		}

		//sendhdr.ts = my_rtt_ts(&rttinfo);
		printf("Sending packet with sequence number = %d\n", sendhdr.seq);
		lastSequenceSent = sendhdr.seq;
 		//tempCounter++;
		tempWindow--;
		if(sendhdr.isLast == myTRUE)
			tempWindow=0;
	}
	

common_all:

	printf("Window contains %d to %d sequence Numbers\n", ((struct header *)head->iv[0].iov_base)->seq , lastSequenceSent);
	
	sendAgain = 0;
	
	if(sigprocmask(SIG_UNBLOCK, &mySignalSet, NULL) < 0)
	{
		perror("sigprocmask unblock error:");
		exit(1);
	}

wait_timer:

	expectedACK = ((struct header *)head->iv[0].iov_base)->seq;

	if(sendAgain == 1)
	{
		if((packet = getNode(head, expectedACK)) != NULL)
		{	
			printf("Retransmission for packet with sequence number = %d after timeout\n", expectedACK);
			iovsendDup = packet->iv;
			if(writev(sockfd, iovsendDup, 2) < 0)
			{
				perror("Writev failed:");
				exit(1);
			}
		//setitimer(ITIMER_REAL, &zeroValue,0);
		//my_rtt_newpack(&rttinfo);
		}
	}

	
	// Timeout for next ACK
	// if(Current Time - Previous time) < RTO, goto select and wait for the remaining time

	countDup =0;

	//currentTime = my_rtt_ts(&rttinfo);
	
	//tempTime = (int32_t)(rttinfo.rtt_rto - (currentTime - ((struct header *)head->iv[0].iov_base)->ts));


	tempTime = my_rtt_start(&rttinfo);
	//tempTime = ((struct header *)head->iv[0].iov_base)->ts;
        if (tempTime >= 1000) {
                tempSec = tempTime / 1000;
        }
        tempMSec = (tempTime % 1000)*1000;

    	value.it_interval.tv_sec = 0;
    	value.it_interval.tv_usec = 0;
    	value.it_value.tv_sec = tempSec;
        value.it_value.tv_usec = tempMSec;

	zeroValue.it_interval.tv_sec = 0;
    	zeroValue.it_interval.tv_usec = 0;
    	zeroValue.it_value.tv_sec = 0;
        zeroValue.it_value.tv_usec = 0;
   
    	printf("\nTIMER Value: %d %d\n", (int)value.it_value.tv_sec,(int)value.it_value.tv_usec);

	setitimer(ITIMER_REAL, &zeroValue,0);
    	setitimer(ITIMER_REAL, &value,0);

	if (sigsetjmp(jmpbuf, 1) != 0) 
	{
		if (my_rtt_timeout(&rttinfo) < 0) {
			printf("Network conditions pretty bad.\n");
			rttinit = 0;	/* reinit in case we're called again */
			errno = ETIMEDOUT;
			exit(1);
			//return(-1);
		}
#ifdef	DEBUG
		err_msg("Timeout, retransmitting\n");
#endif
		sendAgain =1;

		myCongestion.ssthresh = myMax(myCongestion.ssthresh/2, 2);
		myCongestion.currWindowSize = 1;
		myCongestion.state = MultiplicativeIncrease;
		//congestionValues(&myCongestion, MultiplicativeIncrease);
		printf("Current Window Size = %d, ssthreshold = %d, state = %d\n", myCongestion.currWindowSize, myCongestion.ssthresh, myCongestion.state);
		//goto start_sending;		

		goto wait_timer;
	}
	
	
	FD_ZERO(&rset);

wait_ACK:

	FD_SET(sockfd, &rset);
	maxfdp1=sockfd+1;

	if(select(maxfdp1, &rset, NULL, NULL, NULL) < 0)
	{
		if(errno == EINTR)
			goto wait_ACK;
		else
		{
			perror("Select Error :");
			exit(1);
		}
	}
	if(FD_ISSET(sockfd, &rset))
	{
		memset((void *)&recvhdr, 0, sizeof(struct header));
		iovrecv[0].iov_base = (void*)&recvhdr;
		iovrecv[0].iov_len = sizeof(recvhdr);
		iovrecv[1].iov_base = recvBuf;
		iovrecv[1].iov_len = sizeof(recvBuf);
		if(readv(sockfd, iovrecv, 2) < 0)
		{
			perror("Error while reading the ACK:");
			exit(1);
		}
		
		//expectedACK = ((struct header *)head->iv[0].iov_base)->seq;
		//printf("Expected ACK = %d\n", expectedACK);
		printf("Window Size Available  = %d\n", recvhdr.availWindow);
		if(recvhdr.isACK ==1)
		{
			printf("Window Size Available  = %d\n", recvhdr.availWindow);
			recvCliWindow = recvhdr.availWindow;

			receivedACK = recvhdr.seq;
			printf("Received ACK for packet : %d\n", recvhdr.seq);
			
			packet = getNode(head, recvhdr.seq);
	                if(packet != NULL)
			{
				if(recvhdr.ts == ((struct header*)(packet->iv[0].iov_base))->ts)
	                        {
					my_rtt_stop(&rttinfo, my_rtt_ts(&rttinfo) - ((struct header*)(head->iv[0].iov_base))->ts);
					//my_rtt_stop(&rttinfo, my_rtt_ts(&rttinfo) - recvhdr.ts);
					printf("RTO updated to: %d millisec\n", rttinfo.rtt_rto);
	                        }
				my_rtt_newpack(&rttinfo);
			}
	
			printf("ReceivedACK =%d ExpectedACK = %d last sequence = %d\n",receivedACK, expectedACK, lastSequenceSent);
			if(receivedACK == expectedACK)
			{
				expectedACK++;
			}
			
			//printf("Length of the linked list = %d\n",getLength(head));
			//head = deleteFromList(head, receivedACK);
			head = deleteNodes(head, receivedACK);
			//printf("Length of the linked list = %d\n",getLength(head));

			if(isLast == 1)
			{
				if(receivedACK == lastSequenceSent)
				{
					printf("File transfer completed successfully \n");
					goto close_file;
				}
			}

			if(receivedACK == lastSequenceSent)
			{
				printf("Received the required sequence number before timeout\n");
				setitimer(ITIMER_REAL, &zeroValue,0);
				
				goto start_sending;			
			}
			
			if(receivedACK < expectedACK)
			{
				if((packet = getNode(head, receivedACK)) == NULL)
				{
					countDup++;
				}
				if(countDup == 2)
				{
					if((packet = getNode(head, receivedACK+1)) != NULL)					
					{
						//Fast Retransmit the packet with sequence number (receivedACK+1)
						printf("Fast retransmission for packet with sequence number = %d\n", receivedACK+1);
						iovsendDup = packet->iv;
						if(writev(sockfd, iovsendDup, 2) < 0)
						{
							perror("Writev failed:");
							exit(1);
						}
						setitimer(ITIMER_REAL, &zeroValue,0);
						my_rtt_newpack(&rttinfo);
						
						printf("Fast recovery Happened here\n");
						myCongestion.ssthresh = myMax(myCongestion.ssthresh/2, 2);
						myCongestion.currWindowSize = myCongestion.ssthresh;
						myCongestion.state = AdditiveIncrease;
						//congestionValues(&myCongestion, AdditiveIncrease);
						printf("Current Window Size = %d, ssthreshold = %d, state = %d\n", myCongestion.currWindowSize, myCongestion.ssthresh, myCongestion.state);

						goto wait_timer;
					}
				}
			}
			if(receivedACK > expectedACK)
			{
				expectedACK = receivedACK+1;
			}
		}
	}
	else
	{
		/*printf("Timeout occured\n");
		int willRetry =0;
		if(lastACKRecv == 0)
		{
			//Have to retransmit packet 1
			willRetry = my_rtt_timeout(&rttinfo);
			if(willRetry < 0)
			{
				printf("Network conditions pretty bad.\n");
				rttinit = 0;					
				errno = ETIMEDOUT;
				exit(1);
				//return(-1);
			}
			packet = getNode(head, firstSequence);
			if(packet != NULL)
			{
				((struct header*)(packet->iv[0].iov_base))->ts = my_rtt_ts(&rttinfo);
				printf("Retransmission of packet with seqNo = : %d\n", firstSequence);
				//writev(sockfd, packet->iv, 2);
			}
		}*/
		
		goto wait_ACK;
	}	
	
goto wait_ACK;

	

close_file:

	fclose(fin);
	
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
//L2L2OpticalFlow(u1,u2,tol,lambda,ux,uy,ut)
{
	double *u1 = mxGetPr(prhs[0]);
	double *u2 = mxGetPr(prhs[1]);

	float tol = (float)mxGetScalar(prhs[2]);
	float lambda = (float)mxGetScalar(prhs[3]);

	int maxIterations = (int)mxGetScalar(prhs[4]);

	const size_t *sizeImage = mxGetDimensions(prhs[0]);

	double *inputV = 0;
	double *inputY = 0;

	int typeNorm = 1;
	if (nrhs > 5)
	{
		typeNorm = (int)mxGetScalar(prhs[5]);
	}

	if (nrhs > 6)
	{
		inputV = mxGetPr(prhs[6]);
	}

	if (nrhs > 7)
	{
		inputY = mxGetPr(prhs[7]);
	}

	int nPx = (int)(sizeImage[0] * sizeImage[1]);

	const size_t sizeY[2] = {5*nPx,1};

	// Output v1
	plhs[0] = mxCreateNumericArray(2, sizeImage, mxDOUBLE_CLASS, mxREAL);
	double *Outv1 = mxGetPr(plhs[0]);

	// Output v2
	plhs[1] = mxCreateNumericArray(2, sizeImage, mxDOUBLE_CLASS, mxREAL);
	double *Outv2 = mxGetPr(plhs[1]);

	// Output  Y
	plhs[2] = mxCreateNumericArray(2, sizeY, mxDOUBLE_CLASS, mxREAL);
	double *YOut = mxGetPr(plhs[2]);

	float* v1 = new float[nPx];
	float* v2 = new float[nPx];

	float* u = new float[nPx];
	float* ut = new float[nPx];
	float* sigut = new float[nPx];

	float* y11 = new float[nPx];
	float* y12 = new float[nPx];
	float* y21 = new float[nPx];
	float* y22 = new float[nPx];
	float* y5 = new float[nPx];
	
	float* Kty1 = new float[nPx];
	float* Kty2 = new float[nPx];

	float* Kx11 = new float[nPx];
	float* Kx12 = new float[nPx];
	float* Kx21 = new float[nPx];
	float* Kx22 = new float[nPx];
	float* Kx5 = new float[nPx];
	
	float tau = 1.0f / sqrt(8.0f);
	float sigma = tau;
	float dTau = 1.0f / tau;
	float dSigma = 1.0f / sigma;

	//residuals
	float p = 0;
	float d = 0;
	float err = 1.0;

	float ssl = 1.0f - sigma / (sigma + lambda);

	#pragma omp parallel for
	for (int j = 0; j < sizeImage[1]; ++j)
	{
		for (int i = 0; i < sizeImage[0]; ++i)
		{
			int tmpIndex = index2DtoLinear(sizeImage, i, j);

			//Index for gradients

			u[tmpIndex] = (float)u1[tmpIndex];
			ut[tmpIndex] = (float)(u2[tmpIndex] - u1[tmpIndex]);
			sigut[tmpIndex] = (float)(sigma*ut[tmpIndex]);

			if (nrhs > 6)
			{
				v1[tmpIndex] = (float)inputV[tmpIndex];
				v2[tmpIndex] = (float)inputV[nPx + tmpIndex];
			}
			else
			{
				v1[tmpIndex] = 0;
				v2[tmpIndex] = 0;
			}

			Kty1[tmpIndex] = 0; 
			Kty2[tmpIndex] = 0;

			if (nrhs > 7)
			{
				y11[tmpIndex] = (float)inputY[tmpIndex];
				y12[tmpIndex] = (float)inputY[nPx + tmpIndex];
				y21[tmpIndex] = (float)inputY[2 * nPx + tmpIndex];
				y22[tmpIndex] = (float)inputY[3 * nPx + tmpIndex];
				y5[tmpIndex] = (float)inputY[4 * nPx + tmpIndex];
			}
			else
			{
				y11[tmpIndex] = 0;
				y12[tmpIndex] = 0;
				y21[tmpIndex] = 0;
				y22[tmpIndex] = 0;
				y5[tmpIndex] = 0;
			}

			Kx11[tmpIndex] = 0;
			Kx12[tmpIndex] = 0;
			Kx21[tmpIndex] = 0;
			Kx22[tmpIndex] = 0;
			Kx5[tmpIndex] = 0;
		}
	}

	int iterations = 0;

	while (err > tol && iterations <= maxIterations)
	{
		++iterations;

		if (iterations % 50 == 0)
		{
				p = 0;
				d = 0;
		}

		//primal step
		#pragma omp parallel for reduction(+:p)
		for (int j = 0; j < sizeImage[1]; ++j)
		{
			for (int i = 0; i < sizeImage[0]; ++i)
			{
				int tmpIndex = index2DtoLinear(sizeImage, i, j);

				float Kty1Old = Kty1[tmpIndex];
				float Kty2Old = Kty2[tmpIndex];

				//transpose equals -div
				Kty1[tmpIndex] = -(dxm(y11, sizeImage, i, j) + dym(y12, sizeImage, i, j) + dycT(y5, u, sizeImage, i, j));
				Kty2[tmpIndex] = -(dxm(y21, sizeImage, i, j) + dym(y22, sizeImage, i, j) + dxcT(y5, u, sizeImage, i, j));

				float v1Old = v1[tmpIndex];
				float v2Old = v2[tmpIndex];

				v1[tmpIndex] = v1Old - tau*Kty1[tmpIndex];
				v2[tmpIndex] = v2Old - tau*Kty2[tmpIndex];

				if (iterations % 50 == 0)
				{
					//residuals
					p += myAbs((v1Old - v1[tmpIndex]) * dTau - Kty1Old + Kty1[tmpIndex])
						+ myAbs((v2Old - v2[tmpIndex]) * dTau - Kty2Old + Kty2[tmpIndex]);
				}
			}
		}
		
		//dual step
		#pragma omp parallel for reduction(+:d)
		for (int j = 0; j < sizeImage[1]; ++j)
		{
			for (int i = 0; i < sizeImage[0]; ++i)
			{
				int tmpIndex = index2DtoLinear(sizeImage, i, j);

				float Kx11Old = Kx11[tmpIndex];
				float Kx12Old = Kx12[tmpIndex];
				float Kx21Old = Kx21[tmpIndex];
				float Kx22Old = Kx22[tmpIndex];
				float Kx5Old = Kx5[tmpIndex];

				Kx11[tmpIndex] = dxp(v1, sizeImage, i, j);
				Kx12[tmpIndex] = dyp(v1, sizeImage, i, j);
				Kx21[tmpIndex] = dxp(v2, sizeImage, i, j);
				Kx22[tmpIndex] = dyp(v2, sizeImage, i, j);
				Kx5[tmpIndex] = dyc(v1, u, sizeImage, i, j) + dxc(v2, u, sizeImage, i, j);

				float y11Old = y11[tmpIndex];
				float y12Old = y12[tmpIndex];
				float y21Old = y21[tmpIndex];
				float y22Old = y22[tmpIndex];
				float y5Old = y5[tmpIndex];

				y11[tmpIndex] = ssl * (y11[tmpIndex] + sigma*(2 * Kx11[tmpIndex] - Kx11Old));
				y12[tmpIndex] = ssl * (y12[tmpIndex] + sigma*(2 * Kx12[tmpIndex] - Kx12Old));
				y21[tmpIndex] = ssl * (y21[tmpIndex] + sigma*(2 * Kx21[tmpIndex] - Kx21Old));
				y22[tmpIndex] = ssl * (y22[tmpIndex] + sigma*(2 * Kx22[tmpIndex] - Kx22Old));

				y5[tmpIndex] = myMax(-1.0f, myMin(1.0f, y5[tmpIndex] + sigma*(2 * Kx5[tmpIndex] - Kx5Old) + sigut[tmpIndex]));

				if (iterations % 50 == 0)
				{
					d += myAbs((y11Old - y11[tmpIndex]) * dSigma - Kx11Old + Kx11[tmpIndex]) +
						myAbs((y12Old - y12[tmpIndex]) * dSigma - Kx12Old + Kx12[tmpIndex]) +
						myAbs((y21Old - y21[tmpIndex]) * dSigma - Kx21Old + Kx21[tmpIndex]) +
						myAbs((y22Old - y22[tmpIndex]) * dSigma - Kx22Old + Kx22[tmpIndex]) + 
						myAbs((y5Old - y5[tmpIndex]) * dSigma - Kx5Old + Kx5[tmpIndex]);
				}
			}
		}

		if (iterations % 50 == 0)
		{
			err = (d*d + p*p) / nPx;
		}

		if (iterations % 1000 == 0)
		{
			mexPrintf("Iteration %d,Residual %e\n", iterations, err);
			mexEvalString("drawnow;");
		}
	}

	//write output
	#pragma omp parallel for
	for (int j = 0; j < sizeImage[1]; ++j)
	{
		for (int i = 0; i < sizeImage[0]; ++i)
		{
			int tmpIndex = index2DtoLinear(sizeImage, i, j);

			YOut[tmpIndex] = (double)y11[tmpIndex];
			YOut[tmpIndex + nPx] = (double)y12[tmpIndex];
			YOut[tmpIndex + 2 * nPx] = (double)y21[tmpIndex];
			YOut[tmpIndex + 3 * nPx] = (double)y22[tmpIndex];
			YOut[tmpIndex + 4 * nPx] = (double)y5[tmpIndex];

			Outv1[tmpIndex] = (double) v1[tmpIndex];
			Outv2[tmpIndex] = (double) v2[tmpIndex];
		}
	}
}
void *calcForce(void *arg) {
	int myID = *((int*)arg);
	double fac, fx, fy, fz;
	double dx, dy, dz, sq, dist; 
	int t = 0, i, j,a,b;

  while ( t<TimeSteps){
    /*  Loop over points calculating force between each pair.*/
    
    
    
    	for ( i=myID; i<BodyNum; i+=thread_num ){
        	a=myMin(i+(BodyNum+1)/2,BodyNum);  //计算任务Wia 
        	b=myMax(0,i-(BodyNum+1)/2); //计算任务Wbi 
        	for ( j=i+1; j<a; j++ ) {/*Calculate force between particle i and j according to Newton's Law*/
		     	if ( i==j ) continue;
             		dx = *(pBody+4*i+1) - *(pBody+4*j+1);
             		dy = *(pBody+4*i+2) - *(pBody+4*j+2);
             		dz = *(pBody+4*i+3) - *(pBody+4*j+3);
             		sq = dx*dx + dy*dy + dz*dz;
             		dist = sqrt(sq);
             		fac = (*(pBody+4*i)) * (*(pBody+4*j)) / ( dist * sq );
             		fx = fac * dx;
             		fy = fac * dy;
             		fz = fac * dz;

             		/*Add in force and opposite force to particle i and j */
		      	pthread_mutex_lock(&mutex1);
              		*(pForce+3*i) = *(pForce+3*i) - fx;
              		*(pForce+3*i+1) = *(pForce+3*i+1) - fy;
              		*(pForce+3*i+2) = *(pForce+3*i+2) - fz;
              		*(pForce+3*j) = *(pForce+3*j) + fx;
              		*(pForce+3*j+1) = *(pForce+3*j+1) + fy;
              		*(pForce+3*j+2) = *(pForce+3*j+2) + fz;
 		      	pthread_mutex_unlock(&mutex1);
         	}
         	for ( j=b; j<i; j++ ){/*Calculate force between particle i and j according to Newton's Law*/
		     	if ( i==j ) continue;
             		dx = *(pBody+4*i+1) - *(pBody+4*j+1);
             		dy = *(pBody+4*i+2) - *(pBody+4*j+2);
             		dz = *(pBody+4*i+3) - *(pBody+4*j+3);
             		sq = dx*dx + dy*dy + dz*dz;
             		dist = sqrt(sq);
             		fac = (*(pBody+4*i)) * (*(pBody+4*j)) / ( dist * sq );
             		fx = fac * dx;
             		fy = fac * dy;
             		fz = fac * dz;

             		/*Add in force and opposite force to particle i and j */
		     	pthread_mutex_lock(&mutex1);
              		*(pForce+3*i) = *(pForce+3*i) - fx;
              		*(pForce+3*i+1) = *(pForce+3*i+1) - fy;
              		*(pForce+3*i+2) = *(pForce+3*i+2) - fz;
              		*(pForce+3*j) = *(pForce+3*j) + fx;
              		*(pForce+3*j+1) = *(pForce+3*j+1) + fy;
              		*(pForce+3*j+2) = *(pForce+3*j+2) + fz;
 		      	pthread_mutex_unlock(&mutex1);
         	}
    	}

	//synchronization: to ensure that the computation of force on each body has been completed before updating its state
	pthread_mutex_lock (&mutex2);
	progress++;
	if ( progress!=thread_num){
		pthread_cond_wait( &cond, &mutex2);
	}else {
		pthread_cond_broadcast(&cond);
		progress = 0;
	}
	pthread_mutex_unlock (&mutex2);

    	for ( i=myID; i<BodyNum; i+=thread_num ){ 
	  	*(pBody+4*i+1) = *(pBody+4*i+1) + (*(pForce+3*i)) / (*(pBody+4*i));
	  	*(pForce+3*i) = 0;
	  	*(pBody+4*i+2) = *(pBody+4*i+2) + (*(pForce+3*i+1)) / (*(pBody+4*i));
	  	*(pForce+3*i+1) = 0;
	  	*(pBody+4*i+3) = *(pBody+4*i+3) + (*(pForce+3*i+2)) / (*(pBody+4*i));
	  	*(pForce+3*i+2) = 0;
    	}

	//synchronization: to ensure that each thread has completed the update of body states before the next step simulation
	pthread_mutex_lock (&mutex2);
	progress++;
	if ( progress!=thread_num){
		pthread_cond_wait( &cond, &mutex2);
	}else {
		pthread_cond_broadcast(&cond);
		progress = 0;
	}
	
	pthread_mutex_unlock (&mutex2);
    	t++;
  }

  return((void*)NULL);
}
Example #8
0
void testKetje()
{
    int keySizeInBits;
    int keyMaxSizeInBits = KeccakF_width - 18;
#if (KeccakF_width == 200)
    #ifdef OUTPUT
        FILE *f = fopen("KetjeJr.txt", "w");
    #endif
    const unsigned char *expected = (unsigned char *)"\x3b\x7d\xea\x9d\xf3\xe0\x58\x06\x98\x92\xc3\xc0\x05\x0f\x4b\xfd";
#endif
#if (KeccakF_width == 400)
    #ifdef OUTPUT
        FILE *f = fopen("KetjeSr.txt", "w");
    #endif
    const unsigned char *expected = (unsigned char *)"\x4a\x31\xc7\x51\x18\x7f\x03\x2c\x78\xc3\xcf\x36\x51\x0b\xe3\xb3";
#endif
    Ketje_Instance checksum;
    unsigned char overallChecksum[16];

#ifdef OUTPUT
    assert(f != NULL);
#endif

	Ketje_Initialize(&checksum, 0, 0, 0, 0);

    for( keySizeInBits=keyMaxSizeInBits; keySizeInBits >=96; keySizeInBits -= (keySizeInBits > 200) ? 100 : ((keySizeInBits > 128) ? 27 : 16))
	{
        int nonceMaxSizeInBits = keyMaxSizeInBits - keySizeInBits;
        int nonceSizeInBits;
        for(nonceSizeInBits = nonceMaxSizeInBits; nonceSizeInBits >= ((keySizeInBits < 112) ? 0 : nonceMaxSizeInBits); nonceSizeInBits -= (nonceSizeInBits > 128) ? 161 : 65)
		{
            Ketje_Instance ketje1;
            Ketje_Instance ketje2;
            unsigned char key[50];
            unsigned char nonce[50];
			unsigned int ADlen;

#ifdef OUTPUT
            printf( "Ketje%s, key length is %u bits, nonce length is %u bits\n",
                #if (KeccakF_width == 200)
                    "Jr",
                #endif
                #if (KeccakF_width == 400)
                    "Sr",
                #endif
                    keySizeInBits, nonceSizeInBits );
#endif
            generateSimpleRawMaterial(key, 50, 0x12+nonceSizeInBits, KeccakF_width);
            generateSimpleRawMaterial(nonce, 50, 0x23+keySizeInBits, KeccakF_width);

            errorIfNotZero( Ketje_Initialize( &ketje1, key, keySizeInBits, nonce, nonceSizeInBits) );
            errorIfNotZero( Ketje_Initialize( &ketje2, key, keySizeInBits, nonce, nonceSizeInBits) );

			if ( (keySizeInBits % 8) != 0)
			{
				key[keySizeInBits / 8] &= (1 << (keySizeInBits % 8)) - 1;
			}
			if ( (nonceSizeInBits % 8) != 0)
			{
				nonce[nonceSizeInBits / 8] &= (1 << (nonceSizeInBits % 8)) - 1;
			}

#ifdef OUTPUT
			fprintf(f, "***\n");
			fprintf(f, "initialize with key of %u bits, nonce of %u bits:\n", keySizeInBits, nonceSizeInBits );
			displayByteString(f, "key", key, (keySizeInBits+7)/8);
			displayByteString(f, "nonce", nonce, (nonceSizeInBits+7)/8);
			fprintf(f, "\n");
#endif

            for( ADlen=0; ADlen<=400; ADlen=ADlen+ADlen/3+1+(keySizeInBits-96)+nonceSizeInBits/32)
			{
				unsigned int Mlen;
                for( Mlen=0; Mlen<=400; Mlen=Mlen+Mlen/2+1+ADlen+((ADlen == 0) ? (keySizeInBits-96) : (nonceSizeInBits/4+keySizeInBits/2)))
				{
                    unsigned char associatedData[400];
                    unsigned char plaintext[400];
                    unsigned char ciphertext[400];
                    unsigned char plaintextPrime[400];
                    unsigned char tag1[16], tag2[16];
					//printf("ADlen %u, Mlen %u\n", ADlen, Mlen);
                    generateSimpleRawMaterial(associatedData, ADlen, 0x34+Mlen, 3);
                    generateSimpleRawMaterial(plaintext, Mlen, 0x45+ADlen, 4);

                    {
                        unsigned int split = myMin(ADlen/4, (unsigned int)200);
						unsigned int i;

//                      errorIfNotZero( Ketje_FeedAssociatedData( &ketje1, associatedData,0 ) );
                        for(i=0; i<split; i++)
                            errorIfNotZero( Ketje_FeedAssociatedData( &ketje1, associatedData+i, 1) );
                        if (split < ADlen)
                            errorIfNotZero( Ketje_FeedAssociatedData( &ketje1, associatedData+split, ADlen-split) );
                    }
                    errorIfNotZero( Ketje_FeedAssociatedData( &ketje2, associatedData, ADlen) );

                    {
                        unsigned int split = Mlen/3;
                        memcpy(ciphertext, plaintext, split);
                        errorIfNotZero( Ketje_WrapPlaintext( &ketje1, ciphertext, ciphertext, split) ); // in place
                        errorIfNotZero( Ketje_WrapPlaintext( &ketje1, plaintext+split, ciphertext+split, Mlen-split) );
                    }

                    {
                        unsigned int split = Mlen/3*2;
                        memcpy(plaintextPrime, ciphertext, split);
                        errorIfNotZero( Ketje_UnwrapCiphertext(&ketje2, plaintextPrime, plaintextPrime, split) ); // in place
                        errorIfNotZero( Ketje_UnwrapCiphertext(&ketje2, ciphertext+split, plaintextPrime+split, Mlen-split) );
                    }
                    errorIfNotZero( memcmp(plaintext, plaintextPrime, Mlen) );

                    errorIfNotZero( Ketje_GetTag( &ketje1, tag1, 16) );
                    errorIfNotZero( Ketje_GetTag( &ketje2, tag2, 16) );
                    errorIfNotZero( memcmp(tag1, tag2, 16) );

                    Ketje_FeedAssociatedData(&checksum, ciphertext, Mlen);
                    Ketje_FeedAssociatedData(&checksum, tag1, 16);

#ifdef OUTPUT
					displayByteString(f, "associated data", associatedData, ADlen);
					displayByteString(f, "plaintext", plaintext, Mlen);
					displayByteString(f, "ciphertext", ciphertext, Mlen);
					displayByteString(f, "tag", tag1, 16);
					fprintf(f, "\n");
#endif
                }
            }
        }
    }
    Ketje_WrapPlaintext(&checksum, 0, 0, 0);
    Ketje_GetTag(&checksum, overallChecksum, 16);
#ifdef OUTPUT
    displayByteString(f, "overall checksum", overallChecksum, 16);
    fclose(f);
#endif
    assert(memcmp(overallChecksum, expected, 16) == 0);
}