如何在OpenCL中读取文本文件

时间:2014-12-22 10:30:38

标签: java opencl jocl

我是OpenCL的新手。有人知道如何通过OpenCL读取文本文件吗?因为我希望能够读取文件并将内容拆分为某些数组。 例如,我有txt文件包含: 1010101 1 1010111 。我想将1010101作为数组a,将1010111作为数组b。在这里,我们看到一个标签/空格,它有一个标签分隔符,带有" 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 0001000000000000000000000000100000000000000000000001000000000100000000000001001100000000000000000010000000000000000100100000000000000000000000000000000100000 0000000000000000000100000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100001000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110000000000000000000100000000000000000001000000000010000000010000000010000000000000000000000000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100

从上面的例子中,我想拆分为: 第一阵列: 00010000000000000000000000001000000000000000000000110000000001000000000010010111000000000000000000100000000000000001001000000000000000000000000000000001000000000000000000000000100000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100101000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110010000000000000000100000000000000000001000000000010000000010000000010000000000000000000010000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100 第二阵列: 00010000000000000000000000001000000000000000000000110000000001000000000010010111000000000000000000100000000000000001001000000000000000000000000000000001000000000000000000000000100000100000000000001000000000000001010000000000010000000000000000001000000000000001000000001000001100101000000000000010000000000010000000000000000000000000010000000000000110000000000001000000000000000000000100000000001001001110010000000000000000100000000000000000001000000000010000000010000000010000000000000000000010000000000100110000001000000000000000000000000001000000000000000100000000000100000000000000000000010000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000100000110000000000000000000000000000000010000000000000000000010000000000000000000000100

每个数组由800个字节组成(来自数组0-799)。分隔符(分隔符),我将其粗体 1

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您似乎想要阅读fixed width flat file

强烈建议:考虑使用像flatpack这样的库:http://flatpack.sourceforge.net/

以下是一些可能有用的其他链接:

1)显然你有一个“制表符分隔文件”。好的 - 您绝对可以将其拆分为String.split("\t")的字符串数组。

2)在你的示例中:

  

我有txt文件包含:1010101 1 1010111.我想要   将1010101作为数组a,将1010111作为数组b。

如果分隔符始终为“\ t1”,则可以使用。如果分隔符可能是"\t2""\t1000",那么您可能希望:

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

  • 在您的示例中,它将为您提供三个数组:“1010101”,“1010101”......和“1”。

  • 相应地解析“array [1]”(值“1”)。

3)我不确定“阵列”是什么意思。

在您的示例中,值“1010101”和“1010101”将是 ONE,SINGLE 数组中的两个元素。

但这应该不是问题:您可以随时使用myArray[n].charAt(i)

或者,您也可以随时将字符串转换为字符数组:

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