250 likes | 389 Views
SESSION CODE: DEV301. Angel Hernandez Avanade Australia. http://www.bonafideideas.com. Maximise .NET with C++ for interop, performance and productivity . r e g e x _ s e a r c h. s t d :: c o u t. i n l i n e. f i n d _ i f _ n o t. V i s u a l C + +. 0 1 0 1
E N D
SESSION CODE: DEV301 Angel Hernandez Avanade Australia http://www.bonafideideas.com Maximise .NET with C++ for interop, performance and productivity (c) 2011 Microsoft. All rights reserved.
r e g e x _ s e a r c h std::cou t inline f i n d _ i f _ n o t Vis u a lC++ 0 1 0 1 0 1 1 #includ e < i o s t r ea m > L a m b d a s s l e e p _ f o r s t d :: f o r _ e a c h t h r e a d _ l o c al v o l a t i l e t e m p l a t e s e t l o c a l e r e p l a c e _ i f bitset m a k e _ p a i r ¦ is_sort e d d y n a m i c _ c ast mutex typedef g e t l i n e b i n a r y _ s e a rch nullptr v o i d ( * p t r ) s t d :: v e c t o r . . :
Agenda • What’s going on with C++ these days? • C++ is the champion of speed • Best situations for C++ • C++/CLI • Commonly used techniques for interop • Native code without sacrificing .NET performance • C++ and the cloud • Introducing C++ AMP • Basic Elements of C++ AMP Coding • C++ AMP at a Glance (c) 2011 Microsoft. All rights reserved.
What’s going on with C++ these days? • The new standard C++11 (previously known as C++0x) will be released soon • Native support for multi-threaded applications • New smart pointer classes, algorithms and containers • Lambda expressions • Automatic Type Deduction and decltype • Lots more! (c) 2011 Microsoft. All rights reserved.
C++ is the champion of speed! (c) 2011 Microsoft. All rights reserved.
DemoGo Speed racer... Go C++… (c) 2011 Microsoft. All rights reserved.
Best situations for C++ • Graphical and audio workstation software • Large-scale productivity applications like Adobe Photoshop • Legacy codebases • Real-time systems of all sizes and descriptions • Anything involving extreme numerical computation • Large-scale data storage and retrieval • Device drivers • And so forth (c) 2011 Microsoft. All rights reserved.
C++/CLI • C++/CLI is a binding between the standard C++ programming language and the CLI. • C++/CLI lets developers reuse their native code base, saving the agony of rewriting it all to run on the .NET Framework. • C++/CLI is designed to be the lowest-level language for the .NET framework. (c) 2011 Microsoft. All rights reserved.
C++/CLI (cont.) • C++ developers can Leverage their existing C++ knowledge to write powerful managed applications • C++/CLI allows developers to leverage the latest managed frameworks • C++/CLI is the most powerful language for interop (Marshalling made easy!) (c) 2011 Microsoft. All rights reserved.
DemoC++/CLI – The best of both worlds! (c) 2011 Microsoft. All rights reserved.
Commonly used techniques for Interop • Straightforward P/Invoke • [DllImport] & [MarshalAs] • COM Interop • C++/CLI wrapper class • CALLI Instruction (Reflection.Emit) (c) 2011 Microsoft. All rights reserved.
Native code without sacrificing .NET performance • Things to consider: • Mode Transition • (Moving data between the managed & unmanaged modes of operation) • Marshalling the data to move across the boundary • Marshalling is computationally expensive and the more data you move back and forth, the more expensive it becomes. (c) 2011 Microsoft. All rights reserved.
Native code without sacrificing .NET performance (cont.) • Ways to transition code & data: • Traditional DLLs (code) • COM-based DLLs (code) • Marshalling (data) • Implement any IPC mechanism (data) (c) 2011 Microsoft. All rights reserved.
DemoIt’s Interop time, baby!!! (c) 2011 Microsoft. All rights reserved.
C++ and the cloud • We can leverage the power of C++ with Windows Azure • Microsoft refreshes C++ for the cloud • Not for this version though…. Introducing now… (c) 2011 Microsoft. All rights reserved.
Introducing C++AMP How What • Part of C++ & Visual Studio • STL-like library for parallel patterns on large arrays • Builds on Direct3D • open spec Why • Performance • Productivity • Portability #include <amp.h> using namespace concurrency; void AddArrays(int n, int*pA, int *pB, int*pC) { array_view<int,1> a(n, pA); array_view<int,1> b(n, pB); array_view<int,1> sum(n, pC); parallel_for_each( sum.grid, [=](index<1> idx) restrict(direct3d) { sum[idx] = a[idx] + b[idx]; } ); } (c) 2011 Microsoft. All rights reserved.
Matrix Multiply (C++ AMP Sample) (c) 2011 Microsoft. All rights reserved.
Basic Elements of C++ AMP Coding void AddArrays(int n, int * pA, int * pB, int * pC) { array_view<int,1> a(n, pA); array_view<int,1> b(n, pB); array_view<int,1> sum(n, pC); parallel_for_each( sum.grid, [=](index<1> idx) restrict(direct3d) { sum[idx] = a[idx] + b[idx]; } ); } parallel_for_each: execute the lambda on the accelerator once per thread restrict(direct3d): tells the compiler to check that this code can execute on Direct3D hardware array_view: Wraps the data to operate on the accelerator grid: the number and shape of threads to execute the lambda array_view variables captured and associated data copied to accelerator (on demand) index: the thread ID that is running the lambda, used to index into data (c) 2011 Microsoft. All rights reserved.
Demoauto x = Windows Azure [=] (void*e) C++ (c) 2011 Microsoft. All rights reserved.
Session Objectives and Takeaways • No universally “better” language • C++ robust, scalable and amazingly fast • Direct access to hardware and memory • Managed languages for RAD and business applications • C++ native out-of-the-box support for multithreading (C++0X) • Develop memory or CPU intensive components in C++ • Consume from any managed language (c) 2011 Microsoft. All rights reserved.
Related Content • Visual C++ MVPs • Bonafide Ideas - My Blog • Visual C++ Team Blog • C9::GoingNative | Channel 9 • Herb Sutter’s Blog • Daniel Moth’s Blog (C++ AMP) • Google’s paper on language performance • The C++ Standards Committee • The Visual C++ Weekly • Visual C++ MSDN Forums (c) 2011 Microsoft. All rights reserved.
Enrol in Microsoft Virtual Academy Today Why Enroll, other than it being free? The MVA helps improve your IT skill set and advance your career with a free, easy to access training portal that allows you to learn at your own pace, focusing on Microsoft technologies. • What Do I get for enrolment? • Free training to make you become the Cloud-Hero in my Organization • Help mastering your Training Path and get the recognition • Connect with other IT Pros and discuss The Cloud Where do I Enrol? www.microsoftvirtualacademy.com Then tell us what you think. TellTheDean@microsoft.com
Resources • www.msteched.com/Australia • Sessions On-Demand & Community • www.microsoft.com/australia/learning • Microsoft Certification & Training Resources • http:// technet.microsoft.com/en-au • Resources for IT Professionals • http://msdn.microsoft.com/en-au • Resources for Developers (c) 2011 Microsoft. All rights reserved.
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. (c) 2011 Microsoft. All rights reserved.