Unity2023.1.19_DOTS_JobSystem

news2024/10/7 19:28:23

Unity2023.1.19_DOTS_JobSystem

上篇我们知道了DOTS是包含Entity Component System,Job System,Burst compiler三者的。接下来看下JobSystem的工作原理和具体实现。

简介:

官方介绍说:JobSystem允许您编写简单而安全的多线程代码,以便您的应用程序可以使用所有可用的CPU内核来执行代码。也就是说JobSystem是为多线程服务的一个模块。

JobSystem可以单独使用,但为了提高性能,也还应该使用Burst compiler,Burst compiler是专门为Unity的JobSystem编译而设计的。urst compiler改进了代码生成,从而提高了性能并减少了移动设备上的电池消耗。Burst compiler将在JobSystem之后再关注。

JobSystem和Burst compiler一起使用的时候,JobSystem工作效果最好。因为Burst不支持托管对象,所以需要使用非托管类型来访问Job中的数据。你可以使用blittable types,或者使用Unity内置的Native container对象,这是一个线程安全的c#本机内存包装器。NativeContainer对象还允许job访问与主线程共享的数据,而不是使用副本。

ECS为其进行了拓展Unity.Collections命名空间以包含其他类型的 NativeContainer:

NativeList - 可调整大小的 NativeArray,类似于List
NativeHashMap<T, R> - 键/值对,类似于Dictionary<T, R>
NativeMultiHashMap<T, R> - 每个键有多个值。
NativeQueue - 先进先出队列,类似于Queue

也可以在ECS中使用JobSystem创建高性能的面向数据代码。

Collections Package提供可在job和Burst-compiled代码中使用的非托管数据结构。

Unity使用原生的JobSystem处理原生代码使用多个工作线程,工作线程取决于你应用程序运行设备的CPU核的可用数量。

通常Unity默认在程序开始时在一个主线程上执行你的代码,你可以使用JobSystem在多个工作线程上执行你的代码,称之为多线程。

简单代码说明:

using UnityEngine;
using Unity.Collections;
using Unity.Jobs;

// Job adding two floating point values together
// It implements IJob, uses a NativeArray to get the results of the job, and uses the Execute method with the implementation of the job inside it:

public struct MyJob : IJob
{
    public float a;
    public float b;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = a + b;
    }
}

下面的例子建立在MyJob任务上,在主线程上调度一个任务:

using UnityEngine;
using Unity.Collections;
using Unity.Jobs;

public class MyScheduledJob : MonoBehaviour
{
    // Create a native array of a single float to store the result.
    // Using a NativeArray is the only way you can get the results of the job, whether you're getting one value or an array of values.
    NativeArray<float> result;

    // Create a JobHandle for the job
    // 给任务创建一个任务处理
    JobHandle handle;

    // Set up the job
    // 创建一个任务
    public struct MyJob : IJob
    {
        public float a;
        public float b;
        public NativeArray<float> result;

        public void Execute()
        {
            result[0] = a + b;
        }
    }

    // Update is called once per frame
    // 每一帧更新
    void Update()
    {
        // Set up the job data 设置任务数据
        result = new NativeArray<float>(1, Allocator.TempJob);

        MyJob jobData = new MyJob
        {
            a = 10,
            b = 10,
            result = result
        };

        // Schedule the job 安排任务
        handle = jobData.Schedule();
    }

    private void LateUpdate()
    {
        // Sometime later in the frame, wait for the job to complete before accessing the results.
        // 稍后在框架中,等待作业完成后再访问结果。
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        // float aPlusB = result[0];

        // Free the memory allocated by the result array
        // 释放由结果数组分配的内存
        result.Dispose();
    }


}

 跟一个测试:

using UnityEngine;
using Unity.Collections;
using Unity.Jobs;

public class MyScheduledJob : MonoBehaviour
{
    // Create a native array of a single float to store the result.
    // Using a NativeArray is the only way you can get the results of the job, whether you're getting one value or an array of values.
    NativeArray<float> result;

    // Create a JobHandle for the job
    // 给任务创建一个任务处理
    JobHandle handle;

    // Set up the job
    // 创建一个任务
    public struct MyJob : IJob
    {
        public float a;
        public float b;
        public NativeArray<float> result;

