Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
8 Views

Function returning odd results

I have a function in my program that has many for loops. After some trial and error I finally got it working and was quicker but the problem is that its returning incorrect data for the results. Im guessing i dont have the proper private and shared settings right in my code
[cpp]int FILE2TEMP = FILES2;

int CountCol2, CountCol, CountRow, CountFile, CountGroup;

#pragma omp parallel for private(CountRow,CountCol2,CountCol,CountFile,CountGroup,Sub1,Sub2,SubFinal)
for(CountGroup = 0;CountGroup<=(GROUPS - 2);CountGroup++)
{
   if(CountGroup == 1)
      continue;
   Sub1 = tf[CountGroup][0]->numticklines;
   Sub2 = tf[CountGroup+1][0]->numticklines;
   if(Sub1 <= Sub2)
      TotalRows = Sub1;
   if(Sub1 > Sub2)
      TotalRows = Sub2;
   for(CountFile = 1; CountFile<=(FILE2TEMP - 1); CountFile++)
   {
      BiggestCompare[0] = 0;
      BiggestCompare2[0] = 0;
      BiggestCompareLoss[0] = 0;
      BiggestCompare2Loss[0] = 0;
      for(CountRow = 0; CountRow<=(TotalRows - 1); CountRow++)
      {
         for(CountCol = 0; CountCol<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol++)
         {
            for(CountCol2 = 0; CountCol2<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol2++)
            {
               Sub1 = tf[CountGroup][0]->lines[CountRow]->tickdata[CountCol];
               Sub2 = tf[CountGroup][CountFile]->lines[CountRow]->tickdata[CountCol2];
               if(Sub1 <= Sub2)
                  BigCompareArray1[CountCol][CountCol2] = BigCompareArray1[CountCol][CountCol2] + 1;

               if(Sub1 > Sub2)
                  BigCompareArray2[CountCol][CountCol2] = BigCompareArray2[CountCol][CountCol2] + 1;



               Sub1 = tf[CountGroup+1][0]->lines[CountRow]->tickdata[CountCol];
               Sub2 = tf[CountGroup+1][CountFile]->lines[CountRow]->tickdata[CountCol2];
               if(Sub1 <= Sub2)
                  BigCompareArray3[CountCol][CountCol2] = BigCompareArray3[CountCol][CountCol2] + 1;

               if(Sub1 > Sub2)
                  BigCompareArray4[CountCol][CountCol2] = BigCompareArray4[CountCol][CountCol2] + 1;
            }
         }
      }

      for(CountCol = 0; CountCol<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol++)
      {
         for(CountCol2 = 0; CountCol2<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol2++)
         {
            Sub1 = BigCompareArray1[CountCol][CountCol2];
            Sub2 = BigCompareArray3[CountCol][CountCol2];
            BigCompareArray1[CountCol][CountCol2] = 0;
            BigCompareArray3[CountCol][CountCol2] = 0;

            if(Sub2 >= 0)
               SubFinal = Sub1 - Sub2;
            if(Sub2 < 0 && Sub1 >= 0)
               SubFinal = Sub2 + Sub1;
            if(Sub2 < 0 && Sub1 < 0)
               SubFinal = Sub2 - Sub1;
            if(SubFinal > BiggestCompare[0])
            {
               BiggestCompare[0] = SubFinal;
               BiggestCompare[1] = CountCol;
               BiggestCompare[2] = CountCol2;
            }
            if(SubFinal < BiggestCompare2[0])
            {
               BiggestCompare2[0] = SubFinal;
               BiggestCompare2[1] = CountCol;
               BiggestCompare2[2] = CountCol2;
            }


            Sub1 = BigCompareArray2[CountCol][CountCol2];
            Sub2 = BigCompareArray4[CountCol][CountCol2];
            BigCompareArray2[CountCol][CountCol2] = 0;
            BigCompareArray4[CountCol][CountCol2] = 0;

            if(Sub2 >= 0)
               SubFinal = Sub1 - Sub2;
            if(Sub2 < 0 && Sub1 >= 0)
               SubFinal = Sub2 + Sub1;
            if(Sub2 < 0 && Sub1 < 0)
               SubFinal = Sub2 - Sub1;
            if(SubFinal > BiggestCompareLoss[0])
            {
               BiggestCompareLoss[0] = SubFinal;
               BiggestCompareLoss[1] = CountCol;
               BiggestCompareLoss[2] = CountCol2;
            }
            if(SubFinal < BiggestCompare2Loss[0])
            {
               BiggestCompare2Loss[0] = SubFinal;
               BiggestCompare2Loss[1] = CountCol;
               BiggestCompare2Loss[2] = CountCol2;
            }
         }
      }

      char buffA[40]={'�'};
      char c[3] = {'�'};
      strcat(buffA, filname1);
      strcat(buffA, itoa((CountGroup + 1), c, 10));
      strcat(buffA, filname2);
      f = fopen(buffA, "a");
      fprintf (f, "%d File:n",CountFile);
      fprintf (f, "%d<%d Final %dn",BiggestCompare[1], BiggestCompare[2], BiggestCompare[0]);
      fprintf (f, "%d<%d Final %dn",BiggestCompare2[1], BiggestCompare2[2], BiggestCompare2[0]);
      fprintf (f, "%d>%d Final %dn",BiggestCompareLoss[1], BiggestCompareLoss[2], BiggestCompareLoss[0]);
      fprintf (f, "%d>%d Final %dnnn",BiggestCompare2Loss[1], BiggestCompare2Loss[2], BiggestCompare2Loss[0]);
      fclose (f);
   }
}[/cpp]

