Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

multiple file saving using single threads in c++

Status
Not open for further replies.

syedshan

Advanced Member level 1
Advanced Member level 1
Joined
Feb 27, 2012
Messages
463
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,308
Location
Jeonju, South Korea
Activity points
5,134
hello every one,

I am receiveing very fast DMA data from FPGA based on PCIe protocol, so I have to save them in file in .txt format and I try to make the protoype of it but it does not work at all. Note that I am usually the FPGA programmer and not much use the c++ and this is the very first time using threads so kinda stuck in here.

So in this prototype, I am inilizating the Global variable pointer (much like I decide to do in the actual thing) and then passing data to it, simulatnously I am activating flags which will activate the file saving in the respective threads. please see the code below. Unfortunately I am unsuccessful in writing to both files.

Moreover I am getting the following error as well (see image below)

Note please find the attached files as well.
Code:
#define BUF_SIZE 255
#define size_of_memory 40

typedef signed short int ssint;

ssint *pInDMA = (ssint *)_aligned_malloc( sizeof(ssint)*(size_of_memory)*2, 4096);
int flag_oper = 0;
 
/*Function to save file */
DWORD WINAPI save_uwpi_file0( LPVOID )
{
//signed short *mem;
ssint *Lmem; int w_cnt = 0;
FILE *opfile;
char fname[30] = "txt_file0.txt";
//opening file for write
opfile = fopen(fname , "w");

do{
printf("assigning memory for file 1 \n");
Lmem = (ssint *) pInDMA;

for( int nbr = 0; nbr < size_of_memory; nbr++){
	fprintf(opfile , "%hi\n", Lmem[nbr] );
	}
printf("aligned free 1\n");
_aligned_free(Lmem);

fclose(opfile);

printf("File saved 1\n\n");
return 1;
}	while ((flag_oper & 0x01) != 0x01);
}


/*Function to save file */
DWORD WINAPI save_uwpi_file1( LPVOID )
{
//signed short *mem;
ssint *Lmem2; int w_cnt = 0;
FILE *opfile;
char fname[30] = "txt_file1.txt";
//opening file for write
opfile = fopen(fname , "w");

do{
	
printf("assigning memory for file 2 \n");
Lmem2 = (ssint *) pInDMA;

for( int nbr = 0; nbr < size_of_memory; nbr++){	
	fprintf(opfile , "%hi\n", Lmem2[nbr] );
	}
_aligned_free(Lmem2);

fclose(opfile);

printf("File saved 2\n\n");
return 1;
} 
while((flag_oper & 0x02) != 0x02) ;

}

// Main thread, for memory buffer data writing
// This causes the other threads to write in the files
DWORD WINAPI mem_data_gen( LPVOID) {

int cnt = 0;
int gt = 0;
for (cnt = 0; cnt < 2; cnt ++ ){
	
	for (int x =0 ;x < size_of_memory; x++) 
		pInDMA[x] = x*(cnt+1);
	gt ++; 
	switch(gt) {	
	case 1 :  { flag_oper = 0x01; 	break;	 }
	case 2 :  { flag_oper = 0x02; 	break; 	 }
	}
}
return 1;

}

void main()
{            
    // Aray to store thread handles 
    HANDLE Array_Of_Thread_Handles[3] = {0, 0, 0};

    // Create thread 1 (mem_data_gen).
    Array_Of_Thread_Handles[0] = CreateThread( NULL, 0, mem_data_gen, NULL, 0, NULL);  
    //if ( Array_Of_Thread_Handles[0] == NULL)  ExitProcess(Data_Of_Thread_1);     
    
	// Create thread 2 (save_uwpi_file0).
    Array_Of_Thread_Handles[1] = CreateThread( NULL, 0, save_uwpi_file0, NULL, 0, NULL);  
    //if ( Array_Of_Thread_Handles[1] == NULL)  ExitProcess(Data_Of_Thread_2);
    
	// Create thread 2 (save_uwpi_file1).
	Array_Of_Thread_Handles[2] = CreateThread( NULL, 0, save_uwpi_file1, NULL, 0, NULL);  
    //if ( Array_Of_Thread_Handles[1] == NULL)  ExitProcess(Data_Of_Thread_2);
	

    // Wait until all threads have terminated.
    WaitForMultipleObjects( 3, Array_Of_Thread_Handles, TRUE, INFINITE);

    printf("Since All threads executed lets close their handles \n");

    // Close all thread handles upon completion.
    CloseHandle(Array_Of_Thread_Handles);
}