        public void Execute()
        {
            result[0] = a + b;
        }
    }

    // Update is called once per frame
    // 每一帧更新
    void Update()
    {
        // Set up the job data 设置任务数据
        result = new NativeArray<float>(1, Allocator.TempJob);

        MyJob jobData = new MyJob
        {
            a = 10,
            b = 10,
            result = result
        };

        // Schedule the job 安排任务
        handle = jobData.Schedule();
    }

    private void LateUpdate()
    {
        // Sometime later in the frame, wait for the job to complete before accessing the results.
        // 稍后在框架中,等待作业完成后再访问结果。
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        // float aPlusB = result[0];

        // Free the memory allocated by the result array
        // 释放由结果数组分配的内存
        result.Dispose();
    }


}

 

意思大概是这样了,具体实践具体看!!

参考文档:

官方文档

Unity - Manual: Job system overview (unity3d.com)

Collections package | Collections | 2.2.1 (unity3d.com)

Unity - Manual: Thread safe types (unity3d.com)

这个博主系列的讲到了DOTS,可以看一下:

DOTS ECS_铸梦xy的博客-CSDN博客

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1494297.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【模型训练】-图形验证码识别

针对网站中的图形验证码图片&#xff0c;进行反向的内容识别&#xff0c;支持数字和字母&#xff0c;不区分大小写。 ​​​​​​​​​​​​​​数据集地址 数据格式如下&#xff1a; 1、依赖导入 import os import torch import torch.nn as nn import torch.optim as o…

GPT vs Gemini vs Claude 测试大比拼 到底谁是最强王者?

Anthropic发布的通用大语言模型Claude&#xff0c;在各项能力方面号称是全方面超越GPT&#xff0c;实测究竟如何呢&#xff1f;这次测试顺便把前段时间发布的Gemini拉上一起做对比&#xff01;主要是以一些有趣幽默的脑筋急转弯为题目&#xff0c;来看看不同大模型对此的反馈。…

闰年导致的哪些 Bug

每次闰年对程序员们都是一个挑战&#xff0c;平时运行好好的系统&#xff0c;在 02-29 这一天&#xff0c;好像就会有各种毛病。 虽然&#xff0c;提前一天&#xff0c;领导们都会提前给下面打招呼。但是&#xff0c;不可避免的&#xff0c;今天公司因为闰年还是有一些小故障。…

SpringBoot中集成LiteFlow(轻量、快速、稳定可编排的组件式规则引擎)实现复杂业务解耦、动态编排、高可扩展

场景 在业务开发中&#xff0c;经常遇到一些串行或者并行的业务流程问题&#xff0c;而业务之间不必存在相关性。 使用策略和模板模式的结合可以解决这个问题&#xff0c;但是使用编码的方式会使得文件太多, 在业务的部分环节可以这样操作&#xff0c;在项目角度就无法一眼洞…

Keil软件无法烧录程序的解决方案

1.由于单片机程序有些情况下出错&#xff0c;导致烧录进去单片机运行异常&#xff0c;无法烧录程序&#xff0c;但是Keil软件可以识别到SW Device器件&#xff0c;点击烧录程序提示no target connected连接。 解决方案: (1).点击魔术棒->debug->Settings&#xff0c;选择…

网络编程day6

1.思维导图 2.数据库操作的增、删、改完成。 #include<myhead.h> //定义新增员工信息函数 int do_add(sqlite3 *ppDb) {int numb;char name;double salary;printf("请输入要插入的信息&#xff1a;");scanf("%d%s%d\n",&numb,name,&salary)…

7大必备应用推荐,为你的 Nextcloud 实例增添更多效率功能

适用于 Linux 的开源云存储软件有很多&#xff0c;ownCloud、Seafile 和 Pydio 只是其中的几个。 不过&#xff0c;如果您非常重视安全问题&#xff0c;并希望完全掌管您的数据&#xff0c;可以选择​Nextcloud并将其安装到您的服务器上。​ Nextcloud 是一个基于 PHP 的开源安…

Pytest中实现自动生成测试用例脚本代码!