Im guessing both sets of arrays and the struct might need certain omp settings so the memory doesn't get screwed up. Any help would be appreciated.
0 Kudos
3 Replies
Highlighted
8 Views


Move all the declarations of your per grouplocal data to inside the omp for loop. Include the group results data (Big....).
i.e. currently all of your threads are overwriting BiggestCompare, BiggestCompare2, etc... include Sub1, Sub2, SubFinal (remove from private too).

Jim Dempsey
0 Kudos
Highlighted
Beginner
8 Views


Move all the declarations of your per grouplocal data to inside the omp for loop. Include the group results data (Big....).
i.e. currently all of your threads are overwriting BiggestCompare, BiggestCompare2, etc... include Sub1, Sub2, SubFinal (remove from private too).

Jim Dempsey

Thanks Jim for the input. I did what you said and moved all the delarations within the parallel region. It didn't error at all but the results aren't the same as sequential results. Heres what i got now in the code
[cpp]void tickfile_firstcompare(struct tickfile *tf[GROUPS][FILES2])
{
	#pragma omp parallel
	{
		int Sub1, Sub2, Sub3, Sub4, Sub5, Sub6, SubFinal;
		int TotalRows;

		int sum=0;

		int BiggestCompare[3];
		int BiggestCompare2[3];
		int BiggestCompareLoss[3];
		int BiggestCompare2Loss[3];

		static int BigCompareArray1[COLUMNS][COLUMNS];
		static int BigCompareArray2[COLUMNS][COLUMNS];
		static int BigCompareArray3[COLUMNS][COLUMNS];
		static int BigCompareArray4[COLUMNS][COLUMNS];

		char filname1[]="C:comparedfinalfirstcompare";
		char filname2[]=".txt";
		FILE *f;

		int FILE2TEMP = FILES2;

		int CountCol2, CountCol, CountRow, CountFile, CountGroup, CounTemp;

		#pragma omp for
		for(CountGroup = 0;CountGroup<=(GROUPS - 2);CountGroup++)
		{
			if(CountGroup == 1)
				continue;
			Sub3 = tf[CountGroup][0]->numticklines;
			Sub4 = tf[CountGroup+1][0]->numticklines;
			if(Sub3 <= Sub4)
				TotalRows = Sub3;
			if(Sub3 > Sub4)
				TotalRows = Sub4;
			for(CountFile = 1; CountFile<=(FILE2TEMP - 1); CountFile++)
			{
				BiggestCompare[0] = 0;
				BiggestCompare2[0] = 0;
				BiggestCompareLoss[0] = 0;
				BiggestCompare2Loss[0] = 0;
				for(CountRow = 0; CountRow<=(TotalRows - 1); CountRow++)
				{
					for(CountCol = 0; CountCol<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol++)
					{
						for(CountCol2 = 0; CountCol2<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol2++)
						{
							Sub1 = tf[CountGroup][0]->lines[CountRow]->tickdata[CountCol];
							Sub2 = tf[CountGroup][CountFile]->lines[CountRow]->tickdata[CountCol2];
							if(Sub1 <= Sub2)
							{
								sum = BigCompareArray1[CountCol][CountCol2];
								sum += 1;
								BigCompareArray1[CountCol][CountCol2] = sum;
							}

							if(Sub1 > Sub2)
							{
								sum = BigCompareArray2[CountCol][CountCol2];
								sum += 1;
								BigCompareArray2[CountCol][CountCol2] = sum;
							}



							Sub5 = tf[CountGroup+1][0]->lines[CountRow]->tickdata[CountCol];
							Sub6 = tf[CountGroup+1][CountFile]->lines[CountRow]->tickdata[CountCol2];
							if(Sub5 <= Sub6)
							{
								sum = BigCompareArray3[CountCol][CountCol2];
								sum += 1;
								BigCompareArray3[CountCol][CountCol2] = sum;
							}

							if(Sub5 > Sub6)
							{
								sum = BigCompareArray4[CountCol][CountCol2];
								sum += 1;
								BigCompareArray4[CountCol][CountCol2] = sum;
							}
						}
					}
				}
				for(CountCol = 0; CountCol<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol++)
				{
					for(CountCol2 = 0; CountCol2<=(tf[CountGroup][0]->lines[0]->numtickdata - 1); CountCol2++)
					{
						Sub1 = BigCompareArray1[CountCol][CountCol2];
						Sub2 = BigCompareArray3[CountCol][CountCol2];
						BigCompareArray1[CountCol][CountCol2] = 0;
						BigCompareArray3[CountCol][CountCol2] = 0;

						if(Sub2 >= 0)
							SubFinal = Sub1 - Sub2;
						if(Sub2 < 0 && Sub1 >= 0)
							SubFinal = Sub2 + Sub1;
						if(Sub2 < 0 && Sub1 < 0)
							SubFinal = Sub2 - Sub1;
						if(SubFinal > BiggestCompare[0])
						{
							BiggestCompare[0] = SubFinal;
							BiggestCompare[1] = CountCol;
							BiggestCompare[2] = CountCol2;
						}
						if(SubFinal < BiggestCompare2[0])
						{
							BiggestCompare2[0] = SubFinal;
							BiggestCompare2[1] = CountCol;
							BiggestCompare2[2] = CountCol2;
						}


						Sub1 = BigCompareArray2[CountCol][CountCol2];
						Sub2 = BigCompareArray4[CountCol][CountCol2];
						BigCompareArray2[CountCol][CountCol2] = 0;
						BigCompareArray4[CountCol][CountCol2] = 0;

						if(Sub2 >= 0)
							SubFinal = Sub1 - Sub2;
						if(Sub2 < 0 && Sub1 >= 0)
							SubFinal = Sub2 + Sub1;
						if(Sub2 < 0 && Sub1 < 0)
							SubFinal = Sub2 - Sub1;
						if(SubFinal > BiggestCompareLoss[0])
						{
							BiggestCompareLoss[0] = SubFinal;
							BiggestCompareLoss[1] = CountCol;
							BiggestCompareLoss[2] = CountCol2;
						}
						if(SubFinal < BiggestCompare2Loss[0])
						{
							BiggestCompare2Loss[0] = SubFinal;
							BiggestCompare2Loss[1] = CountCol;
							BiggestCompare2Loss[2] = CountCol2;
						}
					}
				}
				char buffA[40]={'�'};
				char c[3] = {'�'};
				strcat(buffA, filname1);
				strcat(buffA, itoa((CountGroup + 1), c, 10));
				strcat(buffA, filname2);
				if(CountFile == 1)
					f = fopen(buffA, "w");
				else
					f = fopen(buffA, "a");
				if (f == NULL)
					continue;
				fprintf (f, "%d File:n",CountFile);
				printf ("%d File:n",CountFile);

				fprintf (f, "%d<%d Final %dn",BiggestCompare[1], BiggestCompare[2], BiggestCompare[0]);
				printf ("1 %d<%d Final %dn",BiggestCompare[1], BiggestCompare[2], BiggestCompare[0]);

				fprintf (f, "%d<%d Final %dn",BiggestCompare2[1], BiggestCompare2[2], BiggestCompare2[0]);
				printf ("2 %d<%d Final %dn",BiggestCompare2[1], BiggestCompare2[2], BiggestCompare2[0]);

				fprintf (f, "%d>%d Final %dn",BiggestCompareLoss[1], BiggestCompareLoss[2], BiggestCompareLoss[0]);
				printf ("3 %d>%d Final %dn",BiggestCompareLoss[1], BiggestCompareLoss[2], BiggestCompareLoss[0]);

				fprintf (f, "%d>%d Final %dnnn",BiggestCompare2Loss[1], BiggestCompare2Loss[2], BiggestCompare2Loss[0]);
				printf ("4 %d>%d Final %dnnn",BiggestCompare2Loss[1], BiggestCompare2Loss[2], BiggestCompare2Loss[0]);
				fclose (f);
			}
			//CountGroup++;
		}
	}
}[/cpp]

I have no idea why its not working the way it should. Any help would be appreciated.
0 Kudos
Highlighted
Beginner
8 Views

Ok i removed the static declartion from the big arrays and the results almost match sequential results. Its very strange.

Sequential results:
1 File:
116<76 Final 16
0<56 Final -23
0>56 Final 23
116>76 Final -16


2 File:
584<480 Final 25
352<100 Final -24
352>100 Final 24
584>480 Final -25

parallel results:
1 File:
0<56 Final 23
116<76 Final -16
116>76 Final 16
0>56 Final -23


2 File:
584<480 Final 25
352<100 Final -24
352>100 Final 24
584>480 Final -25

So you can on file 1 thats its got rows 1 and 2 mixed up and 3 and 4 mixed up too. File 2 seems to be ok. In the code i put printf statements in with numbers to see if it was just printing them out in the wrong order but no it went down in the right order. So only thing i can think of is that some how its putting what should be in BiggestCompare into BiggestCompare2 and so on with BiggestCompareLoss. How is it getting these two mixed up. Ill do some further testing to see if i can figure this out but any help would be appreciated.
0 Kudos