error.JPG
 

Attachments

  • txt_file0.txt
    155 bytes · Views: 56
  • txt_file1.txt
    267 bytes · Views: 60

Pretty confuse. Using the same globally allocated memory for multiple threads and freeing it mutiple times.

Any reason why you dont allocate the memory for each thread separately?
 

Pretty confuse. Using the same globally allocated memory for multiple threads and freeing it mutiple times.

Any reason why you dont allocate the memory for each thread separately?

because I have around 2000 files in the future. I will take DMA data from PCIe 2000 times (or the other version is 500 times) and then save them in files, since DMA transfer is SOOOO fast and when I save them individually in files, file saving take lots of time hence the purpose of DMA died. Hence the other thing was, one thread continues to take data, transfer it to other threads, while other threads start saving them in file and gets close as soon as they are done, dynamically freeing the individual memories alloted within them. Hope this helps to understand
 

Your consideration don't change the simple fact that separate threads need to allocate separate memory.

The other question is which adavantage are you expecting from running multiple parallel threads. Even if you have enough processor RAM to run 2000 threads in parallel, this will only increase overall execution time.
 

Your consideration don't change the simple fact that separate threads need to allocate separate memory.

yes but they are allocated dynamically, I will implement the flags, at which point all the threads will be at halt and only when flags become active then the allocation occurs. and once the file written the memory is freed back into the pool. Hence at each time, lets say 20 to 30 files would be opened and their respective memory would be allocated.
This is my proposed idea and I said I am not much of a software guy at such high level.


The other question is which adavantage are you expecting from running multiple parallel threads. Even if you have enough processor RAM to run 2000 threads in parallel, this will only increase overall execution time.

any other idea would be appreciable. Now I am getting all my data 500 DMA transfers of 40000 WORDS (500*40000*2 Bytes) in 0.6 seconds. so for example, roughly 1 file => 0.6 /500 secs => 1.3 us

I want to build the real-time system, we can increase our time somewhat more for example total time goes to 5.0 seconds etc. but not more.

Any other idea would be appreciable (keeping in view c++ and visual studio 2010)

bests,
Shan
 

Referring to your original post. The error occurs because you are freeing the memory but continue to use it. That must never happen.

The other point is that you don't implement a save method to synchronize thread execution. do ... while won't work for it.

The bottleneck in file I/O is the media access, including directory and file allocation table reads and writes. The operations have to be performed for each file, effectively sequentially. It's not necessarily speeded up by distributing it to multiple threads.
 

Hi thanks again,


The bottleneck in file I/O is the media access, including directory and file allocation table reads and writes. The operations have to be performed for each file, effectively sequentially. It's not necessarily speeded up by distributing it to multiple threads.

I understand this thing, for this reason I am looking for prospective solutions, since in my point of view the threading was the better option hence I consider this. Is there any other idea that I can implement. or in threading is there some other options. I am doing search constantly over internet but the things are quite vast.

The other point is that you don't implement a save method to synchronize thread execution. do ... while won't work for it.

oh ok... this is silly mistake...I think I confused this part with the hardware implentation in VHDL. I was also thinking of including if-else statement in some loop but I guess that wont work either !

OK. So to summarize my case, our actual final goal is to implement the real-time based system (now I am doing on post-processing only where the file is read from PC, data save into DDR3 with some algorithm implementation and then read through DMA where we have this problem) which cannot be achieved if the file saving is not concurrent and does not lower the benefit of DMA.


thanks

- - - Updated - - -

Referring to your original post. The error occurs because you are freeing the memory but continue to use it. That must never happen.

oh my God...I thought soooo mistakenly typecast would give the memory data to the other memory address. Only now after testing I know that it will merely cast that same memory location into the different type, hence when I free that memory, it will be freed in its original type as well.... as in below code, 'p' will get free as the 'q' get free


Code:
typedef short int ssint; 
typedef signed long int slint; 

int main(){
	ssint *p = (ssint *) malloc(sizeof(ssint)*10); 
	ssint *q;

	for(int g=0; g < 10; g++){
		p[g] = g;
	}

	q = (ssint *) p;

	printf("printing values of q\n");
	for (int g=0; g < 10; g++)	printf("%d\n", q[g]);
	
	printf("printing values of p\n");
	for (int g=0; g < 10; g++)	printf("%d\n", p[g]);

	printf("freeing q\n");
	free(q); //this will free 'p' as well

}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top