CodeFest 2014. Коновалов А., Шкурко Д. — Разработка и...

Post on 20-Oct-2014

612 views 4 download

Tags:

description

 

Transcript of CodeFest 2014. Коновалов А., Шкурко Д. — Разработка и...

Оптимизация приложений для Android x86Антон Коновалов, Дмитрий Шкурко

Intel Corporation

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice2

План выступления

Обзор

Устройства на базе аппаратуры средств Intel

Инструменты разработки приложений на HTML5

Инструменты разработки приложений для Dalvik* и C/C++ приложений

Разработка C/C++ приложений

Инструменты разработки C/C++ приложений

Демо

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Телефоны и планшеты на базе x86 архитектуры

3

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Следующее поколение u-архитектуры Atom*

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Точка зрения пользователя

Android Runtime

Core Libraries

Dalvik Virtual Machine

Blink (WebKit)

Приложения на базе Android Dalvik* и HTML5• Должны работать без изменений

Приложения, содержащие двоичный код,• Должны работать без изменений, но не с максимальной

эффективностью

Как правило, популярные приложения используют бинарныебиблиотеки

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Типы приложений, поставляемые в бинарном виде

7

• Порт существующего кода с других платформ• Оптимизация без глобальной переделки• Требования к производительности в условиях ограничения на ресурсы

• Обработка видео, аудио, сигналов• Алгоритмы симуляции• Прямой доступ к CPU/HW

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Инструменты разработки приложений на HTML5

8

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Intel® HTML5 Development Environment (XDK)

Создание, отладка и распространение HTML5 приложенийна базе общего кода для разных платформ

xdk.intel.com

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Инструменты разработки приложений для Dalvik* иC/C++ приложений

10

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice11

Integrated Native Developer Experience (beta)

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice12

Integrated Native Developer Experience (beta)

Базовые инструментыразработки

Media Pack for Android

Threading Building Blocks

Инструменты для анализапроизводительности и отладки

Compute Code Builder

Intel C/C++ компилятор

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice13

Базовые инструменты разработки

Android developer tools (ADT)

Android NDK

Android SDK Eclipse* IDE

Android plugin for

Visual Studio*

Apache Ant* Intel* HAXM

Android plugin for Eclipse*

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Hardware Accelerated Execution Manager (Intel®

HAXM)

www.intel.com/software/android

HAXM драйвер для эмулятораx86

Образ ОС Android для x86Готовые образы для эмуляторавходят в SDK• Образы для x86 предоставляются, начиная с API 10

Виртуализация ускоряет эмуляцию в2-4 раза• HAXM на Mac и Windows ОС• KVM на Linux

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice15

Демо (тесты на производительность памяти ипроцессора)

HAXM off HAXM on

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Compute Code Builder

16

http://software.intel.com/en-us/articles/getting-started-with-compute-code-builder

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Возможности Compute Code Builder

17

• Preview JumpStart kit – учебный инструмент для пошагового построенияOpenCL проекта

• Code editing – Интеграция с популярными IDE:

• Синтаксическая раскраска кода

• Авто-дополнение кода

• Обработка ошибок компиляции

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Возможности Compute Code Builder (продолжение)

18

• Compile and Build – компиляция OpenCL ядер, подготовка бинарных файловна Android устройствах

• OpenCL API debug – отладка вызовов OpenCL API, инспектированиеобъектов и очередей OpenCL.

• OpenCL kernel debug – пошаговая отладка на центральном процессоре

• Kernel analyze – анализ производительности OpenCL ядер на машинах сграфическим процессором Intel® Graphics

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice19

Threading Building Blocks

• Современная C++ библиотека параллельных алгоритмов для эффективногораспараллеливания приложений

• Программирование на уровне элементов работы, а не потоков ОС

• Динамическое распределение нагрузки

• Проверенные алгоритмы

• Код доступен https://www.threadingbuildingblocks.org/ под GPL

