-1

私はOpenCLが初めてです。OpenCL を介してテキスト ファイルを読み取る方法を知っている人はいますか? ファイルを読み取って、コンテンツをいくつかの配列に分割できるようにしたいからです。例として、txt ファイルは1010101 1 1010111で構成されています。配列 a として 1010101 を、配列 b として 1010111 を取得したいと考えています。ここでは、タブ/スペースが表示されています。これには「1」のタブ区切り文字があるため、区切り文字「\t1」でも分割する必要があります。

ここに私のメインコードがあります:

for (int i = 0; i < N; i++) { 

            System.out.println("perturb node: "+i);
            cs=calcDistanceCs();//ok
            System.out.println("csTemp: "+Arrays.toString(cs));
            //System.out.println("csTemp: "+cs); 
            for (int j = 0; j < N; j++) {

                if (j!=i) {
                    System.out.println("target node: "+j);
                    nTarget=j;
                    rs=calcDistanceRs();

                    System.out.println("rsTemp: "+Arrays.toString(rs));

                    //System.out.println("rsTemp: "+rs);

                    eff=dtmp/numofRStates;
                    System.out.println("Effectiveness "+eff);

                    //print-out result
                    pw.println(i+"\t"+eff+"\t"+j);
                    // flush the writer
                    pw.flush();
                } 
            }
        }
        Pointer srcA = Pointer.to(cs);
        Pointer srcB = Pointer.to(rs);
        //Pointer dst = Pointer.to(resultArr);

        // The platform, device type and device number
        final int platformIndex = 0;
        final long deviceType = CL_DEVICE_TYPE_ALL;
        final int deviceIndex = 0;

        // Enable exceptions and subsequently omit error checks in this sample
        CL.setExceptionsEnabled(true);

        // Obtain the number of platforms
        int numPlatformsArray[] = new int[1];
        clGetPlatformIDs(0, null, numPlatformsArray);
        int numPlatforms = numPlatformsArray[0];

        //obtain a platform id
        System.out.println("Obtaining platform..."); //running, muncul
        cl_platform_id platforms[] = new cl_platform_id[numPlatforms];
        clGetPlatformIDs(platforms.length, platforms, null);
        cl_platform_id platform = platforms[platformIndex];
        System.out.println("Platform id: "+platform); //uda langsung ke string tanpa harus diconvert

        //Initialize the context properties
        cl_context_properties contextProperties = new cl_context_properties();
        contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform);        

        // Obtain the number of devices for the platform
        int numDevicesArray[] = new int[1];
        clGetDeviceIDs(platform, deviceType, 0, null, numDevicesArray);
        int numDevices = numDevicesArray[0];

        // Obtain a device ID 
        cl_device_id devices[] = new cl_device_id[numDevices];
        clGetDeviceIDs(platform, deviceType, numDevices, devices, null);
        cl_device_id device = devices[deviceIndex];

        // Create a context for the selected device
        cl_context context = clCreateContext(contextProperties, 1, new cl_device_id[]{device},null, null, null);  
        //create an opencl context on a gpu device
        //cl_context context=clCreateContextFromType(contextProperties,CL_DEVICE_TYPE_GPU,null,null,null);
        if (context==null) {
            /*if no context for a gpu device could be created,
            try to create one for a cpu device
            */
            System.out.println("Cannot create GPU context...");
            context=clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_CPU, null, null, null);
            if (context==null) {
                System.out.println("Unable to create a context");
                return;
            }else{
                System.out.println("Successfully created CPU context ^^");
            }
        }else{
            System.out.println("Successfully created GPU context...");
        }

        // Create a command-queue for the selected device
        cl_command_queue commandQueue = clCreateCommandQueue(context, device, 0, null);

        // Allocate the memory objects for the input- and output data
        cl_mem memObjects[] = new cl_mem[2];
        memObjects[0] = clCreateBuffer(context, 
                CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                Sizeof.cl_char * N, srcA, null);
        memObjects[1] = clCreateBuffer(context, 
                CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                Sizeof.cl_char * N, srcB, null);
        /*            memObjects[2] = clCreateBuffer(context,
        CL_MEM_READ_WRITE,
        Sizeof.cl_float * N, null, null);*/

        // Create the program from the source code

        System.out.println("Reading file from kernels...");
        String programSource=readFile("kernel_/CalEff.cl"); //membaca file dari luar folder src--sukses ^^

        //create the program from the source code 
        cl_program program=clCreateProgramWithSource(context,1,new String[]{programSource},null,null);        

        // Build the program
        clBuildProgram(program, 0, null, null, null, null);

        // Create the kernel
        cl_kernel kernel = clCreateKernel(program, "Effectiveness", null); //nama procedure yg dipanggil di kernel/opencl

        // Set the arguments for the kernel -->kernel, arg index,long arg size, pointer
        clSetKernelArg(kernel, 0, Sizeof.cl_mem, Pointer.to(memObjects[0]));//jumlahnya ada 3 cz yg dideklarasi jg ada3
        clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(memObjects[1]));
        clSetKernelArg(kernel, 2, Sizeof.cl_mem, Pointer.to(memObjects[2]));

        // Set the work-item dimensions
        long global_work_size[] = new long[]{N};
        long local_work_size[] = new long[]{1};//This means each item is a separate workgroup

        // Execute the kernel
        clEnqueueNDRangeKernel(commandQueue, kernel, 1, null,global_work_size, local_work_size, 0, null, null); //execure each array in parallel

        // Read the output data-->from c parameter /kalo dibutuhkan
        //clEnqueueReadBuffer(commandQueue, memObjects[2], CL_TRUE, 0,N * Sizeof.cl_float, dst, 0, null, null);
        /*BASIC END HERE*/

        // Release kernel, program, and memory objects
        clReleaseMemObject(memObjects[0]);
        clReleaseMemObject(memObjects[1]);
        clReleaseMemObject(memObjects[2]);
        clReleaseKernel(kernel);
        clReleaseProgram(program);
        clReleaseCommandQueue(commandQueue);
        clReleaseContext(context);

