tag:blogger.com,1999:blog-45945699387446937842023-11-15T05:11:30.700-08:00Concurrency in javaNitesh Kumarhttp://www.blogger.com/profile/15413750278395382414noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4594569938744693784.post-80260230882988630422013-03-14T00:46:00.000-07:002013-03-15T04:14:53.648-07:00Concurrency Speed Up Factor : Amdahl's Law<div dir="ltr" style="text-align: left;" trbidi="on">
Amdahl's Law or Amdahl's argument defines the Maximum improvement of a collective system when the parts of the system are improved.<br />
<br />
In parallelisation or Concurrency domain, it defines the Maximum SpeedUp Factor when then task executed on a single processor is divided into tasks that can be executed by Multiple Processors.<br />
<br />
<h3 style="text-align: left;">
What is Speedup ?</h3>
<div style="text-align: left;">
<i><b>Speed up factor is the ration of 'Time of Execution on a single processor' Vs 'Time of exection on a processor when the task is distributed in parallel across N processors</b></i>'<br />
<br />
Lets assume a Task , task_overall , is executed on a single processor. Let the time of exection = T(task_overall)</div>
<div style="text-align: left;">
Now the nature of the task permits it be divided N number of smaller tasks.</div>
<div style="text-align: left;">
Let each smaller task = <b><i>task(i)</i></b> , where i <= n and i = 1, 2,..,N</div>
<div style="text-align: left;">
In order of improve time execution , we decide to execute each task(i) on a separate processor</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Since the Time of execution on a single process <i><b>IS PROPORTIONAL TO </b></i>amount of task executed.</div>
<div style="text-align: left;">
Let theTime of Execution for a chunk of task, <i><b>task(i) = T(i)</b></i> , where<b> <i>task(i) = task(over_all)/N</i></b> fraction of the complete task.</div>
<div style="text-align: left;">
So the new fractional time on each processor now is <i><b>T(i) = T(task_overall)/N</b></i><br />
<b>Sped Up factor = T(task_overall)/T(i)</b></div>
<div style="text-align: left;">
E.g :</div>
<div style="text-align: left;">
Let T(task_overall) = 10 hours. i.e It takes 10 hours to do a task on a single processor.</div>
<div style="text-align: left;">
Let's divide this task into 7 chunks and allow it to processed by 7 processors.</div>
<div style="text-align: left;">
Fractional task executed at each processor now is 1/7</div>
<div style="text-align: left;">
So reduced Time on each processor has changed from 10 hours to 10*(1/7) = 10/7</div>
<div style="text-align: left;">
This ratio of the previous time to this reduced time is the Speed Up Factor.<br />
<i><b>Speed Up Factor = T(over_all) / T(i) = 10/ (10/7) = 7 </b></i><br />
So speed increases seven times by dividing a task in parallel across 7 processors.<br />
<br />
<h3 style="text-align: left;">
Speed up Factor when only a part of task can be parallelised</h3>
<div style="text-align: left;">
Let the Task, task_overall consists of a fraction of task that cannot be parallelised. i.e this fraction of task can be executed only serially , hence no speed up can be gained on this fraction when the number of parallel processes is increased</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Task( over_all ) = Task( serial ) + Task( task that can be parallelised )</div>
<div style="text-align: left;">
Task( over_all ) = K percent of Task ( serial ) + ( 1 - K ) percent of Task ( parallel )</div>
<div style="text-align: left;">
Since Time is proportional to task</div>
<div style="text-align: left;">
T( over_all ) = K % T ( over_all ) + ( 1 - K ) % T ( over_all)</div>
<div style="text-align: left;">
T(i) = K % T ( over_all ) + ( 1 - K ) % T ( over_all) / N</div>
<div style="text-align: left;">
<br />
<i><b>So Speed Up Factor = T( over_all ) / T ( i )</b></i></div>
<div style="text-align: left;">
<i><b><i><b>Speed Up Factor </b></i>= 1 / [ f + (1-f)/N ]</b></i><br />
<i><b><br /></b></i>
Here f = K % T( over_all ) i.e f represents the fraction of task that cannot be parallelized</div>
</div>
<div style="text-align: left;">
<br />
Amdahl's Law can be used to measure increase in execution time when chunks of serial + parallel tasks is executed in parallel across multiple processors.<br />
<br />
This Law can also be used to measure the performance gains on concurrent executions by threads <br />
<br />
<br /></div>
<div style="text-align: left;">
<br /></div>
</div>
Nitesh Kumarhttp://www.blogger.com/profile/15413750278395382414noreply@blogger.com0tag:blogger.com,1999:blog-4594569938744693784.post-21907709407449493382013-03-13T01:39:00.000-07:002013-03-15T05:32:48.035-07:00Different Concurrency Models - Java Vs Others<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
Concurrency Models in Programming Languages</h3>
<h3 style="text-align: left;">
</h3>
<div style="text-align: left;">
Two main paradigms or models
of concurrency in programming languages today, one is the shared-state
that utilize multiple threads of execution synchronized using locks. The
other is the share-nothing model that uses asynchronous messaging
passing across lightweight processes or threads. </div>
<div style="text-align: left;">
An important factor why concurrency
models are becoming very popular is because of the increasing multi-core
processors trend, processor manufacturers are increasing the numbers of
cores in processors, giving access to processors with 4, 8 or
more cores capable of doing multiple calculations in parallel. There has
been an increased interest in software engineering techniques to fully
utilize the capabilities offered by these processors </div>
<h4>
Shared-state model</h4>
<div style="text-align: justify;">
Java supports the shared-state
concurrency model that relies on synchronizing concurrent threads via
concurrency-related objects, such as locks, barriers, and latches. All
concurrency abstractions in Java are based on the shared mutable state
paradigm and prone to the vulnerabilities of race conditions,
blocking/contention and deadlocks. Since Java 1.5 or 5 new packages and
classes have been added to improve the concurrency model and Java 7 has introduced Fork/Join. Fork/Join allows programmers to utilize finer-grained
concurrency by allowing independent task components to be parallelized
through recursive techniques. The individual subtasks known as the
Fork/Join subtasks are mapped to a pool of threads that initiates and
manages their execution using advanced queuing and scheduling
techniques.</div>
<div style="text-align: justify;">
Thread management in Java imposes a number of challenges. In
applications that demand execution of large number of concurrent
processes in the order of thousands, JVM’s threads show to be slow
because of stack maintenance overhead, locking contention, and context
switch overhead. </div>
<div style="text-align: justify;">
Apart from the expensive context
switching and the heavy weight nature of Threads in Java, the the main problem is the shared-state
model itself. As a model of computation, threads are
non-deterministic, and with the shared-state of the Java memory model,
when multiple threads can potentially access a piece of information in
memory concurrently, the programming model becomes highly complex.</div>
<h4>
Share-nothing model</h4>
<div style="text-align: justify;">
The other model, the shared-nothing
model, supported by Erlang has been used extensively to achieve a high
level of concurrency. Erlang has popularized concurrency oriented
programming by implementing an extremely lightweight process model on
top of the operating system and platform. The actor model in Erlang is
based on strong isolation semantics, self-contained processes with
asynchronous message passing. </div>
<div style="text-align: justify;">
On the JVM Scala provides the same model
supported by Erlang, Scala brings actor based concurrency on the JVM
that allows the design of scalable concurrent applications that
automatically take advantage of the multicore processors. Scala actors provide a better model for programming
concurrently in the virtual machine environment. Share-nothing model and Scala can be proved to have advantages over the JVM.</div>
<h3>
Scala Actors</h3>
<div style="text-align: justify;">
Scala Actors are based on the
shared-nothing and message passing model. An Actor is a mathematical
model of concurrent computation that encapsulate data, code and its own
thread of control, and communicate asynchronously using immutable
message passing techniques. When the basic architecture is
shared-nothing, each actor seems to act in its own process space. And
the success and scalability of the actor implementation depends a lot on
the ability of the language to implement lightweight processes on top
of the native threading model. Every actor has its own mailbox for
storing messages, implemented as asynchronous, race-free, non-blocking
queues. Scala actors use Fork/Join as the underlying implementation and
exposes a concurrency model that encourages shared-nothing structures.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
</div>
Nitesh Kumarhttp://www.blogger.com/profile/15413750278395382414noreply@blogger.com0tag:blogger.com,1999:blog-4594569938744693784.post-20196031193847832092012-06-28T05:03:00.001-07:002012-06-28T05:03:14.722-07:00SVN - Restore a Deleted File<div dir="ltr" style="text-align: left;" trbidi="on">
1. Get the Svn version that matches the file deletion<br />
Go to the deleted file directory and<br />
<br />
>> svn log --verbose<br />
<br />
------------------------------------------------------------------------<br />r23332 | niteshk | 2012-06-28 11:33:14 +0530 (Thu, 28 Jun 2012) | 1 line<br />Changed paths:<br /> D /crap/branches/Crap.java<br /> M /crap/branches/adc.java<br /><br />deleting Crap file<br />
----------------------------------<br />
<br />
revision # 23332 is the revision version corresponding to the deletion of Crap.java<br />
Choose any earlier verision, just intelligent guess, that could have contained the file<br />
<br />
Lets choose a verison earlier = 2332<br />
<br />
>> <br />
<pre>svn update -r 2331 Crap.java</pre>
<pre> </pre>
<pre>This command fails to do the expected, so lets try another one.</pre>
<pre> </pre>
<pre>>></pre>
<pre>svn copy -r 2331 https://{URL}/Crap.java . </pre>
<pre>
</pre>
<pre>This too fails !!</pre>
<pre>
</pre>
<pre>So the final one. Add revision version to the Url</pre>
<pre>
</pre>
<pre>>>
svn copy -r 98 https://{URL}/Crap.java@2331</pre>
<pre> </pre>
<pre> </pre>
<br /></div>Nitesh Kumarhttp://www.blogger.com/profile/15413750278395382414noreply@blogger.com1