• Коммерческая версия http://software.intel.com/en-us/intel-tbb

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Intel® Threading Building Blocks - Тизер#include <tbb/parallel_reduce.h>

#include <tbb/blocked_range.h>

double getPi() // \int_0^1 4dx/(1+x^2) = \pi

{

const int num_steps = 10000000;

const double step = 1./num_steps;

double pi = tbb::parallel_reduce(

tbb::blocked_range<int>(0, num_steps),

double(0),

[&](const tbb::blocked_range<int>& r, double current_sum ) -> double {

for (size_t i=r.begin(); i!=r.end(); ++i) {

double x = (i+0.5)*step;

current_sum += 4.0/(1.0 + x*x);

}

return current_sum; // body returns updated value of the accumulator

},

[]( double s1, double s2 ) {

return s1+s2;

}

);

return pi*step;

}

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Intel® TBB – Использование в приложениях

В новых версиях Android

обработка зависимостей

между библиотеками

осуществляется автоматически

Android.mk#for including tbb in your project:

include $(CLEAR_VARS)

LOCAL_MODULE := tbb

LOCAL_SRC_FILES := $(TBB_PATH)/lib/android/libtbb.so

LOCAL_EXPORT_C_INCLUDES := $(TBB_PATH)/include

include $(PREBUILT_SHARED_LIBRARY)

#for calling tbb from your lib:

LOCAL_CPP_FEATURES += rtti exceptions

LOCAL_SHARED_LIBRARIES += tbb

LOCAL_CFLAGS += -DTBB_USE_GCC_BUILTINS -std=c++11

APP_ABI := x86

APP_STL := gnustl_shared

Application.mk

System.loadLibrary("gnustl_shared");

System.loadLibrary("tbb");

System.loadLibrary("YourLib");

Java

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice22

Media Pack

Media Pack предоставляет код и примеры использования для:

• Захвата видео потока с камеры и экрана

• Редактирования видео

• Включения потокового видео

• Обработки звука

Media Pack поддерживает x86 и ARM устройств на базе Android версии 4.3

http://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials-building-samples

http://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials-running-samples

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice23

Захвата видео потока

http://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials-video-capturing-for-libgdx-applications

http://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials-video-capturing-for-opengl-applications

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

24

• Анализ производительности в реальномвремени

• Быстрые эксперименты с системныминастройками для локализации проблем сCPU и GPU

• Множество метрик для определения иоптимизации критичного кода

• CPU, GPU, API, RAM, сеть, энергопотребление и т.д.

Intel® Graphics Performance AnalyzersИнструмент системного анализа ОС Android

http://software.intel.com/en-us/vcsource/tools/intel-gpa

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Метрики и системные настройки

25

Метрики

BUS/DRAM

CPU

Flash File System I/O

Network I/O

GPU

App Memory

OpenGL API and States

Power

Системные настройки для анализапроизводительности и отладки

1x1 Scissor Rect

Disable Alpha Blending

Disable Alpha Test

Disable Draw Calls

Disable Z-Test

Show Wireframe

Texture 2x2

Hyper Threading Disable

CPU Frequency Override

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Инструменты разработки нативных приложений

26

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Android* NDK для разработки нативных приложений

27

Зачем он нужен?• Перенос существующего нативного кода с других платформ• Требования к производительности в условиях ограничения на ресурсы,

например, на память• Обработка изображений, сигналов• Алгоритмы симуляции• Прямой доступ к CPU/HW• Оптимизация кода без изменения алгоритма

http://developer.android.com/tools/sdk/ndk/index.html

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Совместимость приложений со всеми устройствами

28

myapp.apk

APP_ABI := all Application.mklibs/x86/mylib.so

libs/arm/mylib.so

libs/arm-v7a/mylib.so

libs/mips/mylib.so

libs/?/mylib.so

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Специализация опций для разных архитектур

29

libs/x86/mylib.so

libs/arm/mylib.so

libs/arm-v7a/mylib.so

