Abstract:
A memory access method for parallel computing, which is applied in the case that n (n≧2) parallel threads invoke the same original execution module, comprises the following steps: S 1 ) determining the number k of mirror execution modules needed to be created based on the number n of threads, wherein n−1≧k≧1; S 2 ) determining the mapping policy F between the n threads and the execution modules including the original execution module and the k mirror execution modules; S 3 ) creating k mirror execution modules, and allocating the individual private memory pool; S 4 ) executing the n parallel threads, and making each thread invoke the execution module according to the mapping policy F, wherein each execution module only accesses its private memory pool; S 5 ) deleting the created mirror execution modules after finishing the thread operation, and releasing the resources. The invention can enable the efficiency of parallel computing to linearly increase as the number of cores increases, without the limitation of the memory access bottleneck. The method is universal in various operating systems, and single-thread programs can be directly transplanted into a multi-thread environment conveniently.

Description:
TECHNICAL FIELD 
       [0001]    The present invention relates to computer technology, particularly to memory access control in the multi-threaded PDF format file operations; and to a consequent memory access policy in the multi-threaded parallel computing, and to a method to improve the operational efficiency. 
       BACKGROUND ART 
       [0002]    The digitalization of the physical resources such as books, pictures, documents and so on usually takes the forms of scanning, recognition and extraction. The documents are first scanned to PDF format, then the recognitions of the document modules and the text are carried out, and then the recognized information are extracted, so as to be filed and supplied to a search engine for indexing. In the prior art, it takes a lot of system resources, including CPU and memory resources, to operate on a large number of documents in PDF format, and the processing speed of the documents in PDF format is also slower. In order to improve the processing speed, various ways have been chosen, and especially in recent years, with the maturation of the technology of multicore processors, it has become one of the preferred ways to improve the computing speed to run multiple threads simultaneously on multiple CPUs. 
         [0003]    However, in practical operation, the situation often occurs that the CPU can not be fully utilized, while the speed is limitedly increased. For example, if the CPU is fully utilized, a test opens four threads on the multicore machine, such as a 4-core CPU. When stress testing, each thread occupies one CPU. In theory, the CPU occupancy rate should be close to 100%. However, it is found in the practical test that the CPU occupancy rate is far less than 100%, and even in a lot of cases, the CPU occupancy rate is only 25%. This shows that not every CPU is at work, there should be some waiting threads. The CPU has not been fully utilized. 
         [0004]    The inventors of the present invention carried out the analyses and researches on the causes of this phenomenon by combing their multi-year industry working experiences and experimental results, and eventually thought out that this situation occurred mainly in that in memory operational engineering, no matter whether the allocation or the release of the memory all required the thread protection and all threads shared one memory pool to access, which results in waiting for access, so that the CPU could not be fully utilized. And since the PDF operation is a very frequent operation on the memory, a large number of threads are caused to wait. 
         [0005]    Concerning the memory access, in the memory management of a traditional operating system, each application usually is loaded into a separate process, and a private memory space is specified for each process, the memory space of one process can not communicate with that of another process. Generally speaking, one application process will create multiple entities, i.e., threads, when running, and the multiple threads belonging to one process share the resources of the process to access. 
         [0006]    With the development of computing technology and hardware, especially in recent years, the extensive use of multicore processors, the concurrent execution of threads (parallel computing) becomes more and more common. When multiple threads are in parallel, taking the data security into account, the shared memory to access can be accessed one by one but can not be accessed simultaneously. Even though allowing for simultaneously accessing, there is frequently a situation that the allocated memory pool is not enough for the use of multiple threads, which results in waiting to access. Especially when multi-CPUs are used and each CPU run one thread, when the time required for CPU processing is less than the time for memory accessing and waiting, the memory accessing becomes the bottleneck of the computing speed, so that the CPU resources can not be fully utilized, and impeding the further increase of the computing speed is impeded. 
         [0007]    In order to improve the memory access, the skilled in the art has proposed a variety of methods. For example, the Chinese Invention Patent which is entitled “a dynamic memory management system of a multi-threaded processor and a method thereof” and which has the grant publication No. CN 100487660C discloses a multi-threaded processor dynamic memory management method (see  FIG. 1 ), the method comprising the steps of: Master kernel thread applying for a block of memory from the system memory as the memory pool (step  101 ); All kernel threads sending messages of applying memory to said master kernel thread, after the master kernel thread having received said message of applying memory, allocating the free memory from the memory pool to the kernel thread of the applied memory and sending the message of allocating memory to the kernel thread of the applied memory (step  102 ); Said master kernel thread querying the usage of the kernel thread applied memory, and regularly sending message of deallocating memory(step  103 ); After the kernel thread having received said memory message of deallocating memory, freeing the memory and sending the message of releasing memory to the master kernel thread (step  104 ). 
         [0008]    To a certain extent, the method solves the problem of memory release of the kernel thread, and can avoid a certain free kernel thread taking up too much free memory at a certain moment. The hardware arbiter is replaced by the master kernel thread, and a message mechanism is used to improve the efficiency of allocating memory among various kernel threads. But this method can not solve the problem that the multiple threads which share a memory pool to access all request a larger memory space and must be queued up for. And it is necessary for this method to change the memory management codes. And thus this method is not suitable for directly transplanting the codes. 
       SUMMARY OF INVENTION 
       [0009]    The object of the invention is that for the problem that when multiple threads call the same execution module, since the same memory pool must be shared to access and the memory pool can not provide sufficient resources, the time waiting to access memory is caused too long. A new memory access policy is provided to shorten or even eliminate the time waiting to access memory when parallel computing, so that one single thread can have a separate memory pool. 
         [0010]    The more important object is to be able to simply transplant single-threaded codes to a multi-threaded environment in the case that the module codes are not changed or rarely changed, and especially memory management is not changed. And give full play to advantages of multicore and multithread. 
         [0011]    A memory access method for parallel computing of the present invention is applied in the case that n(n≧2) parallel threads call the same original execution module, characterized in that it comprises the steps of: 
         [0012]    S 1 ) Determining the number k of mirror execution modules needed to be created based on the number n of threads, wherein n−1≧k≧1; 
         [0013]    S 2 ) Determining the mapping policy F between the n threads and the execution modules including the original execution module as well as the k mirror execution modules; 
         [0014]    S 3 ) Creating k mirror execution modules, and allocating private memory pool to each of them respectively; 
         [0015]    S 4 ) Executing the n parallel threads, and making each thread call the execution module according to the mapping policy F, wherein each execution module access and only access its private memory pool; 
         [0016]    S 5 ) Deleting the created mirror execution modules after the thread operation is finished, and releasing the resources. 
         [0017]    Wherein, the number k of the mirror execution modules may vary with time. Alternatively, the number k of the mirror execution modules can also be constant during executing the threads. 
         [0018]    Preferably, the number of mirror execution modules can be determined according to a statistical value of the number of threads calling the same execution module at the same time. 
         [0019]    According to an embodiment of the present invention, the number k of the mirror execution modules is 1. 
         [0020]    According to another embodiment of the present invention, the number k of the mirror execution modules is n−1. 
         [0021]    Wherein, when the number k of the mirror execution modules is n−1, the mapping policy F may be: each thread calls a different execution module, and the threads and the execution modules are in one-to-one correspondence. 
         [0022]    According to yet another embodiment of the present invention, the mapping policy F can be a static policy, one thread only points to one execution module which is called by the thread, and one execution module is called by one or more threads. 
         [0023]    Preferably, in the mapping policy F, each of the threads frequently calling execution modules is configured to exclusively access one execution module, and the threads relatively less frequently calling execution modules are configured to share to access one execution module. 
         [0024]    According to yet another embodiment of the present invention, the mapping policy F is a “multi-window-queuing” mechanism of dynamic policy; in said dynamic policy, each execution module is defined as two states including “free” and “busy”, all of the free execution modules are queued, the thread which is needed to call execution modules calls one module selected from the queue of the free execution modules; if there are no free execution modules, the thread waits; the execution modules are labeled as busy during calling the execution modules of the thread; after the execution is finished, the thread releases the execution modules, and the execution modules are labeled as free. 
         [0025]    Application of the method of the invention can make the parallel computing efficiency increase linearly as the number of core increases, but not be limited by the memory access bottleneck. It can be applied to a variety of operating systems. And, using the static mode, even without the need to add the code, a single-threaded program can be transplanted directly to a multi-threaded environment. Flexible dynamic mode and dynamic mapping policy can increase the efficiency in a manner that gets twice the performances with half the efforts without the present invention occupying so many system resources. 
     
    
     
       BRIEF DESCRIPTION OF DRAWINGS 
         [0026]      FIG. 1  is a flowchart of memory management method of the prior art; 
           [0027]      FIG. 2  is a schematic diagram of the memory access when multiple threads all call the same execution module; 
           [0028]      FIG. 3  is a schematic diagram of the memory access when multiple threads create their execution modules respectively; 
           [0029]      FIG. 4  is a schematic flowchart of a preferred embodiment of the memory access method of the present invention; 
           [0030]      FIG. 5  is a first comparative diagram of the thread performance when utilizing the method of the invention and the other measures; 
           [0031]      FIG. 6  is a second comparative diagram of the thread performance when utilizing the method of the invention and the other measures; and 
           [0032]      FIG. 7  is a third comparative diagram of the thread performance when utilizing the method of the invention and the other measures. 
       
    
    
     DESCRIPTION OF EMBODIMENTS 
       [0033]    An object of the present invention is to change the memory access manner on the premise that the operating system memory management doesn&#39;t change. To facilitate the better understanding of the invention, description is first made to the general memory management mode. 
         [0034]    One of the characteristics of the memory management is the execution modules are isolated, each execution module is assigned a separate space of memory pool. Wherein, the execution module can be regarded as a data structure which is created by the system in the memory, for managing the program code, data and resources which are loaded into the memory upon the management operation. Each execution module as a system resource allocation unit exclusively accesses its own memory pool, multiple functions in the execution module share to access the memory pool of this execution module. 
         [0035]    The execution module can be an executable program or a dynamic link library (d 11 ) and so on in the windows which is loaded into a process after operation. No matter how many threads are being called, the same execution module will only be loaded once by all threads which are turned on by same process, and for the same execution module, its memory pool is shared to access. Depending on the difference between the types of operating systems, the execution modules may also have other forms. According to the known of the skilled in the art, the memory management modes of different operating systems will be different, but there all exists such a specific unit that it is made up of a collection of some data and codes and appears as the forms of program, function and so on, and the operating system will assign a private memory space (logical or physical) for this kind of codes combination for its access. Therefore, in the present invention, the execution module can also be interpreted in a given operating system as a collection of a set of codes and data which is assigned a separate memory pool based on the operating system&#39;s default memory management mode. 
         [0036]      FIG. 2  shows the memory access with multiple threads using the same execution module. Wherein, process  1  ( 211 ), process  2  ( 212 ) . . . process n ( 213 ) and so on are multiple parallel threads of an application, they are required to call an execution module  220 , and the execution module  220  accesses the memory pool  230 . 
         [0037]    In such a multi-threaded environment, when multiple threads ( 211 - 213 ) call an execution module ( 220 ) at the same time, if multiple threads in the execution module need to operating on memory (for example, applying and releasing) at the same time, i.e. operating on the same memory pool, the access of thread on memory need to be queued, thus the memory cause the thread to wait, resulting in that the CPU resources could not fully utilized. This is also where it is intended to solve the problems of the present invention. 
         [0038]      FIG. 3  shows a memory access schematic diagram when the invention respectively creates the respective execution module for each thread in order to solve the above problem. Taking example for n threads (n is a natural number), in the default case (see  FIG. 2 ), the system has only one original execution module  1  ( 321 ), but in the present invention, based on the number of processes, n−1 mirror execution modules are created additionally, i.e. execution module  2  ( 322 ) to execution module n ( 323 ), so that the total number of the execution modules is equal to the total number of the threads. Wherein, the mirror execution modules can be obtained by copying the original execution module  1 , and the function of them is the same as that of the original execution module. At this point, in terms of the operating system, since it will assign a separate memory pool for each execution module by default, each execution module respectively accesses respective different memory pool without disturbing each other. For example as shown in  FIG. 3 , the execution module  1  ( 321 ) accesses the memory pool  1  ( 331 ), the execution module  2  ( 322 ) accesses the memory pool  2  ( 332 ), . . . , the execution module n ( 323 ) accesses the memory pool  3  ( 333 ). So on the premise that does not change the operating system and the memory management codes, it is achieved that each thread has a separate memory pool. In this way, when the memory operations are needed, each thread does not need to compete for accessing the same memory pool, and it is also unnecessary to wait. 
         [0039]    In short, the invention achieves that each thread uses a separate memory pool by the method of copying the execution module. The shared resources to access are made become the exclusive resources, exchanging space for time and avoiding the waiting caused by the memory access conflicts between the threads. 
         [0040]    This embodiment is the most typical example of exchanging space for time. This solution can simply transplant single-threaded codes to a multi-threaded environment in the case that don&#39;t change or rarely change the module codes and especially don&#39;t change memory management at all, and can be said to be a universal method to solve the problem of parallel computing efficiency. 
         [0041]    It should be emphasized that in terms of n threads, the number of the mirror execution modules created is not necessarily n−1, which can be any number from 1 to n−1. Since it is clear from the above analysis that the mirror execution modules aim to ease the queuing phenomenon of memory access. This is like the case of queuing at the service window, when the n customers can only obtain service through one window, the efficiency is low, and the mathematical expectation of the waiting time is long; but when an additional window is added (k=1), the entire waiting time of queuing can be significantly reduced; creating n−1 mirror execution modules can reduce the expected queuing time to 0. It&#39;s clear that the embodiment shown in  FIG. 3  is a preferred embodiment which aims to reduce the waiting time of memory access and not as the limitation to the embodiment of the invention. 
         [0042]    Below in combination with the specific flow of  FIG. 4 , the method of the present invention will be further described. On the whole, the creation of the mirror execution modules of the invention can be divided into two classes including the dynamic mode and the static mode. Wherein, the static mode means the corresponding topology mapping policy between the threads and each execution module is constant during the life cycle of the threads; and in the dynamic mode, the number of the mirror execution modules and the mapping policy between the threads and each execution module are variable. 
         [0043]      FIG. 4  is a schematic flowchart of a preferred embodiment of the memory access method of the present invention. Wherein, the process from the beginning to the end can be a whole story representing an application; and can also be a set of implementation processes which are needed to call the life cycle of threads of the same original execution module and so on. This can be determined according to the applicable field of the invention, be chosen by the skilled in the art by themselves according to different applications, and does not constitute the limitation of the invention. For the Convenience of depiction, any point in time from the beginning to the end of the process is represented by t (t∈[0,T]) hereinafter, wherein t=0 for the beginning, t=T for the end. Collectively, the execution module includes the original execution module and the mirror execution modules. Wherein, the mirror execution modules and the original execution module perform the same function, but are assigned to use different system resources (such as CPU and memory space). 
         [0044]    First, at step  410 , obtain the number n of parallel threads which are needed to call the same original execution module, wherein n is a natural number. Then, in step  420 , make a judgment on n, when n=1, there is only one thread, this thread uniquely corresponds to the execution module, the waiting to access memory caused by the queuing doesn&#39;t occur, and go to step  460  to execute the thread directly. When n&gt;2, this means that waiting may occur, and mirror execution modules are needed to be created to improve the memory access. Generally speaking, the setting of the number n of threads is associated with the number of CPU core of the computing devices. 
         [0045]    Next, execute step  430  to determine the number k of mirror execution modules needed to be created, wherein k is a natural number, and preferably, the n−1≧k≧1. Wherein, see  FIG. 3  the case that n−1 mirror execution modules are created, that is, the maximum space is exchanged for maximum time efficiency. The selection between other values is the result of the compromise between space and time. The skilled in the art can make a selection according to the actual application environment under the teachings of the present invention. For example, when the number of parallel threads is too large, but the total memory space is limited, if too many mirror execution modules are created, the operation of other programs will be affected or the overall system performance will be significantly reduced, and even there is not enough memory space to create so many mirror execution modules, there is need to reduce the number of the mirror execution modules. Another example, the number of the mirror execution modules can be determined according to the probability that the threads simultaneously call the same execution module, when the probability is high, more mirror execution modules are created; and the number of the execution modules is determined according to the statistical value (such as maximum, median, or mathematical expectation) of the number of threads which call the same execution module at the same time. 
         [0046]    Selecting a static mode is a simple embodiment. Wherein, k is a constant value, and k(t) is kept constant when t∈[0,T]. The advantage of the static mode is that the program is simple to be executed and the memory space which is occupied is constant, which will help stabilize execution. The disadvantage is that when the frequency fluctuations of invocations on the execution modules are large in the course of process operation, if a large value k is chosen, this may result in the waste of memory space, and if a smaller value k is chosen, this may result in that the waiting to access memory occurs during a period of frequent invocation. 
         [0047]    The dynamic mode is more flexible. k(t) is variable when t∈[0,T], according to the frequency of invocation on the execution modules in the course of process operation, k(t) can be set to a piecewise function. During a period that multiple threads call the execution modules frequently, a larger value k(t) is chosen, but during a period of that the execution modules are relatively less called, a smaller value k(t) is chosen. Flexibly changing in the course of the process operation and thus it avoids wasting memory space to release resources in time. But the disadvantage of the dynamic mode is that the access control is slightly too complicated, there is a problem of parameter passing, additional codes is needed, and it is not conducive to the direct transplant of a single-threaded program to multi-threaded program. 
         [0048]    In step  440 , the mapping policy F between the n threads and the execution modules including the original execution module as well as the k mirror execution modules is determined. 
         [0049]    The mapping policy F mainly reflects such a one-way relationship that which thread calls which execution module. Similarly, the mapping policy F is also divided into static policy and dynamic policy. Wherein, for the static policy, one thread only points to one execution module which is called by the thread, and one execution module can be called by one or more threads. And this calling relationship is kept constant during a period of t∈[0,T]. Static policy performs simple without too much extra codes. 
         [0050]    It is more preferred that, for those threads which call execution modules frequently, they can be configured to exclusive access one execution module, and for the threads which relatively less frequently call execution modules, they are configured to share to access one execution module. 
         [0051]    For dynamic policy, the thread can not be fixedly pointed to a certain execution module. For example, a segmentation setting can be made to the mapping policy F in coordination with the dynamic mode of mirror execution modules, the time t(t∈[0,T]) is divided into several time periods, and the static policy is used during each time period. Alternatively, a variety of different modes of a similar multi-window-queuing mechanism and so on can also be taken. 
         [0052]    According to an embodiment of the present invention, two states including “free” and “busy” which are defined as for each execution module correspond to different calling states. All of the free execution modules are queued, the first thread which needs to call execution modules calls the first one from the free execution module queues; if there is a new thread needing to call execution modules; if there are no free execution modules, the new thread is made to call the next one from the free execution modules. The execution modules are labeled as busy during calling the execution modules of the thread; after the execution is finished, the thread releases the execution modules, and the execution modules are labeled as free. The benefits of such a queuing mechanism can be that maximize the full use of resources, especially for the case that k&lt;n−1, not only overall saving the memory resources but also effectively avoiding the case that the waiting to access memory occurs with resources being free. Particularly suitable for more threads and the case that the randomness of invocation of thread on the execution modules is strong, a substantial increase in efficiency can be achieved with the method of the invention which uses less memory mirror modules. 
         [0053]    Obviously, the static mode and the dynamic mode of various mirror modules and the static policy and the dynamic policy of mapping policy F can be combined with each other to constitute various flexible memory access control policy. Its specific implementation steps should be implemented by the skilled in the art by themselves under the above teachings of the present invention. It should be noted that, no matter what combinations and conversions, without departing from the spirit and essence of the present invention, will fall into the scope defined by the appended claims. 
         [0054]    Next proceeding to step  450 , create k mirror execution modules and assign a respective separate memory pool to the mirror execution modules. In this step, after the mirror execution modules are created, memory assignation is actually completed by the operating system control and does not need the intervention of the application. 
         [0055]    After the mirror modules are completed, then proceeding to step  460 , execute the n parallel threads and make them call the execution modules according to the mapping policy F. In the course of operation, each execution module accesses and only accesses its private memory pool. 
         [0056]    The last step is  470 , delete all the created mirror execution modules after the thread operation is finished, and release the resources which are assigned to the mirror execution modules. 
         [0057]      FIG. 5  to  FIG. 7  show the comparison between the performance after performing memory access control according to the method of the invention and that of when don&#39;t employ the invention, which is embodied by the following examples. It should be noted that the following examples are presented to illustrate the effect of the invention and not to restrict the implementation conditions of the present invention. 
         [0058]    Taking example for Windows operating system, perform test by simple text extraction on pdf file. Testing environment: software Environment: Windows XP professional SP2 32 bit, hardware environment: Core 2 Quad Q6600 2.4 GHz (4 core) 3.25 GB memory. 
         [0059]    Testing tools: Using the ifilttst.exe testing tool program ifilttst.exe of the platform sdk released by Microsoft, this program simply makes complete extraction on a file once and performs no more check work. This testing tool is one of that are commonly known in the art, and with regard to its details, refer to the description of http://msdn.microsoft.com/en-us/library/ms692580(28VS.85).aspx. 
         [0060]    The ifilttst.exe may create a different number of threads correspondingly, and during operation, each thread will call an execution module example.dll. The abscissa 1, 2, 3, 4 in  FIG. 5  to  FIG. 7  represent the number of threads, correspondingly using a plurality of different processor core run operation. Ordinate represents the performance indexes using the file processing speeds. The different curves represent the cases of implementation when the method of the invention is used and not used. Wherein, the embodiment of the method of the invention is to: for the different number n of threads, create k (k=n−1) mirror execution modules respectively, and employ the static mapping policy of the static mode. That is, each thread points to an execution module and remains constant in the course of the implementation. 
         [0061]    In the embodiment of  FIG. 5 , the sample 1 is 219 files and the total size is 1.54 GB, characteristics: the files are larger and the number of the files is less. 
         [0062]    In the embodiment of  FIG. 6 , the sample 3 is 351 files, the total size is 1.16 GB, and the number of the files and the sizes of each file are more evenly. 
         [0063]    Sample 3 in  FIG. 7  is 1274 files, the total size is 925 MB, and the number and the sizes of the files are smaller. 
         [0064]    In  FIG. 5  to  FIG. 7 , the curve L 1  is the implementation case when ifilttst.exe use the method of the invention; curve L 2  is the operation case when ifilttst.exe don&#39;t use the accelerating method of the invention. 
         [0065]    It can be seen from  FIG. 5  to  FIG. 7  that although the single-threaded performance of the testing tool program ifilttst.exe does not improve, multi-threaded performance can really be released, each core is utilized, and the performance grows from 1-4 threads. Curve L 2  without the accelerating method of the invention, although the performance improves with the increase in the number of CPU core, can not play CPU to the best performance, and the performance is always limited. The above description of the invention is intended to be illustrative, but not restrictive, it should be understood by the skilled in the art that many modifications, changes or equivalents can be made to the invention within its spirit and scope, but they all fall within the scope of protection of the invention.