offload mode cannot open file, warnings exist

offload mode cannot open file, warnings exist

hi~ all 

I want to use multiple threads to parallelize a for loop. Each iteration opens a file. I use the offload mode, and the compiler reports warning saying

"warning #2571: variable has not been declared with compatible "target" attribute
      ifstream fin(my_str.c_str(),ios::in);"

I ignored the warnings, but the program crashed.

What can I do to make the codes work? Thanks!

The compile command is:

icc -std=c++0x -openmp -O3 

The codes are these:

main.h

#ifndef MAIN_H
#define MAIN_H
#include "A.h"
#include <omp.h>
#include <sys/time.h>

using namespace std;

__attribute__((target(mic))) static double dtime()
{
	double tseconds = 0;
	struct timeval mytime;
	gettimeofday(&mytime,(struct timezone*)0);
	tseconds = (double)(mytime.tv_sec+(double)mytime.tv_usec*1e-6);
	return tseconds;
}
#endif

main.cpp

#include "main.h"

using namespace std;

int main()
{
	cout << "Start.\n";
	int N_total = 10, index_counter = 0, numthreads = 0;
	double t_begin = dtime(), t_end;
#pragma offload target (mic)
#pragma omp parallel
#pragma omp master
	numthreads = omp_get_num_threads();
	printf("Initializing.. %d Threads",numthreads);

#pragma offload target(mic)
#pragma omp  parallel for private (index_counter)
	for (index_counter=0; index_counter<N_total; index_counter++)
	{
		if (index_counter==0)
			printf("\n%d threads.\n", numthreads);
		string my_str = "content_string";
		vector< vector<int> > my_vec_vec;
		vector<int> my_vec;
		set<int> my_set;
		unordered_map<int,int> my_u_map;
		int my_int = 1;	
		double t_i_start=dtime(), t_i_end;
		A_func_1(my_str,my_vec_vec,my_set,my_u_map);
		
		t_i_end = dtime();
		A_func_2(my_vec_vec,my_u_map,my_int);
	}
	return 0;
}

A.h

#ifndef A_H
#define A_H
#include <omp.h>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>


using namespace std;

 __attribute__((target(mic))) void A_func_1(string my_str, vector<vector<int> >& my_vec_vec, set<int>& my_set, unordered_map<int,int>& my_u_map);
 __attribute__((target(mic))) void A_func_2(vector<vector<int> >& my_vec_vec, unordered_map<int,int>& my_u_map, int my_int=0);
#endif

A.cpp

#include "A.h"

using namespace std;

void A_func_1(string my_str, vector<vector<int> >& my_vec_vec, set<int>& my_set, unordered_map<int,int>& my_u_map)
{
	srand(time(NULL));
	int n_length = rand()%10+1;
	for (int i=0; i<n_length ;i++)
	{
		vector<int> temp_vec;
		int m_length = rand()%10+1;
		for (int j=0; j<m_length; j++)
			temp_vec.push_back(rand()%20);
		my_vec_vec.push_back(temp_vec);
	}
	string input_buffer;
	int int_buffer;
	ifstream fin(my_str.c_str(),ios::in);
	if (!fin.is_open())
		printf("file open error.\n");
	else
	{
		while (getline(fin,input_buffer,'\n'))
		{
			istringstream iss(input_buffer);
			while (iss>>int_buffer)
			{
				my_set.insert(int_buffer);
			}
		}
	}
}
void A_func_2(vector<vector<int> >& my_vec_vec, unordered_map<int,int>& my_u_map, int my_int)
{
	int n = my_vec_vec.size();
	for (int i=0; i<n; i++)
		for (int j=0; j<my_vec_vec[i].size(); j++)
			my_vec_vec[i][j] = j;
}

 

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

This appears to be (about) the same program posted in your subsequent post here (https://software.intel.com/en-us/forums/topic/517851) which compiles and runs based on my reply in the other thread, however, it still reports the file opened failed.

I will look into the open failure a bit more and then post an update in this thread when I know more.

Login to leave a comment.