‎2009 Dec 24 1:14 PM
Hello friends,
I had posted in the performance tuning forum , regarding a performance issue problem , I am reposting it as it involves OO concept .
the link for the previous posting
Here is the scenario,
I have a internal table with 10 records(indepentent) , and i need to process them .The processing of one record doesnt have any influence on the another . When we go for loop , the performance issue is that , the 10 th record has to wait until the 9 records get processed even though there is no dependency on the output.
Could some one tell a way out to improve the performance..
If i am not clear with the question , i would explain it still clearer...
A internal table has 5 numbers , say( 1,3,4,6,7)
we are trying to find square of each number ,,,
If it is a loop the finding of suare of 7 has to wait until 6 is getting completed and it is waste of time ...
This is related to parallel processing , I have refered to parallel processing documents,But I want to do this conceptually ..
I am not using conventional procedural paradigm but Object orientedness...I am having a method which is performing this action .What am I supposed to do in that regard.
Comradely ,
K.Sibi
‎2010 Jan 06 1:45 PM
You might use the function module 'SPTA_PARA_PROCESS_START_2' for parallel processing. See demo report 'SPTA_PARA_DEMO_1' for details.
Edit: The function module internally uses ABAP objects, to do the handling. Maybe you can reuse the relevant sections, to build your own object based task handler.
Edited by: Carsten Grafflage on Jan 6, 2010 2:52 PM
‎2009 Dec 27 3:45 PM
Hi
As far as I know, in SAP parallel processing means background processing. You easily can cick of 10 process which will to your calculations in parallel to each other and you program will react until all have finished. In addtion you could at a timer so you can check once in a while if all is done?
Is this what you are looking for?
Rene
‎2009 Dec 29 11:59 PM
You will need to put your method call in a function module. The function module has to be "Remote-Enabled Module".
Here is an example based on your internal table with numbers.
The function module does the SQRT( ) calculation but this can be your own method call as well.
FUNCTION ZEVP_PARA.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IV_INDEX) TYPE I
*" VALUE(IV_VALUE) TYPE I
*" EXPORTING
*" VALUE(EV_SQRT) TYPE F
*"----------------------------------------------------------------------
ev_sqrt = sqrt( iv_value ).
ENDFUNCTION.
The following program calls the function module asynchronously in separate tasks. The task id is used as an index to the internal table. Also the internal table index is passed to the FM in case a more complex logic needs to be built.
The WAIT UNTIL statement waits until all tasks are completed or a 30 second time out passes.
This code is very rough and should not be used in a productive environment. One concern is what if the internal table gets too large. You will kill your application server if you start too many tasks. You will need to code around that problem. Also a tight error handling would be required to make your code more robust.
Edited by: Edward Pelyavskyy on Dec 30, 2009 1:00 AM
‎2009 Dec 30 12:17 AM
REPORT ZEVP_PARA.
class lcl_app definition final.
public section.
types:
begin of ts_value,
value type i,
sqrt type f,
end of ts_value,
tt_value type standard table of ts_value.
class-methods:
main,
on_task_complete
importing p_task type clike.
class-data:
gt_value type tt_value read-only.
private section.
class-data:
gv_tasksStarted type i,
gv_tasksCompleted type i.
endclass.
class lcl_app implementation.
method main.
data:
ls_value type ts_value,
lv_task type c length 5.
" Populate the test data
ls_value-value = 100.
append ls_value to gt_value.
ls_value-value = 200.
append ls_value to gt_value.
" Calculate SQRT in parallel
loop at gt_value into ls_value.
lv_task = sy-tabix. " A unique task id
call function 'ZEVP_PARA'
starting new task lv_task
destination in group default
calling on_task_complete on end of task
exporting
iv_index = sy-tabix
iv_value = ls_value-value
exceptions
communication_failure = 1
system_failure = 2
others = 3.
if sy-subrc = 0.
" The number of tasks started
gv_tasksStarted = gv_tasksStarted + 1.
endif.
endloop.
wait until gv_tasksCompleted = gv_tasksStarted
up to 30 seconds.
if sy-subrc ne 0.
write: / 'WAIT ERROR:', sy-subrc.
endif.
loop at gt_value into ls_value.
write: / sy-tabix, ls_value-value, ls_value-sqrt.
endloop.
endmethod.
method on_task_complete.
data:
lv_sqrt type f,
lv_index type i.
field-symbols:
<lf_value> type ts_value.
gv_tasksCompleted = gv_tasksCompleted + 1.
receive results from function 'ZEVP_PARA'
importing
ev_sqrt = lv_sqrt
exceptions
communication_failure = 1
system_failure = 2
others = 3.
if sy-subrc <> 0.
" Error processing
else.
lv_index = p_task.
read table gt_value assigning <lf_value>
index lv_index.
if sy-subrc = 0.
<lf_value>-sqrt = lv_sqrt.
endif.
endif.
endmethod.
endclass.
start-of-selection.
lcl_app=>main( ).
‎2010 Jan 13 5:00 PM
Hi,
As examplified by Edward, there is no RFC/asynchronous support for Methods of ABAP Objects as such. You would indeed need to "wrap" your method or ABAP Object in a Function Module, that you can then call with the addition "STARTING NEW TASK". Optionally, you can define a Method that will process the results of the Function Module that is executed asynchronously, as demonstrated as well in Edward's program.
You do need some additional code to avoid the situation where your program takes all the available resources on the Application Server. Theoretically, you cannot bring the server or system down, as there is a system profile parameter that determines the maximum number of asynchronous tasks that the system will allow. However, in a productive environment, it would be a good idea to limit the number of asynchronous tasks started from your program so that other programs can use some as well.
Function Group SPBT contains a set of Function Modules to manage parallel processing. In particular, FM SPBT_INITIALIZE will "initialize" a Server Group and return the maximum number of Parallel Tasks, as well as the number of free ones at the time of the initialization. The other FM of interest is SPBT_GET_CURR_RESOURCE_INFO, that can be called after the Server Group has been initialized, whenever you want to "fork" a new asynchronous task. This FM will give you the number of free tasks available for Parallel Processing at the time of calling the Function Module.
Below is a code snippet showing how these Function Modules could be used, so that your program always leaves a minimum of 2 tasks for Parallel Processing, that will be available for other programs in the system.
IF md_parallel IS NOT INITIAL.
IF md_parallel_init IS INITIAL.
*----- Server Group not initialized yet => Initialize it, and get the number of tasks available
CALL FUNCTION 'SPBT_INITIALIZE'
EXPORTING
GROUP_NAME = ' '
IMPORTING
max_pbt_wps = ld_max_tasks
free_pbt_wps = ld_free_tasks
EXCEPTIONS
invalid_group_name = 1
internal_error = 2
pbt_env_already_initialized = 3
currently_no_resources_avail = 4
no_pbt_resources_found = 5
cant_init_different_pbt_groups = 6
OTHERS = 7.
md_parallel_init = 'X'.
ELSE.
*----- Server Group initialized => check how many free tasks are available in the Server Group
for parallel processing
CALL FUNCTION 'SPBT_GET_CURR_RESOURCE_INFO'
IMPORTING
max_pbt_wps = ld_max_tasks
free_pbt_wps = ld_free_tasks
EXCEPTIONS
internal_error = 1
pbt_env_not_initialized_yet = 2
OTHERS = 3.
ENDIF.
IF ld_free_tasks GE 2.
*----- We have at leasr 2 remaining available tasks => reserve one
ld_taskid = ld_taskid + 1.
ENDIF.
ENDIF.
You may also need to program a WAIT statement, to wait until all asynchronous tasks "forked" from your program have completed their processing. Otherwise, you might find yourself in the situation where your main program has finished its processing, but some of the asynchronous tasks that it started are still running. If you do not need to report on the results of these asynchronous tasks, then that is not an issue. But, if you need to report on the success/failure of the processing performed by the asynchronous tasks, you would most likely report incomplete results in your program.
In the example where you have 10 entries to process asynchronously in an internal table, if you do not WAIT until all asynchronous tasks have completed, your program might report success/failure for only 8 of the 10 entries, because your program has completed before the asynchronous tasks for entries 9 and 10 in your internal table.
Given the complexity of Parallel Processing, you would only consider it in a customer program for situations where you have many (ie, thousands, if not tens of thousands) records to process, that the processing for each record tends to take a long time (like creating a Sales Order or Material via BAPI calls), and that you have a limited time window to process all of these records.
Well, whatever your decision is, good luck.
‎2010 Jan 06 1:45 PM
You might use the function module 'SPTA_PARA_PROCESS_START_2' for parallel processing. See demo report 'SPTA_PARA_DEMO_1' for details.
Edit: The function module internally uses ABAP objects, to do the handling. Maybe you can reuse the relevant sections, to build your own object based task handler.
Edited by: Carsten Grafflage on Jan 6, 2010 2:52 PM
‎2010 Jan 07 5:07 AM
Helllo Edward Pelyavskyy and Carsten Grafflage ,
Thanks for your valuable inputs.
I am still keepin the thread open so that I could get more inputs in that regard as it is more of a general question and not solution to a specific problem .
Comradely,
K.Sibi
‎2010 Jan 13 12:51 PM
There is a parallel processing framework in the SAP standard. Refer to component CA-GTF-TS-PPM,
ot look in package BANK_PP_JOBCTRL. Unfortunately, I am not aware of any thorough documentation for this.
-- Sebastian
‎2010 Jan 13 1:19 PM
Hello Sebastian ,
Thank you for the help. Could you elobarate a little more .
Comradely,
K.SIbi
‎2010 Jan 14 8:48 AM
I am marking this thread as unanswered , though I have given the fianl reply by Philippe Remy 10 points , because I wanted further more inputs on this from the community and I did not want to give Philippe Remy mere 2 points when he has taken pain to give such a long reply .
Requesting the moderators to allow users to have atleast 3 very helpful answers per thread !
Comradely ,
K.Sibi
Edited by: sibi k kanagaraj on Jan 14, 2010 9:53 AM
‎2011 Dec 08 3:49 AM
There is a project in Code Exchange that does Object Oriented Threading in ABAP:
https://cw.sdn.sap.com/cw/groups/abap-threading
Let me know if this fulfills your requirement.
‎2011 Dec 11 2:32 PM
Halo Sibi,
Parallel processing can be used to improve the perfomance but it has got its own short comings.
You can create seperate remote enabled FMs and call them asynchronously to process the individual records asynchronously.
You can even pass objects to the Remote enabled FM. This is done by serializing the object and passing it to the FM . Inside the FM you can deserialize the object and call its methods
Most improtantly you should use the WAIT statement for the synchronisation of the asynchronously called FMs
Regards
Arshad