473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# 3.0 adding functional suppoty and Multi core processors

I just read an overview of C# 3.0, and it occured to me that some of
these features could make it much easier to write programs that
automatically make use of multi core processors. After reading that
lambda functions are available in 3.0, I read up on the advantages of
functional programming, and one of the advantages is that tasks
performed in a functional language do not have side effects, and thus
tasks can be divided between processors easily without dealing with
concurrency as much.

What I'm wondering, if someone exprienced with functional programming
can chime in, are the added features enough to program tasks in a
purely functional manner?

I'm imagining some sort of architecture where each function call in a
task is automatically handed off to a different worker thread. As long
as we write the task in a functional manner, where there is no
dependancy on side effects, then we don't have to worry about the
locking and synchornization issues.

It's been a long time since I wrote anything in a functional
programming language, and am not deeply familier with C# 3.0, but I
wanted to post this and see what other's opinions are.

Jun 7 '06 #1
5 1793
sh******@cs.fsu.edu wrote:
I'm imagining some sort of architecture where each function call in a
task is automatically handed off to a different worker thread. As long
as we write the task in a functional manner, where there is no
dependancy on side effects, then we don't have to worry about the
locking and synchornization issues.


Simply calling each function call in a seperate worker thread won't
automatically give you benefits of a multi-core or processor system as
the calling function will need to block until the callee returns. Thus
if you're using thirty different threads but 29 of them are blocking you
really don't get any advantages.

Besides, IIRC from college, functional languages are really good at
solving certain problem sets, but for other problem sets they are
actually more complex to use.

Andrew Faust
Jun 7 '06 #2
Andrew Faust <an****@lapathy.com> wrote:
sh******@cs.fsu.edu wrote:
I'm imagining some sort of architecture where each function call in a
task is automatically handed off to a different worker thread. As long
as we write the task in a functional manner, where there is no
dependancy on side effects, then we don't have to worry about the
locking and synchornization issues.
Simply calling each function call in a seperate worker thread won't
automatically give you benefits of a multi-core or processor system as
the calling function will need to block until the callee returns.


Separating each function call into a separate thread pretty much implies
*not* blocking on the function result immediately, otherwise there'd be
no point in creating a separate thread at all. Therefore the most
sensible route is to call multiple functions one after the other, then
wait on them to return only when the return value is needed. The fact
that it's a functional approach implies that the return value is the
only side-effect anyway. Imagine code something like this:

---8<---
Active<int> first = CalcFirst();
Active<int> second = CalcSecond();
return new Active<int>(delegate { return first.Value * second.Value; });
--->8---

.... where Active<T>.Value waits for the calculation to finish, and
Active<T>'s constructor takes a delegate Evaluator that looks like:

delegate T Evaluator<T>()

.... and runs it on a background thread.

That code borrows from the "active" ideas for C++ presented by Herb
Sutter at the last PDC. I've written some ideas like this in my own
libraries. The can work well for limited scenarios - where the
calculation cost is much greater than the current overhead for handing
off to the threadpool.

If the .NET framework had efficient pipelined low-level threading
primitives, one could see this as being an efficient way to use multiple
cores, imagining that CalcFirst, CalcSecond and '*' on ints were
operations sufficiently slow to benefit from parallel calculation, which
implies extra latency for communicating the return values.
Besides, IIRC from college, functional languages are really good at
solving certain problem sets, but for other problem sets they are
actually more complex to use.


If all your data is immutable for a particular subgraph and you need to
perform calculations or copying transformations on this subgraph, a
functional approach can make life much easier and less buggy, as well as
possibly improving performance in a multi-core world. I know its helped
me in the past.

-- Barry

--
http://barrkel.blogspot.com/
Jun 7 '06 #3
Hi!
cores, imagining that CalcFirst, CalcSecond and '*' on ints were
operations sufficiently slow to benefit from parallel calculation, which
implies extra latency for communicating the return values.


Considering that the OS switches between threads about every 20ms.
It would only make sense to use multiple cores for tasks that take
longer than 20ms to compute. And that is usually a very "fat" chunk of
processing.

Regards!
Atmapuri
Jun 7 '06 #4
"Atmapuri" <di*@community.nospam> wrote:
Hi!
cores, imagining that CalcFirst, CalcSecond and '*' on ints were
operations sufficiently slow to benefit from parallel calculation, which
implies extra latency for communicating the return values.
Considering that the OS switches between threads about every 20ms.