しかし、calcDistanceCs() と calcDistanceRs() の場合、どちらも txt ファイルを読み取る必要があります。次に、そのファイルから、次のような配列 (stateArray[]) を取得しました。

    FileReader fr;
    LineNumberReader lnr; 
    String str;
    int i;
    System.out.println("Calculate distance Current State: ");
    try{
        // create new reader
        fr = new FileReader("AttractorCs.txt");
        lnr = new LineNumberReader(fr);

        csLine=0;
        // read lines till the end of the stream
        while((str=lnr.readLine())!=null){
            i=lnr.getLineNumber();

            csBariske=i;
            csBaris=str;

            if(csBaris!=null){
                List<ModEff> listState= new LinkedList<>();
                String[] stateArray;            
                stateArray=csBaris.split("\t");

                node1=BinaryStringtoBinaryByteArrAwal(stateArray[0]);
                node2=BinaryStringtoBinaryByteArrAkhir(stateArray[2]);

            }
            if (csBariske % 2 == 1) {
                it_attcGanj=String.valueOf(node1)+String.valueOf(node2);  
            }else{
                it_attcGen=String.valueOf(node2)+String.valueOf(node1);

                it_att=it_attcGanj+it_attcGen;
                nodeCsArr = convertNodeArrToByteArr(it_att); 
            }
            csLine++;
        }

        lnr.close();
    }catch(Exception e){

       // if any error occurs
       e.printStackTrace();
    }
    System.gc();
    return nodeCsArr;

読む必要があるテキスト ファイルの例を次に示します。 00010000000000000000000000001000000000000000000000110000000001000000000010010111000000000000000000100000000000000001001000000000000000000000000000000001000000000000000000000000100000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100101000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110010000000000000000100000000000000000001000000000010000000010000000010000000000000000000010000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100 1 00010000000000000000000000001000000000000000000000010000000001000000000000010011000000000000000000100000000000000001001000000000000000000000000000000001000000000000000000000000100000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100001000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110000000000000000000100000000000000000001000000000010000000010000000010000000000000000000000000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100