libs/mips/mylib.so

libs/?/mylib.so

ifeq ($(TARGET_ARCH_ABI),x86)

LOCAL_CFLAGS := -mtune=atom -msse3

endif

ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)

LOCAL_CFLAGS := -march=armv7-a

Endif

Android.mk

#ifdef __i386__

strlcat(buf, "__i386__", sizeof(buf));

#endif

#ifdef __arm__

strlcat(buf, "__arm__", sizeof(buf));

#endif

source.c

http://software.intel.com/en-us/android/articles/creating-an-x86-and-arm-apk-using-the-intelr-compiler-and-gnu-gcc

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

C/C++ компиляторы для Android x86

30

Поддержка x86 включена, начиная с NDK r6• Включал или включает до сих пор

• GCC 4.4.3, GCC 4.6, GCC 4.7, GCC 4.8• Clang 3.1, Clang 3.2, Clang 3.3• ICC 13.1, 14.0• RenderScript*

• Сейчас по умолчанию используется версия 4.6

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Рекомендуемые опции GCC

31

Уровни оптимизации -O0, -O1, -O2, -O3, -Os-O0 уровень оптимизации по умолчанию

Настройки и оптимизации для заданных микроархитектур• -march=atom (или –mtune=atom)• -march=slm (или –mtune=slm)

Настройки точности арифметики• -ffast-math• -mfpmath=sse

Другие полезные опции• -flto• -funroll-loops, -funroll-all-loops

EEMBC® CoreMark®

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

32

Intel® C/C++ CompilerОптимизация приложений и системы в целом

Высокая производительность

• Простая рекомпиляция может дать прирост производительности

• Совместимость с GNU* GCC компилятором

• Intel® Cilk™ Plus – параллелизм данных и задач

• Поддержка стандартов IEEE 754-2008, С++11

Оптимизации

• Поддержка SSE инструкций от SSE до AVX2

• Автоматическая и полуавтоматическая векторизация

• Межфайловые оптимизации

• Оптимизации с использованием профиля

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Рекомендуемые опции Intel C/C++ компилятора

33

Уровни оптимизации -O0, -O1, -O2, -O3, -Os• -O2 уровень оптимизации по умолчанию

• если не включена поддержка отладки –g

• -Ofast, -fast

Настройки и оптимизации для заданных микроархитектур• -xATOM_SSSE3• -xATOM_SSE4.2

Настройки точности арифметики• -fp-model fast=1|2• -no-prec-div

Другие полезные опции-ansi-alias, -ipo

http://software.intel.com/en-us/android/articles/creating-an-x86-and-arm-apk-using-the-intelr-compiler-and-gnu-gcc

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

34

Intel® Cilk™ Plus

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

35

Intel® Cilk™ Plus

int fib(int n)

{

if (n < 2)

return n;

int x = fib(n-1);

int y = fid(n-2);

return x + y;

}

int fib(int n)

{

if (n < 2)

return n;

int x = cilk_spawn fib(n-1);

int y = fid(n-2);

cilk_sync;

return x + y;

}

Оригинальный код Оптимизированный код

for (int i; i<8; ++i)

{

do_work(i);

}

cilk_for (int i; i<8; ++i)

{

do_work(i);

}

Оригинальный код Оптимизированный код

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

36

Intel® Cilk™ Plus

//declaring the function body

__declspec((vector)) double ef_add(double x, double y)

{

return x + y;

}

//invoking the function using array notation

//operations on the whole extent of the arrays a,b,c

a[:] = ef_add(b[:], c[:]);

//use the full array notation construct to also specify n as an extend and s as a stride

a[0:n:s] = ef_add(b[0:n:s], c[0:n:s]);

//use the cilk_for construction to invoke the SIMD-enabled function in data parallel context

cilk_for (i = 0; i<n; ++i)

{

a[i] = ef_add(b[i], c[i]);

}

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

37

Intel® Cilk™ Plus

void add_floats(float *a, float *b, float *c, float *d, float *e, int n)

