cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Adventurer
Adventurer
3,738 Views
Registered: ‎11-25-2015

Initialize multidimension array from txt files

Jump to solution

Hello all,

 

I'm working on a project that needs me to initialize some data in the Memories before the program runs. Here is what I'm doing;

 

	double weights[N][K][H][W];               //RAM or ROM to store weights

	//---------------------------------------------------------------------
	//read from the weights txt file
	//---------------------------------------------------------------------
	double conv1_weights_buf[500];
	FILE *conv1_weights;
	conv1_weights = fopen("weights_out_conv1.txt","r");
	if(!conv1_weights){
		fprintf(stdout, "*******************************************\n");
		fprintf(stdout, "FAIL: no file read or file is empty!\n");
		fprintf(stdout, "*******************************************\n");
	}

	int i =0;
	while(!feof(conv1_weights)){
		fscanf(conv1_weights,"%lf",&conv1_weights_buf[i]);
		//printf("%lf",conv1_weights_buf[i]);
		//printf("    index: %d\n",i);
		i++;
	}
	fclose(conv1_weights);
	//read finished

	//---------------------------------------------------------------------
	//transfer the weights from the buffer into the weights variable
	//---------------------------------------------------------------------
	for(n=0;n<N;n++){
		for(k=0;k<K;k++){
			for(h=0;h<H;h++){
				for(w=0;w<W;w++){
					weights[n][k][h][w]=conv1_weights_buf[((n * K + k) * H + h) * W + w];
					printf("%lf\n",weights[n][k][h][w]);
				}
			}
		}
	}

But by doing so the synthesize program will infer an the memory conv1_weights_buf[500] which I don't want. I just want to put data in the weights[N][K][W][H] buffer. Can anyone tell me how to do the initialization?

 

Thanks!

 

Derick.

 

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Mentor
Mentor
6,591 Views
Registered: ‎04-26-2015

Re: Initialize multidimension array from txt files

Jump to solution

Is "weights" intended to be a ROM, or can those values change? UG902 has a bit of information on declaring a ROM. Generally:

 

(1) Declare it static.

 

(2) Immediately after it's declared, pass it to a function that populates it with the correct values, without relying on anything else in the code (ie does not take any non-constant inputs apart from the array, and does not access global variables).

 

(3) Don't ever change it (obviously if you change it then HLS cannot infer a ROM).

 

 

The other option is to use something like Matlab to write out the instantiation in full, then include that as a header file.

View solution in original post

0 Kudos
2 Replies
Highlighted
Mentor
Mentor
6,592 Views
Registered: ‎04-26-2015

Re: Initialize multidimension array from txt files

Jump to solution

Is "weights" intended to be a ROM, or can those values change? UG902 has a bit of information on declaring a ROM. Generally:

 

(1) Declare it static.

 

(2) Immediately after it's declared, pass it to a function that populates it with the correct values, without relying on anything else in the code (ie does not take any non-constant inputs apart from the array, and does not access global variables).

 

(3) Don't ever change it (obviously if you change it then HLS cannot infer a ROM).

 

 

The other option is to use something like Matlab to write out the instantiation in full, then include that as a header file.

View solution in original post

0 Kudos
Highlighted
Adventurer
Adventurer
3,498 Views
Registered: ‎11-25-2015

Re: Initialize multidimension array from txt files

Jump to solution

Thanks! I used python to generate a header file containing the values of the arrays, it works!

0 Kudos