前言 在Python的测试框架中&#xff0c;我们通常会针对某个系统进行测试用例的维护&#xff0c;在对庞大系统进行用例维护时&#xff0c;往往会发现很多测试用例是差不多的&#xff0c;甚至大多数代码是一样的。 故为了提高我们测试用例维护的效率&#xff0c;在本文中&#…

Java常用笔试题,面试java对未来的规划

最重要的话 2021年&#xff0c;真希望行业能春暖花开。 去年由于疫情的影响&#xff0c;无数行业都受到了影响&#xff0c;互联网寒冬下&#xff0c;许多程序员被裁&#xff0c;大环境格外困难。 我被公司裁掉后&#xff0c;便着急地开始找工作&#xff0c;一次次地碰壁&#…

爬虫学习笔记-requests爬取王者荣耀皮肤图片

1.导入所需的包 import requests from lxml import etree import os from time import sleep 2.定义请求头 headers {User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36} 3.发送请求 # hero…

数据结构->链表分类与oj(题),带你提升代码好感

✅作者简介&#xff1a;大家好&#xff0c;我是橘橙黄又青&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;橘橙黄又青-CSDN博客 1.&#x1f34e;链表的分类 前面我们学过顺序表&#xff0c;顺序表问题&#xff1a; …

基于springboot实现的幼儿园管理系统

一、系统架构 前端&#xff1a;html | layui | jquery | css 后端&#xff1a;springboot | mybatis 环境&#xff1a;jdk1.8 | mysql | maven 二、代码及数据库 三、功能介绍 01. 登录页 02. 系统管理-用户管理 03. 系统管理-页面管理 04. 系统管理-角色管…

喜报|3DCAT成为国内首批适配Vision Pro内容开发者

近日&#xff0c;苹果在上海总部举办了国内首场 Apple Vision Pro 开发者实验室活动&#xff0c;3DCAT作为国内领先的实时渲染云平台参与了此次活动&#xff0c;成为国内首批适配 Vision Pro 的内容开发者之一。 Vision Pro是苹果于2023年6月发布的首个空间计算设备&#xff0…

【C++STL详解 —— string类】

【CSTL详解 —— string类】 CSTL详解 —— sring类一、string的定义方式二、string的插入三、string的拼接四、string的删除五、string的查找六、string的比较七、string的替换八、string的交换九、string的大小和容量十、string中元素的访问十一、string中运算符的使用十二、…

鸿蒙NEXT开发实战:【视频文件裁剪】

使用OpenHarmony系统提供的ffmpeg三方库的能力在系统中实现了音视频文件裁剪的功能&#xff0c;并通过NAPI提供给上层应用调用。 基础信息 视频文件裁剪 简介 在OpenHarmony系统整个框架中有很多子系统&#xff0c;其中多媒体子系统是OpenHarmony比较重要的一个子系统&#…

Java+SpringBoot+Vue+MySQL:农业管理新篇章

✍✍计算机毕业编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java、…

软件测试实战,Web项目网页bug定位详细分析总结(详全)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、前置条件 1&a…

9、Linux-安装JDK、Tomcat和MySql

目录 一、安装JDK 1、传输JDK文件&#xff08;.tar.gz&#xff09; 2、解压 3、备份环境变量 4、配置环境变量 5、重新加载环境变量 6、验证&#xff08;java -version&#xff09; 二、安装Tomcat 1、传输文件&#xff0c;解压到/usr/local 2、进入Tomcat的bin目录 …

数据库-ER图教程

一.什么是E-R图 E-R图全称&#xff1a;“Entity-Relationship Approach”&#xff0c;是一种“实体-联系”方法。 E-R图的优点&#xff1a; 1.自然地描述现实世界。 2.图形结构简单。 3.设计者和用户易理解。 4.是数据库设计的中间步骤&#xff0c;易于向数据模型转换。 …

44、网络编程/数据库相关操作练习20240306

一、代码实现数据库的创建&#xff08;员工信息表&#xff09;&#xff0c;并存储员工信息&#xff08;工号、姓名、薪资&#xff09;&#xff0c;能实现增加人员信息、删除人员信息、修改人员薪资操作。 代码&#xff1a; #include<myhead.h>int do_update(sqlite3 *p…