上記の例から、次のように分割します: 1 番目の配列:2 番目の配列:00010000000000000000000000001000000000000000000000110000000001000000000010010111000000000000000000100000000000000001001000000000000000000000000000000001000000000000000000000000100000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100101000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110010000000000000000100000000000000000001000000000010000000010000000010000000000000000000010000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100000100101110000000000000000001000000000000000010010000000000000000000000000000000010000000000000000000000001000001000000000000010000000000000010100000000000100000000000000000010000000000000010000000010000011001010000000000000100000000000100000000000000000000000000100000000000001100000000000010000000000000000000001000000000010010011100100000000000000001000000000000000000010000000000100000000100000000100000000000000000000100000000001001100000010000000000000000000000000010000000000000001000000000001000000000000000000000100000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000100000000000000001000001100000000000000000000000000000000100000000000000000000100000000000000000000001000001001011100000000000000000010000000000000000100100000000000000000000000000000000100000000000000000000000010000010000000000000100000000000000101000000000001000000000000000000100000000000000100000000100000110010100000000000001000000000001000000000000000000000000001000000000000011000000000000100000000000000000000010000000000100100111001000000000000000010000000000000000000100000000001000000001000000001000000000000000000001000000000010011000000100000000000000000000000000100000000000000010000000000010000000000000000000001000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000001000000000000000010000011000000000000000000000000000000001000000000000000000001000000000000000000000010000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100101000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110010000000000000000100000000000000000001000000000010000000010000000010000000000000000000010000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100001000000000000010000000000000010100000000000100000000000000000010000000000000010000000010000011001010000000000000100000000000100000000000000000000000000100000000000001100000000000010000000000000000000001000000000010010011100100000000000000001000000000000000000010000000000100000000100000000100000000000000000000100000000001001100000010000000000000000000000000010000000000000001000000000001000000000000000000000100000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000100000000000000001000001100000000000000000000000000000000100000000000000000000100000000000000000000001001001000000000000000010000000000000000000100000000001000000001000000001000000000000000000001000000000010011000000100000000000000000000000000100000000000000010000000000010000000000000000000001000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000001000000000000000010000011000000000000000000000000000000001000000000000000000001000000000000000000000010010010000000000000000100000000000000000001000000000010000000010000000010000000000000000000010000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100

各配列は 800 バイト (配列 0 ~ 799) で構成されます。セパレーター(区切り文字)、太字にします1 .

4

1 に答える 1

1

あなたの質問を正しく理解していれば、固定幅のフラット ファイルを読みたいようです。

強い提案: flatpack のようなライブラリの使用を検討してください: http://flatpack.sourceforge.net/

役立つ可能性のあるその他のリンクを次に示します。

1)どうやら「タブ区切りファイル」を持っているようです。OK - で文字列の配列に分割できますString.split("\t")

2)あなたの例では:

私はtxtファイルで構成されています:1010101 1 1010111。配列aとして1010101、配列bとして1010111を取得したい。

区切り文字が常に「\t1」の場合、それは機能します。区切り文字が"\t2"または"\t1000"の場合は、次のようにします。

  • 使用するString.split("\t")

  • あなたの例では、「1010101」、「1010101」、「1」の3 つの配列が得られます。

  • それに応じて "array[1]" (値 "1") を解析します。

3)「配列」の意味がわかりません。

あなたの例では、値「1010101」と「1010101」は、ONE、SINGLE配列の 2 つの要素になります。

しかし、これは問題にはなりません。いつでも使用できますmyArray[n].charAt(i)

または、代わりに、いつでも文字列を文字配列に変換できます。

char[] charArray = myArray[n].toCharArray();

于 2014-12-25T01:42:35.053 に答える