This assumes that there are other tasks that are using 100% of every
CPU. Also, a thread's time slice ends sooner if it blocks on a
synchronization object. It only makes sense to multi-thread these
calculations if not all CPUs are being utilized to 100% anyway, which
implies that there will (probably) be an idle CPU for the task, which in
turn implies that it should take less than 20ms before the thread gets
started (a slice "timeout" will not be needed at all if a CPU is idle).
It would only make sense to use multiple cores for tasks that take
longer than 20ms to compute. And that is usually a very "fat" chunk of
processing.


I don't agree with your reasoning.

-- Barry

--
http://barrkel.blogspot.com/
Jun 7 '06 #5
Ah, I love when I hit tab and then reallize I can't tab in a text box
because it changes focus, then hit backspace and it goes back a page
and I lose everything I have written :(

In a nutshell, if you have a recursive process that uses the recursive
calls as parameters to itself like Do(Do(n+1,term), Do(n+2,term)) then
your calls can be visuallized as a tree, and you would hand off the
right paramter to another thread, but only until you've filled all
processors. At that point there would be no more handing off, and each
thread would work it's part of the call tree using the normal call
stack process.

If a thread finished before the others, then one of the others could
hand off again at the next most convient split point.

Since we don't want to mess with threads if the recursive process isn't
going to get very deep, the programmer could specifiy conditions in an
imperetive manner such as:

DoInit(int n, int term)
{
if( (n-term) >= 100)
{
FlagUsageOfMultithreading();
}

Do(n,term);
}

If you were processing some sort of large set of data, your condition
would be based on the number of items in the data set. Of course it's
not provable when or if something will terminate, but since you're only
concerned with identifying what is "too small" for multithreading, then
for small values or small sets of data, the programmer should be able
to make a good estimate of how many calls will be required. With that
they would estimate what the breaking point is that makes
multithreading worth it.

I suppose there has already been alot of research in these regards in
the realm of distributed computing. It seems that since the future is
going to be single user systems running multi core processors, then
there is going to be a need for easy to use techniques that allow
programmers to get the most out of these processors.

Barry Kelly wrote:
"Atmapuri" <di*@community.nospam> wrote:
Hi!
cores, imagining that CalcFirst, CalcSecond and '*' on ints were
operations sufficiently slow to benefit from parallel calculation, which
implies extra latency for communicating the return values.


Considering that the OS switches between threads about every 20ms.


This assumes that there are other tasks that are using 100% of every
CPU. Also, a thread's time slice ends sooner if it blocks on a
synchronization object. It only makes sense to multi-thread these
calculations if not all CPUs are being utilized to 100% anyway, which
implies that there will (probably) be an idle CPU for the task, which in
turn implies that it should take less than 20ms before the thread gets
started (a slice "timeout" will not be needed at all if a CPU is idle).
It would only make sense to use multiple cores for tasks that take
longer than 20ms to compute. And that is usually a very "fat" chunk of
processing.


I don't agree with your reasoning.

-- Barry

--
http://barrkel.blogspot.com/


Jun 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
2
by: JohnFol | last post by:
I know Windows / SQL etc can utilise multiple processors. In the good old days of coding, you simply wrote the .EXE and Windows would run it on a single processor (or a given processor for...
2
by: webwarrior | last post by:
Hi, Is there a reason why we have to pay more for licensing for a different kind of processor? Why are we not charged for the Hyperthreading on some processors also. If Oracle is really...
20
by: dotyet | last post by:
Hi Everyone, It would be a real big help if anyone can shed light on whether DB2 UDB 8.2 on Solaris will make optimum use of a Sun T2000 server. The server has 1 CPU with 8 cores and each core...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
3
by: JDeats | last post by:
Looking to pick up a notebook and wanted query the developer community for any feedback, sorry if this is off topic. Does the Intel Core 2 Duo chip offer any advantages to .NET developers? I...
5
by: Coaster | last post by:
I am designing a process which will spawn a good number of threads and some of them will execute a c++ process which is quite memory intensive (although not multithreaded). This will run on a 2 cpu...
11
by: John | last post by:
Is there a way to find the number of processors on a machine (on linux/ windows/macos/cygwin) using python code (using the same code/cross platform code)?
14
tharden3
by: tharden3 | last post by:
When you get a 2.4 GHz Core Duo, are you splitting one 2.4 processor into two parts, or do you literally have two 2.4 GHz processors? Same for a Quad Core?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.