{

int i;

#pragma simd

for (int i; i<8; i++)

{

a[i] = a[i] + b[i] + c[i] + d[i] + e[i];

}

}

http://software.intel.com/en-us/intel-cilk-plus

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

40

Оптимизация приложений с Intel® Cilk™ Plus

http://habrahabr.ru/company/intel/blog/202088/

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice41

Перенос нативных Android* приложений на x86

• Изменение конфигурации построения (makefiles, файлы конфигурации)

• Перенос C/C++ кода

• Изменение типов данных

• Изменение выравнивания типов данных

• Перенос кода с интринсиками

• Перенос ассемблерного кода

http://software.intel.com/en-us/articles/ndk-android-application-porting-methodologies/

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Расположение данных в памятиunion {

struct TestStruct {

int mVar1;

long long mVar2;

int mVar3;

};

int mVar4[6];

} var;

Оригинальный код

union {

struct TestStruct {

int mVar1;

long long mVar2 __attribute__ ((aligned(8)));

int mVar3;

};

int mVar4[6];

} var;

Исправленный код

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

ARM* NEON vs. Intel® SSE

43

http://software.intel.com/en-us/blogs/2012/12/12/from-arm-neon-to-intel-mmxsse-automatic-porting-solution-tips-and-tricks

// VADD.I8 d0,d0,d0

int8x8_t vadd_s8(int8x8_t a, int8x8_t b);

#ifdef USE_MMX

#define vadd_s8 _mm_add_pi8 //MMX

#else

#define vadd_s8 _mm_add_epi8

#endif

NEONtoSSE.h

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Примеры

44

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice45

Демо (ffmpeg-2.1.4)

• Минимум внешних зависимостей

• Используется в нескольких проектах

• Простое построение

• configure

• make –j 16 install

http://www.ffmpeg.org

--disable-shared--enable-static--enable-pic--disable-doc--disable-symver--enable-ffmpeg--disable-ffplay--disable-ffprobe--disable-ffserver--disable-avdevice--disable-doc--target-os=linux--arch=atom--enable-cross-compile--cross-prefix=… --sysroot=…

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice46

Демо (ffmpeg-2.1.4)Преобразование из формата h265 в формат h264

ICC: 60 sec, FPS 17

• fmpeg_icc -i surfing.265 -t 40 -f mp4 -threads 4 -y /dev/null

GCC: 87 sec, FPS 11

• fmpeg_gcc -i surfing.265 -t 40 -f mp4 -threads 4 -y /dev/null

Опции GCC 4.8:

• -O3 -march=atom -funroll-all-loops -ffast-math -fomit-frame-pointer -mfpmath=sse

Опции ICC 14.0.1:

• -O3 -xATOM_SSSE3 -no-prec-div

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice47

Демо (Bouncing balls)

http://software.intel.com/en-us/videos/intel-c-compiler-for-android-5-optimizing-performance-icc-techniques-to-make-your-app-soar

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Intel® Developer Zone (IDZ)• Учебные материалы и техническаяподдержка

• Обмен опытом• Специализированные сообщества• Новости из мира технологий

Intel Android* Developer Website• Перенос нативных приложений

• Советы и руководства• Статьи на техническая темы• Форумы и блоги, которые ведутсяинженерами Intel

• Информация о SW/HW продуктах Intel для разработчиков на Android

www.intel.com/software/android

Дополнительная информация

Copyright © 2014, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others. Optimization Notice

Legal Disclaimer & Optimization Notice

INFORMATION IN THIS DOCUMENT IS PROVIDED “AS IS”. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO THIS INFORMATION INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance ofthat product when combined with other products.

Copyright © 2014, Intel Corporation. All rights reserved. Intel, Pentium, Xeon, Xeon Phi, Core, VTune, Cilk, and the Intel logo are trademarks of Intel Corporation in the U.S. and other countries.

Optimization Notice

Intel’s compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804

50